Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 { | |
86538308 EM |
36 | /** |
37 | * @param $formValues | |
38 | */ | |
00be9182 | 39 | public function __construct(&$formValues) { |
6a488035 TO |
40 | parent::__construct($formValues); |
41 | ||
42 | $this->_columns = array( | |
7b99ead3 | 43 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
44 | ts('Name') => 'sort_name', |
45 | ts('Email') => 'email', | |
46 | ts('Zip') => 'postal_code', | |
47 | ); | |
48 | } | |
49 | ||
86538308 | 50 | /** |
c490a46a | 51 | * @param CRM_Core_Form $form |
86538308 | 52 | */ |
00be9182 | 53 | public function buildForm(&$form) { |
6a488035 TO |
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 | ||
86538308 EM |
78 | /** |
79 | * @return array | |
80 | */ | |
00be9182 | 81 | public function summary() { |
6a488035 TO |
82 | $summary = array(); |
83 | return $summary; | |
84 | } | |
85 | ||
00be9182 | 86 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { |
7c34ab11 | 87 | return $this->all($offset, $rowcount, $sort, FALSE, TRUE); |
88 | } | |
89 | ||
86538308 EM |
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 | */ | |
51ccfbbe TO |
99 | function all( |
100 | $offset = 0, $rowcount = 0, $sort = NULL, | |
6a488035 TO |
101 | $includeContactIDs = FALSE, $justIDs = FALSE |
102 | ) { | |
103 | if ($justIDs) { | |
104 | $selectClause = "contact_a.id as contact_id"; | |
7c34ab11 | 105 | $sort = "contact_a.id"; |
6a488035 TO |
106 | } |
107 | else { | |
108 | $selectClause = " | |
109 | contact_a.id as contact_id , | |
110 | contact_a.sort_name as sort_name , | |
111 | email.email as email , | |
112 | address.postal_code as postal_code | |
113 | "; | |
114 | } | |
115 | return $this->sql($selectClause, | |
116 | $offset, $rowcount, $sort, | |
117 | $includeContactIDs, NULL | |
118 | ); | |
119 | } | |
120 | ||
86538308 EM |
121 | /** |
122 | * @return string | |
123 | */ | |
00be9182 | 124 | public function from() { |
6a488035 TO |
125 | return " |
126 | FROM civicrm_contact contact_a | |
127 | LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND | |
128 | address.is_primary = 1 ) | |
129 | LEFT JOIN civicrm_email email ON ( email.contact_id = contact_a.id AND | |
130 | email.is_primary = 1 ) | |
131 | "; | |
132 | } | |
133 | ||
86538308 EM |
134 | /** |
135 | * @param bool $includeContactIDs | |
136 | * | |
137 | * @return string | |
138 | */ | |
00be9182 | 139 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
140 | $params = array(); |
141 | ||
142 | $low = CRM_Utils_Array::value('postal_code_low', | |
143 | $this->_formValues | |
144 | ); | |
145 | $high = CRM_Utils_Array::value('postal_code_high', | |
146 | $this->_formValues | |
147 | ); | |
148 | if ($low == NULL || $high == NULL) { | |
149 | CRM_Core_Error::statusBounce(ts('Please provide start and end postal codes'), | |
150 | CRM_Utils_System::url('civicrm/contact/search/custom', | |
151 | "reset=1&csid={$this->_formValues['customSearchID']}", | |
152 | FALSE, NULL, FALSE, TRUE | |
153 | ) | |
154 | ); | |
155 | } | |
156 | ||
157 | $where = "ROUND(address.postal_code) >= %1 AND ROUND(address.postal_code) <= %2"; | |
51ccfbbe TO |
158 | $params = array( |
159 | 1 => array(trim($low), 'Integer'), | |
6a488035 TO |
160 | 2 => array(trim($high), 'Integer'), |
161 | ); | |
162 | ||
163 | return $this->whereClause($where, $params); | |
164 | } | |
165 | ||
86538308 EM |
166 | /** |
167 | * @return array | |
168 | */ | |
00be9182 | 169 | public function setDefaultValues() { |
6a488035 TO |
170 | return array(); |
171 | } | |
172 | ||
86538308 EM |
173 | /** |
174 | * @return string | |
175 | */ | |
00be9182 | 176 | public function templateFile() { |
6a488035 TO |
177 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
178 | } | |
179 | ||
86538308 EM |
180 | /** |
181 | * @param $title | |
182 | */ | |
00be9182 | 183 | public function setTitle($title) { |
6a488035 TO |
184 | if ($title) { |
185 | CRM_Utils_System::setTitle($title); | |
186 | } | |
187 | else { | |
188 | CRM_Utils_System::setTitle(ts('Search')); | |
189 | } | |
190 | } | |
191 | } |