2012-04-18 23:15:45 +02:00
|
|
|
<?php
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* Widgets let CMS authors drag and drop small pieces of functionality into
|
2012-04-18 23:15:45 +02:00
|
|
|
* defined areas of their websites.
|
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* You can use forms in widgets by implementing a {@link WidgetController}.
|
|
|
|
*
|
2012-04-18 23:15:45 +02:00
|
|
|
* See {@link Widget_Controller} for more information.
|
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* @package widgets
|
2012-04-18 23:15:45 +02:00
|
|
|
*/
|
|
|
|
class Widget extends DataObject {
|
2012-10-05 23:39:49 +02:00
|
|
|
|
2012-10-05 23:47:26 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $db = array(
|
2015-03-06 01:33:26 +01:00
|
|
|
"Title" => "Varchar(255)",
|
2012-04-18 23:15:45 +02:00
|
|
|
"Sort" => "Int",
|
2015-03-06 01:33:26 +01:00
|
|
|
"Enabled" => "Boolean",
|
2012-04-18 23:15:45 +02:00
|
|
|
);
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $defaults = array(
|
2015-03-06 01:33:26 +01:00
|
|
|
'Enabled' => true,
|
2012-04-18 23:15:45 +02:00
|
|
|
);
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-06 01:33:26 +01:00
|
|
|
private static $casting = array(
|
|
|
|
'CMSTitle' => 'Text',
|
|
|
|
'Description' => 'Text',
|
2012-04-18 23:15:45 +02:00
|
|
|
);
|
2015-03-06 01:33:26 +01:00
|
|
|
|
|
|
|
private static $only_available_in = array();
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-06 01:33:26 +01:00
|
|
|
private static $has_one = array(
|
|
|
|
"Parent" => "WidgetArea",
|
|
|
|
);
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $default_sort = "\"Sort\"";
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $title = "Widget Title";
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $cmsTitle = "Name of this widget";
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-04-01 21:37:44 +02:00
|
|
|
private static $description = "Description of what this widget does.";
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2013-07-24 03:44:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $summary_fields = array(
|
|
|
|
'CMSTitle' => 'Title'
|
|
|
|
);
|
|
|
|
|
2013-05-07 10:56:38 +02:00
|
|
|
/**
|
|
|
|
* @var WidgetController
|
|
|
|
*/
|
|
|
|
protected $controller;
|
2015-03-06 01:33:26 +01:00
|
|
|
|
|
|
|
public function populateDefaults() {
|
|
|
|
parent::populateDefaults();
|
|
|
|
$this->setField('Title', $this->getTitle());
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* Note: Overloaded in {@link WidgetController}.
|
2012-04-18 23:15:45 +02:00
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function WidgetHolder() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith("WidgetHolder");
|
|
|
|
}
|
2013-07-24 03:49:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default way to render widget in templates.
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
public function forTemplate($holder = true){
|
|
|
|
if($holder){
|
|
|
|
return $this->WidgetHolder();
|
|
|
|
}
|
|
|
|
return $this->Content();
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* Renders the widget content in a custom template with the same name as the
|
|
|
|
* current class. This should be the main point of output customization.
|
2012-04-18 23:15:45 +02:00
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* Invoked from within WidgetHolder.ss, which contains the "framing" around
|
|
|
|
* the custom content, like a title.
|
2012-04-18 23:15:45 +02:00
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* Note: Overloaded in {@link WidgetController}.
|
2012-04-18 23:15:45 +02:00
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Content() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
2015-03-06 01:33:26 +01:00
|
|
|
* @deprecated
|
2012-10-05 23:47:26 +02:00
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Title() {
|
2015-03-06 01:33:26 +01:00
|
|
|
return $this->getTitle();
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-06 01:33:26 +01:00
|
|
|
* Get the frontend title for this widget
|
|
|
|
*
|
2012-10-05 23:47:26 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-03-06 01:33:26 +01:00
|
|
|
public function getTitle() {
|
|
|
|
return $this->getField('Title')
|
|
|
|
?: _t($this->class.'.TITLE', $this->config()->title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function CMSTitle() {
|
2015-03-06 01:33:26 +01:00
|
|
|
return $this->getCMSTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCMSTitle() {
|
2013-03-20 14:00:54 +01:00
|
|
|
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
2015-03-06 01:33:26 +01:00
|
|
|
* @deprecated
|
2012-10-05 23:47:26 +02:00
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Description() {
|
2015-03-06 01:33:26 +01:00
|
|
|
return $this->getDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDescription() {
|
2013-03-20 14:00:54 +01:00
|
|
|
return _t($this->class.'.DESCRIPTION', $this->config()->description);
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string - HTML
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function DescriptionSegment() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith('WidgetDescription');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* @see WidgetController::editablesegment()
|
|
|
|
*
|
2012-10-05 23:47:26 +02:00
|
|
|
* @return string - HTML
|
2012-04-18 23:15:45 +02:00
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function EditableSegment() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith('WidgetEditor');
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
2014-07-01 23:14:12 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields() {
|
2015-03-06 01:33:26 +01:00
|
|
|
$fields = new FieldList(
|
|
|
|
new TextField('Title', $this->fieldLabel('Title'), null, 255),
|
|
|
|
new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
|
|
|
|
);
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
2014-07-01 23:14:12 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2012-10-05 23:47:26 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function CMSEditor() {
|
2012-04-18 23:15:45 +02:00
|
|
|
$fields = $this->getCMSFields();
|
2012-08-30 08:56:00 +02:00
|
|
|
$outputFields = new FieldList();
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
foreach($fields as $field) {
|
2012-06-19 02:27:35 +02:00
|
|
|
$name = $field->getName();
|
2012-08-30 08:56:00 +02:00
|
|
|
$value = $this->getField($name);
|
|
|
|
if ($value) {
|
|
|
|
$field->setValue($value);
|
|
|
|
}
|
|
|
|
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
|
|
|
|
$field->setName($name);
|
|
|
|
$outputFields->push($field);
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-08-30 08:56:00 +02:00
|
|
|
return $outputFields;
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function ClassName() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->class;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Name() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return "Widget[".$this->ID."]";
|
|
|
|
}
|
|
|
|
|
2012-10-05 23:47:26 +02:00
|
|
|
/**
|
2013-05-07 10:56:38 +02:00
|
|
|
* @throws Exception
|
2012-10-05 23:47:26 +02:00
|
|
|
*
|
2013-05-07 10:56:38 +02:00
|
|
|
* @return WidgetController
|
|
|
|
*/
|
|
|
|
public function getController() {
|
|
|
|
if($this->controller) {
|
|
|
|
return $this->controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
|
|
|
|
$controllerClass = "{$widgetClass}_Controller";
|
|
|
|
|
|
|
|
if(class_exists($controllerClass)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$controllerClass = "{$widgetClass}Controller";
|
|
|
|
|
|
|
|
if(class_exists($controllerClass)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!class_exists($controllerClass)) {
|
|
|
|
throw new Exception("Could not find controller class for $this->classname");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->controller = Injector::inst()->create($controllerClass, $this);
|
|
|
|
|
|
|
|
return $this->controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-05 23:47:26 +02:00
|
|
|
* @param array $data
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function populateFromPostData($data) {
|
2012-08-30 08:56:00 +02:00
|
|
|
$fields = $this->getCMSFields();
|
2012-04-18 23:15:45 +02:00
|
|
|
foreach($data as $name => $value) {
|
|
|
|
if($name != "Type") {
|
2012-08-30 08:56:00 +02:00
|
|
|
if ($field = $fields->dataFieldByName($name)) {
|
|
|
|
$field->setValue($value);
|
|
|
|
$field->saveInto($this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->setField($name, $value);
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 20:33:02 +02:00
|
|
|
//Look for checkbox fields not present in the data
|
|
|
|
foreach($fields as $field) {
|
|
|
|
if($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
|
|
|
|
$field->setValue(false);
|
|
|
|
$field->saveInto($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
$this->write();
|
|
|
|
|
|
|
|
// The field must be written to ensure a unique ID.
|
|
|
|
$this->Name = $this->class.$this->ID;
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|