ayand04

Client

Jan 30th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.71 KB | None | 0 0
  1. elseif ($page_action == 'add_confirm') {
  2.  
  3.             if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  4.  
  5.                 if ($_POST["toggleSwitch"] == 0) { // When checkbox unchecked
  6.                     // Get the current year
  7.                     $currentYear = date("Y");
  8.  
  9.                     // Get all the holidays in the file
  10.                     $allHolidays = handleFileUpload("holiday_list");
  11.  
  12.                     // If no error exists
  13.                     if(!array_key_exists("ERROR", $allHolidays)) {
  14.                         // Fetch existing holidays in the db
  15.                         $existingHolidayList = array();
  16.                         $query = $database->query("SELECT holiday_date FROM " . TABLE_HOLIDAYS);
  17.                         while($existingHoliday = $database->fetch_array($query)) {
  18.                             $existingHolidayList[] = $existingHoliday["holiday_date"];
  19.                         }
  20.  
  21.                         foreach ($allHolidays["VEVENT"] as $holiday) {
  22.                             $startDate = $holiday["DTSTART"];
  23.                             $holiday_year = date("Y", strtotime($startDate));
  24.                             $holiday_month = date("m", strtotime($startDate));
  25.                             $holiday_day = date("d", strtotime($startDate));
  26.  
  27.                             // If the holiday year in the file is same or greater than the current year
  28.                             if($holiday_year >= $currentYear) {
  29.                                 $holidayDate = $holiday_year . '-' . $holiday_month . '-' . $holiday_day;
  30.                                 // Check if the date is already present in the database
  31.                                 if(!in_array($holidayDate, $existingHolidayList)) {
  32.                                     $database->query("INSERT INTO " . TABLE_HOLIDAYS . " (holiday_date, holiday_year) VALUES ('" . $holidayDate . "', '" . $holiday_year . "')");
  33.                                 }
  34.                             }
  35.                         }
  36.                         // Display Success message
  37.                         $message = "Holidays Successfully Added";
  38.                         $msgClass = "mainSuccess";
  39.                     }
  40.                     else {
  41.                         // Display Error message
  42.                         $message = $allHolidays["ERROR"]["MSG"];
  43.                         $msgClass = "mainError";
  44.                     }
  45.                 }
  46.                 elseif ($_POST["toggleSwitch"] == 1) { // When checkbox checked
  47.                     $date = $_POST["holidayPicker"];
  48.  
  49.                     // ToDo Check to add upcoming holidays only
  50.                     if(!empty($date)) {
  51.                         $holiday_year = date("Y", strtotime($date));
  52.                         $holiday_month = date("m", strtotime($date));
  53.                         $holiday_day = date("d", strtotime($date));
  54.  
  55.                         // Check if the entered date is a valid date
  56.                         if(checkdate($holiday_month, $holiday_day, $holiday_year)) {
  57.  
  58.                             $holidayDate = $holiday_year . '-' . $holiday_month . '-' . $holiday_day;
  59.  
  60.                             $existingHolidayList = array();
  61.                             $query = $database->query("SELECT holiday_date FROM " . TABLE_HOLIDAYS);
  62.                             while ($existingHoliday = $database->fetch_array($query)) {
  63.                                 $existingHolidayList[] = $existingHoliday["holiday_date"];
  64.                             }
  65.  
  66.                             if (!in_array($holidayDate, $existingHolidayList)) {
  67.                                 $database->query("INSERT INTO " . TABLE_HOLIDAYS . " (holiday_date, holiday_year) VALUES ('" . $holidayDate . "', '" . $holiday_year . "')");
  68.  
  69.                                 // Display Success message
  70.                                 $message = "Holiday Successfully Added";
  71.                                 $msgClass = "mainSuccess";
  72.                             } else {
  73.                                 // Display Error message
  74.                                 $message = "Holiday already exists";
  75.                                 $msgClass = "mainError";
  76.                             }
  77.                         }
  78.                         else {
  79.                             // Display Error message
  80.                             $message = "Invalid date or the date has passed";
  81.                             $msgClass = "mainError";
  82.                         }
  83.                     }
  84.                     else {
  85.                         // Display Error message
  86.                         $message = "Select a date";
  87.                         $msgClass = "mainError";
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             $action = '';
  93.         }
Add Comment
Please, Sign In to add comment