<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<!--
    CS 328 - Week 12 Lecture 1 - postback PHP that either generates a form for
        a user to enter information for a new department to be inserted,
        (THIS time only asking for new department's name and location),
        or responds to that submitted form by using OCI to request that
        a PL/SQL STORED *FUNCTION* add_dept_2 be executed to make a primary
        key for the new department and insert that new department,
        returning a status code telling whether insert succeeded or not;
        if function failed, this PHP generates a p element saying so,
        otherwise it uses oci_commit to request a commit of that function's
        insertion and generates a p element confirming the insertion

    assumes: PL/SQL stored *function* add_dept_2 has been created in your
        Oracle student database
    uses: hum_conn_no_login.php, make_dept_form_2.php, 328footer-plus-end.html,
          insert-dept.css

    by: Sharon Tuttle
    last modified: 2025-04-17

    you can run this using the URL:

    https://nrs-projects.humboldt.edu/~st10/s25cs328/328lect12-1/insert-dept-3.php
-->

<head>
    <title> Insert Department 3 </title>
    <meta charset="utf-8" />

    <?php
        // enable error reporting for now

        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        // get PHP functions called in the body

        require_once("hum_conn_no_login.php");
        require_once("make_dept_form_2.php");
    ?>
    
    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />

    <link href="insert-dept.css" type="text/css" rel="stylesheet" />    
</head>

<body>
    <?php

    // either generate a form to enter a new department's information,
    //     or respond by trying to insert the submitted form's requested
    //     new department

    if ($_SERVER["REQUEST_METHOD"] == "GET")
    {
        ?>
        <h1> Add a New Department </h1>
        <?php
        make_dept_form_2();
    }

    else
    {
        ?>
        <h1> New Department Status </h1>
        <?php
        // warily responding to form that has been submitted

        // grab the entered new dept info, stripping tags
        //    if there are any (because there shouldn't be...)
        //    and making any odd characters display-only

        $new_dept_name = htmlspecialchars(strip_tags($_POST['deptname']));
        $new_dept_loc = htmlspecialchars(strip_tags($_POST['deptloc']));

        $conn = hum_conn_no_login();

        // if reach here, connected successfully!

        // request that PL/SQL stored *function* add_dept_2 be called
        //
        // (note the needed bind variable to hold the function's return value!)

        $add_dept_str = 
            "BEGIN :add_status := add_dept_2(:new_dept_name, :new_dept_loc); END;";

        $add_dept_stmt = oci_parse($conn, $add_dept_str);

        // now "setting" bind variables for add_dept procedure call

         oci_bind_by_name($add_dept_stmt, ":new_dept_name", $new_dept_name);
         oci_bind_by_name($add_dept_stmt, ":new_dept_loc", $new_dept_loc);

        // BUT! for an "OUTPUT" bind variable:
        //    *   the oci_bind_by_name 3rd argument is the PHP
        //        variable you want to be SET by the call!
        //    *   and, a 4th argument is required: the maximum
        //        size of what may be put INTO that variable
        //        by this call; 
        // 
        //   in the particular case of add_dept_2: 
        //        since (I think??) PHP "sees" integer as a string,
        //        2 digits/characters is more than big enough,
        //        since function returns an integer < 10

        oci_bind_by_name($add_dept_stmt, ":add_status",
                         $add_status, 2);

        // now executing!

        oci_execute($add_dept_stmt, OCI_DEFAULT);

        // generate a paragraph with an appropriate error message if the
        //     insertion failed

        if ($add_status < 0)
        {
            ?>
            <p> SORRY, could not add new department: <?= $new_dept_name ?>. <br />
            <?php
            if ($add_status == -1)
            {
                ?>
                A department with that name already exists. </p>
                <?php
            }
            else if ($add_status == -2)
            {
                ?>
                Department names are limited to 15 characters. </p>
                <?php
            }
            else if ($add_status == -3)
            {
                ?>
                Department locations are limited to 15 characters. </p>
                <?php
            }
        }
        else
        {
            // if get here -- should have added department, happy to commit
            //     (since this IS considered the logical end to this
            //     application's transaction)

            oci_commit($conn);

            ?>
            <p> Added new department: <?= $new_dept_name ?> </p>
            <?php
        }

        // close statement and connection

        oci_free_statement($add_dept_stmt);
        oci_close($conn);

        // and add a useful link if want to add another department
        ?>
        <p> <a href="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>">
            Insert another department </a> </p>
        <?php
    }
    require_once("328footer-plus-end.html");
    ?>