From 47358d9253a58c843299443a84ffafc608a11919 Mon Sep 17 00:00:00 2001 From: kurund Date: Fri, 19 Dec 2014 00:43:57 +0530 Subject: [PATCH] CRM-15180, now we disable non-selectable tags ---------------------------------------- * 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 | 4 +- CRM/Case/Form/Case.php | 5 ++- CRM/Core/BAO/Tag.php | 67 ++++++++++++++++++++++++++++------ js/Common.js | 6 +++ 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/CRM/Activity/Form/Activity.php b/CRM/Activity/Form/Activity.php index 455ea63099..0b5d13fcb2 100644 --- a/CRM/Activity/Form/Activity.php +++ b/CRM/Activity/Form/Activity.php @@ -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, + '  ', TRUE); if (!empty($tags)) { $this->add('select', 'tag', ts('Tags'), $tags, FALSE, diff --git a/CRM/Case/Form/Case.php b/CRM/Case/Form/Case.php index f0bd253471..07529edabf 100644 --- a/CRM/Case/Form/Case.php +++ b/CRM/Case/Form/Case.php @@ -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, + '  ', TRUE); + if (!empty($tags)) { $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE, array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2') diff --git a/CRM/Core/BAO/Tag.php b/CRM/Core/BAO/Tag.php index 0983b663eb..43e586a749 100644 --- a/CRM/Core/BAO/Tag.php +++ b/CRM/Core/BAO/Tag.php @@ -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 = '  ' + $separator = '  ', + $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; } diff --git a/js/Common.js b/js/Common.js index 9586f9c588..3f679f23a7 100644 --- a/js/Common.js +++ b/js/Common.js @@ -309,6 +309,12 @@ CRM.strings = CRM.strings || {}; $(this).nextUntil('option[value^=crm_optgroup]').wrapAll(''); $(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; -- 2.25.1