У меня есть блок if / else, охватывающий более 2000 строк для расширенного поиска, который, я чувствую, может быть серьезно сокращен
Вот небольшой пример больших операторов if / else
if($isSearch){
//Is search
if($isWordSearch){
//Is word search
if($isSubjectSearch){
//Is subject search
if($isDepartmentSearch){
//Is department Search
if($isOperatorSearch){
//Is operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,operator,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("user_id","=",$search_customer)
->count_all();
}
}else{
//Is not customer search
if($search_status == "Open"){
//Is word,subject,department,operator,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,operator,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,operator,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("operator_id","=",$search_operator)
->count_all();
}
}
}else{
//Is not operator search
if($isCustomerSearch){
//Is customer search
if($search_status == "Open"){
//Is word,subject,department,customer,open
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","NOT IN",array("50","60"))
->count_all();
}elseif($search_status == "Closed"){
//Is word,subject,department,customer,closed
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->and_where("status_id","IN",array("50","60"))
->count_all();
}else{
//Is word,subject,department,customer,all
$tickets_row = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
$tickets_row_count = $support_tickets_model
->where("subject","LIKE","%$search_text%")
->and_where("___dep_id","=",$search_department)
->and_where("user_id","=",$search_customer)
->count_all();
}
}
Что здесь происходит, я проверяю все возможные комбинации поисков, которые могут произойти
Ищем ли мы word:subject:operator:customer:open
или мы ищем word:subject:operator:customer:closed
Поэтому я записал каждую возможную комбинацию
До этого я пытался сделать это более динамичным c и оптимизировать, пытаясь
<?php
$tickets_row = $support_tickets_model;
if(true){
$tickets_row->where("subject","LIKE","%$search_text%");
}
$tickets_row
->limit($items_per_page)
->offset($offset)
->order_by("status_id","asc")
->order_by("update_date","asc")
->find_all();
?>
Это просто приводит к тому, что $tickets_row
будет нулевым
Каков идеальный способ уменьшить это в поддерживаемый формат?