Example #1
0
 public function rebuild(View $view)
 {
     $widgetMap = array();
     if ($view->getTemplate()) {
         $widgetMap = $view->getTemplate()->getWidgetMap();
     }
     foreach ($view->getWidgets() as $widget) {
         if (!isset($widgetMap[$widget->getSlot()])) {
             $widgetMap[$widget->getSlot()] = array();
         }
         //create the new widget map
         $widgetMapEntry = new WidgetMap();
         $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
         $widgetMapEntry->setWidgetId($widget->getId());
         $widgetMapEntry->setAsynchronous($widget->isAsynchronous());
         $widgetMapEntry = $this->helper->generateWidgetPosition($widgetMapEntry, $widget, $widgetMap, null);
         $widgetMap[$widget->getSlot()][] = $widgetMapEntry;
     }
     $widgetMapAsArray = array();
     foreach ($widgetMap as $slotId => $widgetMapItems) {
         foreach ($widgetMapItems as $widgetMapItem) {
             $widgetMapAsArray[$slotId][] = $this->widgetMapTransformer->transform($widgetMapItem);
         }
     }
     $view->setWidgetMap($widgetMapAsArray);
 }
 /**
  * Guess the position of the widget relatively to the positionReference.
  *
  * @param Widget $widget            The widget to position
  * @param int    $positionReference Id of the parent widget
  *
  * @return WidgetMap The position of the widget
  */
 public function generateWidgetPosition(EntityManager $entityManager, WidgetMap $widgetMapEntry, $widget, $widgetMap, $positionReference)
 {
     $position = 1;
     $slotId = $widget->getSlot();
     if (empty($widgetMap[$slotId])) {
         $widgetMapEntry->setPosition($position);
         return $widgetMapEntry;
     }
     $slot = $widgetMap[$slotId];
     $referenceWidget = $entityManager->getRepository('Victoire\\Bundle\\WidgetBundle\\Entity\\Widget')->findOneById($positionReference);
     //If we added a widget just after a parent widget
     //The position of the new widget is the one just after the parent widget
     if ($referenceWidget && $widget->getView() !== $referenceWidget->getView()) {
         $position = 1;
         $widgetMapEntry->setPosition($position);
         $widgetMapEntry->setPositionReference($positionReference);
     } else {
         foreach ($slot as $key => $_widgetMap) {
             if ($_widgetMap->getWidgetId() === (int) $positionReference) {
                 $widgetMapEntry->setPosition($_widgetMap->getPosition() + 1);
                 break;
             } elseif (0 === (int) $positionReference) {
                 $widgetMapEntry->setPosition(1);
             }
         }
     }
     return $widgetMapEntry;
 }
 /**
  * Transforms an array to an object (WidgetMap).
  * @param  array $widgetMapAsArray
  *
  * @return WidgetMap|null
  * @throws TransformationFailedException if object (issue) is not found.
  */
 public function reverseTransform($widgetMapAsArray)
 {
     if (!$widgetMapAsArray) {
         return null;
     }
     $widgetMap = new WidgetMap();
     $widgetMap->setAction(@$widgetMapAsArray['action']);
     $widgetMap->setPosition(@$widgetMapAsArray['position']);
     $widgetMap->setPositionReference(@$widgetMapAsArray['positionReference']);
     $widgetMap->setAsynchronous(@$widgetMapAsArray['asynchronous']);
     $widgetMap->setReplacedWidgetId(@$widgetMapAsArray['replacedWidgetId']);
     $widgetMap->setWidgetId(intval($widgetMapAsArray['widgetId']));
     return $widgetMap;
 }
Example #4
0
 /**
  * create a widgetMap for the new Widget cloned
  *
  * @return void
  **/
 public function overwriteWidgetMap(Widget $widgetCopy, Widget $widget, View $view)
 {
     //the id of the new widget
     $widgetId = $widgetCopy->getId();
     //the widget slot
     $widgetSlotId = $widget->getSlot();
     //the widget id
     $replacedWidgetId = $widget->getId();
     //get the slot
     $slot = $view->getSlotById($widgetSlotId);
     //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);
     }
     $originalWidgetMap = $slot->getWidgetMapByWidgetId($replacedWidgetId);
     //If widgetmap was not found current view, we dig
     if (!$originalWidgetMap) {
         $watchDog = 100;
         $_view = $view;
         while (!$originalWidgetMap && $watchDog) {
             $_view = $_view->getTemplate();
             $parentSlot = $_view->getSlotById($widgetSlotId);
             if ($parentSlot) {
                 $originalWidgetMap = $parentSlot->getWidgetMapByWidgetId($replacedWidgetId);
             }
             $watchDog--;
         }
         if (0 == $watchDog) {
             throw new \Exception(sprintf("The slot %s doesn't appears to be in any templates WidgetMaps. You should check this manually.", $widgetSlotId));
         }
     }
     //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_OVERWRITE);
     $widgetMap->setReplacedWidgetId($replacedWidgetId);
     $widgetMap->setWidgetId($widgetId);
     $widgetMap->setPosition($originalWidgetMap->getPosition());
     $widgetMap->setAsynchronous($widgetCopy->isAsynchronous());
     $widgetMap->setPositionReference($originalWidgetMap->getPositionReference());
     $slot->addWidgetMap($widgetMap);
 }
Example #5
0
 /**
  * Create a widget.
  *
  * @param string $mode
  * @param string $type
  * @param string $slotId
  * @param View   $view
  * @param string $entity
  * @param int    $positionReference
  * @param string $type
  *
  * @throws \Exception
  *
  * @return Template
  */
 public function createWidget($mode, $type, $slotId, View $view, $entity, $positionReference)
 {
     //services
     $formErrorHelper = $this->formErrorHelper;
     $request = $this->request;
     if ($view instanceof VirtualBusinessPage) {
         $this->virtualToBpTransformer->transform($view);
     }
     //create a new widget
     $widget = $this->widgetHelper->newWidgetInstance($type, $view, $slotId, $mode);
     $form = $this->widgetFormBuilder->callBuildFormSwitchParameters($widget, $view, $entity, $positionReference);
     $noValidate = $request->query->get('novalidate', false);
     $form->handleRequest($request);
     if ($noValidate === false && $form->isValid()) {
         if (!$view->getId()) {
             //create a view for the business entity instance if we are currently on a virtual one
             $this->entityManager->persist($view);
         }
         //get the widget from the form
         $widget = $form->getData();
         //update fields of the widget
         $widget->setBusinessEntityId($entity);
         $widget->positionReference = $positionReference;
         $widget->slotId = $slotId;
         //persist the widget
         $this->entityManager->persist($widget);
         $this->entityManager->flush();
         //create the new widget map
         $widgetMapEntry = new WidgetMap();
         $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
         $widgetMapEntry->setWidgetId($widget->getId());
         $widgetMap = $this->widgetMapBuilder->build($view, false);
         $widgetMapEntry = $this->widgetMapPositionBuilder->generateWidgetPosition($this->entityManager, $widgetMapEntry, $widget, $widgetMap, $positionReference);
         $this->widgetMapHelper->insertWidgetMapInSlot($slotId, $widgetMapEntry, $view);
         $this->entityManager->persist($view);
         $this->entityManager->flush();
         $widget->setCurrentView($view);
         //get the html for the widget
         $htmlWidget = $this->widgetRenderer->renderContainer($widget, $view);
         $response = ['success' => true, 'widgetId' => $widget->getId(), 'html' => $htmlWidget];
     } else {
         //get the errors as a string
         $response = ['success' => false, 'message' => $noValidate === false ? $formErrorHelper->getRecursiveReadableErrors($form) : null, 'html' => $this->widgetFormBuilder->renderNewForm($form, $widget, $slotId, $view, $entity)];
     }
     return $response;
 }
Example #6
0
 /**
  * Method called once the entity is loaded.
  *
  * @ORM\PostLoad
  */
 public function postLoad()
 {
     $widgetMap = $this->widgetMap;
     //the slots of the page
     $slots = [];
     //convert the widget map array as objects
     foreach ($widgetMap as $slotId => $_widgetMapEntries) {
         $slot = new Slot();
         $slot->setId($slotId);
         foreach ($_widgetMapEntries as $_widgetMapEntry) {
             $_widgetMap = new WidgetMap();
             $_widgetMap->setAction(@$_widgetMapEntry['action']);
             $_widgetMap->setPosition(@$_widgetMapEntry['position']);
             $_widgetMap->setPositionReference(@$_widgetMapEntry['positionReference']);
             $_widgetMap->setAsynchronous(isset($_widgetMapEntry['asynchronous']) ? $_widgetMapEntry['asynchronous'] : null);
             $_widgetMap->setReplacedWidgetId(@$_widgetMapEntry['replacedWidgetId']);
             $_widgetMap->setWidgetId(intval($_widgetMapEntry['widgetId']));
             $slot->addWidgetMap($_widgetMap);
         }
         $slots[] = $slot;
     }
     //set the slots to the page
     $this->slots = $slots;
 }
Example #7
0
 /**
  * Create a widget
  * @param string  $mode
  * @param string  $type
  * @param string  $slotId
  * @param View    $view
  * @param string  $entity
  * @param integer $positionReference
  *
  * @param string $type
  * @throws \Exception
  * @return Template
  *
  */
 public function createWidget($mode, $type, $slotId, View $view, $entity, $positionReference)
 {
     //services
     $formErrorHelper = $this->formErrorHelper;
     $request = $this->request;
     //create a new widget
     $widget = $this->widgetHelper->newWidgetInstance($type, $view, $slotId, $mode);
     $form = $this->widgetFormBuilder->callBuildFormSwitchParameters($widget, $view, $entity, $positionReference);
     $form->handleRequest($request);
     if ($request->query->get('novalidate', false) === false && $form->isValid()) {
         $needViewCacheUpdate = false;
         if (!$view->getId()) {
             //create a view for the business entity instance if we are currently on a virtual one
             $this->em->persist($view);
             //REBUILD VIEWS REFERENCE
             $needViewCacheUpdate = true;
         }
         //get the widget from the form
         $widget = $form->getData();
         //update fields of the widget
         $widget->setBusinessEntityName($entity);
         //persist the widget
         $this->em->persist($widget);
         $this->em->flush();
         if (true === $needViewCacheUpdate) {
             $this->pageHelper->viewCacheHelper->update($view);
         }
         //create the new widget map
         $widgetMapEntry = new WidgetMap();
         $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
         $widgetMapEntry->setWidgetId($widget->getId());
         $widgetMap = $this->widgetMapBuilder->build($view, false);
         $widgetMapEntry = $this->widgetMapHelper->generateWidgetPosition($widgetMapEntry, $widget, $widgetMap, $positionReference);
         $this->widgetMapHelper->insertWidgetMapInSlot($slotId, $widgetMapEntry, $view);
         $this->em->persist($view);
         $this->em->flush();
         $widget->setCurrentView($view);
         //get the html for the widget
         $htmlWidget = $this->widgetRenderer->renderContainer($widget, $view, $widgetMapEntry->getPosition());
         $htmlWidget .= WidgetRenderer::$newContentActionButtonHtml;
         $response = array("success" => true, 'widgetId' => $widget->getId(), "html" => $htmlWidget);
     } else {
         //get the errors as a string
         $response = array("success" => false, "message" => $formErrorHelper->getRecursiveReadableErrors($form), "html" => $this->widgetFormBuilder->renderNewForm($form, $widget, $slotId, $view, $entity));
     }
     return $response;
 }