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