Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_section_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 require_once SQ_SYSTEM_ROOT.'/core/attributes/option_list/option_list.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 
33 {
34 
35 
41  {
42  $this->Asset_Edit_Fns();
43  $this->static_screens['details']['force_unlock'] = FALSE;
44 
45  }//end constructor
46 
47 
58  function paintQuestionLinks(&$asset, &$o, $prefix)
59  {
60 
61  $am = $GLOBALS['SQ_SYSTEM']->am;
62  $admin_access = $asset->writeAccess('attributes');
63  $prefix = $asset->getPrefix();
64  $questions = $asset->getQuestions();
65 
66  // include script for re-ordering questions
67  ?>
68  <script type="text/javascript">
69  function sortOrderMoveDown(currentOrder) {
70  if (!document.getElementById) {
71  alert('<?php echo translate('cms_form_no_getelementbyid') ?>');
72  return;
73  }
74  // move a question up a row
75  var form = document.getElementById('main_form');
76 
77  var currentElement = document.getElementById('<?php echo $prefix ?>_order_o' + currentOrder);
78  var nextElement = document.getElementById('<?php echo $prefix ?>_order_o' + (currentOrder + 1));
79 
80  // if there is no next element, then this is the last one; we shouldn't be running
81  if (!nextElement) return;
82 
83  if (!currentElement.innerHTML) {
84  alert('<?php echo translate('cms_form_no_innerhtml') ?>');
85  return;
86  }
87 
88  // switch the question names
89  var temp = currentElement.innerHTML;
90  currentElement.innerHTML = nextElement.innerHTML;
91  nextElement.innerHTML = temp;
92 
93  // switch the question types
94  var currentElement = document.getElementById('<?php echo $prefix ?>_order_t' + currentOrder);
95  var nextElement = document.getElementById('<?php echo $prefix ?>_order_t' + (currentOrder + 1));
96 
97  var temp = currentElement.innerHTML;
98  currentElement.innerHTML = nextElement.innerHTML;
99  nextElement.innerHTML = temp;
100 
101 
102 
103  // switch the 'checked for deletion' parameters
104  temp = form.elements['<?php echo $prefix ?>_order[delete][' + (currentOrder+1) + ']'].checked;
105  form.elements['<?php echo $prefix ?>_order[delete][' + (currentOrder+1) + ']'].checked = form.elements['<?php echo $prefix ?>_order[delete][' + currentOrder + ']'].checked;
106  form.elements['<?php echo $prefix ?>_order[delete][' + currentOrder + ']'].checked = temp;
107 
108  // switch the reorder values
109  temp = form.elements['<?php echo $prefix ?>_order[reorder][' + (currentOrder+1) + ']'].value;
110  form.elements['<?php echo $prefix ?>_order[reorder][' + (currentOrder+1) + ']'].value = form.elements['<?php echo $prefix ?>_order[reorder][' + currentOrder + ']'].value;
111  form.elements['<?php echo $prefix ?>_order[reorder][' + currentOrder + ']'].value = temp;
112 
113  }
114 
115  function sortOrderMoveUp(currentOrder) {
116  // move a question up a row
117  if (!document.getElementById) {
118  alert('<?php echo translate('cms_form_no_getelementbyid') ?>');
119  return;
120  }
121 
122  if (currentOrder == 0) return;
123 
124  var form = document.getElementById('main_form');
125 
126  var currentElement = document.getElementById('<?php echo $prefix ?>_order_o' + currentOrder);
127  var prevElement = document.getElementById('<?php echo $prefix ?>_order_o' + (currentOrder - 1));
128 
129  if (!currentElement.innerHTML) {
130  alert('<?php echo translate('cms_form_no_innerhtml') ?>');
131  return;
132  }
133 
134  // switch the question names
135  var temp = currentElement.innerHTML;
136  currentElement.innerHTML = prevElement.innerHTML;
137  prevElement.innerHTML = temp;
138 
139  // switch the question types
140  var currentElement = document.getElementById('<?php echo $prefix ?>_order_t' + currentOrder);
141  var prevElement = document.getElementById('<?php echo $prefix ?>_order_t' + (currentOrder - 1));
142 
143  var temp = currentElement.innerHTML;
144  currentElement.innerHTML = prevElement.innerHTML;
145  prevElement.innerHTML = temp;
146 
147  // switch the delete checkboxes
148  temp = form.elements['<?php echo $prefix ?>_order[delete][' + (currentOrder-1) + ']'].checked;
149  form.elements['<?php echo $prefix ?>_order[delete][' + (currentOrder-1) + ']'].checked = form.elements['<?php echo $prefix ?>_order[delete][' + currentOrder + ']'].checked;
150  form.elements['<?php echo $prefix ?>_order[delete][' + currentOrder + ']'].checked = temp;
151 
152  // switch the includes checkboxes
153  temp = form.elements['<?php echo $prefix ?>_order[reorder][' + (currentOrder-1) + ']'].value;
154  form.elements['<?php echo $prefix ?>_order[reorder][' + (currentOrder-1) + ']'].value = form.elements['<?php echo $prefix ?>_order[reorder][' + currentOrder + ']'].value;
155  form.elements['<?php echo $prefix ?>_order[reorder][' + currentOrder + ']'].value = temp;
156 
157  }
158 
159  function changeAllDelStatuses(status)
160  {
161  var inputs = document.getElementsByTagName('INPUT');
162  for (var i = 0; i < inputs.length; i++) {
163  if (inputs[i].id.indexOf("_order[delete][") < 0) continue;
164  inputs[i].checked = status;
165  }
166  }
167  </script>
168  <table class="sq-backend-table">
169  <tr>
170  <td class="sq-backend-table-header">
171  <?php echo translate('question') ?>
172  </td>
173  <td class="sq-backend-table-header">
174  <?php echo translate('type') ?>
175  </td>
176  <?php
177  if ($admin_access) {
178  ?>
179  <td align="center" width="100" class="sq-backend-table-header" style="font-weight: bold;"><?php
180  if (!empty($questions)) {
181  $delete_all = check_box($prefix.'_order[delete]', '1', FALSE, 'changeAllDelStatuses(this.checked)');
182  } else {
183  $delete_all = '';
184  }
185  echo translate('delete_question').$delete_all; ?>
186  </td>
187  <td align="center" width="100" class="sq-backend-table-header" style="font-weight: bold;"><?php echo translate('move') ?></td>
188  <?php
189  }
190  ?>
191  </tr>
192  <?php
193  $sort_order = $asset->attr('sort_order');
194 
195  if (!empty($sort_order)) {
196  // sort order is based upon shadow asset id (eg. '87:q1'), not just question id - this
197  // will allow sections to be sorted in later
198  $i = 0;
199  foreach ($sort_order as $sort_id => $assetid) {
200  $q = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
201  if (is_null($q)) continue;
202  $href = $q->getBackendHref('details');
203  ?>
204  <tr>
205  <td class="sq-backend-table-cell">
206  <span name="<?php echo $prefix.'_order_o'.$sort_id ?>" id="<?php echo $prefix.'_order_o'.$sort_id ?>"><?php echo get_asset_tag_line($assetid, 'details') ?></span><?php hidden_field($prefix.'_order[reorder]['.$sort_id.']', $assetid); ?>
207  </td>
208  <td class="sq-backend-table-cell">
209  <span name="<?php echo $prefix.'_order_t'.$sort_id ?>" id="<?php echo $prefix.'_order_t'.$sort_id ?>"><?php
210  $q_type = str_replace('Form_Question_Type', '', get_class($q));
211  $q_type = str_replace('_', ' ', $q_type);
212  $q_type = trim(ucwords($q_type));
213  echo $q_type;
214  ?></span>
215  </td>
216  <?php
217  if ($admin_access) {
218  ?>
219  <td align="center" width="100" class="sq-backend-table-cell">
220  <?php
221  check_box($prefix.'_order[delete]['.$sort_id.']');
222  ?>
223  </td>
224  <td align="center" width="100" class="sq-backend-table-cell">
225  <?php
226  if ($i != 0) {
227  ?><a href="#" onclick="sortOrderMoveUp(<?php echo $sort_id ?>); return false;"><script type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/up_arrow.png' ?>", "16", "16", "Move Up");</script></a><?php
228  }
229 
230  if ($i != count($sort_order) -1) {
231  ?><a href="#" onclick="sortOrderMoveDown(<?php echo $sort_id ?>); return false;"><img src="<?php echo sq_web_path('lib').'/web/images/icons/down_arrow.png' ?>" width="16" height="16" border="0"alt="Move Down" /></a><?php
232  }
233  ?>
234  </td>
235  <?php
236  }//end if admin access
237  ?>
238  </tr>
239  <?php
240  $i++;
241  }//end foreach data
242  }//end if
243  ?>
244  </table>
245  <?php
246  return TRUE;
247 
248  }//end paintQuestionLinks()
249 
250 
261  function processQuestionLinks(&$asset, &$o, $prefix)
262  {
263  $prefix = $asset->getPrefix();
264 
265  if (isset($_POST[$prefix.'_order']['reorder'])) {
266  $asset->setAttrValue('sort_order', array_values($_POST[$prefix.'_order']['reorder']));
267  $asset->saveAttributes();
268  }
269 
270  if (isset($_POST[$prefix.'_order']['delete'])) {
271  foreach (array_keys($_POST[$prefix.'_order']['delete']) as $sort_order) {
272  $question = $asset->getQuestionByOrder($sort_order);
273  $asset->deleteQuestion($question);
274  }
275 
276  // bugfix 2461: make sure there is not 'gap' between question index
277  $asset->setAttrValue('sort_order', array_values($asset->attr('sort_order')));
278  $asset->saveAttributes();
279  }
280 
281  // regen the content file
282  $asset->_updated();
283 
284  return TRUE;
285 
286  }//end processQuestionLinks()
287 
288 
299  function paintAddQuestions(&$asset, &$o, $prefix)
300  {
301  if (!$asset->writeAccess('attributes')) return FALSE;
302 
303  $am = $GLOBALS['SQ_SYSTEM']->am;
304  $question_types = $am->getTypeDescendants('form_question');
305  $questions = Array('' => '-- Choose Type --');
306 
307  foreach ($question_types as $question) {
308  $q = str_replace('form_question_type', '', $question);
309  $q = str_replace('_', ' ', $q);
310  $q = trim(ucwords($q));
311  $questions[$question] = $q;
312  }
313 
314  combo_box('question_type', $questions, FALSE, '');
315  echo '&nbsp;';
316 
317  $num = Array();
318  for ($i = 1; $i <= 10; $i++) {
319  $num[$i] = $i;
320  }
321  combo_box('num_questions', $num, FALSE, '');
322  return TRUE;
323 
324  }//end paintAddQuestions()
325 
326 
337  function processAddQuestions(&$asset, &$o, $prefix)
338  {
339  $type = (isset($_POST['question_type'])) ? $_POST['question_type'] : '';
340  $number = (isset($_POST['num_questions'])) ? $_POST['num_questions'] : '';
341 
342  // get the number of questions in the system allready
343  // so when we create the new ones, we can give them an
344  // appropriate name
345 
346  $am = $GLOBALS['SQ_SYSTEM']->am;
347  $curr_question_count = $asset->getQuestionCount() + 1;
348 
349  if ($type && $number) {
350  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
351  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
352 
353  // create some questions
354  for ($i = 0; $i < $number; $i++) {
355  if (!$asset->attachQuestion($type)) return FALSE;
356 
357  $curr_question_count++;
358  }//end for
359  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
360  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
361  }//end if
362 
363  return TRUE;
364 
365  }//end processAddQuestions()
366 
367 
378  function paintNestedSections(Form_Section $asset, Backend_Outputter $o, $prefix)
379  {
380  $write_access = $asset->writeAccess('links');
381  $section_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_ALL, 'form_section', FALSE);
382 
383  if (empty($section_links)) {
384  echo translate('cms_form_section_no_nested_sections');
385  } else {
386  for (reset($section_links); NULL !== ($k = key($section_links)); next($section_links)) {
387  $s =& $GLOBALS['SQ_SYSTEM']->am->getAsset($section_links[$k]['minorid']);
388  echo get_asset_tag_line($s->id, 'details').'<br />';
389  }
390  $o->note(translate('cms_form_section_nested_sections_note'));
391  }
392 
393  return $write_access;
394 
395  }//end paintNestedSections()
396 
397 
411  {
412  return FALSE;
413 
414  }//end paintNestedSections()
415 
416 
427  function paintUseBodycopy(&$asset, &$o, $prefix)
428  {
429  $write_access = $asset->writeAccess('links');
430  $bodycopy_link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_TYPE_2, 'bodycopy', 'format');
431  $enabled = !empty($bodycopy_link);
432 
433  if ($write_access) {
434  $options = Array(
435  '1' => translate('yes'),
436  '0' => translate('no'),
437  );
438 
439  combo_box($prefix.'_use_bodycopy', $options, FALSE, Array($enabled));
440 
441  } else {
442  echo $enabled ? translate('on') : translate('off');
443  }
444 
445  return $write_access;
446 
447  }//end paintUseBodycopy()
448 
449 
461  function processUseBodycopy(&$asset, &$o, $prefix)
462  {
463  if (!$asset->writeAccess('links')) return FALSE;
464  $enabled = $_POST[$prefix.'_use_bodycopy'];
465 
466  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
467  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
468 
469  if (!$asset->setUseFormatBodycopy($enabled)) {
470  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
471  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
472  return FALSE;
473  }
474 
475  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
476  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
477 
478  return TRUE;
479 
480  }//end processUseBodycopy()
481 
482 
494  {
495  $write_access = $asset->writeAccess('links');
496  if ($write_access) {
497  $option_list = new Asset_Attribute_Option_List();
498  $option_list->paint($prefix.'_sections');
499  }
500 
501  return $write_access;
502 
503  }//end paintAddSections()
504 
505 
517  {
518  if (!$asset->writeAccess('links')) return FALSE;
519 
520  $new_sections = Array();
521 
522  $section_text = array_get_index($_POST, $prefix.'_sections_options', '');
523  if (!empty($section_text)) {
524  // make sure the section names are not empty
525  foreach ($section_text as $option) {
526  $option = trim($option);
527  if (empty($option)) continue;
528  $new_sections[] = $option;
529  }
530  }
531 
532  if (!empty($new_sections)) {
533 
534  foreach ($new_sections as $new_section_name) {
535  $new_section_asset = new Form_Section();
536  $new_section_asset->setAttrValue('name', $new_section_name);
537 
538  // link it underneath this section
539  $link = Array(
540  'asset' => $asset,
541  'link_type' => SQ_LINK_TYPE_2,
542  'is_dependant' => '1',
543  'is_exclusive' => '0',
544  );
545 
546  // If we cannot create this asset for whatever reason, let
547  // the user know about it
548  if (!$new_section_asset->create($link)) {
549  trigger_localised_error('CMS0108', E_USER_WARNING, $new_section_name, $asset->name);
550  }
551  }
552 
553  $asset->linksUpdated();
554  }
555 
556  return !empty($new_sections);
557 
558  }//end processNewSections()
559 
560 
569  function generateContentFile(&$asset)
570  {
571  $output = '';
572  $form_link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_TYPE_2, 'form', FALSE, NULL, 'minor');
573  if(isset($form_link['majorid'])){
574  $output = '<'.'?php if(!isset($form_asset)) $form_asset=$GLOBALS["SQ_SYSTEM"]->am->getAsset('.$form_link['majorid'].'); ?'.'>'."\n";
575  }
576 
577  if ($asset->isFormatBodycopyEnabled()) {
578  $format_bodycopy = $asset->getFormatBodycopy(TRUE);
579 
580  ob_start();
581  $replacements = $asset->getDisplayKeywordReplacements(TRUE);
582  $format_bodycopy->setKeywordReplacements($replacements);
583  $format_bodycopy->printBody();
584  $output .= ob_get_clean();
585  } else {
586  // No custom format, so generate a default format
587  $output .= $this->generateGenericSection($asset);
588  }
589 
590  // if there are any tags left that haven't been replaced,
591  // we want to strip them out
592  $output = preg_replace('/%[^<>%]+%/', '', $output);
593 
594  create_directory($asset->data_path);
595  $ok = string_to_file($output, $asset->data_path.'/content_file.php');
596 
597  // Re-generate content files for higher sections
598  $section_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_ALL, 'form_section', FALSE, 'minor');
599  foreach ($section_links as $section_link) {
600  // We should let the parent section use its own edit fns - just in
601  // case Form Section is subclassed for some reason
602  $parent_section = $GLOBALS['SQ_SYSTEM']->am->getAsset($section_link['majorid']);
603  $edit_fns = $parent_section->getEditFns();
604  $edit_fns->generateContentFile($parent_section);
605  unset($edit_fns);
606  unset($parent_section);
607  }
608 
609  return $ok;
610 
611  }//end generateContentFile()
612 
613 
622  function generateGenericSection(&$asset)
623  {
624  // Do we have any questions of our own?
625  $sort_order = $asset->attr('sort_order');
626  $output = '';
627 
628  if (!empty($sort_order)) {
629 
630  $asset_name = $asset->attr('name');
631  escape_php($asset_name);
632 
633  $output .= '<h3>'.$asset_name.'</h3>';
634 
635  $output .= '<table width="'.$asset->attr('section_width').'">';
636 
637  foreach ($sort_order as $i => $assetid) {
638  $q = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
639  // skip over if asset doesn't exist anymore
640  if (is_null($q)) continue;
641 
642  $output .= '<tr>';
643  $output .= '<td valign="top"';
644  $output .= ($asset->attr('question_col_width')) ? ' width="'.$asset->attr('question_col_width').'">' : '>';
645  $label = $q->getHtmlLabel();
646  escape_php($label);
647  $output .= $label;
648  if ($q->getVal('note')) {
649  $output .= '<br /><span style="font-size: <'.'?php echo $form_asset->attr("note_size") ?'.'>">'.$q->getVal('note').'</span>';
650  }
651  $output .= '</td><td';
652  $output .= ($asset->attr('answer_col_width')) ? ' width="'.$asset->attr('answer_col_width').'">' : '>';
653  $output .= '<'.'?php echo $GLOBALS["SQ_SYSTEM"]->am->getAsset("'.$assetid.'")->getHtmlField(); ?'.'>'."\n";
654  $output .= '</td></tr>';
655  }
656 
657  $output .= '</table>';
658  }
659 
660  // Print nested sections as part of default bodycopy
661  $section_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_ALL, 'form_section', FALSE);
662 
663  for (reset($section_links); NULL !== ($k = key($section_links)); next($section_links)) {
664  $section =& $GLOBALS['SQ_SYSTEM']->am->getAsset($section_links[$k]['minorid']);
665 
666  // output an include BUT we want to make what's printed relative to SQ_DATA_PATH
667  $output .= '<'.'?php include_once($GLOBALS[\'SQ_SYSTEM\']->am->getAsset(\''.$section->id.'\')->data_path.\'/content_file.php\'); ?'.'>';
668  }
669 
670  return $output;
671 
672  }//end generateGenericSection()
673 
674 
675 
676  //-- FORM SECTION ACTIONS --//
677 
678 
688  public function paintAllActions(Form_Section $asset, Backend_Outputter $o, $prefix)
689  {
690  $write_access = $asset->writeAccess('attributes');
691  $actions = $asset->attr('actions');
692 
693  // Get selected action, which may have changed last commit
694  if ($write_access) {
695  $selected_action = array_get_index($_POST, $prefix.'_new_selected_action', NULL);
696  if (!is_numeric($selected_action)) {
697  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
698  }
699  } else {
700  $selected_action = NULL;
701  }
702 
703  $o->openField('');
704 
705  hidden_field($prefix.'_new_selected_action', '');
706 
707  if (!empty($actions)) {
708  ?><table class="sq-backend-table">
709  <colgroup>
710  <col width="60" align="center" />
711  <col width="150" />
712  <col/>
713  <col width="60" align="center" />
714  <col width="60" align="center" />
715  </colgroup>
716  <thead>
717  <th><p>Edit</p></th>
718  <th><p>Action Name / Type</p></th>
719  <th><p>Summary</p></th>
720  <th><p>Valid ?</p></th>
721  <th><p>Active ?</p></th>
722  <th><p>Delete ?</p></th>
723  </thead><tbody><?php
724 
725  foreach ($actions as $key => $action) {
726  $is_selected_action = (!is_null($selected_action) && ((string)$selected_action === (string)$key));
727  ?><tr<?php
728  if ($is_selected_action) {
729  echo ' class="alt"';
730  }
731  ?>><?php
732  $action_type = $action['type_code'];
733  $settings = $action['settings'];
734  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action_type, 'name');
735 
736  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action_type);
737 
738  if ($write_access && !$is_selected_action) {
739  ?><td class="sq-backend-table-cell"><p><a href="#" onclick="document.getElementById('<?php echo $prefix ?>_new_selected_action').value = <?php echo $key ?>; document.main_form.submit(); return false;"><img src="<?php echo sq_web_path('lib'); ?>/web/images/icons/edit_mode.png" width="15" height="15" border="0" /></a></p></td><?php
740  } else {
741  ?><td class="sq-backend-table-cell">&nbsp;</td><?php
742  }
743  ?><td class="sq-backend-table-cell"><p><strong><?php echo $action['name'] ?></strong><br/>(<?php echo $type_name ?>)</p></td><?php
744  ?><td class="sq-backend-table-cell"><?php echo call_user_func(Array($action_type, 'paintSummary'), $asset, $settings, $o, $prefix) ?></td><?php
745  ?><td class="sq-backend-table-cell"><p><img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo call_user_func(Array($action_type, 'isValid'), $asset, $settings) ? 'tick' : 'cross' ?>.gif" width="15" height="15" border="0" /></p></td><?php
746  if ($write_access) {
747  ?><td class="sq-backend-table-cell"><p><?php check_box($prefix.'_rules[active]['.$key.']', '1', $action['active']) ?></p></td><?php
748  ?><td class="sq-backend-table-cell"><p><?php check_box($prefix.'_rules[delete]['.$key.']', '1', FALSE) ?></p></td><?php
749  } else {
750  ?><td class="sq-backend-table-cell"><p><img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo $action['active'] ? 'tick' : 'cross' ?>.gif" width="15" height="15" border="0" /></p></td><?php
751  ?><td class="sq-backend-table-cell">&nbsp;</td><?php
752  }
753  ?></tr><?php
754  }
755  ?></tbody></table><?php
756  } else {
757  ?><p>There are no actions currently defined for this form section.</p><?php
758  }
759 
760  $o->closeField();
761  $o->sectionNote('Only actions that are <strong>valid</strong> and <strong>active</strong> will be executed when a form is submitted. An action can be activated or deactivated with the <strong>Active ?</strong> check box.');
762 
763  return $write_access;
764 
765  }//end paintAllActions()
766 
767 
777  public function processAllActions(Form_Section $asset, Backend_Outputter $o, $prefix)
778  {
779  $write_access = $asset->writeAccess('attributes');
780 
781  // If we have selected "edit action", then we don't want this to run,
782  // because we only want active/delete changes to take effect upon
783  // hitting the commit button.
784  $edit_action = array_get_index($_POST, $prefix.'_new_selected_action', NULL);
785  if (!empty($edit_action)) {
786  return $write_access;
787  }
788 
789  if ($write_access) {
790  $actions = $asset->attr('actions');
791 
792  $rule_changes = array_get_index($_POST, $prefix.'_rules', Array());
793 
794  if(!empty($actions)) {
795  foreach ($actions as $key => $action) {
796  if (!isset($rule_changes['active'][$key])) {
797  $rule_changes['active'][$key] = Array();
798  }
799  }
800  } else {
801  if (!isset($rule_changes['active'])) {
802  $rule_changes['active'] = Array();
803  }
804  }
805 
806  if (!isset($rule_changes['delete'])) {
807  $rule_changes['delete'] = Array();
808  }
809 
810  foreach ($rule_changes['active'] as $key => $value) {
811  $actions[$key]['active'] = (boolean)$value;
812  }
813  foreach ($rule_changes['delete'] as $key => $value) {
814  unset($actions[$key]);
815  }
816 
817  $asset->setAttrValue('actions', $actions);
818  if (!$asset->saveAttributes()) return FALSE;
819  }
820 
821  return $write_access;
822 
823  }//end processAllActions()
824 
825 
835  public function paintNewAction(Form_Section $asset, Backend_Outputter $o, $prefix)
836  {
837  $write_access = $asset->writeAccess('attributes');
838 
839  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('form_section_action');
840  $names = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($types, 'name');
841 
842  $o->openField('');
843  if ($write_access) {
844  ?><p><?php
845  check_box($prefix.'_new', '1', FALSE, 'this.form.'.$prefix.'_new_type.disabled = !this.checked; return true;');
846  ?><label for="<?php echo $prefix ?>_new">Create a new action of type:</label> <?php
847  $names = Array('' => 'Select action type...') + $names;
848  combo_box($prefix.'_new_type', $names, FALSE, '', 0, 'disabled="disabled"');
849  ?></p><?php
850  } else {
851  ?><p>This screen must be locked before you can create a new action.</p><?php
852  }
853 
854  // If we have a new action, are we
855  $o->closeField();
856 
857  $o->sectionNote('If you create a new action, the new action will be displayed immediately for you to edit. If you already have an action open to edit, the changes to that action will be saved when you commit before the new action is created.');
858 
859  return $write_access;
860 
861  }//end paintNewAction()
862 
863 
873  public function processNewAction(Form_Section $asset, Backend_Outputter $o, $prefix)
874  {
875  $write_access = $asset->writeAccess('attributes');
876 
877  if ($write_access) {
878  // First check that we are creating a new rule.
879  $new_action = (int)array_get_index($_POST, $prefix.'_new', '0');
880 
881  if ($new_action) {
882  // Paranoia, paranoia...
883  // Check whether the type we've been passed is a valid type,
884  // to protect possible stuffed POSTDATA (including a blank)
885  $action_type = array_get_index($_POST, $prefix.'_new_type', '');
886  if (!empty($action_type)) {
887  if ($GLOBALS['SQ_SYSTEM']->am->installed($action_type)) {
888  // Also check whether it's a valid form action
889  $type_parents = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($action_type);
890  if (in_array('form_section_action', $type_parents)) {
891  // okay, we seem to be in the clear
892  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action_type, 'name');
893  $actions = $asset->attr('actions');
894 
895  if (!empty($actions)) {
896  $new_key = max(array_keys($actions)) + 1;
897  } else {
898  $new_key = 0;
899  }
900 
901  $actions[$new_key] = Array(
902  'name' => $this->_makeValidActionName($asset, $type_name),
903  'type_code' => $action_type,
904  'settings' => NULL,
905  'active' => TRUE,
906  );
907  $asset->setAttrValue('actions', $actions);
908 
909  // Set new selected action
910  $_POST[$prefix.'_new_selected_action'] = $new_key;
911 
912  if (!$asset->saveAttributes()) return FALSE;
913  } else {
914  trigger_error('Cannot create new action; asset type "'.$action_type.'" is not a form action', E_USER_WARNING);
915  return FALSE;
916  }
917  } else {
918  trigger_error('Cannot create new action; "'.$action_type.'" is not a valid asset type', E_USER_WARNING);
919  return FALSE;
920  }
921  }
922  }
923 
924  }
925 
926  return $write_access;
927 
928  }//end processNewAction()
929 
930 
940  public function paintSelectedAction(Form_Section $asset, Backend_Outputter $o, $prefix)
941  {
942  $write_access = $asset->writeAccess('attributes');
943  $selected_action = array_get_index($_POST, $prefix.'_new_selected_action', NULL);
944  if (!is_numeric($selected_action)) {
945  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
946  }
947 
948  if ($write_access) {
949  if (!is_null($selected_action)) {
950  $actions = $asset->attr('actions');
951 
952  // Get the current action and process - but not if we've
953  // already been deleted further up!
954  if (!isset($actions[$selected_action])) {
955  $o->openField('');
956  ?><p>No action is currently selected for editing. To edit an action, please select from the "Existing Actions" list above.</p><?php
957  $o->closeField();
958  return FALSE;
959  }
960 
961  $action = $actions[$selected_action];
962 
963  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action['type_code'], 'name');
964 
965  $o->openField('Action Name');
966 
967  text_box($prefix.'_selected_name', $action['name'], 30);
968  $o->note('An optional name to identify this particular action. If cleared, the action\'s type ("'.$type_name.'" in this case) will be used, numerically indexed if necessary.');
969 
970  $o->closeField();
971  $o->openField('Settings');
972 
973  hidden_field($prefix.'_selected_action', $selected_action);
974  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action['type_code']);
975  call_user_func(Array($action['type_code'], 'paintInlineInterface'), $asset, $action['settings'], $o, $prefix);
976 
977  $o->closeField();
978 
979  } else {
980  $o->openField('');
981  ?><p>No action is currently selected for editing. To edit an action, please select from the "Existing Actions" list above.</p><?php
982  $o->closeField();
983 
984  }//end if there is a selected action
985  } else {
986  $o->openField('');
987  ?><p>This screen must be locked before you can edit an action.</p><?php
988  $o->closeField();
989  }//end if write_access
990 
991  return $write_access;
992 
993  }//end paintSelectedAction()
994 
995 
1005  public function processSelectedAction(Form_Section $asset, Backend_Outputter $o, $prefix)
1006  {
1007  $write_access = $asset->writeAccess('attributes');
1008  $actions = $asset->attr('actions');
1009  $result = FALSE;
1010 
1011  // If an "edit action" has been clicked, then we've lost the changes
1012  // to the current rule.
1013  $edit_action = array_get_index($_POST, $prefix.'_new_selected_action', NULL);
1014  if (is_numeric($edit_action)) {
1015  return FALSE;
1016  }
1017 
1018  // If not, then get the current action and process - but not if we've
1019  // already been deleted!
1020  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
1021  if (!isset($actions[$selected_action])) {
1022  return FALSE;
1023  }
1024 
1025  if ($write_access) {
1026  if (!is_null($selected_action)) {
1027  $new_name = array_get_index($_POST, $prefix.'_selected_name', NULL);
1028  $action = $actions[$selected_action];
1029 
1030  $new_name = array_get_index($_POST, $prefix.'_selected_name', NULL);
1031  if ($new_name != $action['name']) {
1032  if (empty($new_name)) {
1033  $new_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action['type_code'], 'name');
1034  }
1035  $actions[$selected_action]['name'] = $this->_makeValidActionName($asset, $new_name);
1036  }
1037 
1038  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action['type_code']);
1039  $settings = $action['settings'];
1040 
1041  // Bug Fix #3145
1042  // call_user_func doesn't work with pass-by-reference in PHP5, so using call_user_func_array instead
1043  $result = call_user_func_array(Array($action['type_code'], 'processInlineInterface'), Array($asset, &$settings, $o, $prefix));
1044  if ($result) {
1045  $actions[$selected_action]['settings'] = $settings;
1046  $asset->setAttrValue('actions', $actions);
1047  if (!$asset->saveAttributes()) return FALSE;
1048  }
1049  } else {
1050  return FALSE;
1051  }
1052  }
1053 
1054  return ($result && $write_access);
1055 
1056  }//end processSelectedAction()
1057 
1058 
1071  protected function _makeValidActionName(Form_Section $asset, $base_name)
1072  {
1073  $actions = $asset->attr('actions');
1074 
1075  $name = $base_name;
1076  $i = 1;
1077 
1078  do {
1079  $found = FALSE;
1080 
1081  foreach ($actions as $action) {
1082  if ($action['name'] == $name) {
1083  $found = TRUE;
1084 
1085  // Incrememt the base name
1086  $i++;
1087  $name = $base_name.' '.$i;
1088  break;
1089  }
1090  }
1091 
1092  } while ($found);
1093 
1094  return $name;
1095 
1096  }//end _makeValidActionName()
1097 
1098 
1099 }//end class
1100 ?>