2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-08-11 02:21:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Form field that embeds a list into a form, such as a member list or a file list.
|
|
|
|
*
|
|
|
|
* All get variables are namespaced in the format ctf[MyFieldName][MyParameter] to avoid collisions
|
|
|
|
* when multiple TableListFields are present in a form.
|
|
|
|
*
|
2012-03-12 17:20:40 +01:00
|
|
|
* @deprecated 3.0 Use GridField with GridFieldConfig_RecordEditor
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class TableListField extends FormField {
|
|
|
|
/**
|
2009-11-22 07:07:45 +01:00
|
|
|
* The {@link DataList} object defining the source data for this view/
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-11-22 07:07:45 +01:00
|
|
|
protected $dataList;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
protected $fieldList;
|
|
|
|
|
2009-10-15 23:53:15 +02:00
|
|
|
protected $disableSorting = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var $fieldListCsv array
|
|
|
|
*/
|
|
|
|
protected $fieldListCsv;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $clickAction
|
|
|
|
*/
|
|
|
|
protected $clickAction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $IsReadOnly;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called method (needs to be retained for AddMode())
|
|
|
|
*/
|
|
|
|
protected $methodName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $summaryFieldList array Shows a row which summarizes the contents of a column by a predefined
|
|
|
|
* Javascript-function
|
|
|
|
*/
|
|
|
|
protected $summaryFieldList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $summaryTitle string The title which will be shown in the first column of the summary-row.
|
|
|
|
* Accordingly, the first column can't be used for summarizing.
|
|
|
|
*/
|
|
|
|
protected $summaryTitle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $template string Template-Overrides
|
|
|
|
*/
|
|
|
|
protected $template = "TableListField";
|
|
|
|
|
2010-02-11 05:21:04 +01:00
|
|
|
/**
|
|
|
|
* @var $itemClass string Class name for each item/row
|
|
|
|
*/
|
|
|
|
public $itemClass = 'TableListField_Item';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var bool Do we use checkboxes to mark records, or delete them one by one?
|
|
|
|
*/
|
|
|
|
public $Markable;
|
|
|
|
|
2008-08-12 01:24:54 +02:00
|
|
|
public $MarkableTitle = null;
|
|
|
|
|
2010-10-15 04:27:59 +02:00
|
|
|
/**
|
|
|
|
* @var array See {@link SelectOptions()}
|
|
|
|
*/
|
2010-10-15 04:51:04 +02:00
|
|
|
|
2010-10-15 04:27:59 +02:00
|
|
|
protected $selectOptions = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var $readOnly boolean Deprecated, please use $permssions instead
|
|
|
|
*/
|
|
|
|
protected $readOnly;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $permissions array Influence output without having to subclass the template.
|
2008-08-12 01:24:54 +02:00
|
|
|
* See $actions for adding your custom actions/permissions.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected $permissions = array(
|
|
|
|
//"print",
|
|
|
|
//"export",
|
|
|
|
"delete"
|
|
|
|
);
|
|
|
|
|
2008-08-12 01:24:54 +02:00
|
|
|
/**
|
|
|
|
* @var $actions array Action that can be performed on a single row-entry.
|
|
|
|
* Has to correspond to a method in a TableListField-class (or subclass).
|
|
|
|
* Actions can be disabled through $permissions.
|
|
|
|
* Format (key is used for the methodname and CSS-class):
|
|
|
|
* array(
|
2009-03-13 11:07:27 +01:00
|
|
|
* 'delete' => array(
|
|
|
|
* 'label' => 'Delete',
|
2012-07-16 05:18:31 +02:00
|
|
|
* 'icon' => 'framework/images/delete.gif',
|
|
|
|
* 'icon_disabled' => 'framework/images/delete_disabled.gif',
|
2009-03-13 11:07:27 +01:00
|
|
|
* 'class' => 'deletelink',
|
|
|
|
* )
|
2008-08-12 01:24:54 +02:00
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public $actions = array(
|
|
|
|
'delete' => array(
|
|
|
|
'label' => 'Delete',
|
2012-07-16 05:18:31 +02:00
|
|
|
'icon' => 'framework/images/delete.gif',
|
|
|
|
'icon_disabled' => 'framework/images/delete_disabled.gif',
|
2008-08-12 01:24:54 +02:00
|
|
|
'class' => 'deletelink'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $defaultAction String Action being executed when clicking on table-row (defaults to "show").
|
|
|
|
* Mostly needed in ComplexTableField-subclass.
|
|
|
|
*/
|
|
|
|
public $defaultAction = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $customCsvQuery Query for CSV-export (might need different fields or further filtering)
|
|
|
|
*/
|
|
|
|
protected $customCsvQuery;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Character to seperate exported columns in the CSV file
|
|
|
|
*/
|
2007-11-13 00:07:45 +01:00
|
|
|
protected $csvSeparator = ",";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Boolean deciding whether to include a header row in the CSV file
|
|
|
|
*/
|
|
|
|
protected $csvHasHeader = true;
|
|
|
|
|
2008-08-11 02:21:44 +02:00
|
|
|
/**
|
|
|
|
* @var array Specify custom escape for the fields.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* array("\""=>"\"\"","\r"=>"", "\r\n"=>"", "\n"=>"")
|
|
|
|
* </code>
|
|
|
|
*/
|
|
|
|
public $csvFieldEscape = array(
|
|
|
|
"\""=>"\"\"",
|
|
|
|
"\r\n"=>"",
|
|
|
|
"\r"=>"",
|
|
|
|
"\n"=>"",
|
|
|
|
);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var boolean Trigger pagination
|
|
|
|
*/
|
|
|
|
protected $showPagination = false;
|
|
|
|
|
2009-09-15 06:11:13 +02:00
|
|
|
/**
|
|
|
|
* @var string Override the {@link Link()} method
|
|
|
|
* for all pagination. Useful to force rendering of the field
|
|
|
|
* in a different context.
|
|
|
|
*/
|
|
|
|
public $paginationBaseLink = null;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var int Number of items to show on a single page (needed for pagination)
|
|
|
|
*/
|
|
|
|
protected $pageSize = 10;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Definitions for highlighting table-rows with a specific class. You can use all column-names
|
2012-09-26 23:34:00 +02:00
|
|
|
* in the result of a query. Use in combination with {@setCustomQuery} to select custom properties and joined
|
|
|
|
* objects.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* array(
|
|
|
|
* array(
|
|
|
|
* "rule" => '$Flag == "red"',
|
|
|
|
* "class" => "red"
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* "rule" => '$Flag == "orange"',
|
|
|
|
* "class" => "orange"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public $highlightConditions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Specify castings with fieldname as the key, and the desired casting as value.
|
|
|
|
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
|
|
|
|
*/
|
|
|
|
public $fieldCasting = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
|
|
|
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
|
|
|
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'
|
|
|
|
*/
|
|
|
|
public $fieldFormatting = array();
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
public $csvFieldFormatting = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $exportButtonLabel = 'Export as CSV';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $groupByField Used to group by a specific column in the DataObject
|
|
|
|
* and create partial summaries.
|
|
|
|
*/
|
|
|
|
public $groupByField = null;
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $extraLinkParams;
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
protected $__cachedQuery;
|
|
|
|
|
2011-03-30 03:28:50 +02:00
|
|
|
/**
|
|
|
|
* This is a flag that enables some backward-compatibility helpers.
|
|
|
|
*/
|
|
|
|
private $getDataListFromForm;
|
|
|
|
|
2012-03-12 17:20:40 +01:00
|
|
|
/**
|
|
|
|
* @param $name string The fieldname
|
|
|
|
* @param $sourceClass string The source class of this field
|
|
|
|
* @param $fieldList array An array of field headings of Fieldname => Heading Text (eg. heading1)
|
|
|
|
* @param $sourceFilter string The filter field you wish to limit the objects by (eg. parentID)
|
|
|
|
* @param $sourceSort string
|
|
|
|
* @param $sourceJoin string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $sourceClass = null, $fieldList = null, $sourceFilter = null,
|
2007-07-19 12:40:28 +02:00
|
|
|
$sourceSort = null, $sourceJoin = null) {
|
|
|
|
|
2009-11-22 07:07:45 +01:00
|
|
|
if($sourceClass) {
|
2011-05-03 04:22:18 +02:00
|
|
|
// You can optionally pass a list
|
|
|
|
if($sourceClass instanceof SS_List) {
|
2009-11-22 07:07:45 +01:00
|
|
|
$this->dataList = $sourceClass;
|
|
|
|
|
|
|
|
} else {
|
2011-10-29 06:11:27 +02:00
|
|
|
$this->dataList = DataObject::get($sourceClass)->where($sourceFilter)->sort($sourceSort);
|
|
|
|
if($sourceJoin) $this->dataList = $this->dataList->join($sourceJoin);
|
2011-03-30 03:28:50 +02:00
|
|
|
// Grab it from the form relation, if available.
|
|
|
|
$this->getDataListFromForm = true;
|
2009-11-22 07:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 03:28:50 +02:00
|
|
|
$this->fieldList = ($fieldList) ? $fieldList : singleton($this->sourceClass())->summaryFields();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->readOnly = false;
|
|
|
|
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2008-08-11 05:39:14 +02:00
|
|
|
return $this->FieldHolder();
|
|
|
|
}
|
2008-10-08 05:32:33 +02:00
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
static $url_handlers = array(
|
|
|
|
'item/$ID' => 'handleItem',
|
2008-10-08 05:32:33 +02:00
|
|
|
'$Action' => '$Action',
|
2008-10-08 04:00:12 +02:00
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sourceClass() {
|
2011-03-30 03:28:50 +02:00
|
|
|
$list = $this->getDataList();
|
|
|
|
if(method_exists($list, 'dataClass')) return $list->dataClass();
|
2011-10-26 08:09:04 +02:00
|
|
|
// Failover for SS_List
|
2011-03-30 03:28:50 +02:00
|
|
|
else return get_class($list->First());
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleItem($request) {
|
2008-10-08 04:00:12 +02:00
|
|
|
return new TableListField_ItemRequest($this, $request->param('ID'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldHolder($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/prototype/prototype.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/behaviour/behaviour.js');
|
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TableListField.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/TableListField.css');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($this->clickAction) {
|
|
|
|
$id = $this->id();
|
|
|
|
Requirements::customScript(<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
'#$id tr' : {
|
|
|
|
onclick : function() {
|
|
|
|
$this->clickAction
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS
|
|
|
|
);}
|
2012-04-11 08:07:55 +02:00
|
|
|
|
|
|
|
$obj = $properties ? $this->customise($properties) : $this;
|
|
|
|
return $obj->renderWith($this->template);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Headings() {
|
2008-08-11 01:29:30 +02:00
|
|
|
$headings = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this->fieldList as $fieldName => $fieldTitle) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$isSorted = (isset($_REQUEST['ctf'][$this->getName()]['sort'])
|
|
|
|
&& $fieldName == $_REQUEST['ctf'][$this->getName()]['sort']);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// we can't allow sorting with partial summaries (groupByField)
|
|
|
|
$isSortable = ($this->form && $this->isFieldSortable($fieldName) && !$this->groupByField);
|
|
|
|
|
|
|
|
// sorting links (only if we have a form to refresh with)
|
|
|
|
if($this->form) {
|
2008-08-09 06:38:44 +02:00
|
|
|
$sortLink = $this->Link();
|
2011-10-29 13:07:40 +02:00
|
|
|
$sortLink = HTTP::setGetVar("ctf[{$this->getName()}][sort]", $fieldName, $sortLink,'&');
|
2010-04-13 04:15:23 +02:00
|
|
|
|
2010-04-13 04:15:50 +02:00
|
|
|
// Apply sort direction to the current sort field
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!empty($_REQUEST['ctf'][$this->getName()]['sort'])
|
|
|
|
&& ($_REQUEST['ctf'][$this->getName()]['sort'] == $fieldName)) {
|
|
|
|
|
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['dir'])) {
|
|
|
|
$dir = $_REQUEST['ctf'][$this->getName()]['dir'];
|
|
|
|
} else {
|
|
|
|
$dir = null;
|
|
|
|
}
|
|
|
|
|
2010-04-13 04:15:50 +02:00
|
|
|
$dir = trim(strtolower($dir));
|
|
|
|
$newDir = ($dir == 'desc') ? null : 'desc';
|
2012-09-26 23:34:00 +02:00
|
|
|
$sortLink = HTTP::setGetVar("ctf[{$this->getName()}][dir]", Convert::raw2xml($newDir),
|
|
|
|
$sortLink,'&');
|
2010-04-13 04:15:50 +02:00
|
|
|
}
|
2010-04-13 04:15:23 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['search'])
|
|
|
|
&& is_array($_REQUEST['ctf'][$this->getName()]['search'])) {
|
|
|
|
|
2011-10-29 13:07:40 +02:00
|
|
|
foreach($_REQUEST['ctf'][$this->getName()]['search'] as $parameter => $value) {
|
2009-05-19 12:32:06 +02:00
|
|
|
$XML_search = Convert::raw2xml($value);
|
2012-09-26 23:34:00 +02:00
|
|
|
$sortLink = HTTP::setGetVar("ctf[{$this->getName()}][search][$parameter]", $XML_search,
|
|
|
|
$sortLink,'&');
|
2009-05-19 12:32:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$sortLink = '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
$headings[] = new ArrayData(array(
|
|
|
|
"Name" => $fieldName,
|
2012-09-26 23:34:00 +02:00
|
|
|
"Title" => ($this->sourceClass())
|
|
|
|
? singleton($this->sourceClass())->fieldLabel($fieldTitle)
|
|
|
|
: $fieldTitle,
|
2007-07-19 12:40:28 +02:00
|
|
|
"IsSortable" => $isSortable,
|
2008-08-19 12:07:28 +02:00
|
|
|
"SortLink" => $sortLink,
|
2007-07-19 12:40:28 +02:00
|
|
|
"SortBy" => $isSorted,
|
2012-09-26 23:34:00 +02:00
|
|
|
"SortDirection" => (isset($_REQUEST['ctf'][$this->getName()]['dir']))
|
|
|
|
? $_REQUEST['ctf'][$this->getName()]['dir']
|
|
|
|
: null
|
2007-07-19 12:40:28 +02:00
|
|
|
));
|
|
|
|
}
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($headings);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-15 23:53:15 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function disableSorting($to = true) {
|
2009-10-15 23:53:15 +02:00
|
|
|
$this->disableSorting = $to;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a field is "sortable".
|
|
|
|
* If the field is generated by a custom getter, we can't sort on it
|
|
|
|
* without generating all objects first (which would be a huge performance impact).
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isFieldSortable($fieldName) {
|
2011-03-30 03:28:50 +02:00
|
|
|
if($this->disableSorting) return false;
|
|
|
|
$list = $this->getDataList();
|
|
|
|
if(method_exists($list,'canSortBy')) return $list->canSortBy($fieldName);
|
|
|
|
else return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-12 01:24:54 +02:00
|
|
|
/**
|
|
|
|
* Dummy function to get number of actions originally generated in
|
|
|
|
* TableListField_Item.
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2008-08-12 01:24:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Actions() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$allowedActions = new ArrayList();
|
2008-08-12 01:24:54 +02:00
|
|
|
foreach($this->actions as $actionName => $actionSettings) {
|
|
|
|
if($this->Can($actionName)) {
|
|
|
|
$allowedActions->push(new ViewableData());
|
|
|
|
}
|
|
|
|
}
|
2008-08-14 06:38:22 +02:00
|
|
|
|
2008-08-12 01:24:54 +02:00
|
|
|
return $allowedActions;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Provide a custom query to compute sourceItems. This is the preferred way to using
|
|
|
|
* {@setSourceItems}, because we can still paginate.
|
|
|
|
* Please use this only as a fallback for really complex queries (e.g. involving HAVING and GROUPBY).
|
|
|
|
*
|
2009-11-22 07:07:45 +01:00
|
|
|
* @param $query DataList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCustomQuery(DataList $dataList) {
|
2009-11-22 07:07:45 +01:00
|
|
|
$this->dataList = $dataList;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCustomCsvQuery(DataList $dataList) {
|
2009-11-22 07:07:45 +01:00
|
|
|
$this->customCsvQuery = $query;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCustomSourceItems(SS_List $items) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error('TableList::setCustomSourceItems() deprecated, just pass the items into the constructor',
|
|
|
|
E_USER_WARNING);
|
2011-03-30 03:28:50 +02:00
|
|
|
|
2010-04-13 04:01:39 +02:00
|
|
|
// The type-hinting above doesn't seem to work consistently
|
2011-10-26 08:09:04 +02:00
|
|
|
if($items instanceof SS_List) {
|
2011-03-30 03:28:50 +02:00
|
|
|
$this->dataList = $items;
|
2010-04-13 04:01:39 +02:00
|
|
|
} else {
|
2011-10-26 08:09:04 +02:00
|
|
|
user_error('TableList::setCustomSourceItems() should be passed a SS_List', E_USER_WARNING);
|
2010-04-13 04:01:39 +02:00
|
|
|
}
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 03:28:50 +02:00
|
|
|
/**
|
|
|
|
* Get items, with sort & limit applied
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sourceItems() {
|
2011-10-26 08:09:04 +02:00
|
|
|
// get items (this may actually be a SS_List)
|
2011-03-30 03:28:50 +02:00
|
|
|
$items = clone $this->getDataList();
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2011-10-26 08:09:04 +02:00
|
|
|
// TODO: Sorting could be implemented on regular SS_Lists.
|
2011-10-29 13:07:40 +02:00
|
|
|
if(method_exists($items,'canSortBy') && isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
|
|
|
$sort = $_REQUEST['ctf'][$this->getName()]['sort'];
|
2011-03-30 03:28:50 +02:00
|
|
|
// TODO: sort direction
|
|
|
|
if($items->canSortBy($sort)) $items = $items->sort($sort);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 03:28:50 +02:00
|
|
|
// Determine pagination limit, offset
|
|
|
|
// To disable pagination, set $this->showPagination to false.
|
|
|
|
if($this->showPagination && $this->pageSize) {
|
|
|
|
$SQL_limit = (int)$this->pageSize;
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
&& is_numeric($_REQUEST['ctf'][$this->getName()]['start'])) {
|
|
|
|
|
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['start'])) {
|
|
|
|
$SQL_start = intval($_REQUEST['ctf'][$this->getName()]['start']);
|
|
|
|
} else {
|
|
|
|
$SQL_start = "0";
|
|
|
|
}
|
2011-03-30 03:28:50 +02:00
|
|
|
} else {
|
|
|
|
$SQL_start = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:02:37 +01:00
|
|
|
$items = $items->limit($SQL_limit, $SQL_start);
|
2011-03-30 03:28:50 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $items;
|
|
|
|
}
|
2009-11-22 07:07:45 +01:00
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Return a SS_List of TableListField_Item objects, suitable for display in the template.
|
2009-11-22 07:07:45 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Items() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$fieldItems = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
if($items = $this->sourceItems()) foreach($items as $item) {
|
2010-02-11 05:21:04 +01:00
|
|
|
if($item) $fieldItems->push(new $this->itemClass($item, $this));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
return $fieldItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-22 07:07:45 +01:00
|
|
|
* Returns the DataList for this field.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDataList() {
|
2011-03-30 03:28:50 +02:00
|
|
|
// If we weren't passed in a DataList to begin with, try and get the datalist from the form
|
|
|
|
if($this->form && $this->getDataListFromForm) {
|
|
|
|
$this->getDataListFromForm = false;
|
2009-11-22 07:07:45 +01:00
|
|
|
$relation = $this->name;
|
|
|
|
if($record = $this->form->getRecord()) {
|
|
|
|
if($record->hasMethod($relation)) $this->dataList = $record->$relation();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
if(!$this->dataList) {
|
|
|
|
user_error(get_class($this). ' is missing a DataList', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2011-03-30 03:28:50 +02:00
|
|
|
return $this->dataList;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCsvDataList() {
|
2009-11-22 07:07:45 +01:00
|
|
|
if($this->customCsvQuery) return $this->customCsvQuery;
|
|
|
|
else return $this->getDataList();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 07:07:45 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Use getDataList() instead.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getQuery() {
|
2011-10-29 01:02:11 +02:00
|
|
|
Deprecation::notice('3.0', 'Use getDataList() instead.');
|
2011-03-30 03:28:50 +02:00
|
|
|
$list = $this->getDataList();
|
|
|
|
if(method_exists($list,'dataQuery')) {
|
|
|
|
return $this->getDataList()->dataQuery()->query();
|
|
|
|
}
|
2009-11-22 07:07:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use getCsvDataList() instead.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCsvQuery() {
|
2011-10-29 01:02:11 +02:00
|
|
|
Deprecation::notice('3.0', 'Use getCsvDataList() instead.');
|
2011-03-30 03:28:50 +02:00
|
|
|
$list = $this->getCsvDataList();
|
|
|
|
if(method_exists($list,'dataQuery')) {
|
|
|
|
return $list->dataQuery()->query();
|
|
|
|
}
|
2009-11-22 07:07:45 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FieldList() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->fieldList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure this table to load content into a subform via ajax
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setClick_AjaxLoad($urlBase, $formID) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->clickAction = "this.ajaxRequest('" . addslashes($urlBase) . "', '" . addslashes($formID) . "')";
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure this table to open a popup window
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setClick_PopupLoad($urlBase) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->clickAction = "var w = window.open(baseHref()+'$urlBase'+this.id.replace(/.*-(\d*)$/,'$1'), 'popup');"
|
|
|
|
. " w.focus();";
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setShowPagination(false);
|
2009-10-16 00:39:07 +02:00
|
|
|
|
|
|
|
// Only include the show action if it was in the original CTF.
|
|
|
|
$clone->setPermissions(in_array('show', $this->permissions) ? array('show') : array());
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone->addExtraClass( 'readonly' );
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* CRUD
|
|
|
|
* #################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function delete($request) {
|
2010-12-05 09:26:03 +01:00
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
$token = $this->getForm()->getSecurityToken();
|
|
|
|
if(!$token->checkRequest($request)) return $this->httpError('400');
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->Can('delete') !== true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->methodName = "delete";
|
|
|
|
|
|
|
|
$childId = Convert::raw2sql($_REQUEST['ctf']['childID']);
|
|
|
|
|
|
|
|
if (is_numeric($childId)) {
|
2011-03-30 03:28:50 +02:00
|
|
|
$this->getDataList()->removeById($childId);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO return status in JSON etc.
|
|
|
|
//return $this->renderWith($this->template);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* Summary-Row
|
|
|
|
* #################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can utilize some built-in summary-functions, with optional casting.
|
|
|
|
* Currently supported:
|
|
|
|
* - sum
|
|
|
|
* - avg
|
|
|
|
*
|
|
|
|
* @param $summaryTitle string
|
|
|
|
* @param $summaryFields array
|
|
|
|
* Simple Format: array("MyFieldName"=>"sum")
|
|
|
|
* With Casting: array("MyFieldname"=>array("sum","Currency->Nice"))
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addSummary($summaryTitle, $summaryFieldList) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->summaryTitle = $summaryTitle;
|
|
|
|
$this->summaryFieldList = $summaryFieldList;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeSummary() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->summaryTitle = null;
|
|
|
|
$this->summaryFields = null;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function HasSummary() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return (isset($this->summaryFieldList));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function SummaryTitle() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->summaryTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param SS_List $items Only used to pass grouped sourceItems for creating
|
2007-07-19 12:40:28 +02:00
|
|
|
* partial summaries.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function SummaryFields($items = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!isset($this->summaryFieldList)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$summaryFields = array();
|
|
|
|
$fieldListWithoutFirst = $this->fieldList;
|
|
|
|
if(!empty($this->summaryTitle)) {
|
|
|
|
array_shift($fieldListWithoutFirst);
|
|
|
|
}
|
|
|
|
foreach($fieldListWithoutFirst as $fieldName => $fieldTitle) {
|
|
|
|
|
|
|
|
if(in_array($fieldName, array_keys($this->summaryFieldList))) {
|
|
|
|
if(is_array($this->summaryFieldList[$fieldName])) {
|
|
|
|
$summaryFunction = "colFunction_{$this->summaryFieldList[$fieldName][0]}";
|
|
|
|
$casting = $this->summaryFieldList[$fieldName][1];
|
|
|
|
} else {
|
|
|
|
$summaryFunction = "colFunction_{$this->summaryFieldList[$fieldName]}";
|
|
|
|
$casting = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fall back to integrated sourceitems if not passed
|
|
|
|
if(!$items) $items = $this->sourceItems();
|
|
|
|
|
|
|
|
$summaryValue = ($items) ? $this->$summaryFunction($items->column($fieldName)) : null;
|
|
|
|
|
|
|
|
// Optional casting, Format: array('MyFieldName'=>array('sum','Currency->Nice'))
|
|
|
|
if(isset($casting)) {
|
2008-08-13 01:04:14 +02:00
|
|
|
$summaryValue = $this->getCastedValue($summaryValue, $casting);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$summaryValue = null;
|
|
|
|
$function = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$summaryFields[] = new ArrayData(array(
|
|
|
|
'Function' => $function,
|
|
|
|
'SummaryValue' => $summaryValue,
|
2012-03-27 06:57:42 +02:00
|
|
|
'Name' => DBField::create_field('Varchar', $fieldName),
|
|
|
|
'Title' => DBField::create_field('Varchar', $fieldTitle),
|
2007-07-19 12:40:28 +02:00
|
|
|
));
|
|
|
|
}
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($summaryFields);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function HasGroupedItems() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return ($this->groupByField);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function GroupedItems() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->groupByField) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $this->sourceItems();
|
|
|
|
if(!$items || !$items->Count()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupedItems = $items->groupBy($this->groupByField);
|
2011-05-05 12:40:24 +02:00
|
|
|
$groupedArrItems = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($groupedItems as $key => $group) {
|
2011-05-05 12:40:24 +02:00
|
|
|
$fieldItems = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($group as $item) {
|
2010-02-11 05:21:04 +01:00
|
|
|
if($item) $fieldItems->push(new $this->itemClass($item, $this));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$groupedArrItems->push(new ArrayData(array(
|
|
|
|
'Items' => $fieldItems,
|
|
|
|
'SummaryFields' => $this->SummaryFields($group)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groupedArrItems;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function colFunction_sum($values) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return array_sum($values);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function colFunction_avg($values) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return array_sum($values)/count($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* Permissions
|
|
|
|
* #################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-03-13 11:07:27 +01:00
|
|
|
* Template accessor for Permissions.
|
|
|
|
* See {@link TableListField_Item->Can()} for object-specific
|
|
|
|
* permissions.
|
|
|
|
*
|
|
|
|
* @return boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Can($mode) {
|
2008-08-12 04:58:48 +02:00
|
|
|
if($mode == 'add' && $this->isReadonly()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
2008-08-12 04:58:48 +02:00
|
|
|
} else if($mode == 'delete' && $this->isReadonly()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
2008-08-12 04:58:48 +02:00
|
|
|
} else if($mode == 'edit' && $this->isReadonly()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return (in_array($mode, $this->permissions));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setPermissions($arr) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->permissions = $arr;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getPermissions() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->permissions;
|
|
|
|
}
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* Pagination
|
|
|
|
* #################################
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setShowPagination($bool) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->showPagination = (bool)$bool;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ShowPagination() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->showPagination && !empty($this->summaryFieldList)) {
|
|
|
|
user_error("You can't combine pagination and summaries - please disable one of them.", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
return $this->showPagination;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setPageSize($pageSize) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->pageSize = $pageSize;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function PageSize() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->pageSize;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ListStart() {
|
2011-10-29 13:07:40 +02:00
|
|
|
return $_REQUEST['ctf'][$this->getName()]['start'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* @param array
|
2008-08-26 03:45:52 +02:00
|
|
|
* @deprecated Put the query string onto your form's link instead :-)
|
2008-08-11 01:17:51 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setExtraLinkParams($params){
|
2011-10-29 01:02:11 +02:00
|
|
|
Deprecation::notice('2.4', 'Put the query string onto your FormAction instead().');
|
2008-08-11 01:17:51 +02:00
|
|
|
$this->extraLinkParams = $params;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-08-11 01:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtraLinkParams(){
|
2008-08-11 01:17:51 +02:00
|
|
|
return $this->extraLinkParams;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FirstLink() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$start = 0;
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
|| !is_numeric($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
|| $_REQUEST['ctf'][$this->getName()]['start'] == 0) {
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
|
|
|
}
|
2009-09-15 06:11:13 +02:00
|
|
|
$baseLink = ($this->paginationBaseLink) ? $this->paginationBaseLink : $this->Link();
|
2011-10-29 13:07:40 +02:00
|
|
|
$link = Controller::join_links($baseLink, "?ctf[{$this->getName()}][start]={$start}");
|
2008-08-11 01:17:51 +02:00
|
|
|
if($this->extraLinkParams) $link .= "&" . http_build_query($this->extraLinkParams);
|
2010-10-15 04:29:44 +02:00
|
|
|
|
|
|
|
// preserve sort options
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][sort]=" . $_REQUEST['ctf'][$this->getName()]['sort'];
|
2010-10-15 04:29:44 +02:00
|
|
|
// direction
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['dir'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][dir]=" . $_REQUEST['ctf'][$this->getName()]['dir'];
|
2010-10-15 04:29:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
return $link;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function PrevLink() {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['start'])) {
|
|
|
|
$currentStart = $_REQUEST['ctf'][$this->getName()]['start'];
|
|
|
|
} else {
|
|
|
|
$currentStart = 0;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($currentStart == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if($_REQUEST['ctf'][$this->getName()]['start'] - $this->pageSize < 0) {
|
|
|
|
$start = 0;
|
|
|
|
} else {
|
|
|
|
$start = $_REQUEST['ctf'][$this->getName()]['start'] - $this->pageSize;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-09-15 06:11:13 +02:00
|
|
|
$baseLink = ($this->paginationBaseLink) ? $this->paginationBaseLink : $this->Link();
|
2011-10-29 13:07:40 +02:00
|
|
|
$link = Controller::join_links($baseLink, "?ctf[{$this->getName()}][start]={$start}");
|
2008-08-11 01:17:51 +02:00
|
|
|
if($this->extraLinkParams) $link .= "&" . http_build_query($this->extraLinkParams);
|
2010-10-15 04:29:44 +02:00
|
|
|
|
|
|
|
// preserve sort options
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][sort]=" . $_REQUEST['ctf'][$this->getName()]['sort'];
|
2010-10-15 04:29:44 +02:00
|
|
|
// direction
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['dir'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][dir]=" . $_REQUEST['ctf'][$this->getName()]['dir'];
|
2010-10-15 04:29:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
return $link;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 03:45:52 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function NextLink() {
|
2012-09-26 23:34:00 +02:00
|
|
|
$currentStart = isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
? $_REQUEST['ctf'][$this->getName()]['start']
|
|
|
|
: 0;
|
|
|
|
$start = ($currentStart + $this->pageSize < $this->TotalCount())
|
|
|
|
? $currentStart + $this->pageSize
|
|
|
|
: $this->TotalCount() % $this->pageSize > 0;
|
2007-07-19 12:40:28 +02:00
|
|
|
if($currentStart >= $start-1) {
|
|
|
|
return null;
|
|
|
|
}
|
2009-09-15 06:11:13 +02:00
|
|
|
$baseLink = ($this->paginationBaseLink) ? $this->paginationBaseLink : $this->Link();
|
2011-10-29 13:07:40 +02:00
|
|
|
$link = Controller::join_links($baseLink, "?ctf[{$this->getName()}][start]={$start}");
|
2008-08-11 01:17:51 +02:00
|
|
|
if($this->extraLinkParams) $link .= "&" . http_build_query($this->extraLinkParams);
|
2010-10-15 04:29:44 +02:00
|
|
|
|
|
|
|
// preserve sort options
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][sort]=" . $_REQUEST['ctf'][$this->getName()]['sort'];
|
2010-10-15 04:29:44 +02:00
|
|
|
// direction
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['dir'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][dir]=" . $_REQUEST['ctf'][$this->getName()]['dir'];
|
2010-10-15 04:29:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
return $link;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function LastLink() {
|
2012-09-26 23:34:00 +02:00
|
|
|
$pageSize = ($this->TotalCount() % $this->pageSize > 0)
|
|
|
|
? $this->TotalCount() % $this->pageSize
|
|
|
|
: $this->pageSize;
|
2008-08-11 01:17:51 +02:00
|
|
|
$start = $this->TotalCount() - $pageSize;
|
2007-07-19 12:40:28 +02:00
|
|
|
// Check if there is only one page, or if we are on last page
|
2012-09-26 23:34:00 +02:00
|
|
|
if($this->TotalCount() <= $pageSize || (isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
&& $_REQUEST['ctf'][$this->getName()]['start'] >= $start)) {
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-09-15 06:11:13 +02:00
|
|
|
$baseLink = ($this->paginationBaseLink) ? $this->paginationBaseLink : $this->Link();
|
2011-10-29 13:07:40 +02:00
|
|
|
$link = Controller::join_links($baseLink, "?ctf[{$this->getName()}][start]={$start}");
|
2008-08-11 01:17:51 +02:00
|
|
|
if($this->extraLinkParams) $link .= "&" . http_build_query($this->extraLinkParams);
|
2010-10-15 04:29:44 +02:00
|
|
|
|
|
|
|
// preserve sort options
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][sort]=" . $_REQUEST['ctf'][$this->getName()]['sort'];
|
2010-10-15 04:29:44 +02:00
|
|
|
// direction
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['dir'])) {
|
|
|
|
$link .= "&ctf[{$this->getName()}][dir]=" . $_REQUEST['ctf'][$this->getName()]['dir'];
|
2010-10-15 04:29:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
return $link;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function FirstItem() {
|
2009-02-02 00:49:53 +01:00
|
|
|
if ($this->TotalCount() < 1) return 0;
|
2012-09-26 23:34:00 +02:00
|
|
|
return isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
? $_REQUEST['ctf'][$this->getName()]['start'] + 1
|
|
|
|
: 1;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function LastItem() {
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['start'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$pageStep = min($this->pageSize, $this->TotalCount() - $_REQUEST['ctf'][$this->getName()]['start']);
|
|
|
|
return $_REQUEST['ctf'][$this->getName()]['start'] + $pageStep;
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
return min($this->pageSize, $this->TotalCount());
|
|
|
|
}
|
|
|
|
}
|
2011-03-30 03:28:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
private $_cache_TotalCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the total number of items in the source DataList
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function TotalCount() {
|
2011-03-30 03:28:50 +02:00
|
|
|
if($this->_cache_TotalCount === null) {
|
|
|
|
$this->_cache_TotalCount = $this->getDataList()->Count();
|
|
|
|
}
|
|
|
|
return $this->_cache_TotalCount;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* Search
|
|
|
|
* #################################
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Not fully implemented at the moment
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile all request-parameters for search and pagination
|
|
|
|
* (except the actual list-positions) as a query-string.
|
|
|
|
*
|
|
|
|
* @return String URL-parameters
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function filterString() {
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* CSV Export
|
|
|
|
* #################################
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFieldListCsv($fields) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fieldListCsv = $fields;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-13 00:07:45 +01:00
|
|
|
* Set the CSV separator character. Defaults to ,
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCsvSeparator($csvSeparator) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->csvSeparator = $csvSeparator;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 01:53:56 +02:00
|
|
|
/**
|
|
|
|
* Get the CSV separator character. Defaults to ,
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCsvSeparator() {
|
2008-08-11 01:53:56 +02:00
|
|
|
return $this->csvSeparator;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Remove the header row from the CSV export
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeCsvHeader() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->csvHasHeader = false;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}.
|
2009-02-02 00:49:53 +01:00
|
|
|
* We move the most filedata generation code to the function {@link generateExportFileData()} so that a child class
|
|
|
|
* could reuse the filedata generation code while overwrite export function.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Make relation-syntax available (at the moment you'll have to use custom sql)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function export() {
|
2012-05-11 04:26:25 +02:00
|
|
|
$now = date("d-m-Y-H-i");
|
2007-07-19 12:40:28 +02:00
|
|
|
$fileName = "export-$now.csv";
|
2011-03-30 03:28:50 +02:00
|
|
|
|
2012-05-11 04:26:25 +02:00
|
|
|
// No pagination for export
|
2011-03-30 03:28:50 +02:00
|
|
|
$oldShowPagination = $this->showPagination;
|
|
|
|
$this->showPagination = false;
|
|
|
|
|
|
|
|
$result = $this->renderWith(array($this->template . '_printable', 'TableListField_printable'));
|
|
|
|
|
|
|
|
$this->showPagination = $oldShowPagination;
|
2008-10-08 04:00:12 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
if($fileData = $this->generateExportFileData($numColumns, $numRows)){
|
2012-05-11 04:26:25 +02:00
|
|
|
return SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
|
|
|
|
} else {
|
2009-02-02 00:49:53 +01:00
|
|
|
user_error("No records found", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function generateExportFileData(&$numColumns, &$numRows) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$separator = $this->csvSeparator;
|
|
|
|
$csvColumns = ($this->fieldListCsv) ? $this->fieldListCsv : $this->fieldList;
|
2009-02-02 00:49:53 +01:00
|
|
|
$fileData = '';
|
|
|
|
$columnData = array();
|
2011-05-05 12:40:24 +02:00
|
|
|
$fieldItems = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($this->csvHasHeader) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$fileData .= "\"" . implode("\"{$separator}\"", array_values($csvColumns)) . "\"";
|
2007-07-19 12:40:28 +02:00
|
|
|
$fileData .= "\n";
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
if(isset($this->customSourceItems)) {
|
2008-10-08 04:00:12 +02:00
|
|
|
$items = $this->customSourceItems;
|
2009-02-02 00:49:53 +01:00
|
|
|
} else {
|
2011-10-29 06:13:37 +02:00
|
|
|
$items = $this->getCsvDataList();
|
2008-08-11 01:53:56 +02:00
|
|
|
}
|
|
|
|
|
2008-08-11 02:21:44 +02:00
|
|
|
// temporary override to adjust TableListField_Item behaviour
|
2008-08-11 01:53:56 +02:00
|
|
|
$this->setFieldFormatting(array());
|
2008-08-11 02:21:44 +02:00
|
|
|
$this->fieldList = $csvColumns;
|
2008-08-11 01:53:56 +02:00
|
|
|
|
2009-05-14 07:26:47 +02:00
|
|
|
if($items) {
|
|
|
|
foreach($items as $item) {
|
|
|
|
if(is_array($item)) {
|
|
|
|
$className = isset($item['RecordClassName']) ? $item['RecordClassName'] : $item['ClassName'];
|
|
|
|
$item = new $className($item);
|
|
|
|
}
|
2010-04-14 04:20:28 +02:00
|
|
|
$fieldItem = new $this->itemClass($item, $this);
|
2009-05-14 07:26:47 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
$fields = $fieldItem->Fields(false);
|
2009-02-02 00:49:53 +01:00
|
|
|
$columnData = array();
|
|
|
|
if($fields) foreach($fields as $field) {
|
2008-10-08 04:00:12 +02:00
|
|
|
$value = $field->Value;
|
|
|
|
|
|
|
|
// TODO This should be replaced with casting
|
|
|
|
if(array_key_exists($field->Name, $this->csvFieldFormatting)) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$format = str_replace('$value', "__VAL__", $this->csvFieldFormatting[$field->Name]);
|
2008-10-08 04:00:12 +02:00
|
|
|
$format = preg_replace('/\$([A-Za-z0-9-_]+)/','$item->$1', $format);
|
|
|
|
$format = str_replace('__VAL__', '$value', $format);
|
|
|
|
eval('$value = "' . $format . '";');
|
2008-08-11 02:21:44 +02:00
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
$value = str_replace(array("\r", "\n"), "\n", $value);
|
2010-04-13 04:16:08 +02:00
|
|
|
$tmpColumnData = '"' . str_replace('"', '\"', $value) . '"';
|
2008-10-08 04:00:12 +02:00
|
|
|
$columnData[] = $tmpColumnData;
|
2008-08-11 02:21:44 +02:00
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
$fileData .= implode($separator, $columnData);
|
|
|
|
$fileData .= "\n";
|
2009-05-14 07:26:47 +02:00
|
|
|
|
|
|
|
$item->destroy();
|
|
|
|
unset($item);
|
|
|
|
unset($fieldItem);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
$numColumns = count($columnData);
|
|
|
|
$numRows = $fieldItems->count();
|
|
|
|
return $fileData;
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-02-02 00:49:53 +01:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need to instanciate this button manually as a normal button has no means of adding inline onclick-behaviour.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ExportLink() {
|
2008-08-11 01:53:56 +02:00
|
|
|
$exportLink = Controller::join_links($this->Link(), 'export');
|
2008-08-26 03:45:52 +02:00
|
|
|
|
2008-08-11 01:53:56 +02:00
|
|
|
if($this->extraLinkParams) $exportLink .= "?" . http_build_query($this->extraLinkParams);
|
|
|
|
return $exportLink;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function printall() {
|
2007-07-19 12:40:28 +02:00
|
|
|
Requirements::clear();
|
2012-03-23 23:49:48 +01:00
|
|
|
if(defined('CMS_DIR')) {
|
|
|
|
Requirements::css(CMS_DIR . '/css/typography.css');
|
|
|
|
Requirements::css(CMS_DIR . '/css/cms_right.css');
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
2010-10-04 06:44:58 +02:00
|
|
|
$this->cachedSourceItems = null;
|
2008-10-08 04:00:12 +02:00
|
|
|
$oldShowPagination = $this->showPagination;
|
|
|
|
$this->showPagination = false;
|
2009-06-28 04:36:46 +02:00
|
|
|
|
|
|
|
increase_time_limit_to();
|
2010-04-13 04:26:52 +02:00
|
|
|
$this->Print = true;
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
$result = $this->renderWith(array($this->template . '_printable', 'TableListField_printable'));
|
|
|
|
|
|
|
|
$this->showPagination = $oldShowPagination;
|
|
|
|
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function PrintLink() {
|
2008-08-09 06:38:44 +02:00
|
|
|
$link = Controller::join_links($this->Link(), 'printall');
|
2011-10-29 13:07:40 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['sort'])) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$link = HTTP::setGetVar("ctf[{$this->getName()}][sort]",
|
|
|
|
Convert::raw2xml($_REQUEST['ctf'][$this->getName()]['sort']), $link);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* #################################
|
|
|
|
* Utilty
|
|
|
|
* #################################
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Utility() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$links = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->can('export')) {
|
|
|
|
$links->push(new ArrayData(array(
|
2008-01-10 23:27:38 +01:00
|
|
|
'Title' => _t('TableListField.CSVEXPORT', 'Export to CSV'),
|
2007-07-19 12:40:28 +02:00
|
|
|
'Link' => $this->ExportLink()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if($this->can('print')) {
|
|
|
|
$links->push(new ArrayData(array(
|
2008-01-10 23:27:38 +01:00
|
|
|
'Title' => _t('TableListField.PRINT', 'Print'),
|
2007-07-19 12:40:28 +02:00
|
|
|
'Link' => $this->PrintLink()
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
return $links;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFieldCasting($casting) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fieldCasting = $casting;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFieldFormatting($formatting) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fieldFormatting = $formatting;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCSVFieldFormatting($formatting) {
|
2008-10-08 04:00:12 +02:00
|
|
|
$this->csvFieldFormatting = $formatting;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Edit the field list
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFieldList($fieldList) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$this->fieldList = $fieldList;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Name() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Title() {
|
2008-01-10 23:27:38 +01:00
|
|
|
// adding translating functionality
|
|
|
|
// this is a bit complicated, because this parameter is passed to this class
|
|
|
|
// and should come here translated already
|
|
|
|
// adding this to TODO probably add a method to the classes
|
|
|
|
// to return they're translated string
|
|
|
|
// added by ruibarreiros @ 27/11/2007
|
2011-10-29 13:07:40 +02:00
|
|
|
return $this->sourceClass() ? singleton($this->sourceClass())->singular_name() : $this->getName();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function NameSingular() {
|
2008-01-10 23:27:38 +01:00
|
|
|
// same as Title()
|
|
|
|
// added by ruibarreiros @ 27/11/2007
|
2011-10-29 13:07:40 +02:00
|
|
|
return $this->sourceClass() ? singleton($this->sourceClass())->singular_name() : $this->getName();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function NamePlural() {
|
2008-01-10 23:27:38 +01:00
|
|
|
// same as Title()
|
|
|
|
// added by ruibarreiros @ 27/11/2007
|
2011-10-29 13:07:40 +02:00
|
|
|
return $this->sourceClass() ? singleton($this->sourceClass())->plural_name() : $this->getName();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTemplate($template) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->template = $template;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 04:57:59 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function CurrentLink() {
|
2008-08-11 04:57:59 +02:00
|
|
|
$link = $this->Link();
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['ctf'][$this->getName()]['start'])
|
|
|
|
&& is_numeric($_REQUEST['ctf'][$this->getName()]['start'])) {
|
|
|
|
|
|
|
|
$start = ($_REQUEST['ctf'][$this->getName()]['start'] < 0)
|
|
|
|
? 0
|
|
|
|
: $_REQUEST['ctf'][$this->getName()]['start'];
|
2011-10-29 13:07:40 +02:00
|
|
|
$link = Controller::join_links($link, "?ctf[{$this->getName()}][start]={$start}");
|
2008-08-11 04:57:59 +02:00
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
|
2008-08-11 04:57:59 +02:00
|
|
|
if($this->extraLinkParams) $link .= "&" . http_build_query($this->extraLinkParams);
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
2010-12-05 09:24:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overloaded to automatically add security token.
|
|
|
|
*
|
|
|
|
* @param String $action
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link($action = null) {
|
2010-12-05 09:24:58 +01:00
|
|
|
$form = $this->getForm();
|
|
|
|
if($form) {
|
|
|
|
$token = $form->getSecurityToken();
|
|
|
|
$parentUrlParts = parse_url(parent::Link($action));
|
|
|
|
$queryPart = (isset($parentUrlParts['query'])) ? '?' . $parentUrlParts['query'] : null;
|
|
|
|
// Ensure that URL actions not routed through Form->httpSubmission() are protected against CSRF attacks.
|
2011-10-29 06:10:02 +02:00
|
|
|
if($token->isEnabled()) $queryPart = $token->addtoUrl($queryPart);
|
2011-01-10 04:46:28 +01:00
|
|
|
return Controller::join_links($parentUrlParts['path'], $queryPart);
|
2010-12-05 09:24:58 +01:00
|
|
|
} else {
|
|
|
|
// allow for instanciation of this FormField outside of a controller/form
|
|
|
|
// context (e.g. for unit tests)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-08-11 04:57:59 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function BaseLink() {
|
2008-08-09 06:38:44 +02:00
|
|
|
user_error("TableListField::BaseLink() deprecated, use Link() instead", E_USER_NOTICE);
|
|
|
|
return $this->Link();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 08:29:50 +02:00
|
|
|
/**
|
|
|
|
* Helper method to determine permissions for a scaffolded
|
2012-09-26 23:34:00 +02:00
|
|
|
* TableListField (or subclasses) - currently used in {@link ModelAdmin} and
|
|
|
|
* {@link DataObject->scaffoldFormFields()}.
|
|
|
|
*
|
2008-08-09 08:29:50 +02:00
|
|
|
* Returns true for each permission that doesn't have an explicit getter.
|
|
|
|
*
|
|
|
|
* @todo Temporary method, implement directly in FormField subclasses with object-level permissions.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param numeric $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function permissions_for_object($class, $id = null) {
|
|
|
|
$permissions = array();
|
|
|
|
$obj = ($id) ? DataObject::get_by_id($class, $id) : singleton($class);
|
|
|
|
|
|
|
|
if(!$obj->hasMethod('canView') || $obj->canView()) $permissions[] = 'show';
|
|
|
|
if(!$obj->hasMethod('canEdit') || $obj->canEdit()) $permissions[] = 'edit';
|
|
|
|
if(!$obj->hasMethod('canDelete') || $obj->canDelete()) $permissions[] = 'delete';
|
|
|
|
if(!$obj->hasMethod('canCreate') || $obj->canCreate()) $permissions[] = 'add';
|
|
|
|
|
|
|
|
return $permissions;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-13 01:04:14 +02:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCastedValue($value, $castingDefinition) {
|
2008-08-13 01:04:14 +02:00
|
|
|
if(is_array($castingDefinition)) {
|
|
|
|
$castingParams = $castingDefinition;
|
|
|
|
array_shift($castingParams);
|
|
|
|
$castingDefinition = array_shift($castingDefinition);
|
|
|
|
} else {
|
|
|
|
$castingParams = array();
|
|
|
|
}
|
|
|
|
if(strpos($castingDefinition,'->') === false) {
|
|
|
|
$castingFieldType = $castingDefinition;
|
2012-03-27 06:57:42 +02:00
|
|
|
$castingField = DBField::create_field($castingFieldType, $value);
|
2008-08-13 01:04:14 +02:00
|
|
|
$value = call_user_func_array(array($castingField,'XML'),$castingParams);
|
|
|
|
} else {
|
|
|
|
$fieldTypeParts = explode('->', $castingDefinition);
|
|
|
|
$castingFieldType = $fieldTypeParts[0];
|
|
|
|
$castingMethod = $fieldTypeParts[1];
|
2012-03-27 06:57:42 +02:00
|
|
|
$castingField = DBField::create_field($castingFieldType, $value);
|
2008-08-13 01:04:14 +02:00
|
|
|
$value = call_user_func_array(array($castingField,$castingMethod),$castingParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setHighlightConditions($conditions) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->highlightConditions = $conditions;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-10-15 04:27:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link SelectOptions()} for introduction.
|
|
|
|
*
|
|
|
|
* @param $options array Options to add, key being a unique identifier of the action,
|
|
|
|
* and value a title for the rendered link element (can contain HTML).
|
|
|
|
* The keys for 'all' and 'none' have special behaviour associated
|
|
|
|
* through TableListField.js JavaScript.
|
|
|
|
* For any other key, the JavaScript automatically checks all checkboxes contained in
|
|
|
|
* <td> elements with a matching classname.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addSelectOptions($options){
|
2010-10-15 04:27:59 +02:00
|
|
|
foreach($options as $k => $title)
|
|
|
|
$this->selectOptions[$k] = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove one all more table's {@link $selectOptions}
|
|
|
|
*
|
|
|
|
* @param $optionsNames array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function removeSelectOptions($names){
|
2010-10-15 04:27:59 +02:00
|
|
|
foreach($names as $name){
|
|
|
|
unset($this->selectOptions[trim($name)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the table's {@link $selectOptions}.
|
|
|
|
* Used to toggle checkboxes for each table row through button elements.
|
|
|
|
*
|
|
|
|
* Requires {@link Markable()} to return TRUE.
|
|
|
|
* This is only functional with JavaScript enabled.
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List of ArrayData objects
|
2010-10-15 04:27:59 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function SelectOptions(){
|
2010-10-15 04:27:59 +02:00
|
|
|
if(!$this->selectOptions) return;
|
|
|
|
|
2011-05-05 12:40:24 +02:00
|
|
|
$selectOptionsSet = new ArrayList();
|
2010-10-15 04:27:59 +02:00
|
|
|
foreach($this->selectOptions as $k => $v) {
|
|
|
|
$selectOptionsSet->push(new ArrayData(array(
|
|
|
|
'Key' => $k,
|
|
|
|
'Value' => $v
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
return $selectOptionsSet;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* A single record in a TableListField.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
* @see TableListField
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class TableListField_Item extends ViewableData {
|
2009-03-13 11:07:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DataObject The underlying data record,
|
|
|
|
* usually an element of {@link TableListField->sourceItems()}.
|
|
|
|
*/
|
|
|
|
protected $item;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TableListField
|
|
|
|
*/
|
|
|
|
protected $parent;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($item, $parent) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->failover = $this->item = $item;
|
|
|
|
$this->parent = $parent;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ID() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->item->ID;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Parent() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Fields($xmlSafe = true) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$list = $this->parent->FieldList();
|
|
|
|
foreach($list as $fieldName => $fieldTitle) {
|
2008-08-11 02:03:57 +02:00
|
|
|
$value = "";
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// This supports simple FieldName syntax
|
|
|
|
if(strpos($fieldName,'.') === false) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$value = ($this->item->XML_val($fieldName) && $xmlSafe)
|
|
|
|
? $this->item->XML_val($fieldName)
|
|
|
|
: $this->item->RAW_val($fieldName);
|
2009-05-28 09:08:23 +02:00
|
|
|
// This support the syntax fieldName = Relation.RelatedField
|
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldNameParts = explode('.', $fieldName) ;
|
|
|
|
$tmpItem = $this->item;
|
|
|
|
for($j=0;$j<sizeof($fieldNameParts);$j++) {
|
|
|
|
$relationMethod = $fieldNameParts[$j];
|
|
|
|
$idField = $relationMethod . 'ID';
|
|
|
|
if($j == sizeof($fieldNameParts)-1) {
|
2008-08-11 01:17:51 +02:00
|
|
|
if($tmpItem) $value = $tmpItem->$relationMethod;
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2008-08-11 01:17:51 +02:00
|
|
|
if($tmpItem) $tmpItem = $tmpItem->$relationMethod();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 07:30:26 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// casting
|
|
|
|
if(array_key_exists($fieldName, $this->parent->fieldCasting)) {
|
2008-08-13 01:04:14 +02:00
|
|
|
$value = $this->parent->getCastedValue($value, $this->parent->fieldCasting[$fieldName]);
|
2009-05-21 06:48:24 +02:00
|
|
|
} elseif(is_object($value) && method_exists($value, 'Nice')) {
|
|
|
|
$value = $value->Nice();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-20 05:09:50 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// formatting
|
|
|
|
$item = $this->item;
|
|
|
|
if(array_key_exists($fieldName, $this->parent->fieldFormatting)) {
|
|
|
|
$format = str_replace('$value', "__VAL__", $this->parent->fieldFormatting[$fieldName]);
|
|
|
|
$format = preg_replace('/\$([A-Za-z0-9-_]+)/','$item->$1', $format);
|
|
|
|
$format = str_replace('__VAL__', '$value', $format);
|
|
|
|
eval('$value = "' . $format . '";');
|
|
|
|
}
|
2008-08-11 01:53:56 +02:00
|
|
|
|
|
|
|
//escape
|
|
|
|
if($escape = $this->parent->fieldEscape){
|
|
|
|
foreach($escape as $search => $replace){
|
|
|
|
$value = str_replace($search, $replace, $value);
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 07:30:26 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields[] = new ArrayData(array(
|
|
|
|
"Name" => $fieldName,
|
|
|
|
"Title" => $fieldTitle,
|
|
|
|
"Value" => $value,
|
2008-08-11 01:53:56 +02:00
|
|
|
"CsvSeparator" => $this->parent->getCsvSeparator(),
|
2007-07-19 12:40:28 +02:00
|
|
|
));
|
|
|
|
}
|
2011-05-05 12:40:24 +02:00
|
|
|
return new ArrayList($fields);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Markable() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->parent->Markable;
|
|
|
|
}
|
|
|
|
|
2009-03-13 11:07:27 +01:00
|
|
|
/**
|
|
|
|
* Checks global permissions for field in {@link TableListField->Can()}.
|
|
|
|
* If they are allowed, it checks for object permissions by assuming
|
|
|
|
* a method with "can" + $mode parameter naming, e.g. canDelete().
|
|
|
|
*
|
|
|
|
* @param string $mode See {@link TableListField::$permissions} array.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Can($mode) {
|
2009-03-13 11:07:27 +01:00
|
|
|
$canMethod = "can" . ucfirst($mode);
|
|
|
|
if(!$this->parent->Can($mode)) {
|
|
|
|
// check global settings for the field instance
|
|
|
|
return false;
|
|
|
|
} elseif($this->item->hasMethod($canMethod)) {
|
|
|
|
// if global allows, check object specific permissions (e.g. canDelete())
|
|
|
|
return $this->item->$canMethod();
|
|
|
|
} else {
|
|
|
|
// otherwise global allowed this action, so return TRUE
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-11 04:57:59 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link($action = null) {
|
2010-12-05 09:24:58 +01:00
|
|
|
$form = $this->parent->getForm();
|
|
|
|
if($form) {
|
2010-12-05 09:26:03 +01:00
|
|
|
$token = $form->getSecurityToken();
|
2008-11-17 23:59:17 +01:00
|
|
|
$parentUrlParts = parse_url($this->parent->Link());
|
|
|
|
$queryPart = (isset($parentUrlParts['query'])) ? '?' . $parentUrlParts['query'] : null;
|
2010-12-05 09:24:58 +01:00
|
|
|
// Ensure that URL actions not routed through Form->httpSubmission() are protected against CSRF attacks.
|
2011-10-29 06:10:02 +02:00
|
|
|
if($token->isEnabled()) $queryPart = $token->addtoUrl($queryPart);
|
2009-10-11 02:07:16 +02:00
|
|
|
return Controller::join_links($parentUrlParts['path'], 'item', $this->item->ID, $action, $queryPart);
|
2008-10-08 04:00:12 +02:00
|
|
|
} else {
|
|
|
|
// allow for instanciation of this FormField outside of a controller/form
|
|
|
|
// context (e.g. for unit tests)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-14 06:38:22 +02:00
|
|
|
/**
|
|
|
|
* Returns all row-based actions not disallowed through permissions.
|
|
|
|
* See TableListField->Action for a similiar dummy-function to work
|
|
|
|
* around template-inheritance issues.
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2008-08-14 06:38:22 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Actions() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$allowedActions = new ArrayList();
|
2008-08-14 06:38:22 +02:00
|
|
|
foreach($this->parent->actions as $actionName => $actionSettings) {
|
|
|
|
if($this->parent->Can($actionName)) {
|
|
|
|
$allowedActions->push(new ArrayData(array(
|
|
|
|
'Name' => $actionName,
|
|
|
|
'Link' => $this->{ucfirst($actionName).'Link'}(),
|
|
|
|
'Icon' => $actionSettings['icon'],
|
2009-03-13 11:07:27 +01:00
|
|
|
'IconDisabled' => $actionSettings['icon_disabled'],
|
2008-08-14 06:38:22 +02:00
|
|
|
'Label' => $actionSettings['label'],
|
|
|
|
'Class' => $actionSettings['class'],
|
|
|
|
'Default' => ($actionName == $this->parent->defaultAction),
|
2009-03-13 11:07:27 +01:00
|
|
|
'IsAllowed' => $this->Can($actionName),
|
2008-08-14 06:38:22 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $allowedActions;
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function BaseLink() {
|
2008-08-09 06:38:44 +02:00
|
|
|
user_error("TableListField_Item::BaseLink() deprecated, use Link() instead", E_USER_NOTICE);
|
2008-08-28 06:14:22 +02:00
|
|
|
return $this->Link();
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function DeleteLink() {
|
2008-08-09 06:38:44 +02:00
|
|
|
return Controller::join_links($this->Link(), "delete");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function MarkingCheckbox() {
|
2011-10-29 13:07:40 +02:00
|
|
|
$name = $this->parent->getName() . '[]';
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
if($this->parent->isReadonly())
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\""
|
|
|
|
. " disabled=\"disabled\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
else
|
|
|
|
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" />";
|
|
|
|
}
|
|
|
|
|
2010-10-15 04:27:59 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* According to {@link TableListField->selectOptions}, each record will check if the options' key on the object is
|
|
|
|
* true, if it is true, add the key as a class to the record
|
2010-10-15 04:27:59 +02:00
|
|
|
*
|
|
|
|
* @return string Value for a 'class' HTML attribute.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function SelectOptionClasses(){
|
2010-10-15 04:27:59 +02:00
|
|
|
$tagArray = array('markingcheckbox');
|
2010-10-15 04:52:05 +02:00
|
|
|
$options = $this->parent->SelectOptions();
|
2010-10-15 04:51:33 +02:00
|
|
|
if($options && $options->exists()){
|
|
|
|
foreach($options as $option){
|
|
|
|
if($option->Key !== 'all' && $option->Key !== 'none'){
|
|
|
|
if($this->{$option->Key}) {
|
|
|
|
$tagArray[] = $option->Key;
|
2010-10-15 04:27:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(" ",$tagArray);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function HighlightClasses() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$classes = array();
|
|
|
|
foreach($this->parent->highlightConditions as $condition) {
|
|
|
|
$rule = str_replace("\$","\$this->item->", $condition['rule']);
|
|
|
|
$ruleApplies = null;
|
|
|
|
eval('$ruleApplies = ('.$rule.');');
|
|
|
|
if($ruleApplies) {
|
2010-10-19 03:36:39 +02:00
|
|
|
if(isset($condition['exclusive']) && $condition['exclusive']) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $condition['class'];
|
|
|
|
} else {
|
|
|
|
$classes[] = $condition['class'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count($classes) > 0) ? " " . implode(" ", $classes) : false;
|
|
|
|
}
|
2008-08-14 06:38:22 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Legacy: Please use permissions instead
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function isReadonly() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->parent->Can('delete');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2008-10-30 23:03:21 +01:00
|
|
|
class TableListField_ItemRequest extends RequestHandler {
|
2008-10-08 04:00:12 +02:00
|
|
|
protected $ctf;
|
|
|
|
protected $itemID;
|
|
|
|
protected $methodName;
|
|
|
|
|
|
|
|
static $url_handlers = array(
|
|
|
|
'$Action!' => '$Action',
|
|
|
|
'' => 'index',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2010-04-12 05:27:03 +02:00
|
|
|
return Controller::join_links($this->ctf->Link(), 'item/' . $this->itemID);
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($ctf, $itemID) {
|
2008-10-08 04:00:12 +02:00
|
|
|
$this->ctf = $ctf;
|
|
|
|
$this->itemID = $itemID;
|
2009-05-18 00:59:48 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function delete($request) {
|
2010-12-05 09:26:03 +01:00
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
$token = $this->ctf->getForm()->getSecurityToken();
|
|
|
|
if(!$token->checkRequest($request)) return $this->httpError('400');
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
if($this->ctf->Can('delete') !== true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dataObj()->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the data object being manipulated
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataObj() {
|
2008-10-08 04:00:12 +02:00
|
|
|
// used to discover fields if requested and for population of field
|
|
|
|
if(is_numeric($this->itemID)) {
|
|
|
|
// we have to use the basedataclass, otherwise we might exclude other subclasses
|
2011-03-30 03:28:50 +02:00
|
|
|
return $this->ctf->getDataList()->byId($this->itemID);
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-25 07:00:25 +02:00
|
|
|
/**
|
|
|
|
* @return TableListField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getParentController() {
|
2010-05-25 07:00:25 +02:00
|
|
|
return $this->ctf;
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
2012-02-12 21:22:11 +01:00
|
|
|
|