2007-07-19 12:40:05 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Provides a common interface for searching, viewing and editing DataObjects.
|
|
|
|
* Extend the class to adjust functionality to your specific DataObjects.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package cms
|
|
|
|
* @subpackage core
|
2008-08-11 02:03:57 +02:00
|
|
|
*
|
|
|
|
* @deprecated Use {@link ModelAdmin} instead, it's much more advanced and does much more for you.
|
2007-07-19 12:40:05 +02:00
|
|
|
*/
|
|
|
|
abstract class GenericDataAdmin extends LeftAndMain {
|
2008-02-25 03:10:37 +01:00
|
|
|
static $allowed_actions = array(
|
|
|
|
'createRecord',
|
|
|
|
'delete',
|
|
|
|
'export',
|
|
|
|
'getResults',
|
|
|
|
'save',
|
|
|
|
'show',
|
2008-08-09 07:57:44 +02:00
|
|
|
'CreationForm',
|
|
|
|
'ExportForm',
|
|
|
|
'SearchForm',
|
2008-02-25 03:10:37 +01:00
|
|
|
);
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
public $filter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldSet Specifies which {Actions} can be performed on a resultset,
|
|
|
|
* e.g. "Export" or "Send". The form contains the resultset as CSV for further processing.
|
|
|
|
* These actions can be extended in a subclassed constructor.
|
|
|
|
*/
|
|
|
|
protected $result_actions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
static $data_type;
|
|
|
|
|
|
|
|
static $data_type_extra;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies which information should be listed in the results-box,
|
|
|
|
* either in "table"- or "list"-format (see {$result_format}).
|
|
|
|
*
|
|
|
|
* Format "table":
|
|
|
|
* array(
|
|
|
|
* 'AccountName' => 'AccountName'
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* Format "list":
|
|
|
|
* see {@DataObject->buildNestedUL}
|
|
|
|
*/
|
|
|
|
static $result_columns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* Either "table" or "list". List-format also supports second level results.
|
|
|
|
*/
|
|
|
|
static $result_format = "table";
|
|
|
|
|
|
|
|
static $csv_columns;
|
|
|
|
|
|
|
|
private $results;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$this->result_actions = new FieldSet(
|
2008-02-25 03:10:37 +01:00
|
|
|
new FormAction('export', _t('GenericDataAdmin.EXPORTCSV', 'Export as CSV'))
|
2007-07-19 12:40:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets Requirements and checks for Permissions.
|
|
|
|
* Subclass this function to add custom Requirements.
|
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
Requirements::javascript(MCE_ROOT . "tiny_mce_src.js");
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/tiny_mce_improvements.js");
|
2007-07-19 12:40:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/hover.js");
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/scriptaculous/controls.js");
|
2007-07-19 12:40:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/SecurityAdmin.js");
|
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/CMSMain_left.js");
|
2007-07-19 12:40:05 +02:00
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/GenericDataAdmin_left.js");
|
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/GenericDataAdmin_right.js");
|
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/SideTabs.js");
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
// We don't want this showing up in every ajax-response, it should always be present in a CMS-environment
|
|
|
|
if(!Director::is_ajax()) {
|
|
|
|
Requirements::javascriptTemplate("cms/javascript/tinymce.template.js", array(
|
|
|
|
"ContentCSS" => project() . "/css/editor.css",
|
|
|
|
"BaseURL" => Director::absoluteBaseURL(),
|
2007-10-08 01:48:05 +02:00
|
|
|
"Lang" => i18n::get_tinymce_lang()
|
2007-07-19 12:40:05 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63175 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-28 15:12:20 +02:00
|
|
|
Requirements::css(CMS_DIR . "/css/GenericDataAdmin.css");
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
//For wrightgroup workshop
|
|
|
|
Requirements::css("writework/css/WorkshopCMSLayout.css");
|
|
|
|
}
|
|
|
|
|
|
|
|
function Link() {
|
|
|
|
$args = func_get_args();
|
|
|
|
return call_user_func_array( array( &$this, 'getLink' ), $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function DataTypeSingular() {
|
|
|
|
return singleton($this->stat('data_type'))->singular_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function DataTypePlural() {
|
|
|
|
return singleton($this->stat('data_type'))->plural_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function CreationForm() {
|
2008-03-03 04:11:34 +01:00
|
|
|
// is this plural name used ??
|
2008-02-25 03:10:37 +01:00
|
|
|
$plural_name = singleton($this->stat('data_type'))->i18n_plural_name();
|
2008-03-03 04:11:34 +01:00
|
|
|
$singular_name = singleton($this->stat('data_type'))->i18n_singular_name();
|
2008-02-25 03:10:37 +01:00
|
|
|
return new Form($this, 'CreationForm', new FieldSet(), new FieldSet(new FormAction("createRecord", _t('GenericDataAdmin.CREATE', 'Create').' '.$singular_name)));
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function EditForm() {
|
|
|
|
$id = isset($_REQUEST['ID']) ? $_REQUEST['ID'] : Session::get('currentPage');
|
|
|
|
if($id && DataObject::get_by_id($this->stat('data_type'), $id)) {
|
|
|
|
return $this->getEditForm($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacy
|
|
|
|
function ExportForm() {
|
2008-02-25 03:10:37 +01:00
|
|
|
return $form = new Form(
|
|
|
|
$this,
|
|
|
|
"ExportForm",
|
|
|
|
new FieldSet(
|
|
|
|
new HiddenField("csvIDs","csvIDs",$_REQUEST[csvIDs])
|
|
|
|
),
|
|
|
|
$this->result_actions
|
|
|
|
);
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function SearchForm() {
|
|
|
|
|
|
|
|
$fields = $this->getSearchFields();
|
2008-02-25 03:10:37 +01:00
|
|
|
$actions = new FieldSet($action = new FormAction("getResults", _t('GenericDataAdmin.GO', 'Go')));
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
$searchForm = new Form($this, "SearchForm", $fields, $actions);
|
|
|
|
$searchForm->loadDataFrom($_REQUEST);
|
|
|
|
return $searchForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines fields and actions for the given {$data_type}, and populates
|
|
|
|
* these fields with values from {$data_type} and any connected {$data_type_extra}.
|
|
|
|
* Adds default actions ("save" and "delete") if no custom actions are found.
|
|
|
|
* Returns an empty form if no fields or actions are found (on first load).
|
|
|
|
*
|
|
|
|
* @param $id Number
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function getEditForm($id) {
|
2007-08-21 01:17:56 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::mark('getEditForm');
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
$genericData = DataObject::get_by_id($this->stat('data_type'), $id);
|
|
|
|
|
2007-08-21 01:17:56 +02:00
|
|
|
$fields = (method_exists($genericData, 'getCMSFields')) ? $genericData->getCMSFields() : new FieldSet();
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
if(!$fields->dataFieldByName('ID')) {
|
|
|
|
|
|
|
|
$fields->push($idField = new HiddenField("ID","ID",$id));
|
|
|
|
$idField->setValue($id);
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:17:56 +02:00
|
|
|
if(method_exists($genericData, 'getGenericStatus')){
|
2007-07-19 12:40:05 +02:00
|
|
|
$genericDataStatus = $genericData->getGenericStatus();
|
|
|
|
if($genericDataStatus){
|
|
|
|
$fields->push($dataStatusField = new ReadonlyField("GenericDataStatus", "", $genericDataStatus));
|
|
|
|
$dataStatusField -> dontEscape = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-21 01:17:56 +02:00
|
|
|
$actions = (method_exists($genericData, 'getCMSActions')) ? $genericData->getCMSActions() : new FieldSet();
|
2007-07-19 12:40:05 +02:00
|
|
|
if(!$actions->fieldByName('action_save')) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$actions->push(new FormAction('save', _t('GenericDataAdmin.SAVE', 'Save'),'ajaxAction-save'));
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
if(!$actions->fieldByName('action_delete')) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$actions->push(new FormAction('delete', _t('GenericDataAdmin.DELETE', 'Delete'),'ajaxAction-delete'));
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
2007-08-23 07:45:41 +02:00
|
|
|
|
2007-08-27 07:07:43 +02:00
|
|
|
$required = (method_exists($genericData, 'getCMSRequiredField')) ? $genericData->getCMSRequiredField() : new RequiredFields();
|
2007-08-23 07:45:41 +02:00
|
|
|
$form = new Form($this, "EditForm", $fields, $actions, $required);
|
2007-07-19 12:40:05 +02:00
|
|
|
if($this->stat('data_type_extra')) {
|
|
|
|
foreach ($this->stat('data_type_extra') as $oneRelated) {
|
|
|
|
$oneExtra = $genericData-> $oneRelated();
|
|
|
|
if($oneExtra) {
|
|
|
|
$allFields = $oneExtra->getAllFields();
|
|
|
|
foreach ($allFields as $k => $v) {
|
|
|
|
$fieldname = $oneRelated . "[" . $k . "]";
|
|
|
|
$allFields[$fieldname] = $v;
|
|
|
|
unset ($allFields[$k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->loadDataFrom($allFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->loadDataFrom($genericData);
|
|
|
|
$form->disableDefaultAction();
|
|
|
|
|
2007-08-21 01:17:56 +02:00
|
|
|
if(isset($_GET['debug_profile'])) Profiler::unmark('getEditForm');
|
2007-07-19 12:40:05 +02:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the results of the search.
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function Results() {
|
|
|
|
$ret = "";
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
$singular_name = singleton($this->stat('data_type'))->i18n_singular_name();
|
|
|
|
$plural_name = singleton($this->stat('data_type'))->i18n_plural_name();
|
2007-09-16 18:31:50 +02:00
|
|
|
if (!$this->filter) {
|
2007-07-19 12:40:05 +02:00
|
|
|
$this->filter = array(
|
|
|
|
"ClassName" => $this->stat('data_type')
|
|
|
|
);
|
2007-09-16 18:31:50 +02:00
|
|
|
} else {
|
|
|
|
$this->filter = $this->filter + array("ClassName" => $this->stat('data_type'));
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
$results = $this->performSearch();
|
|
|
|
if($results) {
|
|
|
|
$name = ($results->Count() > 1) ? $plural_name : $singular_name;
|
2008-02-25 03:10:37 +01:00
|
|
|
$ret .= "<H2>{$results->Count()} {$name} "._t('GenericDataAdmin.FOUND', 'found:')."</H2>";
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
switch($this->stat('result_format')) {
|
|
|
|
case 'table':
|
|
|
|
$ret .= $this->getResultTable($results);
|
|
|
|
break;
|
|
|
|
case 'list':
|
|
|
|
$ret .= $this->getResultList($results);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$ret .= $this->getResultActionsForm($results);
|
|
|
|
} else {
|
|
|
|
if($this->hasMethod('isEmptySearch') && $this->isEmptySearch()) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$ret .='<h3>'._t('GenericDataAdmin.CHOOSECRIT', 'Please choose some search criteria and press \'Go\'.').'</h3>';
|
2007-07-19 12:40:05 +02:00
|
|
|
} else {
|
2008-02-25 03:10:37 +01:00
|
|
|
$ret .='<h3>'.sprintf(_t('GenericDataAdmin.NORESULTS', 'Sorry, no %s found by this search.'), $plural_name).'</h3>';
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2007-08-17 07:49:19 +02:00
|
|
|
function getResults($data, $form) {
|
|
|
|
return $this->Results($data, $form);
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getResultList($results, $link = true) {
|
|
|
|
$listBody = $results->buildNestedUL($this->stat('result_columns'));
|
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
<div class="ResultList">
|
|
|
|
$listBody
|
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $results
|
|
|
|
* @param $link Link the rows to their according result (evaluated by javascript)
|
|
|
|
* @return String Result-Table as HTML
|
|
|
|
*/
|
|
|
|
function getResultTable($results, $link = true) {
|
|
|
|
$tableHeader = $this->columnheader();
|
|
|
|
|
|
|
|
$tableBody = $this->columnbody($results);
|
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
<table class="ResultTable">
|
|
|
|
<thead>
|
|
|
|
$tableHeader
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
$tableBody
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function columnheader(){
|
|
|
|
$content = "";
|
|
|
|
foreach( array_keys($this->stat('result_columns')) as $field ) {
|
|
|
|
$content .= $this->htmlTableCell($field);
|
|
|
|
}
|
|
|
|
return $this->htmlTableRow($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function columnbody($results=null) {
|
|
|
|
// shouldn't be called here, but left in for legacy
|
|
|
|
if(!$results) {
|
|
|
|
$results = $this->performSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = "";
|
|
|
|
if($results){
|
|
|
|
$i=0;
|
|
|
|
foreach($results as $result){
|
|
|
|
$i++;
|
|
|
|
$html = "";
|
|
|
|
foreach($this->stat('result_columns') as $field) {
|
|
|
|
$value = $this->buildResultFieldValue($result, $field);
|
|
|
|
$html .= $this->htmlTableCell($value, $this->Link("show", $result->ID), "show", true);
|
|
|
|
|
|
|
|
}
|
|
|
|
$evenOrOdd = ($i%2)?"even":"odd";
|
|
|
|
$row = $this->htmlTableRow($html, null, $evenOrOdd);
|
|
|
|
$body .= $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function listbody($results=null) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $results Array
|
|
|
|
* @return String Form-Content
|
|
|
|
*/
|
|
|
|
function getResultActionsForm($results) {
|
|
|
|
$ret = "";
|
|
|
|
|
|
|
|
$csvValues = array();
|
|
|
|
foreach($results as $record) {
|
|
|
|
$csvValues[] = $record->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
"ExportForm",
|
|
|
|
new FieldSet(
|
|
|
|
new HiddenField("csvIDs","csvIDs",implode(",",$csvValues))
|
|
|
|
),
|
|
|
|
$this->result_actions
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret = <<<HTML
|
|
|
|
<div id="Form_ResultForm">
|
|
|
|
{$form->renderWith("Form")}
|
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $result
|
|
|
|
* @param $field Mixed can be array: eg: array("FirstName", "Surname"), in which case two fields
|
|
|
|
* in database table should concatenate to one cell for report table.
|
|
|
|
* The field could be "Total->Nice" or "Order.Created->Date", intending to show its specific format.
|
|
|
|
* Caster then is "Nice" "Date" etc.
|
|
|
|
*/
|
|
|
|
protected function buildResultFieldValue($result, $field) {
|
|
|
|
if(is_array($field)) {
|
2008-08-08 06:50:54 +02:00
|
|
|
// array-syntax
|
2007-07-19 12:40:05 +02:00
|
|
|
$i = 0;
|
|
|
|
foreach($field as $each) {
|
|
|
|
$value .= $i == 0 ? "" : "_";
|
|
|
|
$value .= $this->buildResultFieldValue($result, $each);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
2008-08-08 06:50:54 +02:00
|
|
|
// This supports simple FieldName syntax
|
|
|
|
if(strpos($field,'.') === false) {
|
|
|
|
$value = ($result->val($field)) ? $result->val($field) : $result->$field;
|
|
|
|
// This support the syntax fieldName = Relation.RelatedField
|
|
|
|
} else {
|
|
|
|
$fieldNameParts = explode('.', $field) ;
|
|
|
|
$tmpItem = $result;
|
|
|
|
for($j=0;$j<sizeof($fieldNameParts);$j++) {
|
|
|
|
$relationMethod = $fieldNameParts[$j];
|
|
|
|
$idField = $relationMethod . 'ID';
|
|
|
|
if($j == sizeof($fieldNameParts)-1) {
|
|
|
|
$value = $tmpItem->$relationMethod;
|
|
|
|
} else {
|
|
|
|
$tmpItem = $tmpItem->$relationMethod();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result = $tmpItem;
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 04:00:39 +02:00
|
|
|
if(strpos($field, '->') !== false) {
|
2008-08-13 01:17:21 +02:00
|
|
|
list ($field, $caster) = explode("->", $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($caster)) {
|
2008-08-08 06:50:54 +02:00
|
|
|
$fieldNameParts = explode('.', $field);
|
|
|
|
$fieldName = $fieldNameParts[sizeof($fieldNameParts)-1];
|
2007-07-19 12:40:05 +02:00
|
|
|
// When the intending value is Created.Date, the obj need to be casted as Datetime explicitely.
|
|
|
|
if ($field == "Created" || $field == "LastEdited") {
|
|
|
|
$created = Object::create('Datetime', $result->Created, "Created");
|
|
|
|
$value = $created->val($caster);
|
2008-08-08 06:50:54 +02:00
|
|
|
} else {
|
|
|
|
// Dealing with other field like "Total->Nice", etc.
|
|
|
|
$value = $result->obj($fieldName)->val($caster);
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function htmlTableCell($value, $link = false, $class = "", $id = null) {
|
|
|
|
if($link) {
|
|
|
|
return "<td><a href=\"$link\" class=\"$class\" id=\"$id\">" . htmlentities($value) . "</a></td>";
|
|
|
|
} else {
|
|
|
|
return "<td>" . htmlentities($value) . "</td>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function htmlTableRow($value, $link = null, $evenOrOdd = null) {
|
|
|
|
if ($link) {
|
|
|
|
return "<tr class=\"$evenOrOdd\"><a href=\"$link\">" . $value . "</a></tr>";
|
|
|
|
} else {
|
|
|
|
return "<tr class=\"$evenOrOdd\">" . $value . "</tr>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports a given set of comma-separated IDs (from a previous search-query, stored in a HiddenField).
|
|
|
|
* Uses {$csv_columns} if present, and falls back to {$result_columns}.
|
|
|
|
*/
|
|
|
|
function export() {
|
|
|
|
|
|
|
|
$now = Date("s-i-H");
|
|
|
|
$fileName = "export-$now.csv";
|
|
|
|
|
|
|
|
$csv_columns = ($this->stat('csv_columns')) ? array_values($this->stat('csv_columns')) : array_values($this->stat('result_columns'));
|
|
|
|
|
|
|
|
$fileData = "";
|
2008-02-28 01:40:31 +01:00
|
|
|
$fileData .= "\"" . implode("\",\"",$csv_columns) . "\"";
|
2007-07-19 12:40:05 +02:00
|
|
|
$fileData .= "\n";
|
|
|
|
|
|
|
|
$records = $this->performSearch();
|
|
|
|
if($records) {
|
|
|
|
foreach($records as $record) {
|
|
|
|
$columnData = array();
|
|
|
|
foreach($csv_columns as $column) {
|
|
|
|
$tmpColumnData = "\"" . str_replace("\"", "\"\"", $record->$column) . "\"";
|
|
|
|
$tmpColumnData = str_replace(array("\r", "\n"), "", $tmpColumnData);
|
|
|
|
$columnData[] = $tmpColumnData;
|
|
|
|
}
|
|
|
|
$fileData .= implode(",",$columnData);
|
|
|
|
$fileData .= "\n";
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2008-10-03 04:26:43 +02:00
|
|
|
return HTTPRequest::send_file($fileData, $fileName);
|
2007-07-19 12:40:05 +02:00
|
|
|
} else {
|
|
|
|
user_error("No records found", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-09-16 18:31:50 +02:00
|
|
|
* Save generic data handler
|
2007-07-19 12:40:05 +02:00
|
|
|
*
|
|
|
|
* @return String Statusmessage
|
|
|
|
*/
|
|
|
|
function save($urlParams, $form) {
|
|
|
|
|
|
|
|
$className = $this->stat('data_type');
|
|
|
|
|
|
|
|
$id = $_REQUEST['ID'];
|
|
|
|
|
|
|
|
if(substr($id, 0, 3) != 'new') {
|
|
|
|
$generic = DataObject::get_one($className, "`$className`.ID = $id");
|
2008-08-08 06:29:32 +02:00
|
|
|
$generic->__Status = "Saved (Update)";
|
2007-07-19 12:40:05 +02:00
|
|
|
} else {
|
|
|
|
$generic = new $className();
|
2008-08-08 06:29:32 +02:00
|
|
|
$generic->__Status = "Saved (New)";
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form->saveInto($generic, true);
|
|
|
|
$id = $generic->write();
|
|
|
|
|
|
|
|
if($this->stat('data_type_extra')) {
|
|
|
|
foreach($this->stat('data_type_extra') as $oneRelated) {
|
|
|
|
$oneExtra = $generic->$oneRelated();
|
|
|
|
if($_REQUEST[$oneExtra->class]) {
|
|
|
|
foreach($_REQUEST[$oneExtra->class] as $field => $value) {
|
|
|
|
$oneExtra->setField($field, $value);
|
|
|
|
}
|
|
|
|
}
|
2008-08-08 07:01:42 +02:00
|
|
|
$oneExtra->write();
|
|
|
|
$oneExtra->destroy();
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
$this->getActionUpdateJS($generic);
|
|
|
|
FormResponse::status_message(_t('GenericDataAdmin.SAVED', 'Saved'), 'good');
|
2008-08-08 06:29:32 +02:00
|
|
|
FormResponse::update_status($generic->__Status);
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
if (method_exists($this, "saveAfterCall")) {
|
|
|
|
$this->saveAfterCall($generic, $urlParams, $form);
|
|
|
|
}
|
|
|
|
|
2008-08-08 07:01:42 +02:00
|
|
|
$generic->destroy();
|
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
return FormResponse::respond();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a single record
|
|
|
|
*
|
|
|
|
* @return Array Editing Form
|
|
|
|
*/
|
|
|
|
function show() {
|
|
|
|
|
|
|
|
Session::set('currentPage', $this->urlParams['ID']);
|
|
|
|
|
|
|
|
$editForm = $this->getEditForm($this->urlParams['ID']);
|
|
|
|
|
|
|
|
if(Director::is_ajax()) {
|
|
|
|
return $editForm->formHtmlContent();
|
|
|
|
} else {
|
|
|
|
return array (
|
|
|
|
'EditForm' => $editForm
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new DataObject
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2008-02-27 00:19:05 +01:00
|
|
|
function createRecord($data, $form) {
|
2007-07-19 12:40:05 +02:00
|
|
|
$baseClass = $this->stat('data_type');
|
|
|
|
$obj = new $baseClass();
|
2008-02-27 00:19:05 +01:00
|
|
|
$form->saveInto($obj);
|
2007-07-19 12:40:05 +02:00
|
|
|
$obj->write();
|
2008-08-08 06:29:32 +02:00
|
|
|
|
|
|
|
// This appears to be a typo. There is no 'urlParms'.
|
|
|
|
// $this->urlParms['ID'] = $obj->ID;
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
$editForm = $this->getEditForm($obj->ID);
|
|
|
|
|
|
|
|
return (Director::is_ajax()) ? $editForm->formHtmlContent() : array ('EditForm' => $editForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a given Dataobjebt by ID
|
|
|
|
*
|
|
|
|
* @param $urlParams Array
|
|
|
|
* @param $form Form
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function delete($urlParams, $form) {
|
|
|
|
$id = Convert::raw2sql($_REQUEST['ID']);
|
|
|
|
$obj = DataObject::get_by_id($this->stat('data_type'), $id);
|
|
|
|
if ($obj) {
|
|
|
|
$obj->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear session data
|
|
|
|
Session::clear('currentPage');
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
FormResponse::status_message(_t('GenericDataAdmin.DELETEDSUCCESS', 'Successfully deleted'), 'good');
|
2007-07-19 12:40:05 +02:00
|
|
|
FormResponse::add("$('Form_EditForm').deleteEffect();");
|
|
|
|
|
|
|
|
return FormResponse::respond();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRelatedData() {
|
|
|
|
|
|
|
|
$relatedName = $_REQUEST['RelatedClass'];
|
|
|
|
$id = $_REQUEST[$relatedName]['ID'];
|
|
|
|
$baseClass = $this->stat('data_type');
|
|
|
|
$relatedClasses = singleton($baseClass)->stat('has_one');
|
|
|
|
if($id){
|
|
|
|
$relatedObject = DataObject::get_by_id($relatedClasses[$relatedName], $id);
|
|
|
|
$response .= <<<JS
|
|
|
|
$('$relatedName').unsetNewRelatedKey();
|
|
|
|
JS;
|
|
|
|
}
|
|
|
|
elseif($id !== '0'){ //in case of null;
|
|
|
|
$relatedObject = new $relatedClasses[$relatedName]();
|
|
|
|
if($parentID = $_REQUEST[$relatedName]['ParentID']){
|
|
|
|
$relatedObject->ParentID = $parentID;
|
|
|
|
}
|
|
|
|
$id = $relatedObject->write();
|
|
|
|
$response .= <<<JS
|
|
|
|
$('$relatedName').setNewRelatedKey($id);
|
|
|
|
JS;
|
|
|
|
}else{ // in case of 0
|
|
|
|
$relatedObject = new $relatedClasses[$relatedName]();
|
|
|
|
if($parentID = $_REQUEST[$relatedName]['ParentID']){
|
|
|
|
$relatedObject->ParentID = $parentID;
|
|
|
|
}
|
|
|
|
$response .= <<<JS
|
|
|
|
$('$relatedName').unsetNewRelatedKey();
|
|
|
|
JS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Director::is_ajax()) {
|
|
|
|
$fields = $_REQUEST[$relatedName];
|
|
|
|
|
|
|
|
$response .= <<<JS
|
|
|
|
var dataArray = new Array();
|
|
|
|
JS;
|
|
|
|
foreach ($fields as $k => $v) {
|
|
|
|
$JS_newKey = Convert::raw2js($relatedName . '[' . $k . ']');
|
|
|
|
$JS_newValue = Convert::raw2js($relatedObject-> $k);
|
|
|
|
$response .=<<<JS
|
|
|
|
dataArray['$JS_newKey'] = '$JS_newValue';
|
|
|
|
JS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response .=<<<JS
|
|
|
|
$('$relatedName').updateChildren(dataArray, true);
|
|
|
|
JS;
|
|
|
|
|
|
|
|
FormResponse::add($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FormResponse::respond();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateRelatedKey() {
|
|
|
|
if(Director::is_ajax()) {
|
|
|
|
$funcName = "get" . $_REQUEST['RelatedClass'] . "Dropdown";
|
|
|
|
$relatedKeyDropdown = singleton($this->stat('data_type'))->$funcName();
|
|
|
|
$relatedKeyDropdown->extraClass = "relatedDataKey";
|
|
|
|
echo $relatedKeyDropdown->FieldHolder();
|
|
|
|
} else {
|
|
|
|
Director::redirectBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a query based on {$filter} and build a DataObjectSet
|
|
|
|
* out of the results.
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
abstract function performSearch();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form fields which trigger {getResults} and {peformSearch}.
|
|
|
|
* Provide HTML in the following format to get auto-collapsing "advanced search"-fields.
|
|
|
|
* <div id="BasicSearchFields"></div>
|
|
|
|
* <div class="ToggleAdvancedSearchFields" style="display:none"><a href="#">Show advanced options</a></div>
|
|
|
|
* <div id="AdvancedSearchFields"></div>
|
|
|
|
*
|
|
|
|
* @return FieldSet
|
|
|
|
*/
|
|
|
|
abstract function getSearchFields();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide custom link.
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
abstract function getLink();
|
|
|
|
|
|
|
|
//abstract function create();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy
|
|
|
|
*/
|
|
|
|
function AddForm() {
|
|
|
|
return $this->CreationForm();
|
|
|
|
}
|
|
|
|
}
|
2008-02-28 01:40:31 +01:00
|
|
|
?>
|