2008-09-26 04:22:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ComplexTableField designed to edit a has_many join.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
class HasManyComplexTableField extends ComplexTableField {
|
|
|
|
|
|
|
|
public $joinField;
|
|
|
|
|
|
|
|
protected $addTitle;
|
|
|
|
|
|
|
|
protected $htmlListEndName = 'CheckedList'; // If you change the value, do not forget to change it also in the JS file
|
|
|
|
|
|
|
|
protected $htmlListField = 'selected'; // If you change the value, do not forget to change it also in the JS file
|
|
|
|
|
|
|
|
public $template = 'RelationComplexTableField';
|
|
|
|
|
|
|
|
public $itemClass = 'HasManyComplexTableField_Item';
|
|
|
|
|
|
|
|
protected $relationAutoSetting = false;
|
|
|
|
|
|
|
|
function __construct($controller, $name, $sourceClass, $fieldList, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
|
|
|
|
parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/i18n.js");
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/HasManyFileField.js");
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/RelationComplexTableField.js');
|
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/HasManyFileField.css');
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
$this->Markable = true;
|
2008-10-08 05:35:27 +02:00
|
|
|
|
|
|
|
if($controllerClass = $this->controllerClass()) {
|
|
|
|
$this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
|
2009-01-08 00:00:54 +01:00
|
|
|
if(!$this->joinField) user_error("Can't find a has_one relationship from '$this->sourceClass' to '$controllerClass'", E_USER_WARNING);
|
2008-10-08 05:35:27 +02:00
|
|
|
} else {
|
|
|
|
user_error("Can't figure out the data class of $controller", E_USER_WARNING);
|
|
|
|
}
|
2008-10-03 02:47:08 +02:00
|
|
|
|
2008-11-12 05:31:33 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 05:35:27 +02:00
|
|
|
/**
|
|
|
|
* Try to determine the DataObject that this field is built on top of
|
|
|
|
*/
|
|
|
|
function controllerClass() {
|
|
|
|
if($this->controller instanceof DataObject) return $this->controller->class;
|
|
|
|
elseif($this->controller instanceof ContentController) return $this->controller->data()->class;
|
|
|
|
}
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
function getQuery($limitClause = null) {
|
|
|
|
if($this->customQuery) {
|
|
|
|
$query = $this->customQuery;
|
|
|
|
$query->select[] = "{$this->sourceClass}.ID AS ID";
|
|
|
|
$query->select[] = "{$this->sourceClass}.ClassName AS ClassName";
|
2008-11-24 00:28:16 +01:00
|
|
|
$query->select[] = "{$this->sourceClass}.ClassName AS \"RecordClassName\"";
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, $limitClause, $this->sourceJoin);
|
|
|
|
|
|
|
|
// Add more selected fields if they are from joined table.
|
|
|
|
|
|
|
|
$SNG = singleton($this->sourceClass);
|
|
|
|
foreach($this->FieldList() as $k => $title) {
|
|
|
|
if(! $SNG->hasField($k) && ! $SNG->hasMethod('get' . $k))
|
|
|
|
$query->select[] = $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clone $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sourceItems() {
|
2009-01-05 07:19:48 +01:00
|
|
|
if($this->sourceItems) return $this->sourceItems;
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
$limitClause = '';
|
2009-01-05 07:19:48 +01:00
|
|
|
if(isset($_REQUEST['ctf'][$this->Name()]['start']) && is_numeric($_REQUEST['ctf'][$this->Name()]['start'])) {
|
2008-09-26 04:22:51 +02:00
|
|
|
$limitClause = $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] . ", $this->pageSize";
|
2009-01-05 07:19:48 +01:00
|
|
|
} else {
|
2008-09-26 04:22:51 +02:00
|
|
|
$limitClause = "0, $this->pageSize";
|
2009-01-05 07:19:48 +01:00
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
$dataQuery = $this->getQuery($limitClause);
|
|
|
|
$records = $dataQuery->execute();
|
|
|
|
$items = new DataObjectSet();
|
2009-01-05 07:19:48 +01:00
|
|
|
|
|
|
|
$sourceClass = $this->sourceClass;
|
|
|
|
$dataobject = new $sourceClass();
|
|
|
|
$items = $dataobject->buildDataObjectSet($records, 'DataObjectSet');
|
2008-09-26 04:22:51 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$this->unpagedSourceItems = $dataobject->buildDataObjectSet($records, 'DataObjectSet');
|
2008-09-26 04:22:51 +02:00
|
|
|
|
|
|
|
$this->totalCount = ($this->unpagedSourceItems) ? $this->unpagedSourceItems->TotalItems() : null;
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getControllerID() {
|
|
|
|
return $this->controller->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveInto(DataObject $record) {
|
|
|
|
$fieldName = $this->name;
|
|
|
|
$saveDest = $record->$fieldName();
|
|
|
|
|
|
|
|
if(! $saveDest)
|
|
|
|
user_error("HasManyComplexTableField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
|
|
|
|
|
|
|
|
$items = array();
|
|
|
|
|
|
|
|
if($list = $this->value[ $this->htmlListField ]) {
|
|
|
|
if($list != 'undefined')
|
|
|
|
$items = explode(',', $list);
|
|
|
|
}
|
|
|
|
|
|
|
|
$saveDest->setByIDList($items);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAddTitle($addTitle) {
|
|
|
|
if(is_string($addTitle))
|
|
|
|
$this->addTitle = $addTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Title() {
|
|
|
|
return $this->addTitle ? $this->addTitle : parent::Title();
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExtraData() {
|
|
|
|
$items = array();
|
2009-01-05 07:19:48 +01:00
|
|
|
if($this->unpagedSourceItems) {
|
|
|
|
foreach($this->unpagedSourceItems as $item) {
|
|
|
|
if($item->{$this->joinField} == $this->controller->ID)
|
|
|
|
$items[] = $item->ID;
|
|
|
|
}
|
2008-09-26 04:22:51 +02:00
|
|
|
}
|
|
|
|
$list = implode(',', $items);
|
|
|
|
$inputId = $this->id() . '_' . $this->htmlListEndName;
|
|
|
|
return <<<HTML
|
|
|
|
<input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="$list"/>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Single record of a {@link HasManyComplexTableField} field.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
class HasManyComplexTableField_Item extends ComplexTableField_Item {
|
|
|
|
|
|
|
|
function MarkingCheckbox() {
|
|
|
|
$name = $this->parent->Name() . '[]';
|
|
|
|
|
2009-01-08 00:00:54 +01:00
|
|
|
if(!$this->parent->joinField) {
|
|
|
|
user_error("joinField not set in HasManyComplexTableField '{$this->parent->name}'", E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2008-09-26 04:22:51 +02:00
|
|
|
$joinVal = $this->item->{$this->parent->joinField};
|
|
|
|
$parentID = $this->parent->getControllerID();
|
|
|
|
|
|
|
|
if($this->parent->IsReadOnly || ($joinVal > 0 && $joinVal != $parentID))
|
|
|
|
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
|
|
|
|
else if($joinVal == $parentID)
|
|
|
|
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\"/>";
|
|
|
|
else
|
|
|
|
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\"/>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-04 05:30:59 +02:00
|
|
|
?>
|