commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Form / Search / Custom / Sample.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 {
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
38 /**
39 * @param $formValues
40 */
41 public function __construct(&$formValues) {
42 parent::__construct($formValues);
43
44 if (!isset($formValues['state_province_id'])) {
45 $this->_stateID = CRM_Utils_Request::retrieve('stateID', 'Integer',
46 CRM_Core_DAO::$_nullObject
47 );
48 if ($this->_stateID) {
49 $formValues['state_province_id'] = $this->_stateID;
50 }
51 }
52
53 $this->_columns = array(
54 ts('Contact ID') => 'contact_id',
55 ts('Contact Type') => 'contact_type',
56 ts('Name') => 'sort_name',
57 ts('State') => 'state_province',
58 );
59 }
60
61 /**
62 * @param CRM_Core_Form $form
63 */
64 public function buildForm(&$form) {
65
66 $form->add('text',
67 'household_name',
68 ts('Household Name'),
69 TRUE
70 );
71
72 $stateProvince = array('' => ts('- any state/province -')) + CRM_Core_PseudoConstant::stateProvince();
73 $form->addElement('select', 'state_province_id', ts('State/Province'), $stateProvince);
74
75 /**
76 * You can define a custom title for the search form
77 */
78 $this->setTitle('My Search Title');
79
80 /**
81 * if you are using the standard template, this array tells the template what elements
82 * are part of the search criteria
83 */
84 $form->assign('elements', array('household_name', 'state_province_id'));
85 }
86
87 /**
88 * @return array
89 */
90 public function summary() {
91 $summary = array(
92 'summary' => 'This is a summary',
93 'total' => 50.0,
94 );
95 return $summary;
96 }
97
98 /**
99 * @param int $offset
100 * @param int $rowcount
101 * @param null $sort
102 * @param bool $returnSQL
103 *
104 * @return string
105 */
106 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
107 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
108 }
109
110 /**
111 * @param int $offset
112 * @param int $rowcount
113 * @param null $sort
114 * @param bool $includeContactIDs
115 * @param bool $justIDs
116 *
117 * @return string
118 */
119 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
120 if ($justIDs) {
121 $selectClause = "contact_a.id as contact_id";
122 $sort = 'contact_a.id';
123 }
124 else {
125 $selectClause = "
126 contact_a.id as contact_id ,
127 contact_a.contact_type as contact_type,
128 contact_a.sort_name as sort_name,
129 state_province.name as state_province
130 ";
131 }
132
133 return $this->sql($selectClause,
134 $offset, $rowcount, $sort,
135 $includeContactIDs, NULL
136 );
137 }
138
139 /**
140 * @return string
141 */
142 public function from() {
143 $this->buildACLClause('contact_a');
144 $from = "
145 FROM civicrm_contact contact_a
146 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
147 address.is_primary = 1 )
148 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = contact_a.id AND
149 civicrm_email.is_primary = 1 )
150 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom}
151 ";
152 return $from;
153 }
154
155 /**
156 * @param bool $includeContactIDs
157 *
158 * @return string
159 */
160 public function where($includeContactIDs = FALSE) {
161 $params = array();
162 $where = "contact_a.contact_type = 'Household'";
163
164 $count = 1;
165 $clause = array();
166 $name = CRM_Utils_Array::value('household_name',
167 $this->_formValues
168 );
169 if ($name != NULL) {
170 if (strpos($name, '%') === FALSE) {
171 $name = "%{$name}%";
172 }
173 $params[$count] = array($name, 'String');
174 $clause[] = "contact_a.household_name LIKE %{$count}";
175 $count++;
176 }
177
178 $state = CRM_Utils_Array::value('state_province_id',
179 $this->_formValues
180 );
181 if (!$state &&
182 $this->_stateID
183 ) {
184 $state = $this->_stateID;
185 }
186
187 if ($state) {
188 $params[$count] = array($state, 'Integer');
189 $clause[] = "state_province.id = %{$count}";
190 }
191
192 if ($this->_aclWhere) {
193 $clause[] = " {$this->_aclWhere} ";
194 }
195
196 if (!empty($clause)) {
197 $where .= ' AND ' . implode(' AND ', $clause);
198 }
199
200 return $this->whereClause($where, $params);
201 }
202
203 /**
204 * @return string
205 */
206 public function templateFile() {
207 return 'CRM/Contact/Form/Search/Custom.tpl';
208 }
209
210 /**
211 * @return array
212 */
213 public function setDefaultValues() {
214 return array_merge(array('household_name' => ''), $this->_formValues);
215 }
216
217 /**
218 * @param $row
219 */
220 public function alterRow(&$row) {
221 $row['sort_name'] .= ' ( altered )';
222 }
223
224 /**
225 * @param $title
226 */
227 public function setTitle($title) {
228 if ($title) {
229 CRM_Utils_System::setTitle($title);
230 }
231 else {
232 CRM_Utils_System::setTitle(ts('Search'));
233 }
234 }
235
236 /**
237 * @param string $tableAlias
238 */
239 public function buildACLClause($tableAlias = 'contact') {
240 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
241 }
242
243 }