Merge pull request #4764 from rohankatkar/CRM-15615
[civicrm-core.git] / CRM / Contact / Form / Edit / TagsAndGroups.php
index 3726676014856cf7f6d5f6c5b45722877e60ec31..d7b91d266ebd7683ef061809b045f2d41d02c5a7 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.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
 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
    */
@@ -45,13 +45,16 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
    * This function is to build form elements
    * params object $form object of the form
    *
-   * @param Object  $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 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 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 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 $groupElementType
    *
    * @static
    * @access public
@@ -110,8 +113,8 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
           ) {
             continue;
           }
-          
-          if ($groupElementType == 'crmasmSelect') {
+
+          if ($groupElementType == 'select') {
             $groupsOptions[$id] = $group['title'];
           }
           else {
@@ -120,9 +123,9 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
           }
         }
 
-        if ($groupElementType == 'crmasmSelect' && !empty($groupsOptions)) {
+        if ($groupElementType == 'select' && !empty($groupsOptions)) {
           $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
-            array('id' => $fName, 'multiple' => 'multiple', 'title' => ts('- select -'))
+            array('id' => $fName, 'multiple' => 'multiple', 'class' => 'crm-select2')
           );
           $form->assign('groupCount', count($groupsOptions));
         }
@@ -139,21 +142,30 @@ 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;
       }
       $form->_tagGroup[$fName] = 1;
+
+      // get the list of all the categories
+      $tags = new CRM_Core_BAO_Tag();
+      $tree = $tags->getTree('civicrm_contact', TRUE);
+
       $elements = array();
-      $tag = CRM_Core_BAO_Tag::getTags();
+      self::climbtree($form, $tree, $elements);
 
-      foreach ($tag as $id => $name) {
-        $elements[] = $form->createElement('checkbox', $id, NULL, $name);
-      }
-      if (!empty($elements)) {
-        $form->addGroup($elements, $fName, $tagName, '<br />');
-        $form->assign('tagCount', count($elements));
-      }
+      $form->addGroup($elements, $fName, $tagName, '<br />');
+      $form->assign('tagCount', count($elements));
+      $form->assign('tree', $tree);
+      $form->assign('tag', $tree);
+      $form->assign('entityID', $contactId);
+      $form->assign('entityTable', 'civicrm_contact');
 
       if ($isRequired) {
         $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
@@ -161,20 +173,37 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
 
       // build tag widget
       $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
-
       CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, FALSE, TRUE);
     }
     $form->assign('tagGroup', $form->_tagGroup);
   }
 
+  static function climbtree($form, $tree, &$elements) {
+    foreach ($tree as $tagID => $varValue) {
+      $tagAttribute = array(
+      'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
+      'id' => "tag_{$tagID}",
+      );
+
+      $elements[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
+
+      if (array_key_exists('children', $varValue)) {
+        self::climbtree($form, $varValue['children'], $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 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
@@ -190,7 +219,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
       $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
       if ($contactGroup) {
         foreach ($contactGroup as $group) {
-          if ($groupElementType == 'crmasmSelect') {
+          if ($groupElementType == 'select') {
             $defaults[$fName][] = $group['group_id'];
           }
           else {
@@ -216,11 +245,14 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
   }
 
   /**
-   * This function sets the default values for the form. Note that in edit/view mode
+   * 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
+   *
    * @return void
    */
   public static function setDefaultValues(&$form, &$defaults) {
@@ -242,7 +274,7 @@ class CRM_Contact_Form_Edit_TagsAndGroups {
         // set the group and tag ids
         $groupElementType = 'checkbox';
         if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
-          $groupElementType = 'crmasmSelect';
+          $groupElementType = 'select';
         }
         self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
       }