2007-08-03 00:07:11 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-09-07 05:28:23 +02:00
|
|
|
* Widgets let CMS authors drag and drop small pieces of functionality into
|
|
|
|
* defined areas of their websites.
|
|
|
|
*
|
|
|
|
* ## Forms
|
|
|
|
* You can use forms in widgets by implementing a {@link Widget_Controller}.
|
|
|
|
* See {@link Widget_Controller} for more information.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage widgets
|
|
|
|
*/
|
2007-08-03 00:07:11 +02:00
|
|
|
class Widget extends DataObject {
|
|
|
|
static $db = array(
|
2009-12-16 06:44:20 +01:00
|
|
|
"Sort" => "Int",
|
|
|
|
"Enabled" => "Boolean"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $defaults = array(
|
|
|
|
'Enabled' => true
|
2007-08-03 00:07:11 +02:00
|
|
|
);
|
2008-11-04 03:21:09 +01:00
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
"Parent" => "WidgetArea",
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
static $has_many = array();
|
|
|
|
static $many_many = array();
|
|
|
|
static $belongs_many_many = array();
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
static $default_sort = "Sort";
|
|
|
|
|
|
|
|
static $title = "Widget Title";
|
|
|
|
static $cmsTitle = "Name of this widget";
|
|
|
|
static $description = "Description of what this widget does.";
|
|
|
|
|
|
|
|
function getCMSFields() {
|
|
|
|
return new FieldSet();
|
|
|
|
}
|
|
|
|
|
2009-09-07 05:28:23 +02:00
|
|
|
/**
|
|
|
|
* Note: Overloaded in {@link Widget_Controller}.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2007-08-03 00:07:11 +02:00
|
|
|
function WidgetHolder() {
|
|
|
|
return $this->renderWith("WidgetHolder");
|
|
|
|
}
|
|
|
|
|
2009-09-07 05:28:23 +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.
|
|
|
|
*
|
|
|
|
* Invoked from within WidgetHolder.ss, which contains
|
|
|
|
* the "framing" around the custom content, like a title.
|
|
|
|
*
|
|
|
|
* Note: Overloaded in {@link Widget_Controller}.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2007-08-03 00:07:11 +02:00
|
|
|
function Content() {
|
2009-09-07 08:26:49 +02:00
|
|
|
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Title() {
|
2009-05-06 04:43:18 +02:00
|
|
|
return Object::get_static($this->class, 'title');
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function CMSTitle() {
|
2009-05-06 04:43:18 +02:00
|
|
|
return Object::get_static($this->class, 'cmsTitle');
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Description() {
|
2009-05-06 04:43:18 +02:00
|
|
|
return Object::get_static($this->class, 'description');
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function DescriptionSegment() {
|
|
|
|
return $this->renderWith('WidgetDescription');
|
|
|
|
}
|
|
|
|
|
2009-09-07 05:28:23 +02:00
|
|
|
/**
|
|
|
|
* @see Widget_Controller->editablesegment()
|
|
|
|
*/
|
2007-08-03 00:07:11 +02:00
|
|
|
function EditableSegment() {
|
|
|
|
return $this->renderWith('WidgetEditor');
|
|
|
|
}
|
|
|
|
|
|
|
|
function CMSEditor() {
|
|
|
|
$output = '';
|
|
|
|
$fields = $this->getCMSFields();
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$name = $field->Name();
|
|
|
|
$field->setValue($this->getField($name));
|
|
|
|
$renderedField = $field->FieldHolder();
|
|
|
|
$renderedField = ereg_replace("name=\"([A-Za-z0-9\-_]+)\"", "name=\"Widget[" . $this->ID . "][\\1]\"", $renderedField);
|
|
|
|
$renderedField = ereg_replace("id=\"([A-Za-z0-9\-_]+)\"", "id=\"Widget[" . $this->ID . "][\\1]\"", $renderedField);
|
|
|
|
$output .= $renderedField;
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ClassName() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Name() {
|
|
|
|
return "Widget[".$this->ID."]";
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateFromPostData($data) {
|
|
|
|
foreach($data as $name => $value) {
|
|
|
|
if($name != "Type") {
|
|
|
|
$this->setField($name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->write();
|
|
|
|
|
|
|
|
// The field must be written to ensure a unique ID.
|
|
|
|
$this->Name = $this->class.$this->ID;
|
|
|
|
$this->write();
|
|
|
|
}
|
2008-03-03 09:48:52 +01:00
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
2009-09-07 05:28:23 +02:00
|
|
|
/**
|
|
|
|
* Optional controller for every widget which has its own logic,
|
|
|
|
* e.g. in forms. It always handles a single widget, usually passed
|
|
|
|
* in as a database identifier through the controller URL.
|
|
|
|
* Needs to be constructed as a nested controller
|
|
|
|
* within a {@link ContentController}.
|
|
|
|
*
|
|
|
|
* ## Forms
|
|
|
|
* You can add forms like in any other sapphire controller.
|
|
|
|
* If you need access to the widget from within a form,
|
|
|
|
* you can use `$this->controller->getWidget()` inside the form logic.
|
|
|
|
* Note: Widget controllers currently only work on {@link Page} objects,
|
|
|
|
* because the logic is implemented in {@link ContentController->handleWidget()}.
|
|
|
|
* Copy this logic and the URL rules to enable it for other controllers.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage widgets
|
|
|
|
*/
|
2008-03-11 04:29:36 +01:00
|
|
|
class Widget_Controller extends Controller {
|
2009-05-06 05:30:44 +02:00
|
|
|
|
2009-09-07 05:28:23 +02:00
|
|
|
/**
|
|
|
|
* @var Widget
|
|
|
|
*/
|
|
|
|
protected $widget;
|
|
|
|
|
|
|
|
function __construct($widget = null) {
|
|
|
|
// TODO This shouldn't be optional, is only necessary for editablesegment()
|
|
|
|
if($widget) {
|
|
|
|
$this->widget = $widget;
|
|
|
|
$this->failover = $widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:16 +02:00
|
|
|
public function Link($action = null) {
|
2009-11-08 22:53:57 +01:00
|
|
|
$segment = Controller::join_links('widget', ($this->widget ? $this->widget->ID : null), $action);
|
|
|
|
|
|
|
|
if(Director::get_current_page()) {
|
|
|
|
return Director::get_current_page()->Link($segment);
|
|
|
|
} else {
|
|
|
|
return Controller::curr()->Link($segment);
|
|
|
|
}
|
2009-09-07 05:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Widget
|
|
|
|
*/
|
|
|
|
function getWidget() {
|
|
|
|
return $this->widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overloaded from {@link Widget->Content()}
|
|
|
|
* to allow for controller/form linking.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
function Content() {
|
2009-09-07 08:26:49 +02:00
|
|
|
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
|
2009-09-07 05:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overloaded from {@link Widget->WidgetHolder()}
|
|
|
|
* to allow for controller/form linking.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
function WidgetHolder() {
|
|
|
|
return $this->renderWith("WidgetHolder");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the `WidgetEditor.ss` template and {@link Widget->editablesegment()}
|
|
|
|
* to render a administrator-view of the widget. It is assumed that this
|
|
|
|
* view contains form elements which are submitted and saved through {@link WidgetAreaEditor}
|
|
|
|
* within the CMS interface.
|
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
2008-03-11 04:29:36 +01:00
|
|
|
function editablesegment() {
|
|
|
|
$className = $this->urlParams['ID'];
|
|
|
|
if(class_exists($className) && is_subclass_of($className, 'Widget')) {
|
|
|
|
$obj = new $className();
|
|
|
|
return $obj->EditableSegment();
|
|
|
|
} else {
|
|
|
|
user_error("Bad widget class: $className", E_USER_WARNING);
|
|
|
|
return "Bad widget class name given";
|
|
|
|
}
|
2009-11-21 02:42:44 +01:00
|
|
|
}
|
2008-03-11 04:29:36 +01:00
|
|
|
}
|
2009-11-21 02:42:44 +01:00
|
|
|
|
|
|
|
class Widget_TreeDropdownField extends TreeDropdownField {
|
|
|
|
function FieldHolder() {}
|
|
|
|
function Field() {}
|
|
|
|
}
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
?>
|