Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-07-15-32-51
[civicrm-core.git] / CRM / Contact / BAO / Group.php
index b8f2bcb3669abe81e8c17008c7a7b3a63fe26ec0..52e860efdc528f4c21d20b5369b24b72ccaea706 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.4                                                |
+  | CiviCRM version 4.5                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2013                                |
+  | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -346,7 +346,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    */
   public static function &create(&$params) {
 
-    if (CRM_Utils_Array::value('id', $params)) {
+    if (!empty($params['id'])) {
       CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params);
     }
     else {
@@ -354,9 +354,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     }
 
     // form the name only if missing: CRM-627
-    if (!CRM_Utils_Array::value('name', $params) &&
-      !CRM_Utils_Array::value('id', $params)
-    ) {
+    $nameParam = CRM_Utils_Array::value('name', $params, NULL);
+    if (!$nameParam && empty($params['id'])) {
       $params['name'] = CRM_Utils_String::titleToVar($params['title']);
     }
 
@@ -385,8 +384,19 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
 
     $group = new CRM_Contact_BAO_Group();
     $group->copyValues($params);
-
-    if (!CRM_Utils_Array::value('id', $params)) {
+    //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but
+    // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn
+    // use metadata to translate the array to the appropriate DB type or altering the param in the api layer,
+    // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down
+    // we need the $params one to be in it's original form & we are not sure what test coverage we have on that
+    if(isset($group->parents) && is_array($group->parents)) {
+      $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
+        array_keys($group->parents)
+      ) . CRM_Core_DAO::VALUE_SEPARATOR;
+    }
+    if (empty($params['id']) &&
+      !$nameParam
+    ) {
       $group->name .= "_tmp";
     }
     $group->save();
@@ -395,7 +405,9 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       return NULL;
     }
 
-    if (!CRM_Utils_Array::value('id', $params)) {
+    if (empty($params['id']) &&
+      !$nameParam
+    ) {
       $group->name = substr($group->name, 0, -4) . "_{$group->id}";
     }
 
@@ -403,7 +415,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     $group->save();
 
     // add custom field values
-    if (CRM_Utils_Array::value('custom', $params)) {
+    if (!empty($params['custom'])) {
       CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id);
     }
 
@@ -447,7 +459,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       }
     }
 
-    if (CRM_Utils_Array::value('organization_id', $params)) {
+    if (!empty($params['organization_id'])) {
       $groupOrg = array();
       $groupOrg = $params;
       $groupOrg['group_id'] = $group->id;
@@ -456,7 +468,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
 
     CRM_Contact_BAO_GroupContactCache::add($group->id);
 
-    if (CRM_Utils_Array::value('id', $params)) {
+    if (!empty($params['id'])) {
       CRM_Utils_Hook::post('edit', 'Group', $group->id, $group);
     }
     else {
@@ -515,7 +527,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @static
    */
   public static function createSmartGroup(&$params) {
-    if (CRM_Utils_Array::value('formValues', $params)) {
+    if (!empty($params['formValues'])) {
       $ssParams = $params;
       unset($ssParams['id']);
       if (isset($ssParams['saved_search_id'])) {
@@ -639,7 +651,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     }
 
     $smartGroupId = NULL;
-    if (CRM_Utils_Array::value('saved_search_id', $params)) {
+    if (!empty($params['saved_search_id'])) {
       $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id');
     }
     else {
@@ -666,9 +678,13 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
    * @param  array   $params associated array for params record id.
    *
    * @return array   $groupList associated array of group list
+   *  -rp = rowcount
+   *  -page= offset
+   *  @todo there seems little reason for the small number of functions that call this to pass in
+   *  params that then need to be translated in this function since they are coding them when calling
    * @access public
    */
-  public function getGroupListSelector(&$params) {
+  static public function getGroupListSelector(&$params) {
     // format the params
     $params['offset']   = ($params['page'] - 1) * $params['rp'];
     $params['rowCount'] = $params['rp'];
@@ -678,7 +694,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     $groups = CRM_Contact_BAO_Group::getGroupList($params);
 
     //skip total if we are making call to show only children
-    if ( !CRM_Utils_Array::value('parent_id', $params) ) {
+    if (empty($params['parent_id'])) {
       // add total
       $params['total'] = CRM_Contact_BAO_Group::getGroupCount($params);
 
@@ -692,22 +708,21 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       foreach ($groups as $id => $value) {
         $groupList[$id]['group_id'] = $value['id'];
         $groupList[$id]['group_name'] = $value['title'];
-        $groupList[$id]['class'] = $value['class'];
+        $groupList[$id]['class'] = implode(' ', $value['class']);
 
         // append parent names if in search mode
-        if ( !CRM_Utils_Array::value('parent_id', $params) &&
-        CRM_Utils_Array::value( 'parents', $value ) ) {
+        if (empty($params['parent_id']) && !empty($value['parents'])) {
           $groupIds = explode(',', $value['parents']);
           $title = array();
           foreach($groupIds as $gId) {
             $title[] = $allGroups[$gId];
           }
           $groupList[$id]['group_name'] .= '<div class="crm-row-parent-name"><em>'.ts('Child of').'</em>: ' . implode(', ', $title) . '</div>';
-          $groupList[$id]['class'] = '';
+          $groupList[$id]['class'] = in_array('disabled', $value['class']) ? 'disabled' : '';
         }
 
         $groupList[$id]['group_description'] = CRM_Utils_Array::value('description', $value);
-        if ( CRM_Utils_Array::value('group_type', $value) ) {
+        if (!empty($value['group_type'])) {
           $groupList[$id]['group_type'] = $value['group_type'];
         }
         else {
@@ -796,6 +811,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
     $allTypes = CRM_Core_OptionGroup::values('group_type');
     $values = array();
 
+    $visibility = CRM_Core_SelectValues::ufVisibility();
+
     while ($object->fetch()) {
       $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title);
       //@todo CRM-12209 introduced an ACL check in the whereClause function
@@ -804,7 +821,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
 
       if ($permission) {
         $newLinks = $links;
-        $values[$object->id] = array();
+        $values[$object->id] = array('class' => array());
         CRM_Core_DAO::storeValues($object, $values[$object->id]);
         if ($object->saved_search_id) {
           $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')';
@@ -833,13 +850,12 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
           }
         }
 
-        $values[$object->id]['class'] = '';
         if (array_key_exists('is_active', $object)) {
           if ($object->is_active) {
             $action -= CRM_Core_Action::ENABLE;
           }
           else {
-            $values[$object->id]['class'] = 'disabled';
+            $values[$object->id]['class'][] = 'disabled';
             $action -= CRM_Core_Action::VIEW;
             $action -= CRM_Core_Action::DISABLE;
           }
@@ -847,9 +863,8 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
 
         $action = $action & CRM_Core_Action::mask($groupPermissions);
 
-        $values[$object->id]['visibility'] = CRM_Contact_DAO_Group::tsEnum('visibility',
-          $values[$object->id]['visibility']
-        );
+        $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']];
+
         if (isset($values[$object->id]['group_type'])) {
           $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
             substr($values[$object->id]['group_type'], 1, -1)
@@ -876,18 +891,13 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
         // If group has children, add class for link to view children
         $values[$object->id]['is_parent'] = false;
         if (array_key_exists('children', $values[$object->id])) {
-          $values[$object->id]['class'] = "crm-group-parent";
+          $values[$object->id]['class'][] = "crm-group-parent";
           $values[$object->id]['is_parent'] = true;
         }
 
         // If group is a child, add child class
         if (array_key_exists('parents', $values[$object->id])) {
-          $values[$object->id]['class'] = "crm-group-child";
-        }
-
-        if (array_key_exists('children', $values[$object->id])
-        && array_key_exists('parents', $values[$object->id])) {
-          $values[$object->id]['class'] = "crm-group-child crm-group-parent";
+          $values[$object->id]['class'][] = "crm-group-child";
         }
 
         if ($groupOrg) {
@@ -1030,7 +1040,7 @@ WHERE  id IN $groupIdString
     $whereClause = self::whereClause($params, FALSE);
     $query = "SELECT COUNT(*) FROM civicrm_group groups";
 
-    if (CRM_Utils_Array::value('created_by', $params)) {
+    if (!empty($params['created_by'])) {
       $query .= "
 INNER JOIN civicrm_contact createdBy
        ON createdBy.id = groups.created_id";
@@ -1040,7 +1050,7 @@ WHERE {$whereClause}";
     return CRM_Core_DAO::singleValueQuery($query, $params);
   }
 
-  function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) {
+  static function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) {
     $values = array();
     $clauses = array();
 
@@ -1175,14 +1185,12 @@ WHERE {$whereClause}";
       ),
       CRM_Core_Action::DISABLE => array(
         'name' => ts('Disable'),
-        'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Contact_BAO_Group' . '\',\'' . 'enable-disable' . '\' );"',
-        'ref' => 'disable-action',
+        'ref' => 'crm-enable-disable',
         'title' => ts('Disable Group'),
       ),
       CRM_Core_Action::ENABLE => array(
         'name' => ts('Enable'),
-        'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Contact_BAO_Group' . '\',\'' . 'disable-enable' . '\' );"',
-        'ref' => 'enable-action',
+        'ref' => 'crm-enable-disable',
         'title' => ts('Enable Group'),
       ),
       CRM_Core_Action::DELETE => array(