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