示例#1
0
 /**
  * Creates a new placed widget. Won't be saved automatically, save it to a
  * widget repository.
  * @param string $baseDirectory Base directory of all widgets.
  * @param string $dirName Name of the widget.
  * @param Document $document The document the widget is placed in.
  * @return PlacedWidget The placed widget.
  * @throws NotFoundException If no widget exists with the given dirName.
  */
 public static function newPlacedWidget($baseDirectory, $dirName, Document $document)
 {
     $placedWidget = new PlacedWidget($baseDirectory);
     $placedWidget->setDocumentId($document->getId());
     $placedWidget->widgetName = (string) $dirName;
     $placedWidget->id = 0;
     if (!file_exists($placedWidget->getWidgetCodeFile())) {
         throw new NotFoundException();
     }
     return $placedWidget;
 }
示例#2
0
 private function addSaveMessage(PlacedWidget $placedWidget, Text $text)
 {
     $homeLink = Link::of($text->getUrlMain(), $text->t("main.home"));
     $documentLink = Link::of($text->getUrlPage("edit_document", $placedWidget->getDocumentId()), $text->t("widgets.view_in_document"));
     $message = "";
     if ($placedWidget->getId() === 0) {
         // New widget
         $message = $text->t("main.widget") . " " . $text->t("editor.is_created");
     } else {
         // Updating existing widget
         $message = $text->t("main.widget") . " " . $text->t("editor.is_edited");
     }
     $text->addMessage($message, $homeLink, $documentLink);
 }
示例#3
0
 /**
  * Gets the widget definition (containing the actual behaviour of the widget)
  * from the placed widget.
  *
  * If the widget code has been uninstalled, an instance of `NullWidget` is
  * returned.
  * @param PlacedWidget $placedWidget The placed widget.
  * @return WidgetDefinition The widget definition.
  * @throws LogicException If the widget code contains an error.
  */
 public function getDefinition(PlacedWidget $placedWidget)
 {
     $dirName = $placedWidget->getDirectoryName();
     if (!preg_match('/^[A-Za-z0-9\\-_]+$/', $dirName)) {
         // Not a valid name
         return new NullWidget("invalidName");
     }
     // Load the widget
     if (!isset($this->loadedWidgets[$dirName])) {
         $this->currentlyLoadingWidgetName = $dirName;
         $file = $placedWidget->getWidgetCodeFile();
         if (!file_exists($file)) {
             return new NullWidget($dirName);
         }
         require $file;
         $this->currentlyLoadingWidgetName = "";
     }
     // Check if loaded
     if (!isset($this->loadedWidgets[$dirName])) {
         throw new LogicException("Widget {$dirName} contains an error: it didn't register itself.");
     }
     return $this->loadedWidgets[$dirName];
 }
示例#4
0
    private function writeWidgetHtml(StreamInterface $stream, PlacedWidget $placedWidget, $widgetNumber)
    {
        $text = $this->text;
        $id = $placedWidget->getId();
        $tokenName = RequestToken::FIELD_NAME;
        $token = $this->requestToken->getTokenString();
        $stream->write("<blockquote>");
        $this->installedWidgets->writeOutput($stream, $placedWidget);
        $stream->write("</blockquote>");
        $stream->write(<<<HTML
            <p>
                <a class="arrow" href="{$text->e($text->getUrlPage("edit_widget", $id))}">
                    {$text->t("main.edit")}
                </a>
                <a class="arrow" href="{$text->e($text->getUrlPage("delete_widget", $id))}">
                    {$text->t("main.delete")}
                </a>
HTML
);
        if ($widgetNumber != 0) {
            $stream->write(<<<HTML
                <a class="arrow" href="{$text->e($text->getUrlPage("move_widget", $id, ["direction" => "up", $tokenName => $token]))}">
                    {$text->t("widgets.move_up")}
                </a>   
HTML
);
        }
        if ($widgetNumber < count($this->placedWidgets) - 1) {
            $stream->write(<<<HTML
                <a class="arrow" href="{$text->e($text->getUrlPage("move_widget", $id, ["direction" => "down", $tokenName => $token]))}">
                    {$text->t("widgets.move_down")}
                </a>  
HTML
);
        }
        $stream->write(<<<HTML
            </p>
            <hr />
HTML
);
    }
示例#5
0
 /**
  * Deletes the placed widget from the database.
  * @param PlacedWidget $placedWidget The placed widget.
  * @throws NotFoundException If the placed widget doesn't appear in the
  * database.
  * @throws PDOException If a database error occurs.
  */
 public function deletePlacedWidget(PlacedWidget $placedWidget)
 {
     $this->where($this->widgetIdField, '=', $placedWidget->getId())->deleteOneOrFail();
 }