From 57a95c1484f172939591837673575dc834707897 Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Sat, 6 Oct 2012 10:27:02 +1300 Subject: [PATCH 1/4] MINOR Tabs must be used to indent lines and line lenght preferable less than 100chars. --- code/form/WidgetAreaEditor.php | 30 +++++++++++++++--------------- code/model/Widget.php | 4 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/code/form/WidgetAreaEditor.php b/code/form/WidgetAreaEditor.php index 814c132..f2f9593 100644 --- a/code/form/WidgetAreaEditor.php +++ b/code/form/WidgetAreaEditor.php @@ -94,28 +94,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; diff --git a/code/model/Widget.php b/code/model/Widget.php index cb9cebd..e855cd4 100644 --- a/code/model/Widget.php +++ b/code/model/Widget.php @@ -222,8 +222,8 @@ class Widget_Controller extends Controller { $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')) { From e90577ee6c8014adee988afdad7cf25f76d54fb8 Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Sat, 6 Oct 2012 10:35:02 +1300 Subject: [PATCH 2/4] API CHANGE Removed unused variables --- code/form/WidgetAreaEditor.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/code/form/WidgetAreaEditor.php b/code/form/WidgetAreaEditor.php index f2f9593..63f1290 100644 --- a/code/form/WidgetAreaEditor.php +++ b/code/form/WidgetAreaEditor.php @@ -5,12 +5,6 @@ * @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) { $this->MaxWidgets = $maxWidgets; From c2788985e7446321893d28ef03e614133778036f Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Sat, 6 Oct 2012 10:39:49 +1300 Subject: [PATCH 3/4] MINOR Adding visibility keywords to methods and class variables. --- .../WigetContentControllerExtension.php | 6 +- code/form/WidgetAreaEditor.php | 14 ++--- code/model/Widget.php | 62 ++++++++++--------- code/model/WidgetArea.php | 22 +++---- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/code/controller/WigetContentControllerExtension.php b/code/controller/WigetContentControllerExtension.php index 78d729f..c9351c8 100644 --- a/code/controller/WigetContentControllerExtension.php +++ b/code/controller/WigetContentControllerExtension.php @@ -1,10 +1,10 @@ owner->getRequest()->param('ID'); if(!$SQL_id) return false; diff --git a/code/form/WidgetAreaEditor.php b/code/form/WidgetAreaEditor.php index 63f1290..e72e6f6 100644 --- a/code/form/WidgetAreaEditor.php +++ b/code/form/WidgetAreaEditor.php @@ -6,20 +6,20 @@ */ class WidgetAreaEditor extends FormField { - function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) { + public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) { $this->MaxWidgets = $maxWidgets; $this->widgetClasses = $widgetClasses; parent::__construct($name); } - function FieldHolder($properties = array()) { + public function FieldHolder($properties = array()) { Requirements::css('widgets/css/WidgetAreaEditor.css'); Requirements::javascript('widgets/javascript/WidgetAreaEditor.js'); return $this->renderWith("WidgetAreaEditor"); } - function AvailableWidgets() { + public function AvailableWidgets() { $widgets= new ArrayList(); @@ -34,7 +34,7 @@ class WidgetAreaEditor extends FormField { return $widgets; } - function UsedWidgets() { + public function UsedWidgets() { // Call class_exists() to load Widget.php earlier and avoid a segfault class_exists('Widget'); @@ -43,16 +43,16 @@ class WidgetAreaEditor extends FormField { return $widgets; } - function IdxField() { + public function IdxField() { return $this->id() . 'ID'; } - function Value() { + public function Value() { $relationName = $this->name; return $this->form->getRecord()->getComponent($relationName)->ID; } - function saveInto(DataObjectInterface $record) { + public function saveInto(DataObjectInterface $record) { $name = $this->name; $idName = $name . "ID"; diff --git a/code/model/Widget.php b/code/model/Widget.php index e855cd4..0156b66 100644 --- a/code/model/Widget.php +++ b/code/model/Widget.php @@ -11,30 +11,31 @@ * @subpackage widgets */ class Widget extends DataObject { - static $db = array( + + public static $db = array( "Sort" => "Int", "Enabled" => "Boolean" ); - static $defaults = array( + public static $defaults = array( 'Enabled' => true ); - static $has_one = array( + public static $has_one = array( "Parent" => "WidgetArea", ); - static $has_many = array(); - static $many_many = array(); - static $belongs_many_many = array(); + public static $has_many = array(); + public static $many_many = array(); + public static $belongs_many_many = array(); - static $default_sort = "\"Sort\""; + public static $default_sort = "\"Sort\""; - static $title = "Widget Title"; - static $cmsTitle = "Name of this widget"; - static $description = "Description of what this widget does."; + public static $title = "Widget Title"; + public static $cmsTitle = "Name of this widget"; + public static $description = "Description of what this widget does."; - function getCMSFields() { + public function getCMSFields() { $fields = new FieldList(); $this->extend('updateCMSFields', $fields); return $fields; @@ -45,7 +46,7 @@ class Widget extends DataObject { * * @return string HTML */ - function WidgetHolder() { + public function WidgetHolder() { return $this->renderWith("WidgetHolder"); } @@ -60,34 +61,34 @@ class Widget extends DataObject { * * @return string HTML */ - function Content() { + public function Content() { return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class))); } - function Title() { + public function Title() { return _t($this->class.'.TITLE', Object::get_static($this->class, 'title')); } - function CMSTitle() { + public function CMSTitle() { return _t($this->class.'.CMSTITLE', Object::get_static($this->class, 'cmsTitle')); } - function Description() { + public function Description() { return _t($this->class.'.DESCRIPTION', Object::get_static($this->class, 'description')); } - function DescriptionSegment() { + public function DescriptionSegment() { return $this->renderWith('WidgetDescription'); } /** * @see Widget_Controller->editablesegment() */ - function EditableSegment() { + public function EditableSegment() { return $this->renderWith('WidgetEditor'); } - function CMSEditor() { + public function CMSEditor() { $fields = $this->getCMSFields(); $outputFields = new FieldList(); foreach($fields as $field) { @@ -103,15 +104,15 @@ class Widget extends DataObject { return $outputFields; } - function ClassName() { + public function ClassName() { return $this->class; } - function Name() { + public function Name() { return "Widget[".$this->ID."]"; } - function populateFromPostData($data) { + public function populateFromPostData($data) { $fields = $this->getCMSFields(); foreach($data as $name => $value) { if($name != "Type") { @@ -159,11 +160,11 @@ class Widget_Controller extends Controller { */ protected $widget; - static $allowed_actions = array( + public static $allowed_actions = array( 'editablesegment' ); - function __construct($widget = null) { + public function __construct($widget = null) { // TODO This shouldn't be optional, is only necessary for editablesegment() if($widget) { $this->widget = $widget; @@ -186,7 +187,7 @@ class Widget_Controller extends Controller { /** * @return Widget */ - function getWidget() { + public function getWidget() { return $this->widget; } @@ -196,7 +197,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 +207,7 @@ class Widget_Controller extends Controller { * * @return string HTML */ - function WidgetHolder() { + public function WidgetHolder() { return $this->renderWith("WidgetHolder"); } @@ -218,7 +219,7 @@ 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 @@ -241,7 +242,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()) {} } diff --git a/code/model/WidgetArea.php b/code/model/WidgetArea.php index a3f1d9e..aec1375 100644 --- a/code/model/WidgetArea.php +++ b/code/model/WidgetArea.php @@ -6,17 +6,17 @@ */ class WidgetArea extends DataObject { - static $db = array(); + public static $db = array(); - static $has_one = array(); + public static $has_one = array(); - static $has_many = array( + public static $has_many = array( "Widgets" => "Widget" ); - static $many_many = array(); + public static $many_many = array(); - static $belongs_many_many = array(); + public static $belongs_many_many = array(); public $template = __CLASS__; @@ -28,7 +28,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) { @@ -46,23 +46,23 @@ class WidgetArea extends DataObject { return $controllers; } - function Items() { + public function Items() { return $this->getComponents('Widgets'); } - function ItemsToRender() { + public function ItemsToRender() { return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1"); } - function forTemplate() { + public function forTemplate() { return $this->renderWith($this->template); } - function setTemplate($template) { + public function setTemplate($template) { $this->template = $template; } - function onBeforeDelete() { + public function onBeforeDelete() { parent::onBeforeDelete(); foreach($this->Widgets() as $widget) { $widget->delete(); From 135792bd968790bea92a85cdd9cdcc0b01f11247 Mon Sep 17 00:00:00 2001 From: Stig Lindqvist Date: Sat, 6 Oct 2012 10:47:26 +1300 Subject: [PATCH 4/4] MINOR Adding docblocks for class methods. --- .../WigetContentControllerExtension.php | 4 + code/form/WidgetAreaEditor.php | 43 +++++- code/model/Widget.php | 126 +++++++++++++++--- code/model/WidgetArea.php | 65 +++++++-- 4 files changed, 205 insertions(+), 33 deletions(-) diff --git a/code/controller/WigetContentControllerExtension.php b/code/controller/WigetContentControllerExtension.php index c9351c8..476d1e3 100644 --- a/code/controller/WigetContentControllerExtension.php +++ b/code/controller/WigetContentControllerExtension.php @@ -4,6 +4,10 @@ */ class WidgetContentControllerExtension extends Extension { + /** + * + * @var array + */ public static $allowed_actions = array( 'handleWidget' ); diff --git a/code/form/WidgetAreaEditor.php b/code/form/WidgetAreaEditor.php index e72e6f6..4edc40f 100644 --- a/code/form/WidgetAreaEditor.php +++ b/code/form/WidgetAreaEditor.php @@ -6,19 +6,34 @@ */ class WidgetAreaEditor extends FormField { + /** + * + * @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); } - + + /** + * + * @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"); } - + + /** + * + * @return ArrayList + */ public function AvailableWidgets() { $widgets= new ArrayList(); @@ -33,7 +48,11 @@ class WidgetAreaEditor extends FormField { return $widgets; } - + + /** + * + * @return HasManyList + */ public function UsedWidgets() { // Call class_exists() to load Widget.php earlier and avoid a segfault class_exists('Widget'); @@ -42,16 +61,28 @@ class WidgetAreaEditor extends FormField { $widgets = $this->form->getRecord()->getComponent($relationName)->Items(); return $widgets; } - + + /** + * + * @return string + */ public function IdxField() { return $this->id() . 'ID'; } - + + /** + * + * @return int + */ public function Value() { $relationName = $this->name; return $this->form->getRecord()->getComponent($relationName)->ID; } - + + /** + * + * @param DataObjectInterface $record + */ public function saveInto(DataObjectInterface $record) { $name = $this->name; $idName = $name . "ID"; diff --git a/code/model/Widget.php b/code/model/Widget.php index 0156b66..30f710b 100644 --- a/code/model/Widget.php +++ b/code/model/Widget.php @@ -12,29 +12,77 @@ */ class Widget extends DataObject { + /** + * + * @var array + */ public static $db = array( "Sort" => "Int", "Enabled" => "Boolean" ); - + + /** + * + * @var array + */ public static $defaults = array( 'Enabled' => true ); - + + /** + * + * @var array + */ public static $has_one = array( "Parent" => "WidgetArea", ); - + + /** + * + * @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); @@ -64,30 +112,51 @@ class Widget extends DataObject { public function Content() { return $this->renderWith(array_reverse(ClassInfo::ancestry($this->class))); } - + + /** + * + * @return string + */ public function Title() { return _t($this->class.'.TITLE', Object::get_static($this->class, 'title')); } - + + /** + * + * @return string + */ public function CMSTitle() { return _t($this->class.'.CMSTITLE', Object::get_static($this->class, 'cmsTitle')); } - + + /** + * + * @return string + */ public function Description() { return _t($this->class.'.DESCRIPTION', Object::get_static($this->class, 'description')); } - + + /** + * + * @return string - HTML + */ public function DescriptionSegment() { return $this->renderWith('WidgetDescription'); } /** * @see Widget_Controller->editablesegment() + * @return string - HTML */ public function EditableSegment() { return $this->renderWith('WidgetEditor'); } - + + /** + * + * @return FieldList + */ public function CMSEditor() { $fields = $this->getCMSFields(); $outputFields = new FieldList(); @@ -103,15 +172,27 @@ class Widget extends DataObject { } return $outputFields; } - + + /** + * + * @return string + */ public function ClassName() { return $this->class; } - + + /** + * + * @return string + */ public function Name() { return "Widget[".$this->ID."]"; } + /** + * + * @param array $data + */ public function populateFromPostData($data) { $fields = $this->getCMSFields(); foreach($data as $name => $value) { @@ -159,11 +240,19 @@ class Widget_Controller extends Controller { * @var Widget */ protected $widget; - + + /** + * + * @var array + */ public static $allowed_actions = array( 'editablesegment' ); - + + /** + * + * @param Widget $widget + */ public function __construct($widget = null) { // TODO This shouldn't be optional, is only necessary for editablesegment() if($widget) { @@ -173,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); diff --git a/code/model/WidgetArea.php b/code/model/WidgetArea.php index aec1375..9c9d775 100644 --- a/code/model/WidgetArea.php +++ b/code/model/WidgetArea.php @@ -5,19 +5,43 @@ * @subpackage widgets */ class WidgetArea extends DataObject { - + + /** + * + * @var array + */ public static $db = array(); - + + /** + * + * @var array + */ public static $has_one = array(); - + + /** + * + * @var array + */ public static $has_many = array( "Widgets" => "Widget" ); - + + /** + * + * @var array + */ public static $many_many = array(); - + + /** + * + * @var array + */ public static $belongs_many_many = array(); - + + /** + * + * @var string + */ public $template = __CLASS__; /** @@ -45,23 +69,42 @@ class WidgetArea extends DataObject { return $controllers; } - + + /** + * + * @return HasManyList + */ public function Items() { return $this->getComponents('Widgets'); } - + + /** + * + * @return HasManyList + */ public function ItemsToRender() { return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1"); } - + + /** + * + * @return string - HTML + */ public function forTemplate() { return $this->renderWith($this->template); } - + + /** + * + * @param string $template + */ public function setTemplate($template) { $this->template = $template; } - + + /** + * Delete all connected Widgets when this WidgetArea gets deleted + */ public function onBeforeDelete() { parent::onBeforeDelete(); foreach($this->Widgets() as $widget) {