Skip to content

Commit

Permalink
FEATURE: Made it possible to hide candidates that have already been p…
Browse files Browse the repository at this point in the history
…icked by setting the ShowPickedInSearch option to false.
  • Loading branch information
ajshort committed Feb 4, 2011
1 parent 14e165c commit 05cdef0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
29 changes: 25 additions & 4 deletions code/HasManyPickerField.php
Expand Up @@ -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'
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 2 additions & 8 deletions code/ManyManyPickerField.php
Expand Up @@ -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);

Expand All @@ -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\"";
Expand Down
5 changes: 5 additions & 0 deletions docs/en/index.md
Expand Up @@ -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
Expand Down

0 comments on commit 05cdef0

Please sign in to comment.