CRM-15456 - Add safety checks to avoid errors when updating tabs
authorColeman Watts <coleman@civicrm.org>
Tue, 14 Oct 2014 21:35:22 +0000 (17:35 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 14 Oct 2014 22:16:09 +0000 (18:16 -0400)
CRM/Contact/Form/Inline/ContactInfo.php
CRM/Event/Page/Tab.php
CRM/Member/Page/Tab.php
templates/CRM/common/TabHeader.js

index e906cd326ad7018664e292b9c07a3d3e7a2c190e..fd54e848714df55e23f077fdf503c566f31bd70c 100644 (file)
@@ -84,9 +84,13 @@ class CRM_Contact_Form_Inline_ContactInfo extends CRM_Contact_Form_Inline {
     // Saving current employer affects relationship tab, and possibly related memberships and contributions
     $this->ajaxResponse['updateTabs'] = array(
       '#tab_rel' => CRM_Contact_BAO_Contact::getCountComponent('rel', $this->_contactId),
-      '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
-      '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId),
     );
+    if (CRM_Core_Permission::access('CiviContribute')) {
+      $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
+    }
+    if (CRM_Core_Permission::access('CiviMember')) {
+      $this->ajaxResponse['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId);
+    }
 
     $this->response();
   }
index 8a8ab7bb7aa49dfc5aa115a3445608db02d90d8f..72d857e725f523bf6e53cf9ce39815ce521448e1 100644 (file)
@@ -62,9 +62,11 @@ class CRM_Event_Page_Tab extends CRM_Core_Page {
       $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('participant', $this->_contactId);
       // Refresh other tabs with related data
       $this->ajaxResponse['updateTabs'] = array(
-        '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
         '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
       );
+      if (CRM_Core_Permission::access('CiviContribute')) {
+        $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
+      }
     }
   }
 
index e451396b501b13aab127a895840dcf2fa97b2073..bc798afb6a545e906c1423298d546363424e29d6 100644 (file)
@@ -208,10 +208,12 @@ class CRM_Member_Page_Tab extends CRM_Core_Page {
       $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId);
       // Refresh other tabs with related data
       $this->ajaxResponse['updateTabs'] = array(
-        '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
         '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
         '#tab_rel' => CRM_Contact_BAO_Contact::getCountComponent('rel', $this->_contactId),
       );
+      if (CRM_Core_Permission::access('CiviContribute')) {
+        $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
+      }
     }
   }
 
index f45929ff931f149d25abe14fa9657af050c07903..429ec7660fa399349a44d94bddeec4a59464b323 100644 (file)
@@ -69,7 +69,7 @@ CRM.$(function($) {
    */
   CRM.tabHeader.getActiveTab = function() {
     return $('.ui-tabs-active', '#mainTabContainer');
-  }
+  };
 
   /**
    * Make a given tab the active one
@@ -84,22 +84,39 @@ CRM.$(function($) {
    * @returns panel jQuery object
    */
   CRM.tabHeader.getTabPanel = function(tab) {
-    return $('#' + $(tab).attr('aria-controls'));
+    var selector = $(tab).attr('aria-controls');
+    return selector ? $('#' + selector) : $();
   };
 
-  CRM.tabHeader.getCount = function(tab) {
-    return parseInt($(tab).find('a em').text(), 10);
+  /**
+   * @param tab jQuery selector
+   * @returns {string|null}
+   */
+  function getCountClass(tab) {
+    var $tab = $(tab),
+      css = $tab.attr('class') || '',
+      val = css.match(/(crm-count-\d+)/);
+    return val && val.length ? val[0] : null;
   }
 
+  /**
+   * @param tab jQuery selector
+   * @returns {Number|null}
+   */
+  CRM.tabHeader.getCount = function(tab) {
+    var cssClass = getCountClass(tab);
+    return cssClass ? parseInt(cssClass.slice(10), 10) : null;
+  };
+
   /**
    * Update the counter in a tab
    * @param tab jQuery selector
-   * @param count number
+   * @param count {Number}
    */
   CRM.tabHeader.updateCount = function(tab, count) {
-    var oldClass = $(tab).attr('class').match(/(crm-count-\d+)/);
+    var oldClass = getCountClass(tab);
     if (oldClass) {
-      $(tab).removeClass(oldClass[0]);
+      $(tab).removeClass(oldClass);
     }
     $(tab)
       .addClass('crm-count-' + count)
@@ -107,15 +124,16 @@ CRM.$(function($) {
   };
 
   /**
-   * Clears tab content so that it will be refreshed next time the user clicks on it
+   * Refresh tab immediately if it is active, otherwise ensure it will be refreshed next time the user clicks on it
    * @param tab
    */
   CRM.tabHeader.resetTab = function(tab) {
     var $panel = CRM.tabHeader.getTabPanel(tab);
     if ($(tab).hasClass('ui-tabs-active')) {
       $panel.crmSnippet('refresh');
-    } else {
-      $panel.data("civiCrmSnippet") && $panel.crmSnippet('destroy');
+    }
+    else if ($panel.data("civiCrmSnippet")) {
+      $panel.crmSnippet('destroy');
     }
   };
 })(CRM.$);