INFRA-132 - CRM/Contact - Misc
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / EmployerListing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Search_Custom_EmployerListing implements CRM_Contact_Form_Search_Interface {
36
37 protected $_formValues;
38
39 /**
40 * @param $formValues
41 */
42 public function __construct(&$formValues) {
43 $this->_formValues = $formValues;
44
45 /**
46 * Define the columns for search result rows
47 */
48 $this->_columns = array(
49 ts('Contact ID') => 'contact_id',
50 ts('Individual Name') => 'sort_name',
51 ts('Individual State') => 'indState',
52 ts('Employer') => 'employer',
53 ts('Employer State') => 'empState',
54 );
55 }
56
57 /**
58 * @param CRM_Core_Form $form
59 */
60 public function buildForm(&$form) {
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
85 /**
86 * Set search form field defaults here.
87 *
88 * @return array
89 */
90 public function setDefaultValues() {
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 */
100 public function templateFile() {
101 return 'CRM/Contact/Form/Search/Custom.tpl';
102 }
103
104 /**
105 * Construct the search query
106 */
107 function all(
108 $offset = 0, $rowcount = 0, $sort = NULL,
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)) {
146 $sort = CRM_Utils_Type::escape($sort, 'String');
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
157 return $sql;
158 }
159
160 /**
161 * @return string
162 */
163 public function from() {
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
177 /**
178 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
179 *
180 * @param bool $includeContactIDs
181 *
182 * @return string
183 */
184 public function where($includeContactIDs = FALSE) {
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
231 /**
232 * @param bool $includeContactIDs
233 *
234 * @return string
235 */
236 public function having($includeContactIDs = FALSE) {
237 $clauses = array();
238 return implode(' AND ', $clauses);
239 }
240
241 /*
242 * Functions below generally don't need to be modified
243 */
244 public function count() {
245 $sql = $this->all();
246
247 $dao = CRM_Core_DAO::executeQuery($sql,
248 CRM_Core_DAO::$_nullArray
249 );
250 return $dao->N;
251 }
252
253 /**
254 * @param int $offset
255 * @param int $rowcount
256 * @param null $sort
257 *
258 * @return string
259 */
260 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
261 return $this->all($offset, $rowcount, $sort);
262 }
263
264 /**
265 * @return array
266 */
267 public function &columns() {
268 return $this->_columns;
269 }
270
271 /**
272 * @param $title
273 */
274 public function setTitle($title) {
275 if ($title) {
276 CRM_Utils_System::setTitle($title);
277 }
278 else {
279 CRM_Utils_System::setTitle(ts('Search'));
280 }
281 }
282
283 /**
284 * @return null
285 */
286 public function summary() {
287 return NULL;
288 }
289 }