[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: Introduce class AnnounceIniRepository

git at icinga.org git at icinga.org
Tue Oct 25 19:28:17 CEST 2016


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

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Tue Oct 25 19:27:32 2016 +0200

Introduce class AnnounceIniRepository

refs #11198

---

 .../Icinga/Repository/AnnounceIniRepository.php    |   99 ++++++++++++++++++++
 library/Icinga/Web/Announce.php                    |   15 ++-
 2 files changed, 105 insertions(+), 9 deletions(-)

diff --git a/library/Icinga/Repository/AnnounceIniRepository.php b/library/Icinga/Repository/AnnounceIniRepository.php
new file mode 100644
index 0000000..af16508
--- /dev/null
+++ b/library/Icinga/Repository/AnnounceIniRepository.php
@@ -0,0 +1,99 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Repository;
+
+use DateTime;
+use Icinga\Application\Config;
+use Icinga\Data\Filter\Filter;
+use Icinga\Web\Announce;
+
+/**
+ * A collection of Announces stored in an INI file
+ */
+class AnnounceIniRepository extends IniRepository
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function __construct($ds = null)
+    {
+        if ($ds === null) {
+            $ds = Config::app('announces');
+        }
+        $config = $ds->getConfigObject();
+        if ($config->getKeyColumn() === null) {
+            $config->setKeyColumn('hash');
+        }
+        parent::__construct($ds);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected $queryColumns = array('announce' => array('hash', 'author', 'message', 'start', 'end'));
+
+    /**
+     * {@inheritDoc}
+     */
+    protected $conversionRules = array('announce' => array(
+        'start' => 'timestamp',
+        'end'   => 'timestamp'
+    ));
+
+    /**
+     * Create a DateTime from a *nix timestamp
+     *
+     * @param   string  $timestamp
+     *
+     * @return  DateTime|null
+     */
+    protected function retrieveTimestamp($timestamp)
+    {
+        return $timestamp === null ? null : DateTime::createFromFormat('U', $timestamp);
+    }
+
+    /**
+     * Get a DateTime's *nix timestamp
+     *
+     * @param   DateTime    $datetime
+     *
+     * @return  int|null
+     */
+    protected function persistTimestamp(DateTime $datetime)
+    {
+        return $datetime === null ? null : $datetime->getTimestamp();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function insert($target, array $data)
+    {
+        if (! isset($data['hash'])) {
+            $announce = new Announce($data);
+            $data['hash'] = $announce->getHash();
+        }
+        parent::insert($target, $data);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function update($target, array $data, Filter $filter = null)
+    {
+        $query = $this->select(array('hash', 'author', 'message', 'start', 'end'));
+        if ($filter !== null) {
+            $query->applyFilter($filter);
+        }
+        foreach ($query->fetchAll() as $row) {
+            $row = (array) $row;
+            $hash = $row['hash'];
+            unset($row['hash']);
+            $announce = new Announce(array_merge($row, $data));
+            $data['hash'] = $announce->getHash();
+            parent::update($target, $data, Filter::expression('hash', '=', $hash));
+            unset($data['hash']);
+        }
+    }
+}
diff --git a/library/Icinga/Web/Announce.php b/library/Icinga/Web/Announce.php
index 75cf8df..8ce66a0 100644
--- a/library/Icinga/Web/Announce.php
+++ b/library/Icinga/Web/Announce.php
@@ -40,17 +40,14 @@ class Announce
     /**
      * Announce constructor
      *
-     * @param   string      $author
-     * @param   string      $message
-     * @param   DateTime    $start
-     * @param   DateTime    $end
+     * @param array $properties
      */
-    public function __construct($author, $message, $start, $end)
+    public function __construct(array $properties = array())
     {
-        $this->author = $author;
-        $this->message = $message;
-        $this->start = clone $start;
-        $this->end = clone $end;
+        foreach ($properties as $k => $v) {
+            $method = 'set' . ucfirst($k);
+            $this->$method($v);
+        }
     }
 
     /**



More information about the icinga-checkins mailing list