<?php

// =====================================
// Export Planning/Zoning Facts to Excel
// =====================================

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Export_PZFactsToExcel extends Base {

    // ==================
    // Class Constructor.
    // ==================

    public function __construct() {

        set_error_handler(array($this, "displayError"));

        date_default_timezone_set(self::TIMEZONE);
    }

    // ===============
    // Export the Data
    // ===============

    public function export() {

        $subscription_serial = $_SESSION['subscription_serial'];
        $usersubscription_serial = $_SESSION['usersubscription_serial'];

        $subscription = (new Subscriptions())->getSubscription($subscription_serial);

        if ($subscription->count() === 0) {
            $this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
        }

        $userssubscription = (new Users())->getUserSubscription($usersubscription_serial);

        $user_subscription_start_date = $userssubscription->record->usersubscription_start_date;
        $user_subscription_end_date = $userssubscription->record->usersubscription_end_date;

        $planningzoningfacts = (new PlanningAndZoningFacts())->getPZFactsPage($user_subscription_start_date, $user_subscription_end_date);

        // Create the Spreadsheet

        $spreadsheet = new Spreadsheet();

        $spreadsheet->getProperties()->setCreator("DEC International")->setTitle("PlanningAndZoningFacts")->setSubject("PlanningAndZoningFacts")
                ->setDescription("PlanningAndZoningFacts")->setKeywords("PlanningAndZoningFacts")->setCategory("PlanningAndZoningFacts");

        // Create the Base Sheet.

        $spreadsheet->setActiveSheetIndex(0);
        $spreadsheet->getActiveSheet()->setTitle("PlanningAndZoningFacts");

        // Format the Cells and Create the Headings.

        $spreadsheet->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("J")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("K")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("L")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("M")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("N")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("O")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("P")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Q")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("R")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("S")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("T")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("U")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("V")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("W")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("X")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Y")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("Z")->setAutoSize(true);
        $spreadsheet->getActiveSheet()->getColumnDimension("AA")->setAutoSize(true);

        $spreadsheet->getActiveSheet()->getStyle("A1:AA1")->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle("A1:AA1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB("D3D3D3");

        $spreadsheet->setActiveSheetIndex(0)->setCellValue("A1", "PZ_ID")
                ->setCellValue("B1", "County")
                ->setCellValue("C1", "OwnerName")
                ->setCellValue("D1", "OwnerState")
                ->setCellValue("E1", "OwnerAddress")
                ->setCellValue("F1", "OwnerPhone")
                ->setCellValue("G1", "OwnerFax")
                ->setCellValue("H1", "ProfessionalName")
                ->setCellValue("I1", "ProfessionalState")
                ->setCellValue("J1", "ProfessionalAddress")
                ->setCellValue("K1", "ProfessionalPhone")
                ->setCellValue("L1", "ProfessionalFax")
                ->setCellValue("M1", "ProjectType")
                ->setCellValue("N1", "ProjectName")
                ->setCellValue("O1", "ProjectDescription")
                ->setCellValue("P1", "ProjectAddress")
                ->setCellValue("Q1", "ProjectCity")
                ->setCellValue("R1", "ActionCode")
                ->setCellValue("S1", "District")
                ->setCellValue("T1", "LandLot")
                ->setCellValue("U1", "Block")
                ->setCellValue("V1", "Parcel")
                ->setCellValue("W1", "Lot")
                ->setCellValue("X1", "ActionDate")
                ->setCellValue("Y1", "Acres")
                ->setCellValue("Z1", "Status")
                ->setCellValue("AA1", "EntryDate");

        $spreadsheet->getActiveSheet()->getStyle("A:A")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
        $spreadsheet->getActiveSheet()->getStyle("C:C")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("D:D")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("E:E")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("F:F")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("G:G")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("H:H")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("I:I")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("J:J")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("K:K")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("L:L")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("M:M")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("N:N")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("O:O")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("P:P")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Q:Q")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("R:R")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("S:S")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("T:T")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("U:U")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("V:V")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("W:W")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("X:X")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Y:Y")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("Z:Z")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        $spreadsheet->getActiveSheet()->getStyle("AA:AA")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);

        $spreadsheet->getActiveSheet()->freezePane("A2");

        // Create the Detail Rows.

        $row = 2;

        foreach ($planningzoningfacts as $planningzoningfact) {

            $spreadsheet->getActiveSheet()->setCellValue("A" . $row, (string) $planningzoningfact->pzfact_legacy_id);
            $spreadsheet->getActiveSheet()->setCellValue("B" . $row, (string) $planningzoningfact->pzfact_county_name_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("C" . $row, (string) $planningzoningfact->pzfact_owner_name);
            $spreadsheet->getActiveSheet()->setCellValue("D" . $row, (string) $planningzoningfact->pzfact_owner_state);
            $spreadsheet->getActiveSheet()->setCellValue("E" . $row, (string) $planningzoningfact->pzfact_owner_address);
            $spreadsheet->getActiveSheet()->setCellValue("F" . $row, $this->formatPhoneNumber((string) $planningzoningfact->pzfact_owner_phone));
            $spreadsheet->getActiveSheet()->setCellValue("G" . $row, $this->formatPhoneNumber((string) $planningzoningfact->pzfact_owner_fax));
            $spreadsheet->getActiveSheet()->setCellValue("H" . $row, (string) $planningzoningfact->pzfact_professional_name);
            $spreadsheet->getActiveSheet()->setCellValue("I" . $row, (string) $planningzoningfact->pzfact_professional_state);
            $spreadsheet->getActiveSheet()->setCellValue("J" . $row, (string) $planningzoningfact->pzfact_professional_address);
            $spreadsheet->getActiveSheet()->setCellValue("K" . $row, $this->formatPhoneNumber((string) $planningzoningfact->pzfact_professional_phone));
            $spreadsheet->getActiveSheet()->setCellValue("L" . $row, $this->formatPhoneNumber((string) $planningzoningfact->pzfact_professional_fax));
            $spreadsheet->getActiveSheet()->setCellValue("M" . $row, (string) $planningzoningfact->pzfact_project_type_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("N" . $row, (string) $planningzoningfact->pzfact_project_name);
            $spreadsheet->getActiveSheet()->setCellValue("O" . $row, (string) $planningzoningfact->pzfact_project_description);
            $spreadsheet->getActiveSheet()->setCellValue("P" . $row, (string) $planningzoningfact->pzfact_project_address);
            $spreadsheet->getActiveSheet()->setCellValue("Q" . $row, (string) $planningzoningfact->project_city_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("R" . $row, (string) $planningzoningfact->pzfact_action_code_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("S" . $row, (string) $planningzoningfact->pzfact_district);
            $spreadsheet->getActiveSheet()->setCellValue("T" . $row, (string) $planningzoningfact->pzfact_land_lot);
            $spreadsheet->getActiveSheet()->setCellValue("U" . $row, (string) $planningzoningfact->pzfact_block);
            $spreadsheet->getActiveSheet()->setCellValue("V" . $row, (string) $planningzoningfact->pzfact_parcel_number);
            $spreadsheet->getActiveSheet()->setCellValue("W" . $row, (string) $planningzoningfact->pzfact_lot);
            $spreadsheet->getActiveSheet()->setCellValue("X" . $row, (string) $planningzoningfact->pzfact_action_date_verbose);
            $spreadsheet->getActiveSheet()->setCellValue("Y" . $row, (string) $planningzoningfact->pzfact_acres);
            $spreadsheet->getActiveSheet()->setCellValue("Z" . $row, (string) $planningzoningfact->pzfact_status);
            $spreadsheet->getActiveSheet()->setCellValue("AA" . $row, (string) $planningzoningfact->pzfact_entry_date_verbose);

            $row++;
        }

        $spreadsheet->setActiveSheetIndex(0);

        $filename = str_replace(' ', '_', $subscription->record->subscription_full_title) . "_" . date("Y-m-d_h:i:s");

        // Download the spreadsheet

        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-disposition: attachment; filename={$filename}.xlsx");
        header("Cache-Control: max-age=0");

        $excelWriter = new Xlsx($spreadsheet);

        @ob_clean();

        $excelWriter->save('php://output');

        exit();
    }

    // ===============
    // Debug an Object
    // ===============

    public function debug($object = "") {

        echo "<pre>";
        print_r($object);
        exit();
    }
}

?>
