3b341bcb2818e524aa223820f3d8b59419b03372
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / ZipCodeRange.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_Contact_Form_Search_Custom_ZipCodeRange extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
34 protected $_aclFrom = NULL;
35 protected $_aclWhere = NULL;
36
37 /**
38 * Class constructor.
39 *
40 * @param array $formValues
41 */
42 public function __construct(&$formValues) {
43 parent::__construct($formValues);
44
45 $this->_columns = [
46 // If possible, don't use aliases for the columns you select.
47 // You can prefix columns with table aliases, if needed.
48 //
49 // If you don't do this, selecting individual records from the
50 // custom search result won't work if your results are sorted on the
51 // aliased colums.
52 // (This is why we map Contact ID on contact_a.id, and not on contact_id).
53 ts('Contact ID') => 'contact_a.id',
54 ts('Name') => 'sort_name',
55 ts('Email') => 'email',
56 ts('Zip') => 'postal_code',
57 ];
58 }
59
60 /**
61 * @param CRM_Core_Form $form
62 */
63 public function buildForm(&$form) {
64 $form->add('text',
65 'postal_code_low',
66 ts('Postal Code Start'),
67 TRUE
68 );
69
70 $form->add('text',
71 'postal_code_high',
72 ts('Postal Code End'),
73 TRUE
74 );
75
76 /**
77 * You can define a custom title for the search form
78 */
79 $this->setTitle('Zip Code Range Search');
80
81 /**
82 * if you are using the standard template, this array tells the template what elements
83 * are part of the search criteria
84 */
85 $form->assign('elements', ['postal_code_low', 'postal_code_high']);
86 }
87
88 /**
89 * @return array
90 */
91 public function summary() {
92 $summary = [];
93 return $summary;
94 }
95
96 /**
97 * @param int $offset
98 * @param int $rowcount
99 * @param null $sort
100 * @param bool $returnSQL
101 *
102 * @return string
103 */
104 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
105 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
106 }
107
108 /**
109 * @param int $offset
110 * @param int $rowcount
111 * @param null $sort
112 * @param bool $includeContactIDs
113 * @param bool $justIDs
114 *
115 * @return string
116 */
117 public function all(
118 $offset = 0, $rowcount = 0, $sort = NULL,
119 $includeContactIDs = FALSE, $justIDs = FALSE
120 ) {
121 if ($justIDs) {
122 $selectClause = "contact_a.id as contact_id";
123 // Don't change sort order when $justIDs is TRUE, see CRM-14920.
124 }
125 else {
126 // We select contact_a.id twice. Once as contact_a.id,
127 // because it is used to fill the prevnext_cache. And once
128 // as contact_a.id, for the patch of CRM-16587 to work when
129 // the results are sorted on contact ID.
130 $selectClause = "
131 contact_a.id as contact_id ,
132 contact_a.id as id ,
133 contact_a.sort_name as sort_name ,
134 email.email as email ,
135 address.postal_code as postal_code
136 ";
137 }
138 return $this->sql($selectClause,
139 $offset, $rowcount, $sort,
140 $includeContactIDs, NULL
141 );
142 }
143
144 /**
145 * @return string
146 */
147 public function from() {
148 $this->buildACLClause('contact_a');
149 $from = "
150 FROM civicrm_contact contact_a
151 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
152 address.is_primary = 1 )
153 LEFT JOIN civicrm_email email ON ( email.contact_id = contact_a.id AND
154 email.is_primary = 1 ) {$this->_aclFrom}
155 ";
156 return $from;
157 }
158
159 /**
160 * @param bool $includeContactIDs
161 *
162 * @return string
163 */
164 public function where($includeContactIDs = FALSE) {
165 $params = [];
166
167 $low = CRM_Utils_Array::value('postal_code_low',
168 $this->_formValues
169 );
170 $high = CRM_Utils_Array::value('postal_code_high',
171 $this->_formValues
172 );
173 if ($low == NULL || $high == NULL) {
174 CRM_Core_Error::statusBounce(ts('Please provide start and end postal codes'),
175 CRM_Utils_System::url('civicrm/contact/search/custom',
176 "reset=1&csid={$this->_formValues['customSearchID']}",
177 FALSE, NULL, FALSE, TRUE
178 )
179 );
180 }
181
182 $where = "ROUND(address.postal_code) >= %1 AND ROUND(address.postal_code) <= %2";
183 $params = [
184 1 => [trim($low), 'Integer'],
185 2 => [trim($high), 'Integer'],
186 ];
187
188 if ($this->_aclWhere) {
189 $where .= " AND {$this->_aclWhere} ";
190 }
191 return $this->whereClause($where, $params);
192 }
193
194 /**
195 * @return string
196 */
197 public function templateFile() {
198 return 'CRM/Contact/Form/Search/Custom.tpl';
199 }
200
201 /**
202 * @param $title
203 */
204 public function setTitle($title) {
205 if ($title) {
206 CRM_Utils_System::setTitle($title);
207 }
208 else {
209 CRM_Utils_System::setTitle(ts('Search'));
210 }
211 }
212
213 /**
214 * @param string $tableAlias
215 */
216 public function buildACLClause($tableAlias = 'contact') {
217 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
218 }
219
220 }