| Conditions | 32 |
| Paths | 5792 |
| Total Lines | 190 |
| Code Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 226 | function search($store, $entryid, $action, $actionType) |
||
| 227 | { |
||
| 228 | $useSearchFolder = isset($action["use_searchfolder"]) ? $action["use_searchfolder"] : false; |
||
| 229 | if (!$useSearchFolder) { |
||
| 230 | /** |
||
| 231 | * store doesn't support search folders so we can't use this |
||
| 232 | * method instead we will pass restriction to messageList and |
||
| 233 | * it will give us the restricted results |
||
| 234 | */ |
||
| 235 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 236 | } |
||
| 237 | $store_props = mapi_getprops($store, array(PR_MDB_PROVIDER, PR_DEFAULT_STORE)); |
||
| 238 | if ($store_props[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID || |
||
| 239 | empty($store_props[PR_DEFAULT_STORE]) || !$store_props[PR_DEFAULT_STORE]){ |
||
| 240 | // public store or share store do not support search folders |
||
| 241 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 242 | } |
||
| 243 | if ($GLOBALS['entryid']->compareEntryIds(bin2hex($entryid), bin2hex(TodoList::getEntryId()))) { |
||
| 244 | // todo list do not need to perform full text index search |
||
| 245 | return parent::messageList($store, $entryid, $action, "list"); |
||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | $this->searchFolderList = true; // Set to indicate this is not the normal folder, but a search folder |
||
| 250 | $this->restriction = false; |
||
| 251 | |||
| 252 | |||
| 253 | // Parse Restriction |
||
| 254 | $this->parseRestriction($action); |
||
| 255 | if($this->restriction == false) { |
||
| 256 | // if error in creating restriction then send error to client |
||
| 257 | $errorInfo = array(); |
||
| 258 | $errorInfo["error_message"] = _("Error in search, please try again") . "."; |
||
| 259 | $errorInfo["original_error_message"] = "Error in parsing restrictions."; |
||
| 260 | |||
| 261 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 262 | } |
||
| 263 | |||
| 264 | $isSetSearchFolderEntryId = isset($action['search_folder_entryid']); |
||
| 265 | if($isSetSearchFolderEntryId) { |
||
| 266 | $this->sessionData['searchFolderEntryId'] = $action['search_folder_entryid']; |
||
| 267 | } |
||
| 268 | |||
| 269 | if (isset($action['forceCreateSearchFolder']) && $action['forceCreateSearchFolder']) { |
||
| 270 | $isSetSearchFolderEntryId = false; |
||
| 271 | } |
||
| 272 | |||
| 273 | // create or open search folder |
||
| 274 | $searchFolder = $this->createSearchFolder($store, $isSetSearchFolderEntryId); |
||
| 275 | if ($searchFolder === false) { |
||
| 276 | // if error in creating search folder then send error to client |
||
| 277 | $errorInfo = array(); |
||
| 278 | switch(mapi_last_hresult()) { |
||
| 279 | case MAPI_E_NO_ACCESS: |
||
| 280 | $errorInfo["error_message"] = _("Unable to perform search query, no permissions to create search folder."); |
||
| 281 | break; |
||
| 282 | case MAPI_E_NOT_FOUND: |
||
| 283 | $errorInfo["error_message"] = _("Unable to perform search query, search folder not found."); |
||
| 284 | break; |
||
| 285 | default: |
||
| 286 | $errorInfo["error_message"] = _("Unable to perform search query, store might not support searching."); |
||
| 287 | } |
||
| 288 | |||
| 289 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
| 290 | |||
| 291 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 292 | } |
||
| 293 | |||
| 294 | $subfolder_flag = 0; |
||
| 295 | $recursive = false; |
||
| 296 | if (isset($action["subfolders"]) && $action["subfolders"] == "true") { |
||
| 297 | $recursive = true; |
||
| 298 | $subfolder_flag = RECURSIVE_SEARCH; |
||
| 299 | } |
||
| 300 | |||
| 301 | if(!is_array($entryid)) { |
||
| 302 | $entryids = array($entryid); |
||
| 303 | } else { |
||
| 304 | $entryids = $entryid; |
||
| 305 | } |
||
| 306 | |||
| 307 | $searchFolderEntryId = $this->sessionData['searchFolderEntryId']; |
||
| 308 | |||
| 309 | // check if searchcriteria has changed |
||
| 310 | $restrictionCheck = md5(serialize($this->restriction) . $searchFolderEntryId . $subfolder_flag); |
||
| 311 | |||
| 312 | // check if there is need to set searchcriteria again |
||
| 313 | if(!isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck != $this->sessionData['searchCriteriaCheck']) { |
||
| 314 | if (!empty($this->sessionData['searchOriginalEntryids'])) { |
||
| 315 | // get entryids of original folders, and use it to set new search criteria |
||
| 316 | $entryids = Array(); |
||
| 317 | for($index = 0; $index < count($this->sessionData['searchOriginalEntryids']); $index++) { |
||
| 318 | $entryids[] = hex2bin($this->sessionData['searchOriginalEntryids'][$index]); |
||
| 319 | } |
||
| 320 | } else { |
||
| 321 | // store entryids of original folders, so that can be used for re-setting the search criteria if needed |
||
| 322 | $this->sessionData['searchOriginalEntryids'] = Array(); |
||
| 323 | for($index = 0, $len = count($entryids); $index < $len; $index++) { |
||
| 324 | $this->sessionData['searchOriginalEntryids'][] = bin2hex($entryids[$index]); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 328 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, $entryids, $subfolder_flag|STOP_SEARCH); |
||
| 329 | $this->sessionData['searchCriteriaCheck'] = $restrictionCheck; |
||
| 330 | } |
||
| 331 | |||
| 332 | if(isset($this->sessionData['searchCriteriaCheck']) || $restrictionCheck == $this->sessionData['searchCriteriaCheck']) { |
||
| 333 | $folderEntryid = bin2hex($entryid); |
||
| 334 | if($this->sessionData['searchOriginalEntryids'][0] !== $folderEntryid) { |
||
| 335 | $this->sessionData['searchOriginalEntryids'][0] = $folderEntryid; |
||
| 336 | // we never start the search folder because we will populate the search folder by ourselves |
||
| 337 | mapi_folder_setsearchcriteria($searchFolder, $this->restriction, array($entryid), $subfolder_flag|STOP_SEARCH); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | unset($action["restriction"]); |
||
| 342 | |||
| 343 | // Sort |
||
| 344 | $this->parseSortOrder($action); |
||
| 345 | |||
| 346 | $search_patterns = array(); |
||
| 347 | AdvancedSearchListModule::parsePatterns($this->restriction, $search_patterns); |
||
| 348 | if (isset($search_patterns['message_classes']) && |
||
| 349 | count($search_patterns['message_classes']) >= 7) { |
||
| 350 | unset($search_patterns['message_classes']); |
||
| 351 | } |
||
| 352 | |||
| 353 | $indexDB = new IndexSqlite(); |
||
| 354 | if (!$indexDB->load()) { |
||
| 355 | // if error in creating search folder then send error to client |
||
| 356 | $errorInfo = array(); |
||
| 357 | $errorInfo["error_message"] = _("Unable to perform search query, store might not support searching."); |
||
| 358 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
| 359 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 360 | } |
||
| 361 | $search_result = $indexDB->search(hex2bin($searchFolderEntryId), $search_patterns['sender'], $search_patterns['from'], |
||
| 362 | $search_patterns['recipients'], $search_patterns['subject'], $search_patterns['content'], |
||
| 363 | $search_patterns['attachments'], $search_patterns['others'], $entryid, $recursive, |
||
| 364 | $search_patterns['message_classes'], $search_patterns['date_start'], $search_patterns['date_end'], |
||
| 365 | $search_patterns['unread'], $search_patterns['has_attachments']); |
||
| 366 | if (false == $search_result) { |
||
| 367 | // if error in creating search folder then send error to client |
||
| 368 | $errorInfo = array(); |
||
| 369 | $errorInfo["error_message"] = _("Unable to perform search query, search folder not found."); |
||
| 370 | $errorInfo["original_error_message"] = _("Error in creating search folder."); |
||
| 371 | return $this->sendSearchErrorToClient($store, $entryid, $action, $errorInfo); |
||
| 372 | } |
||
| 373 | |||
| 374 | // Get the table and merge the arrays |
||
| 375 | $table = $GLOBALS["operations"]->getTable($store, hex2bin($searchFolderEntryId), $this->properties, $this->sort, $this->start); |
||
| 376 | // Create the data array, which will be send back to the client |
||
| 377 | $data = array(); |
||
| 378 | $data = array_merge($data, $table); |
||
| 379 | |||
| 380 | $this->getDelegateFolderInfo($store); |
||
| 381 | $data = $this->filterPrivateItems($data); |
||
| 382 | |||
| 383 | // remember which entryid's are send to the client |
||
| 384 | $searchResults = array(); |
||
| 385 | foreach($table["item"] as $item) { |
||
| 386 | // store entryid => last_modification_time mapping |
||
| 387 | $searchResults[$item["entryid"]] = $item["props"]["last_modification_time"]; |
||
| 388 | } |
||
| 389 | |||
| 390 | // store search results into session data |
||
| 391 | if(!isset($this->sessionData['searchResults'])) { |
||
| 392 | $this->sessionData['searchResults'] = array(); |
||
| 393 | } |
||
| 394 | $this->sessionData['searchResults'][$searchFolderEntryId] = $searchResults; |
||
| 395 | |||
| 396 | $result = mapi_folder_getsearchcriteria($searchFolder); |
||
| 397 | |||
| 398 | $data["search_meta"] = array(); |
||
| 399 | $data["search_meta"]["searchfolder_entryid"] = $searchFolderEntryId; |
||
| 400 | $data["search_meta"]["search_store_entryid"] = $action["store_entryid"]; |
||
| 401 | $data["search_meta"]["searchstate"] = $result["searchstate"]; |
||
| 402 | $data["search_meta"]["results"] = count($searchResults); |
||
| 403 | |||
| 404 | // Reopen the search folder, because otherwise the suggestion property will |
||
| 405 | // not have been updated |
||
| 406 | $searchFolder = $this->createSearchFolder($store, true); |
||
| 407 | $storeProps = mapi_getprops($searchFolder, array(PR_EC_SUGGESTION)); |
||
| 408 | if ( isset($storeProps[PR_EC_SUGGESTION]) ){ |
||
| 409 | $data["search_meta"]["suggestion"] = $storeProps[PR_EC_SUGGESTION]; |
||
| 410 | } |
||
| 411 | |||
| 412 | $this->addActionData("search", $data); |
||
| 413 | $GLOBALS["bus"]->addData($this->getResponseData()); |
||
| 414 | |||
| 415 | return true; |
||
| 416 | |||
| 421 |