Merge pull request #15745 from seamuslee001/master
[civicrm-core.git] / CRM / Mailing / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Mailing_Form_Search extends CRM_Core_Form_Search {
34
35 /**
36 * Get the default entity being queried.
37 *
38 * @return string
39 */
40 public function getDefaultEntity() {
41 return 'Mailing';
42 }
43
44 public function preProcess() {
45 parent::preProcess();
46 }
47
48 public function buildQuickForm() {
49 $parent = $this->controller->getParent();
50 $nameTextLabel = ($parent->_sms) ? ts('SMS Name') : ts('Mailing Name');
51
52 $this->add('text', 'mailing_name', $nameTextLabel,
53 CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'title')
54 );
55
56 $dateFieldLabel = ($parent->_sms) ? ts('SMS Date') : ts('Mailing Date');
57 $this->addDatePickerRange('mailing', $dateFieldLabel);
58
59 $this->add('text', 'sort_name', ts('Created or Sent by'),
60 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
61 );
62
63 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this);
64
65 // CRM-15434 - Fix mailing search by status in non-English languages
66 $statusVals = CRM_Core_SelectValues::getMailingJobStatus();
67 foreach ($statusVals as $statusId => $statusName) {
68 $this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName);
69 }
70 $this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled'));
71 $this->addYesNo('is_archived', ts('Mailing is Archived'), TRUE);
72
73 // Search by language, if multi-lingual
74 $enabledLanguages = CRM_Core_I18n::languages(TRUE);
75
76 if (count($enabledLanguages) > 1) {
77 $this->addElement('select', 'language', ts('Language'), ['' => ts('- all languages -')] + $enabledLanguages, ['class' => 'crm-select2']);
78 }
79
80 if ($parent->_sms) {
81 $this->addElement('hidden', 'sms', $parent->_sms);
82 }
83 $this->add('hidden', 'hidden_find_mailings', 1);
84
85 $this->addButtons([
86 [
87 'type' => 'refresh',
88 'name' => ts('Search'),
89 'isDefault' => TRUE,
90 ],
91 ]);
92 }
93
94 /**
95 * @return array
96 */
97 public function setDefaultValues() {
98 $defaults = $statusVals = [];
99 $parent = $this->controller->getParent();
100
101 if ($parent->get('unscheduled')) {
102 $defaults['status_unscheduled'] = 1;
103 }
104 if ($parent->get('scheduled')) {
105 $statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
106 $defaults['is_archived'] = 0;
107 }
108 if ($parent->get('archived')) {
109 $defaults['is_archived'] = 1;
110 }
111 foreach ($statusVals as $status) {
112 $defaults['mailing_status'][$status] = 1;
113 }
114
115 if ($parent->_sms) {
116 $defaults['sms'] = 1;
117 }
118 return $defaults;
119 }
120
121 public function postProcess() {
122 $params = $this->controller->exportValues($this->_name);
123
124 if (!empty($params['mailing_relative'])) {
125 list($params['mailing_low'], $params['mailing_high']) = CRM_Utils_Date::getFromTo($params['mailing_relative'], $params['mailing_low'], $params['mailing_high']);
126 unset($params['mailing_relative']);
127 }
128 elseif (!empty($params['mailing_high'])) {
129 $params['mailing_high'] .= ' ' . '23:59:59';
130 }
131
132 $parent = $this->controller->getParent();
133 if (!empty($params)) {
134 $fields = [
135 'mailing_name',
136 'mailing_low',
137 'mailing_high',
138 'sort_name',
139 'campaign_id',
140 'mailing_status',
141 'sms',
142 'status_unscheduled',
143 'is_archived',
144 'language',
145 'hidden_find_mailings',
146 ];
147 foreach ($fields as $field) {
148 if (isset($params[$field]) &&
149 !CRM_Utils_System::isNull($params[$field])
150 ) {
151 $parent->set($field, $params[$field]);
152 }
153 else {
154 $parent->set($field, NULL);
155 }
156 }
157 }
158 }
159
160 }