Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ecommerce_checkout_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page_edit_fns.inc';
19 
32 {
33 
34 
45  function paintDeliveryMethod(&$asset, &$o, $prefix)
46  {
47  // get delivery method links
48  $delivery_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, '', FALSE, 'major', 'delivery_method');
49  $delivery_asset_ids = Array();
50  foreach ($delivery_links as $one_link) {
51  $delivery_asset_ids[$one_link['minorid']] = $one_link['minor_type_code'];
52  }
53 
54  if ($asset->writeAccess('links')) {
55  multiple_asset_finder($prefix.'_delivery_methods', array_keys($delivery_asset_ids), Array('ecommerce_delivery_method'=>'D'));
56  } else {
57  $infos = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($delivery_asset_ids));
58  ?>
59  <table class="sq-backend-table">
60  <tr>
61  <th class="sq-backend-table-header"><?php echo translate('asset') ?></th>
62  <th class="sq-backend-table-header"><?php echo translate('type') ?></th>
63  </tr>
64  <?php
65  foreach ($infos as $assetid => $info) {
66  ?>
67  <tr>
68  <td class="sq-backend-table-cell"><?php echo get_asset_tag_line($assetid) ?></td>
69  <td class="sq-backend-table-cell"><?php echo ucwords(str_replace('_',' ', $info['type_code'])) ?></td>
70  </tr>
71  <?php
72  }
73  ?>
74  </table>
75  <?php
76  }
77  return TRUE;
78 
79  }//end paintDeliveryMethod()
80 
81 
92  function processDeliveryMethod(&$asset, &$o, $prefix)
93  {
94  if (!isset($_POST[$prefix.'_delivery_methods'])) {
95  return FALSE;
96  }
97  $new_delivery_methods = $_POST[$prefix.'_delivery_methods'];
98  foreach ($new_delivery_methods as $new_delivery_method) {
99  $new_delivery_method_ids[] = $new_delivery_method['assetid'];
100  }
101 
102  // get existing delivery method links
103  $delivery_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_NOTICE, '', FALSE, 'major', 'delivery_method');
104 
105  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
106  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
107  $ok = TRUE;
108 
109  // lets delete the nodes that have been removed
110  foreach ($delivery_links as $delivery_link) {
111  if (!in_array($delivery_link['minorid'], $new_delivery_method_ids)) {
112  if (!$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($delivery_link['linkid'])) {
113  $ok = FALSE;
114  }
115  }
116  }
117 
118  // now, create the new node links
119  foreach ($new_delivery_methods as $index => $node) {
120  // first test to see if the node already exists -- if so, ignore it
121  foreach ($delivery_links as $delivery_link) {
122  if ($delivery_link['minorid'] == $node['assetid']) {
123  continue 2;
124  }
125  }
126  // otherwise, create the appropriate notice link
127  if ($node['assetid'] != 0) {
128  $new_node = $GLOBALS['SQ_SYSTEM']->am->getAsset($node['assetid']);
129  if (!$asset->createLink($new_node, SQ_LINK_NOTICE, 'delivery_method')) {
130  $ok = FALSE;
131  trigger_localised_error('CMS0016', E_USER_WARNING, $new_node->name, $new_node->id);
132  break;
133  }
134  }
135  }
136 
137  if ($ok) {
138  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
139  } else {
140  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
141  }
142 
143  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
144  return $ok;
145 
146  }//end processDeliveryMethod()
147 
148 
159  function paintContextScreens(&$asset, &$o, $prefix)
160  {
161  ?>
162  <table class="sq-backend-table">
163  <tr>
164  <th><?php echo translate('edit_format') ?></th>
165  <th><?php echo translate('note') ?></th>
166  </tr>
167  <?php
168  // get the link to folders
169  $link_to_folder = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_TYPE_2, 'folder');
170  $links_to_bodycopies = $GLOBALS['SQ_SYSTEM']->am->getLinks($link_to_folder['minorid'], SQ_LINK_TYPE_2, 'bodycopy');
171 
172  $contexts = $asset->_getAvailableContexts();
173 
174  if (!empty($links_to_bodycopies)) {
175  foreach ($links_to_bodycopies as $link) {
176  $info = Array($link['minorid'] => 'contents');
177  $href = $GLOBALS['SQ_SYSTEM']->am->getAssetBackendHref($info);
178  ?>
179  <tr>
180  <td class="sq-backend-table-cell">
181  <?php echo get_asset_tag_line($link['minorid'], 'contents') ?>
182  </td>
183  <td class="sq-backend-table-cell">
184  <?php
185  switch ($link['value']) {
186  case 'checkout':
187  echo translate('ecom_checkout_checkout_context_desc');
188  break;
189  case 'confirmation':
190  echo translate('ecom_checkout_confirmation_context_desc');
191  break;
192  case 'failure':
193  echo translate('ecom_checkout_failure_context_desc');
194  break;
195  case 'receipt':
196  echo translate('ecom_checkout_receipt_context_desc');
197  break;
198  }
199  ?>
200  </td>
201  </tr>
202  <?php
203  }//end foreach
204 
205  echo '</table>';
206  }//end if
207  return TRUE;
208 
209  }//end paintContextScreens()
210 
211 
222  function paintFlatCharges(&$asset, &$o, $prefix)
223  {
224  $current_value = $asset->attr('flat_charge_assetid');
225 
226  if ($asset->writeAccess('links')) {
227  $flat_charges = !empty($current_value) ? $current_value : Array();
228  multiple_asset_finder($prefix.'_flat_charge', $flat_charges, Array('product' => 'D'));
229  } else {
230  for ($i=0; $i<count($current_value); $i++) {
231  echo get_asset_tag_line($current_value[$i]).'<br />';
232  }
233  }
234  return TRUE;
235 
236  }//end paintFlatCharges()
237 
238 
249  function processFlatCharges(&$asset, &$o, $prefix)
250  {
251  if (!isset($_POST[$prefix.'_flat_charge'])) {
252  return FALSE;
253  }
254 
255  $flat_charge_values = $_POST[$prefix.'_flat_charge'];
256  $new_charge_values = Array();
257  foreach ($flat_charge_values as $value) {
258  if (($value['assetid'] != 0) && (!in_array($value['assetid'], $new_charge_values))) {
259  $new_charge_values[] = $value['assetid'];
260  }
261  }
262  $asset->setAttrValue('flat_charge_assetid', $new_charge_values);
263  return TRUE;
264 
265  }//end processFlatCharges()
266 
267 
278  function paintTaxableCountries(&$asset, &$o, $prefix)
279  {
280  $selected_country_values = $asset->attr('taxable_countries');
281  $name = $prefix.'_taxable_countries';
282  $this->_paintCountrySelectBox($asset->writeAccess('attributes'), $name, $selected_country_values);
283  return TRUE;
284 
285  }//end paintTaxableCountries()
286 
287 
298  function processTaxableCountries(&$asset, &$o, $prefix)
299  {
300  $name = 'taxable_countries';
301  return $this->_processCountrySelectBox($asset, $prefix, $name);
302 
303  }//end processTaxableCountries()
304 
305 
317  function _paintCountrySelectBox($wa, $name, $selected, $size=9, $multiple=TRUE)
318  {
319  global $standards_lists_countries;
320  require_once SQ_FUDGE_PATH.'/standards_lists/countries.inc';
321  // prepend blank value
322  $standards_lists_countries = array_merge(Array('blank'=>''), $standards_lists_countries);
323 
324  if ($wa) {
325  echo combo_box($name, $standards_lists_countries, $multiple ? TRUE : FALSE, $selected, $size);
326  } else {
327  // get a comma delimited string of all countries currently selected
328  $countries_string = '';
329  for ($i=0; $i<count($selected); $i++) {
330  foreach ($standards_lists_countries as $code => $country) {
331  if ($selected[$i] === $code) {
332  if (empty($countries_string)) {
333  $countries_string = $country;
334  } else {
335  $countries_string .= ', '.$country;
336  }
337  continue(2);
338  }
339  }
340  }
341  echo $countries_string;
342  }
343 
344  }//end _paintCountrySelectBox()
345 
346 
357  function _processCountrySelectBox(&$asset, $prefix, $name)
358  {
359  if ($asset->writeAccess('attributes')) {
360  $post = isset($_POST[$prefix.'_'.$name]) ? $_POST[$prefix.'_'.$name] : Array();
361  if ($asset->setAttrValue($name, $post)) {
362  return TRUE;
363  }
364  }
365  return FALSE;
366 
367  }//end _processCountrySelectBox()
368 
369 
380  function paintQuantitySource(&$asset, &$o, $prefix)
381  {
382  return $this->_paintFieldSelector($asset, 'quantity');
383 
384  }//end paintQuantitySource()
385 
386 
397  function processQuantitySource(&$asset, &$o, $prefix)
398  {
399  return $this->_processFieldSelector($asset, 'quantity');
400 
401  }//end processQuantitySource()
402 
403 
414  function paintItemWeightSource(&$asset, &$o, $prefix)
415  {
416  return $this->_paintFieldSelector($asset, 'item_weight');
417 
418  }//end paintProductWeightSource()
419 
420 
431  function processItemWeightSource(&$asset, &$o, $prefix)
432  {
433  return $this->_processFieldSelector($asset, 'item_weight');
434 
435  }//end processProductWeightSource()
436 
437 
447  function _paintFieldSelector(&$asset, $selector)
448  {
449  switch ($selector) {
450  case 'quantity':
451  $selector_type = $asset->attr('quantity_selector');
452  $metadata_source_name = 'quantity_field';
453  $attribute_source_name = 'quantity_attribute';
454  break;
455  case 'item_weight':
456  $selector_type = $asset->attr('item_weight_selector');
457  $metadata_source_name = 'item_weight_metadata_source';
458  $attribute_source_name = 'item_weight_attribute_source';
459  break;
460  }
461  $prefix = $asset->getPrefix();
462  $wa = $asset->writeAccess('attributes');
463  switch ($selector_type) {
464  case 'metadata':
465  if ($wa) {
466  echo asset_finder($prefix.'_'.$metadata_source_name, $asset->attr($metadata_source_name), Array('metadata_field_text' => 'I'));
467  } else {
468  $field = $asset->attr($metadata_source_name);
469  if (!empty($field)) {
470  echo get_asset_tag_line($asset->attr($metadata_source_name));
471  }
472  }
473  break;
474 
475  case 'attribute':
476  if ($wa) {
477  echo text_box($prefix.'_'.$attribute_source_name, $asset->attr($attribute_source_name));
478  } else {
479  echo $asset->attr($attribute_source_name);
480  }
481  break;
482  }
483 
484  }//end _paintFieldSelector()
485 
486 
496  function _processFieldSelector(&$asset, $selector)
497  {
498  switch ($selector) {
499  case 'quantity':
500  $metadata_source_name = 'quantity_field';
501  $attribute_source_name = 'quantity_attribute';
502  break;
503  case 'item_weight':
504  $metadata_source_name = 'item_weight_metadata_source';
505  $attribute_source_name = 'item_weight_attribute_source';
506  break;
507  }
508  $prefix = $asset->getPrefix();
509  if ($asset->writeAccess('attributes')) {
510  if (isset($_POST[$prefix.'_'.$metadata_source_name]['assetid'])) {
511  if ($asset->setAttrValue($metadata_source_name, $_POST[$prefix.'_'.$metadata_source_name]['assetid'])) {
512  return TRUE;
513  } else {
514  return FALSE;
515  }
516  } else if (isset($_POST[$prefix.'_'.$attribute_source_name])) {
517  if ($asset->setAttrValue($attribute_source_name, $_POST[$prefix.'_'.$attribute_source_name])) {
518  return TRUE;
519  } else {
520  return FALSE;
521  }
522  }
523  } else {
524  return TRUE;
525  }
526 
527  }//end _processFieldSelector()
528 
529 
540  function paintCountryDeliveryWeightFees(&$asset, &$o, $prefix)
541  {
542  global $standards_lists_countries;
543  require_once SQ_FUDGE_PATH.'/standards_lists/countries.inc';
544  // prepend blank value
545  $standards_lists_countries = array_merge(Array('blank'=>''), $standards_lists_countries);
546 
547  $wa = $asset->writeAccess('attributes');
548  $delivery_weight_fees = $asset->attr('delivery_weight_fee_mapping');
549  if ($wa && empty($delivery_weight_fees)) {
550  $delivery_weight_fees = Array(0);
551  }
552 
553  // note: these javascript functions are used by paintConditionalDeliveryFee() as well
554  ?>
555  <script type="text/javascript">
556 
557  function prependLastTable(moreButton)
558  {
559  var lastTable = moreButton.parentNode;
560  while (lastTable.tagName != 'TABLE') {
561  lastTable = lastTable.parentNode;
562  }
563 
564  // to be used when adding the button back
565  var lastSibling = lastTable.previousSibling;
566  var lastTableButtonCell = moreButton.parentNode;
567 
568  // look for sq-backend-data to prepend this table
569  var validLocation = moreButton.parentNode;
570  while (validLocation.className != 'sq-backend-data') {
571  validLocation = validLocation.parentNode;
572  }
573 
574  var clone = lastTable.cloneNode(true);
575  // inserting clone after original
576  validLocation.insertBefore(clone, lastTable.nextSibling);
577 
578  // append the 'More' button to the cloned table
579  lastTableButtonCell.appendChild(moreButton);
580 
581  // remove the 'More' button from the original after cloning
582  moreButton.parentNode.removeChild(moreButton);
583 
584  // traverse dom and increment each field name
585  incrementTableFieldNames(clone);
586 
587  return clone;
588  }
589 
590  function incrementTableFieldNames(element)
591  {
592  var i=0;
593  var elementChildren = element.childNodes[i];
594  var tagName = element.tagName;
595 
596  if ((tagName == 'INPUT') || (tagName == 'SELECT')) {
597  // increment field name
598  var currentKey = element.name.match(/\[(\d+)\]/);
599  var newKey = parseInt(currentKey[1]) + 1;
600  element.name = element.name.replace(/\[(\d+)\]/, '[' + newKey + ']');
601  }
602 
603  while (elementChildren) {
604  incrementTableFieldNames(elementChildren);
605  i++;
606  elementChildren = element.childNodes[i];
607  }
608  }
609 
610  function resetLastTableFields(element, findTable)
611  {
612  // clear all elements inside the table
613  if (findTable) {
614  var element = element.parentNode;
615  while (element.tagName != 'TABLE') {
616  element = element.parentNode;
617  }
618  }
619 
620  var i=0;
621  var elementChildren = element.childNodes[i];
622  var tagName = element.tagName;
623 
624  if (tagName == 'INPUT') {
625  // don't clear buttons
626  if (element.type != 'button') {
627  element.value = '';
628  }
629  return;
630  } else if (tagName == 'SELECT') {
631  element.selectedIndex = 0;
632  return;
633  }
634 
635  while (elementChildren) {
636  resetLastTableFields(elementChildren, false);
637  i++;
638  elementChildren = element.childNodes[i];
639  }
640  }
641  </script>
642  <?php
643  foreach ($delivery_weight_fees as $key => $data) {
644  ?>
645  <table width="200px">
646  <tr>
647  <td colspan="3" width="100%" valign="top">
648  <?php
649  $name = $prefix.'_country_delivery_weight_country['.$key.']';
650  $this->_paintCountrySelectBox($wa, $name, $data['countries'], 9, TRUE);
651  ?>
652  </td>
653  </tr>
654  <tr>
655  <td>
656  <?php
657  $name = $prefix.'_country_delivery_weight_price['.$key.']';
658  echo translate('price').':';
659  ?>
660  </td>
661  <td>
662  <?php
663  if ($wa) {
664  echo text_box($prefix.'_country_delivery_weight_price['.$key.']', $data['price'], 10);
665  } else {
666  echo $data['price'];
667  }
668  ?>
669  </td>
670  <td>
671  <?php
672  if ($wa) {
673  $clear_button_name = $prefix.'_country_delivery_weight_clear_button['.$key.']';
674  $more_button_name = $prefix.'_country_delivery_weight_more_button['.$key.']';
675  echo normal_button($clear_button_name, translate('clear'), 'resetLastTableFields(this, true)');
676  // only print this button for last table
677  if ($key === count($delivery_weight_fees)-1) {
678  echo normal_button($more_button_name, translate('more').'...', 'var table = prependLastTable(this); resetLastTableFields(table, false)');
679  }
680  }
681  ?>
682  </td>
683  </tr>
684  </table>
685  <?php
686  }//end foreach
687 
688  return TRUE;
689 
690  }//end paintCountryDeliveryWeightFees()
691 
692 
703  function processCountryDeliveryWeightFees(&$asset, &$o, $prefix)
704  {
705  if ($asset->writeAccess('attributes')) {
706  $countries_form_name = $prefix.'_country_delivery_weight_country';
707  $price_form_name = $prefix.'_country_delivery_weight_price';
708  if (isset($_POST[$countries_form_name])) {
709  $delivery_weight_fees = Array();
710  foreach ($_POST[$countries_form_name] as $key => $codes) {
711  if (empty($codes[0]) || empty($_POST[$price_form_name][$key])) {
712  continue;
713  }
714  $delivery_weight_fees[$key] = Array(
715  'countries' => $codes,
716  'price' => (float) $_POST[$price_form_name][$key],
717  );
718  }
719  $asset->setAttrValue('delivery_weight_fee_mapping', $delivery_weight_fees);
720  return TRUE;
721  }
722  }
723  return FALSE;
724 
725  }//end processCountryDeliveryWeightFees()
726 
727 
738  function paintInternationalDeliveryFeeExempt(&$asset, &$o, $prefix)
739  {
740  $selected_country_values = $asset->attr('international_delivery_fee_exempt_countries');
741  $name = $prefix.'_international_delivery_fee_exempt_countries';
742  $this->_paintCountrySelectBox($asset->writeAccess('attributes'), $name, $selected_country_values);
743  return TRUE;
744 
745  }//end paintInternationalDeliveryFeeExempt()
746 
747 
758  function processInternationalDeliveryFeeExempt(&$asset, &$o, $prefix)
759  {
760  $name = 'international_delivery_fee_exempt_countries';
761  return $this->_processCountrySelectBox($asset, $prefix, $name);
762 
763  }//end processInternationalDeliveryFeeExempt()
764 
765 
776  function paintConditionalDeliveryFee(&$asset, &$o, $prefix)
777  {
778  $wa = $asset->writeAccess('attributes');
779  $conditions = $asset->attr('conditional_delivery_fees');
780  if ($wa && empty($conditions)) {
781  // so we still print an empty table if there are no conditions yet
782  $conditions = Array(0);
783  }
784  foreach ($conditions as $key => $data) {
785  ?>
786  <table width="500px">
787  <tr>
788  <td width="50%" rowspan="6" valign="top">
789  <?php
790  $name = $prefix.'_conditional_fee_countries['.$key.']';
791  $this->_paintCountrySelectBox($wa, $name, $data['countries'], 9, TRUE);
792  ?>
793  </td>
794  </tr>
795  <tr>
796  <?php
797  $name = $prefix.'_conditional_fee_comparison_price['.$key.']';
798  ?>
799  <td width="20%">
800  <?php echo translate('ecom_checkout_comparison_price').':'; ?>
801  </td>
802  <td>
803  <?php
804  if ($wa) {
805  echo text_box($name, $data['comparison_price'], 5);
806  } else {
807  echo $data['comparison_price'];
808  }
809  ?>
810  </td>
811  </tr>
812  <tr>
813  <?php
814  $name = $prefix.'_conditional_fee_operator['.$key.']';
815  $operators = Array(
816  '' => '',
817  '>' => '>',
818  '<' => '<',
819  '==' => '=='
820  );
821  ?>
822  <td>
823  <?php echo translate('ecom_checkout_operator').':'; ?>
824  </td>
825  <td>
826  <?php
827  if ($wa) {
828  echo combo_box($name, $operators, FALSE, $data['operator']);
829  } else {
830  echo $data['operator'];
831  }
832  ?>
833  </td>
834  </tr>
835  <tr>
836  <?php
837  $name = $prefix.'_conditional_fee_additional_cost['.$key.']';
838  ?>
839  <td>
840  <?php echo translate('ecom_checkout_additional_cost').':'; ?>
841  </td>
842  <td>
843  <?php
844  if ($wa) {
845  echo text_box($name, $data['additional_cost'], 5);
846  } else {
847  echo $data['additional_cost'];
848  }
849  ?>
850  </td>
851  </tr>
852  <tr>
853  <?php
854  $name = $prefix.'_conditional_fee_remove_fees['.$key.']';
855  ?>
856  <td>
857  <?php echo translate('ecom_checkout_remove_fees').':'; ?>
858  </td>
859  <td>
860  <?php
861  if ($wa) {
862  echo check_box($name, '1', $data['remove_fees']);
863  } else {
864  if ($data['remove_fees']) {
865  echo translate('yes');
866  } else {
867  echo translate('no');
868  }
869  }
870  ?>
871  </td>
872  </tr>
873  <?php
874  if ($wa) {
875  ?>
876  <tr>
877  <td colspan="2">
878  <?php
879  $clear_button_name = $prefix.'_conditional_fee_clear_button['.$key.']';
880  $more_button_name = $prefix.'_conditional_fee_more_button['.$key.']';
881  echo normal_button($clear_button_name, translate('clear'), 'resetLastTableFields(this, true)');
882  // only print this button for last table
883  if ($key === count($conditions)-1) {
884  echo normal_button($more_button_name, translate('more').'...', 'var table = prependLastTable(this); resetLastTableFields(table, false)');
885  }
886  ?>
887  </td>
888  </tr>
889  <?php
890  }//end if
891  ?>
892  </table>
893  <?php
894  }//end foreach
895 
896  return TRUE;
897 
898  }//end paintConditionalDeliveryFee()
899 
900 
911  function processConditionalDeliveryFee(&$asset, &$o, $prefix)
912  {
913  if ($asset->writeAccess('attributes')) {
914  $countries_form_name = $prefix.'_conditional_fee_countries';
915  $comparison_price_form_name = $prefix.'_conditional_fee_comparison_price';
916  $operator_form_name = $prefix.'_conditional_fee_operator';
917  $additional_cost_form_name = $prefix.'_conditional_fee_additional_cost';
918  $remove_fees_form_name = $prefix.'_conditional_fee_remove_fees';
919  if (isset($_POST[$countries_form_name])) {
920  $conditions = Array();
921  foreach ($_POST[$countries_form_name] as $key => $codes) {
922  if (empty($codes[0]) || empty($_POST[$comparison_price_form_name][$key]) || empty($_POST[$operator_form_name][$key])) {
923  continue;
924  }
925  $conditions[$key] = Array(
926  'countries' => $codes,
927  'comparison_price' => (float) $_POST[$comparison_price_form_name][$key],
928  'operator' => $_POST[$operator_form_name][$key],
929  'additional_cost' => (float) $_POST[$additional_cost_form_name][$key],
930  );
931  // check for the remove_fees checkbox
932  if (isset($_POST[$remove_fees_form_name][$key])) {
933  $conditions[$key]['remove_fees'] = TRUE;
934  } else {
935  $conditions[$key]['remove_fees'] = FALSE;
936  }
937  }
938  $asset->setAttrValue('conditional_delivery_fees', $conditions);
939  return TRUE;
940  }
941  }
942  return FALSE;
943 
944  }//end processConditionalDeliveryFee()
945 
946 
957  function paintCustomiseReceipt(&$asset, &$o, $prefix)
958  {
959  $wa = $asset->writeAccess('attributes');
960  $customise_receipt = $asset->attr('customise_receipt');
961  if ($wa) {
962  $contents = Array(
963  '1' => translate('yes'),
964  '0' => translate('no'),
965  );
966  echo combo_box($prefix.'_customise_receipt', $contents, FALSE, $customise_receipt);
967  } else {
968  echo $customise_receipt ? translate('yes') : translate('no');
969  }
970 
971  return $wa;
972 
973  }//end paintCustomiseReceipt()
974 
975 
986  function processCustomiseReceipt(&$asset, &$o, $prefix)
987  {
988  $wa = $asset->writeAccess('attributes');
989  if ($wa) {
990  if (isset($_POST[$prefix.'_customise_receipt'])) {
991  $asset->setAttrValue('customise_receipt', (bool) $_POST[$prefix.'_customise_receipt']);
992  }
993  }
994 
995  return $wa;
996 
997  }//end processCustomiseReceipt()
998 
999 
1000  //-- CHECKOUT COMPLETION ACTIONS --//
1001 
1011  public function paintAllActions($asset, Backend_Outputter $o, $prefix)
1012  {
1013  $write_access = $asset->writeAccess('attributes');
1014  $actions = $asset->attr('actions');
1015 
1016  // Get selected action, which may have changed last commit
1017  if ($write_access) {
1018  $selected_action = array_get_index($_REQUEST, $prefix.'_new_selected_action', NULL);
1019  if (!is_numeric($selected_action)) {
1020  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
1021  }
1022  } else {
1023  $selected_action = NULL;
1024  }
1025 
1026  $o->openField('');
1027 
1028  hidden_field($prefix.'_new_selected_action', '');
1029 
1030  if (!empty($actions)) {
1031  ?><table class="sq-backend-table">
1032  <colgroup>
1033  <col width="60" align="center" />
1034  <col width="150" />
1035  <col/>
1036  <col width="60" align="center" />
1037  <col width="60" align="center" />
1038  </colgroup>
1039  <thead>
1040  <th><p>Edit</p></th>
1041  <th><p>Action Name / Type</p></th>
1042  <th><p>Summary</p></th>
1043  <th><p>Valid ?</p></th>
1044  <th><p>Active ?</p></th>
1045  <th><p>Delete ?</p></th>
1046  </thead><tbody><?php
1047 
1048  foreach ($actions as $key => $action) {
1049  $is_selected_action = (!is_null($selected_action) && ((string)$selected_action === (string)$key));
1050  ?><tr<?php
1051  if ($is_selected_action) {
1052  echo ' class="alt"';
1053  }
1054  ?>><?php
1055  $action_type = $action['type_code'];
1056  $settings = $action['settings'];
1057  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action_type, 'name');
1058 
1059  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action_type);
1060 
1061  if ($write_access && !$is_selected_action) {
1062  ?><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
1063  } else {
1064  ?><td class="sq-backend-table-cell">&nbsp;</td><?php
1065  }
1066  ?><td class="sq-backend-table-cell"><p><strong><?php echo $action['name'] ?></strong><br/>(<?php echo $type_name ?>)</p></td><?php
1067  ?><td class="sq-backend-table-cell"><?php echo call_user_func(Array($action_type, 'paintSummary'), $asset, $settings, $o, $prefix) ?></td><?php
1068  ?><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
1069  if ($write_access) {
1070  ?><td class="sq-backend-table-cell"><p><?php check_box($prefix.'_rules[active]['.$key.']', '1', $action['active']) ?></p></td><?php
1071  ?><td class="sq-backend-table-cell"><p><?php check_box($prefix.'_rules[delete]['.$key.']', '1', FALSE) ?></p></td><?php
1072  } else {
1073  ?><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
1074  ?><td class="sq-backend-table-cell">&nbsp;</td><?php
1075  }
1076  ?></tr><?php
1077  }
1078  ?></tbody></table><?php
1079  } else {
1080  ?><p>There are no actions currently defined for this form.</p><?php
1081  }
1082 
1083  $o->closeField();
1084  $o->sectionNote('Only actions that are <strong>valid</strong> and <strong>active</strong> will be executed when the checkout is completed. An action can be activated or deactivated with the <strong>Active ?</strong> check box.');
1085 
1086  return $write_access;
1087 
1088  }//end paintAllActions()
1089 
1090 
1100  public function processAllActions($asset, Backend_Outputter $o, $prefix)
1101  {
1102  $write_access = $asset->writeAccess('attributes');
1103 
1104  // If we have selected "edit action", then we don't want this to run,
1105  // because we only want active/delete changes to take effect upon
1106  // hitting the commit button.
1107  $edit_action = array_get_index($_REQUEST, $prefix.'_new_selected_action', NULL);
1108  if (!empty($edit_action)) {
1109  return $write_access;
1110  }
1111 
1112  if ($write_access) {
1113  $actions = $asset->attr('actions');
1114 
1115  $rule_changes = array_get_index($_POST, $prefix.'_rules', Array());
1116 
1117  if(!empty($actions)) {
1118  foreach ($actions as $key => $action) {
1119  if (!isset($rule_changes['active'][$key])) {
1120  $rule_changes['active'][$key] = Array();
1121  }
1122  }
1123  } else {
1124  if (!isset($rule_changes['active'])) {
1125  $rule_changes['active'] = Array();
1126  }
1127  }
1128 
1129  if (!isset($rule_changes['delete'])) {
1130  $rule_changes['delete'] = Array();
1131  }
1132 
1133  foreach ($rule_changes['active'] as $key => $value) {
1134  $actions[$key]['active'] = (boolean)$value;
1135  }
1136  foreach ($rule_changes['delete'] as $key => $value) {
1137  unset($actions[$key]);
1138  }
1139 
1140  $asset->setAttrValue('actions', $actions);
1141  if (!$asset->saveAttributes()) return FALSE;
1142  }
1143 
1144  return $write_access;
1145 
1146  }//end processAllActions()
1147 
1148 
1158  public function paintNewAction($asset, Backend_Outputter $o, $prefix)
1159  {
1160  $write_access = $asset->writeAccess('attributes');
1161 
1162  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('checkout_action');
1163  $names = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($types, 'name');
1164 
1165  $o->openField('');
1166  if ($write_access) {
1167  ?><p><?php
1168  check_box($prefix.'_new', '1', FALSE, 'this.form.'.$prefix.'_new_type.disabled = !this.checked; return true;');
1169  ?><label for="<?php echo $prefix ?>_new">Create a new action of type:</label> <?php
1170  $names = Array('' => 'Select action type...') + $names;
1171  combo_box($prefix.'_new_type', $names, FALSE, '', 0, 'disabled="disabled"');
1172  ?></p><?php
1173  } else {
1174  ?><p>This screen must be locked before you can create a new action.</p><?php
1175  }
1176 
1177  // If we have a new action, are we
1178  $o->closeField();
1179 
1180  $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.');
1181 
1182  return $write_access;
1183 
1184  }//end paintNewAction()
1185 
1186 
1196  public function processNewAction($asset, Backend_Outputter $o, $prefix)
1197  {
1198  $write_access = $asset->writeAccess('attributes');
1199 
1200  if ($write_access) {
1201  // First check that we are creating a new rule.
1202  $new_action = (int)array_get_index($_POST, $prefix.'_new', '0');
1203 
1204  if ($new_action) {
1205  // Paranoia, paranoia...
1206  // Check whether the type we've been passed is a valid type,
1207  // to protect possible stuffed POSTDATA (including a blank)
1208  $action_type = array_get_index($_POST, $prefix.'_new_type', '');
1209  if (!empty($action_type)) {
1210  if ($GLOBALS['SQ_SYSTEM']->am->installed($action_type)) {
1211  // Also check whether it's a valid form action
1212  $type_parents = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($action_type);
1213  if (in_array('checkout_action', $type_parents)) {
1214  // okay, we seem to be in the clear
1215  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action_type, 'name');
1216  $actions = $asset->attr('actions');
1217 
1218  if (!empty($actions)) {
1219  $new_key = max(array_keys($actions)) + 1;
1220  } else {
1221  $new_key = 0;
1222  }
1223 
1224  $actions[$new_key] = Array(
1225  'name' => $this->_makeValidActionName($asset, $type_name),
1226  'type_code' => $action_type,
1227  'settings' => NULL,
1228  'active' => TRUE,
1229  );
1230  $asset->setAttrValue('actions', $actions);
1231 
1232  // Set new selected action
1233  $_POST[$prefix.'_new_selected_action'] = $new_key;
1234 
1235  if (!$asset->saveAttributes()) return FALSE;
1236  } else {
1237  trigger_error('Cannot create new action; asset type "'.$action_type.'" is not a checkout action', E_USER_WARNING);
1238  return FALSE;
1239  }
1240  } else {
1241  trigger_error('Cannot create new action; "'.$action_type.'" is not a valid asset type', E_USER_WARNING);
1242  return FALSE;
1243  }
1244  }
1245  }
1246 
1247  }
1248 
1249  return $write_access;
1250 
1251  }//end processNewAction()
1252 
1253 
1263  public function paintSelectedAction($asset, Backend_Outputter $o, $prefix)
1264  {
1265  $write_access = $asset->writeAccess('attributes');
1266  $selected_action = array_get_index($_REQUEST, $prefix.'_new_selected_action', NULL);
1267  if (!is_numeric($selected_action)) {
1268  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
1269  }
1270 
1271  if ($write_access) {
1272  if (!is_null($selected_action)) {
1273  $actions = $asset->attr('actions');
1274 
1275  // Get the current action and process - but not if we've
1276  // already been deleted further up!
1277  if (!isset($actions[$selected_action])) {
1278  $o->openField('');
1279  ?><p>No action is currently selected for editing. To edit an action, please select from the "Existing Actions" list above.</p><?php
1280  $o->closeField();
1281  return FALSE;
1282  }
1283 
1284  $action = $actions[$selected_action];
1285 
1286  $type_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action['type_code'], 'name');
1287 
1288  $o->openField('Action Name');
1289 
1290  text_box($prefix.'_selected_name', $action['name'], 30);
1291  $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.');
1292 
1293  $o->closeField();
1294  $o->openField('Settings');
1295 
1296  hidden_field($prefix.'_selected_action', $selected_action);
1297  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action['type_code']);
1298  call_user_func(Array($action['type_code'], 'paintInlineInterface'), $asset, $action['settings'], $o, $prefix);
1299 
1300  $o->closeField();
1301 
1302  } else {
1303  $o->openField('');
1304  ?><p>No action is currently selected for editing. To edit an action, please select from the "Existing Actions" list above.</p><?php
1305  $o->closeField();
1306 
1307  }//end if there is a selected action
1308  } else {
1309  $o->openField('');
1310  ?><p>This screen must be locked before you can edit an action.</p><?php
1311  $o->closeField();
1312  }//end if write_access
1313 
1314  return $write_access;
1315 
1316  }//end paintSelectedAction()
1317 
1318 
1330  function _paintAssetTypeChooser($field_name, $selected_type_code)
1331  {
1332  $asset_types = $GLOBALS['SQ_SYSTEM']->am->getAssetTypes(TRUE);
1333 
1334  $all_types = Array();
1335  foreach ($asset_types as $type_code => $info) {
1336  $all_types[$type_code] = $info['name'];
1337  $type_anc = $GLOBALS['SQ_SYSTEM']->am->getTypeAncestors($type_code);
1338 
1339  foreach ($type_anc as $anc_type_code) {
1340  if ($anc_type_code == 'asset') continue;
1341  // save on db calls if we have this ancestor already
1342  if (!in_array($anc_type_code, array_keys($all_types))) {
1343  $all_types[$anc_type_code] = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($anc_type_code, 'name');
1344  }
1345  }
1346  }
1347  $all_types = array_unique($all_types);
1348  arsort($all_types);
1349  $all_types['asset'] = '-- '.translate('sch_all_asset_types').' --';
1350  $all_types[''] = '';
1351  $all_types = array_reverse($all_types);
1352 
1353  combo_box($field_name, $all_types, FALSE, $selected_type_code);
1354  echo '&nbsp;';
1355  submit_button($field_name.'_go', translate('go'), 'main_form.submit()');
1356 
1357  }//end _paintAssetTypeChooser()
1358 
1368  public function processSelectedAction($asset, Backend_Outputter $o, $prefix)
1369  {
1370  $write_access = $asset->writeAccess('attributes');
1371  $actions = $asset->attr('actions');
1372  $result = FALSE;
1373 
1374  // If an "edit action" has been clicked, then we've lost the changes
1375  // to the current rule.
1376  $edit_action = array_get_index($_REQUEST, $prefix.'_new_selected_action', NULL);
1377  if (is_numeric($edit_action)) {
1378  return FALSE;
1379  }
1380 
1381  // If not, then get the current action and process - but not if we've
1382  // already been deleted!
1383  $selected_action = array_get_index($_POST, $prefix.'_selected_action', NULL);
1384  if (!isset($actions[$selected_action])) {
1385  return FALSE;
1386  }
1387 
1388  if ($write_access) {
1389  if (!is_null($selected_action)) {
1390  $new_name = array_get_index($_POST, $prefix.'_selected_name', NULL);
1391  $action = $actions[$selected_action];
1392 
1393  $new_name = array_get_index($_POST, $prefix.'_selected_name', NULL);
1394  if ($new_name != $action['name']) {
1395  if (empty($new_name)) {
1396  $new_name = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($action['type_code'], 'name');
1397  }
1398  $actions[$selected_action]['name'] = $this->_makeValidActionName($asset, $new_name);
1399  }
1400 
1401  $GLOBALS['SQ_SYSTEM']->am->includeAsset($action['type_code']);
1402  $settings = $action['settings'];
1403 
1404  // Bug Fix #3145
1405  // call_user_func doesn't work with pass-by-reference in PHP5, so using call_user_func_array instead
1406  $result = call_user_func_array(Array($action['type_code'], 'processInlineInterface'), Array($asset, &$settings, $o, $prefix));
1407  if ($result) {
1408  $actions[$selected_action]['settings'] = $settings;
1409  $asset->setAttrValue('actions', $actions);
1410  if (!$asset->saveAttributes()) return FALSE;
1411  }
1412  } else {
1413  return FALSE;
1414  }
1415  }
1416 
1417  return ($result && $write_access);
1418 
1419  }//end processSelectedAction()
1420 
1421 
1434  protected function _makeValidActionName($asset, $base_name)
1435  {
1436  $actions = $asset->attr('actions');
1437 
1438  $name = $base_name;
1439  $i = 1;
1440 
1441  do {
1442  $found = FALSE;
1443 
1444  foreach ($actions as $action) {
1445  if ($action['name'] == $name) {
1446  $found = TRUE;
1447 
1448  // Incrememt the base name
1449  $i++;
1450  $name = $base_name.' '.$i;
1451  break;
1452  }
1453  }
1454 
1455  } while ($found);
1456 
1457  return $name;
1458 
1459  }//end _makeValidActionName()
1460 
1461 
1462 }//end class
1463 
1464 ?>