/**
  * @param DataObject $owner
  * @param string $association_name
  * @param string $inversed_by
  */
 public function __construct(DataObject $owner, $association_name, $inversed_by = null, QueryObject $target_query = null)
 {
     $this->owner = $owner;
     $this->association_name = $association_name;
     $this->inversed_by = $inversed_by;
     $this->target_class = $this->owner->has_one($association_name);
     $this->snapshot = $this->owner->getComponent($this->association_name);
     $this->target = $this->snapshot;
     if (is_null($target_query)) {
         $target_query = new QueryObject();
     }
     $this->target_query = $target_query;
     UnitOfWork::getInstance()->loadMany2OneAssociation($this);
 }
 /**
  * Recursivly search for a PlaceholderImage.
  *
  * @param DataObject $object
  * @return Image | null
  */
 protected function getPlaceholderImageRecursive(DataObject $object)
 {
     if ($object->has_one('PlaceholderImage')) {
         $image = $object->getComponent('PlaceholderImage');
         if ($image->exists()) {
             return $image;
         }
     }
     $parentObject = $object->hasMethod('Parent') ? $object->Parent() : null;
     return isset($parentObject) && $parentObject->exists() ? $this->getPlaceholderImageRecursive($parentObject) : null;
 }
 function saveInto(DataObject $record)
 {
     $name = $this->name;
     $idName = $name . "ID";
     $widgetarea = $record->getComponent($name);
     $widgetarea->write();
     $record->{$idName} = $widgetarea->ID;
     $widgets = $widgetarea->Widgets();
     // store the field IDs and delete the missing fields
     // alternatively, we could delete all the fields and re add them
     $missingWidgets = array();
     foreach ($widgets as $existingWidget) {
         $missingWidgets[$existingWidget->ID] = $existingWidget;
     }
     // write the new widgets to the database
     if (isset($_REQUEST['Widget'])) {
         foreach (array_keys($_REQUEST['Widget']) as $newWidgetID) {
             $newWidgetData = $_REQUEST['Widget'][$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', sprintf('(`ParentID` = %d OR `ParentID` = 0) AND `Widget`.`ID` = %d', $record->{$name}()->ID, (int) $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;
                 }
                 $widget->populateFromPostData($newWidgetData);
                 //$editable->write();
             }
         }
     }
     // remove the fields not saved
     foreach ($missingWidgets as $removedWidget) {
         if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
             $removedWidget->delete();
         }
     }
 }
 function saveInto(DataObject $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 (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('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;
                     }
                     // echo "Saving $widget->ID into $name/$widget->ParentID\n<br/>";
                     $widget->populateFromPostData($newWidgetData);
                 }
             }
         }
     }
     // remove the fields not saved
     if ($missingWidgets) {
         foreach ($missingWidgets as $removedWidget) {
             if (isset($removedWidget) && is_numeric($removedWidget->ID)) {
                 $removedWidget->delete();
             }
         }
     }
 }