CRM-15180, now we disable non-selectable tags
authorkurund <kurund@civicrm.org>
Thu, 18 Dec 2014 19:13:57 +0000 (00:43 +0530)
committerkurund <kurund@civicrm.org>
Thu, 18 Dec 2014 19:13:57 +0000 (00:43 +0530)
----------------------------------------
* CRM-15180: Using the taglist style of the "contact" page for the "new contact" page
  https://issues.civicrm.org/jira/browse/CRM-15180

CRM/Activity/Form/Activity.php
CRM/Case/Form/Case.php
CRM/Core/BAO/Tag.php
js/Common.js

index 455ea63099cea7fd9ced519d8ec708caf8132287..0b5d13fcb2ae4002317a1e668943f5b38233b8e1 100644 (file)
@@ -738,7 +738,9 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     $this->assign('customDataSubType', $this->_activityTypeId);
     $this->assign('entityID', $this->_activityId);
 
-    $tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
+    $tags = CRM_Core_BAO_Tag::getTags('civicrm_activity',
+      CRM_Core_DAO::$_nullArray, NULL,
+      '&nbsp;&nbsp;', TRUE);
 
     if (!empty($tags)) {
       $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
index f0bd2534718b51dcc189b382fb6b66d44d275267..07529edabf28acf30328b08bc16c29821f3e0350 100644 (file)
@@ -266,7 +266,10 @@ class CRM_Case_Form_Case extends CRM_Core_Form {
         'maxlength' => '128')), TRUE
     );
 
-    $tags = CRM_Core_BAO_Tag::getTags('civicrm_case');
+    $tags = CRM_Core_BAO_Tag::getTags('civicrm_case',
+      CRM_Core_DAO::$_nullArray, NULL,
+      '&nbsp;&nbsp;', TRUE);
+
     if (!empty($tags)) {
       $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
         array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
index 0983b663ebc4283034840ab0fedb2dbe4f790ffd..43e586a749e93fea8133eb045975d8b775ffd9e9 100644 (file)
@@ -179,17 +179,22 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
   }
 
   /**
-   * @param string $usedFor
-   * @param array $tags
-   * @param int $parentId
-   * @param string $separator
+   * Function to retrieve tags
+   *
+   * @param string $usedFor which type of tag entity
+   * @param array $tags tags array
+   * @param int $parentId parent id if you want need only children
+   * @param string $separator separator to indicate children
+   * @param boolean $formatSelectable add special property for non-selectable
+   *                tag, so they cannot be selected
    *
    * @return array
    */
   static function getTags($usedFor = 'civicrm_contact',
     &$tags = array(),
     $parentId  = NULL,
-    $separator = '&nbsp;&nbsp;'
+    $separator = '&nbsp;&nbsp;',
+    $formatSelectable = FALSE
   ) {
     if (!is_array($tags)) {
       $tags = array();
@@ -200,7 +205,7 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
     // Instead of recursively making mysql queries, we'll make one big
     // query and build the heirarchy with the algorithm below.
     $args = array(1 => array('%' . $usedFor . '%', 'String'));
-    $query = "SELECT id, name, parent_id, is_tagset
+    $query = "SELECT id, name, parent_id, is_tagset, is_selectable
                   FROM civicrm_tag
               WHERE used_for LIKE %1";
     if ($parentId) {
@@ -217,13 +222,35 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
     // are necessarily placed.
     $roots = $rows = array();
     while ($dao->fetch()) {
+      // note that we are prepending id with "crm_disabled_opt" which identifies
+      // them as disabled so that they cannot be selected. We do some magic
+      // in crm-select2 js function that marks option values to "disabled"
+      // current QF version in CiviCRM does not support passing this attribute,
+      // so this is another ugly hack / workaround,
+      // also know one is too keen to upgrade QF :P
+      $idPrefix = '';
+      if ($formatSelectable && !$dao->is_selectable) {
+        $idPrefix = "crm_disabled_opt";
+      }
       if ($dao->parent_id == $parentId && $dao->is_tagset == 0) {
-        $roots[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name);
+        $roots[] = array(
+          'id' => $dao->id,
+          'prefix' => '',
+          'name' => $dao->name,
+          'idPrefix' => $idPrefix,
+          );
       }
       else {
-        $rows[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name, 'parent_id' => $dao->parent_id);
+        $rows[] = array(
+          'id' => $dao->id,
+          'prefix' => '',
+          'name' => $dao->name,
+          'parent_id' => $dao->parent_id,
+          'idPrefix' => $idPrefix,
+         );
       }
     }
+
     $dao->free();
     // While we have nodes left to build, shift the first (alphabetically)
     // node of the list, place it in our tags list and loop through the
@@ -234,7 +261,11 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
       $new_roots         = array();
       $current_rows      = $rows;
       $root              = array_shift($roots);
-      $tags[$root['id']] = array($root['prefix'], $root['name']);
+      $tags[$root['id']] = array(
+        $root['prefix'],
+        $root['name'],
+        $root['idPrefix'],
+        );
 
       // As you find the children, append them to the end of the new set
       // of roots (maintain alphabetical ordering). Also remove the node
@@ -242,7 +273,12 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
       if (is_array($current_rows)) {
         foreach ($current_rows as $key => $row) {
           if ($row['parent_id'] == $root['id']) {
-            $new_roots[] = array('id' => $row['id'], 'prefix' => $tags[$root['id']][0] . $separator, 'name' => $row['name']);
+            $new_roots[] = array(
+              'id' => $row['id'],
+              'prefix' => $tags[$root['id']][0] . $separator,
+              'name' => $row['name'],
+              'idPrefix' => $row['idPrefix'],
+              );
             unset($rows[$key]);
           }
         }
@@ -255,10 +291,17 @@ class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
 
     // Prefix each name with the calcuated spacing to give the visual
     // appearance of ordering when transformed into HTML in the form layer.
-    foreach ($tags as & $tag) {
-      $tag = $tag[0] . $tag[1];
+    // here is the actual code that to prepends and set disabled attribute for
+    // non-selectable tags
+    $formattedTags = array();
+    foreach ($tags as $key => $tag) {
+      if (!empty($tag[2])) {
+        $key = $tag[2]. "-" . $key;
+      }
+      $formattedTags[$key] = $tag[0] . $tag[1];
     }
 
+    $tags = $formattedTags;
     return $tags;
   }
 
index 9586f9c588cfda0cf10536689141be87058d6ef7..3f679f23a7d8478397b614d5781a64d32527c05e 100644 (file)
@@ -309,6 +309,12 @@ CRM.strings = CRM.strings || {};
         $(this).nextUntil('option[value^=crm_optgroup]').wrapAll('<optgroup label="' + $(this).text() + '" />');
         $(this).remove();
       });
+
+      // quickform does not support disabled option, so yet another hack to
+      // add disabled property for option values
+      $('option[value^=crm_disabled_opt]', this).each(function () {
+        $(this).attr('disabled', 'disabled');
+      });
       // Defaults for single-selects
       if ($el.is('select:not([multiple])')) {
         settings.minimumResultsForSearch = 10;