/**
  * Search one marriage by one similar name
  * 
  * @author Jonathan Sandoval <*****@*****.**>
  * @param  Marriage        $marriage    Pseudo-marriage with the data to search
  * @param  string         $operator     To search with 'or' or 'and'
  * @param  string         $order        The type of sort of the Marriage
  * @param  integer        $begin        The number of page to display the registry
  * @return Array[Marriage] $marriages   Marriage objects with the similar name or null
  */
 static function advancedSearchMarriage($marriage = null, $operator = 'AND', $order = 'id', $begin = 0)
 {
     if ($marriage === null) {
         return null;
     }
     $tableMarriage = DatabaseManager::getNameTable('TABLE_MARRIAGE');
     $tablePerson = DatabaseManager::getNameTable('TABLE_PERSON');
     $tableChurch = DatabaseManager::getNameTable('TABLE_CHURCH');
     $celebrationDate = $marriage->getCelebrationDate();
     $queryBoy = "(";
     $posibleBoy = $marriage->getIdBoyfriend();
     $queryGirl = "(";
     $posibleGirl = $marriage->getIdGirlfriend();
     $queryChurch = "(";
     $posibleChurch = $marriage->getIdChurchMarriage();
     if ($posibleBoy !== NULL) {
         for ($i = 0; $i < sizeof($posibleBoy) - 1; $i++) {
             $queryBoy = $queryBoy . $posibleBoy[$i]->getId() . ",";
         }
         $queryBoy = $queryBoy . $posibleBoy[sizeof($posibleBoy) - 1]->getId() . ")";
         $queryBoy = "((b.id IN " . $queryBoy . ") OR b.id IS NULL)";
     }
     if ($posibleGirl !== NULL) {
         for ($i = 0; $i < sizeof($posibleGirl) - 1; $i++) {
             $queryGirl = $queryGirl . $posibleGirl[$i]->getId() . ",";
         }
         $queryGirl = $queryGirl . $posibleGirl[sizeof($posibleGirl) - 1]->getId() . ")";
         $queryGirl = "((g.id IN " . $queryGirl . ") OR g.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 ($marriage->getId() == 0) {
         $id = '';
     } else {
         $id = $marriage->getId();
     }
     if ($marriage->getIdBookRegistry() == 0) {
         $idBookRegistry = '';
     } else {
         $idBookRegistry = $marriage->getIdBookRegistry()->getId();
     }
     $query = "SELECT m.* \r\n                        FROM {$tableMarriage} AS m \r\n                        LEFT JOIN {$tablePerson} AS b ON m.idBoyfriend = b.id\r\n                        LEFT JOIN {$tablePerson} AS g ON m.idGirlfriend = g.id\r\n                        JOIN {$tableChurch} AS c  ON m.idChurchMarriage = c.id\r\n                        WHERE m.id               LIKE '%{$id}%'               {$operator}\r\n                              m.marriageDate     LIKE '%{$celebrationDate}%'  {$operator} ";
     //Join the Query with the posibiitation query
     if ($queryBoy != '(') {
         $query = $query . $queryBoy . " " . $operator . " ";
     } else {
         $query = $query . "(b.id IN ())" . $operator . " ";
     }
     if ($queryGirl != '(') {
         $query = $query . $queryGirl . " " . $operator . " ";
     } else {
         $query = $query . "(g.id IN ())" . $operator . " ";
     }
     if ($queryChurch != '(') {
         $query = $query . $queryChurch . " " . $operator . " ";
     } else {
         $query = $query . "(c.id IN ())" . $operator . " ";
     }
     if ($idBookRegistry !== NULL) {
         $query = $query . "m.idMarriageRegistry LIKE '%{$idBookRegistry}%'";
     } else {
         $query = $query . "m.idMarriageRegistry LIKE '%%'";
     }
     if ($order == 'nameBoy') {
         $query = $query . " ORDER BY b.lastname1";
     } else {
         if ($order == 'nameGirl') {
             $query = $query . " ORDER BY g.lastname1";
         } else {
             if ($order == 'nameChurch') {
                 $query = $query . " ORDER BY c.name";
             } else {
                 $query = $query . " ORDER BY m.id DESC";
             }
         }
     }
     $query = $query . " LIMIT " . strval($begin * 10) . ", 11 ";
     $arrayMarriages = DatabaseManager::multiFetchAssoc($query);
     $marriages = array();
     if ($arrayMarriages !== NULL) {
         $i = 0;
         foreach ($arrayMarriages as $marriage) {
             if ($i == 10) {
                 continue;
             }
             $marriages[] = self::ArrayToMarriage($marriage);
             $i++;
         }
         return $marriages;
     } else {
         return null;
     }
 }