db = $db; $this->parser = $parser; $this->renderer = $renderer; $this->utils = $utils; $this->categories_table = $categories_table; } /** * {@inheritDoc} */ public function load($id, $slug = '') { $where = $id <> 0 ? 'category_id = ' . (int) $id : "category_slug = '" . $this->db->sql_escape($slug) . "'"; $sql = 'SELECT * FROM ' . $this->categories_table . ' WHERE ' . $where; $result = $this->db->sql_query($sql); $this->data = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); if ($this->data === false) { throw new runtime_exception('ASS_ERROR_NOT_FOUND'); } return $this; } /** * {@inheritDoc} */ public function import(array $data) { $this->data = $data; return $this; } /** * {@inheritDoc} */ public function save() { if (empty($this->data['category_id'])) { throw new runtime_exception('ASS_ERROR_NOT_EXISTS'); } $data = array_diff_key($this->data, ['category_id' => null]); $sql = 'UPDATE ' . $this->categories_table . ' SET ' . $this->db->sql_build_array('UPDATE', $data) . ' WHERE category_id = ' . $this->get_id(); $this->db->sql_query($sql); return $this; } /** * {@inheritDoc} */ public function insert() { if (!empty($this->data['category_id'])) { throw new runtime_exception('ASS_ERROR_ALREADY_EXISTS'); } $sql = 'SELECT COALESCE(MAX(category_order), 0) as category_order FROM ' . $this->categories_table; $result = $this->db->sql_query($sql); $order = (int) $this->db->sql_fetchfield('category_order'); $this->db->sql_freeresult($result); $this->data['category_order'] = ++$order; $sql = 'INSERT INTO ' . $this->categories_table . ' ' . $this->db->sql_build_array('INSERT', $this->data); $this->db->sql_query($sql); $this->data['category_id'] = (int) $this->db->sql_nextid(); return $this; } /** * {@inheritDoc} */ public function get_id() { return isset($this->data['category_id']) ? (int) $this->data['category_id'] : 0; } /** * {@inheritDoc} */ public function get_title() { return isset($this->data['category_title']) ? (string) $this->data['category_title'] : ''; } /** * {@inheritDoc} */ public function set_title($title) { $title = (string) $title; if ($title === '') { throw new runtime_exception('ASS_ERROR_TOO_SHORT', ['TITLE', 0, 0]); } if (($length = utf8_strlen($title)) > 255) { throw new runtime_exception('ASS_ERROR_TOO_LONG', ['TITLE', 255, $length]); } $this->data['category_title'] = $title; return $this; } /** * {@inheritDoc} */ public function get_slug() { return isset($this->data['category_slug']) ? (string) $this->data['category_slug'] : ''; } /** * {@inheritDoc} */ public function set_slug($slug) { $slug = (string) $slug; if ($slug === '') { throw new runtime_exception('ASS_ERROR_TOO_SHORT', ['SLUG', 0, 0]); } if (($length = utf8_strlen($slug)) > 255) { throw new runtime_exception('ASS_ERROR_TOO_LONG', ['SLUG', 255, $length]); } // Route should not contain any unexpected special characters if (!preg_match('/^[^!"#$%&*\'()+,.\/\\\\:;<=>?@\\[\\]^`{|}~ ]*$/', $slug)) { throw new runtime_exception('ASS_ERROR_ILLEGAL_CHARS', ['SLUG']); } // Routes must be unique if (!$this->get_id() || ($this->get_id() && $this->get_slug() !== '' && $this->get_slug() !== $slug)) { $sql = 'SELECT category_title FROM ' . $this->categories_table . " WHERE category_slug = '" . $this->db->sql_escape($slug) . "' AND category_id <> " . $this->get_id(); $result = $this->db->sql_query_limit($sql, 1); $row = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); if ($row !== false) { throw new runtime_exception('ASS_ERROR_NOT_UNIQUE', ['SLUG', $row['category_title']]); } } $this->data['category_slug'] = $slug; return $this; } /** * {@inheritDoc} */ public function get_icon() { return isset($this->data['category_icon']) ? (string) $this->data['category_icon'] : ''; } /** * {@inheritDoc} */ public function set_icon($icon) { $icon = (string) $icon; if ($icon === '') { throw new runtime_exception('ASS_ERROR_TOO_SHORT', ['ICON', 0, 0]); } $this->data['category_icon'] = $icon; return $this; } /** * {@inheritDoc} */ public function get_desc_for_display() { return isset($this->data['category_desc']) ? (string) $this->renderer->render(htmlspecialchars_decode($this->data['category_desc'], ENT_COMPAT)) : ''; } /** * {@inheritDoc} */ public function get_desc() { return isset($this->data['category_desc']) ? (string) $this->utils->unparse($this->data['category_desc']) : ''; } /** * {@inheritDoc} */ public function set_desc($desc) { $desc = (string) $desc; $desc = $this->parser->parse($desc); $this->data['category_desc'] = $desc; return $this; } /** * {@inheritDoc} */ public function get_active() { return isset($this->data['category_active']) ? (bool) $this->data['category_active'] : true; } /** * {@inheritDoc} */ public function set_active($active) { $active = (bool) $active; $this->data['category_active'] = $active; return $this; } /** * {@inheritDoc} */ public function get_order() { return isset($this->data['category_order']) ? (int) $this->data['category_order'] : 0; } /** * {@inheritDoc} */ public function set_order($order) { $order = (int) $order; $this->data['category_order'] = $order; return $this; } /** * {@inheritDoc} */ public function get_conflicts() { return isset($this->data['item_conflicts']) ? (int) $this->data['item_conflicts'] : 0; } /** * {@inheritDoc} */ public function set_conflicts($conflicts) { $conflicts = (int) $conflicts; $this->data['item_conflicts'] = $conflicts; return $this; } }