diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-28 20:44:19 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-28 20:44:19 -0400 |
commit | 14d6affe2e42fc86a86fbe5e624dbaacf6b8223c (patch) | |
tree | 2349e97550af64f4df05a490244d2702862967cf /app/Model | |
parent | 937079709514c037959d691c0d653c39d5ea84e6 (diff) |
Improve base FileModel class
Diffstat (limited to 'app/Model')
-rw-r--r-- | app/Model/FileModel.php | 77 | ||||
-rw-r--r-- | app/Model/ProjectFileModel.php | 52 | ||||
-rw-r--r-- | app/Model/TaskFileModel.php | 52 |
3 files changed, 143 insertions, 38 deletions
diff --git a/app/Model/FileModel.php b/app/Model/FileModel.php index ad839799..8cdea9a0 100644 --- a/app/Model/FileModel.php +++ b/app/Model/FileModel.php @@ -17,6 +17,42 @@ use Kanboard\Core\ObjectStorage\ObjectStorageException; abstract class FileModel extends Base { /** + * Get the table + * + * @abstract + * @access protected + * @return string + */ + abstract protected function getTable(); + + /** + * Define the foreign key + * + * @abstract + * @access protected + * @return string + */ + abstract protected function getForeignKey(); + + /** + * Get the path prefix + * + * @abstract + * @access protected + * @return string + */ + abstract protected function getPathPrefix(); + + /** + * Get event name + * + * @abstract + * @access protected + * @return string + */ + abstract protected function getEventName(); + + /** * Get PicoDb query to get all files * * @access protected @@ -25,21 +61,21 @@ abstract class FileModel extends Base protected function getQuery() { return $this->db - ->table(static::TABLE) + ->table($this->getTable()) ->columns( - static::TABLE.'.id', - static::TABLE.'.name', - static::TABLE.'.path', - static::TABLE.'.is_image', - static::TABLE.'.'.static::FOREIGN_KEY, - static::TABLE.'.date', - static::TABLE.'.user_id', - static::TABLE.'.size', + $this->getTable().'.id', + $this->getTable().'.name', + $this->getTable().'.path', + $this->getTable().'.is_image', + $this->getTable().'.'.$this->getForeignKey(), + $this->getTable().'.date', + $this->getTable().'.user_id', + $this->getTable().'.size', UserModel::TABLE.'.username', UserModel::TABLE.'.name as user_name' ) ->join(UserModel::TABLE, 'id', 'user_id') - ->asc(static::TABLE.'.name'); + ->asc($this->getTable().'.name'); } /** @@ -51,7 +87,7 @@ abstract class FileModel extends Base */ public function getById($file_id) { - return $this->db->table(static::TABLE)->eq('id', $file_id)->findOne(); + return $this->db->table($this->getTable())->eq('id', $file_id)->findOne(); } /** @@ -63,7 +99,7 @@ abstract class FileModel extends Base */ public function getAll($id) { - return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->findAll(); + return $this->getQuery()->eq($this->getForeignKey(), $id)->findAll(); } /** @@ -75,7 +111,7 @@ abstract class FileModel extends Base */ public function getAllImages($id) { - return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->eq('is_image', 1)->findAll(); + return $this->getQuery()->eq($this->getForeignKey(), $id)->eq('is_image', 1)->findAll(); } /** @@ -87,7 +123,7 @@ abstract class FileModel extends Base */ public function getAllDocuments($id) { - return $this->getQuery()->eq(static::FOREIGN_KEY, $id)->eq('is_image', 0)->findAll(); + return $this->getQuery()->eq($this->getForeignKey(), $id)->eq('is_image', 0)->findAll(); } /** @@ -103,7 +139,7 @@ abstract class FileModel extends Base public function create($id, $name, $path, $size) { $values = array( - static::FOREIGN_KEY => $id, + $this->getForeignKey() => $id, 'name' => substr($name, 0, 255), 'path' => $path, 'is_image' => $this->isImage($name) ? 1 : 0, @@ -112,12 +148,12 @@ abstract class FileModel extends Base 'date' => time(), ); - $result = $this->db->table(static::TABLE)->insert($values); + $result = $this->db->table($this->getTable())->insert($values); if ($result) { $file_id = (int) $this->db->getLastId(); $event = new FileEvent($values + array('file_id' => $file_id)); - $this->dispatcher->dispatch(static::EVENT_CREATE, $event); + $this->dispatcher->dispatch($this->getEventName(), $event); return $file_id; } @@ -133,7 +169,7 @@ abstract class FileModel extends Base */ public function removeAll($id) { - $file_ids = $this->db->table(static::TABLE)->eq(static::FOREIGN_KEY, $id)->asc('id')->findAllByColumn('id'); + $file_ids = $this->db->table($this->getTable())->eq($this->getForeignKey(), $id)->asc('id')->findAllByColumn('id'); $results = array(); foreach ($file_ids as $file_id) { @@ -160,7 +196,7 @@ abstract class FileModel extends Base $this->objectStorage->remove($this->getThumbnailPath($file['path'])); } - return $this->db->table(static::TABLE)->eq('id', $file['id'])->remove(); + return $this->db->table($this->getTable())->eq('id', $file['id'])->remove(); } catch (ObjectStorageException $e) { $this->logger->error($e->getMessage()); return false; @@ -211,7 +247,7 @@ abstract class FileModel extends Base */ public function generatePath($id, $filename) { - return static::PATH_PREFIX.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time()); + return $this->getPathPrefix().DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time()); } /** @@ -253,6 +289,7 @@ abstract class FileModel extends Base * @access public * @param integer $id * @param array $file + * @throws Exception */ public function uploadFile($id, array $file) { diff --git a/app/Model/ProjectFileModel.php b/app/Model/ProjectFileModel.php index 2177df27..b464bb2a 100644 --- a/app/Model/ProjectFileModel.php +++ b/app/Model/ProjectFileModel.php @@ -11,30 +11,64 @@ namespace Kanboard\Model; class ProjectFileModel extends FileModel { /** - * SQL table name + * Table name * * @var string */ const TABLE = 'project_has_files'; /** - * SQL foreign key + * Events * * @var string */ - const FOREIGN_KEY = 'project_id'; + const EVENT_CREATE = 'project.file.create'; /** - * Path prefix + * Get the table * - * @var string + * @abstract + * @access protected + * @return string */ - const PATH_PREFIX = 'projects'; + protected function getTable() + { + return self::TABLE; + } /** - * Events + * Define the foreign key * - * @var string + * @abstract + * @access protected + * @return string */ - const EVENT_CREATE = 'project.file.create'; + protected function getForeignKey() + { + return 'project_id'; + } + + /** + * Define the path prefix + * + * @abstract + * @access protected + * @return string + */ + protected function getPathPrefix() + { + return 'projects'; + } + + /** + * Get event name + * + * @abstract + * @access protected + * @return string + */ + protected function getEventName() + { + return self::EVENT_CREATE; + } } diff --git a/app/Model/TaskFileModel.php b/app/Model/TaskFileModel.php index 21d319b0..24c1ad4b 100644 --- a/app/Model/TaskFileModel.php +++ b/app/Model/TaskFileModel.php @@ -11,32 +11,66 @@ namespace Kanboard\Model; class TaskFileModel extends FileModel { /** - * SQL table name + * Table name * * @var string */ const TABLE = 'task_has_files'; /** - * SQL foreign key + * Events * * @var string */ - const FOREIGN_KEY = 'task_id'; + const EVENT_CREATE = 'task.file.create'; /** - * Path prefix + * Get the table * - * @var string + * @abstract + * @access protected + * @return string */ - const PATH_PREFIX = 'tasks'; + protected function getTable() + { + return self::TABLE; + } /** - * Events + * Define the foreign key * - * @var string + * @abstract + * @access protected + * @return string */ - const EVENT_CREATE = 'task.file.create'; + protected function getForeignKey() + { + return 'task_id'; + } + + /** + * Define the path prefix + * + * @abstract + * @access protected + * @return string + */ + protected function getPathPrefix() + { + return 'tasks'; + } + + /** + * Get event name + * + * @abstract + * @access protected + * @return string + */ + protected function getEventName() + { + return self::EVENT_CREATE; + } /** * Handle screenshot upload |