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 shopFeatureModel(); $values = array(waRequest::post('value')); $code = waRequest::post('code'); if ($values && $code && ($feature = $model->getByField('code', $code))) { $this->response = $model->setValues($feature, $values); } else { $this->setError(_w('Product feature not found')); } }
private function setValues($feature, $values) { if (!$this->isMultidimensional($feature)) { return $this->feature_model->setValues($feature, $values, false); } else { $type = $feature['type']; $parts = explode('.', $type); $values_model = shopFeatureModel::getValuesModel($parts[1]); $children = $this->getChildren($feature); $data = array(); $sort = 0; foreach ($values as $id => $value) { $val = $value['value']; $f_id = $value['feature_id']; $row =& $data[]; $row = $values_model->addValue($f_id, $val, $id, $children[$f_id]['type'], ++$sort); $row['feature_id'] = $f_id; } foreach ($children as $c) { $this->feature_model->recount($c); } return $data; } }
/** * @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; }