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