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',
|
2008-08-09 07:00:42 +02:00
|
|
|
'import',
|
|
|
|
'renderimportform',
|
2008-08-09 05:54:55 +02:00
|
|
|
'handleList',
|
2008-08-11 02:03:57 +02:00
|
|
|
'handleItem',
|
|
|
|
'ImportForm'
|
2008-08-09 05:54:55 +02:00
|
|
|
);
|
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
|
|
|
* @param string $collection_controller_class Override for controller class
|
|
|
|
*/
|
|
|
|
protected static $collection_controller_class = "ModelAdmin_CollectionController";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $collection_controller_class Override for controller class
|
|
|
|
*/
|
|
|
|
protected static $record_controller_class = "ModelAdmin_RecordController";
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
|
|
|
* 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 07:00:42 +02:00
|
|
|
/**
|
|
|
|
* List of all {@link DataObject}s which can be imported through
|
|
|
|
* a subclass of {@link BulkLoader} (mostly CSV data).
|
|
|
|
* By default {@link CsvBulkLoader} is used, assuming a standard mapping
|
|
|
|
* of column names to {@link DataObject} properties/relations.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $model_importers = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Amount of results showing on a single page.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected static $page_length = 30;
|
|
|
|
|
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-09 07:00:42 +02:00
|
|
|
//user_error('ModelAdmin::init(): Invalid Model class', E_USER_ERROR);
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
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');
|
2008-08-09 06:53:34 +02:00
|
|
|
Requirements::javascript('jsparty/jquery/plugins/livequery/jquery.livequery.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/ui/ui.core.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/ui/ui.tabs.js');
|
|
|
|
Requirements::javascript('jsparty/jquery/plugins/form/jquery.form.js');
|
2008-09-13 08:19:17 +02:00
|
|
|
Requirements::javascript('jsparty/jquery/plugins/effen/jquery.fn.js');
|
2008-08-11 08:02:58 +02:00
|
|
|
Requirements::javascript('jsparty/jquery/jquery_improvements.js');
|
2008-08-09 04:16:46 +02:00
|
|
|
Requirements::javascript('cms/javascript/ModelAdmin.js');
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* overwrite the static page_length of the admin panel,
|
|
|
|
* should be called in the project _config file.
|
|
|
|
*/
|
|
|
|
static function set_page_length($length){
|
|
|
|
self::$page_length = $length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the static page_length of the admin, default as 30
|
|
|
|
*/
|
|
|
|
static function get_page_length(){
|
|
|
|
return self::$page_length;
|
|
|
|
}
|
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) {
|
2008-08-09 09:03:24 +02:00
|
|
|
$class = $this->stat('collection_controller_class');
|
|
|
|
return new $class($this, $model);
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
|
|
|
|
2008-09-13 06:45:30 +02:00
|
|
|
/**
|
|
|
|
* This method can be overloaded to specify the UI by which the search class is chosen.
|
|
|
|
*
|
|
|
|
* It can create a tab strip or a dropdown. The dropdown is useful when there are a large number of classes.
|
|
|
|
* By default, it will show a tabs for 1-3 classes, and a dropdown for 4 or more classes.
|
|
|
|
*
|
|
|
|
* @return String: 'tabs' or 'dropdown'
|
|
|
|
*/
|
|
|
|
public function SearchClassSelector() {
|
|
|
|
return sizeof($this->getManagedModels()) > 3 ? 'dropdown' : 'tabs';
|
|
|
|
}
|
|
|
|
|
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();
|
2008-08-11 02:14:48 +02:00
|
|
|
foreach($models as $modelName) {
|
2008-08-27 03:56:37 +02:00
|
|
|
if ($this->hasMethod('alternatePermissionCheck')) {
|
|
|
|
if ($this->alternatePermissionCheck()) {
|
|
|
|
$modelMap[$modelName] = singleton($modelName)->singular_name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(singleton($modelName)->canCreate(Member::currentUser())) $modelMap[$modelName] = singleton($modelName)->singular_name();
|
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
}
|
2008-08-06 05:28:25 +02:00
|
|
|
|
|
|
|
$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 07:00:42 +02:00
|
|
|
/**
|
|
|
|
* Generate a CSV import form with an option to select
|
|
|
|
* one of the "importable" models specified through {@link self::$model_importers}.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function ImportForm() {
|
|
|
|
$models = $this->getManagedModels();
|
|
|
|
$modelMap = array();
|
2008-08-11 01:29:30 +02:00
|
|
|
$importers = $this->getModelImporters();
|
|
|
|
if(!$importers) return false;
|
|
|
|
|
|
|
|
foreach($importers as $modelName => $importerClass) {
|
|
|
|
$modelMap[$modelName] = singleton($modelName)->singular_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = new FieldSet(
|
|
|
|
new DropdownField('ClassName', 'Type', $modelMap),
|
|
|
|
new FileField('_CsvFile', false)
|
|
|
|
);
|
|
|
|
|
|
|
|
// get HTML specification for each import (column names etc.)
|
|
|
|
foreach($importers as $modelName => $importerClass) {
|
|
|
|
$importer = new $importerClass($modelName);
|
|
|
|
$spec = $importer->getImportSpec();
|
|
|
|
$specFields = new DataObjectSet();
|
|
|
|
foreach($spec['fields'] as $name => $desc) {
|
|
|
|
$specFields->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
|
|
|
|
}
|
|
|
|
$specRelations = new DataObjectSet();
|
|
|
|
foreach($spec['relations'] as $name => $desc) {
|
|
|
|
$specRelations->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
|
|
|
|
}
|
|
|
|
$specHTML = $this->customise(array(
|
|
|
|
'ModelName' => $modelName,
|
|
|
|
'Fields' => $specFields,
|
|
|
|
'Relations' => $specRelations,
|
|
|
|
))->renderWith('ModelAdmin_ImportSpec');
|
|
|
|
|
|
|
|
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
|
|
|
|
}
|
|
|
|
|
|
|
|
$actions = new FieldSet(
|
|
|
|
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
|
|
|
|
);
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
"ImportForm",
|
2008-08-11 01:29:30 +02:00
|
|
|
$fields,
|
|
|
|
$actions
|
2008-08-09 07:00:42 +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-09 07:00:42 +02:00
|
|
|
/**
|
|
|
|
* Imports the submitted CSV file based on specifications given in
|
|
|
|
* {@link self::model_importers}.
|
|
|
|
* Redirects back with a success/failure message.
|
|
|
|
*
|
|
|
|
* @todo Figure out ajax submission of files via jQuery.form plugin
|
|
|
|
*
|
|
|
|
* @param unknown_type $data
|
|
|
|
* @param unknown_type $form
|
|
|
|
* @param unknown_type $request
|
|
|
|
*/
|
|
|
|
function import($data, $form, $request) {
|
2008-08-11 01:29:30 +02:00
|
|
|
$importers = $this->getModelImporters();
|
|
|
|
$importerClass = $importers[$data['ClassName']];
|
|
|
|
|
|
|
|
$loader = new $importerClass($data['ClassName']);
|
2008-08-20 04:41:07 +02:00
|
|
|
$resultsCount = $loader->load($_FILES['_CsvFile']['tmp_name']);
|
|
|
|
//$resultsCount = ($results) ? $results->Count() : 0;
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
Session::setFormMessage('Form_ImportForm', "Loaded {$resultsCount} items", 'good');
|
|
|
|
Director::redirect($_SERVER['HTTP_REFERER'] . '#Form_ImportForm_holder');
|
|
|
|
}
|
|
|
|
|
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 07:00:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all importers defined in {@link self::$model_importers}.
|
|
|
|
* If none are defined, we fall back to {@link self::managed_models}
|
|
|
|
* with a default {@link CsvBulkLoader} class. In this case the column names of the first row
|
|
|
|
* in the CSV file are assumed to have direct mappings to properties on the object.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getModelImporters() {
|
|
|
|
$importers = $this->stat('model_importers');
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
// fallback to all defined models if not explicitly defined
|
2008-08-11 01:29:30 +02:00
|
|
|
if(is_null($importers)) {
|
2008-08-09 07:00:42 +02:00
|
|
|
$models = $this->getManagedModels();
|
|
|
|
foreach($models as $modelName) $importers[$modelName] = 'CsvBulkLoader';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $importers;
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a managed model class and provides default collection filtering behavior.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ModelAdmin_CollectionController extends Controller {
|
2008-08-11 02:21:44 +02:00
|
|
|
public $parentController;
|
2008-08-09 05:54:55 +02:00
|
|
|
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
|
2008-08-09 06:53:34 +02:00
|
|
|
* URL parameter is a number (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) {
|
2008-08-09 09:03:24 +02:00
|
|
|
$class = $this->parentController->stat('record_controller_class');
|
|
|
|
return new $class($this, $request);
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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-11 02:03:57 +02:00
|
|
|
$fields = $context->getSearchFields();
|
|
|
|
$columnSelectionField = $this->ColumnSelectionField();
|
|
|
|
$fields->push($columnSelectionField);
|
2008-08-06 05:28:25 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
$form = new Form($this, "SearchForm",
|
2008-08-11 02:03:57 +02:00
|
|
|
$fields,
|
2008-08-06 05:28:25 +02:00
|
|
|
new FieldSet(
|
2008-08-11 02:21:44 +02:00
|
|
|
new FormAction('search', _t('MemberTableField.SEARCH')),
|
|
|
|
$clearAction = new ResetFormAction('clearsearch', _t('ModelAdmin.CLEAR_SEARCH','Clear Search'))
|
2008-08-06 05:28:25 +02:00
|
|
|
)
|
|
|
|
);
|
2008-08-11 05:03:52 +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-11 02:21:44 +02:00
|
|
|
$clearAction->useButtonTag = true;
|
|
|
|
$clearAction->addExtraClass('minorAction');
|
2008-08-06 05:28:25 +02:00
|
|
|
return $form;
|
|
|
|
}
|
2008-08-11 02:03:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Give the flexibilility to show variouse combination of columns in the search result table
|
|
|
|
*/
|
|
|
|
public function ColumnSelectionField() {
|
|
|
|
$model = singleton($this->modelClass);
|
|
|
|
|
|
|
|
$source = $model->summaryFields();
|
2008-09-13 06:45:30 +02:00
|
|
|
$value = array();
|
2008-08-14 05:35:17 +02:00
|
|
|
if($source) foreach ($source as $fieldName => $label){
|
2008-08-11 02:03:57 +02:00
|
|
|
$value[] = $fieldName;
|
|
|
|
}
|
2008-08-29 00:57:18 +02:00
|
|
|
|
|
|
|
// Reorder the source so that you read items down the column and then across
|
|
|
|
$columnisedSource = array();
|
|
|
|
$keys = array_keys($source);
|
|
|
|
$midPoint = ceil(sizeof($source)/2);
|
|
|
|
for($i=0;$i<$midPoint;$i++) {
|
|
|
|
$key1 = $keys[$i];
|
|
|
|
$columnisedSource[$key1] = $source[$key1];
|
|
|
|
// If there are an odd number of items, the last item will be unset
|
|
|
|
if(isset($keys[$i+$midPoint])) {
|
|
|
|
$key2 = $keys[$i+$midPoint];
|
|
|
|
$columnisedSource[$key2] = $source[$key2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$checkboxes = new CheckboxSetField("ResultAssembly", false, $columnisedSource, $value);
|
2008-08-11 02:03:57 +02:00
|
|
|
|
|
|
|
$field = new CompositeField(
|
2008-08-11 02:21:44 +02:00
|
|
|
new LiteralField(
|
|
|
|
"ToggleResultAssemblyLink",
|
|
|
|
sprintf("<a class=\"form_frontend_function toggle_result_assembly\" href=\"#\">%s</a>",
|
|
|
|
_t('ModelAdmin.CHOOSE_COLUMNS', 'Select result columns...')
|
|
|
|
)
|
|
|
|
),
|
2008-08-11 02:03:57 +02:00
|
|
|
$checkboxesBlock = new CompositeField(
|
|
|
|
$checkboxes,
|
|
|
|
new LiteralField("ClearDiv", "<div class=\"clear\"></div>"),
|
|
|
|
new LiteralField("TickAllAssemblyLink","<a class=\"form_frontend_function tick_all_result_assembly\" href=\"#\">select all</a>"),
|
|
|
|
new LiteralField("UntickAllAssemblyLink","<a class=\"form_frontend_function untick_all_result_assembly\" href=\"#\">select none</a>")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$field -> setExtraClass("ResultAssemblyBlock");
|
|
|
|
$checkboxesBlock -> setExtraClass("hidden");
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2008-08-11 01:29:30 +02:00
|
|
|
* @return string
|
2008-08-06 05:43:48 +02:00
|
|
|
*/
|
2008-08-11 05:03:52 +02:00
|
|
|
function search($request, $form) {
|
2008-09-13 06:45:30 +02:00
|
|
|
// Get the results form to be rendered
|
2008-08-11 05:03:52 +02:00
|
|
|
$resultsForm = $this->ResultsForm($form->getData());
|
2008-09-13 06:45:30 +02:00
|
|
|
// Before rendering, let's get the total number of results returned
|
|
|
|
$tableField = $resultsForm->Fields()->fieldByName($this->modelClass);
|
|
|
|
$numResults = $tableField->TotalCount();
|
|
|
|
|
|
|
|
if($numResults) {
|
|
|
|
return new HTTPResponse($resultsForm->forTemplate(), 200, "Your search found $numResults matching items");
|
|
|
|
} else {
|
|
|
|
return new HTTPResponse($resultsForm->forTemplate(), 404, "Your search didn't return any matching items");
|
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the search query generated on the SearchContext from
|
|
|
|
* {@link DataObject::getDefaultSearchContext()},
|
|
|
|
* and the current GET parameters on the request.
|
|
|
|
*
|
|
|
|
* @return SQLQuery
|
|
|
|
*/
|
2008-08-11 05:03:52 +02:00
|
|
|
function getSearchQuery($searchCriteria) {
|
2008-08-11 01:29:30 +02:00
|
|
|
$context = singleton($this->modelClass)->getDefaultSearchContext();
|
2008-08-11 05:03:52 +02:00
|
|
|
return $context->getQuery($searchCriteria);
|
2008-08-11 01:29:30 +02:00
|
|
|
}
|
|
|
|
|
2008-08-19 12:08:52 +02:00
|
|
|
function getResultColumns($searchCriteria) {
|
2008-08-09 05:54:55 +02:00
|
|
|
$model = singleton($this->modelClass);
|
2008-08-11 01:53:56 +02:00
|
|
|
$summaryFields = $model->summaryFields();
|
2008-08-19 12:08:52 +02:00
|
|
|
|
|
|
|
$resultAssembly = $searchCriteria['ResultAssembly'];
|
2008-08-26 03:54:53 +02:00
|
|
|
if(!is_array($resultAssembly)) {
|
|
|
|
$explodedAssembly = split(' *, *', $resultAssembly);
|
|
|
|
$resultAssembly = array();
|
|
|
|
foreach($explodedAssembly as $item) $resultAssembly[$item] = true;
|
2008-08-11 02:03:57 +02:00
|
|
|
}
|
2008-08-26 03:54:53 +02:00
|
|
|
|
|
|
|
return array_intersect_key($summaryFields, $resultAssembly);
|
2008-08-19 12:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows results from the "search" action in a TableListField.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function ResultsForm($searchCriteria) {
|
|
|
|
if($searchCriteria instanceof HTTPRequest) $searchCriteria = $searchCriteria->getVars();
|
|
|
|
$summaryFields = $this->getResultColumns($searchCriteria);
|
|
|
|
|
2008-08-09 08:29:50 +02:00
|
|
|
$tf = new TableListField(
|
|
|
|
$this->modelClass,
|
|
|
|
$this->modelClass,
|
|
|
|
$summaryFields
|
|
|
|
);
|
2008-08-11 05:03:52 +02:00
|
|
|
$tf->setCustomQuery($this->getSearchQuery($searchCriteria));
|
2008-08-11 01:29:30 +02:00
|
|
|
$tf->setPageSize($this->parentController->stat('page_length'));
|
2008-08-11 01:17:51 +02:00
|
|
|
$tf->setShowPagination(true);
|
2008-08-14 06:43:15 +02:00
|
|
|
// @todo Remove records that can't be viewed by the current user
|
|
|
|
$tf->setPermissions(array_merge(array('view','export'), TableListField::permissions_for_object($this->modelClass)));
|
2008-08-11 01:17:51 +02:00
|
|
|
$url = '<a href=\"' . $this->Link() . '/$ID/edit\">$value</a>';
|
2008-08-09 08:29:50 +02:00
|
|
|
$tf->setFieldFormatting(array_combine(array_keys($summaryFields), array_fill(0,count($summaryFields), $url)));
|
2008-08-11 01:53:56 +02:00
|
|
|
|
2008-08-09 08:29:50 +02:00
|
|
|
// implemented as a form to enable further actions on the resultset
|
|
|
|
// (serverside sorting, export as CSV, etc)
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'ResultsForm',
|
|
|
|
new FieldSet(
|
|
|
|
new HeaderField(_t('ModelAdmin.SEARCHRESULTS','Search Results'), 2),
|
|
|
|
$tf
|
|
|
|
),
|
|
|
|
new FieldSet()
|
|
|
|
);
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2008-08-26 03:54:53 +02:00
|
|
|
// Include the search criteria on the results form URL, but not dodgy variables like those below
|
|
|
|
$filteredCriteria = $searchCriteria;
|
|
|
|
unset($filteredCriteria['ctf']);
|
|
|
|
unset($filteredCriteria['url']);
|
|
|
|
unset($filteredCriteria['action_search']);
|
|
|
|
$form->setFormAction($this->Link() . '/ResultsForm?' . http_build_query($filteredCriteria));
|
2008-08-11 01:29:30 +02:00
|
|
|
return $form;
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new model record.
|
|
|
|
*
|
|
|
|
* @param unknown_type $request
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function add($request) {
|
2008-09-13 06:45:30 +02:00
|
|
|
return new HTTPResponse($this->AddForm()->forAjaxTemplate(), 200, "Fill out this form to add a $this->modelClass to the database.");
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form for editing the attached model
|
|
|
|
*/
|
|
|
|
public function AddForm() {
|
|
|
|
$newRecord = new $this->modelClass();
|
2008-08-26 02:25:22 +02:00
|
|
|
if($newRecord->hasMethod('getCMSAddFormFields')) {
|
|
|
|
$fields = $newRecord->getCMSAddFormFields();
|
|
|
|
} else {
|
|
|
|
$fields = $newRecord->getCMSFields();
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
$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();
|
2008-08-26 02:25:22 +02:00
|
|
|
// We write before saveInto, since this will let us save has-many and many-many relationships :-)
|
|
|
|
$model->write();
|
2008-08-09 05:54:55 +02:00
|
|
|
$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) {
|
2008-08-11 02:21:44 +02:00
|
|
|
if(Director::is_ajax()) {
|
2008-09-13 06:45:30 +02:00
|
|
|
return new HTTPResponse($this->EditForm()->forAjaxTemplate(), 200, "Loaded '" . $this->currentRecord->Title . "' for editing.");
|
2008-08-11 02:21:44 +02:00
|
|
|
} else {
|
2008-08-26 02:25:22 +02:00
|
|
|
// This is really quite ugly; to fix will require a change in the way that customise() works. :-(
|
2008-09-12 03:50:08 +02:00
|
|
|
return $this->parentController->parentController->customise(array(
|
2008-08-26 02:25:22 +02:00
|
|
|
'Right' => $this->parentController->parentController->customise(array(
|
|
|
|
'EditForm' => $this->EditForm()
|
|
|
|
))->renderWith('ModelAdmin_right')
|
|
|
|
))->renderWith(array('ModelAdmin','LeftAndMain'));
|
|
|
|
return ;
|
2008-08-11 02:21:44 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
} 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;
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
$actions = new FieldSet(
|
|
|
|
new FormAction("doSave", "Save")
|
|
|
|
);
|
2008-08-09 05:54:55 +02:00
|
|
|
|
2008-08-11 02:21:44 +02:00
|
|
|
if($this->currentRecord->canDelete(Member::currentUser())) {
|
|
|
|
$actions->insertFirst($deleteAction = new FormAction('doDelete', 'Delete'));
|
|
|
|
$deleteAction->addExtraClass('delete');
|
|
|
|
}
|
2008-08-19 07:18:11 +02:00
|
|
|
|
|
|
|
$actions->insertFirst(new FormAction("goBack", "Back"));
|
2008-08-11 02:21:44 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
$form = new Form($this, "EditForm", $fields, $actions, $validator);
|
|
|
|
$form->loadDataFrom($this->currentRecord);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
|
|
|
* Postback action to save a record
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-08-09 05:54:55 +02:00
|
|
|
function doSave($data, $form, $request) {
|
2008-08-11 02:14:48 +02:00
|
|
|
$form->saveInto($this->currentRecord);
|
2008-08-09 05:54:55 +02:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 02:21:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the current record
|
|
|
|
*/
|
|
|
|
public function doDelete($data, $form, $request) {
|
|
|
|
if($this->currentRecord->canDelete(Member::currentUser())) {
|
|
|
|
$this->currentRecord->delete();
|
|
|
|
Director::redirect($this->parentController->Link());
|
|
|
|
}
|
|
|
|
else Director::redirectBack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2008-08-09 06:53:34 +02:00
|
|
|
* Renders the record view template.
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return mixed
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
|
|
|
function view($request) {
|
|
|
|
if ($this->currentRecord) {
|
|
|
|
$form = $this->ViewForm();
|
2008-08-09 06:38:44 +02:00
|
|
|
return $form->forAjaxTemplate();
|
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
|
2008-08-09 06:53:34 +02:00
|
|
|
*
|
|
|
|
* @return Form
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
?>
|