Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 
20 
30 {
31 
32 
37  function Trigger_Edit_Fns()
38  {
39  $this->Asset_Edit_Fns();
40 
41  // disable all static screens
42  $this->static_screens = Array();
43 
44  // enable just the details
45  $this->static_screens['details']['name'] = translate('details');
46  $this->static_screens['details']['force_unlock'] = FALSE;
47 
48  }//end constructor
49 
50 
61  function paintEvents(&$asset, &$o, $prefix)
62  {
63  $write_access = $asset->writeAccess('attributes');
64 
65  // the styles below can be moved to edit.css in the future
66  $o->openRaw();
67  ?><style type="text/css">
68  .sq-backend-triggers-component-header {
69  background-color: #666;
70  color: #fff;
71  padding: 2px;
72  }
73 
74  .sq-backend-triggers-component-global-options {
75  background-color: #eee;
76  padding: 2px;
77  border-bottom: 1px dotted black;
78  border-top: 1px dotted black;
79  }
80 
81  .sq-backend-triggers-component-type-info{
82  float: left;
83  font-weight: bold;
84  padding: 1px;
85  }
86 
87  .sq-backend-triggers-component-controls {
88  text-align: right;
89  }
90 
91  .sq-backend-triggers-component-block-wrapper {
92  padding: 1px;
93  }
94 
95  .sq-backend-triggers-component-body {
96  padding: 3px;
97  margin: 1px;
98  background-color: #fff;
99  }
100 
101  .sq-backend-triggers-new-component-selector {
102  font-weight: bold;
103  padding: 3px;
104  margin: 0 0 10px 0;
105  }
106 
107  .sq-backend-triggers-component-wrapper {
108  background-color: #eee;
109  border: 1px solid black;
110  margin: 10px 0;
111  }
112 
113  </style><script type="text/javascript">//<![CDATA[
117  function moveUp(div) {
118  previous = div.previousSibling;
119  if (previous == null) {
120  return;
121  }
122  while (previous.className != div.className) {
123  previous = previous.previousSibling;
124  if (previous == null) {
125  return;
126  }
127  }
128  div.parentNode.insertBefore(div, previous);
129  }
130 
134  function moveDown(div) {
135  next = div.nextSibling;
136  while (next.className != div.className) {
137  next = next.nextSibling;
138  if (next == null) {
139  return;
140  }
141  }
142  div.parentNode.insertBefore(next, div);
143  }
144 
148  function deleteDiv(div) {
149  if(confirm(js_translate('confirm_delete_component'))) {
150  div.parentNode.removeChild(div);
151  }
152  }
153 
166  componentOrderer = function(field_id, count)
167  {
168  this.field_id = field_id;
169 
170  this.values = Array();
171  this.keys = Array();
172 
173  count = parseInt(count);
174  for (i=0; i<count; i++) {
175  this.values.push(i);
176  this.keys.push(i);
177  }
178 
179  this.moveUp = function(value)
180  {
181  var key = this.keys[value];
182  if (key == 0) {
183  return;
184  }
185 
186  var new_key = key-1;
187 
188  this.swap(key, new_key);
189  this.update()
190  }
191 
192  this.moveDown = function(value)
193  {
194  var key = this.keys[value];
195  if (key == this.values.length-1) {
196  return;
197  }
198 
199  var new_key = key+1;
200 
201  this.swap(key, new_key);
202  this.update()
203  }
204 
205  this.remove = function(value)
206  {
207  var key = this.keys[value];
208  this.values.splice(key,1);
209  this.keys[value] = null;
210 
211  for (var i=0; i<this.values.length; i++) {
212  this.keys[this.values[i]] = i;
213  }
214  this.update()
215  }
216 
217  // this function assumes that keys are valid and can be swapped
218  this.swap = function(key1, key2)
219  {
220  var val1 = this.values[key1];
221  var val2 = this.values[key2];
222 
223  this.values[key1] = val2;
224  this.values[key2] = val1;
225 
226  this.keys[val1] = key2;
227  this.keys[val2] = key1;
228 
229  }
230 
231  this.update = function()
232  {
233  var joined = this.values.join();
234  document.getElementById(this.field_id).value = joined;
235  }
236 
237  }
238  //]]>
239  </script><?php
240 
241  $tm = $this->_getTriggerManager($asset);
242  if (is_null($tm)) return FALSE;
243 
244  // get event types
245  $event_type_list = $tm->_getEventList();
246  asort($event_type_list);
247 
248  $events = $asset->attr('events');
249 
250  // paint a tickbox list of all the events in the system
251  $events_selected = FALSE;
252  $event_prefix = $prefix.'[events]';
253  $event_count = count($event_type_list);
254 
255  $event_columns = 4;
256  $events_per_column = floor($event_count / $event_columns);
257  $extra_in_columns = $event_count % $event_columns;
258  $current_column = 0;
259  $current_event_no = 0;
260  $max_this_col = 0;
261 
262  echo '<div style="margin-bottom: 2ex;">';
263  echo '<table><tr>';
264  foreach ($event_type_list as $e_type => $e_name) {
265  $open = FALSE;
266  if ($current_event_no == $max_this_col) {
267  $current_column++;
268  $max_this_col = $max_this_col + $events_per_column;
269  if ($extra_in_columns) {
270  $extra_in_columns--;
271  $max_this_col++;
272  }
273  $open = TRUE;
274  }
275 
276  if (isset($events[$e_type])) {
277  $checked = TRUE;
278  } else {
279  $checked = FALSE;
280  }
281 
282  $events_selected = $checked || $events_selected;
283 
284  if ($open) {
285  if ($current_column && $current_event_no) {
286  echo '</td>';
287  }
288  echo '<td valign="top" style="padding-right: 3ex;">';
289  }
290 
291  $current_event_no++;
292 
293  if ($write_access) {
294  check_box($event_prefix.'[]', $e_type, $checked, '', 'id="'.$event_prefix.'_'.$e_type.'"');
295  label($e_name, $event_prefix.'_'.$e_type);
296  echo '<br />';
297  } else {
298  if ($checked) echo '<li>'.$e_name.'</li>';
299  }
300 
301  if ($current_event_no == $event_count) echo '</td>';
302 
303  }//end foreach
304 
305  echo '</tr></table>';
306  echo '</div>';
307 
308  if (!$write_access && !$events_selected) {
309  echo '<strong>'.translate('no_event_selected').'</strong>';
310  }
311 
312  $o->closeRaw();
313 
314  return $write_access;
315 
316  }//end paintEvents()
317 
318 
329  function processEvents(&$asset, &$o, $prefix)
330  {
331  $write_access = $asset->writeAccess('attributes');
332  if (!$write_access) return FALSE;
333 
334  $edit_data = array_get_index($_REQUEST, $prefix);
335 
336  // events
337  $raw_events = array_get_index($edit_data, 'events', Array());
338  $processed_events = Array();
339  foreach ($raw_events as $one_event_type) {
340  $processed_events[$one_event_type] = $one_event_type;
341  }
342 
343  $asset->setAttrValue('events', $processed_events);
344  return TRUE;
345 
346  }//end processEvents()
347 
348 
359  function paintConditions(&$asset, &$o, $prefix)
360  {
361  $write_access = $asset->writeAccess('attributes');
362 
363  $tm = $this->_getTriggerManager($asset);
364  if (is_null($tm)) return FALSE;
365 
366  // get event types
367  $condition_type_list = $tm->_getConditionList();
368  ksort($condition_type_list);
369 
370  $conditions = $asset->attr('conditions');
371 
372  $o->openRaw();
373  ?>
374  <div class="sq-backend-triggers-component-block-wrapper">
375  <div class="sq-backend-triggers-component-block">
376  <?php
377  // process the condition list to detemine which ones allow multiple instances
378  $multiple_status = Array();
379  $type_instance_count = Array();
380  foreach ($condition_type_list as $type => $name) {
381  $multiple_status[$type] = $tm->_isMultipleConditionAllowed($type);
382  $type_instance_count[$type] = 0;
383  }
384 
385  $condition_prefix = $prefix.'[conditions]';
386  $condition_order_prefix = $prefix.'[condition_order]';
387  $i = 0;
388 
389  foreach ($conditions as $condition) {
390  $type = $condition['type'];
391 
392  // check if condition is allowed to be displayed
393  // even if somehow it got into the settings, if it is not multiple,
394  // it will not be displayed
395  if (!$multiple_status[$type] && $type_instance_count[$type] != 0) {
396  continue;
397  }
398 
399  $type_instance_count[$type]++;
400 
401  $data = $condition['data'];
402  $inverse_condition = array_get_index($condition, 'inverse_condition', FALSE);
403 
404  $data_prefix = $condition_prefix.'['.$i.'][data]';
405  $type_prefix = $condition_prefix.'['.$i.'][type]';
406  $delete_prefix = $condition_prefix.'['.$i.'][delete]';
407  $inverse_condition_prefix = $condition_prefix.'['.$i.'][inverse_condition]';
408 
409  $interface = $tm->_getComponentInterface($type, $data, $data_prefix, $write_access, $asset, $i);
410  $type_name = $tm->_getComponentName($type);
411  ?>
412  <div class="sq-backend-triggers-component-wrapper">
413  <div class="sq-backend-triggers-component-header">
414  <div class="sq-backend-triggers-component-type-info">
415  <?php echo $type_name; ?>
416  </div>
417  <div class="sq-backend-triggers-component-controls">
418  <?php
419  if ($write_access) {
420  ?>
421  <input type="checkbox" name="<?php echo $delete_prefix; ?>" >
422  <?php echo label(translate('delete'), $delete_prefix); ?>
423  &nbsp;&nbsp;&nbsp;&nbsp;
424  <a href="#" onclick="moveUp(this.parentNode.parentNode.parentNode); conditionOrdererObject.moveUp(<?php echo $i; ?>); return false;"><script language="JavaScript" type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/up_arrow.png' ?>", "16", "16", "Move Up");</script></a>
425  <a href="#" onclick="moveDown(this.parentNode.parentNode.parentNode); conditionOrdererObject.moveDown(<?php echo $i; ?>); return false;"><script language="JavaScript" type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/down_arrow.png' ?>", "16", "16", "Move Down");</script></a>
426  <?php
427  } else {
428  echo '&nbsp;';
429  }
430  ?>
431  </div>
432  </div>
433  <div class="sq-backend-triggers-component-global-options">
434  <?php
435  if (!$write_access) {
436  $extras = 'disabled="disabled"';
437  } else {
438  $extras = '';
439  }
440 
441  check_box($inverse_condition_prefix, 1, $inverse_condition, NULL, $extras);
442  label(translate('inverse_condition'), $inverse_condition_prefix);
443  ?>
444  </div>
445  <div class="sq-backend-triggers-component-body">
446  <input type="hidden" name="<?php echo $type_prefix; ?>" value="<?php echo $type; ?>" />
447  <?php echo $interface; ?>
448  </div>
449  </div>
450  <?php
451  $i++;
452  }//end foreach
453  ?>
454  </div>
455  <?php if ($write_access) {
456  ?>
457  <script type="text/javascript">var conditionOrdererObject = new componentOrderer('conditionOrderer',<?php echo $i; ?>)</script>
458  <input type="hidden" id="conditionOrderer" name="<?php echo $condition_order_prefix; ?>" />
459  <!-- Adding a New Condition -->
460  <div class="sq-backend-triggers-new-component-selector">
461  <?php echo translate('add_new_condition_type'); ?>:
462  <?php
463  $select_list = Array(' '=>' ---- '.translate('select_to_add').' ---- ');
464  $removed = FALSE;
465  foreach ($condition_type_list as $type => $name) {
466  if (!$multiple_status[$type] && $type_instance_count[$type] != 0) {
467  $removed = TRUE;
468  continue;
469  }
470  $select_list[$type] = $name;
471  }
472 
473  combo_box($prefix.'[new_condition]', $select_list, FALSE, ' ');
474  if ($removed) {
475  echo ' <span class="sq-backend-warning">'.translate('some_conditions_can_only_be_added_once').'</span>';
476  }
477  ?>
478  </div>
479  <?php
480  } else {
481  if ($i < 0) echo translate('no_conditions_specified');
482  }
483  ?>
484  </div><?php
485  $o->closeRaw();
486 
487  return $write_access;
488 
489  }//end paintConditions()
490 
491 
502  function processConditions(&$asset, &$o, $prefix)
503  {
504  $write_access = $asset->writeAccess('attributes');
505  if (!$write_access) return FALSE;
506 
507  $tm = $this->_getTriggerManager($asset);
508  if (is_null($tm)) return FALSE;
509 
510  $edit_data = array_get_index($_REQUEST, $prefix);
511 
512  // conditions
513  $raw_conditions = array_get_index($edit_data, 'conditions');
514  $condition_order = array_get_index($edit_data, 'condition_order');
515  if (!empty($condition_order)) {
516  $order_array = explode(',', $condition_order);
517  foreach ($order_array as $position) {
518  $new_raw_conditions[] = $raw_conditions[$position];
519  }
520  $raw_conditions = $new_raw_conditions;
521  }
522  $new_condition_type = array_get_index($edit_data, 'new_condition');
523  if (!empty($new_condition_type)) {
524  $new_condition['type'] = $new_condition_type;
525  $new_condition['new'] = TRUE;
526 
527  $raw_conditions[] = $new_condition;
528  }
529  $processed_conditions = $this->_processRawConditionSet($tm, $raw_conditions);
530  $asset->setAttrValue('conditions', $processed_conditions);
531  return TRUE;
532 
533  }//end processConditions()
534 
535 
547  function _processRawConditionSet(&$tm, $raw_components)
548  {
549  $processed_components = Array();
550 
551  if (empty($raw_components)) {
552  return $processed_components;
553  }
554 
555  $i = 1;
556  foreach ($raw_components as $component) {
557  $error = FALSE;
558  if (isset($component['delete'])) continue;
559  $settings = Array();
560 
561  $type = array_get_index($component, 'type');
562  if (empty($type) || !$tm->_loadComponent($type)) {
563  // invalid type is silently ignored
564  continue;
565  }
566 
567  $data = array_get_index($component, 'data', Array());
568  if (!array_get_index($component, 'new', FALSE)) {
569  eval('$error = '.$type.'::processInterface($settings, $data);');
570  }
571 
572  if (FALSE !== $error) {
573  trigger_localised_error('CORE0217', E_USER_NOTICE, $i, $type, $error);
574  }
575 
576  $processed_components[] = Array(
577  'type' => $type,
578  'data' => $settings,
579  'inverse_condition' => isset($component['inverse_condition']),
580  );
581 
582  $i++;
583  }
584  return $processed_components;
585 
586  }//end _processRawConditionSet()
587 
588 
599  function paintActions(&$asset, &$o, $prefix)
600  {
601  $write_access = $asset->writeAccess('attributes');
602 
603  $tm = $this->_getTriggerManager($asset);
604  if (is_null($tm)) return FALSE;
605 
606  // get event types
607  $action_type_list = $tm->_getActionList();
608  ksort($action_type_list);
609 
610  $actions = $asset->attr('actions');
611 
612  $o->openRaw();
613  ?>
614  <div class="sq-backend-triggers-component-block-wrapper">
615  <div class="sq-backend-triggers-component-block">
616  <?php
617  $action_prefix = $prefix.'[actions]';
618  $action_order_prefix = $prefix.'[action_order]';
619  $i = 0;
620  foreach ($actions as $action) {
621  $this_prefix = $action_prefix.'['.$i.']';
622 
623  $data_prefix = $this_prefix.'[data]';
624  $type_prefix = $this_prefix.'[type]';
625  $delete_prefix = $this_prefix.'[delete]';
626  $ignore_permissions_prefix = $this_prefix.'[ignore_permissions]';
627  $not_required_prefix = $this_prefix.'[not_required]';
628 
629  $type = $action['type'];
630  $data = $action['data'];
631  $not_required = array_get_index($action, 'not_required', FALSE);
632  $ignore_permissions = array_get_index($action, 'ignore_permissions', FALSE);
633 
634  $interface = $tm->_getComponentInterface($type, $data, $data_prefix, $write_access, $asset, $i);
635  $type_name = $tm->_getComponentName($type);
636  ?>
637  <div class="sq-backend-triggers-component-wrapper">
638  <div class="sq-backend-triggers-component-header">
639  <div class="sq-backend-triggers-component-type-info">
640  <?php echo $type_name; ?>
641  </div>
642  <div class="sq-backend-triggers-component-controls">
643  <?php
644  if ($write_access) {
645  ?>
646  <input type="checkbox" name="<?php echo $delete_prefix; ?>"><?php echo label(translate('delete')); ?>
647  &nbsp;&nbsp;&nbsp;&nbsp;
648  <a href="#" onclick="moveUp(this.parentNode.parentNode.parentNode); actionOrdererObject.moveUp(<?php echo $i; ?>); return false;"><script language="JavaScript" type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/up_arrow.png' ?>", "16", "16", "Move Up");</script></a>
649  <a href="#" onclick="moveDown(this.parentNode.parentNode.parentNode); actionOrdererObject.moveDown(<?php echo $i; ?>); return false;"><script language="JavaScript" type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/down_arrow.png' ?>", "16", "16", "Move Down");</script></a>
650  <?php
651  } else {
652  echo '&nbsp;';
653  }
654  ?>
655  </div>
656  </div>
657  <div class="sq-backend-triggers-component-global-options">
658  <?php
659  if (!$write_access) {
660  $extras = 'disabled="disabled"';
661  } else {
662  $extras = '';
663  }
664 
665  check_box($ignore_permissions_prefix, 1, $ignore_permissions, NULL, $extras);
666  label(translate('ignore_permissions'), $ignore_permissions_prefix);
667 
668  check_box($not_required_prefix, 1, $not_required, NULL, $extras);
669  label(translate('not_required'), $not_required_prefix);
670  ?>
671  </div>
672  <div class="sq-backend-triggers-component-body">
673  <input type="hidden" name="<?php echo $type_prefix; ?>" value="<?php echo $type; ?>" />
674  <?php echo $interface; ?>
675  </div>
676  </div>
677  <?php
678  $i++;
679  }//end foreach
680  if ($write_access) {
681  ?>
682  <script type="text/javascript">var actionOrdererObject = new componentOrderer('actionOrderer',<?php echo $i; ?>)</script>
683  <input type="hidden" id="actionOrderer" name="<?php echo $action_order_prefix; ?>" />
684  <!-- Add a New Action -->
685  <div class="sq-backend-triggers-new-component-selector">
686  <?php echo translate('add_new_action_type'); ?>:
687  <select name="<?php echo $prefix; ?>[new_action]" >
688  <option value="" > ---- <?php echo translate('select_to_add'); ?> ---- </option>
689  <?php
690  foreach ($action_type_list as $a_type => $a_name) {
691  echo '<option value="'.$a_type.'" >'.$a_name.'</option>';
692  }
693  ?>
694  </select>
695  </div>
696  <?php
697  } else {
698  if ($i < 0) echo translate('no_action_specified');
699  }
700  ?>
701  </div>
702  </div><?php
703 
704  $o->closeRaw();
705 
706  return $write_access;
707 
708  }//end paintActions()
709 
710 
721  function processActions(&$asset, &$o, $prefix)
722  {
723  $write_access = $asset->writeAccess('attributes');
724  if (!$write_access) return FALSE;
725 
726  $tm = $this->_getTriggerManager($asset);
727  if (is_null($tm)) return FALSE;
728 
729  $edit_data = array_get_index($_REQUEST, $prefix);
730 
731  // actions
732  $raw_actions = array_get_index($edit_data, 'actions');
733  $action_order = array_get_index($edit_data, 'action_order');
734  if (!empty($action_order)) {
735  $order_array = explode(',', $action_order);
736  foreach ($order_array as $position) {
737  $new_raw_actions[] = $raw_actions[$position];
738  }
739  $raw_actions = $new_raw_actions;
740  }
741  $new_action_type = array_get_index($edit_data, 'new_action');
742  if (!empty($new_action_type)) {
743  $new_action['type'] = $new_action_type;
744  $new_action['new'] = TRUE;
745 
746  $raw_actions[] = $new_action;
747  }
748  $processed_actions = $this->_processRawActionSet($tm, $raw_actions);
749 
750  $asset->setAttrValue('actions', $processed_actions);
751  return TRUE;
752 
753  }//end processActions()
754 
755 
768  function _processRawActionSet(&$tm, $raw_components)
769  {
770  $processed_components = Array();
771 
772  if (empty($raw_components)) {
773  return $processed_components;
774  }
775 
776  $i=1;
777  foreach ($raw_components as $component) {
778  $error = FALSE;
779  if (isset($component['delete'])) continue;
780 
781  $settings = Array();
782 
783  $type = array_get_index($component, 'type');
784  if (empty($type) || !$tm->_loadComponent($type)) {
785  continue; // invalid type is silently ignored
786  }
787 
788 
789  $data = array_get_index($component, 'data', Array());
790  if (!array_get_index($component, 'new', FALSE)) {
791  eval('$error = '.$type.'::processInterface($settings, $data);');
792  }
793 
794  $processed_components[] = Array(
795  'type' => $type,
796  'data' => $settings,
797  'not_required' => isset($component['not_required']),
798  'ignore_permissions' => isset($component['ignore_permissions']),
799  );
800 
801  if (FALSE !== $error) {
802  trigger_localised_error('CORE0130', E_USER_NOTICE, $i, $type, $error);
803  }
804  $i++;
805  }
806  return $processed_components;
807 
808  }//end _processRawActionSet()
809 
810 
819  function &_getTriggerManager(&$asset)
820  {
821  $tm = NULL;
822 
823  $tmid = 0;
824  if (isset($asset->tmid) && !empty($asset->tmid)) {
825  // asset already has a trigger manager
826  $tmid = $asset->tmid;
827  } else {
828  // maybe being created under a trigger manager
829  $parent_assetid = array_get_index($_REQUEST, 'parent_assetid');
830  if (!is_null($parent_assetid)) {
831  // if it's already a trigger manager
832  if(is_numeric($parent_assetid)){
833  $tmid = $parent_assetid;
834  }
835  // if it's a trigger folder, we should get the real trigger manager
836  else {
837  $id_parts = explode(':', $parent_assetid);
838  $tmid = $id_parts[0];
839  }
840  }
841  }
842 
843  $tm = $GLOBALS['SQ_SYSTEM']->am->getAsset($tmid, 'trigger_manager');
844  if (!is_null($tm) && empty($asset->tmid)) {
845  $asset->setTriggerManager($tmid);
846  }
847  return $tm;
848 
849  }//end _getTriggerManager()
850 
851 
852 }//end class
853 
854 ?>