2007-08-18 04:33:28 +02:00
|
|
|
<?php
|
|
|
|
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2008-01-08 07:37:50 +01:00
|
|
|
*/
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
/**
|
|
|
|
* TableField behaves in the same manner as TableListField, however allows the addition of
|
|
|
|
* fields and editing of attributes specified, and filtering results.
|
|
|
|
*
|
|
|
|
* Caution: If you insert DropdownFields in the fieldTypes-array, make sure they have an empty first option.
|
|
|
|
* Otherwise the saving can't determine if a new row should really be saved.
|
|
|
|
*
|
|
|
|
* Caution: TableField relies on {@FormResponse} to reload the field after it is saved.
|
|
|
|
* A TableField-instance should never be saved twice without reloading, because otherwise it
|
|
|
|
* can't determine if a field is new (=create) or existing (=update), and will produce duplicates.
|
|
|
|
*
|
|
|
|
* @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 $fieldTypes array An array of field types of fieldname => fieldType (eg. formfield). Do not use for extra data/hiddenfields.
|
2007-11-25 10:48:25 +01:00
|
|
|
* @param $filterField string DEPRECATED The actual limiting filter, eg. 1 (please use $sourceFilter in the form "ParentID = 1" instead)
|
2007-08-18 04:33:28 +02:00
|
|
|
* @param $sourceFilter string The filter you wish to limit the objects by
|
|
|
|
* @param $editExisting boolean (Note: Has to stay on this position for legacy reasons)
|
|
|
|
* @param $sourceSort string
|
|
|
|
* @param $sourceJoin string
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @todo We should refactor this to support a single FieldSet instead of evaluated Strings for building FormFields
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2007-08-18 04:33:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class TableField extends TableListField {
|
|
|
|
|
|
|
|
protected $sourceClass;
|
|
|
|
|
|
|
|
protected $sourceFilter;
|
|
|
|
|
|
|
|
protected $fieldList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $fieldTypes FieldSet
|
|
|
|
* Caution: Use {@setExtraData()} instead of manually adding HiddenFields if you want to
|
|
|
|
* preset relations or other default data.
|
|
|
|
*/
|
|
|
|
protected $fieldTypes;
|
|
|
|
|
|
|
|
protected $sourceSort;
|
|
|
|
|
|
|
|
protected $sourceJoin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $template string Template-Overrides
|
|
|
|
*/
|
|
|
|
protected $template = "TableField";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $extraData array Any extra data that need to be included, e.g. to retain
|
|
|
|
* has-many relations. Format: array('FieldName' => 'Value')
|
|
|
|
*/
|
|
|
|
protected $extraData;
|
|
|
|
|
|
|
|
protected $tempForm;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Influence output without having to subclass the template.
|
|
|
|
*/
|
|
|
|
protected $permissions = array(
|
|
|
|
"edit",
|
|
|
|
"delete",
|
|
|
|
"add",
|
|
|
|
//"export",
|
|
|
|
);
|
|
|
|
|
|
|
|
public $transformationConditions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $requiredFields array Required fields as a numerical array.
|
|
|
|
* Please use an instance of Validator on the including
|
|
|
|
* form.
|
|
|
|
*/
|
|
|
|
protected $requiredFields = null;
|
|
|
|
|
|
|
|
/**
|
2008-01-16 21:34:40 +01:00
|
|
|
* Shows a row of empty fields for adding a new record
|
|
|
|
* (turned on by default).
|
|
|
|
* Please use {@link TableField::$permissions} to control
|
|
|
|
* if the "add"-functionality incl. button is shown at all.
|
|
|
|
*
|
|
|
|
* @param boolean $showAddRow
|
2007-08-18 04:33:28 +02:00
|
|
|
*/
|
2008-01-16 21:34:40 +01:00
|
|
|
public $showAddRow = true;
|
2007-11-25 10:48:25 +01:00
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
function __construct($name, $sourceClass, $fieldList, $fieldTypes, $filterField = null,
|
|
|
|
$sourceFilter = null, $editExisting = true, $sourceSort = null, $sourceJoin = null) {
|
|
|
|
|
|
|
|
$this->fieldTypes = $fieldTypes;
|
|
|
|
$this->filterField = $filterField;
|
2007-11-22 03:17:16 +01:00
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
$this->editExisting = $editExisting;
|
2007-08-18 04:33:28 +02:00
|
|
|
parent::__construct($name, $sourceClass, $fieldList, $sourceFilter, $sourceSort, $sourceJoin);
|
|
|
|
|
|
|
|
Requirements::javascript('sapphire/javascript/TableField.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the headings on the template
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
function Headings() {
|
|
|
|
$i=0;
|
|
|
|
foreach($this->fieldList as $fieldName => $fieldTitle) {
|
|
|
|
$extraClass = "col".$i;
|
|
|
|
$class = $this->fieldTypes[$fieldName];
|
|
|
|
if(is_object($class)) $class = "";
|
|
|
|
$class = $class." ".$extraClass;
|
2007-11-25 10:48:25 +01:00
|
|
|
$headings[] = new ArrayData(array("Name" => $fieldName, "Title" => $fieldTitle, "Class" => $class));
|
2007-08-18 04:33:28 +02:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return new DataObjectSet($headings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the number of columns needed for colspans
|
|
|
|
* used in template
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function ItemCount() {
|
|
|
|
return count($this->fieldList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the databased saved items, from DataObjects
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
function sourceItems() {
|
|
|
|
if($this->customSourceItems) {
|
|
|
|
$items = $this->customSourceItems;
|
|
|
|
} elseif($this->cachedSourceItems) {
|
|
|
|
$items = $this->cachedSourceItems;
|
|
|
|
} else {
|
|
|
|
if(!empty($this->filterField) && intval($this->sourceFilter) > 0) {
|
|
|
|
// Legacy: If a filterField is specified and the sourceFilter is a valid ID (old format)
|
|
|
|
$SQL_filter = Convert::raw2sql($this->sourceFilter);
|
|
|
|
$SQL_filterField = $this->filterField;
|
2007-11-25 10:48:25 +01:00
|
|
|
$items = DataObject::get($this->sourceClass,"`$SQL_filterField` = '$SQL_filter'", $this->sourceSort, $this->sourceJoin);
|
2007-08-18 04:33:28 +02:00
|
|
|
} else {
|
|
|
|
// get query
|
|
|
|
$dataQuery = $this->getQuery();
|
|
|
|
// get data
|
|
|
|
$records = $dataQuery->execute();
|
2007-11-25 10:48:25 +01:00
|
|
|
$items = singleton($this->sourceClass)->buildDataObjectSet($records);
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the items from sourceItems using the encapsulation object
|
|
|
|
*
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
function Items() {
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
if($items = $this->sourceItems()) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
// Load the data in to a temporary form (for correct field types)
|
|
|
|
$fieldset = $this->FieldSetForRow();
|
|
|
|
if($fieldset){
|
|
|
|
$form = new Form(null, null, $fieldset, new FieldSet());
|
|
|
|
$form->loadDataFrom($item);
|
|
|
|
// Add the item to our new DataObjectSet, with a wrapper class.
|
|
|
|
$output->push(new TableField_Item($item, $this, $form, $this->fieldTypes));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create a temporary DataObject
|
|
|
|
if($this->Can('add')) {
|
2008-01-16 21:34:40 +01:00
|
|
|
if($this->showAddRow){
|
2007-08-18 04:33:28 +02:00
|
|
|
$output->push(new TableField_Item(null, $this, null, $this->fieldTypes, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all fields for each row contained in the TableField.
|
|
|
|
* Does not include the empty row.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function FieldSet() {
|
|
|
|
$fields = array ();
|
|
|
|
if($items = $this->sourceItems()) {
|
|
|
|
foreach($items as $item) {
|
|
|
|
// Load the data in to a temporary form (for correct field types)
|
|
|
|
$fieldset = $this->FieldSetForRow();
|
|
|
|
if ($fieldset)
|
|
|
|
{
|
|
|
|
// TODO Needs to be attached to a form existing in the DOM-tree
|
|
|
|
$form = new Form($this, 'EditForm', $fieldset, new FieldSet());
|
2007-11-25 10:48:25 +01:00
|
|
|
$form->loadDataFrom($item);
|
2007-08-18 04:33:28 +02:00
|
|
|
$row = new TableField_Item($item, $this, $form, $this->fieldTypes);
|
|
|
|
$fields = array_merge($fields, $row->Fields()->toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
function SubmittedFieldSet(&$sourceItems){
|
|
|
|
$fields = array ();
|
|
|
|
if($rows = $_POST[$this->name]){
|
|
|
|
if(count($rows)){
|
|
|
|
foreach($rows as $idx => $row){
|
|
|
|
if($idx == 'new'){
|
|
|
|
$newitems = ArrayLib::invert($row);
|
|
|
|
if(count($newitems)){
|
|
|
|
$sourceItems = new DataObjectSet();
|
|
|
|
foreach($newitems as $k => $newitem){
|
|
|
|
$fieldset = $this->FieldSetForRow();
|
|
|
|
if($fieldset){
|
|
|
|
$newitem[ID] = "new".$k;
|
|
|
|
foreach($newitem as $k => $v){
|
|
|
|
if(array_key_exists($k, $this->extraData)){
|
|
|
|
unset($newitem[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sourceItem = new DataObject($newitem);
|
|
|
|
if(!$sourceItem->isEmpty()){
|
|
|
|
$sourceItems->push($sourceItem);
|
|
|
|
$form = new Form($this, "EditForm", $fieldset, new FieldSet());
|
|
|
|
$form->loadDataFrom($sourceItem);
|
|
|
|
$item = new TableField_Item($sourceItem, $this, $form, $this->fieldTypes);
|
|
|
|
$fields = array_merge($fields, $item->Fields()->toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function FieldList() {
|
|
|
|
return $this->fieldList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the Dataobjects contained in the field
|
|
|
|
*/
|
|
|
|
function saveInto(DataObject $record) {
|
|
|
|
// CMS sometimes tries to set the value to one.
|
|
|
|
if(is_array($this->value)){
|
2007-11-25 10:48:25 +01:00
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
// Sort into proper array
|
|
|
|
$this->value = ArrayLib::invert($this->value);
|
2007-11-25 10:48:25 +01:00
|
|
|
$dataObjects = $this->sortData($this->value, $record->ID);
|
2007-08-18 04:33:28 +02:00
|
|
|
if($dataObjects['new']) {
|
2007-11-25 10:48:25 +01:00
|
|
|
$newFields = $this->sortData($dataObjects['new'], $record->ID);
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2007-11-25 10:48:25 +01:00
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
$savedObj = $this->saveData($dataObjects, $this->editExisting);
|
|
|
|
if($savedObj && $newFields) {
|
|
|
|
$savedObj += $this->saveData($newFields,false);
|
|
|
|
} else if($newFields) {
|
|
|
|
$savedObj = $this->saveData($newFields,false);
|
|
|
|
}
|
|
|
|
$items = $this->sourceItems();
|
|
|
|
FormResponse::update_dom_id($this->id(), $this->FieldHolder());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all fields in a single row.
|
|
|
|
*
|
|
|
|
* @return FieldSet
|
|
|
|
*/
|
|
|
|
function FieldSetForRow() {
|
|
|
|
$fieldset = new FieldSet();
|
|
|
|
if($this->fieldTypes){
|
|
|
|
foreach($this->fieldTypes as $key => $fieldType) {
|
|
|
|
if(isset($fieldType->class) && is_subclass_of($fieldType, 'FormField')) {
|
|
|
|
// using clone, otherwise we would just add stuff to the same field-instance
|
|
|
|
$field = clone $fieldType;
|
|
|
|
} elseif(strpos($fieldType, '(') === false) {
|
|
|
|
$field = new $fieldType($key);
|
|
|
|
} else {
|
|
|
|
$fieldName = $key;
|
|
|
|
$fieldTitle = "";
|
|
|
|
$field = eval("return new $fieldType;");
|
|
|
|
}
|
|
|
|
if($this->IsReadOnly || !$this->Can('edit')) {
|
|
|
|
$field = $field->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
$fieldset->push($field);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
USER_ERROR("TableField::FieldSetForRow() - Fieldtypes were not specified",E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fieldset;
|
|
|
|
}
|
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$this->permissions = array('show');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function performDisabledTransformation() {
|
|
|
|
$this->permissions = array('show');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
/**
|
|
|
|
* Needed for Form->callfieldmethod.
|
|
|
|
*/
|
|
|
|
public function getField($fieldName, $combinedFieldName = null) {
|
|
|
|
$fieldSet = $this->FieldSetForRow();
|
|
|
|
$field = $fieldSet->dataFieldByName($fieldName);
|
|
|
|
if(!$field) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($combinedFieldName) {
|
|
|
|
$field->Name = $combinedFieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on save, it creates the appropriate objects and writes them
|
|
|
|
* to the database.
|
|
|
|
*/
|
|
|
|
function saveData($dataObjects,$ExistingValues = true){
|
|
|
|
$savedObj = array();
|
|
|
|
$fieldset = $this->FieldSetForRow();
|
|
|
|
|
|
|
|
// add hiddenfields
|
|
|
|
if($this->extraData) {
|
2007-11-25 10:48:25 +01:00
|
|
|
foreach($this->extraData as $fieldName => $fieldValue) {
|
|
|
|
$fieldset->push(new HiddenField($fieldName));
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form = new Form(null, null, $fieldset, new FieldSet());
|
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
if($dataObjects) {
|
|
|
|
foreach ($dataObjects as $objectid => $fieldValues) {
|
|
|
|
// we have to "sort" new data first, and process it in a seperate saveData-call (see setValue())
|
|
|
|
if($objectid === "new") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra data was creating fields, but
|
|
|
|
if($this->extraData) {
|
|
|
|
$fieldValues = array_merge( $this->extraData, $fieldValues );
|
|
|
|
}
|
|
|
|
|
|
|
|
$hasData = false;
|
|
|
|
$obj = new $this->sourceClass();
|
|
|
|
|
|
|
|
if($ExistingValues) {
|
|
|
|
$obj->ID = $objectid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy: Use the filter as a predefined relationship-ID
|
|
|
|
if(!empty($this->filterField) && intval($this->sourceFilter) > 0) {
|
|
|
|
$filterField = $this->filterField;
|
|
|
|
$obj->$filterField = $this->sourceFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if there is changed data for saving
|
|
|
|
$dataFields = array();
|
|
|
|
|
|
|
|
foreach($fieldValues as $type => $value) {
|
|
|
|
if(is_array($this->extraData)){ // if the field is an actual datafield (not a preset hiddenfield)
|
|
|
|
if(!in_array($type, array_keys($this->extraData))){
|
|
|
|
$dataFields[$type] = $value;
|
|
|
|
}
|
|
|
|
}else{ // all fields are real
|
|
|
|
$dataFields[$type] = $value;
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 03:17:16 +01:00
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
$dataValues = ArrayLib::array_values_recursive($dataFields);
|
2007-08-18 04:33:28 +02:00
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
foreach($dataValues as $value) {
|
|
|
|
if(!empty($value)) {
|
|
|
|
$hasData = true;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-22 03:17:16 +01:00
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
// save
|
|
|
|
if($hasData) {
|
|
|
|
$form->loadDataFrom($fieldValues, true);
|
|
|
|
$form->saveInto($obj);
|
|
|
|
|
|
|
|
$objectid = $obj->write();
|
|
|
|
|
|
|
|
$savedObj[$objectid] = "Updated";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return $savedObj;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* organises the data in the appropriate manner for saving
|
|
|
|
*/
|
2007-11-25 10:48:25 +01:00
|
|
|
function sortData($data, $recordID = null) {
|
|
|
|
$dataObjects = array();
|
2007-08-18 04:33:28 +02:00
|
|
|
if($data) {
|
2007-11-25 10:48:25 +01:00
|
|
|
$dataObjects = array();
|
2007-08-18 04:33:28 +02:00
|
|
|
foreach($data as $field => $rowData) {
|
|
|
|
$i = 0;
|
|
|
|
$blank = 0;
|
|
|
|
if(!is_array($rowData)) continue;
|
|
|
|
foreach($rowData as $id => $value) {
|
2007-11-25 10:48:25 +01:00
|
|
|
if($value == '$RecordID') $value = $recordID;
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
if($value){
|
|
|
|
$dataObjects[$id][$field] = $value;
|
|
|
|
}else{
|
|
|
|
$blank++;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ADD stuff for removing rows with incomplete data
|
|
|
|
}
|
2007-11-25 10:48:25 +01:00
|
|
|
return $dataObjects;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $extraData array
|
|
|
|
*/
|
|
|
|
function setExtraData($extraData) {
|
|
|
|
$this->extraData = $extraData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getExtraData() {
|
|
|
|
return $this->extraData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the template to be rendered with
|
|
|
|
*/
|
|
|
|
function FieldHolder() {
|
|
|
|
return $this->renderWith($this->template);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Int
|
|
|
|
*/
|
|
|
|
function sourceID() {
|
|
|
|
return $this->filterField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function delete() {
|
2007-11-25 10:48:25 +01:00
|
|
|
$childId = Convert::raw2sql($_REQUEST['childID']);
|
2007-08-18 04:33:28 +02:00
|
|
|
if (is_numeric($childId)) {
|
|
|
|
$childObject = DataObject::get_by_id($this->sourceClass, $childId);
|
|
|
|
if($childObject) {
|
|
|
|
$childObject->delete();
|
2007-11-25 10:48:25 +01:00
|
|
|
return 1;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTransformationConditions($conditions) {
|
|
|
|
$this->transformationConditions = $conditions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validation
|
|
|
|
*/
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$js = "";
|
|
|
|
|
|
|
|
$fields = $this->FieldSet();
|
2007-11-25 10:48:25 +01:00
|
|
|
$fields = new FieldSet($fields);
|
2007-08-18 04:33:28 +02:00
|
|
|
// TODO doesn't automatically update validation when adding a row
|
|
|
|
foreach($fields as $field) {
|
|
|
|
//if the field type has some special specific specification for validation of itself
|
2007-11-25 10:48:25 +01:00
|
|
|
$js .= $field->jsValidation($this->form->class."_".$this->form->Name());
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Implement custom requiredFields
|
2007-11-25 10:48:25 +01:00
|
|
|
$items = $this->sourceItems();
|
2008-01-16 22:50:18 +01:00
|
|
|
if($items && $this->requiredFields && $items->count()) {
|
2007-08-18 04:33:28 +02:00
|
|
|
foreach ($this->requiredFields as $field) {
|
2007-11-25 10:48:25 +01:00
|
|
|
/*if($fields->dataFieldByName($field)) {
|
2007-08-18 04:33:28 +02:00
|
|
|
$js .= "\t\t\t\t\trequire('$field');\n";
|
2007-11-25 10:48:25 +01:00
|
|
|
}*/
|
|
|
|
foreach($items as $item){
|
|
|
|
$cellName = $this->Name().'['.$item->ID.']['.$field.']';
|
|
|
|
$js .= "\n";
|
|
|
|
if($fields->dataFieldByName($cellName)) {
|
|
|
|
$js .= <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$cellName')
|
|
|
|
require(fromAnOnBlur);
|
|
|
|
}else{
|
|
|
|
require('$cellName');
|
|
|
|
}
|
|
|
|
JS;
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $js;
|
|
|
|
}
|
|
|
|
|
|
|
|
function php($data) {
|
|
|
|
$valid = true;
|
2007-11-25 10:48:25 +01:00
|
|
|
|
|
|
|
if($data['methodName'] != 'delete'){
|
|
|
|
$fields = $this->FieldSet();
|
|
|
|
$fields = new FieldSet($fields);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
return $valid;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2007-11-25 10:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate($validator){
|
|
|
|
$valid = true;
|
|
|
|
$fields = $this->SubmittedFieldSet($sourceItemsNew);
|
2007-08-18 04:33:28 +02:00
|
|
|
$fields = new FieldSet($fields);
|
2007-11-25 10:48:25 +01:00
|
|
|
foreach($fields as $field){
|
|
|
|
$valid = $field->validate($validator)&&$valid;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
//debug::show($this->form->Message());
|
|
|
|
if($this->requiredFields&&$sourceItemsNew&&$sourceItemsNew->count()) {
|
|
|
|
foreach ($this->requiredFields as $field) {
|
|
|
|
foreach($sourceItemsNew as $item){
|
|
|
|
$cellName = $this->Name().'['.$item->ID.']['.$field.']';
|
|
|
|
$cName = $this->Name().'[new]['.$field.'][]';
|
|
|
|
|
|
|
|
if($fieldObj = $fields->dataFieldByName($cellName)) {
|
|
|
|
if(!trim($fieldObj->Value())){
|
|
|
|
$title = $fieldObj->Title();
|
|
|
|
$errorMessage .= sprintf(
|
|
|
|
_t('TableField.ISREQUIRED', "In %s '%s' is required."),
|
|
|
|
$this->name,
|
|
|
|
$title
|
|
|
|
);
|
|
|
|
$errorMessage .= "<br />";
|
|
|
|
}
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-25 10:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($errorMessage){
|
|
|
|
$messageType .= "validation";
|
|
|
|
$message .= "<br />".$errorMessage;
|
2007-08-18 04:33:28 +02:00
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
$validator->validationError($this->name, $message, $messageType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setRequiredFields($fields) {
|
|
|
|
$this->requiredFields = $fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-09 05:18:36 +01:00
|
|
|
* Single record in a TableField.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
* @see TableField
|
2007-08-18 04:33:28 +02:00
|
|
|
*/
|
|
|
|
class TableField_Item extends TableListField_Item {
|
|
|
|
|
|
|
|
protected $fields;
|
|
|
|
|
|
|
|
protected $data;
|
|
|
|
|
|
|
|
protected $fieldTypes;
|
|
|
|
|
|
|
|
protected $isAddRow;
|
|
|
|
|
|
|
|
protected $extraData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Each row contains a dataobject with any number of attributes
|
|
|
|
* @param $ID int The ID of the record
|
|
|
|
* @param $form Form A Form object containing all of the fields for this item. The data should be loaded in
|
|
|
|
* @param $fieldTypes array An array of name => fieldtype for use when creating a new field
|
|
|
|
* @param $parent TableListField The parent table for quick reference of names, and id's for storing values.
|
|
|
|
*/
|
|
|
|
function __construct($item = null, $parent, $form, $fieldTypes, $isAddRow = false) {
|
|
|
|
$this->data = $form;
|
|
|
|
$this->fieldTypes = $fieldTypes;
|
|
|
|
$this->isAddRow = $isAddRow;
|
|
|
|
$this->item = $item;
|
|
|
|
|
|
|
|
parent::__construct(($this->item) ? $this->item : new DataObject(), $parent);
|
|
|
|
|
|
|
|
$this->fields = $this->createFields();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Represents each cell of the table with an attribute
|
|
|
|
*/
|
|
|
|
function createFields() {
|
|
|
|
// Existing record
|
|
|
|
if($this->item && $this->data) {
|
|
|
|
$form = $this->data;
|
|
|
|
$this->fieldset = $form->Fields();
|
|
|
|
if($this->fieldset) {
|
|
|
|
$i=0;
|
|
|
|
foreach($this->fieldset as $field) {
|
2007-11-22 03:17:16 +01:00
|
|
|
$origFieldName = $field->Name();
|
2007-11-25 10:48:25 +01:00
|
|
|
|
|
|
|
// set unique fieldname with id
|
|
|
|
$combinedFieldName = $this->parent->Name() . "[" . $this->ID . "][" . $origFieldName . "]";
|
|
|
|
if($this->isAddRow) $combinedFieldName .= '[]';
|
|
|
|
|
|
|
|
// get value
|
|
|
|
if(strpos($origFieldName,'.') === false) {
|
|
|
|
$value = $field->dataValue();
|
|
|
|
} else {
|
|
|
|
// this supports the syntax fieldName = Relation.RelatedField
|
|
|
|
$fieldNameParts = explode('.', $origFieldName) ;
|
|
|
|
$tmpItem = $this->item;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
$field->Name = $combinedFieldName;
|
2007-11-25 10:48:25 +01:00
|
|
|
$field->setValue($field->dataValue());
|
2008-02-20 10:39:17 +01:00
|
|
|
$field->addExtraClass('col'.$i);
|
2007-08-18 04:33:28 +02:00
|
|
|
$field->setForm($this->data);
|
|
|
|
|
|
|
|
// transformation
|
|
|
|
if(isset($this->parent->transformationConditions[$origFieldName])) {
|
|
|
|
$transformation = $this->parent->transformationConditions[$origFieldName]['transformation'];
|
|
|
|
$rule = str_replace("\$","\$this->item->", $this->parent->transformationConditions[$origFieldName]['rule']);
|
|
|
|
$ruleApplies = null;
|
|
|
|
eval('$ruleApplies = ('.$rule.');');
|
|
|
|
if($ruleApplies) {
|
|
|
|
$field = $field->$transformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-11-25 10:48:25 +01:00
|
|
|
// formatting
|
|
|
|
$item = $this->item;
|
|
|
|
$value = $field->Value();
|
|
|
|
if(array_key_exists($origFieldName, $this->parent->fieldFormatting)) {
|
|
|
|
$format = str_replace('$value', "__VAL__", $this->parent->fieldFormatting[$origFieldName]);
|
|
|
|
$format = preg_replace('/\$([A-Za-z0-9-_]+)/','$item->$1', $format);
|
|
|
|
$format = str_replace('__VAL__', '$value', $format);
|
|
|
|
eval('$value = "' . $format . '";');
|
|
|
|
$field->dontEscape = true;
|
|
|
|
$field->setValue($value);
|
|
|
|
}
|
|
|
|
|
2007-08-18 04:33:28 +02:00
|
|
|
$this->fields[] = $field;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// New record
|
|
|
|
} else {
|
|
|
|
$list = $this->parent->FieldList();
|
2007-11-25 10:48:25 +01:00
|
|
|
$i=0;
|
|
|
|
foreach($list as $fieldName => $fieldTitle) {
|
|
|
|
if(strpos($fieldName, ".")) {
|
|
|
|
$shortFieldName = substr($fieldName, strpos($fieldName, ".")+1, strlen($fieldName));
|
|
|
|
} else {
|
|
|
|
$shortFieldName = $fieldName;
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
$combinedFieldName = $this->parent->Name() . "[new][" . $shortFieldName . "][]";
|
2007-11-25 10:48:25 +01:00
|
|
|
$fieldType = $this->fieldTypes[$fieldName];
|
2007-08-18 04:33:28 +02:00
|
|
|
if(isset($fieldType->class) && is_subclass_of($fieldType, 'FormField')) {
|
2007-11-25 10:48:25 +01:00
|
|
|
$field = clone $fieldType; // we can't use the same instance all over, as we change names
|
|
|
|
$field->Name = $combinedFieldName;
|
2007-08-18 04:33:28 +02:00
|
|
|
} elseif(strpos($fieldType, '(') === false) {
|
|
|
|
//echo ("<li>Type: ".$fieldType." fieldName: ". $filedName. " Title: ".$fieldTitle."</li>");
|
2007-11-25 10:48:25 +01:00
|
|
|
$field = new $fieldType($combinedFieldName,$fieldTitle);
|
2007-08-18 04:33:28 +02:00
|
|
|
} else {
|
2007-11-25 10:48:25 +01:00
|
|
|
$field = eval("return new " . $fieldType . ";");
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2008-02-20 10:39:17 +01:00
|
|
|
$field->addExtraClass('col'.$i);
|
2007-11-25 10:48:25 +01:00
|
|
|
$this->fields[] = $field;
|
|
|
|
$i++;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataObjectSet($this->fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Fields() {
|
|
|
|
return $this->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExtraData() {
|
|
|
|
$content = "";
|
|
|
|
$id = ($this->item->ID) ? $this->item->ID : "new";
|
|
|
|
if($this->parent->getExtraData()) {
|
|
|
|
foreach($this->parent->getExtraData() as $fieldName=>$fieldValue) {
|
|
|
|
$name = $this->parent->Name() . "[" . $id . "][" . $fieldName . "]";
|
|
|
|
if($this->isAddRow) $name .= '[]';
|
|
|
|
$field = new HiddenField($name, null, $fieldValue);
|
|
|
|
$content .= $field->FieldHolder() . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Can($mode) {
|
|
|
|
return $this->parent->Can($mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Parent() {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the base link for the call below.
|
|
|
|
*/
|
|
|
|
function BaseLink() {
|
|
|
|
$parent = $this->parent;
|
2007-11-25 10:48:25 +01:00
|
|
|
$action = $parent->FormAction();
|
|
|
|
if(substr($action, -1, 1) !== '&'){
|
|
|
|
$action = $action."&";
|
|
|
|
}
|
|
|
|
$action = str_replace('&', '&', $action);
|
|
|
|
|
|
|
|
return $action . "action_callfieldmethod=1&fieldName=". $parent->Name() . "&childID=" . $this->ID;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Runs the delete() method on the Tablefield parent.
|
|
|
|
* Allows the deletion of objects via ajax
|
|
|
|
*/
|
|
|
|
function DeleteLink() {
|
2007-11-25 10:48:25 +01:00
|
|
|
return $this->BaseLink() . "&methodName=delete";
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-11-25 10:48:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
?>
|