OSS-905 PSR2 compliance

This commit is contained in:
Shoaib Ali 2015-11-07 15:42:22 +13:00
parent eee39bca4e
commit 0a19119114
9 changed files with 185 additions and 197 deletions

View File

@ -5,7 +5,6 @@
* @package widgets
*/
class WidgetContentControllerExtension extends Extension {
/**
*
* @var array
@ -27,18 +26,20 @@ class WidgetContentControllerExtension extends Extension {
*/
public function handleWidget() {
$SQL_id = $this->owner->getRequest()->param('ID');
if(!$SQL_id) return false;
if (!$SQL_id) {
return false;
}
// find WidgetArea relations
$widgetAreaRelations = array();
$hasOnes = $this->owner->data()->hasOne();
if(!$hasOnes) {
if (!$hasOnes) {
return false;
}
foreach($hasOnes as $hasOneName => $hasOneClass) {
if($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
foreach ($hasOnes as $hasOneName => $hasOneClass) {
if ($hasOneClass == 'WidgetArea' || is_subclass_of($hasOneClass, 'WidgetArea')) {
$widgetAreaRelations[] = $hasOneName;
}
}
@ -46,8 +47,8 @@ class WidgetContentControllerExtension extends Extension {
// find widget
$widget = null;
foreach($widgetAreaRelations as $widgetAreaRelation) {
if($widget) {
foreach ($widgetAreaRelations as $widgetAreaRelation) {
if ($widget) {
break;
}
@ -56,7 +57,7 @@ class WidgetContentControllerExtension extends Extension {
->First();
}
if(!$widget) {
if (!$widget) {
user_error('No widget found', E_USER_ERROR);
}

View File

@ -19,7 +19,6 @@
* @package widgets
*/
class WidgetController extends Controller {
/**
* @var Widget
*/
@ -36,7 +35,7 @@ class WidgetController extends Controller {
* @param Widget $widget
*/
public function __construct($widget = null) {
if($widget) {
if ($widget) {
$this->widget = $widget;
$this->failover = $widget;
}
@ -52,7 +51,7 @@ class WidgetController extends Controller {
$id = ($this->widget) ? $this->widget->ID : null;
$segment = Controller::join_links('widget', $id, $action);
if($page = Director::get_current_page()) {
if ($page = Director::get_current_page()) {
return $page->Link($segment);
}
@ -101,7 +100,7 @@ class WidgetController extends Controller {
$locale = Member::currentUser()->Locale;
i18n::set_locale($locale);
}
if(class_exists($className) && is_subclass_of($className, 'Widget')) {
if (class_exists($className) && is_subclass_of($className, 'Widget')) {
$obj = new $className();
return $obj->EditableSegment();
} else {
@ -116,5 +115,4 @@ class WidgetController extends Controller {
* @package widgets
*/
class Widget_Controller extends WidgetController {
}

View File

@ -9,7 +9,6 @@
* without using this class.
*/
class WidgetPageExtension extends DataExtension {
private static $db = array(
'InheritSideBar' => 'Boolean',
);
@ -37,30 +36,29 @@ class WidgetPageExtension extends DataExtension {
* @return WidgetArea
*/
public function SideBarView() {
if(
if (
$this->owner->InheritSideBar
&& ($parent = $this->owner->getParent())
&& $parent->hasMethod('SideBarView')
) {
return $parent->SideBarView();
} elseif($this->owner->SideBar()->exists()){
} elseif ($this->owner->SideBar()->exists()) {
return $this->owner->SideBar();
}
}
public function onBeforeDuplicate($duplicatePage) {
if($this->owner->hasField('SideBarID')) {
if ($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
foreach($sideBar->Items() as $originalWidget) {
foreach ($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
}
return $duplicatePage;
@ -73,5 +71,4 @@ class WidgetPageExtension extends DataExtension {
//reset the sidebar ID
$this->owner->SideBarID = 0;
}
}

View File

@ -6,7 +6,6 @@
* @package widgets
*/
class WidgetAreaEditor extends FormField {
/**
* @param string $name
* @param array $widgetClasses
@ -38,25 +37,23 @@ class WidgetAreaEditor extends FormField {
public function AvailableWidgets() {
$widgets= new ArrayList();
foreach($this->widgetClasses as $widgetClass) {
foreach ($this->widgetClasses as $widgetClass) {
$classes = ClassInfo::subclassesFor($widgetClass);
if (isset($classes['Widget'])) {
unset($classes['Widget']);
}
else if (isset($classes[0]) && $classes[0] == 'Widget') {
} elseif (isset($classes[0]) && $classes[0] == 'Widget') {
unset($classes[0]);
}
foreach($classes as $class) {
foreach ($classes as $class) {
$available = Config::inst()->get($class, 'only_available_in');
if (!empty($available) && is_array($available)) {
if(in_array($this->Name, $available)) {
if (in_array($this->Name, $available)) {
$widgets->push(singleton($class));
}
}else {
} else {
$widgets->push(singleton($class));
}
}
@ -113,40 +110,42 @@ class WidgetAreaEditor extends FormField {
// alternatively, we could delete all the fields and re add them
$missingWidgets = array();
if($widgets) {
foreach($widgets as $existingWidget) {
if ($widgets) {
foreach ($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget;
}
}
if(!$this->getForm()) throw new Exception("no form");
if (!$this->getForm()) {
throw new Exception("no form");
}
$widgetData = $this->getForm()->getRequest()->requestVar('Widget');
if($widgetData && isset($widgetData[$this->getName()])) {
if ($widgetData && isset($widgetData[$this->getName()])) {
$widgetAreaData = $widgetData[$this->getName()];
foreach($widgetAreaData as $newWidgetID => $newWidgetData) {
foreach ($widgetAreaData as $newWidgetID => $newWidgetData) {
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) {
if (!is_numeric($newWidgetID)) {
$newWidgetID = 0;
}
$widget = null;
if($newWidgetID) {
if ($newWidgetID) {
// \"ParentID\" = '0' is for the new page
$widget = Widget::get()
->filter('ParentID', array(0, $record->$name()->ID))
->byID($newWidgetID);
// check if we are updating an existing widget
if($widget && isset($missingWidgets[$widget->ID])) {
if ($widget && isset($missingWidgets[$widget->ID])) {
unset($missingWidgets[$widget->ID]);
}
}
// create a new object
if(!$widget
if (!$widget
&& !empty($newWidgetData['Type'])
&& class_exists($newWidgetData['Type'])
&& is_subclass_of($newWidgetData['Type'], 'Widget')
@ -156,8 +155,8 @@ class WidgetAreaEditor extends FormField {
$widget->ParentID = $record->$name()->ID;
}
if($widget) {
if($widget->ParentID == 0) {
if ($widget) {
if ($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID;
}
@ -167,9 +166,9 @@ class WidgetAreaEditor extends FormField {
}
// remove the fields not saved
if($missingWidgets) {
foreach($missingWidgets as $removedWidget) {
if(isset($removedWidget) && is_numeric($removedWidget->ID)) {
if ($missingWidgets) {
foreach ($missingWidgets as $removedWidget) {
if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}

View File

@ -11,7 +11,6 @@
* @package widgets
*/
class Widget extends DataObject {
/**
* @var array
*/
@ -95,8 +94,8 @@ class Widget extends DataObject {
* Default way to render widget in templates.
* @return string HTML
*/
public function forTemplate($holder = true){
if($holder){
public function forTemplate($holder = true) {
if ($holder) {
return $this->WidgetHolder();
}
return $this->Content();
@ -200,7 +199,7 @@ class Widget extends DataObject {
$fields = $this->getCMSFields();
$outputFields = new FieldList();
foreach($fields as $field) {
foreach ($fields as $field) {
$name = $field->getName();
$value = $this->getField($name);
if ($value) {
@ -234,25 +233,25 @@ class Widget extends DataObject {
* @return WidgetController
*/
public function getController() {
if($this->controller) {
if ($this->controller) {
return $this->controller;
}
foreach(array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
foreach (array_reverse(ClassInfo::ancestry($this->class)) as $widgetClass) {
$controllerClass = "{$widgetClass}_Controller";
if(class_exists($controllerClass)) {
if (class_exists($controllerClass)) {
break;
}
$controllerClass = "{$widgetClass}Controller";
if(class_exists($controllerClass)) {
if (class_exists($controllerClass)) {
break;
}
}
if(!class_exists($controllerClass)) {
if (!class_exists($controllerClass)) {
throw new Exception("Could not find controller class for $this->classname");
}
@ -266,21 +265,20 @@ class Widget extends DataObject {
*/
public function populateFromPostData($data) {
$fields = $this->getCMSFields();
foreach($data as $name => $value) {
if($name != "Type") {
foreach ($data as $name => $value) {
if ($name != "Type") {
if ($field = $fields->dataFieldByName($name)) {
$field->setValue($value);
$field->saveInto($this);
}
else {
} else {
$this->setField($name, $value);
}
}
}
//Look for checkbox fields not present in the data
foreach($fields as $field) {
if($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
foreach ($fields as $field) {
if ($field instanceof CheckboxField && !array_key_exists($field->getName(), $data)) {
$field->setValue(false);
$field->saveInto($this);
}
@ -293,4 +291,3 @@ class Widget extends DataObject {
$this->write();
}
}

View File

@ -6,7 +6,6 @@
* @package widgets
*/
class WidgetArea extends DataObject {
/**
* @var array
*/
@ -30,7 +29,7 @@ class WidgetArea extends DataObject {
public function WidgetControllers() {
$controllers = new ArrayList();
foreach($this->ItemsToRender() as $widget) {
foreach ($this->ItemsToRender() as $widget) {
$controller = $widget->getController();
$controller->init();
@ -75,9 +74,8 @@ class WidgetArea extends DataObject {
*/
public function onBeforeDelete() {
parent::onBeforeDelete();
foreach($this->Widgets() as $widget) {
foreach ($this->Widgets() as $widget) {
$widget->delete();
}
}
}

View File

@ -22,7 +22,7 @@ class WidgetAreaEditorTest extends SapphireTest {
)
);
function testFillingOneArea() {
public function testFillingOneArea() {
$data = array(
'Widget' => array(
'BottomBar' => array(
@ -53,7 +53,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testFillingTwoAreas() {
public function testFillingTwoAreas() {
$data = array(
'Widget' => array(
'SideBar' => array(
@ -96,7 +96,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($bottWidgets[0]->Title(), 'MyTestWidgetBottom');
}
function testDeletingOneWidgetFromOneArea() {
public function testDeletingOneWidgetFromOneArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
@ -162,7 +162,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testDeletingAWidgetFromEachArea() {
public function testDeletingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
@ -222,7 +222,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
}
function testEditingOneWidget() {
public function testEditingOneWidget() {
// First get some widgets in there
$data = array(
'Widget' => array(
@ -294,7 +294,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditingAWidgetFromEachArea() {
public function testEditingAWidgetFromEachArea() {
// First get some widgets in there
$data = array(
'Widget' => array(
@ -366,7 +366,7 @@ class WidgetAreaEditorTest extends SapphireTest {
$this->assertEquals($sideWidgets[0]->Title(), 'MyTestWidgetSide-edited');
}
function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() {
// First get some widgets in there
$data = array(
'Widget' => array(

View File

@ -4,7 +4,6 @@
* @subpackage tests
*/
class WidgetControllerTest extends FunctionalTest {
protected static $fixture_file = 'WidgetControllerTest.yml';
protected $extraDataObjects = array(
@ -12,7 +11,7 @@ class WidgetControllerTest extends FunctionalTest {
'WidgetControllerTest_Widget',
);
function testWidgetFormRendering() {
public function testWidgetFormRendering() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
@ -28,7 +27,7 @@ class WidgetControllerTest extends FunctionalTest {
);
}
function testWidgetFormSubmission() {
public function testWidgetFormSubmission() {
$page = $this->objFromFixture('WidgetControllerTestPage', 'page1');
$page->publish('Stage', 'Live');
@ -64,13 +63,13 @@ class WidgetControllerTest_Widget extends Widget implements TestOnly {
* @package widgets
* @subpackage tests
*/
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly {
class WidgetControllerTest_WidgetController extends WidgetController implements TestOnly
{
private static $allowed_actions = array(
'Form'
);
function Form() {
public function Form() {
$widgetform = new Form(
$this,
'Form',
@ -85,7 +84,7 @@ class WidgetControllerTest_WidgetController extends WidgetController implements
return $widgetform;
}
function doAction($data, $form) {
public function doAction($data, $form) {
return sprintf('TestValue: %s\nWidget ID: %d',
$data['TestValue'],
$this->widget->ID

View File

@ -14,12 +14,11 @@ class WidgetControllerTestPage extends Page implements TestOnly {
* @subpackage tests
*/
class WidgetControllerTestPage_Controller extends Page_Controller implements TestOnly {
/**
* Template selection doesnt work in test folders,
* so we enforce a template name.
*/
function getViewer($action) {
public function getViewer($action) {
$templates = array('WidgetControllerTestPage');
return new SSViewer($templates);