Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_future_trigger_actions.php
1 <?php
23 error_reporting(E_ALL);
24 if ((php_sapi_name() != 'cli')) {
25  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
26 }
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 if (empty($SYSTEM_ROOT)) {
30  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
31  exit();
32 }
33 
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
35  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
36  exit();
37 }
38 
39 require_once $SYSTEM_ROOT.'/core/include/init.inc';
40 
41 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
42 
43 // log in as root
44 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
45  echo "ERROR: Failed logging in as root user\n";
46  exit();
47 }
48 
49 $am = $GLOBALS['SQ_SYSTEM']->am;
50 $count = 0;
51 
52 // Upgrade each Trigger where Trigger Set Future Permissions or Status is used
53 echo 'Upgrading Future Trigger Actions...';
54 
55 // Grab each Trigger and its settings
56 $tm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('trigger_manager');
57 $triggers = $tm->getTriggerList();
58 
59 foreach ($triggers as $trigger_id => $trigger) {
60  $trigger_asset = $am->getAsset($tm->id.':'.$trigger['id']);
61  upgradeTrigger($trigger_asset);
62 }
63 
64 echo "\n- Done\n";
65 
66 
77 function upgradeTrigger(Trigger &$trigger)
78 {
79  $trigger_actions = $trigger->attr('actions');
80  $trigger_modified = FALSE;
81 
82  foreach ($trigger_actions as $key => &$trigger_action) {
83  $trigger_action_data =& $trigger_action['data'];
84 
85  // Set Future Permissions and Status triggers < v0.2 will not have an "offset_used" value
86  if ((($trigger_action['type'] == 'trigger_action_set_future_permissions') ||
87  ($trigger_action['type'] == 'trigger_action_set_future_status')) &&
88  (!isset($trigger_action_data['offset_used']))) {
89 
90  // Modify data associated with the Trigger Action (using a reference for ease and fun)
91 
92  // Add new "offset_used" value
93  $trigger_action_data['offset_used'] = FALSE;
94 
95  // Convert to new "by_attr_value" when_type and enable offset if one was specified
96  if (($trigger_action_data['when_type'] == 'attr_interval') || ($trigger_action_data['when_type'] == 'attr_exact')) {
97  $trigger_action_data['when_type'] = 'by_attr_value';
98  $trigger_action_data['offset_used'] = ($trigger_action_data['when_type'] == 'attr_interval');
99  }
100 
101  // Convert "explicit_interval" to "explicit_exact", as the offset is now handled separately
102  if ($trigger_action_data['when_type'] == 'explicit_interval') {
103  $trigger_action_data['when_type'] = 'explicit_exact';
104  $trigger_action_data['offset_used'] = TRUE;
105  }
106 
107  // Restock the main Trigger Actions array for this Trigger
108  $trigger_actions[$key] = $trigger_action;
109  $trigger_modified = TRUE;
110  }
111  }
112 
113  if ($trigger_modified) {
114  // Supply the new Trigger Actions values and save the Trigger if it was modified
115  echo "\n- Upgrading Trigger ".$trigger->id.'... ';
116 
117  // Ok we need the Attributes lock now...
118  $GLOBALS['SQ_SYSTEM']->acquireLock($trigger->id, 'attributes');
119 
120  $trigger->setAttrValue('actions', $trigger_actions);
121  $trigger->saveAttributes();
122 
123  $GLOBALS['SQ_SYSTEM']->releaseLock($trigger->id, 'attributes');
124 
125  echo 'done';
126  }
127 
128 }//end upgradeTrigger()
129 
130 
131 ?>