Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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_EmployerListing implements CRM_Contact_Form_Search_Interface { | |
36 | ||
37 | protected $_formValues; | |
38 | ||
86538308 EM |
39 | /** |
40 | * @param $formValues | |
41 | */ | |
00be9182 | 42 | public function __construct(&$formValues) { |
6a488035 TO |
43 | $this->_formValues = $formValues; |
44 | ||
45 | /** | |
46 | * Define the columns for search result rows | |
47 | */ | |
48 | $this->_columns = array( | |
7b99ead3 | 49 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
50 | ts('Individual Name') => 'sort_name', |
51 | ts('Individual State') => 'indState', | |
52 | ts('Employer') => 'employer', | |
53 | ts('Employer State') => 'empState', | |
54 | ); | |
55 | } | |
56 | ||
86538308 | 57 | /** |
c490a46a | 58 | * @param CRM_Core_Form $form |
86538308 | 59 | */ |
00be9182 | 60 | public function buildForm(&$form) { |
6a488035 TO |
61 | |
62 | /** | |
63 | * You can define a custom title for the search form | |
64 | */ | |
65 | $this->setTitle('List Employers for Individual Contacts'); | |
66 | ||
67 | /** | |
68 | * Define the search form fields here | |
69 | */ | |
70 | $form->add('text', | |
71 | 'sort_name', | |
72 | ts('Individual\'s Name (last, first)') | |
73 | ); | |
74 | ||
75 | $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant::stateProvince(); | |
76 | $form->addElement('select', 'state_province_id', ts('Individual\'s Home State'), $stateProvince); | |
77 | ||
78 | /** | |
79 | * If you are using the sample template, this array tells the template fields to render | |
80 | * for the search form. | |
81 | */ | |
82 | $form->assign('elements', array('sort_name', 'state_province_id')); | |
83 | } | |
84 | ||
86538308 | 85 | /** |
c490a46a CW |
86 | * Set search form field defaults here. |
87 | * | |
86538308 EM |
88 | * @return array |
89 | */ | |
00be9182 | 90 | public function setDefaultValues() { |
6a488035 TO |
91 | // Setting default search state to California |
92 | return array( | |
93 | 'state_province_id' => 1004, | |
94 | ); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Define the smarty template used to layout the search form and results listings. | |
99 | */ | |
00be9182 | 100 | public function templateFile() { |
6a488035 TO |
101 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
102 | } | |
103 | ||
104 | /** | |
105 | * Construct the search query | |
106 | */ | |
51ccfbbe TO |
107 | function all( |
108 | $offset = 0, $rowcount = 0, $sort = NULL, | |
6a488035 TO |
109 | $includeContactIDs = FALSE, $justIDs = FALSE |
110 | ) { | |
111 | if ($justIDs) { | |
112 | $select = "cInd.id as contact_id"; | |
113 | } | |
114 | else { | |
115 | $select = " | |
116 | DISTINCT cInd.id as contact_id, | |
117 | cInd.sort_name as sort_name, | |
118 | indSP.name as indState, | |
119 | cEmp.sort_name as employer, | |
120 | empSP.name as empState | |
121 | "; | |
122 | } | |
123 | ||
124 | $from = $this->from(); | |
125 | ||
126 | $where = $this->where($includeContactIDs); | |
127 | ||
128 | $having = $this->having(); | |
129 | if ($having) { | |
130 | $having = " HAVING $having "; | |
131 | } | |
132 | ||
133 | // Define GROUP BY here if needed. | |
134 | $grouping = ""; | |
135 | ||
136 | $sql = " | |
137 | SELECT $select | |
138 | FROM $from | |
139 | WHERE $where | |
140 | $grouping | |
141 | $having | |
142 | "; | |
143 | // Define ORDER BY for query in $sort, with default value | |
144 | if (!empty($sort)) { | |
145 | if (is_string($sort)) { | |
21d32567 | 146 | $sort = CRM_Utils_Type::escape($sort, 'String'); |
6a488035 TO |
147 | $sql .= " ORDER BY $sort "; |
148 | } | |
149 | else { | |
150 | $sql .= " ORDER BY " . trim($sort->orderBy()); | |
151 | } | |
152 | } | |
153 | else { | |
154 | $sql .= "ORDER BY sort_name asc"; | |
155 | } | |
156 | ||
6a488035 TO |
157 | return $sql; |
158 | } | |
159 | ||
86538308 EM |
160 | /** |
161 | * @return string | |
162 | */ | |
00be9182 | 163 | public function from() { |
6a488035 TO |
164 | return " |
165 | civicrm_relationship cR, | |
166 | civicrm_contact cInd | |
167 | LEFT JOIN civicrm_address indAddress ON ( indAddress.contact_id = cInd.id AND | |
168 | indAddress.is_primary = 1 ) | |
169 | LEFT JOIN civicrm_state_province indSP ON indSP.id = indAddress.state_province_id, | |
170 | civicrm_contact cEmp | |
171 | LEFT JOIN civicrm_address empAddress ON ( empAddress.contact_id = cEmp.id AND | |
172 | empAddress.is_primary = 1 ) | |
173 | LEFT JOIN civicrm_state_province empSP ON empSP.id = empAddress.state_province_id | |
174 | "; | |
175 | } | |
176 | ||
86538308 | 177 | /** |
c490a46a CW |
178 | * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values |
179 | * | |
86538308 EM |
180 | * @param bool $includeContactIDs |
181 | * | |
182 | * @return string | |
183 | */ | |
00be9182 | 184 | public function where($includeContactIDs = FALSE) { |
6a488035 TO |
185 | $clauses = array(); |
186 | ||
187 | // These are required filters for our query. | |
188 | $clauses[] = "cInd.contact_type = 'Individual'"; | |
189 | $clauses[] = "cR.relationship_type_id = 4"; | |
190 | $clauses[] = "cR.contact_id_a = cInd.id"; | |
191 | $clauses[] = "cR.contact_id_b = cEmp.id"; | |
192 | $clauses[] = "cR.is_active = 1"; | |
193 | ||
194 | // These are conditional filters based on user input | |
195 | $name = CRM_Utils_Array::value('sort_name', | |
196 | $this->_formValues | |
197 | ); | |
198 | if ($name != NULL) { | |
199 | if (strpos($name, '%') === FALSE) { | |
200 | $name = "%{$name}%"; | |
201 | } | |
202 | $clauses[] = "cInd.sort_name LIKE '$name'"; | |
203 | } | |
204 | ||
205 | $state = CRM_Utils_Array::value('state_province_id', | |
206 | $this->_formValues | |
207 | ); | |
208 | if ($state) { | |
209 | $clauses[] = "indSP.id = $state"; | |
210 | } | |
211 | ||
212 | if ($includeContactIDs) { | |
213 | $contactIDs = array(); | |
214 | foreach ($this->_formValues as $id => $value) { | |
215 | if ($value && | |
216 | substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX | |
217 | ) { | |
218 | $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN); | |
219 | } | |
220 | } | |
221 | ||
222 | if (!empty($contactIDs)) { | |
223 | $contactIDs = implode(', ', $contactIDs); | |
224 | $clauses[] = "contact.id IN ( $contactIDs )"; | |
225 | } | |
226 | } | |
227 | ||
228 | return implode(' AND ', $clauses); | |
229 | } | |
230 | ||
86538308 EM |
231 | /** |
232 | * @param bool $includeContactIDs | |
233 | * | |
234 | * @return string | |
235 | */ | |
00be9182 | 236 | public function having($includeContactIDs = FALSE) { |
6a488035 TO |
237 | $clauses = array(); |
238 | return implode(' AND ', $clauses); | |
239 | } | |
240 | ||
241 | /* | |
c490a46a CW |
242 | * Functions below generally don't need to be modified |
243 | */ | |
00be9182 | 244 | public function count() { |
6a488035 TO |
245 | $sql = $this->all(); |
246 | ||
247 | $dao = CRM_Core_DAO::executeQuery($sql, | |
248 | CRM_Core_DAO::$_nullArray | |
249 | ); | |
250 | return $dao->N; | |
251 | } | |
252 | ||
86538308 EM |
253 | /** |
254 | * @param int $offset | |
255 | * @param int $rowcount | |
256 | * @param null $sort | |
257 | * | |
258 | * @return string | |
259 | */ | |
00be9182 | 260 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) { |
6a488035 TO |
261 | return $this->all($offset, $rowcount, $sort); |
262 | } | |
263 | ||
86538308 EM |
264 | /** |
265 | * @return array | |
266 | */ | |
00be9182 | 267 | public function &columns() { |
6a488035 TO |
268 | return $this->_columns; |
269 | } | |
270 | ||
86538308 EM |
271 | /** |
272 | * @param $title | |
273 | */ | |
00be9182 | 274 | public function setTitle($title) { |
6a488035 TO |
275 | if ($title) { |
276 | CRM_Utils_System::setTitle($title); | |
277 | } | |
278 | else { | |
279 | CRM_Utils_System::setTitle(ts('Search')); | |
280 | } | |
281 | } | |
282 | ||
86538308 EM |
283 | /** |
284 | * @return null | |
285 | */ | |
00be9182 | 286 | public function summary() { |
6a488035 TO |
287 | return NULL; |
288 | } | |
289 | } |