INFRA-132 - CRM/Contact - Misc
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Sample.php
CommitLineData
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 */
35class 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 */
00be9182 39 public function __construct(&$formValues) {
6a488035
TO
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(
7b99ead3 52 ts('Contact ID') => 'contact_id',
6a488035
TO
53 ts('Contact Type') => 'contact_type',
54 ts('Name') => 'sort_name',
55 ts('State') => 'state_province',
56 );
57 }
58
86538308 59 /**
c490a46a 60 * @param CRM_Core_Form $form
86538308 61 */
00be9182 62 public function buildForm(&$form) {
6a488035
TO
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 */
00be9182 88 public function summary() {
6a488035
TO
89 $summary = array(
90 'summary' => 'This is a summary',
91 'total' => 50.0,
92 );
93 return $summary;
94 }
95
00be9182 96 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
7c34ab11 97 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
98 }
99
86538308
EM
100 /**
101 * @param int $offset
102 * @param int $rowcount
103 * @param null $sort
104 * @param bool $includeContactIDs
105 * @param bool $justIDs
106 *
107 * @return string
108 */
51ccfbbe
TO
109 function all(
110 $offset = 0, $rowcount = 0, $sort = NULL,
6a488035
TO
111 $includeContactIDs = FALSE, $justIDs = FALSE
112 ) {
113 if ($justIDs) {
114 $selectClause = "contact_a.id as contact_id";
7c34ab11 115 $sort = 'contact_a.id';
6a488035
TO
116 }
117 else {
118 $selectClause = "
119contact_a.id as contact_id ,
120contact_a.contact_type as contact_type,
121contact_a.sort_name as sort_name,
122state_province.name as state_province
123";
124 }
125
126 return $this->sql($selectClause,
127 $offset, $rowcount, $sort,
128 $includeContactIDs, NULL
129 );
130 }
131
86538308
EM
132 /**
133 * @return string
134 */
00be9182 135 public function from() {
6a488035
TO
136 return "
137FROM civicrm_contact contact_a
138LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
139 address.is_primary = 1 )
140LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = contact_a.id AND
141 civicrm_email.is_primary = 1 )
142LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id
143";
144 }
145
86538308
EM
146 /**
147 * @param bool $includeContactIDs
148 *
149 * @return string
150 */
00be9182 151 public function where($includeContactIDs = FALSE) {
6a488035
TO
152 $params = array();
153 $where = "contact_a.contact_type = 'Household'";
154
155 $count = 1;
156 $clause = array();
157 $name = CRM_Utils_Array::value('household_name',
158 $this->_formValues
159 );
160 if ($name != NULL) {
161 if (strpos($name, '%') === FALSE) {
162 $name = "%{$name}%";
163 }
164 $params[$count] = array($name, 'String');
165 $clause[] = "contact_a.household_name LIKE %{$count}";
166 $count++;
167 }
168
169 $state = CRM_Utils_Array::value('state_province_id',
170 $this->_formValues
171 );
172 if (!$state &&
173 $this->_stateID
174 ) {
175 $state = $this->_stateID;
176 }
177
178 if ($state) {
179 $params[$count] = array($state, 'Integer');
180 $clause[] = "state_province.id = %{$count}";
181 }
182
183 if (!empty($clause)) {
184 $where .= ' AND ' . implode(' AND ', $clause);
185 }
186
187 return $this->whereClause($where, $params);
188 }
189
86538308
EM
190 /**
191 * @return string
192 */
00be9182 193 public function templateFile() {
6a488035
TO
194 return 'CRM/Contact/Form/Search/Custom.tpl';
195 }
196
86538308
EM
197 /**
198 * @return array
199 */
00be9182 200 public function setDefaultValues() {
6a488035
TO
201 return array(
202 'household_name' => '',
203 );
204 }
205
86538308
EM
206 /**
207 * @param $row
208 */
00be9182 209 public function alterRow(&$row) {
6a488035
TO
210 $row['sort_name'] .= ' ( altered )';
211 }
212
86538308
EM
213 /**
214 * @param $title
215 */
00be9182 216 public function setTitle($title) {
6a488035
TO
217 if ($title) {
218 CRM_Utils_System::setTitle($title);
219 }
220 else {
221 CRM_Utils_System::setTitle(ts('Search'));
222 }
223 }
224}