Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_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['major_assetid'])) {
59  // if no settings, fail
60  return FALSE;
61  }
62 
63  if (empty($state['asset'])) {
64  // grab the asset if assetid is given, but not the asset.
65  if (empty($state['assetid'])) {
66  return FALSE;
67  } else {
68  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
69  }
70  }
71 
72  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
73  if (is_null($cron_mgr)) return FALSE;
74 
75  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
76  $fs = new Cron_Job_Future_Lineage();
77 
78  // Set up the type of the link to make.
79  $link_info = $fs->attr('link_info');
80  $link_info['link_type'] = $settings['link_type'];
81 
82  // Calculate one-off date/time from asset if it was dynamically supplied
83  if (!empty($settings['attribute']) && !empty($settings['asset_type']) && ($state['asset'] instanceof $settings['asset_type'])) {
84  $attr = $state['asset']->getAttribute($settings['attribute']);
85 
86  // Perform some validation on the supplied date/time value
87  $datetime = $attr->value;
88 
89  // If the time is not set, assume midnight so the job will run on the first cron run of the day
90  $time = substr($datetime, 10);
91  if ($time == ' --:--:--') {
92  $time = ' 00:00:00';
93  $datetime = substr($datetime, 0, 10).$time;
94  }
95 
96  // Remove leading space from time component
97  $time = ltrim($time);
98 
99  $timestamp = mktime(
100  (int) substr($time,0,2),
101  (int) substr($time,3,2),
102  (int) substr($time,6,2),
103  (int) substr($datetime,5,2),
104  (int) substr($datetime,8,2),
105  (int) substr($datetime,0,4)
106  );
107  $parsed_datetime = date('Y-m-d H:i:s', $timestamp);
108  if (strcmp($parsed_datetime, $datetime) != 0) {
109  trigger_localised_error('SYS0311', E_USER_NOTICE, $datetime);
110  }
111 
112  // Set a one-off job to run based on the supplied attribute value
113  $fs->setAttrValue('when', 'OO='.$parsed_datetime);
114  } else {
115  $fs->setAttrValue('when', $settings['when']);
116  }
117 
118  $fs->setAttrValue('link_info', $link_info);
119  $fs->setAttrValue('delete_link_all', $settings['delete_link_all']);
120 
121  if ($GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
122  $user_for_status_change = $GLOBALS['SQ_SYSTEM']->user;
123  } else {
124  // not doing the whole "security" thing - so pretend we are root
125  $user_for_status_change = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
126  }
127 
128  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
129  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
130  $major_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['major_assetid']);
131  if ($fs->setAssetInLink($state['asset'], 'minor') && $fs->setAssetInLink($major_asset, 'major')) {
132  if ($cron_mgr->addJob($fs, $user_for_status_change)) {
133  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
134  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
135  return Array(
136  'jobid' => $fs->id,
137  'userid' => $user_for_status_change->id,
138  'major_assetid' => $settings['major_assetid'],
139  'when' => $fs->attr('when'),
140  );
141  }
142  }
143  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
144  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
145  return FALSE;
146 
147  }//end execute()
148 
149 
160  public static function getInterface($settings, $prefix, $write_access=FALSE)
161  {
162  // First, munge the prefix so that we can use the Cron Job interfaces to paint/process
163  $munge_prefix = str_replace('[', '_', $prefix);
164  $munge_prefix = str_replace(']', '', $munge_prefix);
165 
166  // set defaults
167  $settings['when'] = array_get_index($settings, 'when');
168  $settings['link_type'] = array_get_index($settings, 'link_type');
169  $settings['major_assetid'] = array_get_index($settings, 'major_assetid');
170  $settings['delete_link_all'] = array_get_index($settings, 'delete_link_all', FALSE);
171  $settings['prefix'] = $munge_prefix;
172 
173  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
174 
175  // Combo box for selecting the type of link.
176  $editable_link_types = Array(
177  SQ_LINK_TYPE_1 => link_type_name(SQ_LINK_TYPE_1),
178  SQ_LINK_TYPE_2 => link_type_name(SQ_LINK_TYPE_2),
179  );
180  ob_start();
181  combo_box($prefix.'[link_type]', $editable_link_types, FALSE, $settings['link_type']);
182  $output['new_link_type'] = ob_get_clean();
183 
184  // Asset finder for selecting the new parent.
185  ob_start();
186  asset_finder($prefix.'[major_assetid]', $settings['major_assetid'], Array(), 'sq_sidenav', FALSE, 'null', Array('clear'));
187  echo ' ';
188  check_box($prefix.'[delete_link_all]', 1, $settings['delete_link_all']);
189  echo ' ';
190  label(translate('cron_fl_delete_all_existing_links_long'), $prefix.'[delete_link_all]');
191  $output['new_link_parent'] = ob_get_clean();
192 
193  // We'll use the cron job we're going to create to print the 'when' part of the interface.
194  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
195  $temporary_cron_job = new Cron_Job_Future_Lineage();
196  $temporary_edit_fns = $temporary_cron_job->getEditFns();
197 
198  // Use the cron job edit functions to paint the 'when' box.
199  $o = new Backend_Outputter();
200  ob_start();
201  $temporary_edit_fns->_paintWhenBox($temporary_cron_job, $o, $munge_prefix, $settings['when']);
202 
203  echo '<br />or by the date/time value from the ';
204  $selected_type = array_get_index($settings, 'asset_type', '');
205  if ($write_access) {
206  asset_type_chooser($prefix.'[asset_type]', FALSE, Array($selected_type), TRUE);
207  } else {
208  echo '<b>'.$selected_type.'</b>';
209  }
210  echo ' attribute ';
211 
212  if ($selected_type == '') {
213  echo '<b>['.translate('asset_type_not_selected').']</b';
214  } else {
215  $attribute = array_get_index($settings, 'attribute', '');
216  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($selected_type);
217  if (empty($attrs)) {
218  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
219  } else {
220  if ($write_access) {
221  $attr_options = Array();
222  foreach ($attrs as $attr_name => $attr_type) {
223  if ($attr_type['type'] == 'datetime') {
224  $attr_options[$attr_name] = $attr_name;
225  }
226  }
227  if (count($attr_options) > 0) {
228  combo_box($prefix.'[attribute]', $attr_options, FALSE, $attribute);
229  } else {
230  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
231  }
232  } else {
233  echo '<b>'.$attribute.'</b>';
234  }
235  }
236  }
237 
238  $output['at'] = ob_get_clean();
239 
240  // Remember our munge-iness.
241  hidden_field($prefix.'[prefix]', $munge_prefix);
242 
243  if (!$write_access) return '';
244 
245  // Stick it all together in a table (yes, I know) so it looks pretty.
246  ob_start();
247  ?><table border="0" cellpadding="4" cellspacing="0"><?php
248  foreach ($output as $title => $text) {
249  ?><tr><td><strong><?php echo translate($title); ?></strong></td><td><?php echo $text; ?></td></tr><?php
250  }
251  ?></table><?php
252  return ob_get_clean();
253 
254  }//end getInterface()
255 
256 
268  public static function processInterface(&$settings, $request_data)
269  {
270  $munge_prefix = $request_data['prefix'];
271  $link_type = $request_data['link_type'];
272  $major_assetid = $request_data['major_assetid']['assetid'];
273 
274  $delete_link_all = FALSE;
275  if (isset($request_data['delete_link_all'])) {
276  $delete_link_all = TRUE;
277  }
278 
279  if (!$major_assetid) return 'New parent not specified';
280 
281  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_future_lineage');
282  $fs = new Cron_Job_Future_Lineage();
283  $edit_fns = $fs->getEditFns();
284 
285  $o = new Backend_Outputter();
286  $settings['when'] = $edit_fns->_processWhenBox($fs, $o, $munge_prefix);
287 
288  $type_code = array_get_index($request_data, 'asset_type', FALSE);
289  if ($type_code) {
290  $settings['asset_type'] = $type_code;
291  $settings['attribute'] = array_get_index($request_data, 'attribute', '');
292 
293  if (!empty($settings['attribute']) && !empty($settings['asset_type'])) {
294  $GLOBALS['SQ_SYSTEM']->am->includeAsset($settings['asset_type']);
295  $dummy_asset = new $settings['asset_type'];
296  $attr = $dummy_asset->getAttribute($settings['attribute']);
297  if (is_null($attr) || (get_class($attr) != 'Asset_Attribute_DateTime')) {
298  // selected asset type does not have an attribute with the selected name, so reset
299  $settings['attribute'] = '';
300  }
301  }
302  }
303 
304  $settings['prefix'] = $munge_prefix;
305 
306  $settings['link_type'] = $link_type;
307  $settings['major_assetid'] = $major_assetid;
308  $settings['delete_link_all'] = $delete_link_all;
309 
310  // Right, everything went well, so let's return FALSE?!!
311  return FALSE;
312 
313  }//end processInterface()
314 
315 
316 }//end class
317 
318 ?>