mirror of
https://github.com/silverstripe/silverstripe-widgets
synced 2024-10-22 17:05:54 +02:00
Merge pull request #19 from stojg/coding-conventions
Coding conventions
This commit is contained in:
commit
3e598934f4
@ -1,10 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Add this to ContentController to enable widgets
|
||||
*/
|
||||
class WidgetContentControllerExtension extends Extension {
|
||||
static $allowed_actions = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'handleWidget'
|
||||
);
|
||||
|
||||
@ -16,7 +20,7 @@ class WidgetContentControllerExtension extends Extension {
|
||||
*
|
||||
* @return RequestHandler
|
||||
*/
|
||||
function handleWidget() {
|
||||
public function handleWidget() {
|
||||
$SQL_id = $this->owner->getRequest()->param('ID');
|
||||
if(!$SQL_id) return false;
|
||||
|
||||
|
@ -5,27 +5,36 @@
|
||||
* @subpackage content
|
||||
*/
|
||||
class WidgetAreaEditor extends FormField {
|
||||
/**
|
||||
* 3 variables to hold titles for the template
|
||||
*/
|
||||
public $InUseTitle;
|
||||
public $AvailableTitle;
|
||||
public $ToAddTitle;
|
||||
|
||||
function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) {
|
||||
/**
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $widgetClasses
|
||||
* @param int $maxWidgets
|
||||
*/
|
||||
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) {
|
||||
$this->MaxWidgets = $maxWidgets;
|
||||
$this->widgetClasses = $widgetClasses;
|
||||
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
function FieldHolder($properties = array()) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $properties
|
||||
* @return string - HTML for this formfield
|
||||
*/
|
||||
public function FieldHolder($properties = array()) {
|
||||
Requirements::css('widgets/css/WidgetAreaEditor.css');
|
||||
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
|
||||
return $this->renderWith("WidgetAreaEditor");
|
||||
}
|
||||
|
||||
function AvailableWidgets() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function AvailableWidgets() {
|
||||
|
||||
$widgets= new ArrayList();
|
||||
|
||||
@ -39,8 +48,12 @@ class WidgetAreaEditor extends FormField {
|
||||
|
||||
return $widgets;
|
||||
}
|
||||
|
||||
function UsedWidgets() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return HasManyList
|
||||
*/
|
||||
public function UsedWidgets() {
|
||||
// Call class_exists() to load Widget.php earlier and avoid a segfault
|
||||
class_exists('Widget');
|
||||
|
||||
@ -48,17 +61,29 @@ class WidgetAreaEditor extends FormField {
|
||||
$widgets = $this->form->getRecord()->getComponent($relationName)->Items();
|
||||
return $widgets;
|
||||
}
|
||||
|
||||
function IdxField() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function IdxField() {
|
||||
return $this->id() . 'ID';
|
||||
}
|
||||
|
||||
function Value() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function Value() {
|
||||
$relationName = $this->name;
|
||||
return $this->form->getRecord()->getComponent($relationName)->ID;
|
||||
}
|
||||
|
||||
function saveInto(DataObjectInterface $record) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param DataObjectInterface $record
|
||||
*/
|
||||
public function saveInto(DataObjectInterface $record) {
|
||||
$name = $this->name;
|
||||
$idName = $name . "ID";
|
||||
|
||||
@ -94,28 +119,28 @@ class WidgetAreaEditor extends FormField {
|
||||
}
|
||||
|
||||
// \"ParentID\" = '0' is for the new page
|
||||
$widget = DataObject::get_one(
|
||||
$widget = DataObject::get_one(
|
||||
'Widget',
|
||||
"(\"ParentID\" = '{$record->$name()->ID}' OR \"ParentID\" = '0') AND \"Widget\".\"ID\" = '$newWidgetID'"
|
||||
"(\"ParentID\" = '{$record->$name()->ID}' OR ".
|
||||
"\"ParentID\" = '0') AND \"Widget\".\"ID\" = '$newWidgetID'"
|
||||
);
|
||||
|
||||
|
||||
// check if we are updating an existing widget
|
||||
// check if we are updating an existing widget
|
||||
if($widget && isset($missingWidgets[$widget->ID])) {
|
||||
unset($missingWidgets[$widget->ID]);
|
||||
unset($missingWidgets[$widget->ID]);
|
||||
}
|
||||
|
||||
// create a new object
|
||||
if(!$widget && !empty($newWidgetData['Type']) && class_exists($newWidgetData['Type'])) {
|
||||
$widget = new $newWidgetData['Type']();
|
||||
$widget->ID = 0;
|
||||
$widget->ParentID = $record->$name()->ID;
|
||||
// create a new object
|
||||
if(!$widget && !empty($newWidgetData['Type']) && class_exists($newWidgetData['Type'])) {
|
||||
$widget = new $newWidgetData['Type']();
|
||||
$widget->ID = 0;
|
||||
$widget->ParentID = $record->$name()->ID;
|
||||
|
||||
if(!is_subclass_of($widget, 'Widget')) {
|
||||
$widget = null;
|
||||
}
|
||||
}
|
||||
|
||||
if(!is_subclass_of($widget, 'Widget')) {
|
||||
$widget = null;
|
||||
}
|
||||
}
|
||||
|
||||
if($widget) {
|
||||
if($widget->ParentID == 0) {
|
||||
$widget->ParentID = $record->$name()->ID;
|
||||
|
@ -11,30 +11,79 @@
|
||||
* @subpackage widgets
|
||||
*/
|
||||
class Widget extends DataObject {
|
||||
static $db = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $db = array(
|
||||
"Sort" => "Int",
|
||||
"Enabled" => "Boolean"
|
||||
);
|
||||
|
||||
static $defaults = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $defaults = array(
|
||||
'Enabled' => true
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $has_one = array(
|
||||
"Parent" => "WidgetArea",
|
||||
);
|
||||
|
||||
static $has_many = array();
|
||||
static $many_many = array();
|
||||
static $belongs_many_many = array();
|
||||
|
||||
static $default_sort = "\"Sort\"";
|
||||
|
||||
static $title = "Widget Title";
|
||||
static $cmsTitle = "Name of this widget";
|
||||
static $description = "Description of what this widget does.";
|
||||
|
||||
function getCMSFields() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $has_many = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $many_many = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $belongs_many_many = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $default_sort = "\"Sort\"";
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $title = "Widget Title";
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $cmsTitle = "Name of this widget";
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $description = "Description of what this widget does.";
|
||||
|
||||
/**
|
||||
*
|
||||
* @return FieldList
|
||||
*/
|
||||
public function getCMSFields() {
|
||||
$fields = new FieldList();
|
||||
$this->extend('updateCMSFields', $fields);
|
||||
return $fields;
|
||||
@ -45,7 +94,7 @@ class Widget extends DataObject {
|
||||
*
|
||||
* @return string HTML
|
||||
*/
|
||||
function WidgetHolder() {
|
||||
public function WidgetHolder() {
|
||||
return $this->renderWith("WidgetHolder");
|
||||
}
|
||||
|
||||
@ -60,34 +109,55 @@ class Widget extends DataObject {
|
||||
*
|
||||
* @return string HTML
|
||||
*/
|
||||
function Content() {
|
||||
public function Content() {
|
||||
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class)));
|
||||
}
|
||||
|
||||
function Title() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Title() {
|
||||
return _t($this->class.'.TITLE', Object::get_static($this->class, 'title'));
|
||||
}
|
||||
|
||||
function CMSTitle() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function CMSTitle() {
|
||||
return _t($this->class.'.CMSTITLE', Object::get_static($this->class, 'cmsTitle'));
|
||||
}
|
||||
|
||||
function Description() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Description() {
|
||||
return _t($this->class.'.DESCRIPTION', Object::get_static($this->class, 'description'));
|
||||
}
|
||||
|
||||
function DescriptionSegment() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string - HTML
|
||||
*/
|
||||
public function DescriptionSegment() {
|
||||
return $this->renderWith('WidgetDescription');
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Widget_Controller->editablesegment()
|
||||
* @return string - HTML
|
||||
*/
|
||||
function EditableSegment() {
|
||||
public function EditableSegment() {
|
||||
return $this->renderWith('WidgetEditor');
|
||||
}
|
||||
|
||||
function CMSEditor() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return FieldList
|
||||
*/
|
||||
public function CMSEditor() {
|
||||
$fields = $this->getCMSFields();
|
||||
$outputFields = new FieldList();
|
||||
foreach($fields as $field) {
|
||||
@ -102,16 +172,28 @@ class Widget extends DataObject {
|
||||
}
|
||||
return $outputFields;
|
||||
}
|
||||
|
||||
function ClassName() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function ClassName() {
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
function Name() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Name() {
|
||||
return "Widget[".$this->ID."]";
|
||||
}
|
||||
|
||||
function populateFromPostData($data) {
|
||||
/**
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function populateFromPostData($data) {
|
||||
$fields = $this->getCMSFields();
|
||||
foreach($data as $name => $value) {
|
||||
if($name != "Type") {
|
||||
@ -158,12 +240,20 @@ class Widget_Controller extends Controller {
|
||||
* @var Widget
|
||||
*/
|
||||
protected $widget;
|
||||
|
||||
static $allowed_actions = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $allowed_actions = array(
|
||||
'editablesegment'
|
||||
);
|
||||
|
||||
function __construct($widget = null) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Widget $widget
|
||||
*/
|
||||
public function __construct($widget = null) {
|
||||
// TODO This shouldn't be optional, is only necessary for editablesegment()
|
||||
if($widget) {
|
||||
$this->widget = $widget;
|
||||
@ -172,7 +262,12 @@ class Widget_Controller extends Controller {
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $action
|
||||
* @return string
|
||||
*/
|
||||
public function Link($action = null) {
|
||||
$segment = Controller::join_links('widget', ($this->widget ? $this->widget->ID : null), $action);
|
||||
|
||||
@ -186,7 +281,7 @@ class Widget_Controller extends Controller {
|
||||
/**
|
||||
* @return Widget
|
||||
*/
|
||||
function getWidget() {
|
||||
public function getWidget() {
|
||||
return $this->widget;
|
||||
}
|
||||
|
||||
@ -196,7 +291,7 @@ class Widget_Controller extends Controller {
|
||||
*
|
||||
* @return string HTML
|
||||
*/
|
||||
function Content() {
|
||||
public function Content() {
|
||||
return $this->renderWith(array_reverse(ClassInfo::ancestry($this->widget->class)));
|
||||
}
|
||||
|
||||
@ -206,7 +301,7 @@ class Widget_Controller extends Controller {
|
||||
*
|
||||
* @return string HTML
|
||||
*/
|
||||
function WidgetHolder() {
|
||||
public function WidgetHolder() {
|
||||
return $this->renderWith("WidgetHolder");
|
||||
}
|
||||
|
||||
@ -218,12 +313,12 @@ class Widget_Controller extends Controller {
|
||||
*
|
||||
* @return string HTML
|
||||
*/
|
||||
function editablesegment() {
|
||||
public function editablesegment() {
|
||||
$className = $this->urlParams['ID'];
|
||||
if (class_exists('Translatable') && Member::currentUserID()) {
|
||||
// set current locale based on logged in user's locale
|
||||
$locale = Member::currentUser()->Locale;
|
||||
Translatable::set_current_locale($locale);
|
||||
$locale = Member::currentUser()->Locale;
|
||||
Translatable::set_current_locale($locale);
|
||||
i18n::set_locale($locale);
|
||||
}
|
||||
if(class_exists($className) && is_subclass_of($className, 'Widget')) {
|
||||
@ -241,7 +336,8 @@ class Widget_Controller extends Controller {
|
||||
* @subpackage widgets
|
||||
*/
|
||||
class Widget_TreeDropdownField extends TreeDropdownField {
|
||||
function FieldHolder($properties = array()) {}
|
||||
function Field($properties = array()) {}
|
||||
|
||||
public function FieldHolder($properties = array()) {}
|
||||
public function Field($properties = array()) {}
|
||||
}
|
||||
|
||||
|
@ -5,19 +5,43 @@
|
||||
* @subpackage widgets
|
||||
*/
|
||||
class WidgetArea extends DataObject {
|
||||
|
||||
static $db = array();
|
||||
|
||||
static $has_one = array();
|
||||
|
||||
static $has_many = array(
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $db = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $has_one = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $has_many = array(
|
||||
"Widgets" => "Widget"
|
||||
);
|
||||
|
||||
static $many_many = array();
|
||||
|
||||
static $belongs_many_many = array();
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $many_many = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $belongs_many_many = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $template = __CLASS__;
|
||||
|
||||
/**
|
||||
@ -28,7 +52,7 @@ class WidgetArea extends DataObject {
|
||||
*
|
||||
* @return SS_List Collection of {@link Widget_Controller}
|
||||
*/
|
||||
function WidgetControllers() {
|
||||
public function WidgetControllers() {
|
||||
$controllers = new ArrayList();
|
||||
|
||||
foreach($this->ItemsToRender() as $widget) {
|
||||
@ -45,24 +69,43 @@ class WidgetArea extends DataObject {
|
||||
|
||||
return $controllers;
|
||||
}
|
||||
|
||||
function Items() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return HasManyList
|
||||
*/
|
||||
public function Items() {
|
||||
return $this->getComponents('Widgets');
|
||||
}
|
||||
|
||||
function ItemsToRender() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return HasManyList
|
||||
*/
|
||||
public function ItemsToRender() {
|
||||
return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1");
|
||||
}
|
||||
|
||||
function forTemplate() {
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string - HTML
|
||||
*/
|
||||
public function forTemplate() {
|
||||
return $this->renderWith($this->template);
|
||||
}
|
||||
|
||||
function setTemplate($template) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $template
|
||||
*/
|
||||
public function setTemplate($template) {
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
function onBeforeDelete() {
|
||||
|
||||
/**
|
||||
* Delete all connected Widgets when this WidgetArea gets deleted
|
||||
*/
|
||||
public function onBeforeDelete() {
|
||||
parent::onBeforeDelete();
|
||||
foreach($this->Widgets() as $widget) {
|
||||
$widget->delete();
|
||||
|
Loading…
Reference in New Issue
Block a user