3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contact_Form_Search_Custom_EmployerListing
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
42 public function __construct(&$formValues) {
43 $this->_formValues
= $formValues;
46 * Define the columns for search result rows
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',
58 * @param CRM_Core_Form $form
60 public function buildForm(&$form) {
63 * You can define a custom title for the search form
65 $this->setTitle('List Employers for Individual Contacts');
68 * Define the search form fields here
72 ts('Individual\'s Name (last, first)')
75 $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant
::stateProvince();
76 $form->addElement('select', 'state_province_id', ts('Individual\'s Home State'), $stateProvince);
79 * If you are using the sample template, this array tells the template fields to render
80 * for the search form.
82 $form->assign('elements', array('sort_name', 'state_province_id'));
86 * Set search form field defaults here.
90 public function setDefaultValues() {
91 // Setting default search state to California
93 'state_province_id' => 1004,
98 * Define the smarty template used to layout the search form and results listings.
100 public function templateFile() {
101 return 'CRM/Contact/Form/Search/Custom.tpl';
105 * Construct the search query
108 $offset = 0, $rowcount = 0, $sort = NULL,
109 $includeContactIDs = FALSE, $justIDs = FALSE
112 $select = "cInd.id as contact_id";
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
124 $from = $this->from();
126 $where = $this->where($includeContactIDs);
128 $having = $this->having();
130 $having = " HAVING $having ";
133 // Define GROUP BY here if needed.
143 // Define ORDER BY for query in $sort, with default value
145 if (is_string($sort)) {
146 $sort = CRM_Utils_Type
::escape($sort, 'String');
147 $sql .= " ORDER BY $sort ";
150 $sql .= " ORDER BY " . trim($sort->orderBy());
154 $sql .= "ORDER BY sort_name asc";
163 public function from() {
165 civicrm_relationship cR,
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,
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
178 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
180 * @param bool $includeContactIDs
184 public function where($includeContactIDs = FALSE) {
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";
194 // These are conditional filters based on user input
195 $name = CRM_Utils_Array
::value('sort_name',
199 if (strpos($name, '%') === FALSE) {
202 $clauses[] = "cInd.sort_name LIKE '$name'";
205 $state = CRM_Utils_Array
::value('state_province_id',
209 $clauses[] = "indSP.id = $state";
212 if ($includeContactIDs) {
213 $contactIDs = array();
214 foreach ($this->_formValues
as $id => $value) {
216 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
218 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
222 if (!empty($contactIDs)) {
223 $contactIDs = implode(', ', $contactIDs);
224 $clauses[] = "contact.id IN ( $contactIDs )";
228 return implode(' AND ', $clauses);
232 * @param bool $includeContactIDs
236 public function having($includeContactIDs = FALSE) {
238 return implode(' AND ', $clauses);
242 * Functions below generally don't need to be modified
244 public function count() {
247 $dao = CRM_Core_DAO
::executeQuery($sql,
248 CRM_Core_DAO
::$_nullArray
255 * @param int $rowcount
260 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
261 return $this->all($offset, $rowcount, $sort);
267 public function &columns() {
268 return $this->_columns
;
274 public function setTitle($title) {
276 CRM_Utils_System
::setTitle($title);
279 CRM_Utils_System
::setTitle(ts('Search'));
286 public function summary() {