0010146: filter search for tags or relations
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 18 Oct 2016 14:59:58 +0000 (16:59 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 26 Oct 2016 12:34:14 +0000 (14:34 +0200)
added a option 'own_model' in Tinebase_Model_Filter_FilterGroup
for the case foreignRecord type relation

added Filemanager_Model_NodeFilter extending Tinebase_Model_Tree_Node_Filter
and overwritting setFromArray to set 'own_model' option

https://forge.tine20.org/view.php?id=10146

Change-Id: Id5b18e31bcc0ca9c0ced97100b9cf0890f0b4761
Reviewed-on: http://gerrit.tine20.com/customers/3668
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Filemanager/Frontend/Json.php
tine20/Filemanager/Model/NodeFilter.php [new file with mode: 0644]
tine20/Tinebase/Container.php
tine20/Tinebase/Model/Filter/FilterGroup.php
tine20/Tinebase/Model/Filter/Relation.php

index 1912236..87b74ba 100644 (file)
@@ -36,7 +36,7 @@ class Filemanager_Frontend_Json extends Tinebase_Frontend_Json_Abstract
      */
     public function searchNodes($filter, $paging)
     {
-        $result = $this->_search($filter, $paging, Filemanager_Controller_Node::getInstance(), 'Tinebase_Model_Tree_Node_Filter');
+        $result = $this->_search($filter, $paging, Filemanager_Controller_Node::getInstance(), 'Filemanager_Model_NodeFilter');
         $this->_removeAppIdFromPathFilter($result);
         
         return $result;
diff --git a/tine20/Filemanager/Model/NodeFilter.php b/tine20/Filemanager/Model/NodeFilter.php
new file mode 100644 (file)
index 0000000..18c3f5c
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package     Filemanager
+ * @subpackage  Model
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Paul Mehrer <p.mehrer@metaways.de>
+ * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+
+class Filemanager_Model_NodeFilter extends Tinebase_Model_Tree_Node_Filter
+{
+    /**
+     * sets this filter group from filter data in array representation
+     *
+     * @param array $_data
+     */
+    public function setFromArray($_data)
+    {
+        foreach ($_data as $key => &$filterData) {
+            if (isset($filterData['field']) && $filterData['field'] === 'foreignRecord' &&
+                $filterData['value']['linkType'] === 'relation') {
+                if (!isset($filterData['options']) || !is_array($filterData['options'])) {
+                    $filterData['options'] = array();
+                }
+                $filterData['options']['own_model'] = 'Filemanager_Model_Node';
+            }
+        }
+
+        parent::setFromArray($_data);
+    }
+}
index ab8e6bc..59e8cd1 100644 (file)
@@ -1153,21 +1153,27 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             $acl = $controller->doContainerACLChecks(FALSE);
         }
         if ($controller && class_exists($filterName)) {
-            Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
-                . ' Delete ' . $model . ' records in container ' . $container->getId());
 
-            $filter = new $filterName(array(array(
-                'field'    => 'container_id',
-                'operator' => 'equals',
-                'value'    => intval($container->id)
-            )), Tinebase_Model_Filter_FilterGroup::CONDITION_AND, array('ignoreAcl' => $_ignoreAcl));
+            // workaround to fix Filemanager as Tinebase_Filesystem does not implement search
+            $backend = $controller::getInstance()->getBackend();
+            if (method_exists($backend, 'search')) {
+                Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                    . ' Delete ' . $model . ' records in container ' . $container->getId());
 
-            if ($_ignoreAcl) {
-                $backend = $controller::getInstance()->getBackend();
-                $idsToDelete = $backend->search($filter, null, /* $_onlyIds */ true);
-                $controller::getInstance()->delete($idsToDelete);
-            } else {
-                $controller::getInstance()->deleteByFilter($filter);
+                $filter = new $filterName(array(array(
+                    'field' => 'container_id',
+                    'operator' => 'equals',
+                    'value' => intval($container->id)
+                )), Tinebase_Model_Filter_FilterGroup::CONDITION_AND, array('ignoreAcl' => $_ignoreAcl));
+
+                if ($_ignoreAcl) {
+
+                    $idsToDelete = $backend->search($filter, null, /* $_onlyIds */
+                        true);
+                    $controller::getInstance()->delete($idsToDelete);
+                } else {
+                    $controller::getInstance()->deleteByFilter($filter);
+                }
             }
         }
 
index 1e01df2..66136f4 100644 (file)
@@ -305,6 +305,9 @@ class Tinebase_Model_Filter_FilterGroup implements Iterator
                 $model = new $this->_modelName();
                 $filterData['options']['related_model'] = $modelName;
                 $filterData['options']['idProperty'] = $model->getIdProperty();
+                if (isset($_filterData['options']) && isset($_filterData['options']['own_model'])) {
+                    $filterData['options']['own_model'] = $_filterData['options']['own_model'];
+                }
                 $filter = new Tinebase_Model_Filter_Relation($filterData);
                 break;
 
index 219016e..b89c365 100644 (file)
@@ -84,11 +84,16 @@ class Tinebase_Model_Filter_Relation extends Tinebase_Model_Filter_ForeignRecord
      */
     public function appendFilterSql($_select, $_backend)
     {
+        if (isset($this->_options['own_model'])) {
+            $ownModel = $this->_options['own_model'];
+        } else {
+            $ownModel = $_backend->getModelName();
+        }
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' 
-            . 'Adding Relation filter: ' . $_backend->getModelName() . ' <-> ' . $this->_options['related_model']);
+            . 'Adding Relation filter: ' . $ownModel . ' <-> ' . $this->_options['related_model']);
         
         $this->_resolveForeignIds();
-        $ownIds = $this->_getOwnIds($_backend->getModelName());
+        $ownIds = $this->_getOwnIds($ownModel);
         
         $idField = (isset($this->_options['idProperty']) || array_key_exists('idProperty', $this->_options)) ? $this->_options['idProperty'] : 'id';
         $db = $_backend->getAdapter();