Added models for tags
This commit is contained in:
139
app/Model/TagModel.php
Normal file
139
app/Model/TagModel.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Model;
|
||||
|
||||
use Kanboard\Core\Base;
|
||||
|
||||
/**
|
||||
* Class TagModel
|
||||
*
|
||||
* @package Kanboard\Model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class TagModel extends Base
|
||||
{
|
||||
/**
|
||||
* SQL table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TABLE = 'tags';
|
||||
|
||||
/**
|
||||
* Get all tags
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $this->db->table(self::TABLE)->asc('name')->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tags by project
|
||||
*
|
||||
* @access public
|
||||
* @param integer $project_id
|
||||
* @return array
|
||||
*/
|
||||
public function getAllByProject($project_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('name')->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one tag
|
||||
*
|
||||
* @access public
|
||||
* @param integer $tag_id
|
||||
* @return array|null
|
||||
*/
|
||||
public function getById($tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->eq('id', $tag_id)->findOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tag id from tag name
|
||||
*
|
||||
* @access public
|
||||
* @param int $project_id
|
||||
* @param string $tag
|
||||
* @return integer
|
||||
*/
|
||||
public function getIdByName($project_id, $tag)
|
||||
{
|
||||
return $this->db
|
||||
->table(self::TABLE)
|
||||
->beginOr()
|
||||
->eq('project_id', 0)
|
||||
->eq('project_id', $project_id)
|
||||
->closeOr()
|
||||
->ilike('name', $tag)
|
||||
->asc('project_id')
|
||||
->findOneColumn('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return tag id and create a new tag if necessary
|
||||
*
|
||||
* @access public
|
||||
* @param int $project_id
|
||||
* @param string $tag
|
||||
* @return bool|int
|
||||
*/
|
||||
public function findOrCreateTag($project_id, $tag)
|
||||
{
|
||||
$tag_id = $this->getIdByName($project_id, $tag);
|
||||
|
||||
if (empty($tag_id)) {
|
||||
$tag_id = $this->create($project_id, $tag);
|
||||
}
|
||||
|
||||
return $tag_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new tag
|
||||
*
|
||||
* @access public
|
||||
* @param int $project_id
|
||||
* @param string $tag
|
||||
* @return bool|int
|
||||
*/
|
||||
public function create($project_id, $tag)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->persist(array(
|
||||
'project_id' => $project_id,
|
||||
'name' => $tag,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a tag
|
||||
*
|
||||
* @access public
|
||||
* @param integer $tag_id
|
||||
* @param string $tag
|
||||
* @return bool
|
||||
*/
|
||||
public function update($tag_id, $tag)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->eq('id', $tag_id)->update(array(
|
||||
'name' => $tag,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a tag
|
||||
*
|
||||
* @access public
|
||||
* @param integer $tag_id
|
||||
* @return bool
|
||||
*/
|
||||
public function remove($tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->eq('id', $tag_id)->remove();
|
||||
}
|
||||
}
|
||||
125
app/Model/TaskTagModel.php
Normal file
125
app/Model/TaskTagModel.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Model;
|
||||
|
||||
use Kanboard\Core\Base;
|
||||
|
||||
/**
|
||||
* Class TaskTagModel
|
||||
*
|
||||
* @package Kanboard\Model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class TaskTagModel extends Base
|
||||
{
|
||||
/**
|
||||
* SQL table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TABLE = 'task_has_tags';
|
||||
|
||||
/**
|
||||
* Get all tags associated to a task
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @return array
|
||||
*/
|
||||
public function getAll($task_id)
|
||||
{
|
||||
return $this->db->table(TagModel::TABLE)
|
||||
->columns(TagModel::TABLE.'.id', TagModel::TABLE.'.name')
|
||||
->eq(self::TABLE.'.task_id', $task_id)
|
||||
->join(self::TABLE, 'tag_id', 'id')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dictionary of tags
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @return array
|
||||
*/
|
||||
public function getList($task_id)
|
||||
{
|
||||
$tags = $this->getAll($task_id);
|
||||
return array_column($tags, 'name', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or update a list of tags to a task
|
||||
*
|
||||
* @access public
|
||||
* @param integer $project_id
|
||||
* @param integer $task_id
|
||||
* @param string[] $tags
|
||||
* @return boolean
|
||||
*/
|
||||
public function save($project_id, $task_id, array $tags)
|
||||
{
|
||||
$task_tags = $this->getList($task_id);
|
||||
|
||||
return $this->addTags($project_id, $task_id, $task_tags, $tags) &&
|
||||
$this->removeTags($task_id, $task_tags, $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a tag to a task
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @param integer $tag_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function associate($task_id, $tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->insert(array(
|
||||
'task_id' => $task_id,
|
||||
'tag_id' => $tag_id,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dissociate a tag from a task
|
||||
*
|
||||
* @access public
|
||||
* @param integer $task_id
|
||||
* @param integer $tag_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function dissociate($task_id, $tag_id)
|
||||
{
|
||||
return $this->db->table(self::TABLE)
|
||||
->eq('task_id', $task_id)
|
||||
->eq('tag_id', $tag_id)
|
||||
->remove();
|
||||
}
|
||||
|
||||
private function addTags($project_id, $task_id, $task_tags, $tags)
|
||||
{
|
||||
foreach ($tags as $tag) {
|
||||
$tag_id = $this->tagModel->findOrCreateTag($project_id, $tag);
|
||||
|
||||
if (! isset($task_tags[$tag_id]) && ! $this->associate($task_id, $tag_id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function removeTags($task_id, $task_tags, $tags)
|
||||
{
|
||||
foreach ($task_tags as $tag_id => $tag) {
|
||||
if (! in_array($tag, $tags)) {
|
||||
if (! $this->dissociate($task_id, $tag_id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user