Skip to content

Commit

Permalink
dev/core#101 Implement field spec pattern for advanced search form.
Browse files Browse the repository at this point in the history
By implementing the field spec pattern as detailed in dev/core#115 extension developers have more control over
 what is displayed in this template without needing to resort to
 overriding it
  • Loading branch information
mickadoo authored and reneolivo committed Jun 8, 2018
1 parent 8df04fb commit 2a4718f
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 168 deletions.
70 changes: 67 additions & 3 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class CRM_Contact_Form_Search_Criteria {
* @param CRM_Core_Form $form
*/
public static function basic(&$form) {

self::setBasicSearchFields($form);
$form->addElement('hidden', 'hidden_basic', 1);

if ($form->_searchOptions['contactType']) {
Expand Down Expand Up @@ -69,7 +71,7 @@ public static function basic(&$form) {
$contactTags = CRM_Core_BAO_Tag::getTags();

if ($contactTags) {
$form->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
$form->add('select', 'contact_tags', ts('Select Tag(s)'), $contactTags, FALSE,
array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;')
);
}
Expand Down Expand Up @@ -99,10 +101,10 @@ public static function basic(&$form) {
}

// add text box for last name, first name, street name, city
$form->addElement('text', 'sort_name', ts('Find...'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
$form->addElement('text', 'sort_name', ts('Complete OR Partial Name'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));

// add text box for last name, first name, street name, city
$form->add('text', 'email', ts('Contact Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
$form->add('text', 'email', ts('Complete OR Partial Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));

//added contact source
$form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'contact_source'));
Expand Down Expand Up @@ -248,6 +250,68 @@ public static function basic(&$form) {
$form->add('select', 'phone_phone_type_id', ts('Phone Type'), array('' => ts('- any -')) + $phoneType, FALSE, array('class' => 'crm-select2'));
}

/**
* Defines the fields that can be displayed for the basic search section.
*
* @param CRM_Core_Form $form
*/
protected static function setBasicSearchFields($form) {
$form->assign('basicSearchFields', [
'sort_name' => ['name' => 'sort_name'],
'email' => ['name' => 'email'],
'contact_type' => ['name' => 'contact_type'],
'group' => [
'name' => 'group',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/group.tpl',
],
'contact_tags' => [
'name' => 'contact_tags',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/contact_tags.tpl',
],
'tag_search' => [
'name' => 'tag_search',
'help' => ['id' => 'id-all-tags']
],
'all_tag_types' => [
'name' => '',
'help' => ['id' => 'id-all-tags'],
'class' => 'search-field__span-3',
],
'phone_numeric' => [
'name' => 'phone_numeric',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/phone_numeric.tpl',
],
'phone_location_type_id' => ['name' => 'phone_location_type_id'],
'phone_phone_type_id' => ['name' => 'phone_phone_type_id'],
'privacy_toggle' => [
'name' => 'privacy_toggle',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/privacy_toggle.tpl',
],
'preferred_communication_method' => [
'name' => 'preferred_communication_method',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/preferred_communication_method.tpl',
],
'contact_source' => [
'name' => 'contact_source',
'help' => ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact'],
],
'job_title' => ['name' => 'job_title'],
'preferred_language' => ['name' => 'preferred_language'],
'contact_id' => [
'name' => 'contact_id',
'help' => ['id' => 'id-contact-id', 'file' => 'CRM/Contact/Form/Contact'],
],
'external_identifier' => [
'name' => 'external_identifier',
'help' => ['id' => 'id-external-id', 'file' => 'CRM/Contact/Form/Contact'],
],
'uf_user' => [
'name' => 'uf_user',
'template' => 'CRM/Contact/Form/Search/Criteria/Fields/uf_user.tpl'
],
]);
}


/**
* @param CRM_Core_Form $form
Expand Down
17 changes: 17 additions & 0 deletions css/searchForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@
color: #41477E;
font-weight: bold;
}

.advanced-search-fields {
display: grid;
grid-template-columns: [col] repeat(3, calc(100% / 3 - 10px));
width: 100%;
}

.advanced-search-fields .search-field {
padding: 5px;
}

.advanced-search-fields .search-field__span-2 {
grid-column: col / span 2;
}
.advanced-search-fields .search-field__span-3 {
grid-column: col / span 3;
}
184 changes: 19 additions & 165 deletions templates/CRM/Contact/Form/Search/Criteria/Basic.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,170 +23,24 @@
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*}
<table class="form-layout">
<tr>
<td><label>{ts}Complete OR Partial Name{/ts}</label><br />
{$form.sort_name.html}
</td>
<td>
<label>{ts}Complete OR Partial Email{/ts}</label><br />
{$form.email.html}
</td>
{if $form.contact_type}
<td><label>{ts}Contact Type(s){/ts}</label><br />
{$form.contact_type.html}
</td>
{else}
<td>&nbsp;</td>
{/if}
</tr>
<tr>
{if $form.group}
<td>
<div id='groupselect'><label>{ts}Group(s){/ts} <span class="description">(<a href="#" id='searchbygrouptype'>{ts}search by group type{/ts}</a>)</span></label>
<br />
{$form.group.html}
</div>
<div id='grouptypeselect'>
<label>{ts}Group Type(s){/ts} <span class="description"> (<a href="#" id='searchbygroup'>{ts}search by group{/ts}</a>)</span></label>
<br />
{$form.group_type.html}
{literal}
<script type="text/javascript">
CRM.$(function($) {
function showGroupSearch() {
$('#grouptypeselect').hide();
$('#groupselect').show();
$('#group_type').select2('val', '');
return false;
}
function showGroupTypeSearch() {
$('#groupselect').hide();
$('#grouptypeselect').show();
$('#group').select2('val', '');
return false;
}
$('#searchbygrouptype').click(showGroupTypeSearch);
$('#searchbygroup').click(showGroupSearch);

if ($('#group_type').val() ) {
showGroupTypeSearch();
}
else {
showGroupSearch();
}

});
</script>
{/literal}
</div>
</td>
{else}
<td>&nbsp;</td>
{/if}
{if $form.contact_tags}
<td><label>{ts}Select Tag(s){/ts}</label>
{$form.contact_tags.html}
</td>
{else}
<td>&nbsp;</td>
{/if}
{if $isTagset}
<td colspan="2">{include file="CRM/common/Tagset.tpl"}</td>
{/if}
<td>{$form.tag_search.label} {help id="id-all-tags"}<br />{$form.tag_search.html}</td>
{if ! $isTagset}
<td colspan="2">&nbsp;</td>
{/if}
<td>&nbsp;</td>
</tr>
{if $form.all_tag_types}
<tr>
<td colspan="5">
{$form.all_tag_types.html} {$form.all_tag_types.label} {help id="id-all-tag-types"}
</td>
</tr>
{/if}
<tr>
<td>
<div>
{$form.phone_numeric.label}<br />{$form.phone_numeric.html}
</div>
<div class="description font-italic">
{ts}Punctuation and spaces are ignored.{/ts}
</div>
</td>
<td>{$form.phone_location_type_id.label}<br />{$form.phone_location_type_id.html}</td>
<td>{$form.phone_phone_type_id.label}<br />{$form.phone_phone_type_id.html}</td>
</tr>
<tr>
<td colspan="2">
<table class="form-layout-compressed">
<tr>
<td colspan="2">
{$form.privacy_toggle.html} {help id="id-privacy"}
</td>
</tr>
<tr>
<td>
{$form.privacy_options.html}
</td>
<td style="vertical-align:middle">
<div id="privacy-operator-wrapper">{$form.privacy_operator.html} {help id="privacy-operator"}</div>
</td>
</tr>
</table>
{literal}
<script type="text/javascript">
cj("select#privacy_options").change(function() {
if (cj(this).val() && cj(this).val().length > 1) {
cj('#privacy-operator-wrapper').show();
} else {
cj('#privacy-operator-wrapper').hide();
}
}).change();
</script>
{/literal}
</td>
<td colspan="3">
{$form.preferred_communication_method.label}<br />
{$form.preferred_communication_method.html}<br />
<div class="spacer"></div>
{$form.email_on_hold.html} {$form.email_on_hold.label}
</td>
</tr>
<tr>
<td>
{$form.contact_source.label} {help id="id-source" file="CRM/Contact/Form/Contact"}<br />
{$form.contact_source.html}
</td>
<td>
{$form.job_title.label}<br />
{$form.job_title.html}
</td>
<td colspan="3">
{$form.preferred_language.label}<br />
{$form.preferred_language.html}
</td>
</tr>
<tr>
<td>
{$form.contact_id.label} {help id="id-internal-id" file="CRM/Contact/Form/Contact"}<br />
{$form.contact_id.html}
</td>
<td>
{$form.external_identifier.label} {help id="id-external-id" file="CRM/Contact/Form/Contact"}<br />
{$form.external_identifier.html}
</td>
<td>
{if $form.uf_user}
{$form.uf_user.label} {$form.uf_user.html}
<div class="description font-italic">
{ts 1=$config->userFramework}Does the contact have a %1 Account?{/ts}
</div>
<div class="advanced-search-fields basic-fields form-layout">
{foreach from=$basicSearchFields item=fieldSpec}
{assign var=field value=$form[$fieldSpec.name]}
{if $field}
{if $fieldSpec.template}
{include file=$fieldSpec.template}
{else}
&nbsp;
<div class="search-field {$fieldSpec.class|escape}">
{$field.label}
{if $fieldSpec.help}
{assign var=help value=$fieldSpec.help}
{capture assign=helpFile}{if $fieldSpec.help}{$fieldSpec.help}{else}''{/if}{/capture}
{help id=$help.id file=$help.file}
{/if}
<br />
{$field.html}
</div>
{/if}
</td>
</tr>
</table>
{/if}
{/foreach}
</div>
10 changes: 10 additions & 0 deletions templates/CRM/Contact/Form/Search/Criteria/Fields/contact_tags.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="search-field">
{$form.contact_tags.label}<br />
{$form.contact_tags.html}
</div>

{if $isTagset}
<div class="search-field search-field__span-2">
{include file="CRM/common/Tagset.tpl"}
</div>
{/if}
51 changes: 51 additions & 0 deletions templates/CRM/Contact/Form/Search/Criteria/Fields/group.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<div class="search-field">
<div id='groupselect'>
<label>{ts}Group(s){/ts}
<span class="description">
(<a href="#" id='searchbygrouptype'>{ts}search by group type{/ts}</a>)
</span>
</label>
<br/>
{$form.group.html}
</div>
<div id='grouptypeselect'>
<label>
{ts}Group Type(s){/ts}
<span class="description">
(<a href="#" id='searchbygroup'>{ts}search by group{/ts}</a>)
</span>
</label>
<br/>
{$form.group_type.html}
{literal}
<script type="text/javascript">
CRM.$(function ($) {
function showGroupSearch() {
$('#grouptypeselect').hide();
$('#groupselect').show();
$('#group_type').select2('val', '');
return false;
}

function showGroupTypeSearch() {
$('#groupselect').hide();
$('#grouptypeselect').show();
$('#group').select2('val', '');
return false;
}

$('#searchbygrouptype').click(showGroupTypeSearch);
$('#searchbygroup').click(showGroupSearch);

if ($('#group_type').val()) {
showGroupTypeSearch();
}
else {
showGroupSearch();
}

});
</script>
{/literal}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="search-field">
<div>
{$form.phone_numeric.label}<br/>
{$form.phone_numeric.html}
</div>
<div class="description font-italic">
{ts}Punctuation and spaces are ignored.{/ts}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="search-field">
{$form.preferred_communication_method.label}
<br/>
{$form.preferred_communication_method.html}
<br/>

{if $form.email_on_hold}
<div class="spacer"></div>
{$form.email_on_hold.html}
{$form.email_on_hold.label}
{/if}
</div>
Loading

0 comments on commit 2a4718f

Please sign in to comment.