Commit | Line | Data |
---|---|---|
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 | */ | |
35 | ||
36 | /** | |
37 | * This class if for search builder processing | |
38 | */ | |
39 | class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { | |
40 | ||
41 | /** | |
100fef9d | 42 | * Number of columns in where |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | public $_columnCount; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Number of blocks to be shown |
6a488035 TO |
50 | * |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | public $_blockCount; | |
54 | ||
55 | /** | |
c490a46a | 56 | * Build the form object |
6a488035 | 57 | * |
355ba699 | 58 | * @return void |
6a488035 TO |
59 | */ |
60 | public function preProcess() { | |
61 | $this->set('searchFormName', 'Builder'); | |
62 | ||
63 | $this->set('context', 'builder'); | |
64 | parent::preProcess(); | |
65 | ||
66 | // Get the block count | |
67 | $this->_blockCount = $this->get('blockCount'); | |
68 | // Initialize new form | |
69 | if (!$this->_blockCount) { | |
70 | $this->_blockCount = 4; | |
71 | $this->set('newBlock', 1); | |
72 | } | |
73 | ||
74 | //get the column count | |
75 | $this->_columnCount = $this->get('columnCount'); | |
76 | ||
77 | for ($i = 1; $i < $this->_blockCount; $i++) { | |
78 | if (empty($this->_columnCount[$i])) { | |
79 | $this->_columnCount[$i] = 5; | |
80 | } | |
81 | } | |
82 | ||
83 | $this->_loadedMappingId = $this->get('savedMapping'); | |
84 | ||
85 | if ($this->get('showSearchForm')) { | |
86 | $this->assign('showSearchForm', TRUE); | |
87 | } | |
88 | else { | |
89 | $this->assign('showSearchForm', FALSE); | |
90 | } | |
91 | } | |
92 | ||
93 | public function buildQuickForm() { | |
94 | $fields = self::fields(); | |
95 | // Get fields of type date | |
96 | // FIXME: This is a hack until our fields contain this meta-data | |
97 | $dateFields = array(); | |
98 | foreach ($fields as $name => $field) { | |
99 | if (strpos($name, '_date') || CRM_Utils_Array::value('data_type', $field) == 'Date') { | |
100 | $dateFields[] = $name; | |
101 | } | |
102 | } | |
103 | // Add javascript | |
104 | CRM_Core_Resources::singleton() | |
96ed17aa | 105 | ->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Search/Builder.js', 1, 'html-header') |
6a488035 TO |
106 | ->addSetting(array( |
107 | 'searchBuilder' => array( | |
108 | // Index of newly added/expanded block (1-based index) | |
109 | 'newBlock' => $this->get('newBlock'), | |
110 | 'dateFields' => $dateFields, | |
111 | 'fieldOptions' => self::fieldOptions(), | |
112 | ), | |
113 | )); | |
114 | //get the saved search mapping id | |
115 | $mappingId = NULL; | |
116 | if ($this->_ssID) { | |
117 | $mappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id'); | |
118 | } | |
119 | ||
120 | CRM_Core_BAO_Mapping::buildMappingForm($this, 'Search Builder', $mappingId, $this->_columnCount, $this->_blockCount); | |
121 | ||
122 | parent::buildQuickForm(); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Add local and global form rules | |
127 | * | |
6a488035 TO |
128 | * |
129 | * @return void | |
130 | */ | |
00be9182 | 131 | public function addRules() { |
6a488035 TO |
132 | $this->addFormRule(array('CRM_Contact_Form_Search_Builder', 'formRule'), $this); |
133 | } | |
134 | ||
135 | /** | |
100fef9d | 136 | * Global validation rules for the form |
6a488035 | 137 | * |
fd31fa4c EM |
138 | * @param $values |
139 | * @param $files | |
140 | * @param $self | |
141 | * | |
6a488035 TO |
142 | * @return array list of errors to be posted back to the form |
143 | * @static | |
6a488035 | 144 | */ |
00be9182 | 145 | public static function formRule($values, $files, $self) { |
8cc574cf | 146 | if (!empty($values['addMore']) || !empty($values['addBlock'])) { |
6a488035 TO |
147 | return TRUE; |
148 | } | |
149 | $fields = self::fields(); | |
150 | $fld = CRM_Core_BAO_Mapping::formattedFields($values, TRUE); | |
151 | ||
152 | $errorMsg = array(); | |
153 | foreach ($fld as $k => $v) { | |
154 | if (!$v[1]) { | |
155 | $errorMsg["operator[$v[3]][$v[4]]"] = ts("Please enter the operator."); | |
156 | } | |
157 | else { | |
158 | // CRM-10338 | |
159 | $v[2] = self::checkArrayKeyEmpty($v[2]); | |
160 | ||
161 | if (in_array($v[1], array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY')) && | |
162 | !empty($v[2])) { | |
163 | $errorMsg["value[$v[3]][$v[4]]"] = ts('Please clear your value if you want to use %1 operator.', array(1 => $v[1])); | |
164 | } | |
165 | elseif (($v[0] == 'group' || $v[0] == 'tag') && !empty($v[2])) { | |
166 | $grpId = array_keys($v[2]); | |
167 | if (!key($v[2])) { | |
168 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
169 | } | |
170 | ||
171 | if (count($grpId) > 1) { | |
172 | if ($v[1] != 'IN' && $v[1] != 'NOT IN') { | |
173 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
174 | } | |
175 | foreach ($grpId as $val) { | |
176 | $error = CRM_Utils_Type::validate($val, 'Integer', FALSE); | |
177 | if ($error != $val) { | |
425658c3 | 178 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); |
6a488035 TO |
179 | break; |
180 | } | |
181 | } | |
182 | } | |
183 | else { | |
184 | $error = CRM_Utils_Type::validate($grpId[0], 'Integer', FALSE); | |
185 | if ($error != $grpId[0]) { | |
186 | $errorMsg["value[$v[3]][$v[4]]"] = ts('Please enter valid %1 id.', array(1 => $v[0])); | |
187 | } | |
188 | } | |
189 | } | |
190 | elseif (substr($v[0], 0, 7) === 'do_not_' or substr($v[0], 0, 3) === 'is_') { | |
191 | if (isset($v[2])) { | |
192 | $v2 = array($v[2]); | |
193 | if (!isset($v[2])) { | |
194 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
195 | } | |
196 | ||
197 | $error = CRM_Utils_Type::validate($v2[0], 'Integer', FALSE); | |
198 | if ($error != $v2[0]) { | |
199 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
200 | } | |
201 | } | |
202 | else { | |
203 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
204 | } | |
205 | } | |
206 | else { | |
207 | if (substr($v[0], 0, 7) == 'custom_') { | |
442df34b CW |
208 | // Get rid of appended location type id |
209 | list($fieldKey) = explode('-', $v[0]); | |
210 | $type = $fields[$fieldKey]['data_type']; | |
6a488035 TO |
211 | |
212 | // hack to handle custom data of type state and country | |
213 | if (in_array($type, array( | |
214 | 'Country', 'StateProvince'))) { | |
215 | $type = "Integer"; | |
216 | } | |
217 | } | |
218 | else { | |
219 | $fldName = $v[0]; | |
220 | // FIXME: no idea at this point what to do with this, | |
221 | // FIXME: but definitely needs fixing. | |
222 | if (substr($v[0], 0, 13) == 'contribution_') { | |
223 | $fldName = substr($v[0], 13); | |
224 | } | |
225 | ||
226 | $fldValue = CRM_Utils_Array::value($fldName, $fields); | |
227 | $fldType = CRM_Utils_Array::value('type', $fldValue); | |
228 | $type = CRM_Utils_Type::typeToString($fldType); | |
229 | // Check Empty values for Integer Or Boolean Or Date type For operators other than IS NULL and IS NOT NULL. | |
230 | if (!in_array($v[1], | |
231 | array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) { | |
dbc6f6d6 | 232 | if ((($type == 'Int' || $type == 'Boolean') && !is_array($v[2]) && !trim($v[2])) && $v[2] != '0') { |
6a488035 TO |
233 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); |
234 | } | |
235 | elseif ($type == 'Date' && !trim($v[2])) { | |
236 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a value."); | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | if ($type && empty($errorMsg)) { | |
242 | // check for valid format while using IN Operator | |
243 | if ($v[1] == 'IN') { | |
dbc6f6d6 | 244 | if (!is_array($v[2])) { |
245 | $inVal = trim($v[2]); | |
246 | //checking for format to avoid db errors | |
247 | if ($type == 'Int') { | |
248 | if (!preg_match('/^[(]([A-Za-z0-9\,]+)[)]$/', $inVal)) { | |
249 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); | |
250 | } | |
6a488035 | 251 | } |
dbc6f6d6 | 252 | else { |
253 | if (!(substr($inVal, 0, 1) == '(' && substr($inVal, -1, 1) == ')') && !preg_match('/^[(]([A-Za-z0-9åäöÅÄÖüÜœŒæÆøØ\,\s]+)[)]$/', $inVal)) { | |
254 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter correct Data (in valid format)."); | |
255 | } | |
6a488035 TO |
256 | } |
257 | } | |
258 | ||
259 | // Validate each value in parenthesis to avoid db errors | |
260 | if (empty($errorMsg)) { | |
261 | $parenValues = array(); | |
dbc6f6d6 | 262 | $parenValues = is_array($v[2]) ? $v[2] : explode(',', trim($inVal, "(..)")); |
6a488035 TO |
263 | foreach ($parenValues as $val) { |
264 | $val = trim($val); | |
265 | if (!$val && $val != '0') { | |
266 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter the values correctly."); | |
267 | } | |
268 | if (empty($errorMsg)) { | |
269 | $error = CRM_Utils_Type::validate($val, $type, FALSE); | |
270 | if ($error != $val) { | |
271 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
272 | } | |
273 | } | |
274 | } | |
275 | } | |
276 | } | |
277 | elseif (trim($v[2])) { | |
278 | //else check value for rest of the Operators | |
279 | $error = CRM_Utils_Type::validate($v[2], $type, FALSE); | |
280 | if ($error != $v[2]) { | |
281 | $errorMsg["value[$v[3]][$v[4]]"] = ts("Please enter a valid value."); | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | if (!empty($errorMsg)) { | |
290 | $self->set('showSearchForm', TRUE); | |
291 | $self->assign('rows', NULL); | |
292 | return $errorMsg; | |
293 | } | |
294 | ||
295 | return TRUE; | |
296 | } | |
297 | ||
298 | public function normalizeFormValues() {} | |
299 | ||
86538308 EM |
300 | /** |
301 | * @param $formValues | |
302 | * | |
303 | * @return array | |
304 | */ | |
b9e573d4 | 305 | public function convertFormValues(&$formValues) { |
6a488035 TO |
306 | return CRM_Core_BAO_Mapping::formattedFields($formValues); |
307 | } | |
308 | ||
86538308 EM |
309 | /** |
310 | * @return array | |
311 | */ | |
6a488035 TO |
312 | public function &returnProperties() { |
313 | return CRM_Core_BAO_Mapping::returnProperties($this->_formValues); | |
314 | } | |
315 | ||
316 | /** | |
317 | * Process the uploaded file | |
318 | * | |
319 | * @return void | |
6a488035 TO |
320 | */ |
321 | public function postProcess() { | |
322 | $this->set('isAdvanced', '2'); | |
323 | $this->set('isSearchBuilder', '1'); | |
324 | $this->set('showSearchForm', FALSE); | |
325 | ||
326 | $params = $this->controller->exportValues($this->_name); | |
327 | if (!empty($params)) { | |
328 | // Add another block | |
329 | if (!empty($params['addBlock'])) { | |
330 | $this->set('newBlock', $this->_blockCount); | |
331 | $this->_blockCount += 3; | |
332 | $this->set('blockCount', $this->_blockCount); | |
333 | $this->set('showSearchForm', TRUE); | |
334 | return; | |
335 | } | |
336 | // Add another field | |
337 | $addMore = CRM_Utils_Array::value('addMore', $params); | |
338 | for ($x = 1; $x <= $this->_blockCount; $x++) { | |
339 | if (!empty($addMore[$x])) { | |
340 | $this->set('newBlock', $x); | |
341 | $this->_columnCount[$x] = $this->_columnCount[$x] + 5; | |
342 | $this->set('columnCount', $this->_columnCount); | |
343 | $this->set('showSearchForm', TRUE); | |
344 | return; | |
345 | } | |
346 | } | |
347 | $this->set('newBlock', NULL); | |
348 | $checkEmpty = NULL; | |
349 | foreach ($params['mapper'] as $key => $value) { | |
350 | foreach ($value as $k => $v) { | |
351 | if ($v[0]) { | |
352 | $checkEmpty++; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | if (!$checkEmpty) { | |
358 | $this->set('newBlock', 1); | |
359 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/search/builder', '_qf_Builder_display=true')); | |
360 | } | |
361 | } | |
362 | ||
363 | // get user submitted values | |
364 | // get it from controller only if form has been submitted, else preProcess has set this | |
365 | if (!empty($_POST)) { | |
366 | $this->_formValues = $this->controller->exportValues($this->_name); | |
367 | ||
368 | // set the group if group is submitted | |
a7488080 | 369 | if (!empty($this->_formValues['uf_group_id'])) { |
6a488035 TO |
370 | $this->set('id', $this->_formValues['uf_group_id']); |
371 | } | |
372 | else { | |
373 | $this->set('id', ''); | |
374 | } | |
375 | } | |
376 | ||
377 | // we dont want to store the sortByCharacter in the formValue, it is more like | |
378 | // a filter on the result set | |
379 | // this filter is reset if we click on the search button | |
380 | if ($this->_sortByCharacter !== NULL && empty($_POST)) { | |
381 | if (strtolower($this->_sortByCharacter) == 'all') { | |
382 | $this->_formValues['sortByCharacter'] = NULL; | |
383 | } | |
384 | else { | |
385 | $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; | |
386 | } | |
387 | } | |
e166ff79 CW |
388 | else { |
389 | $this->_sortByCharacter = NULL; | |
390 | } | |
6a488035 | 391 | |
b9e573d4 | 392 | $this->_params = $this->convertFormValues($this->_formValues); |
6a488035 TO |
393 | $this->_returnProperties = &$this->returnProperties(); |
394 | ||
395 | // CRM-10338 check if value is empty array | |
396 | foreach ($this->_params as $k => $v) { | |
397 | $this->_params[$k][2] = self::checkArrayKeyEmpty($v[2]); | |
398 | } | |
399 | ||
400 | parent::postProcess(); | |
401 | } | |
402 | ||
86538308 EM |
403 | /** |
404 | * @return array | |
405 | */ | |
00be9182 | 406 | public static function fields() { |
e1ab2e91 | 407 | $fields = array_merge( |
6a488035 TO |
408 | CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE), |
409 | CRM_Core_Component::getQueryFields(), | |
eb1e3589 | 410 | CRM_Contact_BAO_Query_Hook::singleton()->getFields(), |
6a488035 TO |
411 | CRM_Activity_BAO_Activity::exportableFields() |
412 | ); | |
e1ab2e91 | 413 | return $fields; |
6a488035 TO |
414 | } |
415 | ||
416 | /** | |
417 | * CRM-9434 Hackish function to fetch fields with options. | |
418 | * FIXME: When our core fields contain reliable metadata this will be much simpler. | |
419 | * @return array: (string => string) key: field_name value: api entity name | |
420 | * Note: options are fetched via ajax using the api "getoptions" method | |
421 | */ | |
00be9182 | 422 | public static function fieldOptions() { |
6a488035 TO |
423 | // Hack to add options not retrieved by getfields |
424 | // This list could go on and on, but it would be better to fix getfields | |
425 | $options = array( | |
e354351f | 426 | 'group' => 'group_contact', |
427 | 'tag' => 'entity_tag', | |
6a488035 TO |
428 | 'on_hold' => 'yesno', |
429 | 'is_bulkmail' => 'yesno', | |
6a488035 TO |
430 | 'payment_instrument' => 'contribution', |
431 | 'membership_status' => 'membership', | |
432 | 'membership_type' => 'membership', | |
e5d696ef | 433 | 'member_campaign_id' => 'membership', |
67744c4e CW |
434 | 'member_is_test' => 'yesno', |
435 | 'member_is_pay_later' => 'yesno', | |
436 | 'is_override' => 'yesno', | |
6a488035 | 437 | ); |
8ae90f85 | 438 | $entities = array('contact', 'address', 'activity', 'participant', 'pledge', 'member', 'contribution', 'case', 'grant'); |
6a5f199e | 439 | CRM_Contact_BAO_Query_Hook::singleton()->alterSearchBuilderOptions($entities, $options); |
6a488035 | 440 | foreach ($entities as $entity) { |
67744c4e | 441 | $fields = civicrm_api3($entity, 'getfields'); |
6a488035 | 442 | foreach ($fields['values'] as $field => $info) { |
4b5ff63c | 443 | if (!empty($info['options']) || !empty($info['pseudoconstant']) || !empty($info['option_group_id'])) { |
6a488035 | 444 | $options[$field] = $entity; |
e61022d7 | 445 | // Hack for when search field doesn't match db field - e.g. "country" instead of "country_id" |
6a488035 TO |
446 | if (substr($field, -3) == '_id') { |
447 | $options[substr($field, 0, -3)] = $entity; | |
448 | } | |
449 | } | |
e61022d7 CW |
450 | elseif (!empty($info['data_type']) && in_array($info['data_type'], array('StateProvince', 'Country'))) { |
451 | $options[$field] = $entity; | |
452 | } | |
6a488035 TO |
453 | elseif (in_array(substr($field, 0, 3), array('is_', 'do_')) || CRM_Utils_Array::value('data_type', $info) == 'Boolean') { |
454 | $options[$field] = 'yesno'; | |
455 | if ($entity != 'contact') { | |
456 | $options[$entity . '_' . $field] = 'yesno'; | |
457 | } | |
458 | } | |
459 | elseif (strpos($field, '_is_')) { | |
460 | $options[$field] = 'yesno'; | |
461 | } | |
462 | } | |
463 | } | |
464 | return $options; | |
465 | } | |
466 | ||
467 | /** | |
468 | * CRM-10338 | |
469 | * tags and groups use array keys for selection list. | |
470 | * if using IS NULL/NOT NULL, an array with no array key is created | |
e60f24eb | 471 | * convert that to simple NULL so processing can proceed |
6a488035 | 472 | */ |
00be9182 | 473 | public static function checkArrayKeyEmpty($val) { |
6a488035 | 474 | if (is_array($val)) { |
4eeb9a5b | 475 | $v2empty = TRUE; |
6a488035 TO |
476 | foreach ($val as $vk => $vv) { |
477 | if (!empty($vk)) { | |
478 | $v2empty = false; | |
479 | } | |
480 | } | |
481 | if ($v2empty) { | |
e60f24eb | 482 | $val = NULL; |
6a488035 TO |
483 | } |
484 | } | |
485 | return $val; | |
486 | } | |
487 | } |