allows customfields as duplicate check fields
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 26 May 2014 09:02:56 +0000 (11:02 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 26 May 2014 09:25:39 +0000 (11:25 +0200)
Change-Id: I8ac4599cfec71c774d65214c8b9baaeb508342d6
Reviewed-on: http://gerrit.tine20.com/customers/692
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Controller/Record/Abstract.php
tine20/Tinebase/CustomField.php

index c71a91d..7a56254 100644 (file)
@@ -343,7 +343,22 @@ abstract class Tinebase_Controller_Record_Abstract
         $value = (func_num_args() === 1) ? (bool) func_get_arg(0) : NULL;
         return $this->_setBooleanMemberVar('_doForceModlogInfo', $value);
     }
-
+    
+    /**
+     * set/get duplicateCheckFields
+     * 
+     * @param array optional
+     * @return array
+     */
+    public function duplicateCheckFields()
+    {
+        if (func_num_args() === 1) {
+            $this->_duplicateCheckFields = func_get_arg(0);
+        }
+        
+        return $this->_duplicateCheckFields;
+    }
+    
     /**
      * get by id
      *
@@ -627,13 +642,24 @@ abstract class Tinebase_Controller_Record_Abstract
         if (empty($this->_duplicateCheckFields)) {
             return NULL;
         }
-
+        
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
+            . ' Duplicate check fields: ' . print_r($this->_duplicateCheckFields, TRUE));
+        
         $filters = array();
         foreach ($this->_duplicateCheckFields as $group) {
             $addFilter = array();
             foreach ($group as $field) {
                 if (! empty($_record->{$field})) {
                     $addFilter[] = array('field' => $field, 'operator' => 'equals', 'value' => $_record->{$field});
+                } else if (isset($_record->customfields[$field])) {
+                    $customFieldConfig = Tinebase_CustomField::getInstance()->getCustomFieldByNameAndApplication($this->_applicationName, $field, $this->_modelName);
+                    if ($customFieldConfig) {
+                        $addFilter[] = array('field' => 'customfield', 'operator' => 'equals', 'value' => array(
+                            'value' => $_record->customfields[$field],
+                            'cfId'  => $customFieldConfig->getId()
+                        ));
+                    }
                 }
             }
             if (! empty($addFilter)) {
@@ -656,7 +682,8 @@ abstract class Tinebase_Controller_Record_Abstract
         
         $filter = new $filterClass($filterData);
         
-        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($filter->toArray(), TRUE));
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' '
+            . print_r($filter->toArray(), TRUE));
         
         return $filter;
     }
index b545fc2..4c5c29e 100644 (file)
@@ -142,6 +142,20 @@ class Tinebase_CustomField implements Tinebase_Controller_SearchInterface
     }
 
     /**
+     * get custom field by name and app
+     *
+     * @param string|Tinebase_Model_Application $applicationId application object, id or name
+     * @param string $customFieldName
+     * @param string $modelName
+     * @return Tinebase_Model_CustomField_Config|null
+     */
+    public function getCustomFieldByNameAndApplication($applicationId, $customFieldName, $modelName = null)
+    {
+        $allAppCustomfields = $this->getCustomFieldsForApplication($applicationId, $modelName);
+        return $allAppCustomfields->find('name', $customFieldName);
+    }
+    
+    /**
      * get custom fields for an application
      * - results are cached in class cache $_cfByApplicationCache
      * - results are cached if caching is active (with cache tag 'customfields')