/** * 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; } }