/**
  * Search one baptism by one similar name
  * 
  * @author Jonathan Sandoval <*****@*****.**>
  * @param  Baptism        $baptism      Pseudo-baptism with the data to search
  * @param  string         $operator     To search with 'or' or 'and'
  * @param  string         $order        The type of sort of the Baptism
  * @param  integer        $begin        The number of page to display the registry
  * @return Array[Baptism] $baptisms     Baptism objects with the similar name or null
  */
 static function advancedSearchBaptism($baptism = null, $operator = 'AND', $order = 'id', $begin = 0)
 {
     if ($baptism === null) {
         return null;
     }
     $tableBaptism = DatabaseManager::getNameTable('TABLE_BAPTISM');
     $tablePerson = DatabaseManager::getNameTable('TABLE_PERSON');
     $tableChurch = DatabaseManager::getNameTable('TABLE_CHURCH');
     $celebrationDate = $baptism->getCelebrationDate();
     $bornPlace = $baptism->getBornPlace();
     $bornDate = $baptism->getBornDate();
     $queryOwner = "(";
     $posibleOwner = $baptism->getIdOwner()[0];
     $queryFather = "(";
     $posibleFather = $baptism->getIdOwner()[1];
     $queryMother = "(";
     $posibleMother = $baptism->getIdOwner()[2];
     $queryChurch = "(";
     $posibleChurch = $baptism->getIdChurch();
     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 . ")";
     }
     if ($posibleFather !== NULL) {
         for ($i = 0; $i < sizeof($posibleFather) - 1; $i++) {
             $queryFather = $queryFather . $posibleFather[$i]->getId() . ",";
         }
         $queryFather = $queryFather . $posibleFather[sizeof($posibleFather) - 1]->getId() . ")";
         $queryFather = "((fa.id IN " . $queryFather . ") OR fa.id IS NULL)";
     }
     if ($posibleMother !== NULL) {
         for ($i = 0; $i < sizeof($posibleMother) - 1; $i++) {
             $queryMother = $queryMother . $posibleMother[$i]->getId() . ",";
         }
         $queryMother = $queryMother . $posibleMother[sizeof($posibleMother) - 1]->getId() . ")";
         $queryMother = "((mo.id IN " . $queryMother . ") OR mo.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 . ")";
     }
     if ($baptism->getId() == 0) {
         $id = '';
     } else {
         $id = $baptism->getId();
     }
     if ($baptism->getIdBookRegistry() == 0) {
         $idBookRegistry = '';
     } else {
         $idBookRegistry = $baptism->getIdBookRegistry()->getId();
     }
     $query = "SELECT b.* \r\n                        FROM {$tableBaptism} AS b LEFT JOIN {$tablePerson} AS o ON b.idOwner = o.id \r\n                        LEFT JOIN {$tablePerson} AS fa ON o.idFather = fa.id\r\n                        LEFT JOIN {$tablePerson} AS mo ON o.idMother = mo.id\r\n                        JOIN {$tableChurch} AS c  ON b.idChurch = c.id\r\n                        WHERE b.id               LIKE '%{$id}%'               {$operator}\r\n                              b.BaptismDate      LIKE '%{$celebrationDate}%'  {$operator}\r\n                              b.bornPlace        LIKE '%{$bornPlace}%'        {$operator}\r\n                              b.bornDate         LIKE '%{$bornDate}%'         {$operator}";
     //Join the Query with the posibiitation query
     if ($queryOwner != '(') {
         $query = $query . $queryOwner . " " . $operator . " ";
     } else {
         $query = $query . "(o.id IN ())" . $operator . " ";
     }
     if ($queryFather != '(') {
         $query = $query . $queryFather . " " . $operator . " ";
     } else {
         $query = $query . "(fa.id IN ())" . $operator . " ";
     }
     if ($queryMother != '(') {
         $query = $query . $queryMother . " " . $operator . " ";
     } else {
         $query = $query . "(mo.id IN ())" . $operator . " ";
     }
     if ($queryChurch != '(') {
         $query = $query . $queryChurch . " " . $operator . " ";
     } else {
         $query = $query . "(c.id IN ())" . $operator . " ";
     }
     if ($idBookRegistry !== NULL) {
         $query = $query . "b.idBookRegistry LIKE '%{$idBookRegistry}%'";
     } else {
         $query = $query . "b.idBookRegistry LIKE '%%'";
     }
     if ($order == 'nameChild') {
         $query = $query . " ORDER BY o.names";
     } else {
         if ($order == 'nameChurch') {
             $query = $query . " ORDER BY c.name";
         } else {
             $query = $query . " ORDER BY b.id DESC";
         }
     }
     $query = $query . " LIMIT " . strval($begin * 10) . ", 11 ";
     $arrayBaptisms = DatabaseManager::multiFetchAssoc($query);
     $baptisms = array();
     if ($arrayBaptisms !== NULL) {
         $i = 0;
         foreach ($arrayBaptisms as $baptism) {
             if ($i == 10) {
                 continue;
             }
             $baptisms[] = self::ArrayToBaptism($baptism);
             $i++;
         }
         return $baptisms;
     } else {
         return null;
     }
 }
     } else {
         echo "KO";
     }
 } else {
     if ($_POST["status"] === 'update') {
         ConfirmationManager::updateConfirmation($confirmation);
         echo "OK";
         if ($_POST["baptismId"] === '0') {
             if ($_POST["baptismChurch"] != "XXXXXXXXXX") {
                 $bookRegistry = new BaptismRegistry();
                 $bookRegistry->setBook($_POST["bookBookRegistryB"]);
                 $bookRegistry->setPage($_POST["pageBookRegistryB"]);
                 $bookRegistry->setNumber($_POST["numBookRegistryB"]);
                 BaptismManager::addBaptismRegistry($bookRegistry);
                 $bookRegistry = BaptismManager::getSingleBaptismRegistry('book', $_POST["bookBookRegistryB"], 'page', $_POST["pageBookRegistryB"], 'number', $_POST["numBookRegistryB"]);
                 $baptism = new Baptism('0', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL');
                 $baptism->setIdCivilRegistry('1');
                 $celb = DatabaseManager::singleDateToDatabaseDate($_POST["baptismDate"]);
                 $baptism->setCelebrationDate($celb);
                 $church = ChurchManager::getSingleChurch('name', $_POST["baptismChurch"]);
                 if ($church !== NULL) {
                     $baptism->setIdChurch($church->getId());
                     $baptism->setIdRector(RectorManager::getSingleRector('idActualChurch', $church->getId())->getId());
                 }
                 $baptism->setIdOwner($child->getId());
                 $baptism->setIdBookRegistry($bookRegistry->getId());
                 BaptismManager::addBaptism($baptism);
             }
         } else {
             $baptism = BaptismManager::getSingleBaptism('id', $_POST["baptismId"]);
             if ($baptism !== NULL) {
$numberPage = intval($_GET["page"]);
$sortType = $_GET["sort"];
$simpleKeyword = $_GET["keyword"];
$kid = $_GET["kid"];
if ($sortType == NULL || $sortType == '') {
    $sortType = 'id';
}
if ($numberPage === NULL || $numberPage < 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}
//Getting all registries
if ($simpleKeyword !== NULL) {
    $baptismRegistries = BaptismManager::simpleSearchBaptism($simpleKeyword, $sortType, $numberPage);
} else {
    if ($kid !== NULL) {
        $baptismSearch = new Baptism();
        $kcelebration = DatabaseManager::singleDateToDatabaseDate($_GET["kcelebration"]);
        $kbornp = $_GET["kbornp"];
        $kbornd = DatabaseManager::singleDateToDatabaseDate($_GET["kbornd"]);
        $knamec = $_GET["knamec"];
        $klastname1c = $_GET["klastname1c"];
        $klastname2c = $_GET["klastname2c"];
        $knamef = $_GET["knamef"];
        $klastname1f = $_GET["klastname1f"];
        $klastname2f = $_GET["klastname2f"];
        $knamem = $_GET["knamem"];
        $klastname1m = $_GET["klastname1m"];
        $klastname2m = $_GET["klastname2m"];
        $kchurch = $_GET["kchurch"];
        $kbook = $_GET["kbook"];
        $knumber = $_GET["knumber"];
require_once __DIR__ . "/../../../Backend/PersonManager.php";
if (!isset($_POST) || $_POST["idChild"] === NULL) {
    echo "KO";
    die;
}
$church = ChurchManager::getSingleChurch('name', $_POST["celebrationChurch"]);
$child = new Person();
if ($_POST["idChild"] !== '0') {
    $child = PersonManager::getSinglePerson('id', $_POST["idChild"]);
}
$child->setId($_POST["idChild"]);
$child->setNames($_POST["nameChild"]);
$child->setLastname1($_POST["lastname1Child"]);
$child->setLastname2($_POST["lastname2Child"]);
$child->setGender(substr($_POST["genderChild"], 0, 1));
$baptism = new Baptism();
$baptism->setId($_POST["idBaptism"]);
$celb = DatabaseManager::singleDateToDatabaseDate($_POST["celebrationDate"]);
$baptism->setCelebrationDate($celb);
$baptism->setBornDate(DatabaseManager::singleDateToDatabaseDate($_POST["bornDateChild"]));
$baptism->setBornPlace($_POST["bornPlaceChild"]);
$baptism->setIdChurch($church->getId());
$baptism->setIdRector($_POST["rectorId"]);
$legitimate = substr($_POST["legitimateChild"], 0, 1);
if ($legitimate === 'S' || $legitimate === 'Y') {
    $baptism->setLegitimate('Y');
} else {
    $baptism->setLegitimate('N');
}
//Data Process for the Father
$father = new Person();