silverstripe-widgets/code/model/Widget.php

262 lines
4.8 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
2012-04-18 23:15:45 +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.
*
* 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.
*
* @package widgets
2012-04-18 23:15:45 +02:00
*/
class Widget extends DataObject {
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $db = array(
2012-04-18 23:15:45 +02:00
"Sort" => "Int",
"Enabled" => "Boolean"
);
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $defaults = array(
2012-04-18 23:15:45 +02:00
'Enabled' => true
);
2013-04-22 15:17:29 +02:00
private static $only_available_in = array();
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $has_one = array(
2012-04-18 23:15:45 +02:00
"Parent" => "WidgetArea",
);
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $has_many = array();
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $many_many = array();
/**
* @var array
*/
2013-04-01 21:37:44 +02:00
private static $belongs_many_many = array();
/**
* @var string
*/
2013-04-01 21:37:44 +02:00
private static $default_sort = "\"Sort\"";
/**
* @var string
*/
2013-04-01 21:37:44 +02:00
private static $title = "Widget Title";
/**
* @var string
*/
2013-04-01 21:37:44 +02:00
private static $cmsTitle = "Name of this widget";
/**
* @var string
*/
2013-04-01 21:37:44 +02:00
private static $description = "Description of what this widget does.";
2013-07-24 03:44:58 +02:00
/**
* @var array
*/
private static $summary_fields = array(
'CMSTitle' => 'Title'
);
/**
* @var WidgetController
*/
protected $controller;
2012-04-18 23:15:45 +02:00
/**
* Note: Overloaded in {@link WidgetController}.
2012-04-18 23:15:45 +02:00
*
* @return string HTML
*/
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
/**
* 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
*
* Invoked from within WidgetHolder.ss, which contains the "framing" around
* the custom content, like a title.
2012-04-18 23:15:45 +02:00
*
* Note: Overloaded in {@link WidgetController}.
2012-04-18 23:15:45 +02:00
*
* @return string HTML
*/
public function Content() {
2012-04-18 23:15:45 +02:00
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
}
/**
* @return string
*/
public function Title() {
2013-03-20 14:00:54 +01:00
return _t($this->class.'.TITLE', $this->config()->title);
2012-04-18 23:15:45 +02:00
}
/**
* @return string
*/
public function CMSTitle() {
2013-03-20 14:00:54 +01:00
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
2012-04-18 23:15:45 +02:00
}
/**
* @return string
*/
public function Description() {
2013-03-20 14:00:54 +01:00
return _t($this->class.'.DESCRIPTION', $this->config()->description);
2012-04-18 23:15:45 +02:00
}
/**
* @return string - HTML
*/
public function DescriptionSegment() {
2012-04-18 23:15:45 +02:00
return $this->renderWith('WidgetDescription');
}
/**
* @see WidgetController::editablesegment()
*
* @return string - HTML
2012-04-18 23:15:45 +02:00
*/
public function EditableSegment() {
2012-04-18 23:15:45 +02:00
return $this->renderWith('WidgetEditor');
}
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('ParentID');
2014-07-01 23:16:53 +02:00
$fields->removeByName('Sort');
return $fields;
}
/**
* @return FieldList
*/
public function CMSEditor() {
2012-04-18 23:15:45 +02:00
$fields = $this->getCMSFields();
$outputFields = new FieldList();
2012-04-18 23:15:45 +02:00
foreach($fields as $field) {
$name = $field->getName();
$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
}
return $outputFields;
2012-04-18 23:15:45 +02:00
}
/**
* @return string
*/
public function ClassName() {
2012-04-18 23:15:45 +02:00
return $this->class;
}
/**
* @return string
*/
public function Name() {
2012-04-18 23:15:45 +02:00
return "Widget[".$this->ID."]";
}
/**
* @throws Exception
*
* @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;
}
/**
* @param array $data
*/
public function populateFromPostData($data) {
$fields = $this->getCMSFields();
2012-04-18 23:15:45 +02:00
foreach($data as $name => $value) {
if($name != "Type") {
if ($field = $fields->dataFieldByName($name)) {
$field->setValue($value);
$field->saveInto($this);
}
else {
$this->setField($name, $value);
}
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();
}
}