Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Mailing / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class 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 }
27
28 public function preProcess() {
29 parent::preProcess();
30 }
31
32 public function buildQuickForm() {
33 $parent = $this->controller->getParent();
34 $nameTextLabel = ($parent->_sms) ? ts('SMS Name') : ts('Mailing Name');
35
36 $this->add('text', 'mailing_name', $nameTextLabel,
37 CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'title')
38 );
39
40 $dateFieldLabel = ($parent->_sms) ? ts('SMS Date') : ts('Mailing Date');
41 $this->addDatePickerRange('mailing', $dateFieldLabel);
42
43 $this->add('text', 'sort_name', ts('Created or Sent by'),
44 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
45 );
46
47 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this);
48
49 // CRM-15434 - Fix mailing search by status in non-English languages
50 $statusVals = CRM_Core_SelectValues::getMailingJobStatus();
51 foreach ($statusVals as $statusId => $statusName) {
52 $this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName);
53 }
54 $this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled'));
55 $this->addYesNo('is_archived', ts('Mailing is Archived'), TRUE);
56
57 // Search by language, if multi-lingual
58 $enabledLanguages = CRM_Core_I18n::languages(TRUE);
59
60 if (count($enabledLanguages) > 1) {
61 $this->addElement('select', 'language', ts('Language'), ['' => ts('- all languages -')] + $enabledLanguages, ['class' => 'crm-select2']);
62 }
63
64 if ($parent->_sms) {
65 $this->addElement('hidden', 'sms', $parent->_sms);
66 }
67 $this->add('hidden', 'hidden_find_mailings', 1);
68
69 $this->addButtons([
70 [
71 'type' => 'refresh',
72 'name' => ts('Search'),
73 'isDefault' => TRUE,
74 ],
75 ]);
76 }
77
78 /**
79 * @return array
80 */
81 public function setDefaultValues() {
82 $defaults = $statusVals = [];
83 $parent = $this->controller->getParent();
84
85 if ($parent->get('unscheduled')) {
86 $defaults['status_unscheduled'] = 1;
87 }
88 if ($parent->get('scheduled')) {
89 $statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
90 $defaults['is_archived'] = 0;
91 }
92 if ($parent->get('archived')) {
93 $defaults['is_archived'] = 1;
94 }
95 foreach ($statusVals as $status) {
96 $defaults['mailing_status'][$status] = 1;
97 }
98
99 if ($parent->_sms) {
100 $defaults['sms'] = 1;
101 }
102 return $defaults;
103 }
104
105 public function postProcess() {
106 $params = $this->controller->exportValues($this->_name);
107
108 if (!empty($params['mailing_relative'])) {
109 list($params['mailing_low'], $params['mailing_high']) = CRM_Utils_Date::getFromTo($params['mailing_relative'], $params['mailing_low'], $params['mailing_high']);
110 unset($params['mailing_relative']);
111 }
112 elseif (!empty($params['mailing_high'])) {
113 $params['mailing_high'] .= ' ' . '23:59:59';
114 }
115
116 $parent = $this->controller->getParent();
117 if (!empty($params)) {
118 $fields = [
119 'mailing_name',
120 'mailing_low',
121 'mailing_high',
122 'sort_name',
123 'campaign_id',
124 'mailing_status',
125 'sms',
126 'status_unscheduled',
127 'is_archived',
128 'language',
129 'hidden_find_mailings',
130 ];
131 foreach ($fields as $field) {
132 if (isset($params[$field]) &&
133 !CRM_Utils_System::isNull($params[$field])
134 ) {
135 $parent->set($field, $params[$field]);
136 }
137 else {
138 $parent->set($field, NULL);
139 }
140 }
141 }
142 }
143
144 }