Merge pull request #12285 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Mailing / Form / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 CRM_Core_Form_Date::buildDateRange($this, 'mailing', 1, '_from', '_to', ts('From'), FALSE);
48
49 $this->add('text', 'sort_name', ts('Created or Sent by'),
50 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')
51 );
52
53 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($this);
54
55 // CRM-15434 - Fix mailing search by status in non-English languages
56 $statusVals = CRM_Core_SelectValues::getMailingJobStatus();
57 foreach ($statusVals as $statusId => $statusName) {
58 $this->addElement('checkbox', "mailing_status[$statusId]", NULL, $statusName);
59 }
60 $this->addElement('checkbox', 'status_unscheduled', NULL, ts('Draft / Unscheduled'));
61 $this->addYesNo('is_archived', ts('Mailing is Archived'), TRUE);
62
63 // Search by language, if multi-lingual
64 $enabledLanguages = CRM_Core_I18n::languages(TRUE);
65
66 if (count($enabledLanguages) > 1) {
67 $this->addElement('select', 'language', ts('Language'), array('' => ts('- all languages -')) + $enabledLanguages, array('class' => 'crm-select2'));
68 }
69
70 if ($parent->_sms) {
71 $this->addElement('hidden', 'sms', $parent->_sms);
72 }
73 $this->add('hidden', 'hidden_find_mailings', 1);
74
75 $this->addButtons(array(
76 array(
77 'type' => 'refresh',
78 'name' => ts('Search'),
79 'isDefault' => TRUE,
80 ),
81 ));
82 }
83
84 /**
85 * @return array
86 */
87 public function setDefaultValues() {
88 $defaults = $statusVals = array();
89 $parent = $this->controller->getParent();
90
91 if ($parent->get('unscheduled')) {
92 $defaults['status_unscheduled'] = 1;
93 }
94 if ($parent->get('scheduled')) {
95 $statusVals = array_keys(CRM_Core_SelectValues::getMailingJobStatus());
96 $defaults['is_archived'] = 0;
97 }
98 if ($parent->get('archived')) {
99 $defaults['is_archived'] = 1;
100 }
101 foreach ($statusVals as $status) {
102 $defaults['mailing_status'][$status] = 1;
103 }
104
105 if ($parent->_sms) {
106 $defaults['sms'] = 1;
107 }
108 return $defaults;
109 }
110
111 public function postProcess() {
112 $params = $this->controller->exportValues($this->_name);
113
114 CRM_Contact_BAO_Query::fixDateValues($params["mailing_relative"], $params['mailing_from'], $params['mailing_to']);
115
116 $parent = $this->controller->getParent();
117 if (!empty($params)) {
118 $fields = array(
119 'mailing_name',
120 'mailing_from',
121 'mailing_to',
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 if (in_array($field, array(
136 'mailing_from',
137 'mailing_to',
138 )) && !$params["mailing_relative"]
139 ) {
140 $time = ($field == 'mailing_to') ? '235959' : NULL;
141 $parent->set($field, CRM_Utils_Date::processDate($params[$field], $time));
142 }
143 else {
144 $parent->set($field, $params[$field]);
145 }
146 }
147 else {
148 $parent->set($field, NULL);
149 }
150 }
151 }
152 }
153
154 }