2009-04-17 04:26:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Base class for multiple option fields such as {@link EditableDropdownField}
|
|
|
|
* and radio sets.
|
|
|
|
*
|
|
|
|
* Implemented as a class but should be viewed as abstract, you should
|
|
|
|
* instantiate a subclass such as {@link EditableDropdownField}
|
|
|
|
*
|
|
|
|
* @see EditableCheckboxGroupField
|
|
|
|
* @see EditableDropdownField
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EditableMultipleOptionField extends EditableFormField {
|
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $has_many = array(
|
2009-04-17 04:26:40 +02:00
|
|
|
"Options" => "EditableOption"
|
|
|
|
);
|
|
|
|
|
2009-09-23 03:36:52 +02:00
|
|
|
/**
|
|
|
|
* Publishing Versioning support.
|
|
|
|
*
|
|
|
|
* When publishing it needs to handle copying across / publishing
|
|
|
|
* each of the individual field options
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-12-12 09:59:20 +01:00
|
|
|
public function doPublish($fromStage, $toStage, $createNewVersion = false) {
|
2009-12-07 01:52:00 +01:00
|
|
|
$live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
|
|
|
|
|
2009-09-23 03:36:52 +02:00
|
|
|
if($live) {
|
|
|
|
foreach($live as $option) {
|
|
|
|
$option->delete();
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2009-09-23 03:36:52 +02:00
|
|
|
if($this->Options()) {
|
|
|
|
foreach($this->Options() as $option) {
|
|
|
|
$option->publish($fromStage, $toStage, $createNewVersion);
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 08:22:30 +01:00
|
|
|
|
2009-12-12 09:59:20 +01:00
|
|
|
$this->publish($fromStage, $toStage, $createNewVersion);
|
2009-09-23 03:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpublishing Versioning support
|
|
|
|
*
|
|
|
|
* When unpublishing the field it has to remove all options attached
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-12-12 09:59:20 +01:00
|
|
|
public function doDeleteFromStage($stage) {
|
2009-09-23 03:36:52 +02:00
|
|
|
if($this->Options()) {
|
|
|
|
foreach($this->Options() as $option) {
|
|
|
|
$option->deleteFromStage($stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-12 09:59:20 +01:00
|
|
|
$this->deleteFromStage($stage);
|
2009-09-23 03:36:52 +02:00
|
|
|
}
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Deletes all the options attached to this field before deleting the
|
|
|
|
* field. Keeps stray options from floating around
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete() {
|
2012-07-17 06:09:31 +02:00
|
|
|
$options = $this->Options();
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
if($options) {
|
|
|
|
foreach($options as $option) {
|
|
|
|
$option->delete();
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2012-07-17 06:09:31 +02:00
|
|
|
parent::delete();
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Duplicate a pages content. We need to make sure all the fields attached
|
|
|
|
* to that page go with it
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
2012-04-14 08:36:50 +02:00
|
|
|
* @return DataObject
|
2009-04-17 04:26:40 +02:00
|
|
|
*/
|
2012-05-04 03:59:10 +02:00
|
|
|
public function duplicate($doWrite = true) {
|
2009-04-17 04:26:40 +02:00
|
|
|
$clonedNode = parent::duplicate();
|
|
|
|
|
|
|
|
if($this->Options()) {
|
|
|
|
foreach($this->Options() as $field) {
|
|
|
|
$newField = $field->duplicate();
|
|
|
|
$newField->ParentID = $clonedNode->ID;
|
|
|
|
$newField->write();
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 08:36:50 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
return $clonedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* On before saving this object we need to go through and keep an eye on
|
|
|
|
* all our option fields that are related to this field in the form
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
2012-04-14 08:36:50 +02:00
|
|
|
* @param ArrayData
|
2009-04-17 04:26:40 +02:00
|
|
|
*/
|
|
|
|
public function populateFromPostData($data) {
|
|
|
|
parent::populateFromPostData($data);
|
|
|
|
|
|
|
|
// get the current options
|
|
|
|
$fieldSet = $this->Options();
|
2009-05-22 01:31:46 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
// go over all the current options and check if ID and Title still exists
|
|
|
|
foreach($fieldSet as $option) {
|
|
|
|
if(isset($data[$option->ID]) && isset($data[$option->ID]['Title']) && $data[$option->ID]['Title'] != "field-node-deleted") {
|
|
|
|
$option->populateFromPostData($data[$option->ID]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$option->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Return whether or not this field has addable options such as a
|
|
|
|
* {@link EditableDropdownField} or {@link EditableRadioField}
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-05-06 05:34:40 +02:00
|
|
|
public function getHasAddableOptions() {
|
2009-04-17 04:26:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-14 08:36:50 +02:00
|
|
|
* Return the form field for this object in the front end form view
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function getFormField() {
|
2009-04-27 08:00:05 +02:00
|
|
|
return user_error('Please implement getFormField() on '. $this->class, E_USER_ERROR);
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|