Fix for CRM-15729
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
index b29fa733dfc0c180a8ffa0dadbf930c3350abaaa..3e87594de9f4b0a64382087813e870cc3c68c47b 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
 class CRM_Contact_Form_Edit_TagsAndGroups {
 
   /**
-   * Constant to determine which forms we are generating
+   * Constant to determine which forms we are generating.
    *
    * Used by both profile and edit contact
    */
-  CONST GROUP = 1, TAG = 2, ALL = 3;
+  const GROUP = 1, TAG = 2, ALL = 3;
 
   /**
-   * This function is to build form elements
+   * build form elements.
    * params object $form object of the form
    *
-   * @param CRM_Core_Form $form the form object that we are operating on
-   * @param int $contactId contact id
-   * @param int $type what components are we interested in
-   * @param boolean $visibility visibility of the field
+   * @param CRM_Core_Form $form
+   *   The form object that we are operating on.
+   * @param int $contactId
+   *   Contact id.
+   * @param int $type
+   *   What components are we interested in.
+   * @param bool $visibility
+   *   Visibility of the field.
    * @param null $isRequired
-   * @param string $groupName if used for building group block
-   * @param string $tagName if used for building tag block
-   * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
+   * @param string $groupName
+   *   If used for building group block.
+   * @param string $tagName
+   *   If used for building tag block.
+   * @param string $fieldName
+   *   This is used in batch profile(i.e to build multiple blocks).
    *
    * @param string $groupElementType
    *
-   * @static
-   * @access public
    */
-  static function buildQuickForm(&$form,
+  public static function buildQuickForm(
+    &$form,
     $contactId = 0,
     $type = self::ALL,
     $visibility = FALSE,
@@ -142,11 +148,6 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
     }
 
     if ($type & self::TAG) {
-      // CODE FROM CRM/Tag/Form/Tag.php //
-      CRM_Core_Resources::singleton()
-        ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
-        ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
-
       $fName = 'tag';
       if ($fieldName) {
         $fName = $fieldName;
@@ -156,6 +157,16 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       // get the list of all the categories
       $tags = new CRM_Core_BAO_Tag();
       $tree = $tags->getTree('civicrm_contact', TRUE);
+      // let's not load jstree if there are not children. This also fixes blank
+      // display at the beginning of checkboxes
+      $form->assign('loadjsTree', FALSE);
+      if (!empty(CRM_Utils_Array::retrieveValueRecursive($tree, 'children'))) {
+        // CODE FROM CRM/Tag/Form/Tag.php //
+        CRM_Core_Resources::singleton()
+          ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
+          ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
+        $form->assign('loadjsTree', TRUE);
+      }
 
       $elements = array();
       self::climbtree($form, $tree, $elements);
@@ -177,12 +188,21 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
     }
     $form->assign('tagGroup', $form->_tagGroup);
   }
-  
-  static function climbtree($form, $tree, &$elements) {
+
+  /**
+   * Climb tree.
+   *
+   * @param $form
+   * @param $tree
+   * @param $elements
+   *
+   * @return mixed
+   */
+  public static function climbtree($form, $tree, &$elements) {
     foreach ($tree as $tagID => $varValue) {
       $tagAttribute = array(
-      'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
-      'id' => "tag_{$tagID}",
+        'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
+        'id' => "tag_{$tagID}",
       );
 
       $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
@@ -191,24 +211,26 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
         self::climbtree($form, $varValue['children'], $elements);
       }
     }
-   return $elements;
+    return $elements;
   }
 
   /**
-   * Set defaults for relevant form elements
+   * Set defaults for relevant form elements.
    *
-   * @param int $id the contact id
-   * @param array $defaults the defaults array to store the values in
-   * @param int $type what components are we interested in
-   * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
+   * @param int $id
+   *   The contact id.
+   * @param array $defaults
+   *   The defaults array to store the values in.
+   * @param int $type
+   *   What components are we interested in.
+   * @param string $fieldName
+   *   This is used in batch profile(i.e to build multiple blocks).
    *
    * @param string $groupElementType
    *
    * @return void
-   * @access public
-   * @static
    */
-  static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
+  public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
     $type = (int ) $type;
     if ($type & self::GROUP) {
       $fName = 'group';
@@ -248,7 +270,6 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
    * Set default values for the form. Note that in edit/view mode
    * the default values are retrieved from the database
    *
-   * @access public
    *
    * @param CRM_Core_Form $form
    * @param $defaults
@@ -280,4 +301,5 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       }
     }
   }
+
 }