2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Generates a three-pane UI for editing model classes,
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Add a route (note - this doc is not currently in sync with the code, need to update)
|
|
|
|
* <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 SearchContext
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
abstract class ModelAdmin extends LeftAndMain {
|
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
static $url_rule = '/$ModelClass/$Action';
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of all managed {@link DataObject}s in this interface.
|
|
|
|
*
|
|
|
|
* Simple notation with class names only:
|
|
|
|
* <code>
|
|
|
|
* array('MyObjectClass','MyOtherObjectClass')
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Extended notation with options (e.g. custom titles):
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'MyObjectClass' => array('title' => "Custom title")
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Available options:
|
|
|
|
* - 'title': Set custom titles for the tabs or dropdown names
|
|
|
|
*
|
|
|
|
* @var array|string
|
|
|
|
*/
|
|
|
|
public static $managed_models = null;
|
|
|
|
|
|
|
|
public static $allowed_actions = array(
|
2012-03-09 09:52:03 +01:00
|
|
|
'ImportForm',
|
|
|
|
'SearchForm',
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public static $url_handlers = array(
|
2012-03-09 09:52:03 +01:00
|
|
|
'$ModelClass/$Action' => 'handleAction'
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2012-03-09 09:52:03 +01:00
|
|
|
* @var String
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
protected $modelClass;
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change this variable if you don't want the Import from CSV form to appear.
|
|
|
|
* This variable can be a boolean or an array.
|
|
|
|
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClasstwo')
|
|
|
|
*/
|
|
|
|
public $showImportForm = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* e.g. "BlogEntry" => "BlogEntryCsvBulkLoader"
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $model_importers = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Amount of results showing on a single page.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $page_length = 30;
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Initialize the model admin interface. Sets up embedded jquery libraries and requisite plugins.
|
|
|
|
*
|
|
|
|
* @todo remove reliance on urlParams
|
|
|
|
*/
|
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$models = $this->getManagedModels();
|
|
|
|
$this->modelClass = (isset($this->urlParams['ModelClass'])) ? $this->urlParams['ModelClass'] : $models[0];
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// security check for valid models
|
2012-03-09 09:52:03 +01:00
|
|
|
if(!in_array($this->modelClass, $models)) {
|
|
|
|
user_error('ModelAdmin::init(): Invalid Model class', E_USER_ERROR);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2011-03-23 11:07:31 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_ADMIN_DIR . '/javascript/ModelAdmin.js');
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
function getEditForm($id = null) {
|
|
|
|
$list = $this->getList();
|
|
|
|
$listField = Object::create('GridField',
|
|
|
|
$this->modelClass,
|
|
|
|
false,
|
|
|
|
$list,
|
|
|
|
$fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
|
|
|
|
->addComponent(new GridFieldExportButton())
|
|
|
|
->removeComponentsByType('GridFieldFilter')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
if(singleton($this->modelClass)->hasMethod('getCMSValidator')) {
|
|
|
|
$detailValidator = singleton($this->modelClass)->getCMSValidator();
|
|
|
|
$listField->getConfig()->getComponentByType('GridFieldDetailForm')->setValidator($detailValidator);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'EditForm',
|
|
|
|
new FieldList($listField),
|
|
|
|
new FieldList()
|
|
|
|
);
|
|
|
|
$form->addExtraClass('cms-edit-form cms-panel-padded center');
|
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
|
|
|
$form->setFormAction(Controller::join_links($this->Link($this->modelClass), 'EditForm'));
|
|
|
|
|
|
|
|
$this->extend('updateEditForm', $form);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
return $form;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2012-03-09 09:52:03 +01:00
|
|
|
* @return SearchContext
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
public function getSearchContext() {
|
|
|
|
$context = singleton($this->modelClass)->getDefaultSearchContext();
|
|
|
|
|
|
|
|
// Namespace fields, for easier detection if a search is present
|
|
|
|
foreach($context->getFields() as $field) $field->setName(sprintf('q[%s]', $field->getName()));
|
|
|
|
foreach($context->getFilters() as $filter) $filter->setFullName(sprintf('q[%s]', $filter->getFullName()));
|
|
|
|
|
|
|
|
$this->extend('updateSearchContext', $context);
|
|
|
|
|
|
|
|
return $context;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2012-03-09 09:52:03 +01:00
|
|
|
* @return Form
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
public function SearchForm() {
|
|
|
|
$context = $this->getSearchContext();
|
|
|
|
$form = new Form($this, "SearchForm",
|
|
|
|
$context->getSearchFields(),
|
|
|
|
new FieldList(
|
|
|
|
Object::create('ResetFormAction','clearsearch', _t('ModelAdmin.CLEAR_SEARCH','Clear Search'))
|
|
|
|
->setUseButtonTag(true)->addExtraClass('ss-ui-action-minor'),
|
|
|
|
Object::create('FormAction', 'search', _t('MemberTableField.SEARCH', 'Search'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
),
|
|
|
|
new RequiredFields()
|
|
|
|
);
|
|
|
|
$form->setFormMethod('get');
|
|
|
|
$form->setFormAction($this->Link($this->modelClass));
|
|
|
|
$form->addExtraClass('cms-search-form');
|
|
|
|
$form->disableSecurityToken();
|
|
|
|
$form->loadDataFrom($this->request->getVars());
|
|
|
|
|
|
|
|
$this->extend('updateSearchForm', $form);
|
|
|
|
|
|
|
|
return $form;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
public function getList() {
|
|
|
|
$context = $this->getSearchContext();
|
|
|
|
$params = $this->request->requestVar('q');
|
|
|
|
$list = $context->getResults($params);
|
|
|
|
|
|
|
|
$this->extend('updateList', $list);
|
|
|
|
|
|
|
|
return $list;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns managed models' create, search, and import forms
|
|
|
|
* @uses SearchContext
|
|
|
|
* @uses SearchFilter
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List of forms
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
protected function getManagedModelTabs() {
|
2011-03-23 10:51:00 +01:00
|
|
|
$models = $this->getManagedModels();
|
2011-05-05 12:40:24 +02:00
|
|
|
$forms = new ArrayList();
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
foreach($models as $class => $options) {
|
|
|
|
if(is_numeric($class)) $class = $options;
|
|
|
|
$forms->push(new ArrayData(array (
|
|
|
|
'Title' => (is_array($options) && isset($options['title'])) ? $options['title'] : singleton($class)->i18n_singular_name(),
|
|
|
|
'ClassName' => $class,
|
2012-03-09 09:52:03 +01:00
|
|
|
'Link' => $this->Link($class),
|
|
|
|
'LinkOrCurrent' => ($class == $this->modelClass) ? 'current' : 'link'
|
2011-03-23 10:51:00 +01:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $forms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getManagedModels() {
|
|
|
|
$models = $this->stat('managed_models');
|
|
|
|
if(is_string($models)) {
|
|
|
|
$models = array($models);
|
|
|
|
}
|
|
|
|
if(!count($models)) {
|
|
|
|
user_error(
|
|
|
|
'ModelAdmin::getManagedModels():
|
|
|
|
You need to specify at least one DataObject subclass in public static $managed_models.
|
|
|
|
Make sure that this property is defined, and that its visibility is set to "public"',
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $models;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2012-03-09 15:33:24 +01:00
|
|
|
* @return array Map of model class names to importer instances
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
function getModelImporters() {
|
2012-03-09 15:33:24 +01:00
|
|
|
$importerClasses = $this->stat('model_importers');
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// fallback to all defined models if not explicitly defined
|
2012-03-09 15:33:24 +01:00
|
|
|
if(is_null($importerClasses)) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$models = $this->getManagedModels();
|
|
|
|
foreach($models as $modelName => $options) {
|
|
|
|
if(is_numeric($modelName)) $modelName = $options;
|
2012-03-09 15:33:24 +01:00
|
|
|
$importerClasses[$modelName] = 'CsvBulkLoader';
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 15:33:24 +01:00
|
|
|
|
|
|
|
$importers = array();
|
|
|
|
foreach($importerClasses as $modelClass => $importerClass) {
|
|
|
|
$importers[$modelClass] = new $importerClass($modelClass);
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
return $importers;
|
|
|
|
}
|
2012-03-09 00:45:18 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Generate a CSV import form for a single {@link DataObject} subclass.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function ImportForm() {
|
|
|
|
$modelName = $this->modelClass;
|
|
|
|
// check if a import form should be generated
|
2012-03-09 09:52:03 +01:00
|
|
|
if(!$this->showImportForm || (is_array($this->showImportForm) && !in_array($modelName,$this->showImportForm))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$importers = $this->getModelImporters();
|
2011-03-23 10:51:00 +01:00
|
|
|
if(!$importers || !isset($importers[$modelName])) return false;
|
|
|
|
|
|
|
|
if(!singleton($modelName)->canCreate(Member::currentUser())) return false;
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2011-03-23 10:51:00 +01:00
|
|
|
new HiddenField('ClassName', _t('ModelAdmin.CLASSTYPE'), $modelName),
|
|
|
|
new FileField('_CsvFile', false)
|
|
|
|
);
|
|
|
|
|
|
|
|
// get HTML specification for each import (column names etc.)
|
|
|
|
$importerClass = $importers[$modelName];
|
|
|
|
$importer = new $importerClass($modelName);
|
|
|
|
$spec = $importer->getImportSpec();
|
2011-05-05 12:40:24 +02:00
|
|
|
$specFields = new ArrayList();
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($spec['fields'] as $name => $desc) {
|
|
|
|
$specFields->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
|
|
|
|
}
|
2011-05-05 12:40:24 +02:00
|
|
|
$specRelations = new ArrayList();
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($spec['relations'] as $name => $desc) {
|
|
|
|
$specRelations->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
|
|
|
|
}
|
|
|
|
$specHTML = $this->customise(array(
|
|
|
|
'ModelName' => Convert::raw2att($modelName),
|
|
|
|
'Fields' => $specFields,
|
|
|
|
'Relations' => $specRelations,
|
|
|
|
))->renderWith('ModelAdmin_ImportSpec');
|
|
|
|
|
|
|
|
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
|
|
|
|
$fields->push(new CheckboxField('EmptyBeforeImport', 'Clear Database before import', false));
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$actions = new FieldList(
|
2011-03-23 10:51:00 +01:00
|
|
|
new FormAction('import', _t('ModelAdmin.IMPORT', 'Import from CSV'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
"ImportForm",
|
|
|
|
$fields,
|
2012-03-09 00:12:33 +01:00
|
|
|
$actions
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-03-09 09:52:03 +01:00
|
|
|
$form->setFormAction(Controller::join_links($this->Link($this->modelClass), 'ImportForm'));
|
|
|
|
|
|
|
|
$this->extend('updateImportForm', $form);
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
*/
|
|
|
|
function import($data, $form, $request) {
|
2012-03-09 09:52:03 +01:00
|
|
|
if(!$this->showImportForm || (is_array($this->showImportForm) && !in_array($this->modelClass,$this->showImportForm))) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
$importers = $this->getModelImporters();
|
2012-03-09 15:33:24 +01:00
|
|
|
$loader = $importers[$this->modelClass];
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// File wasn't properly uploaded, show a reminder to the user
|
|
|
|
if(
|
|
|
|
empty($_FILES['_CsvFile']['tmp_name']) ||
|
|
|
|
file_get_contents($_FILES['_CsvFile']['tmp_name']) == ''
|
|
|
|
) {
|
|
|
|
$form->sessionMessage(_t('ModelAdmin.NOCSVFILE', 'Please browse for a CSV file to import'), 'good');
|
|
|
|
$this->redirectBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import
|
|
|
|
$loader->deleteExistingRecords = true;
|
|
|
|
}
|
|
|
|
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
|
|
|
|
|
|
|
|
$message = '';
|
|
|
|
if($results->CreatedCount()) $message .= sprintf(
|
|
|
|
_t('ModelAdmin.IMPORTEDRECORDS', "Imported %s records."),
|
|
|
|
$results->CreatedCount()
|
|
|
|
);
|
|
|
|
if($results->UpdatedCount()) $message .= sprintf(
|
|
|
|
_t('ModelAdmin.UPDATEDRECORDS', "Updated %s records."),
|
|
|
|
$results->UpdatedCount()
|
|
|
|
);
|
|
|
|
if($results->DeletedCount()) $message .= sprintf(
|
|
|
|
_t('ModelAdmin.DELETEDRECORDS', "Deleted %s records."),
|
|
|
|
$results->DeletedCount()
|
|
|
|
);
|
|
|
|
if(!$results->CreatedCount() && !$results->UpdatedCount()) $message .= _t('ModelAdmin.NOIMPORT', "Nothing to import");
|
|
|
|
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
$this->redirectBack();
|
|
|
|
}
|
|
|
|
|
2012-03-08 08:28:52 +01:00
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
public function Breadcrumbs($unlinked = false) {
|
|
|
|
$items = parent::Breadcrumbs($unlinked);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
// Show the class name rather than ModelAdmin title as root node
|
|
|
|
$models = $this->getManagedModels();
|
|
|
|
$modelSpec = ArrayLib::is_associative($models) ? $models[$this->modelClass] : null;
|
|
|
|
if(is_array($modelSpec) && isset($modelSpec['title'])) {
|
|
|
|
$items[0]->Title = $modelSpec['title'];
|
2011-03-23 10:51:00 +01:00
|
|
|
} else {
|
2012-03-09 09:52:03 +01:00
|
|
|
$items[0]->Title = singleton($this->modelClass)->i18n_singular_name();
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
return $items;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-09 09:52:03 +01:00
|
|
|
* overwrite the static page_length of the admin panel,
|
|
|
|
* should be called in the project _config file.
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
static function set_page_length($length){
|
|
|
|
self::$page_length = $length;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-09 09:52:03 +01:00
|
|
|
* Return the static page_length of the admin, default as 30
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
static function get_page_length(){
|
|
|
|
return self::$page_length;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
}
|