2012-04-18 23:15:45 +02:00
|
|
|
<?php
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
/**
|
|
|
|
* Special field type for selecting and configuring widgets on a page.
|
2013-05-07 10:56:38 +02:00
|
|
|
*
|
|
|
|
* @package widgets
|
2012-04-18 23:15:45 +02:00
|
|
|
*/
|
|
|
|
class WidgetAreaEditor extends FormField {
|
|
|
|
|
2012-10-05 23:47:26 +02:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param array $widgetClasses
|
|
|
|
* @param int $maxWidgets
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $properties
|
2013-05-07 10:56:38 +02:00
|
|
|
*
|
|
|
|
* @return string - HTML
|
2012-10-05 23:47:26 +02:00
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
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');
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->renderWith("WidgetAreaEditor");
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function AvailableWidgets() {
|
2012-04-18 23:15:45 +02:00
|
|
|
$widgets= new ArrayList();
|
2013-04-22 15:18:07 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
foreach($this->widgetClasses as $widgetClass) {
|
|
|
|
$classes = ClassInfo::subclassesFor($widgetClass);
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2013-05-07 12:22:41 +02:00
|
|
|
if (isset($classes['Widget'])) {
|
|
|
|
unset($classes['Widget']);
|
|
|
|
}
|
|
|
|
else if (isset($classes[0]) && $classes[0] == 'Widget') {
|
|
|
|
unset($classes[0]);
|
2013-05-07 10:56:38 +02:00
|
|
|
}
|
2013-05-07 12:22:41 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
foreach($classes as $class) {
|
2013-08-17 02:46:39 +02:00
|
|
|
|
|
|
|
$available = Config::inst()->get($class, 'only_available_in');
|
|
|
|
|
|
|
|
if (!empty($available) && is_array($available)) {
|
|
|
|
if(in_array($this->Name, $available)) {
|
2013-04-22 15:18:07 +02:00
|
|
|
$widgets->push(singleton($class));
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
$widgets->push(singleton($class));
|
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $widgets;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasManyList
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
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();
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
return $widgets;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function IdxField() {
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->id() . 'ID';
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
public function Value() {
|
2012-04-18 23:15:45 +02:00
|
|
|
$relationName = $this->name;
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2012-04-18 23:15:45 +02:00
|
|
|
return $this->form->getRecord()->getComponent($relationName)->ID;
|
|
|
|
}
|
2012-10-05 23:47:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DataObjectInterface $record
|
|
|
|
*/
|
2012-10-05 23:39:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:02:46 +01:00
|
|
|
if(!$this->getForm()) throw new Exception("no form");
|
2012-04-18 23:15:45 +02:00
|
|
|
|
2015-03-06 00:02:46 +01:00
|
|
|
$widgetData = $this->getForm()->getRequest()->requestVar('Widget');
|
|
|
|
if($widgetData && isset($widgetData[$this->getName()])) {
|
|
|
|
$widgetAreaData = $widgetData[$this->getName()];
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
$newWidgetID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$widget = null;
|
|
|
|
if($newWidgetID) {
|
2012-04-18 23:15:45 +02:00
|
|
|
// \"ParentID\" = '0' is for the new page
|
2015-03-06 00:02:46 +01:00
|
|
|
$widget = Widget::get()
|
|
|
|
->filter('ParentID', array(0, $record->$name()->ID))
|
|
|
|
->byID($newWidgetID);
|
2012-04-18 23:15:45 +02:00
|
|
|
|
2012-10-05 23:27:02 +02:00
|
|
|
// check if we are updating an existing widget
|
2012-04-18 23:15:45 +02:00
|
|
|
if($widget && isset($missingWidgets[$widget->ID])) {
|
2012-10-05 23:27:02 +02:00
|
|
|
unset($missingWidgets[$widget->ID]);
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2015-03-06 00:02:46 +01:00
|
|
|
}
|
2012-04-18 23:15:45 +02:00
|
|
|
|
2015-03-06 00:02:46 +01:00
|
|
|
// create a new object
|
|
|
|
if(!$widget
|
|
|
|
&& !empty($newWidgetData['Type'])
|
|
|
|
&& class_exists($newWidgetData['Type'])
|
|
|
|
&& is_subclass_of($newWidgetData['Type'], 'Widget')
|
|
|
|
) {
|
|
|
|
$widget = Injector::inst()->create($newWidgetData['Type']);
|
|
|
|
$widget->ID = 0;
|
|
|
|
$widget->ParentID = $record->$name()->ID;
|
|
|
|
}
|
2013-05-07 10:56:38 +02:00
|
|
|
|
2015-03-06 00:02:46 +01:00
|
|
|
if($widget) {
|
|
|
|
if($widget->ParentID == 0) {
|
|
|
|
$widget->ParentID = $record->$name()->ID;
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
2015-03-06 00:02:46 +01:00
|
|
|
|
|
|
|
$widget->populateFromPostData($newWidgetData);
|
2012-04-18 23:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the fields not saved
|
|
|
|
if($missingWidgets) {
|
|
|
|
foreach($missingWidgets as $removedWidget) {
|
|
|
|
if(isset($removedWidget) && is_numeric($removedWidget->ID)) {
|
|
|
|
$removedWidget->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|