2007-08-03 00:07:11 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Special field type for selecting and configuring widgets on a page.
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2007-08-03 00:07:11 +02:00
|
|
|
class WidgetAreaEditor extends FormField {
|
2009-12-16 07:06:30 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
$this->widgetClasses = $widgetClasses;
|
|
|
|
|
2009-11-21 03:00:58 +01:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
2012-04-11 08:09:11 +02:00
|
|
|
function FieldHolder($properties = array()) {
|
2008-11-12 05:31:53 +01:00
|
|
|
Requirements::css(CMS_DIR . '/css/WidgetAreaEditor.css');
|
2012-03-24 04:38:34 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/prototype/prototype.js");
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/behaviour/behaviour.js');
|
2008-11-12 05:31:53 +01:00
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/WidgetAreaEditor.js');
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
return $this->renderWith("WidgetAreaEditor");
|
|
|
|
}
|
|
|
|
|
|
|
|
function AvailableWidgets() {
|
2009-12-16 07:06:30 +01:00
|
|
|
|
2011-05-05 12:40:26 +02:00
|
|
|
$widgets= new ArrayList();
|
2007-08-03 00:07:11 +02:00
|
|
|
|
2009-12-16 07:06:30 +01:00
|
|
|
foreach($this->widgetClasses as $widgetClass) {
|
|
|
|
$classes = ClassInfo::subclassesFor($widgetClass);
|
|
|
|
array_shift($classes);
|
|
|
|
foreach($classes as $class) {
|
|
|
|
$widgets->push(singleton($class));
|
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $widgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
function UsedWidgets() {
|
2010-04-13 08:57:52 +02:00
|
|
|
// Call class_exists() to load Widget.php earlier and avoid a segfault
|
|
|
|
class_exists('Widget');
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
$relationName = $this->name;
|
2009-11-21 03:00:58 +01:00
|
|
|
$widgets = $this->form->getRecord()->getComponent($relationName)->Items();
|
2007-08-03 00:07:11 +02:00
|
|
|
return $widgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
function IdxField() {
|
|
|
|
return $this->id() . 'ID';
|
|
|
|
}
|
|
|
|
|
|
|
|
function Value() {
|
|
|
|
$relationName = $this->name;
|
2009-05-21 02:17:27 +02:00
|
|
|
return $this->form->getRecord()->getComponent($relationName)->ID;
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-12 01:23:15 +02:00
|
|
|
function saveInto(DataObjectInterface $record) {
|
2007-08-03 00:07:11 +02:00
|
|
|
$name = $this->name;
|
|
|
|
$idName = $name . "ID";
|
2009-11-05 01:13:56 +01:00
|
|
|
|
2009-05-21 02:17:27 +02:00
|
|
|
$widgetarea = $record->getComponent($name);
|
2007-08-03 00:07:11 +02:00
|
|
|
$widgetarea->write();
|
2009-05-06 05:31:15 +02:00
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
$record->$idName = $widgetarea->ID;
|
2009-11-05 01:13:56 +01:00
|
|
|
|
2009-11-21 03:00:58 +01:00
|
|
|
$widgets = $widgetarea->Items();
|
2009-11-05 01:13:56 +01:00
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// store the field IDs and delete the missing fields
|
|
|
|
// alternatively, we could delete all the fields and re add them
|
|
|
|
$missingWidgets = array();
|
2009-05-06 05:31:15 +02:00
|
|
|
|
|
|
|
if($widgets) {
|
|
|
|
foreach($widgets as $existingWidget) {
|
|
|
|
$missingWidgets[$existingWidget->ID] = $existingWidget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($_REQUEST['Widget'])) {
|
2009-11-05 01:13:56 +01:00
|
|
|
foreach(array_keys($_REQUEST['Widget']) as $widgetAreaName) {
|
|
|
|
if ($widgetAreaName !== $this->name) {
|
|
|
|
continue;
|
2009-05-06 05:31:15 +02:00
|
|
|
}
|
2009-11-05 01:13:56 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2009-05-06 05:31:15 +02:00
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
// \"ParentID\" = '0' is for the new page
|
|
|
|
$widget = DataObject::get_one(
|
|
|
|
'Widget',
|
|
|
|
"(\"ParentID\" = '{$record->$name()->ID}' OR \"ParentID\" = '0') AND \"Widget\".\"ID\" = '$newWidgetID'"
|
|
|
|
);
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
// check if we are updating an existing widget
|
|
|
|
if($widget && isset($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;
|
|
|
|
|
|
|
|
if(!is_subclass_of($widget, 'Widget')) {
|
|
|
|
$widget = null;
|
|
|
|
}
|
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
if($widget) {
|
|
|
|
if($widget->ParentID == 0) {
|
|
|
|
$widget->ParentID = $record->$name()->ID;
|
|
|
|
}
|
|
|
|
// echo "Saving $widget->ID into $name/$widget->ParentID\n<br/>";
|
|
|
|
$widget->populateFromPostData($newWidgetData);
|
2009-05-06 05:31:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the fields not saved
|
|
|
|
if($missingWidgets) {
|
|
|
|
foreach($missingWidgets as $removedWidget) {
|
2009-11-05 01:13:56 +01:00
|
|
|
if(isset($removedWidget) && is_numeric($removedWidget->ID)) {
|
|
|
|
$removedWidget->delete();
|
|
|
|
}
|
2009-05-06 05:31:15 +02:00
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 01:23:15 +02:00
|
|
|
}
|