silverstripe-userforms/code/model/editableformfields/EditableOption.php

165 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/**
2015-09-11 00:20:06 +02:00
* Base Class for EditableOption Fields such as the ones used in
* dropdown fields and in radio check box groups
2015-09-11 00:20:06 +02:00
*
* @method EditableMultipleOptionField Parent()
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableOption extends DataObject
{
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $default_sort = "Sort";
2016-07-21 07:53:59 +02:00
private static $db = array(
"Name" => "Varchar(255)",
"Title" => "Varchar(255)",
"Default" => "Boolean",
2016-04-20 00:40:37 +02:00
"Sort" => "Int",
"Value" => "Varchar(255)",
2016-07-21 07:53:59 +02:00
);
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $has_one = array(
"Parent" => "EditableMultipleOptionField",
);
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
private static $extensions = array(
"Versioned('Stage', 'Live')"
);
2016-07-21 07:53:59 +02:00
private static $summary_fields = array(
'Title',
'Default'
);
2015-07-24 04:37:48 +02:00
2016-04-20 00:40:37 +02:00
protected static $allow_empty_values = false;
/**
* 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;
}
2016-07-21 07:53:59 +02: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);
}
public function getEscapedTitle()
{
return Convert::raw2att($this->Title);
}
/**
* @param Member $member
* @return bool
*/
2016-07-21 07:53:59 +02:00
public function canView($member = null)
{
return $this->Parent()->canView($member);
}
2016-07-21 07:53:59 +02:00
/**
* 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
2016-07-21 07:53:59 +02:00
* @return bool
*/
public function canCreate($member = null)
{
// Check parent page
$parent = $this->getCanCreateContext(func_get_args());
2016-07-21 07:53:59 +02:00
if ($parent) {
return $parent->canEdit($member);
}
// Fall back to secure admin permissions
return parent::canCreate($member);
2016-07-21 07:53:59 +02:00
}
/**
* Helper method to check the parent for this object
*
* @param array $args List of arguments passed to canCreate
* @return DataObject Some parent dataobject to inherit permissions from
*/
2016-07-21 07:53:59 +02:00
protected function getCanCreateContext($args)
{
// Inspect second parameter to canCreate for a 'Parent' context
2016-07-21 07:53:59 +02:00
if (isset($args[1]['Parent'])) {
return $args[1]['Parent'];
}
// Hack in currently edited page if context is missing
2016-07-21 07:53:59 +02:00
if (Controller::has_curr() && Controller::curr() instanceof CMSMain) {
return Controller::curr()->currentPage();
}
// No page being edited
return null;
}
/**
* @param Member $member
* @return bool
*/
2016-07-21 07:53:59 +02:00
public function canPublish($member = null)
{
return $this->canEdit($member);
}
/**
* @param Member $member
* @return bool
*/
2016-07-21 07:53:59 +02:00
public function canUnpublish($member = null)
{
return $this->canDelete($member);
}
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;
}
}