Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Contact_Form_Search_Custom_Sample extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface { | |
d14ccbdc SL |
18 | protected $_aclFrom = NULL; |
19 | protected $_aclWhere = NULL; | |
69078420 | 20 | |
86538308 | 21 | /** |
5a409b50 | 22 | * Class constructor. |
23 | * | |
24 | * @param array $formValues | |
86538308 | 25 | */ |
00be9182 | 26 | public function __construct(&$formValues) { |
6a488035 TO |
27 | parent::__construct($formValues); |
28 | ||
29 | if (!isset($formValues['state_province_id'])) { | |
1273d77c | 30 | $this->_stateID = CRM_Utils_Request::retrieve('stateID', 'Integer'); |
6a488035 TO |
31 | if ($this->_stateID) { |
32 | $formValues['state_province_id'] = $this->_stateID; | |
33 | } | |
34 | } | |
35 | ||
be2fb01f | 36 | $this->_columns = [ |
7b99ead3 | 37 | ts('Contact ID') => 'contact_id', |
6a488035 TO |
38 | ts('Contact Type') => 'contact_type', |
39 | ts('Name') => 'sort_name', | |
40 | ts('State') => 'state_province', | |
be2fb01f | 41 | ]; |
6a488035 TO |
42 | } |
43 | ||
86538308 | 44 | /** |
5a409b50 | 45 | * Build form. |
46 | * | |
c490a46a | 47 | * @param CRM_Core_Form $form |
86538308 | 48 | */ |
00be9182 | 49 | public function buildForm(&$form) { |
6a488035 TO |
50 | |
51 | $form->add('text', | |
52 | 'household_name', | |
53 | ts('Household Name'), | |
54 | TRUE | |
55 | ); | |
56 | ||
be2fb01f | 57 | $stateProvince = ['' => ts('- any state/province -')] + CRM_Core_PseudoConstant::stateProvince(); |
6a488035 TO |
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 | */ | |
ccc29f8e | 63 | $this->setTitle(ts('My Search Title')); |
6a488035 TO |
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 | */ | |
be2fb01f | 69 | $form->assign('elements', ['household_name', 'state_province_id']); |
6a488035 TO |
70 | } |
71 | ||
86538308 EM |
72 | /** |
73 | * @return array | |
74 | */ | |
00be9182 | 75 | public function summary() { |
be2fb01f | 76 | $summary = [ |
6a488035 TO |
77 | 'summary' => 'This is a summary', |
78 | 'total' => 50.0, | |
be2fb01f | 79 | ]; |
6a488035 TO |
80 | return $summary; |
81 | } | |
82 | ||
1cd3ffa9 EM |
83 | /** |
84 | * @param int $offset | |
85 | * @param int $rowcount | |
5e21e0f3 | 86 | * @param string|null $sort |
1cd3ffa9 EM |
87 | * @param bool $returnSQL |
88 | * | |
89 | * @return string | |
90 | */ | |
00be9182 | 91 | public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) { |
7c34ab11 | 92 | return $this->all($offset, $rowcount, $sort, FALSE, TRUE); |
93 | } | |
94 | ||
86538308 EM |
95 | /** |
96 | * @param int $offset | |
97 | * @param int $rowcount | |
5e21e0f3 | 98 | * @param string|null $sort |
86538308 EM |
99 | * @param bool $includeContactIDs |
100 | * @param bool $justIDs | |
101 | * | |
102 | * @return string | |
103 | */ | |
8d7a9d07 | 104 | public function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) { |
6a488035 TO |
105 | if ($justIDs) { |
106 | $selectClause = "contact_a.id as contact_id"; | |
7c34ab11 | 107 | $sort = 'contact_a.id'; |
6a488035 TO |
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 | ||
86538308 EM |
124 | /** |
125 | * @return string | |
126 | */ | |
00be9182 | 127 | public function from() { |
d14ccbdc SL |
128 | $this->buildACLClause('contact_a'); |
129 | $from = " | |
6a488035 TO |
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 ) | |
d14ccbdc | 135 | LEFT JOIN civicrm_state_province state_province ON state_province.id = address.state_province_id {$this->_aclFrom} |
6a488035 | 136 | "; |
d14ccbdc | 137 | return $from; |
6a488035 TO |
138 | } |
139 | ||
86538308 EM |
140 | /** |
141 | * @param bool $includeContactIDs | |
142 | * | |
143 | * @return string | |
144 | */ | |
00be9182 | 145 | public function where($includeContactIDs = FALSE) { |
be2fb01f | 146 | $params = []; |
6a488035 TO |
147 | $where = "contact_a.contact_type = 'Household'"; |
148 | ||
353ffa53 | 149 | $count = 1; |
be2fb01f | 150 | $clause = []; |
353ffa53 | 151 | $name = CRM_Utils_Array::value('household_name', |
6a488035 TO |
152 | $this->_formValues |
153 | ); | |
154 | if ($name != NULL) { | |
155 | if (strpos($name, '%') === FALSE) { | |
156 | $name = "%{$name}%"; | |
157 | } | |
be2fb01f | 158 | $params[$count] = [$name, 'String']; |
6a488035 TO |
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) { | |
be2fb01f | 173 | $params[$count] = [$state, 'Integer']; |
6a488035 TO |
174 | $clause[] = "state_province.id = %{$count}"; |
175 | } | |
7bbd0c9d | 176 | |
47b8444f | 177 | if ($this->_aclWhere) { |
b3798d48 | 178 | $clause[] = " {$this->_aclWhere} "; |
47b8444f | 179 | } |
6a488035 TO |
180 | |
181 | if (!empty($clause)) { | |
182 | $where .= ' AND ' . implode(' AND ', $clause); | |
183 | } | |
184 | ||
185 | return $this->whereClause($where, $params); | |
186 | } | |
187 | ||
86538308 EM |
188 | /** |
189 | * @return string | |
190 | */ | |
00be9182 | 191 | public function templateFile() { |
6a488035 TO |
192 | return 'CRM/Contact/Form/Search/Custom.tpl'; |
193 | } | |
194 | ||
86538308 EM |
195 | /** |
196 | * @return array | |
197 | */ | |
00be9182 | 198 | public function setDefaultValues() { |
be2fb01f | 199 | return array_merge(['household_name' => ''], $this->_formValues); |
6a488035 TO |
200 | } |
201 | ||
86538308 EM |
202 | /** |
203 | * @param $row | |
204 | */ | |
00be9182 | 205 | public function alterRow(&$row) { |
6a488035 TO |
206 | $row['sort_name'] .= ' ( altered )'; |
207 | } | |
208 | ||
d14ccbdc SL |
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 | ||
6a488035 | 216 | } |