function getFilter($options)
 {
     $defOpt = array('models' => array('CustomFilterCond' => array('field', 'val1', 'type'), 'CustomFilterGroup' => array('group' => 'title', 'group_key' => 'key'), 'CustomFilter'), 'override' => array(), 'default' => array());
     $opt = Set::merge($defOpt, $options);
     $data = $this->_optionsToFormatedData($opt, $defOpt);
     $override = $this->_optionsToFormatedData($opt['override'], $defOpt);
     $default = $this->_optionsToFormatedData($opt['default'], $defOpt);
     if (empty($data[$this->alias]['model'])) {
         return null;
     }
     App::import('Lib', 'CustomFilter.SetMulti');
     //debug($data);
     $key = empty($data['CustomFilter']['key']) ? null : $data['CustomFilter']['key'];
     if ($key) {
         $findOpt = array('conditions' => array('CustomFilter.key' => $key), 'recursive' => -1);
         $override = SetMulti::complexMerge($data, $override, 2);
     } else {
         $findOpt = array('conditions' => Set::flatten(array_diff_key($data, array_flip(array('CustomFilterCond')))), 'contain' => 'CustomFilterGroup');
         if (!empty($data['CustomFilterCond'])) {
             $findOpt['conditions']['CustomFilter.cond_count'] = count($data['CustomFilterCond']);
             foreach ($data['CustomFilterCond'] as $i => $cond) {
                 $a = 'CustomFilterCond' . $i;
                 $join = array('alias' => $a, 'table' => $this->CustomFilterCond->useTable, 'type' => 'INNER', 'conditions' => Set::flatten(array($a => $cond)));
                 $join['conditions'][] = 'CustomFilter.id = ' . $a . '.custom_filter_id';
                 $findOpt['joins'][] = $join;
             }
         }
         //debug($findOpt);
     }
     $existing = $this->find('first', $findOpt);
     if ($existing) {
         $filter = $existing;
     }
     if (empty($existing) || !empty($override)) {
         $filter = $default;
         if ($existing) {
             $filter = SetMulti::complexMerge($filter, $existing, 2);
         } else {
             $filter = SetMulti::complexMerge($filter, $data, 2);
         }
         $filter = SetMulti::complexMerge($filter, $override, 2);
         //debug($filter);
         $this->create();
         if ($this->save($filter)) {
             return $this->id;
         }
     } else {
         return $existing['CustomFilter']['id'];
     }
 }
Exemplo n.º 2
0
 function save($data = null)
 {
     if (!empty($data)) {
         $nomalized = $data;
         if (isset($data['ShopCart'])) {
             $nomalized = $data['ShopCart'];
         }
         if (isset($data['ShopOrder'])) {
             $nomalized['order'] = $data['ShopOrder'];
             unset($nomalized['ShopOrder']);
         }
         if (!empty($nomalized['products'])) {
             foreach ($tmp = $nomalized['products'] as $no => $prod) {
                 if ($prod['nb'] <= 0) {
                     //unset($nomalized['products'][$no]);
                     array_splice($nomalized['products'], $no, 1);
                     $this->remove($no);
                 }
             }
         }
         App::import('Lib', 'Shop.SetMulti');
         $this->data = SetMulti::complexMerge($this->data, $nomalized, array('sequences' => false));
         $this->clearCache();
     }
     //debug($this->data);
     $this->Session->write('Shop.cart', $this->data);
     $this->_itemListData = null;
 }