Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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_Sample extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { | |
86538308 EM |
36 | /** |
37 | * @param $formValues | |
38 | */ | |
6a488035 TO |
39 | function __construct(&$formValues) { |
40 | parent::__construct($formValues); | |
41 | ||
42 | if (!isset($formValues['state_province_id'])) { | |
43 | $this->_stateID = CRM_Utils_Request::retrieve('stateID', 'Integer', | |
44 | CRM_Core_DAO::$_nullObject | |
45 | ); | |
46 | if ($this->_stateID) { | |
47 | $formValues['state_province_id'] = $this->_stateID; | |
48 | } | |
49 | } | |
50 | ||
51 | $this->_columns = array( | |
52 | ts('Contact Id') => 'contact_id', | |
53 | ts('Contact Type') => 'contact_type', | |
54 | ts('Name') => 'sort_name', | |
55 | ts('State') => 'state_province', | |
56 | ); | |
57 | } | |
58 | ||
86538308 EM |
59 | /** |
60 | * @param $form | |
61 | */ | |
6a488035 TO |
62 | function buildForm(&$form) { |
63 | ||
64 | $form->add('text', | |
65 | 'household_name', | |
66 | ts('Household Name'), | |
67 | TRUE | |
68 | ); | |
69 | ||
70 | $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant::stateProvince(); | |
71 | $form->addElement('select', 'state_province_id', ts('State/Province'), $stateProvince); | |
72 | ||
73 | /** | |
74 | * You can define a custom title for the search form | |
75 | */ | |
76 | $this->setTitle('My Search Title'); | |
77 | ||
78 | /** | |
79 | * if you are using the standard template, this array tells the template what elements | |
80 | * are part of the search criteria | |
81 | */ | |
82 | $form->assign('elements', array('household_name', 'state_province_id')); | |
83 | } | |
84 | ||
86538308 EM |
85 | /** |
86 | * @return array | |
87 | */ | |
6a488035 TO |
88 | function summary() { |
89 | $summary = array( | |
90 | 'summary' => 'This is a summary', | |
91 | 'total' => 50.0, | |
92 | ); | |
93 | return $summary; | |
94 | } | |
95 | ||
86538308 EM |
96 | /** |
97 | * @param int $offset | |
98 | * @param int $rowcount | |
99 | * @param null $sort | |
100 | * @param bool $includeContactIDs | |
101 | * @param bool $justIDs | |
102 | * | |
103 | * @return string | |
104 | */ | |
6a488035 TO |
105 | function all($offset = 0, $rowcount = 0, $sort = NULL, |
106 | $includeContactIDs = FALSE, $justIDs = FALSE | |
107 | ) { | |
108 | if ($justIDs) { | |
109 | $selectClause = "contact_a.id as contact_id"; | |
110 | } | |
111 | else { | |
112 | $selectClause = " | |
113 | contact_a.id as contact_id , | |
114 | contact_a.contact_type as contact_type, | |
115 | contact_a.sort_name as sort_name, | |
116 | state_province.name as state_province | |
117 | "; | |
118 | } | |
119 | ||
120 | return $this->sql($selectClause, | |
121 | $offset, $rowcount, $sort, | |
122 | $includeContactIDs, NULL | |
123 | ); | |
124 | } | |
125 | ||
86538308 EM |
126 | /** |
127 | * @return string | |
128 | */ | |
6a488035 TO |
129 | function from() { |
130 | return " | |
131 | FROM civicrm_contact contact_a | |
132 | LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND | |
133 | address.is_primary = 1 ) | |
134 | LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = contact_a.id AND | |
135 | civicrm_email.is_primary = 1 ) | |
136 | LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id | |
137 | "; | |
138 | } | |
139 | ||
86538308 EM |
140 | /** |
141 | * @param bool $includeContactIDs | |
142 | * | |
143 | * @return string | |
144 | */ | |
6a488035 TO |
145 | function where($includeContactIDs = FALSE) { |
146 | $params = array(); | |
147 | $where = "contact_a.contact_type = 'Household'"; | |
148 | ||
149 | $count = 1; | |
150 | $clause = array(); | |
151 | $name = CRM_Utils_Array::value('household_name', | |
152 | $this->_formValues | |
153 | ); | |
154 | if ($name != NULL) { | |
155 | if (strpos($name, '%') === FALSE) { | |
156 | $name = "%{$name}%"; | |
157 | } | |
158 | $params[$count] = array($name, 'String'); | |
159 | $clause[] = "contact_a.household_name LIKE %{$count}"; | |
160 | $count++; | |
161 | } | |
162 | ||
163 | $state = CRM_Utils_Array::value('state_province_id', | |
164 | $this->_formValues | |
165 | ); | |
166 | if (!$state && | |
167 | $this->_stateID | |
168 | ) { | |
169 | $state = $this->_stateID; | |
170 | } | |
171 | ||
172 | if ($state) { | |
173 | $params[$count] = array($state, 'Integer'); | |
174 | $clause[] = "state_province.id = %{$count}"; | |
175 | } | |
176 | ||
177 | if (!empty($clause)) { | |
178 | $where .= ' AND ' . implode(' AND ', $clause); | |
179 | } | |
180 | ||
181 | return $this->whereClause($where, $params); | |
182 | } | |
183 | ||
86538308 EM |
184 | /** |
185 | * @return string | |
186 | */ | |
6a488035 TO |
187 | function templateFile() { |
188 | return 'CRM/Contact/Form/Search/Custom.tpl'; | |
189 | } | |
190 | ||
86538308 EM |
191 | /** |
192 | * @return array | |
193 | */ | |
6a488035 TO |
194 | function setDefaultValues() { |
195 | return array( | |
196 | 'household_name' => '', | |
197 | ); | |
198 | } | |
199 | ||
86538308 EM |
200 | /** |
201 | * @param $row | |
202 | */ | |
6a488035 TO |
203 | function alterRow(&$row) { |
204 | $row['sort_name'] .= ' ( altered )'; | |
205 | } | |
206 | ||
86538308 EM |
207 | /** |
208 | * @param $title | |
209 | */ | |
6a488035 TO |
210 | function setTitle($title) { |
211 | if ($title) { | |
212 | CRM_Utils_System::setTitle($title); | |
213 | } | |
214 | else { | |
215 | CRM_Utils_System::setTitle(ts('Search')); | |
216 | } | |
217 | } | |
218 | } | |
219 |