silverstripe-widgets/code/form/WidgetAreaEditor.php

169 lines
3.8 KiB
PHP
Raw Normal View History

2012-04-18 23:15:45 +02:00
<?php
2012-04-18 23:15:45 +02:00
/**
* Special field type for selecting and configuring widgets on a page.
*
* @package widgets
2012-04-18 23:15:45 +02:00
*/
class WidgetAreaEditor extends FormField {
/**
* @param string $name
* @param array $widgetClasses
* @param int $maxWidgets
*/
public function __construct($name, $widgetClasses = array('Widget'), $maxWidgets = 0) {
2012-04-18 23:15:45 +02:00
$this->MaxWidgets = $maxWidgets;
$this->widgetClasses = $widgetClasses;
parent::__construct($name);
}
/**
* @param array $properties
*
* @return string - HTML
*/
public function FieldHolder($properties = array()) {
2012-04-18 23:15:45 +02:00
Requirements::css('widgets/css/WidgetAreaEditor.css');
Requirements::javascript('widgets/javascript/WidgetAreaEditor.js');
2012-04-18 23:15:45 +02:00
return $this->renderWith("WidgetAreaEditor");
}
/**
*
* @return ArrayList
*/
public function AvailableWidgets() {
2012-04-18 23:15:45 +02:00
$widgets= new ArrayList();
foreach($this->widgetClasses as $widgetClass) {
$classes = ClassInfo::subclassesFor($widgetClass);
if(count($classes) > 1) {
array_shift($classes);
}
2012-04-18 23:15:45 +02:00
foreach($classes as $class) {
$widgets->push(singleton($class));
}
}
return $widgets;
}
/**
* @return HasManyList
*/
public function UsedWidgets() {
2012-04-18 23:15:45 +02:00
// Call class_exists() to load Widget.php earlier and avoid a segfault
class_exists('Widget');
$relationName = $this->name;
$widgets = $this->form->getRecord()->getComponent($relationName)->Items();
2012-04-18 23:15:45 +02:00
return $widgets;
}
/**
* @return string
*/
public function IdxField() {
2012-04-18 23:15:45 +02:00
return $this->id() . 'ID';
}
/**
*
* @return int
*/
public function Value() {
2012-04-18 23:15:45 +02:00
$relationName = $this->name;
2012-04-18 23:15:45 +02:00
return $this->form->getRecord()->getComponent($relationName)->ID;
}
/**
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record) {
2012-04-18 23:15:45 +02:00
$name = $this->name;
$idName = $name . "ID";
$widgetarea = $record->getComponent($name);
$widgetarea->write();
$record->$idName = $widgetarea->ID;
$widgets = $widgetarea->Items();
// store the field IDs and delete the missing fields
// alternatively, we could delete all the fields and re add them
$missingWidgets = array();
if($widgets) {
foreach($widgets as $existingWidget) {
$missingWidgets[$existingWidget->ID] = $existingWidget;
}
}
if(isset($_REQUEST['Widget'])) {
foreach(array_keys($_REQUEST['Widget']) as $widgetAreaName) {
if ($widgetAreaName !== $this->name) {
continue;
}
foreach(array_keys($_REQUEST['Widget'][$widgetAreaName]) as $newWidgetID) {
$newWidgetData = $_REQUEST['Widget'][$widgetAreaName][$newWidgetID];
// Sometimes the id is "new-1" or similar, ensure this doesn't get into the query
if(!is_numeric($newWidgetID)) {
$newWidgetID = 0;
}
// \"ParentID\" = '0' is for the new page
$widget = DataObject::get_one(
2012-04-18 23:15:45 +02:00
'Widget',
"(\"ParentID\" = '{$record->$name()->ID}' OR ".
"\"ParentID\" = '0') AND \"Widget\".\"ID\" = '$newWidgetID'"
2012-04-18 23:15:45 +02:00
);
// check if we are updating an existing widget
2012-04-18 23:15:45 +02:00
if($widget && isset($missingWidgets[$widget->ID])) {
unset($missingWidgets[$widget->ID]);
2012-04-18 23:15:45 +02:00
}
// 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;
}
}
2012-04-18 23:15:45 +02:00
if($widget) {
if($widget->ParentID == 0) {
$widget->ParentID = $record->$name()->ID;
}
2012-04-18 23:15:45 +02:00
$widget->populateFromPostData($newWidgetData);
}
}
}
}
// remove the fields not saved
if($missingWidgets) {
foreach($missingWidgets as $removedWidget) {
if(isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}
}
}
}