Exemple #1
0
 /**
  * Delete the widget from the view
  *
  * @param View   $view
  * @param Widget $widget
  *
  * @throws \Exception The slot does not exists
  */
 public function deleteWidgetFromView(View $view, Widget $widget)
 {
     //the widget view
     $widgetView = $widget->getView();
     //the widget slot
     $widgetSlotId = $widget->getSlot();
     //the widget id
     $widgetId = $widget->getId();
     //get the slot
     $slot = $view->getSlotById($widgetSlotId);
     //we remove the widget from the current view
     if ($widgetView === $view) {
         //test that the slot for the widget exists
         if ($slot === null) {
             throw new \Exception('The slot[' . $widgetSlotId . '] for the widget [' . $widgetId . '] of the view [' . $view->getId() . '] was not found.');
         }
         //get the widget map
         $widgetMap = $slot->getWidgetMapByWidgetId($widgetId);
         //check that the widget map exists
         if ($widgetMap === null) {
             throw new \Exception('The widgetMap for the widget [' . $widgetId . '] and the view [' . $view->getId() . '] does not exists.');
         }
         //remove the widget map from the slot
         $slot->removeWidgetMap($widgetMap);
     } else {
         //there might be no slot yet for the child view
         if ($slot === null) {
             //create a new slot
             $slot = new Slot();
             $slot->setId($widgetSlotId);
             //add the new slot to the view
             $view->addSlot($slot);
         }
         //the widget is owned by another view (a parent)
         //so we add a new widget map that indicates we delete this widget
         $widgetMap = new WidgetMap();
         $widgetMap->setAction(WidgetMap::ACTION_DELETE);
         $widgetMap->setWidgetId($widgetId);
         $slot->addWidgetMap($widgetMap);
     }
 }