Skip to content

Commit

Permalink
CRM-20673: Tag and group edit form: implement Select2 for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Jun 14, 2017
1 parent 338893c commit 2e479fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 105 deletions.
1 change: 1 addition & 0 deletions CRM/Contact/Form/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ public function postProcess() {

if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
//add contact to tags
$params['tag'] = array_flip(explode(',', $params['tag']));
CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $params['contact_id']);

//save free tags
Expand Down
82 changes: 9 additions & 73 deletions CRM/Contact/Form/Edit/TagsAndGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,72 +145,17 @@ public static function buildQuickForm(
}

if ($type & self::TAG) {
$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);
// let's not load jstree if there are not children. This also fixes blank
// display at the beginning of checkboxes
$loadJsTree = CRM_Utils_Array::retrieveValueRecursive($tree, 'children');
$form->assign('loadjsTree', FALSE);
if (!empty($loadJsTree)) {
// 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);

$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');
$form->assign('allTags', CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE));
$tags = CRM_Core_BAO_Tag::getColorTags('civicrm_contact');

if ($isRequired) {
$form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
if (!empty($tags)) {
$form->add('select2', 'tag', ts('Tag(s)'), $tags, FALSE, array('class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE));
}

// 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);
}

/**
* 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}",
);

$elements[$tagID] = $form->createElement('checkbox', $tagID, '', $varValue['name'], $tagAttribute);

if (array_key_exists('children', $varValue)) {
self::climbtree($form, $varValue['children'], $elements);
}
}
return $elements;
// 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);
}

/**
Expand Down Expand Up @@ -249,17 +194,8 @@ public static function setDefaults($id, &$defaults, $type = self::ALL, $fieldNam
}

if ($type & self::TAG) {
$fName = 'tag';
if ($fieldName) {
$fName = $fieldName;
}

$contactTag = CRM_Core_BAO_EntityTag::getTag($id);
if ($contactTag) {
foreach ($contactTag as $tag) {
$defaults[$fName . '[' . $tag . ']'] = 1;
}
}
$defaults['tag'] = implode(',', CRM_Core_BAO_EntityTag::getTag($id, 'civicrm_contact'));
CRM_Core_Error::debug_var('tag', $defaults['tag']);
}
}

Expand Down
58 changes: 28 additions & 30 deletions templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,36 @@
<div class="crm-accordion-header">{$title}</div>
<div class="crm-accordion-body" id="tagGroup">
{/if}
<div class="form-layout-compressed{if $context EQ 'profile'} crm-profile-tagsandgroups{/if}">
{if !$type || $type eq 'group'}
<div class="crm-section crm-clearfix">
{if $title}
<div class="label">{$form.group.label}</div>
{/if}
{if $groupElementType eq 'select'}
<div class="content">{$form.group.html}</div>
{else}
{foreach key=key item=item from=$tagGroup.group}
<div class="group-wrapper">
{$form.group.$key.html}
{if $item.description}
<span class="description">{$item.description}</span>
<table class="form-layout-compressed{if $context EQ 'profile'} crm-profile-tagsandgroups{/if}">
<tr>
{if !$type || $type eq 'tag'}
<td>
<span class="label">{if $title}{$form.tag.label}{/if}</span><br />
{$form.tag.html}
</td>
{/if}
{if !$type || $type eq 'group'}
<td>
{if $groupElementType eq 'select'}
<span class="label">{if $title}{$form.group.label}{/if}</span>
{$form.group.html}
{else}
{foreach key=key item=item from=$tagGroup.group}
<div class="group-wrapper">
{$form.group.$key.html}
{if $item.description}
<div class="description">{$item.description}</div>
{/if}
</div>
{/foreach}
{/if}
</div>
{/foreach}
{/if}
</div>
{/if}
{if (!$type || $type eq 'tag') && $tree}
<div class="crm-section crm-clearfix">
{if $title}
<div class="label">{$form.tag.label}</div>
</td>
{/if}
</tr>
{if !$type || $type eq 'tag'}
<tr><td>{include file="CRM/common/Tagset.tpl"}</td></tr>
{/if}
<div class="content" id="tagtree">
{include file="CRM/Contact/Form/Edit/Tagtree.tpl" level=1}
</div>
</div>
{include file="CRM/common/Tagset.tpl"}
{/if}
</div>
</table>
{if $title}
</div>
</div><!-- /.crm-accordion-wrapper -->
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/common/Tagset.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
{else}
<div class="crm-section tag-section {$tagsetType}-tagset {$tagsetType}-tagset-{$tagset.parentID}-section">
<div class="crm-clearfix">
<div class="label">{$form.$elemName.$parID.label}</div>
<div class="content">{$form.$elemName.$parID.html}</div>
{$form.$elemName.$parID.label}
{$form.$elemName.$parID.html}
</div>
</div>
{/if}
Expand Down

0 comments on commit 2e479fa

Please sign in to comment.