/** * Создаёт новую категорию * @param $args array mixed * @return void */ function add(array $args = array()) { $category = array('system' => 0, 'parent' => 0); $types = array_flip(Category::getTypesArray()); if (array_key_exists(0, $args) && array_key_exists($args[0], $types)) { $category['type'] = $types[$args[0]]; } else { $category['type'] = Category::TYPE_WASTE; } if ($this->request->method == 'POST') { $errors = array(); $category = array('name' => trim($this->request->post['name']), 'parent' => (int) $this->request->post['parent'], 'system' => (int) $this->request->post['system'], 'type' => isset($this->request->post['type']) ? $this->request->post['type'] : $category['type']); if (!$category['name']) { $errors[] = 'Не указано название!'; } if (!$category['system']) { $errors[] = 'Не указана системная категория!'; } if (!array_key_exists($category['type'], Category::getTypesArray())) { $errors[] = 'Некорректный тип категории!'; } if (!sizeof($errors)) { $category['id'] = $this->model->add($category['name'], $category['parent'], $category['system'], $category['type']); $this->tpl->assign('result', array('text' => "Категория успешно добавлена.", 'id' => $category['id'])); } else { $this->tpl->assign('error', array('text' => implode(" \n", $errors))); } } $this->tpl->assign('category', $category); $this->tpl->assign('name_page', 'category/edit'); }
/** * 创建联系人分组 */ public function create() { if ($this->get_method() != 'POST') { $this->send_response(405, NULL, Kohana::lang('contact.method_not_exist')); } $data = $this->get_data(); $name = isset($data['name']) ? $data['name'] : ''; if (empty($name)) { $this->send_response(400, NULL, Kohana::lang('contact.group_name_empty')); } elseif (mb_strlen($name, 'utf8') > 32) { $this->send_response(400, NULL, Kohana::lang('contact.group_name_too_long')); } else { if ($this->model->check_name($this->user_id, $name)) { $this->send_response(400, NULL, Kohana::lang('contact.group_name_exist')); } else { $gid = $this->model->add($this->user_id, $name); $result = array('id' => (int) $gid, 'name' => $name); $this->send_response(200, $result); } } }