Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_delete_future_lineage.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 
32 {
33 
34 
55  public static function execute($settings, &$state)
56  {
57  // check settings, status
58  if (empty($settings['link_type']) || empty($settings['assetid']) || empty($settings['relation'])) {
59  // if no settings, fail
60  return FALSE;
61  }
62 
63  // Make sure the assets are assets ;)
64  if (empty($state['asset'])) {
65  // grab the asset if assetid is given, but not the asset.
66  if (empty($state['assetid'])) {
67  return FALSE;
68  } else {
69  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
70  }
71  }
72  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['assetid']);
73  if (is_null($asset)) return FALSE;
74 
75  // Check for the linkid (if no link id found, stop firing the trigger, nothing to do)
76  $am = $GLOBALS['SQ_SYSTEM']->am;
77  if ($settings['relation'] == 'minor') {
78  $major_asset = $state['asset'];
79  $minor_asset = $asset;
80  } else {
81  $major_asset = $asset;
82  $minor_asset = $state['asset'];
83  }
84  $links = $am->getLinks($major_asset->id, $settings['link_type'], $minor_asset->type(), TRUE, 'major');
85  $linkid = 0;
86  foreach ($links as $link) {
87  if (isset($link['minorid']) && ($link['minorid'] == $minor_asset->id)) {
88  // We only need one result ie. the result between the current asset and the trigger asset
89  $linkid = $link['linkid'];
90  break;
91  }
92  }
93 
94  if (!empty($linkid)) {
95  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
96  if (is_null($cron_mgr)) return FALSE;
97 
98  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
99  $fs = new Cron_Job_Future_Lineage();
100 
101  // Calculate one-off date/time from asset if it was dynamically supplied
102  if (!empty($settings['attribute']) && !empty($settings['asset_type']) && ($state['asset'] instanceof $settings['asset_type'])) {
103 
104  $attr = $state['asset']->getAttribute($settings['attribute']);
105 
106  // Perform some validation on the supplied date/time value
107  $datetime = $attr->value;
108 
109  // If the time is not set, assume midnight so the job will run on the first cron run of the day
110  $time = substr($datetime, 10);
111  if ($time == ' --:--:--') {
112  $time = ' 00:00:00';
113  $datetime = substr($datetime, 0, 10).$time;
114  }
115 
116  // Remove leading space from time component
117  $time = ltrim($time);
118 
119  $timestamp = mktime(
120  (int) substr($time,0,2),
121  (int) substr($time,3,2),
122  (int) substr($time,6,2),
123  (int) substr($datetime,5,2),
124  (int) substr($datetime,8,2),
125  (int) substr($datetime,0,4)
126  );
127  $parsed_datetime = date('Y-m-d H:i:s', $timestamp);
128  if (strcmp($parsed_datetime, $datetime) != 0) {
129  trigger_localised_error('SYS0311', E_USER_NOTICE, $datetime);
130  }
131 
132  // Set a one-off job to run based on the supplied attribute value
133  $fs->setAttrValue('when', 'OO='.$parsed_datetime);
134  } else {
135  $fs->setAttrValue('when', $settings['when']);
136  }
137 
138  // Set the linkid to the delete_linkid attribute
139  $fs->setAttrValue('delete_linkid', $linkid);
140 
141  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
142  $user_for_status_change = $GLOBALS['SQ_SYSTEM']->user;
143  } else {
144  // not doing the whole "security" thing - so pretend we are root
145  $user_for_status_change = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
146  }
147 
148  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
149  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
150  if ($fs->setAssetInLink($minor_asset, 'minor')) {
151  if ($cron_mgr->addJob($fs, $user_for_status_change)) {
152  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
153  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
154  return Array(
155  'jobid' => $fs->id,
156  'userid' => $user_for_status_change->id,
157  'major_assetid' => $settings['assetid'],
158  'when' => $fs->attr('when'),
159  );
160  }
161  }
162  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
163  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
164  return FALSE;
165  } else {
166  // Return - nothing to do
167  return Array();
168  }//end if
169 
170  }//end execute()
171 
172 
183  public static function getInterface($settings, $prefix, $write_access=FALSE)
184  {
185  // First, munge the prefix so that we can use the Cron Job interfaces to paint/process
186  $munge_prefix = str_replace('[', '_', $prefix);
187  $munge_prefix = str_replace(']', '', $munge_prefix);
188 
189  // set defaults
190  $settings['when'] = array_get_index($settings, 'when');
191  $settings['link_type'] = array_get_index($settings, 'link_type');
192  $settings['assetid'] = array_get_index($settings, 'assetid');
193  $settings['relation'] = array_get_index($settings, 'relation', 'major');
194  $settings['asset_type'] = array_get_index($settings, 'asset_type');
195  $settings['prefix'] = $munge_prefix;
196 
197  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
198 
199  // Combo box for selecting the type of link.
200  $editable_link_types = Array(
201  SQ_LINK_TYPE_1 => link_type_name(SQ_LINK_TYPE_1),
202  SQ_LINK_TYPE_2 => link_type_name(SQ_LINK_TYPE_2),
203  );
204  $relationships = Array(
205  'major' => 'Parent',
206  'minor' => 'Child',
207  );
208  $interface = '';
209  ob_start();
210 
211  // Print the interface
212  hidden_field($prefix.'[prefix]', $munge_prefix);
213  ?>
214  <table border="0" cellpadding="4" cellspacing="0">
215  <?php
216 
217  // Link Type Interface
218  ob_start();
219  if ($write_access) {
220  combo_box($prefix.'[link_type]', $editable_link_types, FALSE, $settings['link_type']);
221  } else {
222  if (isset($settings['link_type']) && !empty($settings['link_type'])) {
223  echo $editable_link_types[$settings['link_type']];
224  } else {
225  echo $editable_link_types[SQ_LINK_TYPE_1];
226  }
227  }
228  $link_type_interface = ob_get_clean();
229  ?>
230  <tr><td><strong><?php echo translate('link_type'); ?></strong></td><td><?php echo $link_type_interface; ?></td></tr>
231  <?php
232 
233  // Asset Interface
234  ob_start();
235  if ($write_access) {
236  asset_finder($prefix.'[assetid]', $settings['assetid'], Array(), 'sq_sidenav', FALSE, 'null', Array('clear'));
237  } else {
238  if (isset($settings['assetid']) && !empty($settings['assetid'])) {
239  echo get_asset_tag_line($settings['assetid']);
240  } else {
241  echo '<em>'.translate('no_asset_selected').'</em>';
242  }
243  }
244  echo ' is the ';
245  if ($write_access) {
246  combo_box($prefix.'[relation]', $relationships, FALSE, $settings['relation']);
247  } else {
248  if (isset($settings['relation']) && !empty($settings['relation'])) {
249  echo $relationships[$settings['relation']];
250  } else {
251  echo $relationships['major'];
252  }
253  }
254  echo ' of the Current Asset';
255  $asset_interface = ob_get_clean();
256  ?>
257  <tr><td><strong><?php echo translate('asset'); ?></strong></td><td><?php echo $asset_interface; ?></td></tr>
258  <?php
259 
260  // Future Interface
261  // We'll use the cron job we're going to create to print the 'when' part of the interface.
262  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
263  $temporary_cron_job = new Cron_Job_Future_Lineage();
264  $temporary_edit_fns = $temporary_cron_job->getEditFns();
265 
266  if ($write_access) {
267  // Use the cron job edit functions to paint the 'when' box.
268  $o = new Backend_Outputter();
269  ob_start();
270  $temporary_edit_fns->_paintWhenBox($temporary_cron_job, $o, $munge_prefix, $settings['when']);
271 
272  echo '<br />or by the date/time value from the ';
273  $selected_type = array_get_index($settings, 'asset_type', '');
274  if ($write_access) {
275  asset_type_chooser($prefix.'[asset_type]', FALSE, Array($selected_type), TRUE);
276  } else {
277  echo '<b>'.$selected_type.'</b>';
278  }
279  echo ' attribute ';
280 
281  if ($selected_type == '') {
282  echo '<b>['.translate('asset_type_not_selected').']</b';
283  } else {
284  $attribute = array_get_index($settings, 'attribute', '');
285  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($selected_type);
286  if (empty($attrs)) {
287  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
288  } else {
289  if ($write_access) {
290  $attr_options = Array();
291  foreach ($attrs as $attr_name => $attr_type) {
292  if ($attr_type['type'] == 'datetime') {
293  $attr_options[$attr_name] = $attr_name;
294  }
295  }
296  if (count($attr_options) > 0) {
297  combo_box($prefix.'[attribute]', $attr_options, FALSE, $attribute);
298  } else {
299  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
300  }
301  } else {
302  echo '<b>'.$attribute.'</b>';
303  }
304  }
305  }
306  $future_interface = ob_get_clean();
307  ?>
308  <tr><td><strong><?php echo translate('at'); ?></strong></td><td><?php echo $future_interface; ?></td></tr>
309  <?php
310  }//end if
311  ?>
312  </table>
313  <?php
314 
315  $interface = ob_get_clean();
316 
317  return $interface;
318 
319  }//end getInterface()
320 
321 
333  public static function processInterface(&$settings, $request_data)
334  {
335  $munge_prefix = $request_data['prefix'];
336  $link_type = $request_data['link_type'];
337  $assetid = $request_data['assetid']['assetid'];
338  $relation = $request_data['relation'];
339 
340  if (!$assetid) return 'Asset not specified';
341 
342  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
343  $fs = new Cron_Job_Future_Lineage();
344  $edit_fns = $fs->getEditFns();
345 
346  $o = new Backend_Outputter();
347  $settings['when'] = $edit_fns->_processWhenBox($fs, $o, $munge_prefix);
348 
349  $type_code = array_get_index($request_data, 'asset_type', FALSE);
350  if ($type_code) {
351  $settings['asset_type'] = $type_code;
352  $settings['attribute'] = array_get_index($request_data, 'attribute', '');
353 
354  if (!empty($settings['attribute']) && !empty($settings['asset_type'])) {
355  $GLOBALS['SQ_SYSTEM']->am->includeAsset($settings['asset_type']);
356  $dummy_asset = new $settings['asset_type'];
357  $attr = $dummy_asset->getAttribute($settings['attribute']);
358  if (is_null($attr) || (get_class($attr) != 'Asset_Attribute_DateTime')) {
359  // selected asset type does not have an attribute with the selected name, so reset
360  $settings['attribute'] = '';
361  }
362  }
363  }
364 
365  $settings['prefix'] = $munge_prefix;
366 
367  $settings['link_type'] = $link_type;
368  $settings['assetid'] = $assetid;
369  $settings['relation'] = $relation;
370 
371  // Right, everything went well, so let's return FALSE?!!
372  return FALSE;
373 
374  }//end processInterface()
375 
376 
377 }//end class
378 
379 ?>