0011764: allow to create apps without json frontend #2
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 1 Jul 2016 15:00:33 +0000 (17:00 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 4 Jul 2016 11:08:46 +0000 (13:08 +0200)
* adds generic json frontend for apps without own json
 frontend class
* allow to set model of filtergroup with a function

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

Change-Id: If5d96d45d47c4df188b777c0c00e0f495995f707
Reviewed-on: http://gerrit.tine20.com/customers/3298
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Frontend/Json/Abstract.php
tine20/Tinebase/Frontend/Json/Generic.php [new file with mode: 0644]
tine20/Tinebase/Model/Filter/FilterGroup.php
tine20/Tinebase/Server/Json.php

index 74e6ba8..629d362 100644 (file)
@@ -217,14 +217,38 @@ abstract class Tinebase_Frontend_Json_Abstract extends Tinebase_Frontend_Abstrac
         $decodedFilter = is_array($_filter) || strlen($_filter) == 40 ? $_filter : $this->_prepareParameter($_filter);
 
         if (is_array($decodedFilter)) {
-            $filter = new $filterModel(array());
+            $filter = $this->_getFilterObject($filterModel);
             $filter->setFromArrayInUsersTimezone($decodedFilter);
         } else if (!empty($decodedFilter) && strlen($decodedFilter) == 40) {
             $filter = Tinebase_PersistentFilter::getFilterById($decodedFilter);
         } else if ($_throwExceptionIfEmpty) {
             throw new Tinebase_Exception_InvalidArgument('Filter must not be empty!');
         } else {
-            $filter = new $filterModel(array());
+            $filter = $this->_getFilterObject($filterModel);
+        }
+
+        return $filter;
+    }
+
+    /**
+     * get the filter group object
+     *
+     * @param $filterModel
+     * @return Tinebase_Model_Filter_FilterGroup
+     */
+    protected function _getFilterObject($filterModel)
+    {
+        if (! class_exists($filterModel)) {
+            $configuredModel = preg_replace('/Filter$/', '', $filterModel);
+
+            // TODO check if model class exists?
+            //if (class_exists($configuredModel))
+
+            // use generic filter model
+            $filter = new Tinebase_Model_Filter_FilterGroup();
+            $filter->setConfiguredModel($configuredModel);
+        } else {
+            $filter = new $filterModel();
         }
 
         return $filter;
@@ -613,7 +637,7 @@ abstract class Tinebase_Frontend_Json_Abstract extends Tinebase_Frontend_Abstrac
      */
     protected function _getPluginForFilterModel($filterModel)
     {
-        if(isset(self::$_filterPlugins[$filterModel])) {
+        if (isset(self::$_filterPlugins[$filterModel])) {
             return self::$_filterPlugins[$filterModel];
         }
         return $filterModel;
diff --git a/tine20/Tinebase/Frontend/Json/Generic.php b/tine20/Tinebase/Frontend/Json/Generic.php
new file mode 100644 (file)
index 0000000..a1a3c92
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package     Tinebase
+ * @subpackage  Application
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @copyright   Copyright (c) 2007-2016 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author      Philipp Schüle <p.schuele@metaways.de>
+ */
+
+/**
+ * generic json frontend class for apps that do not define their own json frontends
+ *
+ * @package     Tinebase
+ * @subpackage  Application
+ */
+class Tinebase_Frontend_Json_Generic extends Tinebase_Frontend_Json_Abstract
+{
+    /**
+     * Tinebase_Frontend_Json_Generic constructor.
+     *
+     * @param $applicationName
+     */
+    public function __construct($applicationName)
+    {
+        $this->_applicationName = $applicationName;
+    }
+}
index 1e01df2..81b583e 100644 (file)
@@ -169,7 +169,7 @@ class Tinebase_Model_Filter_FilterGroup implements Iterator
     protected $_filterObjects = array();
     
     /**
-     * @var array spechial options
+     * @var array special options
      */
     protected $_options = NULL;
     
@@ -570,6 +570,16 @@ class Tinebase_Model_Filter_FilterGroup implements Iterator
     {
         $this->_label = $_label;
     }
+
+    /**
+     * set configured model
+     *
+     * @param $configuredModel
+     */
+    public function setConfiguredModel($configuredModel)
+    {
+        $this->_configuredModel = $configuredModel;
+    }
     
     /**
      * returns id
index 981e7c9..63da151 100644 (file)
@@ -454,6 +454,14 @@ class Tinebase_Server_Json extends Tinebase_Server_Abstract implements Tinebase_
 
                     foreach ($commonJsonApiMethods as $name => $method) {
                         $key = $application->name . '.' . $name . $simpleModelName . ($method['plural'] ? 's' : '');
+                        $appJsonFrontendClass = $application->name . '_Frontend_Json';
+                        if (class_exists($appJsonFrontendClass)) {
+                            $class = $appJsonFrontendClass;
+                            $object = null;
+                        } else {
+                            $class = 'Tinebase_Frontend_Json_Generic';
+                            $object = new Tinebase_Frontend_Json_Generic($application->name);
+                        }
                         $definitions[$key] = new Zend_Server_Method_Definition(array(
                             'name'            => $key,
                             'prototypes'      => array(array(
@@ -462,10 +470,10 @@ class Tinebase_Server_Json extends Tinebase_Server_Abstract implements Tinebase_
                             )),
                             'methodHelp'      => $method['help'],
                             'invokeArguments' => array(),
-                            'object'          => null,
+                            'object'          => $object,
                             'callback'        => array(
                                 'type'   => 'instance',
-                                'class'  => $application->name . '_Frontend_Json',
+                                'class'  => $class,
                                 'method' => $name . $simpleModelName . ($method['plural'] ? 's' : '')
                             ),
                         ));