Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brice Burgess
committed
Mar 21, 2013
1 parent
56dbd7c
commit 36ba1b3
Showing
10 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#### Eclipse IDE | ||
/.project | ||
/.pydevproject | ||
/.classpath | ||
/.buildpath | ||
/.settings/ | ||
|
||
#### General | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,75 @@ | ||
silverstripe-pickerfield | ||
======================== | ||
|
||
SilverStripe 3 GridField based management of has_one , has_many , and many_many relationship selection | ||
SilverStripe 3 GridField based management of has_one , has_many , and many_many relationship selection | ||
|
||
|
||
## Requirements | ||
* SilverStripe >= 3.1 | ||
* Andrew Short's GridFieldExtensions [ https://github.com/ajshort/silverstripe-gridfieldextensions ] | ||
|
||
## Why? | ||
|
||
1. Because I needed a consistent interface to manage relationship selections in an efficient* manner. | ||
2. GridField doesn't appear to natively support has_one relationships. | ||
|
||
Thanks to the great work of the SilverStripe team and Andrew Short's GridFieldExtensions, this was relatively easy to implement. Please by them beers or tea. | ||
|
||
\* by efficient we needed ajax + pagination, as we couldn't load all records into a dropdown list for instance. | ||
|
||
## Usage Overview | ||
|
||
Screenshots; | ||
* overview - https://github.com/briceburg/silverstripe-pickerfield/blob/master/docs/screenshots/pickerfield.png | ||
* link button search [via GridFieldExtensions] - https://github.com/briceburg/silverstripe-pickerfield/blob/master/docs/screenshots/add-existing-search.png | ||
|
||
|
||
```php | ||
|
||
/*********************** | ||
Mock DataObject | ||
************************/ | ||
|
||
class Dog extends DataObject { | ||
static $db = array( | ||
'Title' => 'Varchar', | ||
// .... | ||
); | ||
|
||
static $has_one = array( | ||
'Breeder' => 'Breeder' | ||
// .... | ||
); | ||
|
||
static $has_many = array( | ||
'Owners' => 'Member', | ||
// .... | ||
); | ||
|
||
// .... | ||
|
||
} | ||
|
||
|
||
/*********************** | ||
Field Usage | ||
************************/ | ||
|
||
// sortable field appropriate for selection of has_many and many_many objects | ||
$field = new PickerGridField('Owners', 'Owners', $this->Owners(), 'Select Owner(s)', 'SortOrder'); | ||
|
||
// non-sortable version of the above | ||
$field = new PickerGridField('Owners', 'Owners', $this->Owners()); | ||
|
||
// sortable field appropriate for the parent selection of a has_one relationship | ||
new HasOnePickerGridField($this, 'BreederID','Breeder', 'Breeder', 'Select a Breeder') | ||
|
||
|
||
``` | ||
|
||
|
||
## Bugs | ||
|
||
For support or questions, please use the GitHub provided issue tracker; | ||
* https://github.com/briceburg/silverstripe-pickerfield/issues | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
class HasOneGridFieldAddExistingSearchButton extends GridFieldAddExistingSearchButton { | ||
|
||
public function handleSearch($grid, $request) { | ||
return new HasOneGridFieldAddExistingSearchHandler($grid, $this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
class HasOneGridFieldAddExistingSearchHandler extends GridFieldAddExistingSearchHandler { | ||
|
||
|
||
public function add($request) { | ||
if(!$id = $request->postVar('id')) { | ||
$this->httpError(400); | ||
} | ||
|
||
$childProperty = $this->grid->getModelClass() . 'ID'; | ||
$this->grid->childObject->$childProperty = $id; | ||
$this->grid->childObject->write(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
class HasOnePickerGridField extends PickerGridField { | ||
|
||
protected $childObject; | ||
|
||
public function __construct($childObject, $name, $title = null, $hasOneRelationMethod= null, $linkExistingTitle = 'Select Existing') { | ||
|
||
$this->childObject = $childObject; | ||
|
||
$modelClass = preg_replace('/ID$/','',$name); | ||
|
||
if(!$hasOneRelationMethod) | ||
{ | ||
$hasOneRelationMethod = $modelClass; | ||
} | ||
|
||
$this->setModelClass($modelClass); | ||
|
||
|
||
$parent = $childObject->$hasOneRelationMethod(); | ||
$dataList = $modelClass::get()->filter(array('ID' => $parent->ID)); | ||
|
||
|
||
// construct a PickerGridField | ||
parent::__construct($name, $title, $dataList); | ||
|
||
|
||
// remove components non-applicable to has_one relationships | ||
$this->getConfig()->removeComponentsByType('GridFieldPaginator'); | ||
$this->getConfig()->removeComponentsByType('GridFieldAddExistingSearchButton'); | ||
|
||
// add custom has_one handling | ||
$component = new HasOneGridFieldAddExistingSearchButton(); | ||
$component->setTitle($linkExistingTitle); | ||
$this->getConfig()->addComponent($component); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
class PickerGridField extends GridField { | ||
|
||
public function __construct($name, $title = null, SS_List $dataList = null, $linkExistingTitle = 'Select Existing', $sortField = null) { | ||
$config = GridfieldConfig::create()->addComponents( | ||
new GridFieldButtonRow('before'), | ||
new GridFieldToolbarHeader(), | ||
new GridFieldDataColumns(), | ||
new GridFieldTitleHeader(), | ||
new GridFieldDeleteAction(true), | ||
new GridFieldPaginator(), | ||
new GridFieldAddExistingSearchButton() | ||
); | ||
|
||
if($sortField) | ||
{ | ||
$config->addComponent(new GridFieldOrderableRows($sortField)); | ||
} | ||
|
||
$config->getComponentByType('GridFieldAddExistingSearchButton')->setTitle($linkExistingTitle); | ||
|
||
return parent::__construct($name, $title, $dataList, $config); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "briceburg/silverstripe-pickerfield", | ||
"description": "GridField based management of has_one , has_many , and many_many relationship selection", | ||
"type": "silverstripe-module", | ||
"homepage": "https://github.com/briceburg/silverstripe-pickerfield", | ||
"keywords": ["silverstripe", "gridfield", "has_one"], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Brice Burgess", | ||
"email": "brice@digome.com" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/briceburg/silverstripe-pickerfield/issues" | ||
}, | ||
"require": { | ||
"silverstripe/framework": ">=3.1" | ||
}, | ||
"extra": { | ||
"installer-name": "pickerfield", | ||
"screenshots": [ | ||
"docs/screenshots/pickerfield.png", | ||
"docs/screenshots/add-existing-search.png" | ||
] | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.