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