Conditions | 33 |
Paths | 8652 |
Total Lines | 205 |
Code Lines | 115 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
277 | #[Override] |
||
278 | public function search($store, $entryid, $action, $actionType) { |
||
279 | $useSearchFolder = $action["use_searchfolder"] ?? false; |
||
280 | if (!$useSearchFolder) { |
||
281 | /* |
||
282 | * store doesn't support search folders so we can't use this |
||
283 | * method instead we will pass restriction to messageList and |
||
284 | * it will give us the restricted results |
||
285 | */ |
||
286 | return parent::messageList($store, $entryid, $action, "list"); |
||
287 | } |
||
288 | $store_props = mapi_getprops($store, [PR_MDB_PROVIDER, PR_DEFAULT_STORE, PR_IPM_SUBTREE_ENTRYID]); |
||
289 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) { |
||
290 | // public store does not support search folders |
||
291 | return parent::messageList($store, $entryid, $action, "search"); |
||
292 | } |
||
293 | if ($GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex(TodoList::getEntryId()))) { |
||
294 | // todo list do not need to perform full text index search |
||
295 | return parent::messageList($store, $entryid, $action, "list"); |
||
296 | } |
||
297 | |||
298 | $this->searchFolderList = true; // Set to indicate this is not the normal folder, but a search folder |
||
299 | $this->restriction = false; |
||
300 | |||
301 | // Parse Restriction |
||
302 | $this->parseRestriction($action); |
||
303 | if ($this->restriction == false) { |
||
304 | // if error in creating restriction then send error to client |
||
305 | $errorInfo = []; |
||
306 | $errorInfo["error_message"] = _("Error in search, please try again") . "."; |
||
307 | $errorInfo["original_error_message"] = "Error in parsing restrictions."; |
||
308 | |||
309 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
310 | } |
||
311 | |||
312 | $isSetSearchFolderEntryId = isset($action['search_folder_entryid']); |
||
313 | if ($isSetSearchFolderEntryId) { |
||
314 | $this->sessionData['searchFolderEntryId'] = $action['search_folder_entryid']; |
||
315 | } |
||
316 | |||
317 | if (isset($action['forceCreateSearchFolder']) && $action['forceCreateSearchFolder']) { |
||
318 | $isSetSearchFolderEntryId = false; |
||
319 | } |
||
320 | |||
321 | // create or open search folder |
||
322 | $searchFolder = $this->createSearchFolder($store, $isSetSearchFolderEntryId); |
||
323 | if ($searchFolder === false) { |
||
324 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
325 | $this->messageList($store, $entryid, $action, "search"); |
||
326 | |||
327 | return true; |
||
328 | } |
||
329 | // if error in creating search folder then send error to client |
||
330 | $errorInfo = []; |
||
331 | |||
332 | $errorInfo["error_message"] = match (mapi_last_hresult()) { |
||
333 | MAPI_E_NO_ACCESS => _("Unable to perform search query, no permissions to create search folder."), |
||
334 | MAPI_E_NOT_FOUND => _("Unable to perform search query, search folder not found."), |
||
335 | default => _("Unable to perform search query, store might not support searching."), |
||
336 | }; |
||
337 | |||
338 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
339 | |||
340 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
341 | } |
||
342 | |||
343 | $subfolder_flag = 0; |
||
344 | $recursive = false; |
||
345 | if (isset($action["subfolders"]) && $action["subfolders"] == "true") { |
||
346 | $recursive = true; |
||
347 | $subfolder_flag = RECURSIVE_SEARCH; |
||
348 | } |
||
349 | |||
350 | if (!is_array($entryid)) { |
||
351 | $entryids = [$entryid]; |
||
352 | } |
||
353 | else { |
||
354 | $entryids = $entryid; |
||
355 | } |
||
356 | |||
357 | $searchFolderEntryId = $this->sessionData['searchFolderEntryId']; |
||
358 | |||
359 | // check if searchcriteria has changed |
||
360 | $restrictionCheck = md5(serialize($this->restriction) . $searchFolderEntryId . $subfolder_flag); |
||
361 | |||
362 | // check if there is need to set searchcriteria again |
||
363 | if (!isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck != $this->sessionData['searchCriteriaCheck']) { |
||
364 | if (!empty($this->sessionData['searchOriginalEntryids']) && |
||
365 | isset($action['entryid']) && |
||
366 | in_array($action['entryid'], $this->sessionData['searchOriginalEntryids']) |
||
367 | ) { |
||
368 | // get entryids of original folders, and use it to set new search criteria |
||
369 | $entryids = []; |
||
370 | $entryIdsCount = count($this->sessionData['searchOriginalEntryids']); |
||
371 | for ($index = 0; $index < $entryIdsCount; ++$index) { |
||
372 | $entryids[] = hex2bin((string) $this->sessionData['searchOriginalEntryids'][$index]); |
||
373 | } |
||
374 | } |
||
375 | else { |
||
376 | // store entryids of original folders, so that can be used for re-setting the search criteria if needed |
||
377 | $this->sessionData['searchOriginalEntryids'] = []; |
||
378 | for ($index = 0, $len = count($entryids); $index < $len; ++$index) { |
||
379 | $this->sessionData['searchOriginalEntryids'][] = bin2hex($entryids[$index]); |
||
380 | } |
||
381 | } |
||
382 | // we never start the search folder because we will populate the search folder by ourselves |
||
383 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, $entryids, $subfolder_flag | STOP_SEARCH); |
||
384 | $this->sessionData['searchCriteriaCheck'] = $restrictionCheck; |
||
385 | } |
||
386 | |||
387 | if (isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck == $this->sessionData['searchCriteriaCheck']) { |
||
388 | $folderEntryid = bin2hex($entryid); |
||
389 | if ($this->sessionData['searchOriginalEntryids'][0] !== $folderEntryid) { |
||
390 | $this->sessionData['searchOriginalEntryids'][0] = $folderEntryid; |
||
391 | // we never start the search folder because we will populate the search folder by ourselves |
||
392 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, [$entryid], $subfolder_flag | STOP_SEARCH); |
||
393 | } |
||
394 | } |
||
395 | |||
396 | // Sort |
||
397 | $this->parseSortOrder($action); |
||
398 | // Initialize search patterns with default values |
||
399 | $search_patterns = array_fill_keys( |
||
400 | ['sender', 'sending', 'recipients', |
||
401 | 'subject', 'content', 'attachments', 'others', 'message_classes', |
||
402 | 'date_start', 'date_end', 'unread', 'has_attachments', 'categories'], |
||
403 | null |
||
404 | ); |
||
405 | $this->parsePatterns($this->restriction, $search_patterns); |
||
406 | if (isset($search_patterns['message_classes']) && |
||
407 | count($search_patterns['message_classes']) >= 7) { |
||
408 | $search_patterns['message_classes'] = null; |
||
409 | } |
||
410 | |||
411 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_DELEGATE_GUID) { |
||
412 | $eidObj = $GLOBALS["entryid"]->createMsgStoreEntryIdObj(hex2bin((string) $action['store_entryid'])); |
||
413 | $username = $eidObj['ServerShortname']; |
||
414 | $session = $GLOBALS["mapisession"]->getSession(); |
||
415 | |||
416 | if ($username) { |
||
417 | $indexDB = new IndexSqlite($username, $session, $store); |
||
418 | } |
||
419 | } |
||
420 | else { |
||
421 | $indexDB = new IndexSqlite(); |
||
422 | } |
||
423 | |||
424 | $search_result = $indexDB->search(hex2bin((string) $searchFolderEntryId), $search_patterns, $entryid, $recursive); |
||
425 | // Use the query search if search in index fails or is not available. |
||
426 | if ($search_result == false) { |
||
427 | // Search in the inbox instead of Top of Information Store |
||
428 | if (isset($store_props[PR_IPM_SUBTREE_ENTRYID]) && |
||
429 | $GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex((string) $store_props[PR_IPM_SUBTREE_ENTRYID]))) { |
||
430 | $inbox = mapi_msgstore_getreceivefolder($store); |
||
431 | $inboxProps = mapi_getprops($inbox, [PR_ENTRYID]); |
||
432 | $entryid = $inboxProps[PR_ENTRYID]; |
||
433 | } |
||
434 | |||
435 | return parent::messageList($store, $entryid, $action, "search"); |
||
436 | } |
||
437 | |||
438 | unset($action["restriction"]); |
||
439 | |||
440 | // Get the table and merge the arrays |
||
441 | $table = $GLOBALS["operations"]->getTable($store, hex2bin((string) $searchFolderEntryId), $this->properties, $this->sort, $this->start); |
||
442 | // Create the data array, which will be send back to the client |
||
443 | $data = []; |
||
444 | $data = array_merge($data, $table); |
||
445 | |||
446 | $this->getDelegateFolderInfo($store); |
||
447 | $data = $this->filterPrivateItems($data); |
||
448 | |||
449 | // remember which entryid's are send to the client |
||
450 | $searchResults = []; |
||
451 | foreach ($table["item"] as $item) { |
||
452 | // store entryid => last_modification_time mapping |
||
453 | $searchResults[$item["entryid"]] = $item["props"]["last_modification_time"]; |
||
454 | } |
||
455 | |||
456 | // store search results into session data |
||
457 | if (!isset($this->sessionData['searchResults'])) { |
||
458 | $this->sessionData['searchResults'] = []; |
||
459 | } |
||
460 | $this->sessionData['searchResults'][$searchFolderEntryId] = $searchResults; |
||
461 | |||
462 | $result = mapi_folder_getsearchcriteria($searchFolder); |
||
463 | |||
464 | $data["search_meta"] = []; |
||
465 | $data["search_meta"]["searchfolder_entryid"] = $searchFolderEntryId; |
||
466 | $data["search_meta"]["search_store_entryid"] = $action["store_entryid"]; |
||
467 | $data["search_meta"]["searchstate"] = $result["searchstate"]; |
||
468 | $data["search_meta"]["results"] = count($searchResults); |
||
469 | |||
470 | // Reopen the search folder, because otherwise the suggestion property will |
||
471 | // not have been updated |
||
472 | $searchFolder = $this->createSearchFolder($store, true); |
||
473 | $storeProps = mapi_getprops($searchFolder, [PR_EC_SUGGESTION]); |
||
474 | if (isset($storeProps[PR_EC_SUGGESTION])) { |
||
475 | $data["search_meta"]["suggestion"] = $storeProps[PR_EC_SUGGESTION]; |
||
476 | } |
||
477 | |||
478 | $this->addActionData("search", $data); |
||
479 | $GLOBALS["bus"]->addData($this->getResponseData()); |
||
480 | |||
481 | return true; |
||
482 | } |
||
484 |