2009-04-17 04:26:40 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2019-03-12 21:20:51 +01:00
|
|
|
use SilverStripe\Security\Member;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
2015-09-11 00:20:06 +02:00
|
|
|
* Base Class for EditableOption Fields such as the ones used in
|
2009-04-17 04:26:40 +02:00
|
|
|
* dropdown fields and in radio check box groups
|
2015-09-11 00:20:06 +02:00
|
|
|
*
|
2015-12-22 23:14:36 +01:00
|
|
|
* @method EditableMultipleOptionField Parent()
|
2009-04-17 04:26:40 +02:00
|
|
|
* @package userforms
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableOption extends DataObject
|
|
|
|
{
|
2017-08-11 02:37:03 +02:00
|
|
|
private static $default_sort = 'Sort';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
private static $db = [
|
|
|
|
'Name' => 'Varchar(255)',
|
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'Default' => 'Boolean',
|
2017-08-11 01:33:06 +02:00
|
|
|
'Sort' => 'Int',
|
|
|
|
'Value' => 'Varchar(255)',
|
2017-08-11 02:37:03 +02:00
|
|
|
];
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
private static $has_one = [
|
|
|
|
'Parent' => EditableMultipleOptionField::class,
|
|
|
|
];
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
private static $extensions = [
|
2019-03-12 21:20:51 +01:00
|
|
|
Versioned::class . "('Stage', 'Live')",
|
2017-08-11 02:37:03 +02:00
|
|
|
];
|
2009-04-17 04:26:40 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
private static $summary_fields = [
|
|
|
|
'Title',
|
2019-03-12 21:20:51 +01:00
|
|
|
'Default',
|
2017-08-11 02:37:03 +02:00
|
|
|
];
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-04-20 00:40:37 +02:00
|
|
|
protected static $allow_empty_values = false;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $table_name = 'EditableOption';
|
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
/**
|
2016-04-20 00:40:37 +02:00
|
|
|
* Returns whether to allow empty values or not.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
public static function allow_empty_values()
|
|
|
|
{
|
2016-04-20 00:40:37 +02:00
|
|
|
return (bool) self::$allow_empty_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether to allow empty values.
|
|
|
|
*
|
|
|
|
* @param boolean $allow
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
public static function set_allow_empty_values($allow)
|
|
|
|
{
|
2016-04-20 00:40:37 +02:00
|
|
|
self::$allow_empty_values = (bool) $allow;
|
|
|
|
}
|
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
/**
|
2018-03-21 02:56:16 +01:00
|
|
|
* @deprecated 5.0..6.0 Use "$Title" in templates instead
|
2017-08-11 02:37:03 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
public function getEscapedTitle()
|
|
|
|
{
|
2018-03-21 02:56:16 +01:00
|
|
|
return Convert::raw2xml($this->Title);
|
2017-08-11 02:37:03 +02:00
|
|
|
}
|
2015-12-22 23:14:36 +01:00
|
|
|
|
2016-04-20 00:40:37 +02:00
|
|
|
/**
|
|
|
|
* Fetches a value for $this->Value. If empty values are not allowed,
|
|
|
|
* then this will return the title in the case of an empty value.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
$value = $this->getField('Value');
|
2016-07-21 07:53:59 +02:00
|
|
|
if (empty($value) && !self::allow_empty_values()) {
|
2016-04-20 00:40:37 +02:00
|
|
|
return $this->Title;
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2016-08-18 00:20:56 +02:00
|
|
|
|
2016-09-23 18:27:15 +02:00
|
|
|
protected function onBeforeWrite()
|
|
|
|
{
|
2016-07-21 08:08:03 +02:00
|
|
|
if (!$this->Sort) {
|
|
|
|
$this->Sort = EditableOption::get()->max('Sort') + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2019-03-12 21:20:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
return $this->Parent()->canEdit($member);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canView($member = null)
|
|
|
|
{
|
|
|
|
return $this->Parent()->canView($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can create an object of this type
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @param array $context Virtual parameter to allow context to be passed in to check
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canCreate($member = null, $context = [])
|
|
|
|
{
|
2019-03-12 22:18:41 +01:00
|
|
|
// Check parent object
|
|
|
|
$parent = $this->Parent();
|
2019-03-12 21:20:51 +01:00
|
|
|
if ($parent) {
|
2019-03-12 22:18:41 +01:00
|
|
|
return $parent->canCreate($member);
|
2019-03-12 21:20:51 +01:00
|
|
|
}
|
2019-03-12 22:18:41 +01:00
|
|
|
|
2019-03-12 21:20:51 +01:00
|
|
|
// Fall back to secure admin permissions
|
|
|
|
return parent::canCreate($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null)
|
|
|
|
{
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canUnpublish($member = null)
|
|
|
|
{
|
|
|
|
return $this->canDelete($member);
|
|
|
|
}
|
2013-05-08 09:08:21 +02:00
|
|
|
}
|