Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
product.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
18 require_once SQ_FUDGE_PATH.'/general/text.inc';
19 require_once dirname(__FILE__).'/../interfaces/buyable/buyable.inc';
20 
31 //register_implementation('product', 'buyable');
32 class Product extends Page implements Buyable
33 {
34 
35 
44  function __construct($assetid=0)
45  {
46  $this->_ser_attrs = TRUE;
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
60  function __wakeup()
61  {
62  // reload product price object as attribute var
63  $this->_loadVars();
64  }//end __wakeup()
65 
66 
75  function _loadVars()
76  {
77  // include price class so unserialize() doesn't barf
78  require_once SQ_PACKAGES_PATH.'/ecommerce/price/product_price.inc';
79  parent::_loadVars();
80 
81  }//end _loadVars()
82 
83 
94  protected function _getName($short_name=FALSE, $contextid=NULL)
95  {
96  // No context specified, using the current context
97  if ($contextid === NULL) {
98  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
99  }//end if
100 
101  // Obtain the attribute value for Name from the specified Context
102  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
103  if (empty($values) === TRUE) {
104  return parent::_getName($short_name, $contextid);
105  } else {
106  return $values[$this->id];
107  }
108 
109  }//end _getName()
110 
111 
123  function setAttrValue($name, $value)
124  {
125  switch ($name) {
126  case 'price' :
127  $price_obj = $this->attr('price_obj');
128  $price_obj->value = $value;
129  return $this->setAttrValue('price_obj', $price_obj);
130  break;
131  case 'price_obj' :
132  if (!parent::setAttrValue($name, $value)) return FALSE;
133  $price_obj = $this->attr('price_obj');
134  if (!parent::setAttrValue('price', $price_obj->value)) {
135  return FALSE;
136  }
137  return TRUE;
138  break;
139  default :
140  return parent::setAttrValue($name, $value);
141  }
142 
143  }//end setAttrValue()
144 
145 
155  function printFrontend()
156  {
157  // if we just submitted this form
158  if (!is_null($qty = array_get_index($_POST, 'quantity', NULL))) {
159 
160  if ($qty > 0) {
161  $this->processSelection();
162  // if form submitted but invalid quantity
163  } else if ($qty < 1 || !is_int($qty)) {
164  // invalid selection - set flag here;
165  }
166  }
167 
168  parent::printFrontend();
169 
170  }//end printFrontend()
171 
172 
181  function printBody()
182  {
183  ?>
184  <form action="<?php echo $this->getURL(); ?>" method="post">
185  <?php
186 
187  $replacement_array = Array();
188  $keywords = $this->getAssetKeywords();
189  // getAssetKeyword returns a array with the structure
190  // $keywords = array( key => array( description => $decription,
191  // value => $value,
192  // )
193  // )
194  // but..
195  // replace_keywords expect array ( key => value)
196  // so we rebuild this structure
197  foreach ($keywords as $keyword => $info) {
198  $replacement_array[$keyword] = $info['value'];
199  }
200  $product_description = $this->attr('description');
201  echo replace_keywords($product_description, $replacement_array);
202 
203  ?>
204  </form>
205  </div>
206  <?php
207 
208  }//end printBody()
209 
210 
219  function processSelection()
220  {
221 
222  $cart_assetid = $this->getActiveCartAssetid();
223 
224  if ($cart_assetid === FALSE) {
225  trigger_localised_error('ECOM0011', E_USER_WARNING);
226  return FALSE;
227  }
228 
229  if (!is_numeric($qty = $_POST['quantity'])) {
230  trigger_localised_error('ECOM0012', E_USER_WARNING);
231  return FALSE;
232  }
233 
234  // get the cart asset
235  $cart =& $GLOBALS['SQ_SYSTEM']->am->getAsset($cart_assetid);
236 
237  // update item and quantity to cart
238  if (isset($_SESSION['sq_cart_contents'][$this->id])) {
239  $cart->update($this->id, (int)$qty);
240  } else {
241  $cart->add($this->id, $this->attr('price'), (int)$qty);
242  }
243  return TRUE;
244 
245  }//end processSelection()
246 
247 
257  {
258  $parents = array_keys($GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'ecommerce_cart', FALSE));
259 
260  switch (count($parents)) {
261  case 0:
262  return FALSE;
263  break;
264 
265  case 1:
266  return reset($parents);
267  break;
268 
269  default:
270  // get current lineage and reverse it so the closest assets are first
271  $lineage = array_reverse($GLOBALS['SQ_SYSTEM']->am->getLineageFromURL());
272 
273  // return the first cart we find, ie. the one closest to us
274  foreach ($lineage as $lineage_item) {
275  if (FALSE !== ($index = array_search($lineage_item['assetid'], $parents))) {
276  return $parents[$index];
277  }
278  }
279 
280  // can't find any of the carts!
281  return FALSE;
282 
283  break;
284  }
285 
286  }//end getActiveCartAssetid()
287 
288 
309  function getAssetKeywords($descriptions=FALSE)
310  {
311  $keywords = parent::getAssetKeywords($descriptions);
312 
313  // only expose the image stuff if we are a real asset (and not a dunmmy
314  // one, as used in the asset listing to put our keywords in a drop down)
315  if ($this->id) {
316 
317  // Adjust the quantity/commit keywords depending in whether there are
318  // items in the cart already
319  if (isset($_SESSION['sq_cart_contents'])) {
320 
321  $qty = array_get_index($_SESSION['sq_cart_contents'], $this->id, Array('quantity' => 0));
322  if ($qty['quantity'] != 0) {
323  $keywords['quantity_field']['value'] = '<input type="text" name="update_quantity['.$this->id.']" size="10" value="'.$qty['quantity'].'" />';
324  $keywords['commit_button']['value'] = '<input type="submit" value="Update Quantity" />';
325  } else {
326  $keywords['quantity_field']['value'] = '<input type="text" name="quantity" size="10" />';
327  $keywords['commit_button']['value'] = '<input type="submit" value="Add to Cart" />';
328  }
329 
330  }
331 
332  $images = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($this->id, 'image', FALSE));
333  foreach ($images as $image_assetid) {
334  $keywords['image_'.$image_assetid]['value'] = '<img src="'.$GLOBALS['SQ_SYSTEM']->am->getAssetURL($image_assetid).'">';
335  }
336 
337  }
338 
339  return $keywords;
340 
341  }//end getAssetKeywords()
342 
343 
350  public function getPrice() {
351  $price_obj = $this->attr('price_obj');
352  if (is_null($price_obj->value)) return 0;
353  return $price_obj->value;
354 
355  }//end getPrice()
356 
357 
358 }//end class
359 ?>