Merge pull request #19087 from civicrm/5.32
[civicrm-core.git] / CRM / Mailing / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 16 */
4b4a10d3 17class CRM_Mailing_Form_Search extends CRM_Core_Form_Search {
18
19 /**
20 * Get the default entity being queried.
21 *
22 * @return string
23 */
24 public function getDefaultEntity() {
25 return 'Mailing';
26 }
6a488035
TO
27
28 public function preProcess() {
29 parent::preProcess();
30 }
31
f8a9e5cc 32 /**
33 * @throws \CiviCRM_API3_Exception
34 */
6a488035
TO
35 public function buildQuickForm() {
36 $parent = $this->controller->getParent();
37 $nameTextLabel = ($parent->_sms) ? ts('SMS Name') : ts('Mailing Name');
38
39 $this->add('text', 'mailing_name', $nameTextLabel,
40 CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'title')
41 );
42
6de4fe34
SL
43 $dateFieldLabel = ($parent->_sms) ? ts('SMS Date') : ts('Mailing Date');
44 $this->addDatePickerRange('mailing', $dateFieldLabel);
6a488035
TO
45
46 $this->add('text', 'sort_name', ts('Created or Sent by'),
47 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
48 );
49
f8a9e5cc 50 CRM_Mailing_BAO_Query::buildSearchForm($this);
51
6a488035
TO
52 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this);
53
d4ad3b22 54 // CRM-15434 - Fix mailing search by status in non-English languages
7236e25f 55 $statusVals = CRM_Core_SelectValues::getMailingJobStatus();
d4ad3b22 56 foreach ($statusVals as $statusId => $statusName) {
57 $this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName);
6a488035 58 }
7ccbe556 59 $this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled'));
f6df2c32 60
c00b95ef
ML
61 // Search by language, if multi-lingual
62 $enabledLanguages = CRM_Core_I18n::languages(TRUE);
63
64 if (count($enabledLanguages) > 1) {
be2fb01f 65 $this->addElement('select', 'language', ts('Language'), ['' => ts('- all languages -')] + $enabledLanguages, ['class' => 'crm-select2']);
c00b95ef
ML
66 }
67
6a488035
TO
68 if ($parent->_sms) {
69 $this->addElement('hidden', 'sms', $parent->_sms);
70 }
f5d7a1ab 71 $this->add('hidden', 'hidden_find_mailings', 1);
6a488035 72
be2fb01f
CW
73 $this->addButtons([
74 [
353ffa53
TO
75 'type' => 'refresh',
76 'name' => ts('Search'),
77 'isDefault' => TRUE,
be2fb01f
CW
78 ],
79 ]);
6a488035
TO
80 }
81
e0ef6999
EM
82 /**
83 * @return array
f8a9e5cc 84 * @throws \CRM_Core_Exception
e0ef6999 85 */
00be9182 86 public function setDefaultValues() {
be2fb01f 87 $defaults = $statusVals = [];
f8a9e5cc 88 $entityDefaults = parent::setDefaultValues();
cbb4cb7b
PJ
89 $parent = $this->controller->getParent();
90
f6df2c32
DS
91 if ($parent->get('unscheduled')) {
92 $defaults['status_unscheduled'] = 1;
93 }
f6df2c32 94 if ($parent->get('scheduled')) {
67d4ed51 95 $statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
f6df2c32
DS
96 $defaults['is_archived'] = 0;
97 }
f6df2c32
DS
98 if ($parent->get('archived')) {
99 $defaults['is_archived'] = 1;
6a488035 100 }
6ff6d85a
DS
101 foreach ($statusVals as $status) {
102 $defaults['mailing_status'][$status] = 1;
103 }
6a488035 104
6a488035
TO
105 if ($parent->_sms) {
106 $defaults['sms'] = 1;
107 }
f8a9e5cc 108 return array_merge($defaults, $entityDefaults);
6a488035
TO
109 }
110
f8a9e5cc 111 /**
112 * @throws \CRM_Core_Exception
113 */
00be9182 114 public function postProcess() {
f8a9e5cc 115 $this->setFormValues();
116 $params = $this->_formValues;
6a488035 117
6de4fe34
SL
118 if (!empty($params['mailing_relative'])) {
119 list($params['mailing_low'], $params['mailing_high']) = CRM_Utils_Date::getFromTo($params['mailing_relative'], $params['mailing_low'], $params['mailing_high']);
120 unset($params['mailing_relative']);
121 }
122 elseif (!empty($params['mailing_high'])) {
123 $params['mailing_high'] .= ' ' . '23:59:59';
124 }
6a488035
TO
125
126 $parent = $this->controller->getParent();
127 if (!empty($params)) {
be2fb01f 128 $fields = [
353ffa53 129 'mailing_name',
6de4fe34
SL
130 'mailing_low',
131 'mailing_high',
353ffa53
TO
132 'sort_name',
133 'campaign_id',
134 'mailing_status',
135 'sms',
136 'status_unscheduled',
137 'is_archived',
c00b95ef 138 'language',
795492f3 139 'hidden_find_mailings',
be2fb01f 140 ];
6a488035
TO
141 foreach ($fields as $field) {
142 if (isset($params[$field]) &&
143 !CRM_Utils_System::isNull($params[$field])
144 ) {
6de4fe34 145 $parent->set($field, $params[$field]);
6a488035
TO
146 }
147 else {
148 $parent->set($field, NULL);
149 }
150 }
151 }
152 }
96025800 153
f8a9e5cc 154 /**
155 * Handle force=1 in the url.
156 *
157 * Search field metadata is normally added in buildForm but we are bypassing that in this flow
158 * (I've always found the flow kinda confusing & perhaps that is the problem but this mitigates)
159 *
160 * @throws \CiviCRM_API3_Exception
161 */
162 protected function handleForcedSearch() {
163 $this->setSearchMetadata();
164 $this->addContactSearchFields();
165 }
166
167 /**
168 * Set the metadata for the form.
169 *
170 * @throws \CiviCRM_API3_Exception
171 */
172 protected function setSearchMetadata() {
173 $this->addSearchFieldMetadata(['Mailing' => CRM_Mailing_BAO_Query::getSearchFieldMetadata()]);
174 }
175
6a488035 176}