Example #1
0
 /**
  * Find a widgetMap by widgetId and view.
  *
  * @param int  $widgetId
  * @param View $view
  *
  * @return WidgetMap
  **/
 public function removeWidgetMapByWidgetIdAndView($widgetId, View $view)
 {
     foreach ($view->getSlots() as $_slot) {
         foreach ($_slot->getWidgetMaps() as $_widgetMap) {
             if ($_widgetMap->getWidgetId() == $widgetId) {
                 $_slot->removeWidgetMap($_widgetMap);
                 //update the widget map
                 $view->updateWidgetMapBySlots();
                 return ['success' => true];
             }
         }
     }
     return ['success' => false, 'message' => 'The widget isn\'t present in widgetMap...'];
 }
Example #2
0
 public function build(View $view, $updatePage = true)
 {
     $viewWidgetMaps = null;
     $widgetMap = array();
     //get the template widget map
     $template = $view->getTemplate();
     if ($template !== null) {
         $widgetMap = $this->build($template);
     }
     // build the view widgetMaps for each its slots
     foreach ($view->getSlots() as $slot) {
         if (empty($widgetMap[$slot->getId()])) {
             $widgetMap[$slot->getId()] = array();
         }
         if ($slot !== null) {
             $viewWidgetMaps = $slot->getWidgetMaps();
         }
         //if the current view have some widget maps
         if ($viewWidgetMaps !== null) {
             //we parse the widget maps
             foreach ($viewWidgetMaps as $viewWidgetMap) {
                 $viewWidgetMap = clone $viewWidgetMap;
                 //depending on the action
                 $action = $viewWidgetMap->getAction();
                 switch ($action) {
                     case WidgetMap::ACTION_CREATE:
                         $position = (int) $viewWidgetMap->getPosition();
                         $reference = (int) $viewWidgetMap->getPositionReference();
                         if ($reference != 0) {
                             if (isset($widgetMap[$slot->getId()])) {
                                 foreach ($widgetMap[$slot->getId()] as $key => $_widgetMap) {
                                     if ($_widgetMap->getWidgetId() === $reference) {
                                         $position += $_widgetMap->getPosition();
                                     }
                                 }
                             }
                         }
                         array_splice($widgetMap[$slot->getId()], $position - 1, 0, array($viewWidgetMap));
                         array_map(function ($key, $_widgetMap) {
                             $_widgetMap->setPosition($key + 1);
                         }, array_keys($widgetMap[$slot->getId()]), $widgetMap[$slot->getId()]);
                         break;
                     case WidgetMap::ACTION_OVERWRITE:
                         //parse the widget maps
                         $position = null;
                         foreach ($widgetMap[$slot->getId()] as $index => $wm) {
                             if ($wm->getWidgetId() == $viewWidgetMap->getReplacedWidgetId()) {
                                 if (null === $viewWidgetMap->isAsynchronous()) {
                                     $viewWidgetMap->setAsynchronous($wm->isAsynchronous());
                                 }
                                 //replace the widget map from the list
                                 unset($widgetMap[$slot->getId()][$index]);
                                 break;
                             }
                         }
                         array_splice($widgetMap[$slot->getId()], $viewWidgetMap->getPosition() - 1, 0, array($viewWidgetMap));
                         array_map(function ($key, $_widgetMap) {
                             $_widgetMap->setPosition($key + 1);
                         }, array_keys($widgetMap[$slot->getId()]), $widgetMap[$slot->getId()]);
                         break;
                     case WidgetMap::ACTION_DELETE:
                         //parse the widget maps
                         foreach ($widgetMap[$slot->getId()] as $index => $wm) {
                             if ($wm->getWidgetId() === $viewWidgetMap->getWidgetId()) {
                                 //remove the widget map from the list
                                 unset($widgetMap[$slot->getId()][$index]);
                             }
                         }
                         break;
                     default:
                         throw new \Exception('The action [' . $action . '] is not handled yet.');
                         break;
                 }
             }
             ksort($widgetMap[$slot->getId()]);
         }
     }
     if ($updatePage) {
         $view->setBuiltWidgetMap($widgetMap);
     }
     return $widgetMap;
 }