Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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_Sample extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { | |
86538308 EM |
36 | /** |
37 | * @param $formValues | |
38 | */ | |
6a488035 TO |
39 | function __construct(&$formValues) { |
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 | */ |
6a488035 TO |
62 | function buildForm(&$form) { |
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 | */ | |
6a488035 TO |
88 | function summary() { |
89 | $summary = array( | |
90 | 'summary' => 'This is a summary', | |
91 | 'total' => 50.0, | |
92 | ); | |
93 | return $summary; | |
94 | } | |
95 | ||
7c34ab11 | 96 | function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { |
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 | */ | |
6a488035 TO |
109 | function all($offset = 0, $rowcount = 0, $sort = NULL, |
110 | $includeContactIDs = FALSE, $justIDs = FALSE | |
111 | ) { | |
112 | if ($justIDs) { | |
113 | $selectClause = "contact_a.id as contact_id"; | |
7c34ab11 | 114 | $sort = 'contact_a.id'; |
6a488035 TO |
115 | } |
116 | else { | |
117 | $selectClause = " | |
118 | contact_a.id as contact_id , | |
119 | contact_a.contact_type as contact_type, | |
120 | contact_a.sort_name as sort_name, | |
121 | state_province.name as state_province | |
122 | "; | |
123 | } | |
124 | ||
125 | return $this->sql($selectClause, | |
126 | $offset, $rowcount, $sort, | |
127 | $includeContactIDs, NULL | |
128 | ); | |
129 | } | |
130 | ||
86538308 EM |
131 | /** |
132 | * @return string | |
133 | */ | |
6a488035 TO |
134 | function from() { |
135 | return " | |
136 | FROM civicrm_contact contact_a | |
137 | LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND | |
138 | address.is_primary = 1 ) | |
139 | LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = contact_a.id AND | |
140 | civicrm_email.is_primary = 1 ) | |
141 | LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id | |
142 | "; | |
143 | } | |
144 | ||
86538308 EM |
145 | /** |
146 | * @param bool $includeContactIDs | |
147 | * | |
148 | * @return string | |
149 | */ | |
6a488035 TO |
150 | function where($includeContactIDs = FALSE) { |
151 | $params = array(); | |
152 | $where = "contact_a.contact_type = 'Household'"; | |
153 | ||
154 | $count = 1; | |
155 | $clause = array(); | |
156 | $name = CRM_Utils_Array::value('household_name', | |
157 | $this->_formValues | |
158 | ); | |
159 | if ($name != NULL) { | |
160 | if (strpos($name, '%') === FALSE) { | |
161 | $name = "%{$name}%"; | |
162 | } | |
163 | $params[$count] = array($name, 'String'); | |
164 | $clause[] = "contact_a.household_name LIKE %{$count}"; | |
165 | $count++; | |
166 | } | |
167 | ||
168 | $state = CRM_Utils_Array::value('state_province_id', | |
169 | $this->_formValues | |
170 | ); | |
171 | if (!$state && | |
172 | $this->_stateID | |
173 | ) { | |
174 | $state = $this->_stateID; | |
175 | } | |
176 | ||
177 | if ($state) { | |
178 | $params[$count] = array($state, 'Integer'); | |
179 | $clause[] = "state_province.id = %{$count}"; | |
180 | } | |
181 | ||
182 | if (!empty($clause)) { | |
183 | $where .= ' AND ' . implode(' AND ', $clause); | |
184 | } | |
185 | ||
186 | return $this->whereClause($where, $params); | |
187 | } | |
188 | ||
86538308 EM |
189 | /** |
190 | * @return string | |
191 | */ | |
6a488035 TO |
192 | function templateFile() { |
193 | return 'CRM/Contact/Form/Search/Custom.tpl'; | |
194 | } | |
195 | ||
86538308 EM |
196 | /** |
197 | * @return array | |
198 | */ | |
6a488035 TO |
199 | function setDefaultValues() { |
200 | return array( | |
201 | 'household_name' => '', | |
202 | ); | |
203 | } | |
204 | ||
86538308 EM |
205 | /** |
206 | * @param $row | |
207 | */ | |
6a488035 TO |
208 | function alterRow(&$row) { |
209 | $row['sort_name'] .= ' ( altered )'; | |
210 | } | |
211 | ||
86538308 EM |
212 | /** |
213 | * @param $title | |
214 | */ | |
6a488035 TO |
215 | function setTitle($title) { |
216 | if ($title) { | |
217 | CRM_Utils_System::setTitle($title); | |
218 | } | |
219 | else { | |
220 | CRM_Utils_System::setTitle(ts('Search')); | |
221 | } | |
222 | } | |
223 | } | |
224 |