2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2013-05-26 01:04:12 +02:00
|
|
|
* 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 {@link DataObject::getCMSFields()}
|
2011-03-23 10:51:00 +01:00
|
|
|
* to scaffold interfaces "out of the box", while at the same time providing
|
|
|
|
* flexibility to customize the default output.
|
|
|
|
*
|
|
|
|
* @uses SearchContext
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-20 12:18:07 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
abstract class ModelAdmin extends LeftAndMain {
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
private 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>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* Extended notation with options (e.g. custom titles):
|
|
|
|
* <code>
|
|
|
|
* array(
|
|
|
|
* 'MyObjectClass' => array('title' => "Custom title")
|
|
|
|
* )
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* Available options:
|
|
|
|
* - 'title': Set custom titles for the tabs or dropdown names
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-03-23 10:51:00 +01:00
|
|
|
* @var array|string
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $managed_models = null;
|
2013-01-29 16:30:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override menu_priority so that ModelAdmin CMSMenu objects
|
|
|
|
* are grouped together directly above the Help menu item.
|
|
|
|
* @var float
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $menu_priority = -0.5;
|
2013-04-09 13:37:05 +02:00
|
|
|
|
|
|
|
private static $menu_icon = 'framework/admin/images/menu-icons/16x16/db.png';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2012-03-09 09:52:03 +01:00
|
|
|
'ImportForm',
|
|
|
|
'SearchForm',
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private 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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Change this variable if you don't want the Import from CSV form to appear.
|
2011-03-23 10:51:00 +01:00
|
|
|
* This variable can be a boolean or an array.
|
2016-10-26 13:35:52 +02:00
|
|
|
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClassTwo')
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
public $showImportForm = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-26 13:35:52 +02:00
|
|
|
/**
|
|
|
|
* Change this variable if you don't want the search 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 $showSearchForm = true;
|
|
|
|
|
2011-03-23 10:51:00 +01: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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* e.g. "BlogEntry" => "BlogEntryCsvBulkLoader"
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-03-23 10:51:00 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $model_importers = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Amount of results showing on a single page.
|
|
|
|
*
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-03-23 10:51:00 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $page_length = 30;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Initialize the model admin interface. Sets up embedded jquery libraries and requisite plugins.
|
|
|
|
*/
|
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$models = $this->getManagedModels();
|
2012-05-27 13:45:42 +02:00
|
|
|
|
2015-04-30 01:04:08 +02:00
|
|
|
if($this->getRequest()->param('ModelClass')) {
|
|
|
|
$this->modelClass = $this->unsanitiseClassName($this->getRequest()->param('ModelClass'));
|
2012-05-27 13:45:42 +02:00
|
|
|
} else {
|
|
|
|
reset($models);
|
|
|
|
$this->modelClass = key($models);
|
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// security check for valid models
|
2012-05-02 22:11:26 +02:00
|
|
|
if(!array_key_exists($this->modelClass, $models)) {
|
2012-03-09 09:52:03 +01:00
|
|
|
user_error('ModelAdmin::init(): Invalid Model class', E_USER_ERROR);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/javascript/ModelAdmin.js');
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
|
2012-05-02 18:04:36 +02:00
|
|
|
public function Link($action = null) {
|
2012-06-29 03:52:33 +02:00
|
|
|
if(!$action) $action = $this->sanitiseClassName($this->modelClass);
|
2012-05-02 18:04:36 +02:00
|
|
|
return parent::Link($action);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getEditForm($id = null, $fields = null) {
|
2012-03-09 09:52:03 +01:00
|
|
|
$list = $this->getList();
|
2015-03-26 02:08:46 +01:00
|
|
|
$exportButton = new GridFieldExportButton('buttons-before-left');
|
2012-03-25 09:01:43 +02:00
|
|
|
$exportButton->setExportColumns($this->getExportFields());
|
2012-04-04 16:59:30 +02:00
|
|
|
$listField = GridField::create(
|
2012-06-29 03:52:33 +02:00
|
|
|
$this->sanitiseClassName($this->modelClass),
|
2012-03-09 09:52:03 +01:00
|
|
|
false,
|
|
|
|
$list,
|
|
|
|
$fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
|
2012-03-25 09:01:43 +02:00
|
|
|
->addComponent($exportButton)
|
2012-03-12 22:35:24 +01:00
|
|
|
->removeComponentsByType('GridFieldFilterHeader')
|
2015-03-26 02:08:46 +01:00
|
|
|
->addComponents(new GridFieldPrintButton('buttons-before-left'))
|
2012-03-09 09:52:03 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$form = CMSForm::create(
|
2012-03-09 09:52:03 +01:00
|
|
|
$this,
|
|
|
|
'EditForm',
|
|
|
|
new FieldList($listField),
|
|
|
|
new FieldList()
|
2013-05-10 15:00:13 +02:00
|
|
|
)->setHTMLID('Form_EditForm');
|
|
|
|
$form->setResponseNegotiator($this->getResponseNegotiator());
|
2012-03-09 09:52:03 +01:00
|
|
|
$form->addExtraClass('cms-edit-form cms-panel-padded center');
|
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
2012-09-26 23:34:00 +02:00
|
|
|
$editFormAction = Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'EditForm');
|
|
|
|
$form->setFormAction($editFormAction);
|
2012-04-18 22:11:40 +02:00
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$this->extend('updateEditForm', $form);
|
2014-08-15 08:53:05 +02: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
|
|
|
|
2012-03-25 09:01:43 +02:00
|
|
|
/**
|
|
|
|
* Define which fields are used in the {@link getEditForm} GridField export.
|
|
|
|
* By default, it uses the summary fields from the model definition.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getExportFields() {
|
|
|
|
return singleton($this->modelClass)->summaryFields();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2016-10-26 13:35:52 +02:00
|
|
|
* @return Form|bool
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-03-09 09:52:03 +01:00
|
|
|
public function SearchForm() {
|
2016-10-26 13:35:52 +02:00
|
|
|
if(!$this->showSearchForm ||
|
|
|
|
(is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm))
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-09 09:52:03 +01:00
|
|
|
$context = $this->getSearchContext();
|
|
|
|
$form = new Form($this, "SearchForm",
|
|
|
|
$context->getSearchFields(),
|
|
|
|
new FieldList(
|
2013-08-03 20:18:06 +02:00
|
|
|
Object::create('FormAction', 'search', _t('MemberTableField.APPLY_FILTER', 'Apply Filter'))
|
2012-04-11 06:00:56 +02:00
|
|
|
->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive'),
|
|
|
|
Object::create('ResetFormAction','clearsearch', _t('ModelAdmin.RESET','Reset'))
|
|
|
|
->setUseButtonTag(true)
|
2012-03-09 09:52:03 +01:00
|
|
|
),
|
|
|
|
new RequiredFields()
|
|
|
|
);
|
|
|
|
$form->setFormMethod('get');
|
2012-06-29 03:52:33 +02:00
|
|
|
$form->setFormAction($this->Link($this->sanitiseClassName($this->modelClass)));
|
2012-03-09 09:52:03 +01:00
|
|
|
$form->addExtraClass('cms-search-form');
|
|
|
|
$form->disableSecurityToken();
|
2015-04-30 01:04:08 +02:00
|
|
|
$form->loadDataFrom($this->getRequest()->getVars());
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$this->extend('updateSearchForm', $form);
|
|
|
|
|
|
|
|
return $form;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
public function getList() {
|
|
|
|
$context = $this->getSearchContext();
|
2015-04-30 01:04:08 +02:00
|
|
|
$params = $this->getRequest()->requestVar('q');
|
2015-02-08 07:25:51 +01:00
|
|
|
|
|
|
|
if(is_array($params)) {
|
2015-09-21 14:30:12 +02:00
|
|
|
$params = ArrayLib::array_map_recursive('trim', $params);
|
2015-02-08 07:25:51 +01:00
|
|
|
|
2016-10-20 14:04:15 +02:00
|
|
|
// Parse all DateFields to handle user input non ISO 8601 dates
|
|
|
|
foreach($context->getFields() as $field) {
|
|
|
|
if($field instanceof DatetimeField && !empty($params[$field->getName()])) {
|
|
|
|
$params[$field->getName()] = date('Y-m-d', strtotime($params[$field->getName()]));
|
|
|
|
}
|
2014-11-11 07:44:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
$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
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Returns managed models' create, search, and import forms
|
|
|
|
* @uses SearchContext
|
|
|
|
* @uses SearchFilter
|
2014-08-15 08:53:05 +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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
foreach($models as $class => $options) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$forms->push(new ArrayData(array (
|
2012-05-02 22:11:26 +02:00
|
|
|
'Title' => $options['title'],
|
2011-03-23 10:51:00 +01:00
|
|
|
'ClassName' => $class,
|
2012-06-29 03:52:33 +02:00
|
|
|
'Link' => $this->Link($this->sanitiseClassName($class)),
|
2012-03-09 09:52:03 +01:00
|
|
|
'LinkOrCurrent' => ($class == $this->modelClass) ? 'current' : 'link'
|
2011-03-23 10:51:00 +01:00
|
|
|
)));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $forms;
|
|
|
|
}
|
2012-06-29 03:52:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitise a model class' name for inclusion in a link
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function sanitiseClassName($class) {
|
|
|
|
return str_replace('\\', '-', $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsanitise a model class' name from a URL param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function unsanitiseClassName($class) {
|
|
|
|
return str_replace('-', '\\', $class);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2012-05-02 22:11:26 +02:00
|
|
|
* @return array Map of class name to an array of 'title' (see {@link $managed_models})
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getManagedModels() {
|
2011-03-23 10:51:00 +01:00
|
|
|
$models = $this->stat('managed_models');
|
|
|
|
if(is_string($models)) {
|
|
|
|
$models = array($models);
|
|
|
|
}
|
|
|
|
if(!count($models)) {
|
|
|
|
user_error(
|
2014-08-15 08:53:05 +02:00
|
|
|
'ModelAdmin::getManagedModels():
|
2011-03-23 10:51:00 +01:00
|
|
|
You need to specify at least one DataObject subclass in public static $managed_models.
|
2014-08-15 08:53:05 +02:00
|
|
|
Make sure that this property is defined, and that its visibility is set to "public"',
|
2011-03-23 10:51:00 +01:00
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
2012-05-02 22:11:26 +02:00
|
|
|
|
|
|
|
// Normalize models to have their model class in array key
|
|
|
|
foreach($models as $k => $v) {
|
|
|
|
if(is_numeric($k)) {
|
2017-05-29 04:02:58 +02:00
|
|
|
$models[$v] = array('title' => singleton($v)->i18n_plural_name());
|
2012-05-02 22:11:26 +02:00
|
|
|
unset($models[$k]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $models;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01: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.
|
|
|
|
*
|
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
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
public 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) {
|
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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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.
|
|
|
|
*
|
2016-10-26 13:35:52 +02:00
|
|
|
* @return Form|bool
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
public function ImportForm() {
|
2015-03-13 14:36:52 +01:00
|
|
|
$modelSNG = singleton($this->modelClass);
|
|
|
|
$modelName = $modelSNG->i18n_singular_name();
|
2011-03-23 10:51:00 +01:00
|
|
|
// check if a import form should be generated
|
2015-03-13 14:36:52 +01:00
|
|
|
if(!$this->showImportForm ||
|
|
|
|
(is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm))
|
|
|
|
) {
|
2012-03-09 09:52:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$importers = $this->getModelImporters();
|
2015-03-13 14:36:52 +01:00
|
|
|
if(!$importers || !isset($importers[$this->modelClass])) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-03-13 14:36:52 +01:00
|
|
|
if(!$modelSNG->canCreate(Member::currentUser())) return false;
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2015-03-13 14:36:52 +01:00
|
|
|
new HiddenField('ClassName', _t('ModelAdmin.CLASSTYPE'), $this->modelClass),
|
2011-03-23 10:51:00 +01:00
|
|
|
new FileField('_CsvFile', false)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// get HTML specification for each import (column names etc.)
|
2015-03-13 14:36:52 +01:00
|
|
|
$importerClass = $importers[$this->modelClass];
|
|
|
|
$importer = new $importerClass($this->modelClass);
|
2011-03-23 10:51:00 +01:00
|
|
|
$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(
|
2015-08-18 16:37:15 +02:00
|
|
|
'ClassName' => $this->sanitiseClassName($this->modelClass),
|
2011-03-23 10:51:00 +01:00
|
|
|
'ModelName' => Convert::raw2att($modelName),
|
|
|
|
'Fields' => $specFields,
|
2014-08-15 08:53:05 +02:00
|
|
|
'Relations' => $specRelations,
|
2011-03-23 10:51:00 +01:00
|
|
|
))->renderWith('ModelAdmin_ImportSpec');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
|
2012-09-26 23:34:00 +02:00
|
|
|
$fields->push(
|
2013-01-19 10:38:55 +01:00
|
|
|
new CheckboxField('EmptyBeforeImport', _t('ModelAdmin.EMPTYBEFOREIMPORT', 'Replace data'),
|
2012-09-26 23:34:00 +02:00
|
|
|
false)
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
|
|
|
|
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'))
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
"ImportForm",
|
|
|
|
$fields,
|
2012-03-09 00:12:33 +01:00
|
|
|
$actions
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
$form->setFormAction(
|
|
|
|
Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'ImportForm')
|
|
|
|
);
|
2012-03-09 09:52:03 +01:00
|
|
|
|
|
|
|
$this->extend('updateImportForm', $form);
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $form;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Imports the submitted CSV file based on specifications given in
|
|
|
|
* {@link self::model_importers}.
|
|
|
|
* Redirects back with a success/failure message.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @todo Figure out ajax submission of files via jQuery.form plugin
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @param SS_HTTPRequest $request
|
2016-10-26 13:35:52 +02:00
|
|
|
* @return bool|null
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function import($data, $form, $request) {
|
2014-08-15 08:53:05 +02:00
|
|
|
if(!$this->showImportForm || (is_array($this->showImportForm)
|
2012-09-26 23:34:00 +02:00
|
|
|
&& !in_array($this->modelClass,$this->showImportForm))) {
|
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
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 = '';
|
2012-05-01 21:44:54 +02:00
|
|
|
if($results->CreatedCount()) $message .= _t(
|
|
|
|
'ModelAdmin.IMPORTEDRECORDS', "Imported {count} records.",
|
|
|
|
array('count' => $results->CreatedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-05-01 21:44:54 +02:00
|
|
|
if($results->UpdatedCount()) $message .= _t(
|
|
|
|
'ModelAdmin.UPDATEDRECORDS', "Updated {count} records.",
|
|
|
|
array('count' => $results->UpdatedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-05-01 21:44:54 +02:00
|
|
|
if($results->DeletedCount()) $message .= _t(
|
|
|
|
'ModelAdmin.DELETEDRECORDS', "Deleted {count} records.",
|
|
|
|
array('count' => $results->DeletedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$results->CreatedCount() && !$results->UpdatedCount()) {
|
|
|
|
$message .= _t('ModelAdmin.NOIMPORT', "Nothing to import");
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
$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();
|
2015-04-30 01:04:08 +02:00
|
|
|
$params = $this->getRequest()->getVars();
|
2013-01-21 12:04:20 +01:00
|
|
|
if(isset($params['url'])) unset($params['url']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-02 22:11:26 +02:00
|
|
|
$items[0]->Title = $models[$this->modelClass]['title'];
|
2013-01-21 12:04:20 +01:00
|
|
|
$items[0]->Link = Controller::join_links(
|
|
|
|
$this->Link($this->sanitiseClassName($this->modelClass)),
|
|
|
|
'?' . http_build_query($params)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 09:52:03 +01:00
|
|
|
return $items;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* overwrite the static page_length of the admin panel,
|
2012-03-09 09:52:03 +01:00
|
|
|
* should be called in the project _config file.
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use "ModelAdmin.page_length" config setting
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_page_length($length){
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "ModelAdmin.page_length" config setting');
|
2013-03-21 19:48:54 +01:00
|
|
|
self::config()->page_length = $length;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
2013-03-21 19:48:54 +01:00
|
|
|
*
|
2015-06-19 01:59:27 +02:00
|
|
|
* @deprecated 4.0 Use "ModelAdmin.page_length" config setting
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_page_length(){
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0', 'Use "ModelAdmin.page_length" config setting');
|
2013-03-21 19:48:54 +01:00
|
|
|
return self::config()->page_length;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2012-03-25 09:01:43 +02:00
|
|
|
}
|