Пример #1
0
 public function init(Website $website, Request $request)
 {
     $this->installedWidgets = $website->getWidgets();
     $widgetRepo = new WidgetRepository($website);
     $widgetId = $request->getParamInt(0);
     if ($widgetId === 0) {
         // New widget
         $this->placedWidget = $this->getNewWidget($website, $request);
     } else {
         $this->placedWidget = $widgetRepo->getPlacedWidget($widgetId);
     }
     if ($request->hasRequestValue("submit") && Validate::requestToken($request)) {
         // Use incoming data
         $widgetDefinition = $this->installedWidgets->getDefinition($this->placedWidget);
         $data = $widgetDefinition->parseData($website, $widgetId);
         $this->placedWidget->setData($data);
         if ($this->isValid($data)) {
             // Save widget
             $widgetRepo->savePlacedWidget($this->placedWidget);
             $this->addSaveMessage($this->placedWidget, $website->getText());
         }
     }
     $this->requestToken = RequestToken::generateNew();
     $this->requestToken->saveToSession();
 }
Пример #2
0
 private function moveWidget(WidgetRepository $repo, $moveUp)
 {
     $delta = $moveUp ? -1 : 1;
     $documentId = $this->placedWidget->getDocumentId();
     $allWidgetsInDocument = array_values($repo->getWidgetsInDocumentWithId($documentId));
     $iterationStartPos = $moveUp ? 1 : 0;
     $iterationEndPos = $moveUp ? count($allWidgetsInDocument) : count($allWidgetsInDocument) - 1;
     $madeChanges = false;
     for ($i = $iterationStartPos; $i < $iterationEndPos; $i++) {
         $placedWidget = $allWidgetsInDocument[$i];
         if ($this->placedWidget->getId() == $placedWidget->getId()) {
             // Swap
             // Moving up:   place widget above here, then place desired
             //              widget in the (now free) slot above
             // Moving down: place widget below here, then place desired
             //              widget in the (now free) slot below
             $allWidgetsInDocument[$i] = $allWidgetsInDocument[$i + $delta];
             $allWidgetsInDocument[$i + $delta] = $this->placedWidget;
             $madeChanges = true;
             break;
         }
     }
     if ($madeChanges) {
         for ($i = 0; $i < count($allWidgetsInDocument); $i++) {
             $placedWidget = $allWidgetsInDocument[$i];
             $placedWidget->setPriority(count($allWidgetsInDocument) - $i);
             $repo->savePlacedWidget($placedWidget);
         }
     }
 }