diff options
Diffstat (limited to 'app/Model/Category.php')
-rw-r--r-- | app/Model/Category.php | 107 |
1 files changed, 95 insertions, 12 deletions
diff --git a/app/Model/Category.php b/app/Model/Category.php index fb54594b..c8ba7251 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -46,6 +46,66 @@ class Category extends Base } /** + * Get the category name by the id + * + * @access public + * @param integer $category_id Category id + * @return string + */ + public function getNameById($category_id) + { + return $this->db->table(self::TABLE)->eq('id', $category_id)->findOneColumn('name') ?: ''; + } + + /** + * Get a category id by the project and the name + * + * @access public + * @param integer $project_id Project id + * @param string $category_name Category name + * @return integer + */ + public function getIdByName($project_id, $category_name) + { + return (int) $this->db->table(self::TABLE) + ->eq('project_id', $project_id) + ->eq('name', $category_name) + ->findOneColumn('id'); + } + + /** + * Prepare categories to be displayed on the board + * + * @access public + * @param integer $project_id + * @return array + */ + public function getBoardCategories($project_id) + { + $descriptions = array(); + + $listing = array( + -1 => t('All categories'), + 0 => t('No category'), + ); + + $categories = $this->db->table(self::TABLE) + ->eq('project_id', $project_id) + ->asc('name') + ->findAll(); + + foreach ($categories as $category) { + $listing[$category['id']] = $category['name']; + $descriptions[$category['id']] = $category['description']; + } + + return array( + $listing, + $descriptions, + ); + } + + /** * Return the list of all categories * * @access public @@ -56,10 +116,10 @@ class Category extends Base */ public function getList($project_id, $prepend_none = true, $prepend_all = false) { - $listing = $this->db->table(self::TABLE) + $listing = $this->db->hashtable(self::TABLE) ->eq('project_id', $project_id) ->asc('name') - ->listing('id', 'name'); + ->getAll('id', 'name'); $prepend = array(); @@ -90,15 +150,38 @@ class Category extends Base } /** - * Create a category + * Create default cetegories during project creation (transaction already started in Project::create()) + * + * @access public + * @param integer $project_id + */ + public function createDefaultCategories($project_id) + { + $categories = explode(',', $this->config->get('project_categories')); + + foreach ($categories as $category) { + + $category = trim($category); + + if (! empty($category)) { + $this->db->table(self::TABLE)->insert(array( + 'project_id' => $project_id, + 'name' => $category, + )); + } + } + } + + /** + * Create a category (run inside a transaction) * * @access public * @param array $values Form values - * @return bool + * @return bool|integer */ public function create(array $values) { - return $this->db->table(self::TABLE)->save($values); + return $this->persist(self::TABLE, $values); } /** @@ -137,26 +220,26 @@ class Category extends Base } /** - * Duplicate categories from a project to another one + * Duplicate categories from a project to another one, must be executed inside a transaction * * @author Antonio Rabelo - * @param integer $project_from Project Template - * @return integer $project_to Project that receives the copy + * @param integer $src_project_id Source project id + * @return integer $dst_project_id Destination project id * @return boolean */ - public function duplicate($project_from, $project_to) + public function duplicate($src_project_id, $dst_project_id) { $categories = $this->db->table(self::TABLE) ->columns('name') - ->eq('project_id', $project_from) + ->eq('project_id', $src_project_id) ->asc('name') ->findAll(); foreach ($categories as $category) { - $category['project_id'] = $project_to; + $category['project_id'] = $dst_project_id; - if (! $this->category->create($category)) { + if (! $this->db->table(self::TABLE)->save($category)) { return false; } } |