2008-08-06 05:28:25 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-08-09 04:00:40 +02:00
|
|
|
* Generates a three-pane UI for editing model classes,
|
2008-08-06 05:28:25 +02:00
|
|
|
* with an automatically generated search panel, tabular results
|
|
|
|
* and edit forms.
|
|
|
|
* Relies on data such as {@link DataObject::$db} and {@DataObject::getCMSFields()}
|
|
|
|
* to scaffold interfaces "out of the box", while at the same time providing
|
|
|
|
* flexibility to customize the default output.
|
|
|
|
*
|
2008-08-09 04:00:40 +02:00
|
|
|
* Add a route (note - this doc is not currently in sync with the code, need to update)
|
2008-08-06 05:28:25 +02:00
|
|
|
* <code>
|
|
|
|
* Director::addRules(50, array('admin/mymodel/$Class/$Action/$ID' => 'MyModelAdmin'));
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @todo saving logic (should mostly use Form->saveInto() and iterate over relations)
|
|
|
|
* @todo ajax form loading and saving
|
|
|
|
* @todo ajax result display
|
|
|
|
* @todo relation formfield scaffolding (one tab per relation) - relations don't have DBField sublclasses, we do
|
|
|
|
* we define the scaffold defaults. can be ComplexTableField instances for a start.
|
|
|
|
* @todo has_many/many_many relation autocomplete field (HasManyComplexTableField doesn't work well with larger datasets)
|
|
|
|
*
|
|
|
|
* Long term TODOs:
|
|
|
|
* @todo Hook into RESTful interface on DataObjects (yet to be developed)
|
|
|
|
* @todo Permission control via datamodel and Form class
|
|
|
|
*
|
|
|
|
* @uses {@link SearchContext}
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
*/
|
|
|
|
abstract class ModelAdmin extends LeftAndMain {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of all managed {@link DataObject}s in this interface.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $managed_models = null;
|
|
|
|
|
|
|
|
public static $allowed_actions = array(
|
|
|
|
'add',
|
|
|
|
'edit',
|
2008-08-09 05:54:55 +02:00
|
|
|
'delete',
|
|
|
|
'handleList',
|
|
|
|
'handleItem'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forward control to the default action handler
|
|
|
|
*/
|
|
|
|
public static $url_handlers = array(
|
|
|
|
'$Action' => 'handleAction'
|
2008-08-06 05:28:25 +02:00
|
|
|
);
|
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
|
|
|
* Model object currently in manipulation queue. Used for updating Link to point
|
|
|
|
* to the correct generic data object in generated URLs.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $currentModel = false;
|
2008-08-09 05:54:55 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Initialize the model admin interface. Sets up embedded jquery libraries and requisite plugins.
|
|
|
|
*
|
|
|
|
* @todo remove reliance on urlParams
|
2008-08-09 04:16:46 +02:00
|
|
|
*/
|
2008-08-06 05:28:25 +02:00
|
|
|
public function init() {
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
// security check for valid models
|
2008-08-09 05:54:55 +02:00
|
|
|
if(isset($this->urlParams['Action']) && !in_array($this->urlParams['Action'], $this->getManagedModels())) {
|
2008-08-06 05:28:25 +02:00
|
|
|
user_error('ModelAdmin::init(): Invalid Model class', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
Requirements::css('cms/css/ModelAdmin.css'); // standard layout formatting for management UI
|
|
|
|
Requirements::css('cms/css/silverstripe.tabs.css'); // follows the jQuery UI theme conventions
|
|
|
|
|
|
|
|
Requirements::javascript('jsparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/livequery/jquery.livequery.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/ui/ui/ui.base.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/ui/ui/ui.tabs.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/ui/plugins/form/jquery.form.js');
|
|
|
|
Requirements::javascript('cms/javascript/ModelAdmin.js');
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
|
|
|
* Add mappings for generic form constructors to automatically delegate to a scaffolded form object.
|
|
|
|
*/
|
|
|
|
function defineMethods() {
|
|
|
|
parent::defineMethods();
|
2008-08-09 05:54:55 +02:00
|
|
|
foreach($this->getManagedModels() as $ClassName) {
|
|
|
|
$this->addWrapperMethod($ClassName, 'bindModelController');
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2008-08-09 04:16:46 +02:00
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Return default link to this controller. This assfaced method is abstract, so we can't
|
|
|
|
* get rid of it.
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
2008-08-09 04:16:46 +02:00
|
|
|
* @todo extract full URL binding from Director::addRules so that it's not tied to 'admin/crm'
|
|
|
|
* @todo is there a way of removing the dependency on this method from the Form?
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link() {
|
2008-08-09 05:54:55 +02:00
|
|
|
return Controller::join_links(Director::absoluteBaseURL(), 'admin/crm/');
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Base scaffolding method for returning a generic model instance.
|
2008-08-06 05:28:25 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
public function bindModelController($model, $request = null) {
|
|
|
|
return new ModelAdmin_CollectionController($this, $model);
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to choose which record needs to be created.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
protected function ManagedModelsSelect() {
|
2008-08-06 05:28:25 +02:00
|
|
|
$models = $this->getManagedModels();
|
|
|
|
$modelMap = array();
|
|
|
|
foreach($models as $modelName) $modelMap[$modelName] = singleton($modelName)->singular_name();
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
2008-08-09 05:54:55 +02:00
|
|
|
"ManagedModelsSelect",
|
2008-08-06 05:28:25 +02:00
|
|
|
new FieldSet(
|
2008-08-09 04:00:40 +02:00
|
|
|
new DropdownField('ClassName', 'Type', $modelMap)
|
2008-08-06 05:28:25 +02:00
|
|
|
),
|
|
|
|
new FieldSet(
|
|
|
|
new FormAction('add', _t('GenericDataAdmin.CREATE'))
|
|
|
|
)
|
|
|
|
);
|
2008-08-09 04:16:46 +02:00
|
|
|
$form->setFormMethod('get');
|
2008-08-06 05:28:25 +02:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
function add($data, $form, $request) {
|
|
|
|
$className = $request->requestVar("ClassName");
|
|
|
|
return $this->$className()->add($request);
|
|
|
|
}
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @uses {@link SearchContext}
|
|
|
|
* @uses {@link SearchFilter}
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
protected function getSearchForms() {
|
|
|
|
$modelClasses = $this->getManagedModels();
|
|
|
|
|
|
|
|
$forms = new DataObjectSet();
|
|
|
|
foreach($modelClasses as $modelClass) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->$modelClass()->SearchForm();
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
$forms->push(new ArrayData(array(
|
2008-08-09 05:54:55 +02:00
|
|
|
'Form' => $this->$modelClass()->SearchForm(),
|
|
|
|
'Title' => singleton($modelClass)->singular_name(),
|
|
|
|
'ClassName' => $modelClass,
|
2008-08-06 05:28:25 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $forms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getManagedModels() {
|
|
|
|
$models = $this->stat('managed_models');
|
|
|
|
if(!count($models)) user_error('ModelAdmin::getManagedModels():
|
|
|
|
You need to specify at least one DataObject subclass in $managed_models', E_USER_ERROR);
|
|
|
|
|
|
|
|
return $models;
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a managed model class and provides default collection filtering behavior.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ModelAdmin_CollectionController extends Controller {
|
|
|
|
protected $parentController;
|
|
|
|
protected $modelClass;
|
|
|
|
|
|
|
|
static $url_handlers = array(
|
|
|
|
'$Action' => 'handleActionOrID'
|
|
|
|
);
|
|
|
|
|
|
|
|
function __construct($parent, $model) {
|
|
|
|
$this->parentController = $parent;
|
|
|
|
$this->modelClass = $model;
|
|
|
|
}
|
2008-08-06 05:28:25 +02:00
|
|
|
|
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Appends the model class to the URL.
|
|
|
|
*
|
|
|
|
* @return unknown
|
2008-08-06 05:28:25 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function Link() {
|
|
|
|
return Controller::join_links($this->parentController->Link(), "$this->modelClass");
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Return the class name of the model being managed.
|
2008-08-09 04:16:46 +02:00
|
|
|
*
|
2008-08-09 05:54:55 +02:00
|
|
|
* @return unknown
|
2008-08-09 04:16:46 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function getModelClass() {
|
|
|
|
return $this->modelClass;
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Delegate to different control flow, depending on whether the
|
|
|
|
* URL parameter is a numeric type (record id) or string (action).
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
2008-08-09 05:54:55 +02:00
|
|
|
* @param unknown_type $request
|
|
|
|
* @return unknown
|
2008-08-06 05:28:25 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function handleActionOrID($request) {
|
|
|
|
if (is_numeric($request->param('Action'))) {
|
|
|
|
return $this->handleID($request);
|
|
|
|
} else {
|
|
|
|
return $this->handleAction($request);
|
|
|
|
}
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to the RecordController if a valid numeric ID appears in the URL
|
|
|
|
* segment.
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return RecordController
|
|
|
|
*/
|
|
|
|
function handleID($request) {
|
|
|
|
return new ModelAdmin_RecordController($this, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
|
|
|
* Get a search form for a single {@link DataObject} subclass.
|
|
|
|
*
|
2008-08-09 05:54:55 +02:00
|
|
|
* @return Form
|
2008-08-06 05:28:25 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
public function SearchForm() {
|
|
|
|
$context = singleton($this->modelClass)->getDefaultSearchContext();
|
2008-08-06 05:28:25 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
$form = new Form($this, "SearchForm",
|
2008-08-06 05:28:25 +02:00
|
|
|
$context->getSearchFields(),
|
|
|
|
new FieldSet(
|
|
|
|
new FormAction('search', _t('MemberTableField.SEARCH'))
|
|
|
|
)
|
|
|
|
);
|
2008-08-09 05:54:55 +02:00
|
|
|
$form->setFormAction(Controller::join_links($this->Link(), "search"));
|
2008-08-09 04:16:46 +02:00
|
|
|
$form->setFormMethod('get');
|
2008-08-09 05:54:55 +02:00
|
|
|
$form->setHTMLID("Form_SearchForm_" . $this->modelClass);
|
2008-08-06 05:28:25 +02:00
|
|
|
return $form;
|
|
|
|
}
|
2008-08-06 05:43:48 +02:00
|
|
|
|
|
|
|
/**
|
2008-08-09 04:00:40 +02:00
|
|
|
* Action to render a data object collection, using the model context to provide filters
|
|
|
|
* and paging.
|
2008-08-09 05:54:55 +02:00
|
|
|
*
|
|
|
|
* @todo push this HTML structure out into separate template
|
2008-08-06 05:43:48 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function search($request) {
|
|
|
|
$model = singleton($this->modelClass);
|
2008-08-09 06:06:52 +02:00
|
|
|
$searchKeys = array_intersect_key($request->getVars(), $model->searchable_fields());
|
2008-08-09 05:54:55 +02:00
|
|
|
$context = $model->getDefaultSearchContext();
|
|
|
|
$results = $context->getResultSet($searchKeys);
|
|
|
|
$output = "";
|
|
|
|
if ($results) {
|
|
|
|
$output .= "<table>";
|
|
|
|
foreach($results as $row) {
|
|
|
|
$uri = Director::absoluteBaseUrl();
|
|
|
|
$output .= "<tr title=\"{$uri}admin/crm/{$this->modelClass}/{$row->ID}/edit\">";
|
2008-08-09 06:06:52 +02:00
|
|
|
foreach($model->searchable_fields() as $key=>$val) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$output .= "<td>";
|
|
|
|
$output .= $row->getField($key);
|
|
|
|
$output .= "</td>";
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
$output .= "</tr>";
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
$output .= "</table>";
|
|
|
|
} else {
|
|
|
|
$output .= "<p>No results found</p>";
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new model record.
|
|
|
|
*
|
|
|
|
* @param unknown_type $request
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function add($request) {
|
|
|
|
return $this->AddForm()->forTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form for editing the attached model
|
|
|
|
*/
|
|
|
|
public function AddForm() {
|
|
|
|
$newRecord = new $this->modelClass();
|
|
|
|
$fields = $newRecord->getCMSFields();
|
|
|
|
|
|
|
|
$validator = ($newRecord->hasMethod('getCMSValidator')) ? $newRecord->getCMSValidator() : null;
|
|
|
|
|
|
|
|
$actions = new FieldSet(new FormAction("doCreate", "Add"));
|
|
|
|
|
|
|
|
$form = new Form($this, "AddForm", $fields, $actions, $validator);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doCreate($data, $form, $request) {
|
|
|
|
$className = $this->getModelClass();
|
|
|
|
$model = new $className();
|
|
|
|
$form->saveInto($model);
|
|
|
|
$model->write();
|
|
|
|
|
|
|
|
Director::redirect(Controller::join_links($this->Link(), $model->ID , 'edit'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles operations on a single record from a managed model.
|
|
|
|
*
|
|
|
|
* @todo change the parent controller varname to indicate the model scaffolding functionality in ModelAdmin
|
|
|
|
*/
|
|
|
|
class ModelAdmin_RecordController extends Controller {
|
|
|
|
protected $parentController;
|
|
|
|
protected $currentRecord;
|
|
|
|
|
|
|
|
static $allowed_actions = array('edit', 'view', 'EditForm', 'ViewForm');
|
|
|
|
|
|
|
|
function __construct($parentController, $request) {
|
|
|
|
$this->parentController = $parentController;
|
|
|
|
$modelName = $parentController->getModelClass();
|
|
|
|
$recordID = $request->param('Action');
|
|
|
|
$this->currentRecord = DataObject::get_by_id($modelName, $recordID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link fragment - appends the current record ID to the URL.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function Link() {
|
|
|
|
return Controller::join_links($this->parentController->Link(), "/{$this->currentRecord->ID}");
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2008-08-06 05:43:48 +02:00
|
|
|
|
2008-08-09 04:00:40 +02:00
|
|
|
/**
|
2008-08-09 05:54:55 +02:00
|
|
|
* Edit action - shows a form for editing this record
|
|
|
|
*/
|
|
|
|
function edit($request) {
|
|
|
|
if ($this->currentRecord) {
|
|
|
|
return $this->EditForm()->forTemplate();
|
|
|
|
} else {
|
|
|
|
return "I can't find that item";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form for editing the attached model
|
2008-08-09 04:00:40 +02:00
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
public function EditForm() {
|
|
|
|
$fields = $this->currentRecord->getCMSFields();
|
|
|
|
$fields->push(new HiddenField("ID"));
|
|
|
|
|
|
|
|
$validator = ($this->currentRecord->hasMethod('getCMSValidator')) ? $this->currentRecord->getCMSValidator() : null;
|
|
|
|
|
|
|
|
$actions = new FieldSet(new FormAction("doSave", "Save"));
|
|
|
|
|
|
|
|
$form = new Form($this, "EditForm", $fields, $actions, $validator);
|
|
|
|
$form->loadDataFrom($this->currentRecord);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSave($data, $form, $request) {
|
|
|
|
$this->currentRecord->update($request->postVars());
|
|
|
|
$this->currentRecord->write();
|
2008-08-06 05:43:48 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
// Behaviour switched on ajax.
|
|
|
|
if(Director::is_ajax()) {
|
|
|
|
return $this->edit($request);
|
|
|
|
} else {
|
|
|
|
Director::redirectBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo remove base controller hack and refactor scaffolding methods to be better distributed in class heirachy
|
|
|
|
*/
|
|
|
|
function view($request) {
|
|
|
|
if ($this->currentRecord) {
|
|
|
|
$form = $this->ViewForm();
|
2008-08-09 04:00:40 +02:00
|
|
|
return $form->forTemplate();
|
2008-08-09 05:54:55 +02:00
|
|
|
} else {
|
|
|
|
return "I can't find that item";
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form for viewing the attached model
|
|
|
|
*/
|
|
|
|
public function ViewForm() {
|
|
|
|
$fields = $this->currentRecord->getCMSFields();
|
|
|
|
$form = new Form($this, "EditForm", $fields, new FieldSet());
|
|
|
|
$form->loadDataFrom($this->currentRecord);
|
|
|
|
$form->makeReadonly();
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
2008-08-06 05:43:48 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2008-08-06 05:43:48 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
function index() {
|
|
|
|
Director::redirect(Controller::join_links($this->Link(), 'edit'));
|
|
|
|
}
|
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
?>
|