Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / CustomGroup.php
index 4bd9725590982a4106b399031f5f1ecb12b95b5d..57e735752e71ace1e637ea5e244a81584854f4bf 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2015
  */
 
 /**
- * Business object for managing custom data groups
- *
+ * Business object for managing custom data groups.
  */
 class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
 
   /**
-   * Class constructor
+   * Class constructor.
    */
   public function __construct() {
     parent::__construct();
   }
 
   /**
-   * Takes an associative array and creates a custom group object
+   * Takes an associative array and creates a custom group object.
    *
    * This function is invoked from within the web form layer and also from the api layer
    *
    * @param array $params
    *   (reference) an assoc array of name/value pairs.
    *
-   * @return CRM_Core_DAO_CustomGroup object
-   * @static
+   * @return CRM_Core_DAO_CustomGroup
    */
   public static function create(&$params) {
     // create custom group dao, populate fields and then save.
@@ -81,8 +77,7 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
       $params['extends'][0] == 'ParticipantEventName' ||
       $params['extends'][0] == 'ParticipantEventType'
     ) {
-      $group->extends_entity_column_id =
-        CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $params['extends'][0], 'value', 'name');
+      $group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $params['extends'][0], 'value', 'name');
     }
 
     //this is format when form get submit.
@@ -144,7 +139,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
       if ((!empty($params['is_multiple']) || $isMultiple) &&
         ($params['is_multiple'] != $isMultiple)
       ) {
-        $oldTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
+        $oldTableName = CRM_Core_DAO::getFieldValue(
+          'CRM_Core_DAO_CustomGroup',
           $params['id'],
           'table_name'
         );
@@ -224,31 +220,29 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
   }
 
   /**
-   * Fetch object based on array of properties
+   * Fetch object based on array of properties.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    * @param array $defaults
    *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return CRM_Core_DAO_CustomGroup object
-   * @static
+   * @return CRM_Core_DAO_CustomGroup
    */
   public static function retrieve(&$params, &$defaults) {
     return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomGroup', $params, $defaults);
   }
 
   /**
-   * Update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
    * @param int $id
    *   Id of the database record.
-   * @param bool $is_activeValue we want to set the is_active field.
+   * @param bool $is_active
    *   Value we want to set the is_active field.
    *
    * @return Object
    *   DAO object on sucess, null otherwise
-   * @static
    */
   public static function setIsActive($id, $is_active) {
     // reset the cache
@@ -277,9 +271,7 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
     $dao = new CRM_Core_DAO_CustomGroup();
     $dao->extends = $extends;
     $dao->extends_entity_column_id = $columnId;
-    $escapedValue =
-      CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::escapeString($columnValue) .
-      CRM_Core_DAO::VALUE_SEPARATOR;
+    $escapedValue = CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::escapeString($columnValue) . CRM_Core_DAO::VALUE_SEPARATOR;
     $dao->whereAdd("extends_entity_column_value LIKE \"%$escapedValue%\"");
     //$dao->extends_entity_column_value = $columnValue;
     return $dao->find() ? TRUE : FALSE;
@@ -330,17 +322,15 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
    * @param bool $onlySubType
    *
    * @return array
-   *   array  The returned array is keyed by group id and has the custom group table fields
-   * and a subkey 'fields' holding the specific custom fields.
-   * If entityId is passed in the fields keys have a subkey 'customValue' which holds custom data
-   * if set for the given entity. This is structured as an array of values with each one having the keys 'id', 'data'
+   *   The returned array is keyed by group id and has the custom group table fields
+   *   and a subkey 'fields' holding the specific custom fields.
+   *   If entityId is passed in the fields keys have a subkey 'customValue' which holds custom data
+   *   if set for the given entity. This is structured as an array of values with each one having the keys 'id', 'data'
    *
    * @todo - review this  - It also returns an array called 'info' with tables, select, from, where keys
-   * The reason for the info array in unclear and it could be determined from parsing the group tree after creation
-   * With caching the performance impact would be small & the function would be cleaner
+   *   The reason for the info array in unclear and it could be determined from parsing the group tree after creation
+   *   With caching the performance impact would be small & the function would be cleaner
    *
-   *
-   * @static
    */
   public static function &getTree(
     $entityType,
@@ -522,8 +512,7 @@ ORDER BY civicrm_custom_group.weight,
     }
 
     $cacheKey = "CRM_Core_DAO_CustomGroup_Query " . md5($cacheString);
-    $multipleFieldGroupCacheKey =
-      "CRM_Core_DAO_CustomGroup_QueryMultipleFields " . md5($cacheString);
+    $multipleFieldGroupCacheKey = "CRM_Core_DAO_CustomGroup_QueryMultipleFields " . md5($cacheString);
     $cache = CRM_Utils_Cache::singleton();
     $tablesWithEntityData = array();
     if ($fromCache) {
@@ -656,7 +645,7 @@ ORDER BY civicrm_custom_group.weight,
    *
    * @param bool $getCount
    *
-   * @return boolean
+   * @return bool
    *   does this entity have data in this custom table
    */
   static public function customGroupDataExistsForEntity($entityID, $table, $getCount = FALSE) {
@@ -740,7 +729,7 @@ ORDER BY civicrm_custom_group.weight,
    * @param string $query
    * @param array $includedTables
    *   Tables to include - required because the function (for historical reasons).
-   * iterates through the group tree
+   *   iterates through the group tree
    */
   static public function buildTreeEntityDataFromQuery(&$groupTree, $query, $includedTables) {
     $dao = CRM_Core_DAO::executeQuery($query);
@@ -772,9 +761,9 @@ ORDER BY civicrm_custom_group.weight,
    *   (reference) the group tree being build.
    * @param string $table
    *   Table name.
-   * @param unknown_type $groupID
+   * @param int $groupID
    *   Custom group ID.
-   * @param unknown_type $fieldID
+   * @param int $fieldID
    *   Custom field ID.
    */
   static public function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID) {
@@ -801,8 +790,8 @@ ORDER BY civicrm_custom_group.weight,
               'name' => ts('Delete Attached File'),
               'url' => 'civicrm/file',
               'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete',
-              'extra' => 'onclick = "if (confirm( \'' . $deleteExtra .
-                '\' ) ) this.href+=\'&confirmed=1\'; else return false;"',
+              'extra' => 'onclick = "if (confirm( \'' . $deleteExtra
+              . '\' ) ) this.href+=\'&confirmed=1\'; else return false;"',
             ),
           );
           $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL,
@@ -832,12 +821,9 @@ ORDER BY civicrm_custom_group.weight,
               'entity_id',
               'file_id'
             );
-            $customValue['imageURL'] =
-              str_replace('persist/contribute', 'custom', $config->imageUploadURL) .
+            $customValue['imageURL'] = str_replace('persist/contribute', 'custom', $config->imageUploadURL) .
               $fileDAO->uri;
-            list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId,
-              NULL, NULL
-            );
+            list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId, NULL, NULL);
             if ($path && file_exists($path)) {
               list($imageWidth, $imageHeight) = getimagesize($path);
               list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
@@ -880,8 +866,6 @@ ORDER BY civicrm_custom_group.weight,
    *
    * @return string
    *   title
-   *
-   * @static
    */
   public static function getTitle($id) {
     return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $id, 'title');
@@ -903,9 +887,6 @@ ORDER BY civicrm_custom_group.weight,
    *
    * @return array
    *   array consisting of all group and field details
-   *
-   *
-   * @static
    */
   public static function &getGroupDetail($groupId = NULL, $searchable = NULL, &$extends = NULL, $inSelector = NULL) {
     // create a new tree
@@ -1104,7 +1085,6 @@ ORDER BY civicrm_custom_group.weight,
    *
    * @return string
    *
-   * @static
    *
    * @see _apachesolr_civiAttachments_dereference_file_parent
    */
@@ -1177,16 +1157,12 @@ ORDER BY civicrm_custom_group.weight,
    * Add the whereAdd clause for the DAO depending on the type of entity
    * the custom group is extending.
    *
-   * @param $customGroupDAO
+   * @param object $customGroupDAO
    * @param string $entityType
    *   What entity are we extending here ?.
    *
-   * @param object CRM_Core_DAO_CustomGroup (reference) - Custom Group DAO.
+   * @param int $entityID
    * @param bool $allSubtypes
-   *
-   * @return void
-   *
-   * @static
    */
   private static function _addWhereAdd(&$customGroupDAO, $entityType, $entityID = NULL, $allSubtypes = FALSE) {
     $addSubtypeClause = FALSE;
@@ -1246,19 +1222,17 @@ ORDER BY civicrm_custom_group.weight,
   /**
    * Delete the Custom Group.
    *
-   * @param $group
-   *   Object the DAO custom group object.
+   * @param CRM_Core_BAO_CustomGroup $group
+   *   Custom group object.
    * @param bool $force
    *   whether to force the deletion, even if there are custom fields.
    *
-   * @return boolean
-   *   false if field exists for this group, true if group gets deleted.
-   *
-   * @static
+   * @return bool
+   *   False if field exists for this group, true if group gets deleted.
    */
   public static function deleteGroup($group, $force = FALSE) {
 
-    //check wheter this contain any custom fields
+    //check whether this contain any custom fields
     $customField = new CRM_Core_DAO_CustomField();
     $customField->custom_group_id = $group->id;
     $customField->find();
@@ -1284,8 +1258,10 @@ ORDER BY civicrm_custom_group.weight,
   }
 
   /**
-   * @param $groupTree
-   * @param $defaults
+   * Set defaults.
+   *
+   * @param array $groupTree
+   * @param array $defaults
    * @param bool $viewMode
    * @param bool $inactiveNeeded
    * @param int $action
@@ -1428,7 +1404,9 @@ ORDER BY civicrm_custom_group.weight,
   }
 
   /**
-   * @param $groupTree
+   * PostProcess function.
+   *
+   * @param array $groupTree
    * @param array $params
    * @param bool $skipFile
    */
@@ -1472,10 +1450,9 @@ ORDER BY civicrm_custom_group.weight,
           case 'CheckBox':
             if (!empty($v)) {
               $customValue = array_keys($v);
-              $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] =
-                CRM_Core_DAO::VALUE_SEPARATOR .
-                implode(CRM_Core_DAO::VALUE_SEPARATOR, $customValue) .
-                CRM_Core_DAO::VALUE_SEPARATOR;
+              $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = CRM_Core_DAO::VALUE_SEPARATOR
+                . implode(CRM_Core_DAO::VALUE_SEPARATOR, $customValue)
+                . CRM_Core_DAO::VALUE_SEPARATOR;
             }
             else {
               $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = NULL;
@@ -1488,10 +1465,9 @@ ORDER BY civicrm_custom_group.weight,
             //added for Multi-Select
           case 'Multi-Select':
             if (!empty($v)) {
-              $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] =
-                CRM_Core_DAO::VALUE_SEPARATOR .
-                implode(CRM_Core_DAO::VALUE_SEPARATOR, $v) .
-                CRM_Core_DAO::VALUE_SEPARATOR;
+              $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = CRM_Core_DAO::VALUE_SEPARATOR
+                . implode(CRM_Core_DAO::VALUE_SEPARATOR, $v)
+                . CRM_Core_DAO::VALUE_SEPARATOR;
             }
             else {
               $groupTree[$groupID]['fields'][$fieldId]['customValue']['data'] = NULL;
@@ -1552,7 +1528,7 @@ ORDER BY civicrm_custom_group.weight,
   }
 
   /**
-   * Generic function to build all the form elements for a specific group tree
+   * Generic function to build all the form elements for a specific group tree.
    *
    * @param CRM_Core_Form $form
    *   The form object.
@@ -1562,9 +1538,6 @@ ORDER BY civicrm_custom_group.weight,
    *   Return inactive custom groups.
    * @param string $prefix
    *   Prefix for custom grouptree assigned to template.
-   *
-   * @return void
-   * @static
    */
   public static function buildQuickForm(&$form, &$groupTree, $inactiveNeeded = FALSE, $prefix = '') {
     $form->assign_by_ref("{$prefix}groupTree", $groupTree);
@@ -1591,8 +1564,7 @@ ORDER BY civicrm_custom_group.weight,
   }
 
   /**
-   * Extract the get params from the url, validate
-   * and store it in session
+   * Extract the get params from the url, validate and store it in session.
    *
    * @param CRM_Core_Form $form
    *   The form object.
@@ -1600,7 +1572,6 @@ ORDER BY civicrm_custom_group.weight,
    *   The type of custom group we are using.
    *
    * @return array
-   * @static
    */
   public static function extractGetParams(&$form, $type) {
     if (empty($_GET)) {
@@ -1707,10 +1678,8 @@ ORDER BY civicrm_custom_group.weight,
    * @param array $removeCustomFieldTypes
    *   Remove custom fields of a type eg: array("Individual") ;.
    *
-   *
-   * @return boolean
+   * @return bool
    *   false if it matches else true
-   * @static
    */
   public static function checkCustomField($customFieldId, &$removeCustomFieldTypes) {
     $query = "SELECT cg.extends as extends
@@ -1817,7 +1786,7 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
    * @param array $groupTree
    * @param int $groupCount
    *   Group count by default 1, but can varry for multiple value custom data.
-   * @param object form object
+   * @param object $form
    *
    * @return array
    */
@@ -1883,7 +1852,7 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
   }
 
   /**
-   * Build custom data view
+   * Build custom data view.
    *
    * @param CRM_Core_Form $form
    *   Page object.
@@ -1990,7 +1959,7 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
 
     //changed isset CRM-4601
     if (CRM_Utils_System::isNull($value)) {
-      return;
+      return NULL;
     }
 
     $htmlType = CRM_Utils_Array::value('html_type', $field);
@@ -2004,38 +1973,30 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
 
     switch ($dataType) {
       case 'Date':
-        $customTimeFormat = '';
         $customFormat = NULL;
 
-        switch ($timeFormat) {
-          case 1:
-            $customTimeFormat = '%l:%M %P';
-            break;
-
-          case 2:
-            $customTimeFormat = '%H:%M';
-            break;
-
-          default:
-            // if time is not selected remove time from value
-            $value = substr($value, 0, 10);
-        }
-
-        $supportableFormats = array(
-          'mm/dd' => "%B %E%f $customTimeFormat",
-          'dd-mm' => "%E%f %B $customTimeFormat",
-          'yy' => "%Y $customTimeFormat",
-          'M yy' => "%b %Y $customTimeFormat",
-          'yy-mm' => "%Y-%m $customTimeFormat",
-        );
+        $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats();
 
         if ($format = CRM_Utils_Array::value('date_format', $field)) {
-          if (array_key_exists($format, $supportableFormats)) {
-            $customFormat = $supportableFormats["$format"];
+          if (array_key_exists($format, $actualPHPFormats)) {
+            $customTimeFormat = (array) CRM_Utils_Array::value($format, $actualPHPFormats);
+            switch ($timeFormat) {
+              case 1:
+                $customTimeFormat[] = 'g:iA';
+                break;
+
+              case 2:
+                $customTimeFormat[] = 'G:i';
+                break;
+
+              default:
+                // if time is not selected remove time from value
+                $value = substr($value, 0, 10);
+            }
+            $customFormat = implode(" ", $customTimeFormat);
           }
         }
-
-        $retValue = CRM_Utils_Date::customFormat($value, $customFormat);
+        $retValue = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
         break;
 
       case 'Boolean':
@@ -2083,8 +2044,8 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
           $retValue = $value;
           break;
         }
-      // note that if its not text / textarea, the code falls thru and executes
-      // the below case also
+        // note that if its not text / textarea, the code falls thru and executes
+        // the below case also
       case 'StateProvince':
       case 'Country':
         $options = array();
@@ -2216,12 +2177,12 @@ SELECT IF( EXISTS(SELECT name FROM civicrm_contact_type WHERE name like %1), 1,
    * @param array $fieldIds
    *   Array of custom field ids.
    *
-   * @return array
+   * @return array|NULL
    *   array consisting of groups and fields labels with ids.
    */
   public static function getGroupTitles($fieldIds) {
     if (!is_array($fieldIds) && empty($fieldIds)) {
-      return;
+      return NULL;
     }
 
     $groupLabels = array();
@@ -2263,12 +2224,12 @@ SELECT  civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT
    * @param int $gID
    *   Custom group id.
    *
-   * @return boolean
+   * @return bool|NULL
    *   true if empty otherwise false.
    */
   public static function isGroupEmpty($gID) {
     if (!$gID) {
-      return;
+      return NULL;
     }
 
     $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
@@ -2293,7 +2254,7 @@ SELECT  civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT
    *   Var which should have the list appended.
    *
    * @return array
-   *   of types.
+   *   Array of types.
    */
   public static function getExtendedObjectTypes(&$types = array()) {
     static $flag = FALSE, $objTypes = array();
@@ -2374,4 +2335,5 @@ SELECT  civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT
     }
     return $multipleGroup;
   }
+
 }