From f7e4c3928aba9cb7f5222cb4af67846312bbb435 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 14 Feb 2015 16:11:13 -0500 Subject: Refactoring/simplification of the pull-request about links --- app/Controller/App.php | 29 +++++++++++---- app/Controller/Base.php | 4 +- app/Controller/Board.php | 2 +- app/Controller/Calendar.php | 4 +- app/Controller/Config.php | 7 +--- app/Controller/Link.php | 91 ++++++++++++++++----------------------------- app/Controller/Task.php | 6 +-- app/Controller/Tasklink.php | 74 ++++++++---------------------------- 8 files changed, 77 insertions(+), 140 deletions(-) (limited to 'app/Controller') diff --git a/app/Controller/App.php b/app/Controller/App.php index ef0a08a9..46731e7c 100644 --- a/app/Controller/App.php +++ b/app/Controller/App.php @@ -2,7 +2,8 @@ namespace Controller; -use Model\Subtask as SubTaskModel; +use Model\Subtask as SubtaskModel; +use Model\Task as TaskModel; /** * Application controller @@ -39,7 +40,7 @@ class App extends Base */ public function index($user_id = 0, $action = 'index') { - $status = array(SubTaskModel::STATUS_TODO, SubTaskModel::STATUS_INPROGRESS); + $status = array(SubTaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS); $user_id = $user_id ?: $this->userSession->getId(); $projects = $this->projectPermission->getActiveMemberProjects($user_id); $project_ids = array_keys($projects); @@ -88,11 +89,8 @@ class App extends Base if (empty($payload['text'])) { $this->response->html('

'.t('Nothing to preview...').'

'); } - else { - $this->response->html( - $this->template->markdown($payload['text']) - ); - } + + $this->response->html($this->template->markdown($payload['text'])); } /** @@ -104,4 +102,21 @@ class App extends Base { $this->response->css($this->color->getCss()); } + + /** + * Task autocompletion (Ajax) + * + * @access public + */ + public function autocomplete() + { + $this->response->json( + $this->taskFilter + ->create() + ->filterByProjects($this->projectPermission->getActiveMemberProjectIds($this->userSession->getId())) + ->excludeTasks(array($this->request->getIntegerParam('exclude_task_id'))) + ->filterByTitle($this->request->getStringParam('term')) + ->toAutoCompletion() + ); + } } diff --git a/app/Controller/Base.php b/app/Controller/Base.php index 2c8b5cde..76a81612 100644 --- a/app/Controller/Base.php +++ b/app/Controller/Base.php @@ -141,7 +141,7 @@ abstract class Base private function sendHeaders($action) { // HTTP secure headers - $this->response->csp(array('style-src' => "'self' 'unsafe-inline'")); + $this->response->csp(array('style-src' => "'self' 'unsafe-inline'", 'img-src' => "'self' data:")); $this->response->nosniff(); $this->response->xss(); @@ -201,7 +201,7 @@ abstract class Base { $project_id = $this->request->getIntegerParam('project_id'); $task_id = $this->request->getIntegerParam('task_id'); - + // Allow urls without "project_id" if ($task_id > 0 && $project_id === 0) { $project_id = $this->taskFinder->getProjectId($task_id); diff --git a/app/Controller/Board.php b/app/Controller/Board.php index ae249982..90b7f357 100644 --- a/app/Controller/Board.php +++ b/app/Controller/Board.php @@ -410,7 +410,7 @@ class Board extends Base { $task = $this->getTask(); $this->response->html($this->template->render('board/tasklinks', array( - 'links' => $this->taskLink->getAll($task['id']), + 'links' => $this->taskLink->getLinks($task['id']), 'task' => $task, ))); } diff --git a/app/Controller/Calendar.php b/app/Controller/Calendar.php index abbcab7f..0e749558 100644 --- a/app/Controller/Calendar.php +++ b/app/Controller/Calendar.php @@ -2,7 +2,7 @@ namespace Controller; -use Model\Task; +use Model\Task as TaskModel; /** * Project Calendar controller @@ -74,7 +74,7 @@ class Calendar extends Base $this->taskFilter ->create() ->filterByOwner($user_id) - ->filterByStatus(Task::STATUS_OPEN) + ->filterByStatus(TaskModel::STATUS_OPEN) ->filterByDueDateRange( $this->request->getStringParam('start'), $this->request->getStringParam('end') diff --git a/app/Controller/Config.php b/app/Controller/Config.php index 6ec10279..01c7ad53 100644 --- a/app/Controller/Config.php +++ b/app/Controller/Config.php @@ -87,17 +87,12 @@ class Config extends Base * * @access public */ - public function board(array $values = array(), array $errors = array()) + public function board() { $this->common('board'); $this->response->html($this->layout('config/board', array( 'default_columns' => implode(', ', $this->board->getDefaultColumns()), - 'links' => $this->link->getMergedList(), - 'values' => $values + array( - 'project_id' => -1 - ), - 'errors' => $errors, 'title' => t('Settings').' > '.t('Board settings'), ))); } diff --git a/app/Controller/Link.php b/app/Controller/Link.php index fca9017a..ec9c6195 100644 --- a/app/Controller/Link.php +++ b/app/Controller/Link.php @@ -1,11 +1,13 @@ projectPermission->getAllowedProjects($this->userSession->getId()); $params['config_content_for_layout'] = $this->template->render($template, $params); - - if (isset($params['values']['project_id']) && -1 != $params['values']['project_id']) { - return $this->projectLayout($template, $params); - } + return $this->template->layout('config/layout', $params); } - + /** * Get the current link * @@ -36,49 +35,27 @@ class Link extends Base */ private function getLink() { - $link = $this->link->getById($this->request->getIntegerParam('link_id'), $this->request->getIntegerParam('project_id', -1)); + $link = $this->link->getById($this->request->getIntegerParam('link_id')); + if (! $link) { $this->notfound(); } - $link['link_id'] = $link[0]['link_id']; - $link['project_id'] = $link[0]['project_id']; - return $link; - } - /** - * Method to get a project - * - * @access protected - * @param integer $project_id Default project id - * @return array - */ - protected function getProject($project_id = -1) - { - $project = array('id' => $project_id); - $project_id = $this->request->getIntegerParam('project_id', $project_id); - if (-1 != $project_id) { - $project = parent::getProject($project_id); - } - return $project; + return $link; } /** - * List of links for a given project + * List of links * * @access public */ public function index(array $values = array(), array $errors = array()) { - $project = $this->getProject(); - $values['project_id'] = $project['id']; - $values[] = array(); - $this->response->html($this->layout('link/index', array( - 'links' => $this->link->getMergedList($project['id']), + 'links' => $this->link->getMergedList(), 'values' => $values, 'errors' => $errors, - 'project' => $project, - 'title' => t('Settings').' > '.t('Board\'s links settings'), + 'title' => t('Settings').' > '.t('Task\'s links'), ))); } @@ -91,19 +68,18 @@ class Link extends Base { $values = $this->request->getValues(); list($valid, $errors) = $this->link->validateCreation($values); - + if ($valid) { - if ($this->link->create($values)) { + + if ($this->link->create($values['label'], $values['opposite_label'])) { $this->session->flash(t('Link added successfully.')); - $this->response->redirect('?controller=link&action=index&project_id='.$values['project_id']); + $this->response->redirect($this->helper->url('link', 'index')); } else { $this->session->flashError(t('Unable to create your link.')); } } - if (!empty($values)) { - $this->link->prepare($values); - } + $this->index($values, $errors); } @@ -114,14 +90,15 @@ class Link extends Base */ public function edit(array $values = array(), array $errors = array()) { - $project = $this->getProject(); - + $link = $this->getLink(); + $link['label'] = t($link['label']); + $this->response->html($this->layout('link/edit', array( - 'values' => empty($values) ? $this->getLink() : $values, + 'values' => $values ?: $link, 'errors' => $errors, - 'project' => $project, - 'edit' => true, - 'title' => t('Links') + 'labels' => $this->link->getList($link['id']), + 'link' => $link, + 'title' => t('Link modification') ))); } @@ -134,19 +111,17 @@ class Link extends Base { $values = $this->request->getValues(); list($valid, $errors) = $this->link->validateModification($values); - + if ($valid) { if ($this->link->update($values)) { $this->session->flash(t('Link updated successfully.')); - $this->response->redirect('?controller=link&action=index&project_id='.$values['project_id']); + $this->response->redirect($this->helper->url('link', 'index')); } - else { + else { $this->session->flashError(t('Unable to update your link.')); } } - if (!empty($values)) { - $this->link->prepare($values); - } + $this->edit($values, $errors); } @@ -157,11 +132,9 @@ class Link extends Base */ public function confirm() { - $project = $this->getProject(); $link = $this->getLink(); - + $this->response->html($this->layout('link/remove', array( - 'project' => $project, 'link' => $link, 'title' => t('Remove a link') ))); @@ -176,14 +149,14 @@ class Link extends Base { $this->checkCSRFParam(); $link = $this->getLink(); - - if ($this->link->remove($link['link_id'])) { + + if ($this->link->remove($link['id'])) { $this->session->flash(t('Link removed successfully.')); - $this->response->redirect('?controller=link&action=index&project_id='.$link['project_id']); } else { $this->session->flashError(t('Unable to remove this link.')); } - $this->confirm(); + + $this->response->redirect($this->helper->url('link', 'index')); } } diff --git a/app/Controller/Task.php b/app/Controller/Task.php index e4c2d773..0789e8eb 100644 --- a/app/Controller/Task.php +++ b/app/Controller/Task.php @@ -37,7 +37,7 @@ class Task extends Base 'project' => $project, 'comments' => $this->comment->getAll($task['id']), 'subtasks' => $this->subtask->getAll($task['id']), - 'links' => $this->taskLink->getAll($task['id']), + 'links' => $this->taskLink->getLinks($task['id']), 'task' => $task, 'columns_list' => $this->board->getColumnsList($task['project_id']), 'colors_list' => $this->color->getList(), @@ -72,13 +72,11 @@ class Task extends Base 'files' => $this->file->getAll($task['id']), 'comments' => $this->comment->getAll($task['id']), 'subtasks' => $subtasks, - 'links' => $this->taskLink->getAll($task['id']), + 'links' => $this->taskLink->getLinks($task['id']), 'task' => $task, 'values' => $values, 'columns_list' => $this->board->getColumnsList($task['project_id']), 'colors_list' => $this->color->getList(), - 'link_list' => $this->link->getLinkLabelList($task['project_id'], false), - 'task_list' => $this->taskFinder->getList($task['project_id'], TaskModel::STATUS_OPEN, $task['id']), 'date_format' => $this->config->get('application_date_format'), 'date_formats' => $this->dateParser->getAvailableFormats(), 'title' => $task['project_name'].' > '.$task['title'], diff --git a/app/Controller/Tasklink.php b/app/Controller/Tasklink.php index d76de8fe..61b7fab8 100644 --- a/app/Controller/Tasklink.php +++ b/app/Controller/Tasklink.php @@ -2,12 +2,12 @@ namespace Controller; -use Model\Task AS TaskModel; /** * TaskLink controller * * @package controller * @author Olivier Maridat + * @author Frederic Guillot */ class Tasklink extends Base { @@ -20,9 +20,11 @@ class Tasklink extends Base private function getTaskLink() { $link = $this->taskLink->getById($this->request->getIntegerParam('link_id')); + if (! $link) { $this->notfound(); } + return $link; } @@ -38,16 +40,15 @@ class Tasklink extends Base if (empty($values)) { $values = array( 'task_id' => $task['id'], - 'another_link' => $this->request->getIntegerParam('another_link', 0) ); } - $this->response->html($this->taskLayout('tasklink/edit', array( + $this->response->html($this->taskLayout('tasklink/create', array( 'values' => $values, 'errors' => $errors, - 'link_list' => $this->link->getLinkLabelList($task['project_id']), - 'task_list' => $this->taskFinder->getList($task['project_id'], TaskModel::STATUS_OPEN, $task['id']), 'task' => $task, + 'labels' => $this->link->getList(0, false), + 'title' => t('Add a new link') ))); } @@ -60,69 +61,23 @@ class Tasklink extends Base { $task = $this->getTask(); $values = $this->request->getValues(); + list($valid, $errors) = $this->taskLink->validateCreation($values); if ($valid) { - if ($this->taskLink->create($values)) { + + if ($this->taskLink->create($values['task_id'], $values['opposite_task_id'], $values['link_id'])) { $this->session->flash(t('Link added successfully.')); - if (isset($values['another_link']) && $values['another_link'] == 1) { - $this->response->redirect('?controller=tasklink&action=create&task_id='.$task['id'].'&project_id='.$task['project_id'].'&another_link=1'); - } - - $this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#links'); + $this->response->redirect($this->helper->url('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])).'#links'); } else { - $this->session->flashError(t('Unable to add the link.')); + $this->session->flashError(t('Unable to create your link.')); } } $this->create($values, $errors); } - /** - * Edit form - * - * @access public - */ - public function edit(array $values = array(), array $errors = array()) - { - $task = $this->getTask(); - $taskLink = $this->getTaskLink(); - - $this->response->html($this->taskLayout('tasklink/edit', array( - 'values' => empty($values) ? $taskLink : $values, - 'errors' => $errors, - 'link_list' => $this->link->getLinkLabelList($task['project_id'], false), - 'task_list' => $this->taskFinder->getList($task['project_id'], TaskModel::STATUS_OPEN, $task['id']), - 'link' => $taskLink, - 'task' => $task, - 'edit' => true, - ))); - } - - /** - * Update and validate a link - * - * @access public - */ - public function update() - { - $task = $this->getTask(); - $values = $this->request->getValues(); - list($valid, $errors) = $this->taskLink->validateModification($values); - - if ($valid) { - if ($this->taskLink->update($values)) { - $this->session->flash(t('Link updated successfully.')); - $this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#links'); - } - else { - $this->session->flashError(t('Unable to update the link.')); - } - } - $this->edit($values, $errors); - } - /** * Confirmation dialog before removing a link * @@ -132,6 +87,7 @@ class Tasklink extends Base { $task = $this->getTask(); $link = $this->getTaskLink(); + $this->response->html($this->taskLayout('tasklink/remove', array( 'link' => $link, 'task' => $task, @@ -147,14 +103,14 @@ class Tasklink extends Base { $this->checkCSRFParam(); $task = $this->getTask(); - + if ($this->taskLink->remove($this->request->getIntegerParam('link_id'))) { $this->session->flash(t('Link removed successfully.')); - $this->response->redirect('?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'].'#links'); } else { $this->session->flashError(t('Unable to remove this link.')); } - $this->confirm(); + + $this->response->redirect($this->helper->url('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']))); } } -- cgit v1.2.3