public function execute()
 {
     $type_model = new shopTypeModel();
     $types = $type_model->getAll('id');
     $category_model = new shopCategoryModel();
     $categories = $category_model->getFullTree('id, name, depth', true);
     $features_model = new shopFeatureModel();
     $features = $features_model->getAll('id');
     $data = array();
     $type_values = array();
     foreach ($types as $type_id => $type) {
         $type_values[] = array($type_id, $type['name']);
         $data[$type_id]['price'] = array('feature' => 'price');
         $data[$type_id]['type_id'] = array('feature' => 'type_id');
         $data[$type_id]['tag'] = array('feature' => 'tag');
     }
     $type_features_model = new shopTypeFeaturesModel();
     $rows = $type_features_model->getAll();
     foreach ($rows as $row) {
         if (isset($features[$row['feature_id']])) {
             $code = $features[$row['feature_id']]['code'];
             $data[$row['type_id']][$code] = array('feature' => $code, 'feature_id' => $row['feature_id']);
         }
     }
     $type_upselling_model = new shopTypeUpsellingModel();
     $rows = $type_upselling_model->getAll();
     foreach ($rows as $row) {
         $data[$row['type_id']][$row['feature']] = array('feature_id' => $row['feature_id'], 'feature' => $row['feature'], 'cond' => $row['cond'], 'value' => $row['value']);
     }
     foreach ($data as &$row) {
         $row = array_values($row);
     }
     unset($row);
     foreach ($types as &$type) {
         if ($type['upselling']) {
             $type['upselling_html'] = self::getConditionHTML($data[$type['id']], $features);
         }
     }
     unset($type);
     $fids = array();
     foreach ($features as $f_key => $f) {
         $features[$f_key]['selectable'] = (int) $f['selectable'];
         $features[$f_key]['multiple'] = (int) $f['multiple'];
         if ($f['selectable']) {
             $fids[$f['id']] = $f;
         }
     }
     if ($fids) {
         $fids = $features_model->getValues($fids);
         foreach ($fids as $feature_id => $f) {
             foreach ($f['values'] as $value_id => $value) {
                 $features[$feature_id]['values'][] = array($value_id, $value);
             }
         }
         unset($fids);
     }
     $features['type_id'] = array('name' => _w('Type'), 'type' => 'varchar', 'selectable' => 1, 'values' => $type_values);
     $this->view->assign(array('types' => $types, 'categories' => $categories, 'features' => $features, 'data' => $data));
 }
 protected function saveUpSelling()
 {
     $value = waRequest::post('value');
     $this->type_model->updateById($this->type_id, array('upselling' => $value));
     $type_upselling_model = new shopTypeUpsellingModel();
     $type_upselling_model->deleteByField('type_id', $this->type_id);
     if ($value) {
         $rows = array();
         $data = waRequest::post('data', array());
         foreach ($data as $feature => $row) {
             if (!isset($row['feature'])) {
                 continue;
             }
             $rows[] = array('type_id' => $this->type_id, 'feature' => $feature, 'feature_id' => isset($row['feature_id']) ? $row['feature_id'] : null, 'cond' => $row['cond'], 'value' => isset($row['value']) ? is_array($row['value']) ? implode(',', $row['value']) : $row['value'] : '');
         }
         if ($rows) {
             $type_upselling_model->multipleInsert($rows);
         }
         $this->response['type_id'] = $this->type_id;
         $this->response['data'] = array('price' => array('feature' => 'price'), 'tag' => array('feature' => 'tag'), 'type_id' => array('feature' => 'type_id'));
         $type_features_model = new shopTypeFeaturesModel();
         $rows = $type_features_model->getByType($this->type_id);
         foreach ($rows as $row) {
             $this->response['data'][$row['code']] = array('feature' => $row['code'], 'feature_id' => $row['feature_id']);
         }
         $data = $type_upselling_model->getByType($this->type_id);
         foreach ($data as $row) {
             $this->response['data'][$row['feature']] = array('feature_id' => $row['feature_id'], 'feature' => $row['feature'], 'cond' => $row['cond'], 'value' => $row['value']);
         }
         $this->response['html'] = shopSettingsRecommendationsAction::getConditionHTML($data);
         $this->response['data'] = array_values($this->response['data']);
     }
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $types_per_page = $this->getConfig()->getOption('types_per_page');
     $values_per_feature = 7;
     $type_model = new shopTypeModel();
     $type_features_model = new shopTypeFeaturesModel();
     $feature_model = new shopFeatureModel();
     $types = $type_model->getAll($type_model->getTableId(), true);
     $type_features_model->countFeatures($types);
     $show_all_features = $feature_model->countAll() < $this->getConfig()->getOption('features_per_page');
     if ($show_all_features) {
         $feature_model = new shopFeatureModel();
         if ($features = $feature_model->getFeatures(true, null, 'id', $values_per_feature)) {
             $show_all_features = count($features);
             $type_features_model->fillTypes($features, $types);
             shopFeatureModel::appendTypeNames($features);
         }
     } else {
         $features = array();
     }
     $this->view->assign('type_templates', shopTypeModel::getTemplates());
     $this->view->assign('show_all_features', $show_all_features);
     $this->view->assign('show_all_types', count($types) - $types_per_page < 3);
     $this->view->assign('types_per_page', $types_per_page);
     $this->view->assign('values_per_feature', $values_per_feature);
     $this->view->assign('icons', (array) $this->getConfig()->getOption('type_icons'));
     $this->view->assign('product_types', $types);
     $this->view->assign('features', $features);
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $feature_model = new shopFeatureModel();
     $values_per_feature = 7;
     $type = waRequest::get('type', waRequest::TYPE_STRING, '');
     if ($type_id = intval($type)) {
         $features = $feature_model->getByType($type_id, 'id', $values_per_feature);
     } else {
         if ($type === 'empty') {
             $features = $feature_model->getByType(null, 'id', $values_per_feature);
         } elseif ($type === '') {
             $features = $feature_model->getFeatures(true, null, 'id', $values_per_feature);
         } else {
             $features = $feature_model->getByType(0, 'id', $values_per_feature);
         }
     }
     if ($features) {
         shopFeatureModel::appendTypeNames($features);
         $type_features_model = new shopTypeFeaturesModel();
         $type_features_model->fillTypes($features);
     }
     $this->view->assign('features', $features);
     $this->view->assign('values_per_feature', $values_per_feature);
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     if ($features = waRequest::post('feature')) {
         $model = new shopFeatureModel();
         $type_features_model = new shopTypeFeaturesModel();
         foreach ($features as $feature_id => &$feature) {
             $feature['id'] = $model->save($feature, $feature_id);
             if ($feature['selectable']) {
                 $feature['values'] = $model->setValues($feature, $feature['values']);
             }
             $feature['types'] = $type_features_model->updateByFeature($feature['id'], $feature['types']);
             if ($feature_id < $feature['id']) {
                 $feature['sort'] = array();
                 foreach ($feature['types'] as $type) {
                     $feature['sort'][$type] = $type_features_model->move(array('feature_id' => $feature['id'], 'type_id' => $type), null, $type);
                 }
             }
         }
         unset($feature);
         shopFeatureModel::appendTypeNames($features);
     }
     $this->response = $features;
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $model = new shopTypeFeaturesModel();
     $data = array('type_id' => waRequest::post('type', 0, waRequest::TYPE_INT), 'feature_id' => waRequest::post('feature', 0, waRequest::TYPE_INT));
     $model->insert($data, 2);
 }
 public function execute()
 {
     $feature_id = waRequest::post('feature_id');
     $feature_model = new shopFeatureModel();
     $feature = $feature_model->getById($feature_id);
     if (!$feature) {
         throw new waException(_w('Unknown feature'));
     }
     if (waRequest::post('subtype')) {
         $to = waRequest::post('subtype');
     } else {
         $to = waRequest::post('type');
     }
     $to = array('type' => $to, 'selectable' => (int) waRequest::post('selectable', 0, waRequest::TYPE_INT), 'multiple' => (int) waRequest::post('multiple', 0, waRequest::TYPE_INT));
     $result = shopFeatureValuesConverter::run($feature_id, $to);
     if (!$result) {
         $this->errors[] = _w('Feature type conversion is not allowed or failed');
     } else {
         if ($feature = $feature_model->getById($feature_id)) {
             $this->response = array($feature_id => &$feature);
             $feature['selectable'] = (int) $feature['selectable'];
             $feature['multiple'] = (int) $feature['multiple'];
             if ($feature['selectable']) {
                 $this->response = $feature_model->getValues($this->response, true);
                 $sort = 0;
                 foreach ($feature['values'] as $id => &$value) {
                     if (!is_object($value)) {
                         $value = array('id' => $id, 'value' => $value, 'sort' => $sort++);
                         unset($value);
                     } else {
                         if (method_exists($value, 'getRaw')) {
                             $value = $value->getRaw();
                         } else {
                             $value = array('id' => $id, 'value' => (string) $value, 'sort' => $sort++);
                         }
                     }
                 }
                 $feature['values'] = array_values($feature['values']);
             }
             shopFeatureModel::appendTypeNames($this->response);
             $type_features_model = new shopTypeFeaturesModel();
             $type_features_model->fillTypes($this->response);
             $feature['types'] = array_keys($feature['types']);
             sort($feature['types']);
         }
     }
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $model = new shopTypeFeaturesModel();
     $id = waRequest::post('feature_id', 0, waRequest::TYPE_INT);
     $type = waRequest::post('type_id', 0, waRequest::TYPE_INT);
     $after_id = waRequest::post('after_id', 0, waRequest::TYPE_INT);
     $item = array('type_id' => $type, 'feature_id' => $id);
     $after = null;
     if ($after_id) {
         $after = array('feature_id' => $after_id, 'type_id' => $type);
     }
     try {
         $model->move($item, $after, $type);
     } catch (waException $e) {
         $this->setError($e->getMessage());
     }
 }
 /**
  *
  * @param array $data
  * @return shopProduct
  */
 private function findProduct(&$data)
 {
     static $currencies;
     static $model;
     static $sku_model;
     /**
      * @var shopTypeFeaturesModel $type_features_model
      */
     static $type_features_model;
     if (empty($model)) {
         $model = new shopProductModel();
     }
     if (empty($currencies)) {
         $currencies = array();
         $config = wa()->getConfig();
         /**
          * @var shopConfig $config
          */
         $c = $config->getCurrency();
         $currencies[$c] = $c;
         foreach ($config->getCurrencies() as $row) {
             $currencies[$row['code']] = $row['code'];
         }
     }
     if (!empty($data['skus'][-1]['stock'])) {
         $per_stock = false;
         $stock =& $data['skus'][-1]['stock'];
         foreach ($stock as $id => &$count) {
             if ($count === '') {
                 $count = null;
             } else {
                 $count = intval($count);
                 if ($id) {
                     $per_stock = true;
                 }
             }
         }
         unset($count);
         if ($per_stock) {
             if (isset($stock[0])) {
                 unset($stock[0]);
             }
         } else {
             $count = ifset($stock[0]);
             $stock = array(0 => $count);
         }
         unset($stock);
     }
     $stack = ifset($this->data['map'][self::STAGE_CATEGORY], array());
     $category_id = end($stack);
     if (!$category_id) {
         $category_id = null;
     }
     $primary = $this->data['primary'];
     $fields = false;
     if (empty($primary)) {
         $keys = explode(':', $this->data['secondary']);
         if (empty($sku_model)) {
             $sku_model = new shopProductSkusModel();
         }
         $sku_fields = array(end($keys) => self::getData($data, $keys));
         //hack for empty SKU code ???
         if (false && reset($sku_fields) === '' && $this->data['extra_secondary']) {
             $extra_keys = explode(':', $this->data['extra_secondary']);
             $sku_fields[end($extra_keys)] = self::getData($data, $this->data['extra_secondary']);
         }
         if ($sku = $sku_model->getByField($sku_fields)) {
             $fields = array('category_id' => $category_id, 'id' => $sku['product_id']);
         }
     } elseif (!empty($primary)) {
         $fields = array('category_id' => $category_id, $primary => ifset($data[$primary], ''));
     }
     if ($fields && $this->data['ignore_category']) {
         unset($fields['category_id']);
     }
     $key = 'p';
     if ($fields && ($current_data = $model->getByField($fields))) {
         $product = new shopProduct($current_data['id']);
         $data['type_id'] = ifempty($current_data['type_id'], $this->data['type_id']);
         if (!empty($current_data['tax_id'])) {
             $data['tax_id'] = $current_data['tax_id'];
         }
         if (isset($data['currency']) && !isset($currencies[$data['currency']])) {
             $this->data['processed_count'][self::STAGE_PRODUCT]['currency']++;
             $data['currency'] = reset($currencies);
         }
         if (!empty($data['skus'])) {
             $data['sku_id'] = ifempty($current_data['sku_id'], -1);
         }
         foreach ($product->skus as $sku_id => $current_sku) {
             if (empty($data['skus'][$sku_id])) {
                 if (!count($current_sku['stock']) && $current_sku['count'] !== null) {
                     $current_sku['stock'][0] = $current_sku['count'];
                 }
                 $data['skus'][$sku_id] = $current_sku;
             }
         }
         $key .= ':u:' . $product->getId();
     } else {
         $product = new shopProduct();
         if ($category_id) {
             $data['categories'] = array($category_id);
         }
         $data['currency'] = ifempty($data['currency'], reset($currencies));
         if (!isset($currencies[$data['currency']])) {
             $this->data['processed_count'][self::STAGE_PRODUCT]['currency']++;
             $data['currency'] = reset($currencies);
         }
         if (!empty($data['skus'])) {
             $sku = reset($data['skus']);
             $data['sku_id'] = key($data['skus']);
             if (!isset($sku['available'])) {
                 $sku['available'] = true;
                 $data['skus'][$data['sku_id']] = $sku;
             }
         }
         $key .= ':i:' . $this->getKey($fields);
     }
     if (!empty($data['features'])) {
         foreach ($data['features'] as $feature => &$values) {
             if (is_array($values)) {
             } elseif (preg_match('/^<\\{(.*)\\}>$/', $values, $matches)) {
                 if (!isset($data['features_selectable'])) {
                     $data['features_selectable'] = array();
                 }
                 if ($values = explode(',', $matches[1])) {
                     foreach ($values as &$value) {
                         if (preg_match('@^(.+)=([\\+\\-]?(\\d+|\\.\\d+|\\d\\.\\d))$@', $value, $matches)) {
                             $value = array('value' => trim($matches[1]), 'price' => $matches[2]);
                         } else {
                             $value = array('value' => trim($value));
                         }
                         unset($value);
                     }
                     $data['features_selectable'][$feature] = array('values' => $values);
                     if (!empty($this->data['virtual_sku_stock']) && isset($data['skus'][-1]['stock'])) {
                         $stock = $data['skus'][-1]['stock'];
                         switch ($this->data['virtual_sku_stock']) {
                             case 'distribute':
                                 if (is_array($stock)) {
                                     foreach ($stock as &$stock_item) {
                                         $stock_item = $stock_item / count($values);
                                         unset($stock_item);
                                     }
                                 } else {
                                     $stock = $stock / count($values);
                                 }
                                 $data['features_selectable'][$feature]['stock'] = $stock;
                                 break;
                             case 'set':
                                 $data['features_selectable'][$feature]['stock'] = $stock;
                                 break;
                         }
                     }
                     $product->sku_type = shopProductModel::SKU_TYPE_SELECTABLE;
                     if (isset($data['skus'][-1])) {
                         if (!isset($data['base_price_selectable'])) {
                             $data['base_price_selectable'] = ifset($data['skus'][-1]['price']);
                         }
                         if (!isset($data['purchase_price_selectable'])) {
                             $data['purchase_price_selectable'] = ifset($data['skus'][-1]['purchase_price']);
                         }
                         if (!isset($data['compare_price_selectable'])) {
                             $data['compare_price_selectable'] = ifset($data['skus'][-1]['compare_price']);
                         }
                     }
                     unset($data['skus']);
                 }
                 unset($data['features'][$feature]);
             } elseif (preg_match('/^\\{(.*)\\}$/', $values, $matches)) {
                 $values = explode(',', $matches[1]);
             }
         }
         unset($values);
     }
     $this->findTax($data);
     $access = $this->findType($data);
     if ($access) {
         $access = !$product->type_id || in_array($product->type_id, $this->data['types']);
     }
     if ($access) {
         $product->__hash = $key;
         foreach ($this->data['new_features'] as $code => &$feature) {
             if (isset($data['features'][$code]) || isset($data['features_selectable'][$code])) {
                 if ($data['type_id'] && !in_array($data['type_id'], $feature['types'])) {
                     if (empty($type_features_model)) {
                         $type_features_model = new shopTypeFeaturesModel();
                     }
                     $type_features_model->updateByFeature($feature['id'], array($data['type_id']), false);
                     $feature['types'][] = $data['type_id'];
                 }
             }
             unset($feature);
         }
     }
     return $access ? $product : null;
 }
Exemplo n.º 10
0
 /**
  * @param $template_id
  * @param bool $extend
  * @return array|null
  * @throws waException
  */
 public function insertTemplate($template_id, $extend = false)
 {
     $types = self::getTemplates();
     $feature_model = new shopFeatureModel();
     $type_features_model = new shopTypeFeaturesModel();
     $type = null;
     if (!empty($types[$template_id])) {
         $type = $types[$template_id];
         $type['sort'] = $this->select('MAX(sort)+1 as max_sort')->fetchField('max_sort');
         $type['id'] = $this->insert($type);
         if ($type['id'] && !empty($type['features'])) {
             foreach ($type['features'] as $code => &$feature) {
                 $feature += array('type' => 'varchar', 'selectable' => false, 'multiple' => false);
                 $feature['types'] = array($type['id']);
                 $feature['name'] = ifempty(self::$translate[$feature['name']], $feature['name']);
                 $feature['code'] = $code;
                 $id = null;
                 if ($data = $feature_model->getByField('code', $code)) {
                     if ($feature['type'] == $data['type'] && $feature['selectable'] == $data['selectable'] && $feature['multiple'] == $data['multiple']) {
                         $id = $data['id'];
                     }
                 }
                 $feature['id'] = $feature_model->save($feature, $id);
                 if ($feature['id']) {
                     if (!empty($feature['selectable']) && !empty($feature['values'])) {
                         foreach ($feature['values'] as &$value) {
                             if (is_string($value)) {
                                 $value = ifempty(self::$translate[$value], $value);
                             } elseif (isset($value['value'])) {
                                 $value['value'] = ifempty(self::$translate[$value['value']], $value['value']);
                             }
                         }
                         unset($value);
                         $feature['values'] = $feature_model->setValues($feature, $feature['values'], false, true);
                     }
                     $feature['types'] = $type_features_model->updateByFeature($feature['id'], $feature['types'], false);
                     if ($id && $extend) {
                         //TODO get exists feature values
                         //$feature_model->getFeatureValues($feature);
                         $feature['types'] = array_keys($type_features_model->getByField('feature_id', $feature['id'], 'type_id'));
                     }
                 }
                 unset($feature);
             }
             if ($extend) {
                 shopFeatureModel::appendTypeNames($type['features']);
             }
         }
     }
     return $type;
 }
Exemplo n.º 11
0
 /**
  * @see shopProductStorageInterface::setData()
  * @param shopProduct $product current product object
  * @param array [string] mixed $data new product feature values
  */
 public function setData(shopProduct $product, $data)
 {
     $product_id = $product->getId();
     $feature_model = new shopFeatureModel();
     $codes = array_keys($data);
     $features = $feature_model->getByCode($codes);
     /**
      * composite fields workaround
      */
     $composite_codes = array();
     foreach ($data as $code => $value) {
         if (!preg_match('/\\.[0-3]$/', $code) && isset($features[$code]) && preg_match('/^([23])d\\./', $features[$code]['type'], $matches)) {
             $n = $matches[1];
             $pattern = '/^' . implode('\\s*[×xX\\*]?\\s*', array_fill(0, $n, '([^\\s]+)')) . '(\\s+.+)?$/u';
             if (preg_match($pattern, trim($value), $matches)) {
                 $unit = ifset($matches[$n + 1]);
                 for ($i = 0; $i < $n; $i++) {
                     $c_code = $code . '.' . $i;
                     $data[$c_code] = $matches[$i + 1] . $unit;
                     $composite_codes[] = $c_code;
                 }
                 unset($features[$code]);
             } else {
                 /**
                  * invalid complex feature format
                  */
             }
             unset($data[$code]);
         }
     }
     if ($composite_codes) {
         $features += $feature_model->getByCode($composite_codes);
     }
     $features_map = array();
     foreach ($features as $code => $f) {
         $features_map[$f['id']] =& $features[$code];
     }
     $current = array();
     $rows = $this->getByField(array('product_id' => $product_id, 'sku_id' => null), true);
     foreach ($rows as $row) {
         $id = $row['feature_id'];
         if (isset($features_map[$id])) {
             $f = $features_map[$id];
             $code = $f['code'];
             if (empty($f['multiple'])) {
                 $current[$code] = intval($row['feature_value_id']);
             } else {
                 if (!isset($current[$code])) {
                     $current[$code] = array();
                 }
                 $current[$code][] = intval($row['feature_value_id']);
             }
         } else {
             //obsolete data
         }
     }
     $add = $delete = array();
     foreach ($data as $code => $value) {
         if (isset($features[$code])) {
             $f =& $features[$code];
             if (is_array($value)) {
                 $empty = isset($value['value']) && $value['value'] === '';
                 if (!$empty && isset($value['value']) && is_array($value['value'])) {
                     foreach ($value['value'] as $key => $v) {
                         if ($v === '') {
                             unset($value['value'][$key]);
                         }
                     }
                     $empty = count($value['value']) == 0;
                 }
                 if (!$empty && !isset($value['value'])) {
                     foreach ($value as $key => $v) {
                         if ($v === '') {
                             unset($value[$key]);
                         }
                     }
                     $empty = count($value) == 0;
                 }
             } else {
                 $empty = $value === '';
             }
             if ($empty) {
                 //delete it
                 if (isset($current[$code])) {
                     $delete[$f['id']] = $current[$code];
                 }
             } else {
                 if (is_array($value) && preg_match('/^(.+\\.)[12]$/', $code, $matches) && isset($data[$matches[1] . '0'])) {
                     $value = array_merge($data[$matches[1] . '0'], $value);
                 }
                 $id = $feature_model->getValueId($f, $value, true);
                 if (isset($current[$code])) {
                     if (empty($f['multiple'])) {
                         if ($current[$code] != $id) {
                             $delete[$f['id']] = $current[$code];
                             $add[$f['id']] = $id;
                         }
                     } else {
                         $delete[$f['id']] = array_diff($current[$code], (array) $id);
                         if (empty($delete[$f['id']])) {
                             unset($delete[$f['id']]);
                         }
                         $add[$f['id']] = array_diff((array) $id, $current[$code]);
                         if (empty($add[$f['id']])) {
                             unset($add[$f['id']]);
                         }
                     }
                 } else {
                     $add[$f['id']] = $id;
                 }
             }
         } elseif (!empty($value) && is_array($value)) {
             //it's a new feature
             if (!empty($value) && (ifset($value['type']) == shopFeatureModel::TYPE_BOOLEAN || !empty($value['value']))) {
                 $f = array('name' => $value['name'], 'type' => $value['type'], 'types' => $value['types']);
                 $f['id'] = $feature_model->save($f);
                 $type_features_model = new shopTypeFeaturesModel();
                 $type_features_model->updateByFeature($f['id'], $f['types']);
                 if ($value['value'] !== '') {
                     $add[$f['id']] = $feature_model->getValueId($f, $value['value'], true);
                 }
             }
         }
     }
     foreach ($features as $code => $f) {
         if (empty($data[$code]) && !empty($current[$code])) {
             $delete[$f['id']] = $current[$code];
         }
     }
     foreach ($delete as $feature_id => $value_id) {
         $this->deleteByField(array('product_id' => $product_id, 'sku_id' => null, 'feature_id' => $feature_id, 'feature_value_id' => $value_id));
     }
     foreach ($add as $feature_id => $value_id) {
         $this->multipleInsert(array('product_id' => $product_id, 'feature_id' => $feature_id, 'feature_value_id' => $value_id));
     }
 }
 /**
  * @param shopProduct $product
  * @return array|bool
  * @example <pre>
  * array(
  *  'feature_1'=>array(
  *      'selected'=>int,
  *      'values'=>array(
  *         %value_id%=> array(
  *              'value_id'=>int,
  *              'value'=>string,
  *              'selected'=>bool,
  *          ),
  *      ),
  *  ),
  * )
  * </pre>
  */
 public function getData(shopProduct $product)
 {
     $feature_model = new shopFeatureModel();
     if ($product->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
         $selected = $this->getByProduct($product->id);
         $env = wa()->getEnv();
         if ($env == 'backend') {
             $features = $feature_model->getMultipleSelectableFeaturesByType($product->type_id);
         } else {
             $features = $feature_model->getById(array_keys($selected));
             if ($product->type_id) {
                 $types = array($product->type_id => true);
                 $type_features_model = new shopTypeFeaturesModel();
                 $type_features_model->fillTypes($features, $types);
                 foreach ($features as &$feature) {
                     unset($feature['types']);
                     $feature['sort'] = ifset($feature['sort'][$product->type_id]);
                     unset($feature);
                 }
                 uasort($features, create_function('$a,$b', 'return max(-1,min(1,$a["sort"]-$b["sort"]));'));
             }
         }
         // attach values
         $features = $feature_model->getValues($features);
         foreach ($features as &$f) {
             $count = 0;
             foreach ($f['values'] as $id => &$v) {
                 $is_selected = isset($selected[$f['id']][$id]);
                 if ($env == 'backend') {
                     if (is_object($v)) {
                         $v['selected'] = $is_selected;
                     } else {
                         $v = array('value' => htmlentities((string) $v, ENT_NOQUOTES, 'utf-8'), 'selected' => $is_selected);
                     }
                     unset($v);
                     if ($is_selected) {
                         $count += 1;
                     }
                 } elseif (!$is_selected) {
                     unset($f['values'][$id]);
                 }
             }
             $f['selected'] = $count;
             unset($f);
         }
     } else {
         if (wa()->getEnv() == 'backend') {
             $features = $feature_model->isTypeMultipleSelectable($product->type_id);
         } else {
             $features = array();
         }
     }
     return $features;
     //Frontend
     $data = $feature_model->getValues($data);
     foreach ($data as $code => $f) {
         foreach ($f['values'] as $id => $v) {
             if (!isset($v_id, $selected[$code][$id])) {
                 unset($data[$code]['values'][$id]);
             }
         }
     }
     return $data;
 }
Exemplo n.º 13
0
 private function setup()
 {
     if ($country = waRequest::post('country')) {
         if (!empty($this->countries[$country])) {
             $path = $this->getConfig()->getConfigPath('data/welcome/', false);
             $country_data = (include $path . "country_{$country}.php");
             # Main country setting
             $model = new waAppSettingsModel();
             $model->set('shop', 'country', $country);
             #currency
             if (!empty($country_data['currency'])) {
                 $currency_model = new shopCurrencyModel();
                 $sort = 0;
                 foreach ($country_data['currency'] as $code => $rate) {
                     // delete old currency info is exists
                     $currency_model->deleteById($code);
                     $currency_model->insert(array('code' => $code, 'rate' => $rate, 'sort' => $sort++), 2);
                     if ($sort == 1) {
                         $model->set('shop', 'currency', $code);
                     }
                 }
             }
             #taxes
             if (!empty($country_data['taxes'])) {
                 foreach ($country_data['taxes'] as $tax_data) {
                     shopTaxes::save($tax_data);
                 }
             }
             #custom code
             $function = 'shopWelcome' . ucfirst($country);
             if (function_exists($function)) {
                 try {
                     call_user_func_array($function, array());
                 } catch (Exception $ex) {
                     //TODO
                 }
             }
         }
     }
     if (!empty($this->types)) {
         $type_model = new shopTypeModel();
         $type_features_model = new shopTypeFeaturesModel();
         $types = waRequest::post('types');
         if (empty($types)) {
             if (!$type_features_model->countAll()) {
                 $types[] = 'default';
             }
         }
         if ($types) {
             foreach ($types as $type) {
                 $type_model->insertTemplate($type);
             }
         }
     }
     $set_model = new shopSetModel();
     $set_model->add(array('id' => 'promo', 'name' => _w('Featured on homepage'), 'type' => shopSetModel::TYPE_STATIC));
     $set_model->add(array('id' => 'bestsellers', 'name' => _w('Bestsellers'), 'type' => shopSetModel::TYPE_DYNAMIC, 'count' => 8, 'rule' => 'rating DESC'));
     // notifications
     $notifications_model = new shopNotificationModel();
     if ($notifications_model->countAll() == 0) {
         $notifications_action = new shopSettingsNotificationsAddAction();
         $notifications = $notifications_action->getTemplates();
         $params_model = new shopNotificationParamsModel();
         $events = $notifications_action->getEvents();
         foreach ($notifications as $event => $n) {
             if ($event == 'order') {
                 continue;
             }
             $data = array('name' => $events[$event]['name'] . ' (' . _w('Customer') . ')', 'event' => $event, 'transport' => 'email', 'status' => 1);
             $id = $notifications_model->insert($data);
             $params = $n;
             $params['to'] = 'customer';
             $params_model->save($id, $params);
             if ($event == 'order.create') {
                 $data['name'] = $events[$event]['name'] . ' (' . _w('Store admin') . ')';
                 $id = $notifications_model->insert($data);
                 $params['to'] = 'admin';
                 $params_model->save($id, $params);
             }
         }
     }
     /* !!! import commented out on welcome screen
        switch (waRequest::post('import')) {
        case 'demo':
        //TODO create demoproducts
        $this->redirect('?action=products');
        break;
        case 'migrate':
        $plugins = $this->getConfig()->getPlugins();
        if (empty($plugins['migrate'])) {
        $url = $this->getConfig()->getBackendUrl(true).'installer/?module=update&action=manager&install=1&app_id[shop/plugins/migrate]=webasyst';
        } else {
        $url = '?action=importexport#/migrate/';
        }
        $this->redirect($url);
        break;
        case 'scratch':
        default: */
     $this->redirect('?action=products#/welcome/');
     //        break;
     //}
 }
Exemplo n.º 14
0
 public function execute()
 {
     $product = new shopProduct(waRequest::get('id', 0, waRequest::TYPE_INT));
     if (!$product->id) {
         if (waRequest::get('id') == 'new') {
             $product->name = '';
             $product->id = 'new';
             $product->status = 1;
         } else {
             throw new waException("Product not found", 404);
         }
     }
     $counters = array('reviews' => 0, 'images' => 0, 'pages' => 0, 'services' => 0);
     $sidebar_counters = array();
     $config = $this->getConfig();
     /**
      * @var shopConfig $config
      */
     #load product types
     $type_model = new shopTypeModel();
     $product_types = $type_model->getTypes(true);
     $product_types_count = count($product_types);
     if (intval($product->id)) {
         # 1 fill extra product data
         # 1.1 fill product reviews
         $product_reviews_model = new shopProductReviewsModel();
         $product['reviews'] = $product_reviews_model->getReviews($product->id, 0, $config->getOption('reviews_per_page_product'), 'datetime DESC', array('is_new' => true));
         $counters['reviews'] = $product_reviews_model->count($product->id);
         $sidebar_counters['reviews'] = array('new' => $product_reviews_model->countNew());
         $counters['images'] = count($product['images']);
         $product_pages_model = new shopProductPagesModel();
         $counters['pages'] = $product_pages_model->count($product->id);
         $product_services_model = new shopProductServicesModel();
         $counters['services'] = $product_services_model->countServices($product->id);
         $product_stocks_log_model = new shopProductStocksLogModel();
         $counters['stocks_log'] = $product_stocks_log_model->countByField('product_id', $product->id);
         $this->view->assign('edit_rights', $product->checkRights());
     } else {
         $counters += array_fill_keys(array('images', 'services', 'pages', 'reviews'), 0);
         $product['images'] = array();
         reset($product_types);
         $product->type_id = 0;
         if ($product_types_count) {
             if (!$product_types) {
                 throw new waRightsException(_w("Access denied"));
             } else {
                 reset($product_types);
                 $product->type_id = key($product_types);
             }
         } elseif (!$product->checkRights()) {
             throw new waRightsException(_w("Access denied"));
         }
         $this->view->assign('edit_rights', true);
         $product['skus'] = array('-1' => array('id' => -1, 'sku' => '', 'available' => 1, 'name' => '', 'price' => 0.0, 'purchase_price' => 0.0, 'count' => null, 'stock' => array(), 'virtual' => 0));
         $product->currency = $config->getCurrency();
     }
     $this->assignReportsData($product);
     $stock_model = new shopStockModel();
     $taxes_mode = new shopTaxModel();
     $this->view->assign('stocks', $stock_model->getAll('id'));
     $this->view->assign(array('use_product_currency' => wa()->getSetting('use_product_currency'), 'currencies' => $this->getCurrencies(), 'primary_currency' => $config->getCurrency(), 'taxes' => $taxes_mode->getAll()));
     $category_model = new shopCategoryModel();
     $categories = $category_model->getFullTree('id, name, depth, url, full_url, parent_id', true);
     $frontend_urls = array();
     if (intval($product->id)) {
         $routing = wa()->getRouting();
         $domain_routes = $routing->getByApp($this->getAppId());
         foreach ($domain_routes as $domain => $routes) {
             foreach ($routes as $r) {
                 if (!empty($r['private'])) {
                     continue;
                 }
                 if (empty($r['type_id']) || in_array($product->type_id, (array) $r['type_id'])) {
                     $routing->setRoute($r, $domain);
                     $params = array('product_url' => $product->url);
                     if ($product->category_id && isset($categories[$product->category_id])) {
                         if (!empty($r['url_type']) && $r['url_type'] == 1) {
                             $params['category_url'] = $categories[$product->category_id]['url'];
                         } else {
                             $params['category_url'] = $categories[$product->category_id]['full_url'];
                         }
                     }
                     $frontend_url = $routing->getUrl('/frontend/product', $params, true);
                     $frontend_urls[] = array('url' => $frontend_url);
                 }
             }
         }
     } else {
         $frontend_urls[] = array('url' => wa()->getRouteUrl('/frontend/product', array('product_url' => '%product_url%'), true));
     }
     $stuff = intval($product->id) ? $product->url : '%product_url%';
     foreach ($frontend_urls as &$frontend_url) {
         $pos = strrpos($frontend_url['url'], $stuff);
         $frontend_url['base'] = $pos !== false ? rtrim(substr($frontend_url['url'], 0, $pos), '/') . '/' : $frontend_url['url'];
     }
     unset($frontend_url);
     $product_model = new shopProductModel();
     $this->view->assign('storefront_map', $product_model->getStorefrontMap($product->id));
     /**
      * Backend product profile page
      * UI hook allow extends product profile page
      * @event backend_product
      * @param shopProduct $entry
      * @return array[string][string]string $return[%plugin_id%]['title_suffix'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_button'] html output
      * @return array[string][string]string $return[%plugin_id%]['toolbar_section'] html output
      * @return array[string][string]string $return[%plugin_id%]['image_li'] html output
      */
     $this->view->assign('backend_product', wa()->event('backend_product', $product));
     /**
      * @event backend_product_edit
      */
     $this->view->assign('backend_product_edit', wa()->event('backend_product_edit', $product));
     $this->view->assign('categories', $categories);
     $this->view->assign('counters', $counters);
     $this->view->assign('product', $product);
     $this->view->assign('current_author', shopProductReviewsModel::getAuthorInfo(wa()->getUser()->getId()));
     $this->view->assign('reply_allowed', true);
     $this->view->assign('review_allowed', true);
     $this->view->assign('sidebar_counters', $sidebar_counters);
     $this->view->assign('lang', substr(wa()->getLocale(), 0, 2));
     $this->view->assign('frontend_urls', $frontend_urls);
     $tag_model = new shopTagModel();
     $this->view->assign('popular_tags', $tag_model->popularTags());
     $counts = array();
     // Selectable features
     $features_selectable = $product->features_selectable;
     if (is_array($features_selectable)) {
         foreach ($features_selectable as $f) {
             if ($f['selected']) {
                 $counts[] = $f['selected'];
             }
         }
     }
     $feature_model = new shopTypeFeaturesModel();
     $features_selectable_types = $feature_model->getSkuTypeSelectableTypes();
     foreach ($product_types as $type_id => &$type) {
         $type['sku_type'] = empty($features_selectable_types[$type_id]) ? shopProductModel::SKU_TYPE_FLAT : shopProductModel::SKU_TYPE_SELECTABLE;
     }
     $this->view->assign('features', $features_selectable);
     $this->view->assign('duble', '???');
     $this->view->assign('features_counts', $counts);
     #load product types
     $this->view->assign('product_types', $product_types);
     $this->view->assign('sidebar_width', $config->getSidebarWidth());
 }