Simplify code to handle ajax popover and redirects

This commit is contained in:
Frederic Guillot
2016-01-30 22:25:16 -05:00
parent 4a52d327f7
commit bb040cfb78
27 changed files with 119 additions and 182 deletions

View File

@@ -189,7 +189,7 @@ abstract class Base extends \Kanboard\Core\Base
*/
protected function taskLayout($template, array $params)
{
$params['ajax'] = $this->request->isAjax() || $this->request->getIntegerParam('ajax') === 1;
$params['ajax'] = $this->request->isAjax();
$content = $this->template->render($template, $params);
if ($params['ajax']) {

View File

@@ -21,13 +21,11 @@ class Comment extends Base
$comment = $this->comment->getById($this->request->getIntegerParam('comment_id'));
if (empty($comment)) {
$this->notfound();
return $this->notfound();
}
if (! $this->userSession->isAdmin() && $comment['user_id'] != $this->userSession->getId()) {
$this->response->html($this->template->layout('comment/forbidden', array(
'title' => t('Access Forbidden')
)));
return $this->forbidden();
}
return $comment;
@@ -66,7 +64,6 @@ class Comment extends Base
{
$task = $this->getTask();
$values = $this->request->getValues();
$ajax = $this->request->isAjax() || $this->request->getIntegerParam('ajax');
list($valid, $errors) = $this->commentValidator->validateCreation($values);
@@ -77,11 +74,7 @@ class Comment extends Base
$this->flash->failure(t('Unable to create your comment.'));
}
if ($ajax) {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
}
$this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), 'comments'));
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), 'comments'), true);
}
$this->create($values, $errors);
@@ -126,7 +119,7 @@ class Comment extends Base
$this->flash->failure(t('Unable to update your comment.'));
}
$this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), 'comment-'.$comment['id']));
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), 'comment-'.$comment['id']));
}
$this->edit($values, $errors);

View File

@@ -23,17 +23,11 @@ class File extends Base
if ($this->request->isPost() && $this->file->uploadScreenshot($task['project_id'], $task['id'], $this->request->getValue('screenshot')) !== false) {
$this->flash->success(t('Screenshot uploaded successfully.'));
if ($this->request->getStringParam('redirect') === 'board') {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
}
$this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), true);
}
$this->response->html($this->taskLayout('file/screenshot', array(
'task' => $task,
'redirect' => 'task',
)));
}

View File

@@ -137,7 +137,7 @@ class TaskExternalLink extends Base
if ($valid && $this->taskExternalLink->update($values)) {
$this->flash->success(t('Link updated successfully.'));
return $this->response->redirect($this->helper->url->to('TaskExternalLink', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));
return $this->response->redirect($this->helper->url->to('TaskExternalLink', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), true);
}
$this->edit($values, $errors);

View File

@@ -18,22 +18,18 @@ class Taskcreation extends Base
public function create(array $values = array(), array $errors = array())
{
$project = $this->getProject();
$method = $this->request->isAjax() ? 'render' : 'layout';
$swimlanes_list = $this->swimlane->getList($project['id'], false, true);
if (empty($values)) {
$values = array(
'swimlane_id' => $this->request->getIntegerParam('swimlane_id', key($swimlanes_list)),
'column_id' => $this->request->getIntegerParam('column_id'),
'color_id' => $this->request->getStringParam('color_id', $this->color->getDefaultColor()),
'owner_id' => $this->request->getIntegerParam('owner_id'),
'another_task' => $this->request->getIntegerParam('another_task'),
'color_id' => $this->color->getDefaultColor(),
);
}
$this->response->html($this->template->$method('task_creation/form', array(
$this->response->html($this->template->render('task_creation/form', array(
'project' => $project,
'ajax' => $this->request->isAjax(),
'errors' => $errors,
'values' => $values + array('project_id' => $project['id']),
'columns_list' => $this->board->getColumnsList($project['id']),
@@ -61,25 +57,26 @@ class Taskcreation extends Base
if ($valid && $this->taskCreation->create($values)) {
$this->flash->success(t('Task created successfully.'));
$this->afterSave($project, $values);
} else {
$this->flash->failure(t('Unable to create your task.'));
return $this->afterSave($project, $values);
}
$this->flash->failure(t('Unable to create your task.'));
$this->create($values, $errors);
}
private function afterSave(array $project, array &$values)
{
if (isset($values['another_task']) && $values['another_task'] == 1) {
unset($values['title']);
unset($values['description']);
if (! $this->request->isAjax()) {
$this->response->redirect($this->helper->url->to('taskcreation', 'create', $values));
}
} else {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $project['id'])));
return $this->create(array(
'owner_id' => $values['owner_id'],
'color_id' => $values['color_id'],
'category_id' => $values['category_id'],
'column_id' => $values['column_id'],
'swimlane_id' => isset($values['swimlane_id']) ? $values['swimlane_id'] : 0,
'another_task' => 1,
));
}
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $project['id'])));
}
}

View File

@@ -22,7 +22,7 @@ class Tasklink extends Base
$link = $this->taskLink->getById($this->request->getIntegerParam('link_id'));
if (empty($link)) {
$this->notfound();
return $this->notfound();
}
return $link;
@@ -74,19 +74,13 @@ class Tasklink extends Base
{
$task = $this->getTask();
$values = $this->request->getValues();
$ajax = $this->request->isAjax() || $this->request->getIntegerParam('ajax');
list($valid, $errors) = $this->taskLinkValidator->validateCreation($values);
if ($valid) {
if ($this->taskLink->create($values['task_id'], $values['opposite_task_id'], $values['link_id'])) {
$this->flash->success(t('Link added successfully.'));
if ($ajax) {
return $this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
}
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])).'#links');
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])).'#links', true);
}
$errors = array('title' => array(t('The exact same link already exists')));

View File

@@ -48,41 +48,44 @@ class Taskmodification extends Base
*
* @access public
*/
public function description()
public function description(array $values = array(), array $errors = array())
{
$task = $this->getTask();
$ajax = $this->request->isAjax() || $this->request->getIntegerParam('ajax');
if ($this->request->isPost()) {
$values = $this->request->getValues();
list($valid, $errors) = $this->taskValidator->validateDescriptionCreation($values);
if ($valid) {
if ($this->taskModification->update($values)) {
$this->flash->success(t('Task updated successfully.'));
} else {
$this->flash->failure(t('Unable to update your task.'));
}
if ($ajax) {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
} else {
$this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])));
}
}
} else {
$values = $task;
$errors = array();
if (empty($values)) {
$values = array('id' => $task['id'], 'description' => $task['description']);
}
$params = array(
$this->response->html($this->taskLayout('task_modification/edit_description', array(
'values' => $values,
'errors' => $errors,
'task' => $task,
);
)));
}
$this->response->html($this->taskLayout('task_modification/edit_description', $params));
/**
* Update description
*
* @access public
*/
public function updateDescription()
{
$task = $this->getTask();
$values = $this->request->getValues();
list($valid, $errors) = $this->taskValidator->validateDescriptionCreation($values);
if ($valid) {
if ($this->taskModification->update($values)) {
$this->flash->success(t('Task updated successfully.'));
} else {
$this->flash->failure(t('Unable to update your task.'));
}
return $this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])), true);
}
$this->description($values, $errors);
}
/**
@@ -94,7 +97,6 @@ class Taskmodification extends Base
{
$task = $this->getTask();
$project = $this->project->getById($task['project_id']);
$ajax = $this->request->isAjax();
if (empty($values)) {
$values = $task;
@@ -102,7 +104,7 @@ class Taskmodification extends Base
$this->dateParser->format($values, array('date_due'));
$params = array(
$this->response->html($this->taskLayout('task_modification/edit_task', array(
'project' => $project,
'values' => $values,
'errors' => $errors,
@@ -112,16 +114,7 @@ class Taskmodification extends Base
'categories_list' => $this->category->getList($task['project_id']),
'date_format' => $this->config->get('application_date_format'),
'date_formats' => $this->dateParser->getAvailableFormats(),
'ajax' => $ajax,
);
if ($ajax) {
$html = $this->template->render('task_modification/edit_task', $params);
} else {
$html = $this->taskLayout('task_modification/edit_task', $params);
}
$this->response->html($html);
)));
}
/**
@@ -138,12 +131,7 @@ class Taskmodification extends Base
if ($valid && $this->taskModification->update($values)) {
$this->flash->success(t('Task updated successfully.'));
if ($this->request->isAjax()) {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
} else {
$this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])));
}
return $this->response->redirect($this->helper->url->to('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])), true);
} else {
$this->flash->failure(t('Unable to update your task.'));
$this->edit($values, $errors);

View File

@@ -17,9 +17,7 @@ class Taskstatus extends Base
*/
public function close()
{
$task = $this->getTask();
$this->changeStatus($task, 'close', t('Task closed successfully.'), t('Unable to close this task.'));
$this->renderTemplate($task, 'task_status/close');
$this->changeStatus('close', 'task_status/close', t('Task closed successfully.'), t('Unable to close this task.'));
}
/**
@@ -29,13 +27,22 @@ class Taskstatus extends Base
*/
public function open()
{
$task = $this->getTask();
$this->changeStatus($task, 'open', t('Task opened successfully.'), t('Unable to open this task.'));
$this->renderTemplate($task, 'task_status/open');
$this->changeStatus('open', 'task_status/open', t('Task opened successfully.'), t('Unable to open this task.'));
}
private function changeStatus(array $task, $method, $success_message, $failure_message)
/**
* Common method to change status
*
* @access private
* @param string $method
* @param string $template
* @param string $success_message
* @param string $failure_message
*/
private function changeStatus($method, $template, $success_message, $failure_message)
{
$task = $this->getTask();
if ($this->request->getStringParam('confirmation') === 'yes') {
$this->checkCSRFParam();
@@ -45,28 +52,11 @@ class Taskstatus extends Base
$this->flash->failure($failure_message);
}
if ($this->request->getStringParam('redirect') === 'board') {
$this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $task['project_id'])));
}
$this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));
}
}
private function renderTemplate(array $task, $template)
{
$redirect = $this->request->getStringParam('redirect');
if ($this->request->isAjax()) {
$this->response->html($this->template->render($template, array(
'task' => $task,
'redirect' => $redirect,
)));
return $this->response->redirect($this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), true);
}
$this->response->html($this->taskLayout($template, array(
'task' => $task,
'redirect' => $redirect,
)));
}
}