17 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action_types/trigger_action_clear_cache/trigger_action_clear_cache.inc';
63 public static function execute($settings, &$state)
65 $use_alternative = array_get_index($settings,
'use_alternative', FALSE);
66 $alt_dsn = array_get_index($settings,
'alt_dsn',
'');
67 $alt_dsn_user = array_get_index($settings,
'alt_dsn_user',
'');
68 $alt_dsn_pwd = array_get_index($settings,
'alt_dsn_pwd',
'');
69 $alt_dsn_type = array_get_index($settings,
'alt_dsn_type',
'');
71 $alt_dsn_array = Array(
73 'user' => $alt_dsn_user,
74 'password' => $alt_dsn_pwd,
75 'type' => $alt_dsn_type,
79 if ($use_alternative && $alt_dsn ==
'') {
83 if (empty($state[
'asset'])) {
85 if (empty($state[
'assetid'])) {
88 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
93 if (!$use_alternative) {
94 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'dbcache');
95 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
98 $db = Trigger_Action_Set_Cache_Expiry::_connectAltDSN($alt_dsn_array);
99 if (is_null($db))
return FALSE;
103 if (!isset($GLOBALS[
'SQ_SYSTEM']->tm) || !($GLOBALS[
'SQ_SYSTEM']->tm instanceof
Transaction_Manager)) {
104 require_once SQ_INCLUDE_PATH.
'/transaction_manager.inc';
105 $GLOBALS[
'SQ_SYSTEM']->tm =
new Transaction_Manager();
107 $GLOBALS[
'SQ_SYSTEM']->tm->begin(
'dbalt');
111 $todo_assetids = Array();
112 if ($settings[
'use_current_asset']) {
113 $todo_assetids = Trigger_Action_Set_Cache_Expiry::_getAssetIDs($state[
'assetid'], $settings);
115 foreach ($settings[
'selected_assets'] as $id) {
116 $todo_assetids = array_merge($todo_assetids, Trigger_Action_Set_Cache_Expiry::_getAssetIDs($id, $settings));
120 if (!empty($todo_assetids)) {
123 $value = array_get_index($settings,
'value', 1);
124 $unit = array_get_index($settings,
'unit', 60);
125 $expiry = ts_iso8601(time() + ($value * $unit));
129 'expires' => $expiry,
130 'assetids' => $todo_assetids,
133 }
catch (Exception $e) {
134 if (!$use_alternative) {
135 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
136 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
138 $GLOBALS[
'SQ_SYSTEM']->tm->rollback(
'dbalt');
145 if (!$use_alternative) {
146 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
147 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
150 $GLOBALS[
'SQ_SYSTEM']->tm->commit(
'dbalt');
155 'assetid' => $state[
'asset']->
id,
171 public static function getInterface($settings, $prefix, $write_access=FALSE)
173 $output = parent::getInterface($settings, $prefix, $write_access);
174 $output .= Trigger_Action_Set_Cache_Expiry::_paintTimeUnit($settings, $prefix, $write_access);
176 $output .= Trigger_Action_Set_Cache_Expiry::_paintDSN($settings, $prefix, $write_access);
196 parent::processInterface($settings, $request_data);
198 $settings[
'value'] = array_get_index($request_data,
'value', 1);
199 $settings[
'unit'] = array_get_index($request_data,
'unit', 60);
201 $settings[
'use_alternative'] = array_get_index($request_data,
'use_alternative', FALSE);
202 $settings[
'alt_dsn'] = array_get_index($request_data,
'alt_dsn',
'');
203 $settings[
'alt_dsn_user'] = array_get_index($request_data,
'alt_dsn_user',
'');
204 $settings[
'alt_dsn_pwd'] = array_get_index($request_data,
'alt_dsn_pwd',
'');
205 $settings[
'alt_dsn_type'] = array_get_index($request_data,
'alt_dsn_type',
'');
222 private static function _paintTimeUnit($settings, $prefix, $write_access=FALSE)
225 $value = array_get_index($settings,
'value', 1);
226 $unit = array_get_index($settings,
'unit', 60);
229 60 => translate(
'minute'),
230 3600 => translate(
'hour'),
231 86400 => translate(
'day'),
236 int_text_box($prefix.
'[value]', $value, FALSE);
237 $value = ob_get_contents();
240 combo_box($prefix.
'[unit]', $time_unit, FALSE, $unit);
241 $unit = ob_get_contents();
243 $output = ($value.
' '.$unit);
246 $unit = $time_unit[$unit];
247 $output = ($value.
' '.$unit.
'(s)');
250 return translate(
'trigger_set_cache_expiry', $output);
265 private static function _paintDSN($settings, $prefix, $write_access=FALSE)
268 $use_alternative = array_get_index($settings,
'use_alternative', FALSE);
269 $alt_dsn = array_get_index($settings,
'alt_dsn',
'');
270 $alt_dsn_user = array_get_index($settings,
'alt_dsn_user',
'');
271 $alt_dsn_pwd = array_get_index($settings,
'alt_dsn_pwd',
'');
272 $alt_dsn_type = array_get_index($settings,
'alt_dsn_type',
'');
276 <script language=
"JavaScript" type=
"text/javascript">
277 function enableAltDSN(value) {
278 var prefix =
'<?php echo $prefix; ?>';
279 var elems =
new Array();
280 elems[0] =
'[alt_dsn]';
281 elems[1] =
'[alt_dsn_user]';
282 elems[2] =
'[alt_dsn_pwd]';
283 elems[3] =
'[alt_dsn_type]';
284 for (var x in elems) {
285 var
id = prefix + elems[x];
286 var element = document.getElementById(
id);
288 element.disabled =
'disabled';
291 element.disabled =
'';
299 check_box($prefix.
'[use_alternative]', TRUE, $use_alternative,
'enableAltDSN(this.checked)');
300 $default = ob_get_contents();
303 $disabled = (!$use_alternative) ?
'disabled="disabled"' :
'';
307 <td valign=
"top"><b>
DSN</b></td>
308 <td valign=
"top" style=
"padding-bottom: 10px;">
309 <?php text_box($prefix.
'[alt_dsn]', $alt_dsn, 60,
'', FALSE, $disabled); ?>
313 <td valign=
"top"><b>user</b></td>
314 <td valign=
"top" style=
"padding-bottom: 10px;">
315 <?php text_box($prefix.
'[alt_dsn_user]', $alt_dsn_user, 30,
'', FALSE, $disabled); ?>
319 <td valign=
"top"><b>password</b></td>
320 <td valign=
"top" style=
"padding-bottom: 10px;">
321 <?php text_box($prefix.
'[alt_dsn_pwd]', $alt_dsn_pwd, 30,
'', FALSE, $disabled); ?>
325 <td valign=
"top"><b>
type</b></td>
326 <td valign=
"top" style=
"padding-bottom: 10px;">
327 <?php text_box($prefix.
'[alt_dsn_type]', $alt_dsn_type, 30,
'', FALSE, $disabled); ?>
332 $alt_dsn_part = ob_get_contents();
334 $output = translate(
'trigger_set_cache_expiry_default_db',
'<b>dbcache</b><br />Use Alternative? '.$default);
335 $output .=
'<br />'.translate(
'trigger_set_cache_expiry_alt_dsn', $alt_dsn_part);
338 $output =
'Cache DSN: ';
339 $output .=
'<b>'.((!$use_alternative) ?
'default dbcache DSN' : $alt_dsn).
'</b>';
356 private static function &_connectAltDSN($dsn)
361 if ($dsn[
'type'] ===
'oci') {
368 $db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
371 $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
374 $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, TRUE);
377 }
catch (Exception $e) {
379 header(
'HTTP/1.0 500 Internal Server Error');
381 throw new Exception(
'Could not create database connection: '.$e->getMessage());
398 private static function _getAssetIDs($id, $settings)
401 $type_code = empty($settings[
'asset_types']) ?
'' : $settings[
'asset_types'];
402 $todo_assetids = Array();
404 switch ($settings[
'level']) {
406 $todo_assetids = Array($id);
409 $todo_assetids = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getDependantChildren($id, $type_code));
410 $todo_assetids[] = $id;
413 $todo_assetids = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getChildren($id, $type_code));
414 $todo_assetids[] = $id;
418 return $todo_assetids;