| Conditions | 26 |
| Paths | 393 |
| Total Lines | 178 |
| Code Lines | 137 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 210 | private function prepareConditionsForSearchPhrases(string &$searchPhrase, array &$parameters): void |
||
| 211 | { |
||
| 212 | $parameters['conditions'] = ''; |
||
| 213 | |||
| 214 | // Search the linkedid, if we found it on the search string we will ignore all other parameters |
||
| 215 | if (preg_match_all("/mikopbx-\d+.\d+/", $searchPhrase, $matches) && count($matches[0]) === 1) { |
||
| 216 | $parameters['conditions'] = 'linkedid = :SearchPhrase:'; |
||
| 217 | $parameters['bind']['SearchPhrase'] = $matches[0][0]; |
||
| 218 | |||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | // Store the original search phrase for employee name search |
||
| 223 | $employeeNumbers = []; |
||
| 224 | |||
| 225 | // Search date ranges |
||
| 226 | if (preg_match_all("/\d{2}\/\d{2}\/\d{4}/", $searchPhrase, $matches)) { |
||
| 227 | if (count($matches[0]) === 1) { |
||
| 228 | $date = DateTime::createFromFormat('d/m/Y', $matches[0][0]); |
||
| 229 | $requestedDate = $date->format('Y-m-d'); |
||
| 230 | $tomorrowDate = $date->modify('+1 day')->format('Y-m-d'); |
||
| 231 | $parameters['conditions'] .= 'start BETWEEN :dateFromPhrase1: AND :dateFromPhrase2:'; |
||
| 232 | $parameters['bind']['dateFromPhrase1'] = $requestedDate; |
||
| 233 | $parameters['bind']['dateFromPhrase2'] = $tomorrowDate; |
||
| 234 | $searchPhrase = str_replace($matches[0][0], "", $searchPhrase); |
||
| 235 | } elseif (count($matches[0]) === 2) { |
||
| 236 | $parameters['conditions'] .= 'start BETWEEN :dateFromPhrase1: AND :dateFromPhrase2:'; |
||
| 237 | $date = DateTime::createFromFormat('d/m/Y', $matches[0][0]); |
||
| 238 | $requestedDate = $date->format('Y-m-d'); |
||
| 239 | $parameters['bind']['dateFromPhrase1'] = $requestedDate; |
||
| 240 | $date = DateTime::createFromFormat('d/m/Y', $matches[0][1]); |
||
| 241 | $tomorrowDate = $date->modify('+1 day')->format('Y-m-d'); |
||
| 242 | $parameters['bind']['dateFromPhrase2'] = $tomorrowDate; |
||
| 243 | $searchPhrase = str_replace( |
||
| 244 | [$matches[0][0], $matches[0][1]], |
||
| 245 | '', |
||
| 246 | $searchPhrase |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // Search phone numbers |
||
| 252 | $searchPhrase = trim(str_replace(['(', ')', '-', '+'], '', $searchPhrase)); |
||
| 253 | |||
| 254 | // Look for employee name in the search phrase |
||
| 255 | if (!empty($searchPhrase)) { |
||
| 256 | $searchPhrase = mb_strtolower($searchPhrase, 'UTF-8'); |
||
| 257 | // Search for employee by name and get their numbers |
||
| 258 | $extensionsParams = [ |
||
| 259 | 'conditions' => 'search_index LIKE :SearchPhrase:', |
||
| 260 | 'bind' => [ |
||
| 261 | 'SearchPhrase' => "%$searchPhrase%" |
||
| 262 | ], |
||
| 263 | 'columns' => 'number' |
||
| 264 | ]; |
||
| 265 | |||
| 266 | $extensionsResult = Extensions::find($extensionsParams); |
||
| 267 | foreach ($extensionsResult as $extension) { |
||
| 268 | $employeeNumbers[] = $extension->number; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | if (preg_match_all("/\d+/", $searchPhrase, $matches)) { |
||
| 274 | $needCloseAnd = false; |
||
| 275 | $extensionsLength = PbxSettings::getValueByKey(PbxSettings::PBX_INTERNAL_EXTENSION_LENGTH); |
||
| 276 | if ($parameters['conditions'] !== '') { |
||
| 277 | $parameters['conditions'] .= ' AND ('; |
||
| 278 | $needCloseAnd = true; |
||
| 279 | } |
||
| 280 | if (count($matches[0]) === 1) { |
||
| 281 | if ($extensionsLength == strlen($matches[0][0])) { |
||
| 282 | $parameters['conditions'] .= 'src_num = :SearchPhrase1: OR dst_num = :SearchPhrase2:'; |
||
| 283 | $parameters['bind']['SearchPhrase1'] = $matches[0][0]; |
||
| 284 | $parameters['bind']['SearchPhrase2'] = $matches[0][0]; |
||
| 285 | } else { |
||
| 286 | $seekNumber = substr($matches[0][0], -9); |
||
| 287 | $parameters['conditions'] .= 'src_num LIKE :SearchPhrase1: OR dst_num LIKE :SearchPhrase2: OR did LIKE :SearchPhrase3:'; |
||
| 288 | $parameters['bind']['SearchPhrase1'] = "%$seekNumber%"; |
||
| 289 | $parameters['bind']['SearchPhrase2'] = "%$seekNumber%"; |
||
| 290 | $parameters['bind']['SearchPhrase3'] = "%$seekNumber%"; |
||
| 291 | } |
||
| 292 | |||
| 293 | $searchPhrase = str_replace($matches[0][0], '', $searchPhrase); |
||
| 294 | } elseif (count($matches[0]) === 2) { |
||
| 295 | if ($extensionsLength == strlen($matches[0][0]) && $extensionsLength === strlen($matches[0][1])) { |
||
| 296 | $parameters['conditions'] .= '(src_num = :SearchPhrase1: AND dst_num = :SearchPhrase2:)'; |
||
| 297 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase3: AND dst_num = :SearchPhrase4:)'; |
||
| 298 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase5: AND did = :SearchPhrase6:)'; |
||
| 299 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase8: AND did = :SearchPhrase7:)'; |
||
| 300 | $parameters['bind']['SearchPhrase1'] = $matches[0][0]; |
||
| 301 | $parameters['bind']['SearchPhrase2'] = $matches[0][1]; |
||
| 302 | $parameters['bind']['SearchPhrase3'] = $matches[0][1]; |
||
| 303 | $parameters['bind']['SearchPhrase4'] = $matches[0][0]; |
||
| 304 | $parameters['bind']['SearchPhrase5'] = $matches[0][1]; |
||
| 305 | $parameters['bind']['SearchPhrase6'] = $matches[0][0]; |
||
| 306 | $parameters['bind']['SearchPhrase7'] = $matches[0][1]; |
||
| 307 | $parameters['bind']['SearchPhrase8'] = $matches[0][0]; |
||
| 308 | } elseif ($extensionsLength == strlen($matches[0][0]) && $extensionsLength !== strlen( |
||
| 309 | $matches[0][1] |
||
| 310 | )) { |
||
| 311 | $seekNumber = substr($matches[0][1], -9); |
||
| 312 | $parameters['conditions'] .= '(src_num = :SearchPhrase1: AND dst_num LIKE :SearchPhrase2:)'; |
||
| 313 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase3: AND dst_num = :SearchPhrase4:)'; |
||
| 314 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase5: AND did = :SearchPhrase6:)'; |
||
| 315 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase8: AND did LIKE :SearchPhrase7:)'; |
||
| 316 | $parameters['bind']['SearchPhrase1'] = $matches[0][0]; |
||
| 317 | $parameters['bind']['SearchPhrase2'] = "%$seekNumber%"; |
||
| 318 | $parameters['bind']['SearchPhrase3'] = "%$seekNumber%"; |
||
| 319 | $parameters['bind']['SearchPhrase4'] = $matches[0][0]; |
||
| 320 | $parameters['bind']['SearchPhrase5'] = "%$seekNumber%"; |
||
| 321 | $parameters['bind']['SearchPhrase6'] = $matches[0][0]; |
||
| 322 | $parameters['bind']['SearchPhrase7'] = "%$seekNumber%"; |
||
| 323 | $parameters['bind']['SearchPhrase8'] = $matches[0][0]; |
||
| 324 | } elseif ($extensionsLength !== strlen($matches[0][0]) && $extensionsLength === strlen( |
||
| 325 | $matches[0][1] |
||
| 326 | )) { |
||
| 327 | $seekNumber = substr($matches[0][0], -9); |
||
| 328 | $parameters['conditions'] .= '(src_num LIKE :SearchPhrase1: AND dst_num = :SearchPhrase2:)'; |
||
| 329 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase3: AND dst_num LIKE :SearchPhrase4:)'; |
||
| 330 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase5: AND did = :SearchPhrase6:)'; |
||
| 331 | $parameters['conditions'] .= ' OR (src_num = :SearchPhrase8: AND did LIKE :SearchPhrase7:)'; |
||
| 332 | $parameters['bind']['SearchPhrase1'] = "%$seekNumber%"; |
||
| 333 | $parameters['bind']['SearchPhrase2'] = $matches[0][1]; |
||
| 334 | $parameters['bind']['SearchPhrase3'] = $matches[0][1]; |
||
| 335 | $parameters['bind']['SearchPhrase4'] = "%$seekNumber%"; |
||
| 336 | $parameters['bind']['SearchPhrase5'] = "%$seekNumber%"; |
||
| 337 | $parameters['bind']['SearchPhrase6'] = $matches[0][1]; |
||
| 338 | $parameters['bind']['SearchPhrase7'] = "%$seekNumber%"; |
||
| 339 | $parameters['bind']['SearchPhrase8'] = $matches[0][1]; |
||
| 340 | } else { |
||
| 341 | $seekNumber0 = substr($matches[0][0], -9); |
||
| 342 | $seekNumber1 = substr($matches[0][1], -9); |
||
| 343 | $parameters['conditions'] .= '(src_num LIKE :SearchPhrase1: AND dst_num LIKE :SearchPhrase2:)'; |
||
| 344 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase3: AND dst_num LIKE :SearchPhrase4:)'; |
||
| 345 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase5: AND did LIKE :SearchPhrase6:)'; |
||
| 346 | $parameters['conditions'] .= ' OR (src_num LIKE :SearchPhrase7: AND did LIKE :SearchPhrase8:)'; |
||
| 347 | $parameters['bind']['SearchPhrase1'] = "%$seekNumber0%"; |
||
| 348 | $parameters['bind']['SearchPhrase2'] = "%$seekNumber1%"; |
||
| 349 | $parameters['bind']['SearchPhrase3'] = "%$seekNumber1%"; |
||
| 350 | $parameters['bind']['SearchPhrase4'] = "%$seekNumber0%"; |
||
| 351 | $parameters['bind']['SearchPhrase5'] = "%$seekNumber0%"; |
||
| 352 | $parameters['bind']['SearchPhrase6'] = "%$seekNumber1%"; |
||
| 353 | $parameters['bind']['SearchPhrase7'] = "%$seekNumber1%"; |
||
| 354 | $parameters['bind']['SearchPhrase8'] = "%$seekNumber0%"; |
||
| 355 | } |
||
| 356 | $searchPhrase = str_replace([$matches[0][0], $matches[0][1]], '', $searchPhrase); |
||
| 357 | } elseif (count($matches[0]) > 2) { |
||
| 358 | $searchPhrase = str_replace([' ', ' '], '', $searchPhrase); |
||
| 359 | $parameters['conditions'] .= 'src_num = :SearchPhrase1: OR dst_num = :SearchPhrase2:'; |
||
| 360 | $parameters['bind']['SearchPhrase1'] = $searchPhrase; |
||
| 361 | $parameters['bind']['SearchPhrase2'] = $searchPhrase; |
||
| 362 | } |
||
| 363 | if ($needCloseAnd) { |
||
| 364 | $parameters['conditions'] .= ')'; |
||
| 365 | } |
||
| 366 | } elseif (count($employeeNumbers) > 0) { |
||
| 367 | // If no phone numbers were found in the search phrase but employee numbers were found |
||
| 368 | $needCloseAnd = false; |
||
| 369 | if ($parameters['conditions'] !== '') { |
||
| 370 | $parameters['conditions'] .= ' AND ('; |
||
| 371 | $needCloseAnd = true; |
||
| 372 | } |
||
| 373 | |||
| 374 | // Add conditions for each employee number |
||
| 375 | $employeeConditions = []; |
||
| 376 | foreach ($employeeNumbers as $index => $number) { |
||
| 377 | $employeeConditions[] = "src_num = :EmployeeNum{$index}src: OR dst_num = :EmployeeNum{$index}dst:"; |
||
| 378 | $parameters['bind']["EmployeeNum{$index}src"] = $number; |
||
| 379 | $parameters['bind']["EmployeeNum{$index}dst"] = $number; |
||
| 380 | } |
||
| 381 | |||
| 382 | if (!empty($employeeConditions)) { |
||
| 383 | $parameters['conditions'] .= '((' . implode(' OR ', $employeeConditions) . ') AND disposition = "ANSWERED")'; |
||
| 384 | } |
||
| 385 | |||
| 386 | if ($needCloseAnd) { |
||
| 387 | $parameters['conditions'] .= ')'; |
||
| 388 | } |
||
| 407 |