]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add system tag check
authorJoas Schilling <coding@schilljs.com>
Wed, 27 Jul 2016 13:37:03 +0000 (15:37 +0200)
committerJoas Schilling <coding@schilljs.com>
Wed, 3 Aug 2016 12:40:12 +0000 (14:40 +0200)
apps/workflowengine/js/admin.js
apps/workflowengine/js/filesystemtagsplugin.js [new file with mode: 0644]
apps/workflowengine/lib/Check/FileSystemTags.php [new file with mode: 0644]

index b9f0744732ffeb95896fb0af780ac596284b7347..e6df4b75f70e236e579579c4580027fb38fc44e5 100644 (file)
                                this.renderOperation(operation);
                        },
                        renderOperation: function(operation){
-                               console.log(operation);
                                var subView = new OCA.WorkflowEngine.OperationView({
                                                model: operation
                                        }),
diff --git a/apps/workflowengine/js/filesystemtagsplugin.js b/apps/workflowengine/js/filesystemtagsplugin.js
new file mode 100644 (file)
index 0000000..6f2f231
--- /dev/null
@@ -0,0 +1,82 @@
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+(function() {
+
+       OCA.WorkflowEngine = OCA.WorkflowEngine || {};
+       OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
+
+       OCA.WorkflowEngine.Plugins.FileSystemTagsPlugin = {
+               getCheck: function() {
+                       this.collection = OC.SystemTags.collection;
+                       this.collection.fetch({
+                               success: function() {
+                                       console.log('done loading tags');
+                               }
+                       });
+
+                       return {
+                               'class': 'OCA\\WorkflowEngine\\Check\\FileSystemTags',
+                               'name': t('workflowengine', 'File system tag'),
+                               'operators': [
+                                       {'operator': 'is', 'name': t('workflowengine', 'is tagged with')},
+                                       {'operator': '!is', 'name': t('workflowengine', 'is not tagged with')}
+                               ]
+                       };
+               },
+               render: function(element, classname, value) {
+                       if (classname !== 'OCA\\WorkflowEngine\\Check\\FileSystemTags') {
+                               return;
+                       }
+
+                       $(element).css('width', '400px');
+
+                       $(element).select2({
+                               allowClear: false,
+                               multiple: false,
+                               placeholder: t('workflowengine', 'Select tag…'),
+                               query: _.debounce(function(query) {
+                                       query.callback({
+                                               results: OCA.WorkflowEngine.Plugins.FileSystemTagsPlugin.collection.filterByName(query.term)
+                                       });
+                               }, 100, true),
+                               id: function(element) {
+                                       return element.get('id');
+                               },
+                               initSelection: function(element, callback) {
+                                       var selection = ($(element).val() || []).split('|').sort();
+                                       callback(selection);
+                               },
+                               formatResult: function (tag) {
+                                       return OC.SystemTags.getDescriptiveTag(tag);
+                               },
+                               formatSelection: function (tagId) {
+                                       tag = OCA.WorkflowEngine.Plugins.FileSystemTagsPlugin.collection.get(tagId);
+                                       return OC.SystemTags.getDescriptiveTag(tag);
+                               },
+                               escapeMarkup: function(m) {
+                                       return m;
+                               }
+                       });
+               }
+       };
+})();
+
+OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.FileSystemTagsPlugin);
diff --git a/apps/workflowengine/lib/Check/FileSystemTags.php b/apps/workflowengine/lib/Check/FileSystemTags.php
new file mode 100644 (file)
index 0000000..7717963
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\WorkflowEngine\Check;
+
+
+use OCP\Files\Cache\ICache;
+use OCP\Files\Storage\IStorage;
+use OCP\SystemTag\ISystemTagManager;
+use OCP\SystemTag\ISystemTagObjectMapper;
+use OCP\SystemTag\TagNotFoundException;
+use OCP\WorkflowEngine\ICheck;
+
+class FileSystemTags implements ICheck {
+
+       /** @var array */
+       protected $fileIds;
+
+       /** @var array */
+       protected $fileSystemTags;
+
+       /** @var ISystemTagManager */
+       protected $systemTagManager;
+
+       /** @var ISystemTagObjectMapper */
+       protected $systemTagObjectMapper;
+
+       /** @var IStorage */
+       protected $storage;
+
+       /** @var string */
+       protected $path;
+
+       /**
+        * @param ISystemTagManager $systemTagManager
+        * @param ISystemTagObjectMapper $systemTagObjectMapper
+        */
+       public function __construct(ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) {
+               $this->systemTagManager = $systemTagManager;
+               $this->systemTagObjectMapper = $systemTagObjectMapper;
+       }
+
+       /**
+        * @param IStorage $storage
+        * @param string $path
+        */
+       public function setFileInfo(IStorage $storage, $path) {
+               $this->storage = $storage;
+               $this->path = $path;
+       }
+
+       /**
+        * @param string $operator
+        * @param string $value
+        * @return bool
+        */
+       public function executeCheck($operator, $value) {
+               $systemTags = $this->getSystemTags();
+               return ($operator === 'is') === in_array($value, $systemTags);
+       }
+
+       /**
+        * @param string $operator
+        * @param string $value
+        * @throws \UnexpectedValueException
+        */
+       public function validateCheck($operator, $value) {
+               if (!in_array($operator, ['is', '!is'])) {
+                       throw new \UnexpectedValueException('Invalid operator', 1);
+               }
+
+               try {
+                       $this->systemTagManager->getTagsByIds($value);
+               } catch (TagNotFoundException $e) {
+                       throw new \UnexpectedValueException('Tag does not exist', 2);
+               } catch (\InvalidArgumentException $e) {
+                       throw new \UnexpectedValueException('Tag does not exist', 3);
+               }
+       }
+
+       /**
+        * Get the ids of the assigned system tags
+        * @return string[]
+        */
+       protected function getSystemTags() {
+               $cache = $this->storage->getCache();
+               $fileIds = $this->getFileIds($cache, $this->path);
+
+               $systemTags = [];
+               foreach ($fileIds as $i => $fileId) {
+                       if (isset($this->fileSystemTags[$fileId])) {
+                               $systemTags[] = $this->fileSystemTags[$fileId];
+                               unset($fileIds[$i]);
+                       }
+               }
+
+               if (!empty($fileIds)) {
+                       $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files');
+                       foreach ($mappedSystemTags as $fileId => $fileSystemTags) {
+                               $this->fileSystemTags[$fileId] = $fileSystemTags;
+                               $systemTags[] = $fileSystemTags;
+                       }
+               }
+
+               $systemTags = call_user_func_array('array_merge', $systemTags);
+               $systemTags = array_unique($systemTags);
+               return $systemTags;
+       }
+
+       /**
+        * Get the file ids of the given path and its parents
+        * @param ICache $cache
+        * @param string $path
+        * @return int[]
+        */
+       protected function getFileIds(ICache $cache, $path) {
+               $cacheId = $cache->getNumericStorageId();
+               if (isset($this->fileIds[$cacheId][$path])) {
+                       return $this->fileIds[$cacheId][$path];
+               }
+
+               if ($path !== dirname($path)) {
+                       $parentIds = $this->getFileIds($cache, dirname($path));
+               } else {
+                       return [];
+               }
+
+               $fileId = $cache->getId($path);
+               if ($fileId !== -1) {
+                       $parentIds[] = $cache->getId($path);
+               }
+
+               $this->fileIds[$cacheId][$path] = $parentIds;
+
+               return $parentIds;
+       }
+}