From 05cdef05c09787977bddbe51b5296fd52300ea2e Mon Sep 17 00:00:00 2001 From: ajshort Date: Fri, 4 Feb 2011 23:12:53 +1100 Subject: [PATCH] FEATURE: Made it possible to hide candidates that have already been picked by setting the ShowPickedInSearch option to false. --- code/HasManyPickerField.php | 29 +++++++++++++++++++++++++---- code/ManyManyPickerField.php | 10 ++-------- docs/en/index.md | 5 +++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/code/HasManyPickerField.php b/code/HasManyPickerField.php index 925b7dd..8ac8cf2 100644 --- a/code/HasManyPickerField.php +++ b/code/HasManyPickerField.php @@ -4,9 +4,9 @@ */ class HasManyPickerField extends ItemSetField { - protected $parent; - protected $otherClass; - protected $searchField; + public static $default_options = array( + 'ShowPickedInSearch' => true + ); public static $actions = array( 'Search' => 'search' @@ -16,6 +16,11 @@ class HasManyPickerField extends ItemSetField { 'Remove' ); + protected $parent; + protected $otherClass; + protected $searchField; + protected $searchFieldClass = 'ManyManyPickerField_SearchField'; + public function __construct($parent, $name, $title = null, $options = null) { $this->parent = $parent; $this->otherClass = $parent->has_many($name); @@ -28,7 +33,7 @@ public function __construct($parent, $name, $title = null, $options = null) { */ public function getSearchField() { if (!$this->searchField) { - $this->searchField = new ManyManyPickerField_SearchField($this); + $this->searchField = new $this->searchFieldClass($this); } return $this->searchField; @@ -120,6 +125,22 @@ public function __construct($parent) { parent::__construct($parent->getOtherClass(), 'Results'); } + public function getItemsQuery() { + $query = parent::getItemsQuery(); + + if (!$this->parent->getOption('ShowPickedInSearch')) { + $id = sprintf('"%s"."ID"', $this->parent->getOtherClass()); + $ignore = $this->parent->getItemsQuery(); + $ignore->select($id); + + if ($ids = $ignore->execute()->column()) { + $query->where("$id NOT IN (" . implode(', ', $ids) . ')'); + } + } + + return $query; + } + public function Choose($data, $item) { return $this->parent->Add($data, $item); } diff --git a/code/ManyManyPickerField.php b/code/ManyManyPickerField.php index 3793a1a..ee44748 100644 --- a/code/ManyManyPickerField.php +++ b/code/ManyManyPickerField.php @@ -9,6 +9,8 @@ class ManyManyPickerField extends HasManyPickerField { 'Sortable' => false ); + protected $searchFieldClass = 'ManyManyPickerField_SearchField'; + public function __construct($parent, $name, $title=null, $options=null) { parent::__construct($parent, $name, $title, $options); @@ -17,14 +19,6 @@ public function __construct($parent, $name, $title=null, $options=null) { $this->otherClass = ( $parent->class == $parentClass || ClassInfo::is_subclass_of($parent->class, $parentClass)) ? $componentClass : $parentClass; } - public function getSearchField() { - if (!$this->searchField) { - $this->searchField = new ManyManyPickerField_SearchField($this); - } - - return $this->searchField; - } - public function getItemsQuery() { if ($this->getOption('Sortable')) { $sort = "\"{$this->joinTable}\".\"ID\""; diff --git a/docs/en/index.md b/docs/en/index.md index 2a9775e..91d7fa3 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -61,6 +61,11 @@ Below is a listing of options available one each class: would be included in the results list. If the callback returns FALSE then the item will not be included. +### HasManyPickerField (and children HasOnePickerField and HasManyPickerField) +* **ShowPickedInSearch** (`bool`) - If this is TRUE, then objects that have + already been picked will show in search results. It is set to TRUE by + default. + ### ManyManyPickerField * **Sortable** (`bool`) - Either enables or disables the drag and drop ordering