3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Contact_Form_Search_Custom_EmployerListing
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
39 function __construct(&$formValues) {
40 $this->_formValues
= $formValues;
43 * Define the columns for search result rows
45 $this->_columns
= array(
46 ts('Contact Id') => 'contact_id',
47 ts('Individual Name') => 'sort_name',
48 ts('Individual State') => 'indState',
49 ts('Employer') => 'employer',
50 ts('Employer State') => 'empState',
54 function buildForm(&$form) {
57 * You can define a custom title for the search form
59 $this->setTitle('List Employers for Individual Contacts');
62 * Define the search form fields here
66 ts('Individual\'s Name (last, first)')
69 $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant
::stateProvince();
70 $form->addElement('select', 'state_province_id', ts('Individual\'s Home State'), $stateProvince);
73 * If you are using the sample template, this array tells the template fields to render
74 * for the search form.
76 $form->assign('elements', array('sort_name', 'state_province_id'));
80 * Set search form field defaults here.
82 function setDefaultValues() {
83 // Setting default search state to California
85 'state_province_id' => 1004,
90 * Define the smarty template used to layout the search form and results listings.
92 function templateFile() {
93 return 'CRM/Contact/Form/Search/Custom.tpl';
97 * Construct the search query
99 function all($offset = 0, $rowcount = 0, $sort = NULL,
100 $includeContactIDs = FALSE, $justIDs = FALSE
103 $select = "cInd.id as contact_id";
107 DISTINCT cInd.id as contact_id,
108 cInd.sort_name as sort_name,
109 indSP.name as indState,
110 cEmp.sort_name as employer,
111 empSP.name as empState
115 $from = $this->from();
117 $where = $this->where($includeContactIDs);
119 $having = $this->having();
121 $having = " HAVING $having ";
124 // Define GROUP BY here if needed.
134 // Define ORDER BY for query in $sort, with default value
136 if (is_string($sort)) {
137 $sql .= " ORDER BY $sort ";
140 $sql .= " ORDER BY " . trim($sort->orderBy());
144 $sql .= "ORDER BY sort_name asc";
147 /* Uncomment the next 2 lines to see the exact query you're generating */
149 // CRM_Core_Error::debug('sql',$sql);
157 civicrm_relationship cR,
159 LEFT JOIN civicrm_address indAddress ON ( indAddress.contact_id = cInd.id AND
160 indAddress.is_primary = 1 )
161 LEFT JOIN civicrm_state_province indSP ON indSP.id = indAddress.state_province_id,
163 LEFT JOIN civicrm_address empAddress ON ( empAddress.contact_id = cEmp.id AND
164 empAddress.is_primary = 1 )
165 LEFT JOIN civicrm_state_province empSP ON empSP.id = empAddress.state_province_id
170 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
173 function where($includeContactIDs = FALSE) {
176 // These are required filters for our query.
177 $clauses[] = "cInd.contact_type = 'Individual'";
178 $clauses[] = "cR.relationship_type_id = 4";
179 $clauses[] = "cR.contact_id_a = cInd.id";
180 $clauses[] = "cR.contact_id_b = cEmp.id";
181 $clauses[] = "cR.is_active = 1";
183 // These are conditional filters based on user input
184 $name = CRM_Utils_Array
::value('sort_name',
188 if (strpos($name, '%') === FALSE) {
191 $clauses[] = "cInd.sort_name LIKE '$name'";
194 $state = CRM_Utils_Array
::value('state_province_id',
198 $clauses[] = "indSP.id = $state";
201 if ($includeContactIDs) {
202 $contactIDs = array();
203 foreach ($this->_formValues
as $id => $value) {
205 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
207 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
211 if (!empty($contactIDs)) {
212 $contactIDs = implode(', ', $contactIDs);
213 $clauses[] = "contact.id IN ( $contactIDs )";
217 return implode(' AND ', $clauses);
220 function having($includeContactIDs = FALSE) {
222 return implode(' AND ', $clauses);
226 * Functions below generally don't need to be modified
231 $dao = CRM_Core_DAO
::executeQuery($sql,
232 CRM_Core_DAO
::$_nullArray
237 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
238 return $this->all($offset, $rowcount, $sort);
241 function &columns() {
242 return $this->_columns
;
245 function setTitle($title) {
247 CRM_Utils_System
::setTitle($title);
250 CRM_Utils_System
::setTitle(ts('Search'));