From d6f170db68af2a8179c8b4653423d8e7e8e91978 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= <tseeker@nocternity.net>
Date: Sun, 17 Jan 2016 00:40:01 +0100
Subject: [PATCH] Adding multiple dependencies

Added a selector that allows multiple dependencies to be added without
going back to the task's page.
---
 includes/t-tasks/controllers.inc.php      | 30 +++++++++++++++++++++--
 includes/t-tasks/page_controllers.inc.php | 13 ++++++++--
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/includes/t-tasks/controllers.inc.php b/includes/t-tasks/controllers.inc.php
index 263a0fd..29cf849 100644
--- a/includes/t-tasks/controllers.inc.php
+++ b/includes/t-tasks/controllers.inc.php
@@ -488,6 +488,12 @@ class Ctrl_DependencyAdd
 	implements FormAware
 {
 	private $form;
+	private $more;
+
+	public function __construct( $more = false )
+	{
+		$this->more = $more;
+	}
 
 	public function setForm( Form $form )
 	{
@@ -496,6 +502,10 @@ class Ctrl_DependencyAdd
 
 	public function handle( Page $page )
 	{
+		if ( $this->more ) {
+			return null;
+		}
+
 		$id = (int) $this->form->field( 'to' )->value( );
 		$dependency = $this->form->field( 'dependency' );
 		$error = Loader::DAO( 'tasks' )->addDependency( $id , $dependency->value( ) );
@@ -503,7 +513,7 @@ class Ctrl_DependencyAdd
 		switch ( $error ) {
 
 		case 0:
-			return true;
+			return $this->checkForMore( );
 
 		case 1:
 			$dependency->putError( 'The task you selected has been deleted.' );
@@ -524,6 +534,16 @@ class Ctrl_DependencyAdd
 
 		return null;
 	}
+
+
+	private function checkForMore( )
+	{
+		$field = $this->form->field( 'moar' );
+		if ( $field === null || !$field->value( ) ) {
+			return true;
+		}
+		return Loader::Ctrl( 'dependency_add_form' , true );
+	}
 }
 
 
@@ -629,8 +649,14 @@ class Ctrl_DependencyAddFiltering
 				$select->addOption( $task->id , $task->title );
 			}
 		}
-		return true;
 
+		if ( count( $this->task->possibleDependencies ) > 1 ) {
+			$this->selector->addField( Loader::Create( 'Field' , 'moar' , 'select' )
+				->setDescription( 'Add more dependencies:' )
+				->setMandatory( false )
+				->addOption( '0' , 'No' )
+				->addOption( '1' , 'Yes' ) );
+		}
 	}
 
 	private function getItemsToDisplay( $depsByItem )
diff --git a/includes/t-tasks/page_controllers.inc.php b/includes/t-tasks/page_controllers.inc.php
index 9803f2b..279db8a 100644
--- a/includes/t-tasks/page_controllers.inc.php
+++ b/includes/t-tasks/page_controllers.inc.php
@@ -433,6 +433,12 @@ class Ctrl_EditNoteForm
 class Ctrl_DependencyAddForm
 	extends Controller
 {
+	private $more;
+
+	public function __construct( $more = false )
+	{
+		$this->more = $more;
+	}
 
 	public function handle( Page $page )
 	{
@@ -458,10 +464,13 @@ class Ctrl_DependencyAddForm
 			->addField( Loader::Create( 'Field' , 'to' , 'hidden' )
 				->setDefaultValue( $id ) )
 			->setURL( 'tasks/view?id=' . $id )
-			->addController( Loader::Ctrl( 'dependency_add' ) );
+			->addController( Loader::Ctrl( 'dependency_add' , $this->more ) );
 
+		// Handle filtering and re-displaying
 		$filters = $this->handleFiltering( $page , $form , $task );
-
+		if ( $this->more ) {
+			return $form->controller( );
+		}
 		return array( $form->controller( ) , $filters );
 	}