本文整理汇总了PHP中DataObjectInterface::getComponent方法的典型用法代码示例。如果您正苦于以下问题:PHP DataObjectInterface::getComponent方法的具体用法?PHP DataObjectInterface::getComponent怎么用?PHP DataObjectInterface::getComponent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataObjectInterface
的用法示例。
在下文中一共展示了DataObjectInterface::getComponent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: saveInto
/**
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record)
{
$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 (!$this->getForm()) {
throw new Exception("no form");
}
$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) {
// \"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])) {
unset($missingWidgets[$widget->ID]);
}
}
// 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;
}
if ($widget) {
if ($widget->ParentID == 0) {
$widget->ParentID = $record->{$name}()->ID;
}
$widget->populateFromPostData($newWidgetData);
}
}
}
// remove the fields not saved
if ($missingWidgets) {
foreach ($missingWidgets as $removedWidget) {
if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}
}
}
示例2: saveInto
/**
* @param DataObjectInterface $record
*/
public function saveInto(DataObjectInterface $record)
{
if ($this->isDisabled() || $this->isReadonly()) {
return;
}
$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;
}
$widgetForm = new Form($this, 'WidgetForm', new FieldList(), new FieldList());
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('Widget', "(\"ParentID\"='{$record->{$name}()->ID}' OR " . "\"ParentID\"='0') AND \"Widget\".\"ID\"='{$newWidgetID}'");
// 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;
}
}
if ($widget) {
if ($widget->ParentID == 0) {
$widget->ParentID = $record->{$name}()->ID;
}
//Set the widget editor
$widget->setWidgetEditor($this);
//Set the form's fields
$widgetForm->setFields($widget->getCMSFields());
//Populate the form
$widgetForm->loadDataFrom($newWidgetData);
//Save the form into the widget and write
$widgetForm->saveInto($widget);
$widget->Sort = array_key_exists('Sort', $newWidgetData) ? $newWidgetData['Sort'] : $widget->Sort;
$widget->write();
}
}
}
}
// remove the fields not saved
if ($missingWidgets) {
foreach ($missingWidgets as $removedWidget) {
if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
$removedWidget->delete();
}
}
}
}
开发者ID:helpfulrobot,项目名称:undefinedoffset-silverstripe-advancedwidgeteditor,代码行数:76,代码来源:AdvancedWidgetAreaEditor.php