Ejemplo n.º 1
0
 function buyUrl($id = null, $nb = null, $model = null, $options = array())
 {
     if (is_array($id) && !is_array($id)) {
         $options = $id;
     } else {
         if (is_array($nb)) {
             $options = $nb;
         } elseif (!empty($nb)) {
             $options['nb'] = $nb;
         }
         if (is_array($id)) {
             $options['product'] = $id;
         } else {
             $options['id'] = $id;
         }
         if (!empty($model)) {
             $options['model'] = $model;
         }
     }
     if (empty($options['id']) && !empty($options['product'])) {
         if (!empty($options['product']['id'])) {
             $options['id'] = $options['product']['id'];
         } else {
             if (empty($options['model'])) {
                 $options['model'] = $this->defModel();
             }
             if (!empty($options['product'][$options['model']]['id'])) {
                 $options['id'] = $options['product'][$options['model']]['id'];
             }
         }
     }
     /*if(is_array($options['id'])){
     			
     		}*/
     if (empty($options['id']) && ($defID = $this->defID())) {
         $options['id'] = $defID;
     }
     if (empty($options['id']) || !is_numeric($options['id'])) {
         return null;
     }
     $localOpt = array('routed', 'product');
     $defaultOptions = array('model' => $this->defModel(), 'nb' => null, 'routed' => true, 'back' => null, 'redirect' => null);
     $options = array_merge($defaultOptions, $options);
     if (!empty($options['back'])) {
         App::import('Lib', 'Shop.UrlParam');
         //debug($options['back']);
         $options['back'] = UrlParam::encode($options['back']);
         //debug($options['back']);
         //debug(UrlParam::decode($options['back']));
     }
     if (!empty($options['redirect'])) {
         App::import('Lib', 'Shop.UrlParam');
         $options['redirect'] = UrlParam::encode($options['redirect']);
     }
     $urlOpt = array('plugin' => 'shop', 'controller' => 'shop_cart', 'action' => 'add');
     $urlOpt = array_merge(array_diff_key($options, array_flip($localOpt)), $urlOpt);
     //debug($urlOpt);
     if ($options['routed']) {
         return $this->Html->url($urlOpt);
     } else {
         return $urlOpt;
     }
 }
Ejemplo n.º 2
0
 function add($options)
 {
     //séparer option et produits ?
     $defaultOptions = array('products' => array(), 'redirect' => true, 'back' => null);
     if (!is_array($options)) {
         $options = array('products' => array($options));
     }
     if (!empty($options['products']) && is_array($options['products']) && empty($options['products'][0])) {
         $options['products'] = array($options['products']);
     }
     $options = array_merge($defaultOptions, $options);
     foreach ($options['products'] as $product) {
         $product = $this->ShopFunct->formatProductAddOption($product);
         //debug($product);
         $pos = $this->match($product);
         if ($pos > -1) {
             $this->data['products'][$pos]['nb'] += $product['nb'];
             if ($this->data['products'][$pos]['nb'] <= 0) {
                 $this->remove($pos);
             }
         } elseif ($product['nb'] > 0) {
             $this->data['products'][] = $product;
         }
     }
     //debug($this->data);
     //exit();
     $this->clearCache();
     $this->save();
     if ($options['redirect']) {
         $url = array('plugin' => 'shop', 'controller' => 'shop_cart', 'action' => 'index');
         if (!empty($options['back'])) {
             App::import('Lib', 'Shop.UrlParam');
             //debug($options['back']);
             $url['redirect'] = UrlParam::encode($options['back']);
             //debug($options['back']);
             //debug(UrlParam::decode($options['back']));
         }
         $this->controller->redirect($url);
         exit;
     }
 }