public function execute()
 {
     $id = $this->get('id', true);
     $category_model = new shopCategoryModel();
     $category = $category_model->getById($id);
     if ($category) {
         $data = waRequest::post();
         $exclude = array('left_key', 'right_key', 'type', 'full_url');
         foreach ($exclude as $k) {
             if (isset($data[$k])) {
                 unset($data[$k]);
             }
         }
         if (isset($data['parent_id']) && $category['parent_id'] != $data['parent_id']) {
             if (!$category_model->getById($data['parent_id'])) {
                 throw new waAPIException('invalid_param', 'Parent category not found', 404);
             }
             if (!$category_model->move($id, null, $data['parent_id'])) {
                 throw new waAPIException('server_error', 500);
             }
         }
         if ($category_model->update($id, $data)) {
             $method = new shopCategoryGetInfoMethod();
             $this->response = $method->getResponse(true);
         } else {
             throw new waAPIException('server_error', 500);
         }
     } else {
         throw new waAPIException('invalid_param', 'Category not found', 404);
     }
 }
 public function move($type, $id, $before_id, $parent_id)
 {
     if ($type == 'category') {
         $category_model = new shopCategoryModel();
         if (!$category_model->move($id, $before_id, $parent_id)) {
             $this->errors = array('Error when move');
         } else {
             if ($parent_id) {
                 $parent = $category_model->getById($parent_id);
                 $this->response['count'] = array('count' => $parent['count'], 'subtree' => $category_model->getTotalProductsCount($parent_id));
             }
         }
     } else {
         if ($type == 'set') {
             $set_model = new shopSetModel();
             if (!$set_model->move($id, $before_id)) {
                 $this->errors = array('Error when move');
             }
         } else {
             throw new waException('Unknown list type: ' . $type);
         }
     }
 }
 /**
  * @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;
 }