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