(merged from branches/roa. use "svn log -c <changeset> -g <module-svn-path>" for detailed commit message)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@59920 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-08-06 03:28:25 +00:00
parent 12899b864c
commit 038bcbb8d9
10 changed files with 331 additions and 5 deletions

265
code/ModelAdmin.php Normal file
View File

@ -0,0 +1,265 @@
<?php
/**
* Generates a three-pane UI for editing one or many 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:
* <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',
'delete',
);
public function init() {
parent::init();
// security check for valid models
if(isset($this->urlParams['Model']) && !in_array($this->urlParams['Model'], $this->getManagedModels())) {
user_error('ModelAdmin::init(): Invalid Model class', E_USER_ERROR);
}
Requirements::css('cms/css/ModelAdmin.css');
}
/**
* Return default link to this controller
*
* @todo We've specified this in Director::addRules(), why repeat?
*
* @return string
*/
public function Link() {
return 'admin/crm/' . implode('/', array_values(array_unique($this->urlParams)));
}
/**
* Create empty edit form (scaffolded from DataObject->getCMSFields() by default).
* Can be called either through {@link AddForm()} or directly through URL:
* "/myadminroute/MyModelClass/add"
*
* @param array $data
* @param Form $form
*/
public function add($data, $form) {
$className = (isset($data['ClassName'])) ? $data['ClassName'] : $this->urlParams['ClassName'];
if(!isset($className) || !in_array($data['ClassName'], $this->getManagedModels())) return false;
return $this->customise(array(
'EditForm' => $this->getEditForm($data['ClassName'])
))->renderWith('LeftAndMain');
}
/**
* Edit forms (scaffolded from DataObject->getCMSFields() by default).
*
* @param array $data
* @param Form $form
*/
public function edit($data, $form) {
if(!isset($data['ClassName']) || !in_array($data['ClassName'], $this->getManagedModels())) return false;
// @todo generate editform
}
/**
* @todo documentation
*
* @param array $data
* @param Form $form
*/
public function save($data, $form) {
// @todo implementation
}
/**
* Allows to choose which record needs to be created.
*
* @return Form
*/
protected function AddForm() {
$models = $this->getManagedModels();
$modelMap = array();
foreach($models as $modelName) $modelMap[$modelName] = singleton($modelName)->singular_name();
$form = new Form(
$this,
'AddForm',
new FieldSet(
new DropdownField(
'ClassName',
'Type',
$modelMap
)
),
new FieldSet(
new FormAction('add', _t('GenericDataAdmin.CREATE'))
)
);
return $form;
}
/**
*
* @uses {@link SearchContext}
* @uses {@link SearchFilter}
* @return Form
*/
protected function getSearchForms() {
$modelClasses = $this->getManagedModels();
$forms = new DataObjectSet();
foreach($modelClasses as $modelClass) {
$modelObj = singleton($modelClass);
$form = $this->getSearchFormForModel($modelClass);
$forms->push(new ArrayData(array(
'Form' => $form,
'Title' => $modelObj->singular_name()
)));
}
return $forms;
}
/**
* Enter description here...
*
* @todo Add customizeable validator
*
* @param string $modelClass
*/
protected function getEditForm($modelClass) {
$modelObj = singleton($modelClass);
$fields = $this->getFieldsForModel($modelObj);
$actions = $this->getActionsForModel($modelObj);
$validator = $this->getValidatorForModel($modelObj);
$form = new Form(
$this,
'ModelEditForm',
$fields,
$actions,
$validator
);
return $form;
}
// ############# Utility Methods ##############
/**
* @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;
}
/**
* Get all cms fields for the model
* (defaults to {@link DataObject->getCMSFields()}).
*
* @todo Make method hook customizeable
*
* @param DataObject $modelObj
* @return FieldSet
*/
protected function getFieldsForModel($modelObj) {
return $modelObj->getCMSFields();
}
/**
* Get all actions which are possible in this controller,
* and allowed by the model security.
*
* @todo Hook into model security once its is implemented
*
* @param DataObject $modelObj
* @return FieldSet
*/
protected function getActionsForModel($modelObj) {
$actions = new FieldSet(
new FormAction('save', _t('GenericDataAdmin.SAVE')),
new FormAction('delete', _t('GenericDataAdmin.DELETE'))
);
return $actions;
}
/**
* NOT IMPLEMENTED
* Get a valdidator for the model.
*
* @todo Hook into model security once its is implemented
*
* @param DataObject $modelObj
* @return FieldSet
*/
protected function getValidatorForModel($modelObj) {
return false;
}
/**
* Get a search form for a single {@link DataObject} subclass.
*
* @param string $modelClass
* @return FieldSet
*/
protected function getSearchFormForModel($modelClass) {
$context = singleton($modelClass)->getDefaultSearchContext();
$form = new Form(
$this,
"SearchForm_{$modelClass}",
$context->getSearchFields(),
new FieldSet(
new FormAction('search', _t('MemberTableField.SEARCH'))
)
);
return $form;
}
}
?>

27
css/ModelAdmin.css Normal file
View File

@ -0,0 +1,27 @@
body.ModelAdmin #left {
width: 250px;
}
body.ModelAdmin #Form_AddForm fieldset {
float: left;
}
body.ModelAdmin #Form_AddForm #ClassName {
margin: 0;
padding: auto;
width: auto;
}
body.ModelAdmin #Form_AddForm #ClassName label {
display: none;
}
body.ModelAdmin #SearchForm_holder,
body.ModelAdmin #AddForm_holder {
padding: 1em;
}
body.ModelAdmin #SearchForm_holder {
overflow: auto;
height: 300px;
}

View File

@ -765,7 +765,7 @@ GBModalDialog.prototype = {
function hideLoading() {
$('Loading').style.display = 'none';
document.body.className = '';
Element.removeClassName(document.body, 'stillLoading');
}
function baseHref() {
var baseTags = document.getElementsByTagName('base');

0
javascript/ModelAdmin.js Normal file
View File

View File

@ -5,7 +5,7 @@
<title>$Title</title>
<% base_tag %>
</head>
<body>
<body class="$CSSClasses">
$Content
<div class="right">
$Form

View File

@ -6,7 +6,7 @@
<title><% _t('UNTITLED','Untitled Document') %></title>
<meta http-equiv="imagetoolbar" content="no">
</head>
<body id="body" onload="ImageEditor.imageEditor = new ImageEditor.Main.initialize('$fileToEdit');">
<body id="body" class="$CSSClasses" onload="ImageEditor.imageEditor = new ImageEditor.Main.initialize('$fileToEdit');">
<div id="Loading" style="background: #FFF url(cms/images/loading.gif) 50% 50% no-repeat; position: absolute;z-index: 100000;height: 100%;width: 100%;margin: 0;padding: 0;z-index: 100000;position: absolute;">Loading...</div>
<div id="Main">
<script type="text/javascript">

View File

@ -0,0 +1,20 @@
<div id="LeftPane">
<h2><% _t('ADDLISTING','Add Listing') %></h2>
<div id="AddForm_holder" class="lefttop">
$AddForm
</div>
<h2><% _t('SEARCHLISTINGS','Search Listings') %></h2>
<div id="SearchForm_holder" class="leftbottom">
<ul>
<% control SearchForms %>
<li><a href="#$Form.Name">$Title</a></li>
<% end_control %>
</ul>
<% control SearchForms %>
$Form
<% end_control %>
</div>
<h2><% _t('SEARCHRESULTS','Search Results') %></h2>
<div id="ResultTable_holder" class="leftbottom">
</div>
</div>

View File

@ -0,0 +1,14 @@
<% include Editor_toolbar %>
<% if EditForm %>
$EditForm
<% else %>
<form id="Form_EditForm" action="admin?executeForm=EditForm" method="post" enctype="multipart/form-data">
<h1>$ApplicationName</h1>
<p><% _t('WELCOME1', 'Welcome to') %> $ApplicationName! <% _t('WELCOME2', 'Please choose on one of the entries in the left pane.') %></p>
</form>
<% end_if %>
<p id="statusMessage" style="visibility:hidden"></p>

View File

@ -11,7 +11,7 @@
<link rel="stylesheet" type="text/css" href="cms/css/cms_right.css" />
</head>
<body class="stillLoading">
<body class="stillLoading $CSSClasses">
<div id="Loading" style="background: #FFF url($LoadingImage) 50% 50% no-repeat; position: absolute;z-index: 100000;height: 100%;width: 100%;margin: 0;padding: 0;z-index: 100000;position: absolute;"><% _t('LOADING','Loading...',PR_HIGH) %></div>
<div id="top">

View File

@ -6,7 +6,7 @@
<title>SilverStripe CMS - $Title</title>
</head>
<body onblur="window.close()">
<body class="$CSSClasses" onblur="window.close()">
$PrintForm
</body>
</html>