Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
promotional_rule_type_quantity_edit_fns.inc
1 <?php
18 require_once dirname(__FILE__).'/../../promotional_rule/promotional_rule_edit_fns.inc';
19 
32 {
33 
34 
38  function __construct()
39  {
40  parent::__construct();
41 
42  }//end constructor
43 
44 
57  function paintRule(&$asset, &$o, $prefix, $rule_data, $write_access)
58  {
59  // Setup default values if there is no rule data
60  if (is_null($rule_data)) {
61  $rule_data = $asset->getDefaultRuleData();
62  }
63 
64  // Paint the discount type selection
65  $o->openField(translate('ecom_promotional_rule_type_quantity_discount_type'));
66  $discount_types = $asset->getDiscountTypes();
67  if ($write_access) {
68  combo_box($prefix.'[discount_type]', $discount_types, FALSE, $rule_data['discount_type']);
69  } else {
70  echo $discount_types[$rule_data['discount_type']];
71  }
72  $o->note(translate('ecom_promotional_rule_type_quantity_discount_type_note'));
73  $o->closeField();
74 
75  // Paint the new discount option if we have write access
76  if ($write_access) {
77  $o->openField(translate('ecom_promotional_rule_type_quantity_new_discount_option'));
78  echo '<b>'.translate('ecom_promotional_rule_type_quantity_asset_quantity').'</b>&nbsp;';
79  text_box($prefix.'[new_asset_quantity]');
80  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
81  echo '<b>'.translate('ecom_promotional_rule_type_quantity_discount_amount').'</b>&nbsp;';
82  text_box($prefix.'[new_discount_amount]');
83  $o->closeField();
84  }
85 
86  // Paint the discount options
87  $o->openField(translate('ecom_promotional_rule_type_quantity_discount_options'));
88  if (empty($rule_data['discount_options'])) {
89  echo translate('ecom_promotional_rule_type_quantity_no_discount_options');
90  } else {
91  ?>
92  <table class="sq-backend-table">
93  <tr>
94  <td class="sq-backend-table-header"><?php echo translate('ecom_promotional_rule_type_quantity_asset_quantity'); ?></td>
95  <td class="sq-backend-table-header"><?php echo translate('ecom_promotional_rule_type_quantity_discount_amount'); ?></td>
96  <?php
97  if ($write_access) {
98  ?>
99  <td class="sq-backend-table-header"><?php echo translate('delete_question'); ?></td>
100  <?php
101  }
102  ?>
103  </tr>
104  <?php
105  $i = 0;
106  foreach ($rule_data['discount_options'] as $asset_quantity => $discount_amount) {
107  ?>
108  <tr>
109  <td class="sq-backend-table-cell">
110  <?php
111  if ($write_access) {
112  text_box($prefix."[discount_options][$i][asset_quantity]", $asset_quantity);
113  } else {
114  echo $asset_quantity;
115  }
116  ?>
117  </td>
118  <td class="sq-backend-table-cell">
119  <?php
120  if ($write_access) {
121  text_box($prefix."[discount_options][$i][discount_amount]", $discount_amount);
122  } else {
123  echo $discount_amount;
124  }
125  ?>
126  </td>
127  <?php
128  if ($write_access) {
129  ?>
130  <td class="sq-backend-table-cell">
131  <?php
132  check_box($prefix."[discount_options][$i][delete]");
133  ?>
134  </td>
135  <?php
136  }
137  ?>
138  </tr>
139  <?php
140  $i++;
141  }
142  ?>
143  </table>
144  <?php
145  }
146  $o->closeField();
147 
148  // Add section note
149  $o->sectionNote(translate('ecom_promotional_rule_type_quantity_section_note'));
150 
151  return $write_access;
152 
153  }//end paintRule()
154 
155 
167  function processRule(&$asset, &$o, $prefix, &$rule_data)
168  {
169  if (!isset($_POST[$prefix])) return FALSE;
170 
171  // Setup default values if there is no rule data
172  if (is_null($rule_data)) {
173  $rule_data = $asset->getDefaultRuleData();
174  }
175 
176  // Save discount type
177  $discount_type = array_get_index($_POST[$prefix], 'discount_type');
178  if (array_key_exists($discount_type, $asset->getDiscountTypes())) {
179  $rule_data['discount_type'] = $discount_type;
180  }
181 
182  // Save the current discount options
183  $discount_options = Array();
184  $post_discount_options = array_get_index($_POST[$prefix], 'discount_options', Array());
185  foreach ($post_discount_options as $i => $option) {
186  // Do not save the option if its delete checkbox is selected
187  if (isset($option['delete'])) continue;
188 
189  // Add the option to the option list if the inputs are valid
190  $asset_quantity = array_get_index($option, 'asset_quantity');
191  $discount_amount = array_get_index($option, 'discount_amount');
192  if (is_numeric($asset_quantity) && is_numeric($discount_amount)) {
193  $asset_quantity = (int) $asset_quantity;
194  if ($asset_quantity > 0) {
195  $discount_options[$asset_quantity] = (float) $discount_amount;
196  }
197  }
198  }
199 
200  // Add new discount option
201  $new_asset_quantity = array_get_index($_POST[$prefix], 'new_asset_quantity');
202  $new_discount_amount = array_get_index($_POST[$prefix], 'new_discount_amount');
203 
204  if (is_numeric($new_asset_quantity) && is_numeric($new_discount_amount)) {
205  $new_asset_quantity = (int) $new_asset_quantity;
206  if ($new_asset_quantity > 0) {
207  $discount_options[$new_asset_quantity] = (float) $new_discount_amount;
208  }
209  }
210 
211  // Sort the asset quantiy ascendingly
212  if (!ksort($discount_options)) return FALSE;
213 
214  // Set the new discount options to rule data
215  $rule_data['discount_options'] = $discount_options;
216 
217  return TRUE;
218 
219  }//end processRule()
220 
221 
222 }//end class
223 
224 ?>