public function createCategory($name) { $url = shopHelper::transliterate($name, false); $url = $this->category_model->suggestUniqueUrl($url); if (empty($name)) { $name = _w('(no-name)'); } return $this->category_model->add(array('name' => $name, 'url' => $url)); }
public function execute() { $data = waRequest::post(); $exclude = array('left_key', 'right_key', 'type', 'full_url'); foreach ($exclude as $k) { if (isset($data[$k])) { unset($data[$k]); } } // check required param name $this->post('name', true); $category_model = new shopCategoryModel(); $parent_id = waRequest::post('parent_id', 0, 'int'); if ($parent_id && !$category_model->getById($parent_id)) { throw new waAPIException('invalid_request', 'Parent category not found', 404); } if ($id = $category_model->add($data, $parent_id)) { // return info of the new category $_GET['id'] = $id; $method = new shopCategoryGetInfoMethod(); $this->response = $method->getResponse(true); } else { throw new waAPIException('server_error', 500); } }
/** * @usedby stepImport * @param $data * @return bool */ private function stepImportCategory($data) { /** * @var shopCategoryModel $model */ static $model; $empty = $this->reader->getEmpty(); $data += $empty; if (!$model) { $model = new shopCategoryModel(); } if (!isset($this->data['map'][self::STAGE_CATEGORY])) { $this->data['map'][self::STAGE_CATEGORY] = array(); } $stack =& $this->data['map'][self::STAGE_CATEGORY]; if (preg_match('/^(!{1,})(.+)$/', trim(ifset($data['name'])), $matches)) { $data['name'] = $matches[2]; $depth = strlen($matches[1]); $stack = array_slice($stack, 0, $depth); $parent_id = end($stack); if (!$parent_id) { $parent_id = 0; } } else { $stack = array(); $parent_id = 0; } $primary = $this->data['primary']; if (strpos($primary, 'skus:') === 0) { $primary = 'name'; } $fields = array('parent_id' => $parent_id); if ($primary) { $fields[$primary] = ifset($data[$primary]); } try { self::filterEmptyRows($data, array('url')); $key = 'c:'; if ($current_data = $model->getByField($fields)) { $key .= 'u:' . $current_data['id']; $stack[] = $current_data['id']; if (!$this->emulate($key)) { $target = 'update'; if (!$model->update($current_data['id'], $data)) { $target = 'error'; } } else { $target = 'found'; } } else { $key .= 'a:'; if (!$this->emulate()) { $id = $model->add($data, $parent_id); $model->move($id, null, $parent_id); $stack[] = $id; $target = 'new'; } else { $stack[] = $this->getKey($fields); $target = 'add'; $key .= implode($stack); $this->emulate($key); } } } catch (waDbException $ex) { $target = 'error'; $this->error($ex->getMessage()); } $this->data['processed_count'][self::STAGE_CATEGORY][$target]++; return true; }