"Remove dependency" implemented

Added a link to dependency views which causes dependencies to be removed
after a confirmation page.
This commit is contained in:
Emmanuel BENOîT 2012-02-05 21:22:06 +01:00
parent ba3fcc2470
commit bbf2c4a13c
6 changed files with 116 additions and 4 deletions

View file

@ -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 );
}
}

View file

@ -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;
}
}

View file

@ -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';

View file

@ -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;
}
}

View file

@ -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' ,
));
}

View file

@ -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;
}