improve performance for action links and add button
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 6 Oct 2016 10:04:12 +0000 (15:34 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 6 Oct 2016 12:59:43 +0000 (18:29 +0530)
CRM/Contact/Form/CustomData.php
CRM/Contact/Page/View/CustomData.php
CRM/Core/BAO/CustomGroup.php
CRM/Custom/Form/CustomData.php
CRM/Profile/Page/MultipleRecordFieldsListing.php

index bbb3c4e09014990acbac9282eca978d22048c521..6b6c38fb626c3aa420fbec0e5c01c04199a883e0 100644 (file)
@@ -233,7 +233,14 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
         $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type,
           $this,
           $this->_entityId,
-          $this->_groupID
+          $this->_groupID,
+          array(),
+          NULL,
+          TRUE,
+          NULL,
+          FALSE,
+          TRUE,
+          $this->_copyValueId
         );
         $valueIdDefaults = array();
         $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
index 9a1a45a6968672be47569ebcf6a456769daab913..16cad611792155016463be59442f220c4349b37c 100644 (file)
@@ -136,18 +136,23 @@ class CRM_Contact_Page_View_CustomData extends CRM_Core_Page {
         //Custom Groups Inline
         $entityType = CRM_Contact_BAO_Contact::getContactType($this->_contactId);
         $entitySubType = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
-        $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityType, $this, $this->_contactId,
-          $this->_groupId, $entitySubType
-        );
         $recId = NULL;
         if ($this->_multiRecordDisplay == 'single') {
           $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupId);
           CRM_Utils_System::setTitle(ts('View %1 Record', array(1 => $groupTitle)));
+          $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityType, $this, $this->_contactId,
+            $this->_groupId, $entitySubType, NULL, TRUE, NULL, FALSE, TRUE, $this->_cgcount
+          );
 
           $recId = $this->_recId;
           $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
           $this->assign('skipTitle', 1);
         }
+        else {
+          $groupTree = CRM_Core_BAO_CustomGroup::getTree($entityType, $this, $this->_contactId,
+            $this->_groupId, $entitySubType
+          );
+        }
         CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, $recId, $this->_contactId);
       }
     }
index 0e3574b66efda8a66935546986c1bb449c686203..b2f402218d1901e7569acbca0b254c7f8550638f 100644 (file)
@@ -330,6 +330,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
    *   api - through which it is properly tested - so can be refactored with some comfort.)
    *
    * @param bool $checkPermission
+   * @param varchar $singleRecord
+   *   holds 'new' or id if view/edit/copy form for a single record is being loaded.
    *
    * @return array
    *   Custom field 'tree'.
@@ -353,7 +355,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup {
     $fromCache = TRUE,
     $onlySubType = NULL,
     $returnAll = FALSE,
-    $checkPermission = TRUE
+    $checkPermission = TRUE,
+    $singleRecord = NULL
   ) {
     if ($entityID) {
       $entityID = CRM_Utils_Type::escape($entityID, 'Integer');
@@ -596,7 +599,7 @@ ORDER BY civicrm_custom_group.weight,
     // add info to groupTree
 
     if (isset($groupTree['info']) && !empty($groupTree['info']) &&
-      !empty($groupTree['info']['tables'])
+      !empty($groupTree['info']['tables']) && $singleRecord != 'new'
     ) {
       $select = $from = $where = array();
       $groupTree['info']['where'] = NULL;
@@ -630,7 +633,7 @@ ORDER BY civicrm_custom_group.weight,
       }
       $multipleFieldTablesWithEntityData = array_keys($entityMultipleSelectClauses);
       if (!empty($multipleFieldTablesWithEntityData)) {
-        self::buildEntityTreeMultipleFields($groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData);
+        self::buildEntityTreeMultipleFields($groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData, $singleRecord);
       }
 
     }
@@ -751,8 +754,10 @@ ORDER BY civicrm_custom_group.weight,
    *   Array of select clauses relevant to the entity.
    * @param array $multipleFieldTablesWithEntityData
    *   Array of tables in which this entity has data.
+   * @param varchar $singleRecord
+   *   holds 'new' or id if view/edit/copy form for a single record is being loaded.
    */
-  static public function buildEntityTreeMultipleFields(&$groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData) {
+  static public function buildEntityTreeMultipleFields(&$groupTree, $entityID, $entityMultipleSelectClauses, $multipleFieldTablesWithEntityData, $singleRecord = NULL) {
     foreach ($entityMultipleSelectClauses as $table => $selectClauses) {
       $select = implode(',', $selectClauses);
       $query = "
@@ -760,7 +765,11 @@ ORDER BY civicrm_custom_group.weight,
         FROM $table
         WHERE entity_id = $entityID
       ";
-      self::buildTreeEntityDataFromQuery($groupTree, $query, array($table));
+      if ($singleRecord) {
+        $offset = $singleRecord - 1;
+        $query .= " LIMIT {$offset}, 1";
+      }
+      self::buildTreeEntityDataFromQuery($groupTree, $query, array($table), $singleRecord);
     }
   }
 
@@ -776,8 +785,10 @@ ORDER BY civicrm_custom_group.weight,
    * @param array $includedTables
    *   Tables to include - required because the function (for historical reasons).
    *   iterates through the group tree
+   * @param varchar $singleRecord
+   *   holds 'new' OR id if view/edit/copy form for a single record is being loaded.
    */
-  static public function buildTreeEntityDataFromQuery(&$groupTree, $query, $includedTables) {
+  static public function buildTreeEntityDataFromQuery(&$groupTree, $query, $includedTables, $singleRecord = NULL) {
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
       foreach ($groupTree as $groupID => $group) {
@@ -792,7 +803,7 @@ ORDER BY civicrm_custom_group.weight,
           continue;
         }
         foreach ($group['fields'] as $fieldID => $dontCare) {
-          self::buildCustomFieldData($dao, $groupTree, $table, $groupID, $fieldID);
+          self::buildCustomFieldData($dao, $groupTree, $table, $groupID, $fieldID, $singleRecord);
         }
       }
     }
@@ -811,8 +822,10 @@ ORDER BY civicrm_custom_group.weight,
    *   Custom group ID.
    * @param int $fieldID
    *   Custom field ID.
+   * @param varchar $singleRecord
+   *   holds 'new' or id if loading view/edit/copy for a single record.
    */
-  static public function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID) {
+  static public function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID, $singleRecord = NULL) {
     $column = $groupTree[$groupID]['fields'][$fieldID]['column_name'];
     $idName = "{$table}_id";
     $fieldName = "{$table}_{$column}";
@@ -896,7 +909,10 @@ ORDER BY civicrm_custom_group.weight,
     if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) {
       $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array();
     }
-    if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) {
+    if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue']) && !empty($singleRecord)) {
+      $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array($singleRecord => $customValue);
+    }
+    elseif (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) {
       $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(1 => $customValue);
     }
     else {
index be7338081aa5ab5fda559b7cf757c18b51faa4ef..51547d825ef4f01c1b6e772c3bdaf72e86e2e5ab 100644 (file)
@@ -151,6 +151,14 @@ class CRM_Custom_Form_CustomData {
    * @return array
    */
   public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = FALSE) {
+    $singleRecord = NULL;
+    if (!empty($form->_groupCount) && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = $form->_groupCount;
+    }
+    $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
+    if ($form->_groupCount == 0 && $mode == 'add' && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = 'new';
+    }
 
     $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
       $form,
@@ -159,7 +167,10 @@ class CRM_Custom_Form_CustomData {
       $subType,
       $form->_subName,
       $getCachedTree,
-      $onlySubType
+      $onlySubType,
+      FALSE,
+      TRUE,
+      $singleRecord
     );
 
     if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
index 1598f4f8df1de14593d18fd9e54b6064f48c10f9..63f1e30d4286d43c034f33b30435d8a37de9b2c3 100644 (file)
@@ -109,7 +109,7 @@ class CRM_Profile_Page_MultipleRecordFieldsListing extends CRM_Core_Page_Basic {
       elseif ($this->_pageViewType == 'customDataView') {
         // custom data specific view links
         $links[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/view/cd';
-        $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&multiRecordDisplay=single&mode=view';
+        $links[CRM_Core_Action::VIEW]['qs'] = 'reset=1&gid=%%gid%%&cid=%%cid%%&recId=%%recId%%&cgcount=%%cgcount%%&multiRecordDisplay=single&mode=view';
 
         // custom data specific update links
         $links[CRM_Core_Action::UPDATE]['url'] = 'civicrm/contact/view/cd/edit';