Exemple #1
0
     if ($query_upd) {
         $result['success'] = true;
         $result['msg'] = 'Комментарий сохранён';
     }
     // Если комментарий клиентский, уведомить исполнителей
     if (!isAdmin($users, $admin_id)) {
         require_once "../vendor/autoload.php";
         // Twig инициализация
         Twig_Autoloader::register();
         $loader = new Twig_Loader_Filesystem("../templates");
         // Twig папка с шаблонами
         $twig = new Twig_Environment($loader, array("cache" => ""));
         // Twig no cache
         $c = getTicket($ticket_id);
         $c["action"] = "user_add_comment";
         $c["changer"] = getFullname($users, $admin_id);
         $performers = explode(",", $c["performers"]);
         foreach ($performers as $performer) {
             $mailto[$performer] = $users[$performer]["email"];
         }
         $c['comment'] = $text;
         $user_email_body = $twig->render("helpdesk/user_email.twig", $c);
         email($mailto, "Заявка #{$ticket_id}. \"" . $c["title"] . "\": новый комментарий", $user_email_body);
     }
     break;
 case "rateTicket":
     $ticket = check_string($_REQUEST['ticket'], 'digits');
     $rating = check_string($_REQUEST['rating'], 'digits');
     $result = rateTicket($admin_login["uid"], $ticket, $rating);
     break;
 case "changeTicketStatus":
function createRecurringBooking($conn, $username)
{
    try {
        echo "<p id='log'>";
        echo "Creating recurring booking...<br>";
        // Sets all variables from the form POST
        // set table, throw error if empty
        // The value provided is now of format
        // 'type,table', so we need to explode this to find table name
        $tablestring = $_POST["selectresource"];
        if (!$tablestring) {
            throw new Exception("Table cannot be empty<br>");
        }
        // if
        // explodes string using comma as delimiter
        $str_explode = explode(",", $tablestring);
        // table is second element in array
        $table = $str_explode[1];
        // table type is first element
        $type = $str_explode[0];
        // set and check day
        $inputday = $_POST["selectday"];
        if (!$inputday) {
            throw new Exception("Day cannot be empty<br>");
        }
        // if
        // format day into 3 characters, and capitalise first char
        // so 'monday' -> 'Mon', for comparisons later
        $day = ucfirst(substr($inputday, 0, 3));
        // Set and check periods
        // Since multiple periods may be selected, an array of available options is created
        $periodoptions = array("period1", "period2", "period3", "period4", "period5", "period6", "break", "lunch", "t9to10", "t10to11", "t11to12", "t12to13", "t13to14", "t14to15", "t15to16", "t16to17");
        // Foreach element in array
        // We check if the checkbox associated with it has been checked
        // If so, we push it to our new empty array called periodsselected
        $periodsselected = [];
        foreach ($periodoptions as $p) {
            if (isset($_POST[$p])) {
                array_push($periodsselected, $p);
            }
            // if
        }
        // foreach
        // Now we know what checkboxes have been selected
        // Set description, does not check because description is allowed to be blank
        $description = $_POST["description"];
        // If not booking whole day
        // type is set as either period/time/vehicle
        // each has their method of getting the numerical value of the period
        // for period based resources
        // Another array which will be used to store numerical values of
        // periods selected, e.g. [1, 3, 7]
        $periodsnumerical = [];
        foreach ($periodsselected as $p) {
            if ($type == "period") {
                // casts last character as an int, to get numerical value of 'n'
                $p = (int) substr($p, -1);
            } elseif ($type == "vehicle") {
                // use a switch to translate string to numerical value
                switch ($p) {
                    case "period1":
                        $p = 1;
                        break;
                    case "period2":
                        $p = 2;
                        break;
                    case "break":
                        $p = 3;
                        break;
                    case "period3":
                        $p = 4;
                        break;
                    case "period4":
                        $p = 5;
                        break;
                    case "lunch":
                        $p = 6;
                        break;
                    case "period5":
                        $p = 7;
                        break;
                    case "period6":
                        $p = 8;
                        break;
                    default:
                        $p = 1;
                        break;
                }
                // switch
            } elseif ($type == "time") {
                // use a switch to translate
                switch ($p) {
                    case "t9to10":
                        $p = 1;
                        break;
                    case "t10to11":
                        $p = 2;
                        break;
                    case "t11to12":
                        $p = 3;
                        break;
                    case "t12to13":
                        $p = 4;
                        break;
                    case "t13to14":
                        $p = 5;
                        break;
                    case "t14to15":
                        $p = 6;
                        break;
                    case "t15to16":
                        $p = 7;
                        break;
                    case "t16to17":
                        $p = 8;
                        break;
                    default:
                        $p = 1;
                        break;
                }
                // switch
            } else {
                throw new Exception("Problem with the resource type<br>");
            }
            // else
            // Pushes period to numerical array
            array_push($periodsnumerical, $p);
        }
        // foreach
        if (empty($periodsnumerical)) {
            throw new Exception("Period cannot be empty<br>");
        }
        // if
        //echo "Period(s):" . implode(", ",$periodsnumerical) . "<br>";
        // set frequency and check
        if (!isset($_POST["frequency"])) {
            throw new Exception("Frequency cannot be empty<br>");
        }
        $frequency = $_POST["frequency"];
        if (!$frequency) {
            throw new Exception("Frequency cannot be empty<br>");
        }
        // if
        // set start date
        $startdate = $_POST["startdate"];
        if (!$startdate) {
            throw new Exception("Start date cannot be empty<br>");
        }
        // if
        // checks if the start date given is in the past
        if (checkIfInThePast($startdate)) {
            throw new Exception("Start date cannot be in the past<br>");
        }
        // if
        // set end date
        $enddate = $_POST["enddate"];
        if (!$enddate) {
            throw new Exception("End date cannot be empty<br>");
        }
        // if
        // checks if end date is in the past
        if (checkIfInThePast($enddate)) {
            throw new Exception("End date cannot be in the past<br>");
        }
        // if
        // checks that end date is after start date
        if (strtotime($enddate) < strtotime($startdate)) {
            throw new Exception("End date should be after start date<br>");
        }
        // if
        // Simple input validation is now complete
        // Now loop through each day, and try to book
        // each day/period for the table given
        // If already booked, report a warning
        // initialise currentdate to use
        // formats to YYYYMMDD
        if ($frequency == "biweekly") {
            // sets a boolean to say if booking is biweekly or not
            $isbiweekly = true;
            $isweekly = false;
        } else {
            $isbiweekly = false;
            $isweekly = true;
        }
        // reformat start and end dates
        $startdate = date("Ymd", strtotime($startdate));
        $enddate = date("Ymd", strtotime($enddate));
        $weekcount = 0;
        $currentdate = $startdate;
        $fullname = getFullname($conn, $username);
        $id = getID($conn, $username);
        // while current date is less than end date
        while (strtotime($currentdate) <= strtotime($enddate)) {
            // if current date is equal to date required
            if (findDayFromDate($currentdate) == $day) {
                // increment weekcount every time a booking is made
                $weekcount++;
                // if not biweekly, or is biweekly and this is an odd week
                // so that biweekly bookings occur 1st week, 3rd week, ...
                // loops through each period in the day and books it
                // Foreach period in numerical list
                foreach ($periodsnumerical as $pd) {
                    if (!$isbiweekly || $isbiweekly && $weekcount % 2 != 0) {
                        $sqlCheck = "SELECT * FROM {$table}\n                             WHERE BookingDate={$currentdate} AND BookingPeriod={$pd}";
                        $resultCheck = mysqli_query($conn, $sqlCheck);
                        if (mysqli_num_rows($resultCheck) > 0) {
                            echo "Warning: Record already exists for period {$pd} on" . styleDate($currentdate) . "<br>";
                            echo "Did not add record<br>";
                        } else {
                            $id = $_SESSION["id"];
                            $fullname = $_SESSION["fullname"];
                            $sqlInsert = "INSERT INTO {$table}\n                                (BookingDate,BookedByID,BookedByName,BookingPeriod,BookingDesc)\n                                VALUES\n                                ({$currentdate},{$id},'{$fullname}',{$pd},'{$description}')";
                            $resultInsert = mysqli_query($conn, $sqlInsert);
                            if ($resultInsert) {
                            } else {
                                echo "Error adding record: " . mysqli_error($conn) . "<br>";
                            }
                            // else
                        }
                        // else
                    }
                    // if
                }
                // foreach
            }
            // if
            // increment current date
            $currentdate = strtotime("+1 day", strtotime($currentdate));
            $currentdate = date("Ymd", $currentdate);
        }
        // while
        if ($isweekly) {
            $isweeklyval = 1;
            $isbiweeklyval = 0;
        } else {
            $isweeklyval = 0;
            $isbiweeklyval = 1;
        }
        // else
        // sets is multipleperiods
        if (count($periodsnumerical) > 1) {
            $ismultipleperiods = 1;
        } else {
            $ismultipleperiods = 0;
        }
        // else
        // If multiple periods, changes the recurperiod variable so that it is used
        // to store which days are recurred
        // So sets recur period to 135 if recurring occurs on periods 1,3, and 5
        if ($ismultipleperiods) {
            $period = implode("", $periodsnumerical);
        } else {
            $period = $periodsnumerical[0];
        }
        // else
        $sqlInsertRecur = "INSERT INTO RecurringBookings\n                           (Resource,StartDate,EndDate,BookedByID,BookedByName,RecurDay,\n                            RecurPeriod,isWeekly,isBiWeekly,isMultiplePeriods)\n                           VALUES\n                           ('{$table}',{$startdate},{$enddate},{$id},'{$fullname}','{$day}',{$period},\n                            {$isweeklyval},{$isbiweeklyval},{$ismultipleperiods})";
        $resultInsertRecur = mysqli_query($conn, $sqlInsertRecur);
        if (!$resultInsertRecur) {
            echo "Error adding record to recurring bookings table:" . mysqli_error($conn) . "<br>";
        } else {
            echo "<b>Successfully added a recurring booking</b><br>";
            echo "Resource: " . styleResource($table, $type) . "<br>";
            echo "Day: " . getFullDay($day) . "<br>";
            // Account for multiple periods
            if (strlen($period) > 1) {
                echo "Periods: ";
            } else {
                echo "Period:";
            }
            // else
            echo stylePeriod($period, $type) . "<br>";
            if ($isweekly) {
                echo "Every week<br>";
            } else {
                echo "Every fortnight<br>";
            }
            // else
            echo "Between " . styleDateWithYears($startdate) . " and " . styleDateWithYears($enddate) . "<br>";
            echo "Description: {$description} <br>";
        }
    } catch (Exception $e) {
        echo "<b><br>An error occured:</b><br>";
        echo $e->getMessage();
    } finally {
        echo "<br><button id='dismiss'>Dismiss</button>";
        echo "</p>";
    }
    // finally
}
function createRecurringBooking($conn, $username)
{
    try {
        echo "<p id='log'>";
        echo "Creating recurring booking<br>";
        // Sets all variables from the form POST
        // set table, throw error if empty
        // The value provided is now of format
        // 'type,table', so we need to explode this to find table name
        $tablestring = $_POST["selectresource"];
        if (!$tablestring) {
            throw new Exception("Table cannot be empty<br>");
        }
        // if
        // explodes string using comma as delimiter
        $str_explode = explode(",", $tablestring);
        // table is second element in array
        $table = $str_explode[1];
        // table type is first element
        $type = $str_explode[0];
        echo "Table: {$table}<br>";
        // set and check day
        $inputday = $_POST["selectday"];
        if (!$inputday) {
            throw new Exception("Day cannot be empty<br>");
        }
        // if
        // format day into 3 characters, and capitalise first char
        // so 'monday' -> 'Mon', for comparisons later
        $day = ucfirst(substr($inputday, 0, 3));
        echo "Day: {$day}<br>";
        // set and check period
        // The period value is set as a string
        // e.g. 'period 1' or 'break' or 't9to10'
        // so this needs to be translated into a period used by the database
        // so a numerical value 1-6 or 1-8
        $description = $_POST["description"];
        echo "Description: {$description} <br>";
        // If booking whole day
        // do not perform all the period-based procedures
        if (isset($_POST["wholeday"])) {
            $bookwholeday = true;
            $bookwholedayval = 1;
        } else {
            // else do all routines based on selected period
            $bookwholeday = false;
            $bookwholedayval = 0;
            $periodstring = $_POST["selectperiod"];
            // type is set as either period/time/vehicle
            // each has their method of getting the numerical value of the period
            // for period based resources
            echo "Type: {$type}<br>";
            if ($type == "period") {
                // explodes using space as delimiter where format is "period n"
                $period_explode = explode(" ", $periodstring);
                // casts second element as an int, to get numerical value of 'n'
                $period = (int) $period_explode[1];
            } elseif ($type == "vehicle") {
                // use a switch to translate string to numerical value
                switch ($periodstring) {
                    case "period 1":
                        $period = 1;
                        break;
                    case "period 2":
                        $period = 2;
                        break;
                    case "break":
                        $period = 3;
                        break;
                    case "period 3":
                        $period = 4;
                        break;
                    case "period 4":
                        $period = 5;
                        break;
                    case "break":
                        $period = 6;
                        break;
                    case "period 5":
                        $period = 7;
                        break;
                    case "period 6":
                        $period = 8;
                        break;
                    default:
                        $period = 1;
                        break;
                }
                // switch
            } elseif ($type == "time") {
                // use a switch to translate
                switch ($periodstring) {
                    case "t9to10":
                        $period = 1;
                        break;
                    case "t10to11":
                        $period = 2;
                        break;
                    case "t11to12":
                        $period = 3;
                        break;
                    case "t12to13":
                        $period = 4;
                        break;
                    case "t13to14":
                        $period = 5;
                        break;
                    case "t14to15":
                        $period = 6;
                        break;
                    case "t15to16":
                        $period = 7;
                        break;
                    case "t16to17":
                        $period = 8;
                        break;
                    default:
                        $period = 1;
                        break;
                }
                // switch
            } else {
                throw new Exception("Problem with the resource type<br>");
            }
            // else
            if (!$period) {
                throw new Exception("Period cannot be empty<br>");
            }
            // if
            echo "Period: {$period}<br>";
        }
        // else
        // set frequency and check
        if (!isset($_POST["frequency"])) {
            throw new Exception("Frequency cannot be empty<br>");
        }
        $frequency = $_POST["frequency"];
        if (!$frequency) {
            throw new Exception("Frequency cannot be empty<br>");
        }
        // if
        echo "Frequency: {$frequency}<br>";
        // set start date
        $startdate = $_POST["startdate"];
        if (!$startdate) {
            throw new Exception("Start date cannot be empty<br>");
        }
        // if
        // checks if the start date given is in the past
        if (checkIfInThePast($startdate)) {
            throw new Exception("Start date cannot be in the past<br>");
        }
        // if
        echo "Start date: {$startdate}<br>";
        // set end date
        $enddate = $_POST["enddate"];
        if (!$enddate) {
            throw new Exception("End date cannot be empty<br>");
        }
        // if
        // checks if end date is in the past
        if (checkIfInThePast($enddate)) {
            throw new Exception("End date cannot be in the past<br>");
        }
        // if
        // checks that end date is after start date
        if (strtotime($enddate) < strtotime($startdate)) {
            throw new Exception("End date should be after start date<br>");
        }
        // if
        echo "End date: {$enddate}<br>";
        echo "Book whole day: {$bookwholeday} <br>";
        // Simple input validation is now complete
        // Now loop through each day, and try to book
        // each day/period for the table given
        // If already booked, report a warning
        // initialise currentdate to use
        // formats to YYYYMMDD
        if ($frequency == "biweekly") {
            // sets a boolean to say if booking is biweekly or not
            $isbiweekly = true;
            $isweekly = false;
        } else {
            $isbiweekly = false;
            $isweekly = true;
        }
        // reformat start and end dates
        $startdate = date("Ymd", strtotime($startdate));
        $enddate = date("Ymd", strtotime($enddate));
        $weekcount = 0;
        $currentdate = $startdate;
        $fullname = getFullname($conn, $username);
        $id = getID($conn, $username);
        // while current date is less than end date
        while (strtotime($currentdate) <= strtotime($enddate)) {
            // if current date is equal to date required
            if (findDayFromDate($currentdate) == $day) {
                // increment weekcount every time a booking is made
                $weekcount++;
                // if booking for the whole day
                if ($bookwholeday) {
                    $maxperiods = getMaxPeriods($type);
                    // for each period 1 to maximum
                    for ($thisperiod = 1; $thisperiod <= $maxperiods; $thisperiod++) {
                        // if not biweekly, or is biweekly and this is an odd week
                        // so that biweekly bookings occur 1st week, 3rd week, ...
                        // if book whole day is true
                        // loops through each period in the day and books it
                        if (!$isbiweekly || $isbiweekly && $weekcount % 2 != 0) {
                            $sqlCheck = "SELECT * FROM {$table}\n                               WHERE BookingDate={$currentdate} AND BookingPeriod={$thisperiod}";
                            $resultCheck = mysqli_query($conn, $sqlCheck);
                            if (mysqli_num_rows($resultCheck) > 0) {
                                echo "Warning: Record already exists for period {$thisperiod} on {$currentdate}<br>";
                                echo "Did not add record<br>";
                            } else {
                                $id = $_SESSION["id"];
                                $fullname = $_SESSION["fullname"];
                                $sqlInsert = "INSERT INTO {$table}\n                                  (BookingDate,BookedByID,BookedByName,BookingPeriod,BookingDesc)\n                                  VALUES\n                                  ({$currentdate},{$id},'{$fullname}',{$thisperiod},'{$description}')";
                                $resultInsert = mysqli_query($conn, $sqlInsert);
                                if ($resultInsert) {
                                } else {
                                    echo "Error adding record: " . mysqli_error($conn) . "<br>";
                                }
                                // else
                            }
                            // else
                        }
                        // if
                    }
                    // for
                } else {
                    // if not biweekly, or is biweekly and this is an odd week
                    // so that biweekly bookings occur 1st week, 3rd week, ...
                    // if book whole day is true
                    // loops through each period in the day and books it
                    if (!$isbiweekly || $isbiweekly && $weekcount % 2 != 0) {
                        $sqlCheck = "SELECT * FROM {$table}\n                             WHERE BookingDate={$currentdate} AND BookingPeriod={$period}";
                        $resultCheck = mysqli_query($conn, $sqlCheck);
                        if (mysqli_num_rows($resultCheck) > 0) {
                            echo "Warning: Record already exists for period {$period} on" . styleDate($currentdate) . "<br>";
                            echo "Did not add record<br>";
                        } else {
                            $id = $_SESSION["id"];
                            $fullname = $_SESSION["fullname"];
                            $sqlInsert = "INSERT INTO {$table}\n                                (BookingDate,BookedByID,BookedByName,BookingPeriod,BookingDesc)\n                                VALUES\n                                ({$currentdate},{$id},'{$fullname}',{$period},'{$description}')";
                            $resultInsert = mysqli_query($conn, $sqlInsert);
                            if ($resultInsert) {
                            } else {
                                echo "Error adding record: " . mysqli_error($conn) . "<br>";
                            }
                            // else
                        }
                        // else
                    }
                    // if
                }
                // else
            }
            // if
            // increment current date
            $currentdate = strtotime("+1 day", strtotime($currentdate));
            $currentdate = date("Ymd", $currentdate);
        }
        // while
        if ($isweekly) {
            $isweeklyval = 1;
            $isbiweeklyval = 0;
        } else {
            $isweeklyval = 0;
            $isbiweeklyval = 1;
        }
        // if booking whole day
        // set recur day to "All" of insertion into RecurringBookings
        // and period to 0
        if ($bookwholeday) {
            $recurday = "All";
            $period = 0;
        }
        $sqlInsertRecur = "INSERT INTO RecurringBookings\n                           (Resource,StartDate,EndDate,BookedByID,BookedByName,RecurDay,\n                            RecurPeriod,isWeekly,isBiWeekly,isWholeDay)\n                           VALUES\n                           ('{$table}',{$startdate},{$enddate},{$id},'{$fullname}','{$day}',{$period},\n                            {$isweeklyval},{$isbiweeklyval},{$bookwholedayval})";
        $resultInsertRecur = mysqli_query($conn, $sqlInsertRecur);
        if (!$resultInsertRecur) {
            echo "Error adding record to recurring bookings table:" . mysqli_error($conn) . "<br>";
        } else {
            echo "<b>Successfully added a recurring booking</b><br>";
        }
    } catch (Exception $e) {
        echo "<b>An error occured:</b><br>";
        echo $e->getMessage();
    } finally {
        echo "<br><button id='dismiss'>Dismiss</button>";
        echo "</p>";
    }
    // finally
}