Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ecommerce_order.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 
19 define('SQ_ECOM_ORDER_STATUS_PROCESSING', 0);
20 define('SQ_ECOM_ORDER_STATUS_SUCCESS', 1);
21 define('SQ_ECOM_ORDER_STATUS_FAILED', 2);
22 
23 
34 class Ecommerce_Order extends Asset
35 {
36 
37 
46  function Ecommerce_Order($assetid=0)
47  {
48  $this->_ser_attrs = true;
49  $this->Asset($assetid);
50 
51  }//end constructor
52 
53 
64  function _createAdditional(&$link)
65  {
66  if (!parent::_createAdditional($link)) return false;
67 
68  // set an initial web path
69  $initial_path = $this->id;
70  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
71  $valid_paths = make_valid_web_paths(Array($initial_path));
72  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($link['asset'], $valid_paths, $this->id, true);
73  return $this->saveWebPaths($good_paths);
74 
75  }//end _createAdditional()
76 
77 
91  function _getAllowedLinks()
92  {
93  $allowed_links = parent::_getAllowedLinks();
94  $allowed_links[SQ_LINK_NOTICE]['product'] = Array('card' => 'M', 'exclusive' => false);
95 
96  return $allowed_links;
97 
98  }//end _getAllowedLinks()
99 
100 
110  function create(&$link)
111  {
112  return parent::create($link);
113 
114  }//end create()
115 
116 
126  function setAttrValue($name, $value)
127  {
128  switch ($name) {
129  case 'products':
130  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
131  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
132 
133  $this->_linkProducts($value);
134 
135  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
136  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
137  break;
138  }
139  return parent::setAttrValue($name, $value);
140 
141  }//end setAttrValue()
142 
143 
152  function _linkProducts($products)
153  {
154  if (empty($products)) {
155  trigger_localised_error('ECOM0014', E_USER_WARNING);
156  return false;
157  }
158 
159  // set run level
160  $run_level_changed = false;
161  if (!$GLOBALS['SQ_SYSTEM']->runLevelEnables(SQ_RUN_LEVEL_FORCED)) {
162  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
163  $run_level_changed = true;
164  }
165 
166  // add items
167  $success = true;
168  foreach ($products as $id => $value) {
169  // put notice link
170  $product = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
171 
172  if ($GLOBALS['SQ_SYSTEM']->am->createAssetLink($this, $product, SQ_LINK_NOTICE) == 0) {
173  trigger_localised_error('ECOM0019', E_USER_WARNING);
174  $success = false;
175  }
176  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($product);
177  }
178 
179  // reset run level to old value
180  if ($run_level_changed) {
181  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
182  }
183 
184  return $success;
185 
186  }//end _linkProducts()
187 
188 
197  function setOrderStatus($status, $message='')
198  {
199  // set status
200  if (is_numeric($status)) {
201 
202  switch ($status) {
203  case SQ_ECOM_ORDER_STATUS_PROCESSING :
204  $new_status = SQ_ECOM_ORDER_STATUS_PROCESSING;
205  if (empty($message)) {
206  $status_message = translate('ecom_order_status_processing');
207  } else {
208  $status_message = $message;
209  }
210  break;
211  case SQ_ECOM_ORDER_STATUS_SUCCESS :
212  $new_status = SQ_ECOM_ORDER_STATUS_SUCCESS;
213  $status_message = translate('ecom_order_status_success');
214  break;
215  case SQ_ECOM_ORDER_STATUS_FAILED :
216  $new_status = SQ_ECOM_ORDER_STATUS_FAILED;
217  $status_message = translate('ecom_order_status_failed');
218  break;
219  default:
220  return false;
221  break;
222  }
223 
224  $success = true;
225 
226  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
227  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
228 
229  // set runlevel
230  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
231 
232  // set status
233  $this->setAttrValue('status', $new_status);
234 
235  // set status message
236  if (!empty($status_message)) {
237  $this->setAttrValue('status_message', $status_message);
238  } else {
239  $success = false;
240  }
241 
242  // save attributes
243  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'attributes')) {
244  $success = false;
245  } else if (!$this->saveAttributes()) {
246  $success = false;
247  }
248  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'attributes');
249 
250  // restore runlevel
251  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
252 
253  if ($success) {
254  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
255  } else {
256  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
257  }
258 
259  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
260  return $success;
261 
262  } else {
263  return false;
264  }
265 
266  }//end setOrderStatus()
267 
268 
275  function getOrderStatus()
276  {
277  $result = Array(
278  'status' => $this->attr('status'),
279  'status_message' => $this->attr('status_message'),
280  );
281 
282  return $result;
283 
284  }//end getOrderStatus()
285 
286 
287 //-- KEYWORDS --//
288 
289 
307  {
308  $keywords = parent::getAvailableKeywords();
309 
310  $keywords['checkout_assetid'] = translate('ecom_order_keyword_checkout_assetid');
311  $keywords['order_summary'] = translate('ecom_order_keyword_order_summary');
312  $keywords['status'] = translate('ecom_order_keyword_status');
313  $keywords['status_message'] = translate('ecom_order_keyword_status_message');
314  $keywords['edit_url'] = translate('ecom_order_keyword_edit_url');
315  $keywords['order_xml'] = translate('ecom_order_keyword_order_xml');
316  $keywords['order_grand_total'] = translate('ecom_order_keyword_order_grand_total');
317 
318  return $keywords;
319 
320  }//end getAvailableKeywords()
321 
322 
332  {
333  if (!isset($this->_tmp['checkout_asset'])) {
334  $link_to_checkout = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'ecommerce_checkout', true, 'checkout', 'minor');
335  $this->_tmp['checkout_asset'] = array_get_index($link_to_checkout, 'majorid', 0);
336  }
337 
338  return $this->_tmp['checkout_asset'];
339 
340  }//end _getCheckoutAssetId()
341 
342 
352  {
353  return $this->_getCheckoutAssetId();
354 
355  }//end getCheckoutAssetidKeywordReplacement()
356 
357 
365  {
366  return $this->attr('summary');
367 
368  }//end getOrderSummaryKeywordReplacement()
369 
370 
378  {
379  return $this->attr('status');
380 
381  }//end getStatusKeywordReplacement()
382 
383 
391  {
392  return $this->attr('status_message');
393 
394  }//end getStatusMessageKeywordReplacement()
395 
396 
404  {
405  // get ecommerce checkout asset id
406  $id = $this->_getCheckoutAssetId();
407 
408  // get url of checkout form and append extra variable
409  $url = $GLOBALS['SQ_SYSTEM']->am->getAssetURL($id);
410 
411  $connector = '';
412  if (empty($url)) {
413  return '';
414  } else {
415  strstr($url, '?') ? $connector = '&' : $connector = '?';
416  }
417  $url .= $connector.'edit_order_id='.$this->id;
418 
419  return '<a href="'.$url.'">'.$url.'</a>';
420 
421  }//end getEditUrlKeywordReplacement()
422 
423 
431  {
432  return $this->attr('order_xml');
433 
434  }//end getOrderXmlKeywordReplacement()
435 
436 
444  {
445  return $this->attr('order_grand_total');
446 
447  }//end getCheckoutAssetidKeywordReplacement()
448 
449 
456  function &getDeliveryMethod()
457  {
458  $method = null;
459  if ($this->attr('delivery_id') != 0) {
460  $method =& $GLOBALS['SQ_SYSTEM']->am->getAsset($this->attr('delivery_id'));
461  }
462 
463  return $method;
464 
465  }//end getDeliveryMethod()
466 
467 
475  function appendAuditLog($msg)
476  {
477  $order_history = $this->attr('order_history');
478  $index = date('r');
479  while (isset($order_history[$index])) {
480  $index .= ' ';
481  }
482  $order_history[$index] = $msg;
483  $this->setAttrValue('order_history', $order_history);
484 
485  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
486 
487  $this->saveAttributes();
488 
489  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
490 
491  }//end appendAuditLog()
492 
493 
494 }//end class
495 
496 ?>