<?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["nameChurch"] === NULL) {
    die;
}
$church = ChurchManager::getSingleChurch('name', $_GET["nameChurch"]);
$rectors = RectorManager::getAllFormerRectors($church->getId());
$response = "";
if ($rectors !== NULL) {
    foreach ($rectors as $singleRector) {
        $person = PersonManager::getSinglePerson('id', $singleRector->getIdPerson());
        $nameRector = $person->getFullNameBeginName();
        $response = $response . "<li><a value='" . $singleRector->getId() . "' onclick='changeParent(\"" . $singleRector->getId() . "\", " . " \" " . $nameRector . " \")'>" . "{$nameRector}</a></li>";
    }
}
echo $response;
require_once __DIR__ . "/../../Backend/LanguageSupport.php";
SessionManager::validateUserInPage('addRelationChurchRector.php');
//Get File contest from template
$string = file_get_contents("template/AddRelationChurchRector.html");
//Remplace the nav
$string = str_replace("|NavBar|", SessionManager::getNavBar(), $string);
//Create a String of Church Options
$churchString = "";
$churchs = ChurchManager::getAllChurchs('name', -1);
foreach ($churchs as $singleChurch) {
    $churchString = $churchString . "<option> " . $singleChurch->getName() . "</option>\n";
}
$string = str_replace("|ChurchOption|", $churchString, $string);
//Create a String of Rector Options
$rectorString = "";
$rectors = RectorManager::getAllRectors('name');
foreach ($rectors as $singleRector) {
    $idRector = $singleRector->getId();
    $person = PersonManager::getSinglePerson('id', $idRector);
    $fullname = $person->getFullName();
    $rectorString = $rectorString . "<option lang='{$idRector}' id='{$fullname}'> " . $fullname . "</option>\n";
}
$string = str_replace("|RectorOption|", $rectorString, $string);
//Create Button contest
$saveButton = '<button type="button" class="btn btn-success" onclick=\'validateData("addRelationChurchRector.php", "' . SessionManager::getLastPage() . '")\'>
                      <img src="../icons/save.png" width="50px"><br>
                      <strong>^Save^</strong>
                   </button>';
$cancelButton = '<button type="button" class="btn btn-success" onclick=\'href("' . SessionManager::getLastPage() . '")\'>
                        <img src="../icons/delete.png" width="50px"><br>
                        <strong>^Cancel^</strong>
<?php

require_once __DIR__ . "/../../../Backend/SessionManager.php";
require_once __DIR__ . "/../../../Backend/RectorManager.php";
require_once __DIR__ . "/../../../Backend/ChurchManager.php";
if (!isset($_GET) || $_GET["idRector"] === NULL) {
    echo "KO";
} else {
    $rector = RectorManager::getSingleRector("id", $_GET["idRector"]);
    if ($rector === NULL) {
        echo "KO";
    } else {
        if (!isset($_SESSION) || $_SESSION["user_type"] === NULL) {
            echo "KO";
        } else {
            //Check for permision
            if ($_SESSION["user_type"] == 'A') {
                if (RectorManager::removeRector($rector)) {
                    echo "OK";
                } else {
                    echo "KO";
                }
            }
        }
    }
}
$string = str_replace("|NavBar|", SessionManager::getNavBar(), $string);
//Validate the URL
$numberPage = intval($_GET["page"]);
$sortType = $_GET["sort"];
$idChurch = $_GET["id"];
$church = ChurchManager::getSingleChurch('id', $idChurch);
if ($sortType == NULL || $sortType == '') {
    $sortType = 'id';
}
if ($numberPage === NULL || $numberPage < 0 || $idChurch === "" || $church === NULL) {
    echo "<script src='../JS/functions.js'></script><script>href('rectorMenu.php')</script>";
}
//Display the name of the church
$string = str_replace("|Church|", "«" . $church->getName() . "»", $string);
//Getting all registries
$rectorRegistries = RectorManager::getAllFormerRectors($idChurch, $sortType, $numberPage);
//Get the total of registries
$totalRegistries = DatabaseManager::getAffectedRows();
$affectedRegistries = DatabaseManager::registriesAffectedLastQuery();
if ($totalRegistries === 0 && $numberPage !== 0) {
    echo "<script src='../JS/functions.js'></script><script>href('rectorMenu.php')</script>";
}
$lastPage = floor($affectedRegistries / 10);
if ($affectedRegistries % 10 === 0) {
    $lastPage = floor($affectedRegistries / 10) - 1;
}
//Create contest for button next and prev
$nextButtonString = '<button type="button" 
                                 class="btn btn-warning"
                                 onclick="nextPage(\'true\')">
                        ^Next^</button>';
}
//Remplace the nav
$string = str_replace("|title|", 'Look Confirmation', $string);
$string = str_replace("|NavBar|", SessionManager::getNavBar(), $string);
//Get The Data of the Confirmation
$idConfirmation = $_GET["id"];
$confirmation = ConfirmationManager::getSingleConfirmation('id', $idConfirmation);
$ownerId = $confirmation->getIdOwner();
$owner = PersonManager::getSinglePerson('id', $ownerId);
$owner->getIdFather() !== NULL ? $fatherId = $owner->getIdFather() : ($fatherId = 0);
$owner->getIdMother() !== NULL ? $motherId = $owner->getIdMother() : ($motherId = 0);
$gender = $owner->getGender();
$churchId = $confirmation->getIdChurch();
$confirmationDate = DatabaseManager::databaseDateToSingleDate($confirmation->getCelebrationDate());
$idRector = $confirmation->getIdRector();
$objRector = RectorManager::getSingleRector('id', $idRector);
$objPerRect = PersonManager::getSinglePerson('id', $objRector->getIdPerson());
$nameRector = $objPerRect->getFullNameBeginName();
$idBookRegistry = $confirmation->getIdBookRegistry();
$bookRegistry = ConfirmationManager::getSingleConfirmationRegistry('id', $idBookRegistry);
$idGodFather = $confirmation->getIdGodFather();
$godMother = PersonManager::getSinglePerson('id', $idGodMother);
$godFather = PersonManager::getSinglePerson('id', $idGodFather);
if ($godFather === NULL) {
    $godFather = new Person();
}
if ($godMother === NULL) {
    $godMother = new Person();
}
//Create a String of options of Last Church
$churchString = "";
<?php

require_once __DIR__ . "/../../../Backend/SessionManager.php";
require_once __DIR__ . "/../../../Backend/RectorManager.php";
require_once __DIR__ . "/../../../Backend/PersonManager.php";
require_once __DIR__ . "/../../../Backend/ChurchManager.php";
if (!isset($_GET) || $_GET["rector"] === NULL) {
    echo "KO";
    die;
}
//Get The Objects
$church = ChurchManager::getSingleChurch('name', $_GET["church"]);
if (RectorManager::addFormerRectorChurch($_GET["rector"], $church->getId())) {
    echo "OK";
} else {
    echo "KO";
}
 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) {
         $celb = DatabaseManager::singleDateToDatabaseDate($_POST["baptismDate"]);
         $baptism->setCelebrationDate($celb);
         $church = ChurchManager::getSingleChurch('name', $_POST["baptismChurch"]);
         if ($church !== NULL) {
             $baptism->setIdChurch($church->getId());
         }
         BaptismManager::updateBaptism($baptism);
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)) {
        echo "OK";
    } else {
        echo "KO";
    }
} else {
    if ($status === 'insert') {
        if (RectorManager::addRector($rector)) {
            echo "OK";
        } else {
            echo "KO";
        }
    }
}
$string = file_get_contents("template/RectorChurchRelationMenu.html");
//Remplace the nav
$string = str_replace("|NavBar|", SessionManager::getNavBar(), $string);
//Validate the URL
$numberPage = intval($_GET["page"]);
$sortType = $_GET["sort"];
$idRector = $_GET["id"];
$rector = RectorManager::getSingleRector('id', $idRector);
if ($sortType == NULL || $sortType == '') {
    $sortType = 'id';
}
if ($numberPage === NULL || $numberPage < 0 || $idRector === "" || $rector === NULL) {
    echo "<script src='../JS/functions.js'></script><script>href('churchMenu.php')</script>";
}
//Getting all registries
$churchRegistries = RectorManager::getAllFormerChurchs($idRector, $sortType, $numberPage);
//Get the total of registries
$totalRegistries = DatabaseManager::getAffectedRows();
$affectedRegistries = DatabaseManager::registriesAffectedLastQuery();
$lastPage = floor($affectedRegistries / 10);
if ($affectedRegistries % 10 === 0) {
    $lastPage = floor($affectedRegistries / 10) - 1;
}
if ($totalRegistries === 0 && $numberPage !== 0) {
    echo "<script src='../JS/functions.js'></script><script>href('churchrMenu.php')</script>";
}
$person = $rector->getIdPerson();
$person = PersonManager::getSinglePerson('id', $person);
//Display the name of the church
$string = str_replace("|Rector|", "«" . $person->getNames() . " " . $person->getLastname1() . " " . $person->getLastname2() . "»", $string);
//Create contest for button next and prev
        $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();
$affectedRegistries = DatabaseManager::registriesAffectedLastQuery();
if ($totalRegistries === 0 && $numberPage !== 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}
$lastPage = floor($affectedRegistries / 10);
if ($affectedRegistries % 10 === 0) {
    $lastPage = floor($affectedRegistries / 10) - 1;
}
//Create contest for button next and prev
$nextButtonString = '<button type="button" 
                                 class="btn btn-warning"