0012030: Customfields - deleting a customfield config in use
authorPaul Mehrer <p.mehrer@metaways.de>
Thu, 19 May 2016 13:35:57 +0000 (15:35 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Aug 2016 08:17:23 +0000 (10:17 +0200)
if a customfield config to be deleted is still in use
an Tinebase_Exception_Record_StillInUse exception will
be thrown, so proper user interaction is possible.

if the user confirms deletion despite the usage of the
customfield, usage check will be skipped

also customfields acls will now be deleted too

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

Change-Id: I7b133c21fa358492a1aa7b6dbcd0e76d37dbb958
Reviewed-on: http://gerrit.tine20.com/customers/3212
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Admin/Controller/Customfield.php
tine20/Admin/Frontend/Json.php
tine20/Tinebase/CustomField.php
tine20/Tinebase/Exception/Record/StillInUse.php [new file with mode: 0644]
tine20/Tinebase/Model/CustomField/ConfigFilter.php

index 86be699..4b257d5 100644 (file)
@@ -104,7 +104,9 @@ class Admin_Controller_Customfield extends Tinebase_Controller_Record_Abstract
      */
     public function delete($ids)
     {
-        $this->_checkCFUsage($ids);
+        if (!is_array($this->_requestContext) || !isset($this->_requestContext['skipUsageCheck']) || !$this->_requestContext['skipUsageCheck']) {
+            $this->_checkCFUsage($ids);
+        }
         foreach ((array) $ids as $id) {
             $this->_customfieldController->deleteCustomField($id);
         }
@@ -130,8 +132,19 @@ class Admin_Controller_Customfield extends Tinebase_Controller_Record_Abstract
         if ($result->count() > 0) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
                 . ' ' . count($result) . ' records still have custom field values.');
+
+            $foundIds = array_values(array_unique($result->customfield_id));
+
+            $filter = new Tinebase_Model_CustomField_ConfigFilter(array(array(
+                'field'     => 'id',
+                'operator'  => 'in',
+                'value'     => (array) $foundIds
+            )));
+
+            $result = $this->search($filter);
+            $names = $result->name;
             
-            throw new Tinebase_Exception_SystemGeneric('Customfield is still in use!');
+            throw new Tinebase_Exception_Record_StillInUse('Customfields: ' . join(', ', $names) . ' are still in use! Are you sure you want to delete them?');
         }
     }
     
index 7f288c7..2252f7d 100644 (file)
@@ -1124,12 +1124,16 @@ class Admin_Frontend_Json extends Tinebase_Frontend_Json_Abstract
     /**
      * deletes existing records
      *
-     * @param  array  $ids 
+     * @param  array $ids
+     * @param  array $context
      * @return array
      */
-    public function deleteCustomfields($ids)
+    public function deleteCustomfields($ids, array $context = array())
     {
-        return $this->_delete($ids, Admin_Controller_Customfield::getInstance());
+        $controller = Admin_Controller_Customfield::getInstance();
+        $controller->setRequestContext($context);
+
+        return $this->_delete($ids, $controller);
     }
 
     /****************************** Config ******************************/
index e100a3c..85ce231 100644 (file)
@@ -268,6 +268,7 @@ class Tinebase_CustomField implements Tinebase_Controller_SearchInterface
         
         $this->_clearCache();
         $this->_backendValue->deleteByProperty($cfId, 'customfield_id');
+        $this->_backendACL->deleteByProperty($cfId, 'customfield_id');
         $this->_backendConfig->delete($cfId);
     }
     
diff --git a/tine20/Tinebase/Exception/Record/StillInUse.php b/tine20/Tinebase/Exception/Record/StillInUse.php
new file mode 100644 (file)
index 0000000..22afc92
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package     Tinebase
+ * @subpackage  Exception
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author      Paul Mehrer <p.mehrer@metaways.de>
+ *
+ */
+
+/**
+ * record still in use, are you sure you want to delete it?
+ *
+ * @package     Tinebase
+ * @subpackage  Exception
+ */
+class Tinebase_Exception_Record_StillInUse extends Tinebase_Exception
+{
+}
index 5d1d867..92895ab 100644 (file)
@@ -41,6 +41,7 @@ class Tinebase_Model_CustomField_ConfigFilter extends Tinebase_Model_Filter_Filt
      * @var array filter model fieldName => definition
      */
     protected $_filterModel = array(
+        'id'                => array('filter' => 'Tinebase_Model_Filter_Id'),
         'application_id'    => array('filter' => 'Tinebase_Model_Filter_Id'),
         'name'              => array('filter' => 'Tinebase_Model_Filter_Text'),
         'model'             => array('filter' => 'Tinebase_Model_Filter_Text'),