3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
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;
40 * @param array $formValues
42 public function __construct(&$formValues) {
43 parent
::__construct($formValues);
46 // If possible, don't use aliases for the columns you select.
47 // You can prefix columns with table aliases, if needed.
49 // If you don't do this, selecting individual records from the
50 // custom search result won't work if your results are sorted on the
52 // (This is why we map Contact ID on contact_a.id, and not on contact_id).
53 ts('Contact ID') => 'contact_a.id',
54 ts('Name') => 'sort_name',
55 ts('Email') => 'email',
56 ts('Zip') => 'postal_code',
61 * @param CRM_Core_Form $form
63 public function buildForm(&$form) {
66 ts('Postal Code Start'),
72 ts('Postal Code End'),
77 * You can define a custom title for the search form
79 $this->setTitle('Zip Code Range Search');
82 * if you are using the standard template, this array tells the template what elements
83 * are part of the search criteria
85 $form->assign('elements', ['postal_code_low', 'postal_code_high']);
91 public function summary() {
98 * @param int $rowcount
100 * @param bool $returnSQL
104 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
105 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
110 * @param int $rowcount
112 * @param bool $includeContactIDs
113 * @param bool $justIDs
118 $offset = 0, $rowcount = 0, $sort = NULL,
119 $includeContactIDs = FALSE, $justIDs = FALSE
122 $selectClause = "contact_a.id as contact_id";
123 // Don't change sort order when $justIDs is TRUE, see CRM-14920.
126 // We select contact_a.id twice. Once as contact_a.id,
127 // because it is used to fill the prevnext_cache. And once
128 // as contact_a.id, for the patch of CRM-16587 to work when
129 // the results are sorted on contact ID.
131 contact_a.id as contact_id ,
133 contact_a.sort_name as sort_name ,
134 email.email as email ,
135 address.postal_code as postal_code
138 return $this->sql($selectClause,
139 $offset, $rowcount, $sort,
140 $includeContactIDs, NULL
147 public function from() {
148 $this->buildACLClause('contact_a');
150 FROM civicrm_contact contact_a
151 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
152 address.is_primary = 1 )
153 LEFT JOIN civicrm_email email ON ( email.contact_id = contact_a.id AND
154 email.is_primary = 1 ) {$this->_aclFrom}
160 * @param bool $includeContactIDs
164 public function where($includeContactIDs = FALSE) {
167 $low = CRM_Utils_Array
::value('postal_code_low',
170 $high = CRM_Utils_Array
::value('postal_code_high',
173 if ($low == NULL ||
$high == NULL) {
174 CRM_Core_Error
::statusBounce(ts('Please provide start and end postal codes'),
175 CRM_Utils_System
::url('civicrm/contact/search/custom',
176 "reset=1&csid={$this->_formValues['customSearchID']}",
177 FALSE, NULL, FALSE, TRUE
182 $where = "ROUND(address.postal_code) >= %1 AND ROUND(address.postal_code) <= %2";
184 1 => [trim($low), 'Integer'],
185 2 => [trim($high), 'Integer'],
188 if ($this->_aclWhere
) {
189 $where .= " AND {$this->_aclWhere} ";
191 return $this->whereClause($where, $params);
197 public function templateFile() {
198 return 'CRM/Contact/Form/Search/Custom.tpl';
204 public function setTitle($title) {
206 CRM_Utils_System
::setTitle($title);
209 CRM_Utils_System
::setTitle(ts('Search'));
214 * @param string $tableAlias
216 public function buildACLClause($tableAlias = 'contact') {
217 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);