[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: Implement announces CRUD

git at icinga.org git at icinga.org
Fri Oct 28 17:42:39 CEST 2016


Module: icingaweb2
Branch: feature/announce-banner-11198
Commit: dca84a4227df91b76009f99975a4e937e838abb4
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=dca84a4227df91b76009f99975a4e937e838abb4

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Fri Oct 28 17:37:33 2016 +0200

Implement announces CRUD

refs #11198

---

 application/controllers/AnnouncesController.php |   89 +++++++++++++++++++++++
 application/forms/AnnounceRepositoryForm.php    |    2 +-
 application/views/scripts/announces/index.phtml |   58 +++++++++++++++
 3 files changed, 148 insertions(+), 1 deletion(-)

diff --git a/application/controllers/AnnouncesController.php b/application/controllers/AnnouncesController.php
new file mode 100644
index 0000000..0165284
--- /dev/null
+++ b/application/controllers/AnnouncesController.php
@@ -0,0 +1,89 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Controllers;
+
+use Icinga\Exception\NotFoundError;
+use Icinga\Forms\AnnounceRepositoryForm;
+use Icinga\Repository\AnnounceIniRepository;
+use Icinga\Web\Controller;
+use Icinga\Web\Url;
+
+class AnnouncesController extends Controller
+{
+    /**
+     * List all announces
+     */
+    public function indexAction()
+    {
+        $this->assertPermission('admin/announces');
+
+        $repo = new AnnounceIniRepository();
+        $this->view->announces = $query = $repo
+            ->select(array('id', 'author', 'message', 'start', 'end'))
+            ->order('start', 'ASC');
+
+        $this->getTabs()->add(
+            'announces',
+            array(
+                'url'       => Url::fromPath('announces'),
+                'label'     => $this->translate('Announces'),
+                'title'     => $this->translate('List All Announces'),
+                'active'    => true
+            )
+        );
+    }
+
+    /**
+     * Add an announce
+     */
+    public function addAction()
+    {
+        $form = $this->prepareForm()->add();
+        $form->handleRequest();
+        $this->renderForm($form, $this->translate('New Announce'));
+    }
+
+    /**
+     * Edit an announce
+     */
+    public function editAction()
+    {
+        $form = $this->prepareForm()->edit($this->params->getRequired('id'));
+        try {
+            $form->handleRequest();
+        } catch (NotFoundError $_) {
+            $this->httpNotFound($this->translate('Announce not found'));
+        }
+        $this->renderForm($form, $this->translate('Update Announce'));
+    }
+
+    /**
+     * Remove an announce
+     */
+    public function removeAction()
+    {
+        $form = $this->prepareForm()->remove($this->params->getRequired('id'));
+        try {
+            $form->handleRequest();
+        } catch (NotFoundError $_) {
+            $this->httpNotFound($this->translate('Announce not found'));
+        }
+        $this->renderForm($form, $this->translate('Remove Announce'));
+    }
+
+    /**
+     * Assert permission admin/announces and return a prepared RepositoryForm
+     *
+     * @return AnnounceRepositoryForm
+     */
+    protected function prepareForm()
+    {
+        $this->assertPermission('admin/announces');
+
+        $form = new AnnounceRepositoryForm();
+        return $form
+            ->setRepository(new AnnounceIniRepository())
+            ->setRedirectUrl(Url::fromPath('announces'));
+    }
+}
diff --git a/application/forms/AnnounceRepositoryForm.php b/application/forms/AnnounceRepositoryForm.php
index 1cf2869..3288321 100644
--- a/application/forms/AnnounceRepositoryForm.php
+++ b/application/forms/AnnounceRepositoryForm.php
@@ -17,7 +17,7 @@ class AnnounceRepositoryForm extends RepositoryForm
     protected function createInsertElements(array $formData)
     {
         $this->addElement(
-            'hidden',
+            'text',
             'author',
             array(
                 'required'  => true,
diff --git a/application/views/scripts/announces/index.phtml b/application/views/scripts/announces/index.phtml
new file mode 100644
index 0000000..d91b0f9
--- /dev/null
+++ b/application/views/scripts/announces/index.phtml
@@ -0,0 +1,58 @@
+<?php if (! $compact): ?>
+<div class="controls">
+    <?= $tabs ?>
+</div>
+<?php endif ?>
+<div class="content">
+<?= $this->qlink(
+    $this->translate('Create a New Announce') ,
+    'announces/add',
+    null,
+    array(
+        'class'             => 'button-link',
+        'data-base-target'  => '_next',
+        'icon'              => 'plus',
+        'title'             => $this->translate('Create a new announce')
+    )
+) ?>
+<?php if (! $announces->hasResult()): ?>
+    <p><?= $this->translate('No announces found.') ?></p>
+</div>
+<?php return; endif ?>
+    <table data-base-target="_next" class="table-row-selectable common-table">
+    <thead>
+        <tr>
+            <th><?= $this->translate('Author') ?></th>
+            <th><?= $this->translate('Message') ?></th>
+            <th><?= $this->translate('Start') ?></th>
+            <th><?= $this->translate('End') ?></th>
+            <th></th>
+        </tr>
+    </thead>
+    <tbody>
+    <?php foreach ($announces as $announce): /** @var object $announce */ ?>
+        <tr>
+            <td><?= $this->escape($announce->author) ?></td>
+            <td><?= $this->qlink(
+                $this->ellipsis($announce->message, 100),
+                'announces/edit',
+                array('id' => $announce->id),
+                array('title' => $this->translate('Edit this announce'))
+            ) ?></td>
+            <td><?= $this->formatDateTime($announce->start->getTimestamp()) ?></td>
+            <td><?= $this->formatDateTime($announce->end->getTimestamp()) ?></td>
+            <td class="icon-col"><?= $this->qlink(
+                null,
+                'announces/remove',
+                array('id' => $announce->id),
+                array(
+                    'class' => 'action-link',
+                    'icon'  => 'cancel',
+                    'title' => $this->translate('Remove this announce')
+                )
+            ) ?></td>
+        </tr>
+    <?php endforeach ?>
+    </tbody>
+    </table>
+</div>



More information about the icinga-checkins mailing list