(NFC) Correct type hints for bad null default values
[civicrm-core.git] / ext / legacycustomsearches / CRM / Contact / Form / Search / Custom / Sample.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_Form_Search_Custom_Sample extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
18 protected $_aclFrom = NULL;
19 protected $_aclWhere = NULL;
20
21 /**
22 * Class constructor.
23 *
24 * @param array $formValues
25 */
26 public function __construct(&$formValues) {
27 parent::__construct($formValues);
28
29 if (!isset($formValues['state_province_id'])) {
30 $this->_stateID = CRM_Utils_Request::retrieve('stateID', 'Integer');
31 if ($this->_stateID) {
32 $formValues['state_province_id'] = $this->_stateID;
33 }
34 }
35
36 $this->_columns = [
37 ts('Contact ID') => 'contact_id',
38 ts('Contact Type') => 'contact_type',
39 ts('Name') => 'sort_name',
40 ts('State') => 'state_province',
41 ];
42 }
43
44 /**
45 * Build form.
46 *
47 * @param CRM_Core_Form $form
48 */
49 public function buildForm(&$form) {
50
51 $form->add('text',
52 'household_name',
53 ts('Household Name'),
54 TRUE
55 );
56
57 $stateProvince = ['' => ts('- any state/province -')] + CRM_Core_PseudoConstant::stateProvince();
58 $form->addElement('select', 'state_province_id', ts('State/Province'), $stateProvince);
59
60 /**
61 * You can define a custom title for the search form
62 */
63 $this->setTitle(ts('My Search Title'));
64
65 /**
66 * if you are using the standard template, this array tells the template what elements
67 * are part of the search criteria
68 */
69 $form->assign('elements', ['household_name', 'state_province_id']);
70 }
71
72 /**
73 * @return array
74 */
75 public function summary() {
76 $summary = [
77 'summary' => 'This is a summary',
78 'total' => 50.0,
79 ];
80 return $summary;
81 }
82
83 /**
84 * @param int $offset
85 * @param int $rowcount
86 * @param string|null $sort
87 * @param bool $returnSQL
88 *
89 * @return string
90 */
91 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
92 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
93 }
94
95 /**
96 * @param int $offset
97 * @param int $rowcount
98 * @param string|null $sort
99 * @param bool $includeContactIDs
100 * @param bool $justIDs
101 *
102 * @return string
103 */
104 public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
105 if ($justIDs) {
106 $selectClause = "contact_a.id as contact_id";
107 $sort = 'contact_a.id';
108 }
109 else {
110 $selectClause = "
111 contact_a.id as contact_id ,
112 contact_a.contact_type as contact_type,
113 contact_a.sort_name as sort_name,
114 state_province.name as state_province
115 ";
116 }
117
118 return $this->sql($selectClause,
119 $offset, $rowcount, $sort,
120 $includeContactIDs, NULL
121 );
122 }
123
124 /**
125 * @return string
126 */
127 public function from() {
128 $this->buildACLClause('contact_a');
129 $from = "
130 FROM civicrm_contact contact_a
131 LEFT JOIN civicrm_address address ON ( address.contact_id = contact_a.id AND
132 address.is_primary = 1 )
133 LEFT JOIN civicrm_email ON ( civicrm_email.contact_id = contact_a.id AND
134 civicrm_email.is_primary = 1 )
135 LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom}
136 ";
137 return $from;
138 }
139
140 /**
141 * @param bool $includeContactIDs
142 *
143 * @return string
144 */
145 public function where($includeContactIDs = FALSE) {
146 $params = [];
147 $where = "contact_a.contact_type = 'Household'";
148
149 $count = 1;
150 $clause = [];
151 $name = CRM_Utils_Array::value('household_name',
152 $this->_formValues
153 );
154 if ($name != NULL) {
155 if (strpos($name, '%') === FALSE) {
156 $name = "%{$name}%";
157 }
158 $params[$count] = [$name, 'String'];
159 $clause[] = "contact_a.household_name LIKE %{$count}";
160 $count++;
161 }
162
163 $state = CRM_Utils_Array::value('state_province_id',
164 $this->_formValues
165 );
166 if (!$state &&
167 $this->_stateID
168 ) {
169 $state = $this->_stateID;
170 }
171
172 if ($state) {
173 $params[$count] = [$state, 'Integer'];
174 $clause[] = "state_province.id = %{$count}";
175 }
176
177 if ($this->_aclWhere) {
178 $clause[] = " {$this->_aclWhere} ";
179 }
180
181 if (!empty($clause)) {
182 $where .= ' AND ' . implode(' AND ', $clause);
183 }
184
185 return $this->whereClause($where, $params);
186 }
187
188 /**
189 * @return string
190 */
191 public function templateFile() {
192 return 'CRM/Contact/Form/Search/Custom.tpl';
193 }
194
195 /**
196 * @return array
197 */
198 public function setDefaultValues() {
199 return array_merge(['household_name' => ''], $this->_formValues);
200 }
201
202 /**
203 * @param $row
204 */
205 public function alterRow(&$row) {
206 $row['sort_name'] .= ' ( altered )';
207 }
208
209 /**
210 * @param string $tableAlias
211 */
212 public function buildACLClause($tableAlias = 'contact') {
213 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
214 }
215
216 }