use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ManagerEvent;
use OCP\SystemTag\TagAlreadyExistsException;
use OCP\SystemTag\TagNotFoundException;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class SystemTagManager implements ISystemTagManager {
const TAG_TABLE = 'systemtag';
- /**
- * @var IDBConnection
- */
- private $connection;
+ /** @var IDBConnection */
+ protected $connection;
+
+ /** @var EventDispatcherInterface */
+ protected $dispatcher;
/**
* Prepared query for selecting tags directly
private $selectTagQuery;
/**
- * Constructor.
- *
- * @param IDBConnection $connection database connection
- */
- public function __construct(IDBConnection $connection) {
+ * Constructor.
+ *
+ * @param IDBConnection $connection database connection
+ * @param EventDispatcherInterface $dispatcher
+ */
+ public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) {
$this->connection = $connection;
+ $this->dispatcher = $dispatcher;
$query = $this->connection->getQueryBuilder();
$this->selectTagQuery = $query->select('*')
);
}
- $tagId = $this->connection->lastInsertId('*PREFIX*' . self::TAG_TABLE);
+ $tagId = $query->getLastInsertId();
- return new SystemTag(
+ $tag = new SystemTag(
(int)$tagId,
$tagName,
(bool)$userVisible,
(bool)$userAssignable
);
+
+ $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent(
+ ManagerEvent::EVENT_CREATE, $tag
+ ));
+
+ return $tag;
}
/**
$userVisible = (int)$userVisible;
$userAssignable = (int)$userAssignable;
+ try {
+ $tags = $this->getTagsByIds($tagId);
+ } catch (TagNotFoundException $e) {
+ throw new TagNotFoundException(
+ 'Tag does not exist', 0, null, [$tagId]
+ );
+ }
+
+ $beforeUpdate = array_shift($tags);
+ $afterUpdate = new SystemTag(
+ (int) $tagId,
+ $tagName,
+ (bool) $userVisible,
+ (bool) $userAssignable
+ );
+
$query = $this->connection->getQueryBuilder();
$query->update(self::TAG_TABLE)
->set('name', $query->createParameter('name'))
$e
);
}
+
+ $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent(
+ ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate
+ ));
}
/**
}
$tagNotFoundException = null;
+ $tags = [];
try {
- $this->getTagsByIds($tagIds);
+ $tags = $this->getTagsByIds($tagIds);
} catch (TagNotFoundException $e) {
$tagNotFoundException = $e;
+
+ // Get existing tag objects for the hooks later
+ $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags());
+ if (!empty($existingTags)) {
+ try {
+ $tags = $this->getTagsByIds($existingTags);
+ } catch (TagNotFoundException $e) {
+ // Ignore further errors...
+ }
+ }
}
// delete relationships first
->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
->execute();
+ foreach ($tags as $tag) {
+ $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent(
+ ManagerEvent::EVENT_DELETE, $tag
+ ));
+ }
+
if ($tagNotFoundException !== null) {
throw new TagNotFoundException(
'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
--- /dev/null
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCP\SystemTag;
+
+use Symfony\Component\EventDispatcher\Event;
+
+/**
+ * Class ManagerEvent
+ *
+ * @package OCP\SystemTag
+ * @since 9.0.0
+ */
+class ManagerEvent extends Event {
+
+ const EVENT_CREATE = 'OCP\SystemTag\ISystemTagManager::createTag';
+ const EVENT_UPDATE = 'OCP\SystemTag\ISystemTagManager::updateTag';
+ const EVENT_DELETE = 'OCP\SystemTag\ISystemTagManager::deleteTag';
+
+ /** @var string */
+ protected $event;
+ /** @var ISystemTag */
+ protected $tag;
+ /** @var ISystemTag */
+ protected $beforeTag;
+
+ /**
+ * DispatcherEvent constructor.
+ *
+ * @param string $event
+ * @param ISystemTag $tag
+ * @param ISystemTag $beforeTag
+ * @since 9.0.0
+ */
+ public function __construct($event, ISystemTag $tag, ISystemTag $beforeTag = null) {
+ $this->event = $event;
+ $this->tag = $tag;
+ $this->beforeTag = $beforeTag;
+ }
+
+ /**
+ * @return string
+ * @since 9.0.0
+ */
+ public function getEvent() {
+ return $this->event;
+ }
+
+ /**
+ * @return ISystemTag
+ * @since 9.0.0
+ */
+ public function getTag() {
+ return $this->tag;
+ }
+
+ /**
+ * @return ISystemTag
+ * @since 9.0.0
+ */
+ public function getTagBefore() {
+ if ($this->event !== self::EVENT_UPDATE) {
+ throw new \BadMethodCallException('getTagBefore is only available on the update Event');
+ }
+ return $this->beforeTag;
+ }
+}