<commit_before><?php

/*
 * Copyright (C) 2006-2011 <NAME>, <NAME>, Cyber IT Solutions
 * Pty. Ltd.
 * 
 * This file is part of the allocPSA application <<EMAIL>>.
 * 
 * allocPSA is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * allocPSA is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with allocPSA. If not, see <http://www.gnu.org/licenses/>.
*/


class calendar {
  var $person;
  var $week_start;
  var $weeks_to_display;
  var $days_of_week = array();
  var $rtp;
  var $first_date;
  var $last_date;
  var $db;
  var $first_day_of_week;


  function __construct($week_start=1, $weeks_to_display=4) {
    $this->db = new db_alloc();
    $this->first_day_of_week = config::get_config_item("calendarFirstDay");
    $this->set_cal_date_range($week_start, $weeks_to_display);
    $this->days_of_week = $this->get_days_of_week_array($this->first_day_of_week);
  }

  function set_cal_person($personID) {
    $this->person = new person();
    $this->person->set_id($personID);
    $this->person->select();
  }

  function set_cal_date_range($week_start, $weeks_to_display) {
    $this->week_start = $week_start;
    $this->weeks_to_display = $weeks_to_display;

    // Wind the date forward till we find the starting day of week
    while (date("D", mktime(0, 0, 0, date("m"), date("d") + $i, date("Y"))) != $this->first_day_of_week) {
      $i++;
    }
    $fd = mktime(date("H"),date("i"),date("s"),date("m"),date("d")-($this->week_start*7)+($i-7),date("Y"));
    
    /// Set the first and last date on the page 
    $this->first_date = date("Y-m-d",$fd); 
    $this->last_date = date("Y-m-d", mktime(date("H",$fd)
                                           ,date("i",$fd)
                                           ,date("s",$fd)
                                           ,date("m",$fd)
                                           ,date("d",$fd)+(($this->weeks_to_display*7)-1)
                                           ,date("Y",$fd)));
  }

  function get_cal_reminders() {

    // Get persons reminders
    $query = prepare("SELECT * 
                        FROM reminder
                        JOIN reminderRecipient ON reminderRecipient.reminderID = reminder.reminderID
                       WHERE personID = %d
                         AND (reminderRecuringInterval = 'No' OR (reminderRecuringInterval != 'No' AND reminderActive))
                       GROUP BY reminder.reminderID", $this->person->get_id());
    $this->db->query($query);
    $reminders = array();
    while ($row = $this->db->row()) {
      $reminder = new reminder();
      $reminder->read_db_record($this->db);

      if ($reminder->is_alive()) {
        $reminderTime = format_date("U",$reminder->get_value("reminderTime"));

        // If repeating reminder
        if ($reminder->get_value('reminderRecuringInterval') != "No" && $reminder->get_value('reminderRecuringValue') != 0) {
          $interval = $reminder->get_value('reminderRecuringValue');
          $intervalUnit = $reminder->get_value('reminderRecuringInterval');

          while ($reminderTime < format_date("U",$this->last_date)+86400) {
            $row["reminderTime"] = $reminderTime;
            $reminders[date("Y-m-d",$reminderTime)][] = $row;
            $reminderTime = $reminder->get_next_reminder_time($reminderTime,$interval,$intervalUnit);
          }

        // Else if once off reminder
        } else {
          $row["reminderTime"] = $reminderTime;
          $reminders[date("Y-m-d",$reminderTime)][] = $row;
        }
      }
    }

    return $reminders;
  }

  function get_cal_tasks_to_start() {
    
    list($ts_open,$ts_pending,$ts_closed) = task::get_task_status_in_set_sql();
    // Select all tasks which are targetted to start
    $query = prepare("SELECT * 
                        FROM task 
                       WHERE personID = %d 
                         AND dateTargetStart >= '%s' 
                         AND dateTargetStart < '%s'
                         AND taskStatus NOT IN (".$ts_closed.")"
                    ,$this->person->get_id()
                    ,$this->first_date
                    ,$this->last_date);

    $this->db->query($query);
    $tasks_to_start = array();
    while ($row = $this->db->next_record()) {
      $tasks_to_start[$row["dateTargetStart"]][] = $row;
    }
    return $tasks_to_start;
  }

  function get_cal_tasks_to_complete() {

    list($ts_open,$ts_pending,$ts_closed) = task::get_task_status_in_set_sql();
    // Select all tasks which are targetted for completion
    $query = prepare("SELECT * 
                        FROM task 
                       WHERE personID = %d 
                         AND dateTargetCompletion >= '%s' 
                         AND dateTargetCompletion < '%s'
                         AND taskStatus NOT IN (".$ts_closed.")"
                    ,$this->person->get_id()
                    ,$this->first_date
                    ,$this->last_date);

    $this->db->query($query);
    $tasks_to_complete = array();
    while ($row = $this->db->next_record()) {
      $tasks_to_complete[$row["dateTargetCompletion"]][] = $row;
    }
    return $tasks_to_complete;
  }

  function get_cal_absences() {
    $query = prepare("SELECT * 
                        FROM absence
                       WHERE (dateFrom >= '%s' OR dateTo <= '%s')"
                    ,$this->first_date,$this->last_date);
    $this->db->query($query);
    $absences = array();
    while ($row = $this->db->row()) {
      $start_time = format_date("U",$row["dateFrom"]);
      $end_time = format_date("U",$row["dateTo"]);
      while ($start_time < $end_time) {
    
        if (date("Y-m-d",$start_time) == $prev_date) {
          // Can't use timezone friendly date magic before 5.3.
          // If the date didn't increment by a day, as is want to happen on DST days, then
          // we manually roll it forward by one hour. Thus hopefully knocking it into the next day.
          $start_time+=3600;
        }

        $row["dates"] = date("Y-m-d H:i:s",$start_time)." ---- ".date("Y-m-d H:i:s",$end_time);
        $absences[date("Y-m-d",$start_time)][] = $row;
        $prev_date = date("Y-m-d",$start_time);
        $start_time += 86400;<commit_msg>22389 Only admin and managers get everybody's absences on calendar.<commit_after><?php

/*
 * Copyright (C) 2006-2011 <NAME>, <NAME>, Cyber IT Solutions
 * Pty. Ltd.
 * 
 * This file is part of the allocPSA application <<EMAIL>>.
 * 
 * allocPSA is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * allocPSA is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with allocPSA. If not, see <http://www.gnu.org/licenses/>.
*/


class calendar {
  var $person;
  var $week_start;
  var $weeks_to_display;
  var $days_of_week = array();
  var $rtp;
  var $first_date;
  var $last_date;
  var $db;
  var $first_day_of_week;


  function __construct($week_start=1, $weeks_to_display=4) {
    $this->db = new db_alloc();
    $this->first_day_of_week = config::get_config_item("calendarFirstDay");
    $this->set_cal_date_range($week_start, $weeks_to_display);
    $this->days_of_week = $this->get_days_of_week_array($this->first_day_of_week);
  }

  function set_cal_person($personID) {
    $this->person = new person();
    $this->person->set_id($personID);
    $this->person->select();
  }

  function set_cal_date_range($week_start, $weeks_to_display) {
    $this->week_start = $week_start;
    $this->weeks_to_display = $weeks_to_display;

    // Wind the date forward till we find the starting day of week
    while (date("D", mktime(0, 0, 0, date("m"), date("d") + $i, date("Y"))) != $this->first_day_of_week) {
      $i++;
    }
    $fd = mktime(date("H"),date("i"),date("s"),date("m"),date("d")-($this->week_start*7)+($i-7),date("Y"));
    
    /// Set the first and last date on the page 
    $this->first_date = date("Y-m-d",$fd); 
    $this->last_date = date("Y-m-d", mktime(date("H",$fd)
                                           ,date("i",$fd)
                                           ,date("s",$fd)
                                           ,date("m",$fd)
                                           ,date("d",$fd)+(($this->weeks_to_display*7)-1)
                                           ,date("Y",$fd)));
  }

  function get_cal_reminders() {

    // Get persons reminders
    $query = prepare("SELECT * 
                        FROM reminder
                        JOIN reminderRecipient ON reminderRecipient.reminderID = reminder.reminderID
                       WHERE personID = %d
                         AND (reminderRecuringInterval = 'No' OR (reminderRecuringInterval != 'No' AND reminderActive))
                       GROUP BY reminder.reminderID", $this->person->get_id());
    $this->db->query($query);
    $reminders = array();
    while ($row = $this->db->row()) {
      $reminder = new reminder();
      $reminder->read_db_record($this->db);

      if ($reminder->is_alive()) {
        $reminderTime = format_date("U",$reminder->get_value("reminderTime"));

        // If repeating reminder
        if ($reminder->get_value('reminderRecuringInterval') != "No" && $reminder->get_value('reminderRecuringValue') != 0) {
          $interval = $reminder->get_value('reminderRecuringValue');
          $intervalUnit = $reminder->get_value('reminderRecuringInterval');

          while ($reminderTime < format_date("U",$this->last_date)+86400) {
            $row["reminderTime"] = $reminderTime;
            $reminders[date("Y-m-d",$reminderTime)][] = $row;
            $reminderTime = $reminder->get_next_reminder_time($reminderTime,$interval,$intervalUnit);
          }

        // Else if once off reminder
        } else {
          $row["reminderTime"] = $reminderTime;
          $reminders[date("Y-m-d",$reminderTime)][] = $row;
        }
      }
    }

    return $reminders;
  }

  function get_cal_tasks_to_start() {
    
    list($ts_open,$ts_pending,$ts_closed) = task::get_task_status_in_set_sql();
    // Select all tasks which are targetted to start
    $query = prepare("SELECT * 
                        FROM task 
                       WHERE personID = %d 
                         AND dateTargetStart >= '%s' 
                         AND dateTargetStart < '%s'
                         AND taskStatus NOT IN (".$ts_closed.")"
                    ,$this->person->get_id()
                    ,$this->first_date
                    ,$this->last_date);

    $this->db->query($query);
    $tasks_to_start = array();
    while ($row = $this->db->next_record()) {
      $tasks_to_start[$row["dateTargetStart"]][] = $row;
    }
    return $tasks_to_start;
  }

  function get_cal_tasks_to_complete() {

    list($ts_open,$ts_pending,$ts_closed) = task::get_task_status_in_set_sql();
    // Select all tasks which are targetted for completion
    $query = prepare("SELECT * 
                        FROM task 
                       WHERE personID = %d 
                         AND dateTargetCompletion >= '%s' 
                         AND dateTargetCompletion < '%s'
                         AND taskStatus NOT IN (".$ts_closed.")"
                    ,$this->person->get_id()
                    ,$this->first_date
                    ,$this->last_date);

    $this->db->query($query);
    $tasks_to_complete = array();
    while ($row = $this->db->next_record()) {
      $tasks_to_complete[$row["dateTargetCompletion"]][] = $row;
    }
    return $tasks_to_complete;
  }

  function get_cal_absences() {
    $query = prepare("SELECT * 
                        FROM absence
                       WHERE (dateFrom >= '%s' OR dateTo <= '%s')"
                    ,$this->first_date,$this->last_date);

    $current_user = &singleton("current_user");
    $current_user->have_role("admin") || $current_user->have_role("manage") or $query.= prepare(" AND personID = %d",$current_user->get_id());

    $this->db->query($query);
    $absences = array();
    while ($row = $this->db->row()) {
      $start_time = format_date("U",$row["dateFrom"]);
      $end_time = format_date("U",$row["dateTo"]);
      while ($start_time < $end_time) {
    
        if (date("Y-m-d",$start_time) == $prev_date) {
          // Can't use timezone friendly date magic before 5.3.
          // If the date didn't increment by a day, as is want to happen on DST days, then
          // we manually roll it forward by one hour. Thus hopefully knocking it into the next day.
          $start_time+=3600;
        }

        $row["dates"] = date("Y-m-d H:i:s",$start_time)." ---- ".date("Y-m-d H:i:s",$end_time);
        $absences[date("Y-m-d",$start_time)][] = $row;
        $prev_date = date("Y-m-d",$start_time);
        $start_time += 86400;
{'fraction_non_alphanumeric': 0.14726002206693636, 'fraction_numerical': 0.005516734093416698, 'mean_word_length': 2.2729898892633607, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2203022', 'n_tokens_mistral': 4299, 'n_tokens_neox': 4135, 'n_words': 926}