diff options
Diffstat (limited to 'app/Controller/File.php')
-rw-r--r-- | app/Controller/File.php | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/app/Controller/File.php b/app/Controller/File.php new file mode 100644 index 00000000..1604ab13 --- /dev/null +++ b/app/Controller/File.php @@ -0,0 +1,136 @@ +<?php + +namespace Controller; + +use Model\File as FileModel; + +/** + * File controller + * + * @package controller + * @author Frederic Guillot + */ +class File extends Base +{ + /** + * File upload form + * + * @access public + */ + public function create() + { + $task = $this->getTask(); + + $this->response->html($this->taskLayout('file_new', array( + 'task' => $task, + 'menu' => 'tasks', + 'title' => t('Attach a document') + ))); + } + + /** + * File upload (save files) + * + * @access public + */ + public function save() + { + $task = $this->getTask(); + $this->file->upload($task['project_id'], $task['id'], 'files'); + $this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'#attachments'); + } + + /** + * File download + * + * @access public + */ + public function download() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + $filename = FileModel::BASE_PATH.$file['path']; + + if ($file['task_id'] == $task['id'] && file_exists($filename)) { + $this->response->forceDownload($file['name']); + $this->response->binary(file_get_contents($filename)); + } + + $this->response->redirect('?controller=task&action=show&task_id='.$task['id']); + } + + /** + * Open a file (show the content in a popover) + * + * @access public + */ + public function open() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + + if ($file['task_id'] == $task['id']) { + $this->response->html($this->template->load('file_open', array( + 'file' => $file + ))); + } + } + + /** + * Return the file content (work only for images) + * + * @access public + */ + public function image() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + $filename = FileModel::BASE_PATH.$file['path']; + + if ($file['task_id'] == $task['id'] && file_exists($filename)) { + $metadata = getimagesize($filename); + + if (isset($metadata['mime'])) { + $this->response->contentType($metadata['mime']); + readfile($filename); + } + } + } + + /** + * Remove a file + * + * @access public + */ + public function remove() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + + if ($file['task_id'] == $task['id'] && $this->file->remove($file['id'])) { + $this->session->flash(t('File removed successfully.')); + } else { + $this->session->flashError(t('Unable to remove this file.')); + } + + $this->response->redirect('?controller=task&action=show&task_id='.$task['id']); + } + + /** + * Confirmation dialog before removing a file + * + * @access public + */ + public function confirm() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + + $this->response->html($this->taskLayout('file_remove', array( + 'task' => $task, + 'file' => $file, + 'menu' => 'tasks', + 'title' => t('Remove a file') + ))); + } +} |