From bbf2c4a13c592ea779cac24e7b8d0c9549683e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 5 Feb 2012 21:22:06 +0100 Subject: [PATCH] "Remove dependency" implemented Added a link to dependency views which causes dependencies to be removed after a confirmation page. --- includes/t-data/dao_tasks.inc.php | 6 ++ includes/t-tasks/controllers.inc.php | 21 +++++++ includes/t-tasks/package.inc.php | 2 + includes/t-tasks/page_controllers.inc.php | 70 +++++++++++++++++++++++ includes/t-tasks/pages.inc.php | 1 + includes/t-tasks/views.inc.php | 20 +++++-- 6 files changed, 116 insertions(+), 4 deletions(-) diff --git a/includes/t-data/dao_tasks.inc.php b/includes/t-data/dao_tasks.inc.php index 0c33450..be9cea1 100644 --- a/includes/t-data/dao_tasks.inc.php +++ b/includes/t-data/dao_tasks.inc.php @@ -231,4 +231,10 @@ class DAO_Tasks return $result[0]->error; } + public function deleteDependency( $from , $to ) + { + $this->query( 'DELETE FROM task_dependencies WHERE task_id = $1 AND task_id_depends = $2' ) + ->execute( $from , $to ); + } + } diff --git a/includes/t-tasks/controllers.inc.php b/includes/t-tasks/controllers.inc.php index 72bb8d0..2db9ff5 100644 --- a/includes/t-tasks/controllers.inc.php +++ b/includes/t-tasks/controllers.inc.php @@ -385,3 +385,24 @@ class Ctrl_DependencyAdd return null; } } + + +class Ctrl_DependencyDelete + extends Controller + implements FormAware +{ + private $form; + + public function setForm( Form $form ) + { + $this->form = $form; + } + + public function handle( Page $page ) + { + Loader::DAO( 'tasks' )->deleteDependency( + (int) $this->form->field( 'from' )->value( ) , + (int) $this->form->field( 'to' )->value( ) ); + return true; + } +} diff --git a/includes/t-tasks/package.inc.php b/includes/t-tasks/package.inc.php index c7956a2..1f495b0 100644 --- a/includes/t-tasks/package.inc.php +++ b/includes/t-tasks/package.inc.php @@ -20,6 +20,8 @@ $package[ 'ctrls' ][] = 'delete_task_form'; $package[ 'ctrls' ][] = 'delete_task'; $package[ 'ctrls' ][] = 'dependency_add'; $package[ 'ctrls' ][] = 'dependency_add_form'; +$package[ 'ctrls' ][] = 'dependency_delete'; +$package[ 'ctrls' ][] = 'dependency_delete_form'; $package[ 'ctrls' ][] = 'edit_note_form'; $package[ 'ctrls' ][] = 'edit_note'; $package[ 'ctrls' ][] = 'edit_task_form'; diff --git a/includes/t-tasks/page_controllers.inc.php b/includes/t-tasks/page_controllers.inc.php index 6f304be..5701e63 100644 --- a/includes/t-tasks/page_controllers.inc.php +++ b/includes/t-tasks/page_controllers.inc.php @@ -447,3 +447,73 @@ class Ctrl_DependencyAddForm return $result; } } + + +class Ctrl_DependencyDeleteForm + extends Controller +{ + + public function handle( Page $page ) + { + $tasks = Loader::DAO( 'tasks' ); + + // Get the task a dependency is being removed from + try { + $from = (int) $this->getParameter( 'from' ); + } catch ( ParameterException $e ) { + return 'tasks'; + } + $task = $tasks->get( $from ); + if ( $task === null ) { + return 'tasks'; + } + $page->setTitle( $task->title . ' (task)' ); + if ( $task->completed_at !== null ) { + return 'tasks/view?id=' . $from; + } + + // Get the dependency being deleted + try { + $to = (int) $this->getParameter( 'to' ); + } catch ( ParameterException $e ) { + return 'tasks/view?id=' . $from; + } + $dependency = $tasks->get( $to ); + if ( $dependency === null || ! $this->checkDependency( $task , $to ) ) { + return 'tasks/view?id=' . $from; + } + + // Generate confirmation text + $confText = HTML::make( 'div' ) + ->appendElement( HTML::make( 'p' ) + ->appendText( 'The selected task will no longer depend on ' ) + ->appendElement( HTML::make( 'strong' ) + ->appendText( $dependency->title ) ) + ->appendText( '.' ) ); + + // Generate form + return Loader::Create( 'Form' , 'Delete dependency' , 'delete-dep' ) + ->addField( Loader::Create( 'Field' , 'from' , 'hidden' ) + ->setDefaultValue( $from ) ) + ->addField( Loader::Create( 'Field' , 'to' , 'hidden' ) + ->setDefaultValue( $to ) ) + ->addField( Loader::Create( 'Field' , 'confirm' , 'html' )->setDefaultValue( $confText ) ) + ->setURL( 'tasks/view?id=' . $from ) + ->addController( Loader::Ctrl( 'dependency_delete' ) ) + ->controller( ); + + } + + + private function checkDependency( $task , $to ) + { + foreach ( $task->dependencies as $dep ) { + if ( $dep->id == $to ) { + return true; + } + } + return false; + } +} + + diff --git a/includes/t-tasks/pages.inc.php b/includes/t-tasks/pages.inc.php index 67ebe12..a13bc88 100644 --- a/includes/t-tasks/pages.inc.php +++ b/includes/t-tasks/pages.inc.php @@ -17,6 +17,7 @@ class Page_TasksTasks 'notes/edit' => 'edit_note_form' , 'notes/delete' => 'delete_note_form' , 'deps/add' => 'dependency_add_form' , + 'deps/delete' => 'dependency_delete_form' , )); } diff --git a/includes/t-tasks/views.inc.php b/includes/t-tasks/views.inc.php index c81ccff..a6f3df1 100644 --- a/includes/t-tasks/views.inc.php +++ b/includes/t-tasks/views.inc.php @@ -306,14 +306,26 @@ class View_TaskDependencies ->appendElement( $itemList ) ); $prevItem = $dependency->item; } - $link = HTML::make( 'a' ) - ->setAttribute( 'href' , $this->base . '/tasks/view?id=' . $dependency->id ) - ->appendText( $dependency->title ); + + $entry = HTML::make( 'li' )->appendElement( + $link = HTML::make( 'a' ) + ->setAttribute( 'href' , $this->base . '/tasks/view?id=' . $dependency->id ) + ->appendText( $dependency->title ) ); if ( ! $this->reverse ) { $link->setAttribute( 'class' , ( $dependency->completed == 't' ) ? 'satisfied' : 'missing' ); + + if ( $this->task->completed_at === null ) { + $entry->appendText( ' (' ) + ->appendElement( HTML::make( 'a' ) + ->setAttribute( 'href' , $this->base . '/tasks/deps/delete?from=' + . $this->task->id . '&to=' . $dependency->id ) + ->appendText( 'remove') ) + ->appendText( ')' ); + } } - $itemList->appendElement( HTML::make( 'li' )->appendElement( $link ) ); + + $itemList->appendElement( $entry ); } return $list; }