Exemple #1
0
 function setup(&$Model, $settings)
 {
     if (!isset($this->settings[$Model->alias])) {
         $this->settings[$Model->alias] = $this->defaultSettings;
     }
     $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], (array) $settings);
     if (empty($Model->productOptions)) {
         $Model->productOptions = array();
     }
     $Model->productOptions = array_merge($Model->productOptions, $this->settings[$Model->alias]);
     $Model->Behaviors->attach('Shop.AssociationEvent');
     $Model->bindModel(array('hasOne' => array('ShopProduct' => array('className' => 'Shop.ShopProduct', 'foreignKey' => 'foreign_id', 'conditions' => array('ShopProduct.model' => $Model->alias), 'dependent' => true))), false);
     if (Configure::read('admin') == true) {
         App::import('Lib', 'Shop.ShopConfig');
         $types = ShopConfig::getSubProductTypes();
         if (!empty($types)) {
             $this->productContain($Model);
         }
     }
 }
Exemple #2
0
 function editForm($model = null, $options = array())
 {
     App::import('Lib', 'Shop.ShopConfig');
     $config = ShopConfig::load();
     if (is_array($model)) {
         $options = $model;
     } elseif (!empty($model)) {
         $options['model'] = $model;
     }
     $defOpt = array('model' => $this->model(), 'fieldset' => true, 'legend' => __('Shop', true));
     $opt = array_merge($defOpt, $options);
     $types = ShopConfig::getSubProductTypes();
     if (empty($this->data['ShopSubproduct']) && !empty($this->data['ShopProduct']['ShopSubproduct'])) {
         App::import('Lib', 'SetMulti');
         $this->data['ShopSubproduct'] = $this->data['ShopProduct']['ShopSubproduct'];
         $this->O2form->data['ShopSubproduct'] = $this->data['ShopSubproduct'];
         $this->Form->data['ShopSubproduct'] = $this->data['ShopSubproduct'];
     }
     $typeFields = $this->_subProductsFields($types);
     $view =& ClassRegistry::getObject('view');
     $html = $view->element('edit_form', array('plugin' => 'shop', 'opt' => $opt, 'typeFields' => $typeFields, 'currencies' => $config['currencies']));
     return $html;
 }
Exemple #3
0
 function subitemInput($type, $prod = null, $options = array(), $no = null)
 {
     $view =& ClassRegistry::getObject('view');
     if (!is_array($type)) {
         App::import('Lib', 'Shop.ShopConfig');
         $types = empty($options['types']) ? ShopConfig::getSubProductTypes() : $options['types'];
         if (empty($types[$type])) {
             return null;
         }
         $type = $types[$type];
     }
     $localOpt = array('prefix', 'recursive', 'types', 'subProducts');
     $defOpt = array('prefix' => !is_null($no) && is_numeric($no) ? 'ShopCart.products.' . $no . '.SubItem.' : 'ShopCart.SubItem.', 'label' => $type['label'], 'class' => 'subItem', 'recursive' => true);
     $opt = array_merge($defOpt, $options);
     if (empty($opt['subProducts'])) {
         //debug($prod);
         if (!is_array($prod)) {
             $prod = null;
             $no = $prod;
         }
         $sources = array('prod.ShopProduct.ShopSubproduct.' . $type['name'], 'prod.ShopSubproduct.' . $type['name'], 'data.ShopProduct.ShopSubproduct.' . $type['name'], 'vars.' . Inflector::singularize($this->params['controller']) . '.ShopProduct.ShopSubproduct.' . $type['name']);
         App::import('Lib', 'Shop.SetMulti');
         $data = array('prod' => $prod, 'data' => $this->data, 'vars' => $view->viewVars, 'params' => $this->params);
         $subProducts = SetMulti::extractHierarchic($sources, $data);
     } elseif (!empty($opt['subProducts'][$type['name']])) {
         $subProducts = $opt['subProducts'][$type['name']];
     } else {
         return null;
     }
     $children = array();
     if (empty($subProducts)) {
         return null;
     }
     $name = $opt['prefix'] . $type['name'];
     if ($opt['recursive']) {
         foreach ($subProducts as $subProduct) {
             if (!empty($subProduct['children'])) {
                 $children[$subProduct['id']] = $this->O2form->conditionalBlock($this->subitemInputs($prod, array('types' => $type['children'], 'subProducts' => $subProduct['children'], 'prefix' => $opt['prefix'] . 'children.' . $subProduct['id'] . '.'), $no), $name, $subProduct['id']);
             }
         }
     }
     return $view->element('subproduct_select', array('plugin' => 'shop', 'type' => $type, 'subProducts' => $subProducts, 'name' => $name, 'options' => array_diff_key($opt, array_flip($localOpt)), 'children' => $children));
 }
Exemple #4
0
 function extractSubItemData($productAndOptions, $order = null)
 {
     //debug($productAndOptions);
     $currency = !empty($order['ShopOrder']['currency']) ? $order['ShopOrder']['currency'] : ShopConfig::load('currency');
     $extract_data = array('shop_product_subproduct_id' => 'ShopProductSubproduct.id', 'shop_subproduct_id' => 'ShopSubproduct.id', 'descr' => array('ShopSubproduct.label'), 'nb' => array('nb'), 'id' => array('id'), 'item_price' => array('ShopSubproduct.currency_prices.' . $currency, 'ShopSubproduct.price'), 'item_operator' => array('ShopSubproduct.operator'), 'type' => 'ShopSubproduct.type', 'ShopSubproduct' => 'ShopSubproduct');
     if (!empty($order['ShopOrder']['confirm'])) {
         $extract_data = Set::merge(array('descr' => array('descr'), 'item_operator' => array('item_operator'), 'item_price' => array('item_price')), $extract_data);
     }
     $orderItemMode = isset($productAndOptions['item_price']);
     if ($orderItemMode) {
         $p =& $productAndOptions;
     } else {
         $p = $this->extractOrderItemData($p2 = $productAndOptions, $order);
     }
     $cur_price = $p['item_price'];
     $p['item_alone_price'] = $cur_price;
     App::import('Lib', 'Shop.ShopConfig');
     $types = ShopConfig::getSubProductTypes();
     $subItems = null;
     if (!empty($types) && !empty($p['SubItem'])) {
         $this->ShopSubproduct = ClassRegistry::init('Shop.ShopSubproduct');
         $ids = array();
         //============ normalize data ============//
         $subItems = $this->_getNormalizedSubItem($types, $p['SubItem'], $ids);
         //debug($ids);
         //============ fetch SubProducts ============//
         $this->ShopSubproduct->ShopProductSubproduct->Behaviors->attach('Containable');
         $this->ShopSubproduct->ShopProductSubproduct->contain(array('ShopSubproduct'));
         $subProduct = $this->ShopSubproduct->ShopProductSubproduct->find('all', array('conditions' => array('ShopProductSubproduct.shop_product_id' => $p['product_id'], 'ShopProductSubproduct.shop_subproduct_id' => $ids)));
         $subProduct = SetMulti::group($subProduct, 'ShopSubproduct.id', array('singleArray' => false));
         //debug($subProduct);
         foreach ($subItems as $type => &$items) {
             foreach ($items as &$subItem) {
                 if (!empty($subProduct[$subItem['id']])) {
                     $subItem = array_merge($subProduct[$subItem['id']], $subItem);
                 }
                 unset($subItem);
             }
         }
     }
     if (!empty($productAndOptions['ShopOrdersSubitem'])) {
         $subItems = SetMulti::group($productAndOptions['ShopOrdersSubitem'], 'ShopSubproduct.type');
     }
     if (!empty($types) && !empty($subItems)) {
         //============ merge data ============//
         $finalSubItems = array();
         foreach ($subItems as $type => &$items) {
             foreach ($items as &$subItem) {
                 $data = SetMulti::extractHierarchicMulti($extract_data, $subItem);
                 if (!empty($subItem['parent'])) {
                     $data['parent'] =& $subItem['parent'];
                 }
                 SetMulti::replaceRef($subItem, $data);
                 $finalSubItems[] =& $subItem;
                 unset($subItem);
             }
             unset($items);
         }
         //debug($finalSubItems);
         return $finalSubItems;
     }
     return null;
 }