Skip to content

Commit

Permalink
Core/dev#692 : Support search arguments to Mailing Search form
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Oct 29, 2019
1 parent 6774f03 commit d5f95c8
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 63 deletions.
12 changes: 7 additions & 5 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6294,10 +6294,6 @@ public static function processSpecialFormValue(&$formValues, $specialFields, $ch
$value = CRM_Utils_Array::value($element, $formValues);
if ($value) {
if (is_array($value)) {
if (in_array($element, array_keys($changeNames))) {
unset($formValues[$element]);
$element = $changeNames[$element];
}
$formValues[$element] = ['IN' => $value];
}
elseif (in_array($value, ['IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'])) {
Expand All @@ -6307,8 +6303,14 @@ public static function processSpecialFormValue(&$formValues, $specialFields, $ch
// if wildcard is already present return searchString as it is OR append and/or prepend with wildcard
$isWilcard = strstr($value, '%') ? FALSE : CRM_Core_Config::singleton()->includeWildCardInName;
$formValues[$element] = ['LIKE' => self::getWildCardedValue($isWilcard, 'LIKE', $value)];

}
}
if (in_array($element, array_keys($changeNames))) {
$newName = $changeNames[$element];
$formValues[$newName] = $formValues[$element];
unset($formValues[$element]);
}
}
}

Expand Down Expand Up @@ -6651,7 +6653,7 @@ protected function addBasicCancelStatsToSummary(&$summary, $where, $from) {
FROM (
SELECT civicrm_contribution.total_amount, civicrm_contribution.currency
$from
$where AND civicrm_contribution.cancel_date IS NOT NULL
$where AND civicrm_contribution.cancel_date IS NOT NULL
GROUP BY civicrm_contribution.id
) as conts
GROUP BY currency";
Expand Down
4 changes: 4 additions & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,10 @@ public function addField($name, $props = [], $required = FALSE, $legacyDate = TR
$props['size'] = isset($props['size']) ? $props['size'] : 60;
return $this->add('password', $name, $label, $props, $required);

case 'YesNo':
$allowClear = (bool) (!empty($props['allowClear']));
return $this->addYesNo($name, $label, $allowClear, $required, $props);

// Check datatypes of fields
// case 'Int':
//case 'Float':
Expand Down
48 changes: 33 additions & 15 deletions CRM/Mailing/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,16 @@ public static function whereClauseSingle(&$values, &$query) {

case 'mailing_name':
$value = addslashes($value);
if ($wildcard) {
$value = "%$value%";
$op = 'LIKE';
}

$query->_where[$grouping][] = "civicrm_mailing.name $op '$value'";
$query->_qill[$grouping][] = "Mailing Namename $op \"$value\"";
$name = 'name';
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'String');
list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op);

$query->_qill[$grouping][] = ts('%1 %2 %3', [
1 => ts('Mailing Name'),
2 => $op,
3 => $value,
]);
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
return;
Expand Down Expand Up @@ -402,6 +405,20 @@ public static function whereClauseSingle(&$values, &$query) {
}
}

/**
* Get the metadata for fields to be included on the mailing search form.
*
* @todo ideally this would be a trait included on the mailing search & advanced search
* rather than a static function.
*/
public static function getSearchFieldMetadata() {
$fields = ['name', 'mailing_job_start_date', 'status', 'is_archived'];
$metadata = civicrm_api3('Mailing', 'getfields', [])['values'];

$metadata = array_merge($metadata, civicrm_api3('MailingJob', 'getfields', [])['values']);
return array_intersect_key($metadata, array_flip($fields));
}

/**
* Add all the elements shared between Mailing search and advnaced search.
*
Expand All @@ -415,21 +432,22 @@ public static function buildSearchForm(&$form) {

// mailing selectors
$mailings = CRM_Mailing_BAO_Mailing::getMailingsList();

if (!empty($mailings)) {
$form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE,
['id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2']
);
}

$mailingJobStatuses = [
'' => ts('- select -'),
'Complete' => 'Complete',
'Scheduled' => 'Scheduled',
'Running' => 'Running',
'Canceled' => 'Canceled',
];
$form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE);
$metadata = self::getSearchFieldMetadata();
if (!empty($metadata['name']) && $parent->_sms) {
$metadata['name']['title'] = ts('SMS Name');
}
$form->addSearchFieldMetadata(['Mailing' => $metadata]);
$form->addFormFieldsFromMetadata();

CRM_Core_Form_Date::buildDateRange($form, 'mailing_date', 1, '_low', '_high', ts('From'), FALSE);
$form->addElement('hidden', 'mailing_date_range_error');
$form->addFormRule(['CRM_Mailing_BAO_Query', 'formRule'], $form);

$mailingBounceTypes = CRM_Core_PseudoConstant::get(
'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Mailing/DAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public static function &fields() {
'bao' => 'CRM_Mailing_BAO_Mailing',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
'type' => 'YesNo',
],
],
'visibility' => [
Expand Down
74 changes: 46 additions & 28 deletions CRM/Mailing/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,34 @@ public function getDefaultEntity() {

public function preProcess() {
parent::preProcess();
$this->loadStandardSearchOptionsFromUrl();
if ($this->_force) {
$this->_formValues = $this->getEntityDefaults($this->getDefaultEntity());
$this->postProcess();
}
}

/**
* @return string
*/
public function getDefaultEntity() {
return 'Mailing';
}

public function buildQuickForm() {
$parent = $this->controller->getParent();
$nameTextLabel = ($parent->_sms) ? ts('SMS Name') : ts('Mailing Name');

$this->add('text', 'mailing_name', $nameTextLabel,
CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'title')
);

$dateFieldLabel = ($parent->_sms) ? ts('SMS Date') : ts('Mailing Date');
$this->addDatePickerRange('mailing', $dateFieldLabel);

$this->add('text', 'sort_name', ts('Created or Sent by'),
CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
);
$this->addSortNameField();

CRM_Mailing_BAO_Query::buildSearchForm($this);

CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this);

// CRM-15434 - Fix mailing search by status in non-English languages
$statusVals = CRM_Core_SelectValues::getMailingJobStatus();
foreach ($statusVals as $statusId => $statusName) {
$this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName);
}
$this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled'));
$this->addYesNo('is_archived', ts('Mailing is Archived'), TRUE);

// Search by language, if multi-lingual
$enabledLanguages = CRM_Core_I18n::languages(TRUE);
Expand Down Expand Up @@ -98,28 +100,37 @@ public function setDefaultValues() {
$defaults = $statusVals = [];
$parent = $this->controller->getParent();

if ($parent->get('unscheduled')) {
$defaults['status_unscheduled'] = 1;
}
if ($parent->get('scheduled')) {
$statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
$defaults['is_archived'] = 0;
}
if ($parent->get('archived')) {
$defaults['is_archived'] = 1;
}
foreach ($statusVals as $status) {
$defaults['mailing_status'][$status] = 1;
if ($this->_force) {
$this->_formValues = $defaults = $this->getEntityDefaults($this->getDefaultEntity());
}
else {
if ($parent->get('unscheduled')) {
$defaults['status_unscheduled'] = 1;
}
if ($parent->get('scheduled')) {
$statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
$defaults['is_archived'] = 0;
}
if ($parent->get('archived')) {
$defaults['is_archived'] = 1;
}
$defaults['status'] = $statusVals;

if ($parent->_sms) {
$defaults['sms'] = 1;
if ($parent->_sms) {
$defaults['sms'] = 1;
}
}

return $defaults;
}

public function postProcess() {
$params = $this->controller->exportValues($this->_name);
if (!empty($this->_force)) {
$params = $this->getEntityDefaults($this->getDefaultEntity());
}
else {
$params = $this->controller->exportValues($this->_name);
}

if (!empty($params['mailing_relative'])) {
list($params['mailing_low'], $params['mailing_high']) = CRM_Utils_Date::getFromTo($params['mailing_relative'], $params['mailing_low'], $params['mailing_high']);
Expand All @@ -129,6 +140,13 @@ public function postProcess() {
$params['mailing_high'] .= ' ' . '23:59:59';
}

$specialParams = ['name', 'status'];
$changeNames = [
'name' => 'mailing_name',
'status' => 'mailing_job_status',
];
CRM_Contact_BAO_Query::processSpecialFormValue($params, $specialParams, $changeNames);

$parent = $this->controller->getParent();
if (!empty($params)) {
$fields = [
Expand Down
7 changes: 6 additions & 1 deletion CRM/Mailing/Selector/Browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,12 @@ public function whereClause(&$params, $sortBy = TRUE) {
$title = $this->_parent->get('mailing_name');
if ($title) {
$clauses[] = 'name LIKE %1';
if (strpos($title, '%') !== FALSE) {
if (is_array($title) && in_array(key($title), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
$op = key($title);
$value = $title[$op];
$params[1] = [$value, 'String', FALSE];
}
elseif (strpos($title, '%') !== FALSE) {
$params[1] = [$title, 'String', FALSE];
}
else {
Expand Down
17 changes: 4 additions & 13 deletions templates/CRM/Mailing/Form/Search.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<div class="crm-block crm-form-block crm-search-form-block">
<table class="form-layout">
<tr>
<td>{$form.mailing_name.label} {help id="id-mailing_name"}<br />
{$form.mailing_name.html|crmAddClass:big}
<td>{$form.name.label} {help id="id-mailing_name"}<br />
{$form.name.html|crmAddClass:big}
</td>
</tr>
<tr>
Expand All @@ -42,18 +42,9 @@
{$form.is_archived.html}
</div>
</td>
{if $form.mailing_status}
{if $form.status}
<td width="100%"><label>{if $sms eq 1}{ts}SMS Status{/ts}{else}{ts}Mailing Status{/ts}{/if}</label><br />
<div class="listing-box" style="height: auto">
{foreach from=$form.mailing_status item="mailing_status_val"}
<div class="{cycle values="odd-row,even-row"}">
{$mailing_status_val.html}
</div>
{/foreach}
<div class="{cycle values="odd-row,even-row"}">
{$form.status_unscheduled.html}
</div>
</div><br />
{$form.status.html|crmAddClass:big}
</td>
{/if}
</tr>
Expand Down

0 comments on commit d5f95c8

Please sign in to comment.