Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/generate_mistar_word.php

<?php

require_once 'vendor/autoload.php';

use PhpOffice\PhpWord\TemplateProcessor;

session_start();
@include 'include/config.php';

$plan_id = $_GET['plan_id'] ?? null;
if (!$plan_id) {
    die("Missing Plan ID");
}

// Load user data
$username = $_SESSION['username_school'] ?? null;
$fullname = 'N/A';
$immediate_supervisor = 'N/A';

if ($username) {
    $user_stmt = $conn->prepare("SELECT fullname, immediate_supervisor FROM information WHERE username = ?");
    $user_stmt->bind_param("s", $username);
    $user_stmt->execute();
    $user_result = $user_stmt->get_result();
    if ($user_result->num_rows > 0) {
        $user_info = $user_result->fetch_assoc();
        $fullname = strtoupper($user_info['fullname']);
        $immediate_supervisor = strtoupper($user_info['immediate_supervisor']);
    }
    $user_stmt->close();
}

// Get report data
$planStmt = $conn->prepare("SELECT * FROM mista_reports WHERE id = ?");
$planStmt->bind_param("i", $plan_id);
$planStmt->execute();
$planResult = $planStmt->get_result()->fetch_assoc();
$planStmt->close();

if (!$planResult) {
    die("Plan not found for ID $plan_id");
}

// Get activity data
$activitiesStmt = $conn->prepare("SELECT * FROM mista_monitoring_reports WHERE plan_id = ?");
$activitiesStmt->bind_param("i", $plan_id);
$activitiesStmt->execute();
$activitiesResult = $activitiesStmt->get_result();
$activitiesStmt->close();

if ($activitiesResult->num_rows === 0) {
    die("No activities found for this plan.");
}

// Load the template
$templatePath = 'C:\\xampp\\htdocs\\cid\\system\\templates\\mistar.docx';
if (!file_exists($templatePath)) {
    die("Template not found at $templatePath");
}

$templateProcessor = new TemplateProcessor($templatePath);

// Set header values
$templateProcessor->setValue('division', $planResult['division']);
$templateProcessor->setValue('section', $planResult['section']);
$templateProcessor->setValue('district', $planResult['district']);
$templateProcessor->setValue('month', $planResult['month']);
$templateProcessor->setValue('year', $planResult['school_year']);
$templateProcessor->setValue('fullname', $fullname);
$templateProcessor->setValue('immediate_supervisor', $immediate_supervisor);

// Set dates
$currentDate = date('F d, Y');
$templateProcessor->setValue('employee_date', $currentDate);
$templateProcessor->setValue('supervisor_date', $currentDate);

// Prepare rows
$activityRows = [];
while ($row = $activitiesResult->fetch_assoc()) {
    $activityRows[] = [
        'date' => $row['dates'],
        'school' => $row['school'],
        'name_of_school_head' => $row['name_of_school_head'],
        'activities_undertaken' => $row['activities_undertaken'],
        'findings' => $row['findings'],
        'technical_assistance_provided' => $row['technical_assistance_provided'],
        'agreement' => $row['agreement'],
        'school_head_signature' => $row['school_head_signature']
    ];
}

// Clone table rows
$templateProcessor->cloneRowAndSetValues('date', $activityRows);

// Output
$filename = "MISTAR_Report_{$plan_id}.docx";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
$templateProcessor->saveAs("php://output");
exit();
?>