Merge pull request #11571 from magnolia61/CRM-21693_Show_Name_in_OnlinePayNow_UI
[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-2018 |
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-2018
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 * Class constructor.
38 *
39 * @param array $formValues
40 */
41 public function __construct(&$formValues) {
42 parent::__construct($formValues);
43
44 $this->_columns = array(
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',
53 ts('Name') => 'sort_name',
54 ts('Email') => 'email',
55 ts('Zip') => 'postal_code',
56 );
57 }
58
59 /**
60 * @param CRM_Core_Form $form
61 */
62 public function buildForm(&$form) {
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
87 /**
88 * @return array
89 */
90 public function summary() {
91 $summary = array();
92 return $summary;
93 }
94
95 /**
96 * @param int $offset
97 * @param int $rowcount
98 * @param null $sort
99 * @param bool $returnSQL
100 *
101 * @return string
102 */
103 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
104 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
105 }
106
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 */
116 public function all(
117 $offset = 0, $rowcount = 0, $sort = NULL,
118 $includeContactIDs = FALSE, $justIDs = FALSE
119 ) {
120 if ($justIDs) {
121 $selectClause = "contact_a.id as contact_id";
122 // Don't change sort order when $justIDs is TRUE, see CRM-14920.
123 }
124 else {
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.
129 $selectClause = "
130 contact_a.id as contact_id ,
131 contact_a.id as id ,
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
143 /**
144 * @return string
145 */
146 public function from() {
147 $this->buildACLClause('contact_a');
148 $from = "
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
153 email.is_primary = 1 ) {$this->_aclFrom}
154 ";
155 return $from;
156 }
157
158 /**
159 * @param bool $includeContactIDs
160 *
161 * @return string
162 */
163 public function where($includeContactIDs = FALSE) {
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";
182 $params = array(
183 1 => array(trim($low), 'Integer'),
184 2 => array(trim($high), 'Integer'),
185 );
186
187 if ($this->_aclWhere) {
188 $where .= " AND {$this->_aclWhere} ";
189 }
190 return $this->whereClause($where, $params);
191 }
192
193 /**
194 * @return string
195 */
196 public function templateFile() {
197 return 'CRM/Contact/Form/Search/Custom.tpl';
198 }
199
200 /**
201 * @param $title
202 */
203 public function setTitle($title) {
204 if ($title) {
205 CRM_Utils_System::setTitle($title);
206 }
207 else {
208 CRM_Utils_System::setTitle(ts('Search'));
209 }
210 }
211
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
219 }