Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Burgess committed Mar 21, 2013
1 parent 56dbd7c commit 36ba1b3
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
#### Eclipse IDE
/.project
/.pydevproject
/.classpath
/.buildpath
/.settings/

#### General
.DS_Store
73 changes: 72 additions & 1 deletion README.md
@@ -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 added _config/.gitignore
Empty file.
8 changes: 8 additions & 0 deletions code/HasOneGridFieldAddExistingSearchButton.php
@@ -0,0 +1,8 @@
<?php
class HasOneGridFieldAddExistingSearchButton extends GridFieldAddExistingSearchButton {

public function handleSearch($grid, $request) {
return new HasOneGridFieldAddExistingSearchHandler($grid, $this);
}

}
17 changes: 17 additions & 0 deletions code/HasOneGridFieldAddExistingSearchHandler.php
@@ -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();
}


}
39 changes: 39 additions & 0 deletions code/HasOnePickerGridField.php
@@ -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);
}

}
26 changes: 26 additions & 0 deletions code/PickerGridField.php
@@ -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);
}

}
27 changes: 27 additions & 0 deletions composer.json
@@ -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"
]
}
}
Binary file added 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.
Binary file added docs/screenshots/pickerfield.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 36ba1b3

Please sign in to comment.