17 require_once SQ_PACKAGES_PATH.
'/cms/listing_engine/listing_engine.inc';
18 require_once SQ_PACKAGES_PATH.
'/ecommerce/lib/ecommerce_cart_processor.inc';
20 define(
'SQ_CART_PRESET_QUANTITY',
'Quantity');
21 define(
'SQ_CART_PRESET_REMOVE',
'Remove');
53 var $insufficient_quantities = FALSE;
60 var $insufficient_quantity_assets = NULL;
73 parent::__construct($assetid);
88 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
90 $copy_link = Array(
'asset' => &$this,
'value' =>
'page_contents' ,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
91 $asset->setAttrValue(
'name',
'Page Contents');
92 return $asset->create($copy_link);
105 if (is_null($this->_cart)) {
108 if ($this->
attr(
'use_local_cart')) {
109 $cart->setCartContainer($_SESSION[
'sq_local_cart_contents'][
'cart_contents']);
112 $this->_cart = $cart;
141 $cart_contents = $cart->getCart();
143 $asset_list = Array();
145 $group_by = $this->
attr(
'group_by');
147 foreach ($cart_contents as $product_assetid => $product_info) {
148 $product_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($product_assetid);
149 if (!empty($product_asset)) {
150 if ($group_by ==
'letter') {
151 $asset_list[$product_assetid] = Array( 0 => Array(
152 'type_code' => $product_asset->type(),
153 'first_letter' => substr($product_asset->attr(
'name'), 0, 1),
157 $asset_list[$product_assetid] = Array( 0 => Array(
158 'type_code' => $product_asset->type()
165 if (empty($asset_list)) $asset_list =
'';
181 $page_links = parent::_getAllowedLinks();
182 $page_links[SQ_LINK_TYPE_1][
'folder'] = Array(
'card' =>
'M');
183 $page_links[SQ_LINK_TYPE_1][
'product'] = Array(
'card' =>
'M');
184 $page_links[SQ_LINK_TYPE_2][
'bodycopy'] = Array(
'card' =>
'M');
200 protected function _getName($short_name=FALSE, $contextid=NULL)
203 if ($contextid === NULL) {
204 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
208 $attr = ($short_name) ?
'short_name' :
'name';
209 $values = $GLOBALS[
'SQ_SYSTEM']->am->getAttributeValuesByName($attr, $this->
type(), Array($this->
id), $contextid);
210 if (empty($values) === TRUE) {
211 return parent::_getName($short_name, $contextid);
213 return $values[$this->id];
229 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
232 parent::printFrontend();
234 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
260 $bodycopy_link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'page_contents');
261 if (empty($bodycopy_link))
return FALSE;
263 $format_bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($bodycopy_link[
'minorid'], $bodycopy_link[
'minor_type_code']);
264 if (is_null($format_bodycopy))
return FALSE;
266 require_once SQ_FUDGE_PATH.
'/general/text.inc';
267 $keywords = $format_bodycopy->getKeywords();
268 $replacements = Array();
270 if (!in_array(
'asset_listing', $keywords)) {
273 $format_bodycopy->printBody();
279 $num_kids = count($children);
283 if ($this->
attr(
'group_by') ==
'grouped') {
285 $this->
sortGroups($this->
attr(
'asset_grouping'), $children, $children);
288 foreach ($keywords as $word) {
295 if (!empty($this->_tmp[
'cart_updated'])) {
296 $replacements[
'cart_updated'] = $this->
attr(
'cart_updated_text');
298 $replacements[
'cart_updated'] =
'';
303 $asset_result_page_var =
'result_'.$this->id.
'_result_page';
304 $generic_result_page_var =
'result_page';
308 if (isset($_REQUEST[$generic_result_page_var])) {
309 $result_page = (int)$_REQUEST[$generic_result_page_var];
311 if (isset($_REQUEST[$asset_result_page_var])) {
312 $result_page = (int)$_REQUEST[$asset_result_page_var];
315 if ($result_page <= 0) $result_page = 1;
318 if (empty($children)) {
319 $replacements[
'asset_listing'] = $this->
attr(
'no_items_text');
320 $replacements[
'next_page'] =
'';
321 $replacements[
'previous_page'] =
'';
322 $replacements[
'total_pages'] = 1;
325 $replacements[
'page_list'] =
'1';
326 $global_contents = $replacements[
'asset_listing'];
327 $num_assets_showing = 0;
330 <script
type=
"text/javascript">
337 $todo =& $this->
getChunk($children, $replacements, Array(), $result_page, $num_per_page);
338 $num_assets_showing = count($todo);
342 $global_contents = ob_get_contents();
347 $global_contents = preg_replace(
'|%[^%\W]+%|',
'', $global_contents);
350 $checkout_id = $this->
attr(
'ecommerce_checkout_id');
351 $checkout_cart_button =
'';
352 $checkout_cart_href =
'';
353 if (!empty($checkout_id) && !empty($children)) {
354 if (!$GLOBALS[
'SQ_SYSTEM']->am->assetExists($checkout_id)) {
355 trigger_localised_error(
'ECOM003', E_USER_WARNING, $checkout_id);
357 $checkout_cart_href = $GLOBALS[
'SQ_SYSTEM']->am->getAssetURL($checkout_id);
359 ?><input
type=
"button" onclick=
"location.href = '<?php echo addslashes($checkout_cart_href); ?>'" value=
"<?php echo $this->attr('checkout_text'); ?>" /><?php
360 $checkout_cart_button = ob_get_contents();
368 if ($num_per_page > 0) {
370 hidden_field(
'result_page', $result_page);
374 $replacements[
'checkout_cart_button'] = $checkout_cart_button;
375 $replacements[
'checkout_cart_href'] = $checkout_cart_href;
378 if (in_array(
'cart_update_button', $keywords)) {
381 $replacements[
'asset_listing'] = $global_contents;
382 $replacements[
'asset_count'] = $num_kids;
383 $replacements[
'page_asset_count'] = $num_assets_showing;
384 $replacements[
'page_number'] = $result_page;
385 $replacements[
'first_asset_position'] = $num_per_page * ($result_page - 1) + 1;
386 $replacements[
'last_asset_position'] = min($num_kids, $num_per_page * $result_page);
387 $replacements[
'root_nodes'] = implode(
',', $this->
getRootNodes());
392 $format_bodycopy->setKeywordReplacements($replacements);
395 if (!in_array(
'select_all_js_code', $keywords)) {
400 $format_bodycopy->printBody();
419 $cart_contents = $cart->getCart();
421 $selections = $this->
attr(
'asset_selections');
424 if ($selection_name ==
'Quantity' && isset($cart_contents[$assetid][
'quantity'])) {
425 return $cart_contents[$assetid][
'quantity'];
456 parent::_printAsset($assetid, $list_position, $keywords);
457 $element_contents = ob_get_contents();
462 $cart_contents = $cart->getCart();
464 $element_replacements = Array(
465 'product_quantity' => $cart_contents[$assetid][
'quantity'],
467 $sub_total = $cart_contents[$assetid][
'quantity'] * $cart_contents[$assetid][
'price'];
468 $element_replacements[
'product_total'] = sprintf(
"%.{$this->attr('float_precision')}f", $sub_total);
469 $element_contents = replace_keywords($element_contents, $element_replacements);
471 echo $element_contents;
492 'product_quantity' =>
'%product_quantity%',
493 'product_total' =>
'%product_total%',
510 $cart_contents = $cart->getCart();
513 $this->_tmp[
'cart_updated'] = FALSE;
515 $add_items = $parameter_map->getParameterValue(
'add_item');
516 if (!empty($add_items)) {
517 foreach ($add_items as $assetid => $quantity) {
518 if (empty($assetid) || !strlen($quantity)) {
522 if (isset($cart_contents[$assetid][
'quantity'])) {
524 $amount = $cart_contents[$assetid][
'quantity'];
525 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
531 if ($cart->updateItem($assetid, $this->_validateItemUpdate(SQ_CART_PRESET_QUANTITY, $amount+$quantity, $assetid))) {
532 $this->_tmp[
'cart_updated'] = TRUE;
535 }
else if ($this->
_validateItemUpdate(SQ_CART_PRESET_QUANTITY, $quantity, $assetid) == $quantity) {
537 if ($cart->addItem($assetid, $quantity)) {
538 $this->_tmp[
'cart_updated'] = TRUE;
546 if (isset($_REQUEST[SQ_CART_PRESET_QUANTITY])) {
547 $quantity = $_REQUEST[SQ_CART_PRESET_QUANTITY];
548 if (is_array($quantity)) {
551 foreach ($asset_list as $assetid => $type_code) {
553 if (isset($quantity[$assetid])) {
554 $amount = $quantity[$assetid];
556 if ($cart->updateItem($assetid, $this->_validateItemUpdate(SQ_CART_PRESET_QUANTITY, $amount, $assetid))) {
557 $this->_tmp[
'cart_updated'] = TRUE;
562 if ($cart->updateItem($quantity, $this->_validateItemUpdate(SQ_CART_PRESET_QUANTITY, 1, $assetid))) {
563 $this->_tmp[
'cart_updated'] = TRUE;
569 if (isset($_REQUEST[SQ_CART_PRESET_REMOVE])) {
570 $remove = $_REQUEST[SQ_CART_PRESET_REMOVE];
571 if (is_array($remove)) {
573 foreach ($remove as $assetid => $amount) {
574 if ($cart->removeItem($assetid)) {
575 $this->_tmp[
'cart_updated'] = TRUE;
580 if ($cart->removeItem($remove)) {
581 $this->_tmp[
'cart_updated'] = TRUE;
607 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
612 $selections = $this->
attr(
'asset_selections');
613 $final_quantity = $quantity;
616 $checkout_id = $this->
attr(
'ecommerce_checkout_id');
617 if (!empty($checkout_id)) {
618 if (!$GLOBALS[
'SQ_SYSTEM']->am->assetExists($checkout_id)) {
619 trigger_localised_error(
'ECOM003', E_USER_WARNING, $checkout_id);
621 $checkout_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($checkout_id);
622 if ($checkout_asset->attr(
'enforce_quantities')) {
624 $source = $checkout_asset->attr(
'quantity_selector');
627 $quantity_field = $checkout_asset->attr(
'quantity_field');
628 if (!empty($quantity_field)) {
629 $mm =& $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
630 $available_quantity = $mm->getMetadataValueByAssetid($assetid, $quantity_field);
631 if ($quantity > $available_quantity) {
632 $this->insufficient_quantities = TRUE;
634 $this->insufficient_quantity_assets[] = $asset->name;
635 $final_quantity = $available_quantity;
637 $this->insufficient_quantities = FALSE;
643 $quantity_attribute = $checkout_asset->attr(
'quantity_attribute');
644 if (!empty($quantity_attribute)) {
645 $available_quantity = $asset->attr($quantity_attribute);
646 if ($quantity > $available_quantity) {
647 $this->insufficient_quantities = TRUE;
649 $this->insufficient_quantity_assets[] = $asset->name;
650 $final_quantity = $available_quantity;
652 $this->insufficient_quantities = FALSE;
657 if (is_array($this->insufficient_quantity_assets)) {
658 $this->insufficient_quantity_assets = array_unique($this->insufficient_quantity_assets);
664 if (empty($selections[$selection_name][
'options'])) {
665 return $final_quantity;
667 $options = $selections[$selection_name][
'options'];
668 $restrict_range = FALSE;
671 if (isset($options[
'allow_negative']) && !$options[
'allow_negative']) {
672 $restrict_range = TRUE;
680 if (isset($options[
'restrict_range']) && !empty($options[
'restrict_range'][
'enable'])) {
681 $restrict_range = TRUE;
684 $min = array_get_index($options[
'restrict_range'],
'min', $defaults[
'options'][
'restrict_range'][
'min']);
685 $max = array_get_index($options[
'restrict_range'],
'max', $defaults[
'options'][
'restrict_range'][
'max']);
689 if ($selections[$selection_name][
'type'] ==
'radio') {
690 $restrict_range = TRUE;
695 if ($restrict_range) {
696 if ($quantity < $min) {
697 $final_quantity = $min;
698 }
else if (!is_null($max) && $quantity > $max) {
699 $final_quantity = $max;
703 return $final_quantity;
723 $keywords = parent::getAvailableKeywords();
725 $keywords[
'product_count'] = translate(
'ecom_cart_keyword_product_count');
726 $keywords[
'total_value'] = translate(
'ecom_cart_keyword_total_value');
744 if (!isset($vars[
'keywords']))
return;
746 $parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($broadcaster->id,
'bodycopy', TRUE);
750 $page_contents_link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'page_contents');
751 if (!empty($page_contents_link)) {
752 if (isset($parents[$page_contents_link[
'minorid']])) {
760 $type_formats = Array();
761 if (!is_null($folder)) {
762 $type_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2,
'bodycopy');
763 foreach ($type_links as $link_info) {
764 if (isset($parents[$link_info[
'minorid']])) {
765 $type_formats[] = $link_info[
'value'];
771 if (count($type_formats) > 0) {
772 $keywords[
'product_quantity'] = translate(
'ecom_cart_keyword_product_quantity');
773 $keywords[
'product_total'] = translate(
'ecom_cart_keyword_product_total');
776 $vars[
'keywords'] = array_merge($vars[
'keywords'], $keywords);
780 parent::onRequestKeywords($broadcaster, $vars);
800 $keywords = parent::_getContentsKeywords();
801 $GLOBALS[
'SQ_SYSTEM']->lm->includeAssetStrings(
'page_asset_listing');
804 'cart_updated' => translate(
'ecom_cart_keyword_cart_updated'),
805 'product_count' => translate(
'ecom_cart_keyword_product_count'),
806 'total_value' => translate(
'ecom_cart_keyword_total_value'),
807 'cart_update_button' => translate(
'ecom_cart_keyword_cart_update_button'),
808 'checkout_cart_button' => translate(
'ecom_cart_keyword_checkout_cart_button'),
809 'checkout_cart_href' => translate(
'ecom_cart_keyword_checkout_cart_href'),
810 'insufficient_quantity_message' => translate(
'ecom_cart_keyword_insufficient_quantity_message'),
811 'asset_listing' => translate(
'cms_listing_asset_listing'),
812 'previous_page' => translate(
'cms_listing_previous_page_link'),
813 'next_page' => translate(
'cms_listing_next_page_link'),
814 'page_list' => translate(
'cms_listing_page_list'),
815 'page_number' => translate(
'cms_listing_page_number'),
816 'asset_count' => translate(
'cms_listing_asset_count'),
817 'page_asset_count' => translate(
'cms_listing_page_asset_count'),
818 'first_asset_position' => translate(
'cms_listing_first_asset_position'),
819 'last_asset_position' => translate(
'cms_listing_last_asset_position'),
835 if ($this->insufficient_quantities) {
836 $checkout_id = $this->
attr(
'ecommerce_checkout_id');
837 if (!empty($checkout_id)) {
838 if (!$GLOBALS[
'SQ_SYSTEM']->am->assetExists($checkout_id)) {
839 trigger_localised_error(
'ECOM003', E_USER_WARNING, $checkout_id);
841 $checkout_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($checkout_id);
843 echo $checkout_asset->attr(
'quantity_message_cart');
845 foreach ($this->insufficient_quantity_assets as $asset_name) {
851 return ob_get_clean();
869 return (
string)$cart->getCount();
884 return sprintf(
"%.{$this->attr('float_precision')}f", $cart->getTotal());