/**
  * Search one rector by one similar name
  * 
  * @author Jonathan Sandoval <*****@*****.**>
  * @param  Rector        $rector      pseudo-rector with the data to search
  * @param  string        $operator    To search with 'OR' or 'AND'
  * @param  string        $order       The type of sort of the Rector
  * @param  integer       $begin       The number of page to display the registry
  * @return Array[Rector] $rectors     Rector objects with the similar name or null
  */
 static function advancedSearchRector($rector = null, $operator = 'AND', $order = 'id', $begin = 0)
 {
     if ($rector === null) {
         return null;
     }
     $tableRector = DatabaseManager::getNameTable('TABLE_RECTOR');
     $tablePerson = DatabaseManager::getNameTable('TABLE_PERSON');
     $tableChurch = DatabaseManager::getNameTable('TABLE_CHURCH');
     $rector->getId() == 0 ? $id = '' : ($id = $rector->getId());
     $type = $rector->getType();
     $status = $rector->getStatus();
     $position = $rector->getPosition();
     $queryOwner = "(";
     $posibleOwner = $rector->getIdPerson();
     $queryChurch = "(";
     $posibleChurch = $rector->getIdActualChurch();
     if ($posibleOwner !== NULL) {
         for ($i = 0; $i < sizeof($posibleOwner) - 1; $i++) {
             $queryOwner = $queryOwner . $posibleOwner[$i]->getId() . ",";
         }
         $queryOwner = $queryOwner . $posibleOwner[sizeof($posibleOwner) - 1]->getId() . ")";
         $queryOwner = "(o.id IN " . $queryOwner . " OR o.id IS NULL)";
     }
     if ($posibleChurch !== NULL) {
         for ($i = 0; $i < sizeof($posibleChurch) - 1; $i++) {
             $queryChurch = $queryChurch . $posibleChurch[$i]->getId() . ",";
         }
         $queryChurch = $queryChurch . $posibleChurch[sizeof($posibleChurch) - 1]->getId() . ")";
         $queryChurch = "(c.id IN " . $queryChurch . " OR c.id IS NULL)";
     }
     $query = "SELECT r.* \r\n                        FROM {$tableRector} AS r LEFT JOIN {$tablePerson} AS o ON r.idPerson = o.id \r\n                        JOIN {$tableChurch} AS c  ON r.idActualChurch = c.id\r\n                        WHERE r.id       LIKE '%{$id}%'       {$operator}\r\n                              r.type     LIKE '%{$type}%'     {$operator}\r\n                              r.status   LIKE '%{$status}%'   {$operator}\r\n                              r.position LIKE '%{$position}%' {$operator}";
     if ($queryOwner != '(') {
         $query = $query . $queryOwner . " " . $operator . " ";
     } else {
         $query = $query . "(o.id IN ())" . $operator . " ";
     }
     if ($queryChurch != '(') {
         $query = $query . $queryChurch . " ";
     } else {
         $query = $query . "(c.id IN ()) ";
     }
     if ($order == 'name') {
         $query = $query . " ORDER BY o.names";
     } else {
         if ($order == 'church') {
             $query = $query . " ORDER BY c.name";
         } else {
             $query = $query . " ORDER BY r.id";
         }
     }
     $query = $query . " LIMIT " . strval($begin * 10) . ", 11 ";
     $arrayRectors = DatabaseManager::multiFetchAssoc($query);
     $rectors = array();
     if ($arrayRectors !== NULL) {
         $i = 0;
         foreach ($arrayRectors as $rector) {
             if ($i == 10) {
                 continue;
             }
             $rectors[] = self::ArrayToRector($rector);
             $i++;
         }
         return $rectors;
     } else {
         return null;
     }
 }
<?php

require_once __DIR__ . "/../../../Backend/SessionManager.php";
require_once __DIR__ . "/../../../Backend/RectorManager.php";
require_once __DIR__ . "/../../../Backend/ChurchManager.php";
require_once __DIR__ . "/../../../Backend/PersonManager.php";
if (!isset($_GET) || $_GET["name"] === NULL) {
    echo "KO";
    die;
}
$status = $_GET["status"];
$rector = new Rector();
$person = PersonManager::getSinglePerson('names', $_GET["name"], 'lastname1', $_GET["lastname1"], 'lastname2', $_GET["lastname2"]);
if ($person === NULL) {
    $person = new Person(0, $_GET["name"], $_GET["lastname1"], $_GET["lastname2"]);
    PersonManager::addPerson($person);
    $person = PersonManager::getSinglePerson('names', $_GET["name"], 'lastname1', $_GET["lastname1"], 'lastname2', $_GET["lastname2"]);
}
$church = ChurchManager::getSingleChurch('name', $_GET["actualChurch"]);
$rector->setIdPerson($person->getId());
$rector->setType($_GET["type"]);
$rector->setPosition($_GET["position"]);
$rector->setIdActualChurch($church->getId());
if ($_GET["statusR"] === 'Activo' || $_GET["statusR"] === 'Active') {
    $rector->setStatus('A');
} else {
    $rector->setStatus('I');
}
if ($status === 'update') {
    $rector->setId($_GET["id"]);
    if (RectorManager::updateRector($rector)) {
}
//Getting all registries
if ($simpleKeyword !== NULL) {
    $rectorRegistries = RectorManager::searchRector($simpleKeyword, 'OR', $sortType, $numberPage);
} else {
    if ($kid !== NULL) {
        $rectorSearch = new Rector();
        $kid = $_GET["kid"];
        $kname = $_GET["kname"];
        $klastname1 = $_GET["klastname1"];
        $klastname2 = $_GET["klastname2"];
        $kchurch = $_GET["kchurch"];
        $ktype = $_GET["ktype"];
        $kstatus = $_GET["kstatus"];
        $kposition = $_GET["kposition"];
        $rector = new Rector();
        $rector->setId($kid);
        $rector->setType($ktype);
        $rector->setStatus($kstatus);
        $rector->setPosition($kposition);
        $posibleNames = PersonManager::searchPersonsByNames($kname, $klastname1, $klastname2, false);
        $rector->setIdPerson($posibleNames);
        $posibleChurch = ChurchManager::simpleSearchChurch($kchurch, 'id', -1);
        $rector->setIdActualChurch($posibleChurch);
        $rectorRegistries = RectorManager::advancedSearchRector($rector, 'AND', $sortType, $numberPage);
    } else {
        $rectorRegistries = RectorManager::getAllRectors($sortType, $numberPage);
    }
}
//Get the total of registries
$totalRegistries = DatabaseManager::getAffectedRows();