home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / scheduler.class.inc < prev    next >
Text File  |  2004-03-08  |  18KB  |  528 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. class scheduler extends db
  14. {
  15.     function scheduler()
  16.     {
  17.         $this->db();
  18.     }
  19.  
  20.     function get_default_scheduler($user_id)
  21.     {
  22.         $sql = "SELECT scheduler_id FROM scSubscribed WHERE user_id='$user_id' AND standard='1'";
  23.         $this->query($sql);
  24.         if($this->next_record())
  25.         {
  26.             return $this->f('scheduler_id');
  27.         }
  28.         return false;
  29.     }
  30.  
  31.     function set_default_scheduler($user_id, $scheduler_id)
  32.     {
  33.         $sql = "UPDATE scSubscribed SET standard='0' WHERE user_id='$user_id' AND standard='1'";
  34.         if ($this->query($sql))
  35.         {
  36.             return $this->query("UPDATE scSubscribed SET standard='1' WHERE user_id='$user_id' AND scheduler_id='$scheduler_id'");
  37.         }
  38.         return false;
  39.     }
  40.  
  41.     function add_participant($event_id, $name, $email, $user_id = 0)
  42.     {
  43.         $id = $this->nextid("scParticipants");
  44.  
  45.         if ($id > 0)
  46.         {
  47.             $sql = "INSERT INTO scParticipants (id, event_id, user_id, name, email) VALUES ('$id', '$event_id', '$user_id', '".smart_addslashes($name)."', '".smart_addslashes($email)."')";
  48.             if ($this->query($sql))
  49.             {
  50.                 return $id;
  51.             }else
  52.             {
  53.                 return -1;
  54.             }
  55.         }else
  56.         {
  57.             return -1;
  58.         }
  59.     }
  60.  
  61.     function remove_participants($event_id)
  62.     {
  63.         $sql = "DELETE FROM scParticipants WHERE event_id='$event_id'";
  64.         return $this->query($sql);
  65.     }
  66.  
  67.     function is_participant($event_id, $email)
  68.     {
  69.         $sql = "SELECT id FROM scParticipants WHERE event_id='$event_id' AND email='".smart_addslashes($email)."'";
  70.         $this->query($sql);
  71.         return $this->next_record();
  72.     }
  73.  
  74.     function get_participants($event_id, $sort="name", $direction="ASC")
  75.     {
  76.         $sql = "SELECT * FROM scParticipants WHERE event_id='$event_id' ORDER BY ".$sort." ".$direction;
  77.         $this->query($sql);
  78.         return $this->num_rows();
  79.     }
  80.  
  81.     function subscribe($user_id, $scheduler_id)
  82.     {
  83.         $sql = "INSERT INTO scSubscribed (user_id, scheduler_id) VALUES ('$user_id','$scheduler_id')";
  84.         return $this->query($sql);
  85.     }
  86.  
  87.     function unsubscribe($user_id, $scheduler_id)
  88.     {
  89.         $sql = "DELETE FROM scSubscribed WHERE user_id='$user_id' AND scheduler_id='$scheduler_id'";
  90.         return $this->query($sql);
  91.     }
  92.  
  93.     function unsubscribe_all($user_id)
  94.     {
  95.         $sql = "DELETE FROM scSubscribed WHERE user_id='$user_id'";
  96.         return $this->query($sql);
  97.     }
  98.  
  99.     function is_subscribed($user_id, $scheduler_id)
  100.     {
  101.         $sql = "SELECT * FROM scSubscribed WHERE user_id='$user_id' AND scheduler_id='$scheduler_id'";
  102.         $this->query($sql);
  103.         return $this->next_record();
  104.     }
  105.  
  106.     function add_scheduler($user_id, $name, $start_hour, $end_hour)
  107.     {
  108.         $name = htmlentities($name);
  109.  
  110.         global $GO_SECURITY;
  111.         $acl_read = $GO_SECURITY->get_new_acl($user_id);
  112.         $acl_write = $GO_SECURITY->get_new_acl($user_id);
  113.         if ($acl_read > 0 && $acl_write > 0)
  114.         {
  115.             $scheduler_id = $this->nextid("scSchedulers");
  116.             if ($scheduler_id > 0)
  117.             {
  118.                 $sql  = "INSERT INTO scSchedulers (id, user_id, name, acl_read, acl_write, start_hour, end_hour) ";
  119.                 $sql .= "VALUES ('$scheduler_id', '$user_id', '".smart_addslashes($name)."', '$acl_read', '$acl_write', '$start_hour', '$end_hour')";
  120.                 if ($this->query($sql))
  121.                 {
  122.                     $this->subscribe($user_id, $scheduler_id);
  123.                     $GO_SECURITY->add_user_to_acl($user_id,$acl_write);
  124.                     if (!$this->get_default_scheduler($user_id))
  125.                     {
  126.                         $this->set_default_scheduler($user_id, $scheduler_id);
  127.                     }
  128.                     return $scheduler_id;
  129.                 }
  130.             }else
  131.             {
  132.                 $GO_SECURITY->delete_acl($acl_read);
  133.                 $GO_SECURITY->delete_acl($acl_write);
  134.             }
  135.  
  136.         }
  137.         return false;
  138.  
  139.     }
  140.  
  141.  
  142.     function delete_scheduler($scheduler_id)
  143.     {
  144.         global $GO_SECURITY;
  145.         $delete = new scheduler;
  146.  
  147.         $sql = "SELECT * FROM scEventsSchedulers WHERE scheduler_id='$scheduler_id'";
  148.         $this->query($sql);
  149.  
  150.         while ($this->next_record())
  151.         {
  152.             $sql = "SELECT * FROM scEventsSchedulers WHERE event_id='".$this->f("event_id")." '";
  153.             $delete->query($sql);
  154.             if ($delete->num_rows() < 2)
  155.             {
  156.                 $event = $delete->get_event($this->f("event_id"));
  157.                 $GO_SECURITY->delete_acl($event["acl_read"]);
  158.                 $GO_SECURITY->delete_acl($event["acl_write"]);
  159.                 $sql = "DELETE FROM scEvents WHERE id='".$this->f("event_id")."'";
  160.                 $delete->query($sql);
  161.                 $sql = "DELETE FROM scParticipants WHERE event_id='".$this->f("event_id")."'";
  162.                 $delete->query($sql);
  163.                 $sql = "DELETE FROM scReminded WHERE event_id='".$this->f("event_id")."'";
  164.                 $delete->query($sql);
  165.             }
  166.         }
  167.         $sql = "DELETE FROM scEventsSchedulers WHERE scheduler_id='$scheduler_id'";
  168.         $this->query($sql);
  169.         $scheduler_prop = $this->get_scheduler($scheduler_id);
  170.  
  171.         $GO_SECURITY->delete_acl($scheduler_prop["acl_read"]);
  172.         $GO_SECURITY->delete_acl($scheduler_prop["acl_write"]);
  173.         $sql= "DELETE FROM scSchedulers WHERE id='$scheduler_id'";
  174.         $this->query($sql);
  175.         $sql = "DELETE FROM scSubscribed WHERE scheduler_id='$scheduler_id'";
  176.         $this->query($sql);
  177.     }
  178.  
  179.     function update_scheduler($scheduler_id, $name, $start_hour, $end_hour)
  180.     {
  181.         $name = htmlentities($name);
  182.         $sql = "UPDATE scSchedulers SET name='".smart_addslashes($name)."', start_hour='$start_hour', end_hour='$end_hour' WHERE id='$scheduler_id'";
  183.         return $this->query($sql);
  184.     }
  185.  
  186.  
  187.     function get_scheduler($scheduler_id)
  188.     {
  189.         $sql = "SELECT * FROM scSchedulers WHERE id='$scheduler_id'";
  190.         $this->query($sql);
  191.         if ($this->next_record())
  192.         {
  193.             return $this->Record;
  194.         }else
  195.         {
  196.             return false;
  197.         }
  198.     }
  199.  
  200.     function get_scheduler_by_name($name)
  201.     {
  202.         $sql = "SELECT * FROM scSchedulers WHERE name='".smart_addslashes($name)."'";
  203.         $this->query($sql);
  204.         if ($this->next_record())
  205.         {
  206.             return $this->Record;
  207.         }else
  208.         {
  209.             return false;
  210.         }
  211.     }
  212.  
  213.     function get_subscribed($user_id)
  214.     {
  215.         $sql = "SELECT scSchedulers.* FROM scSchedulers LEFT JOIN scSubscribed ON (scSubscribed.scheduler_id=scSchedulers.id) WHERE scSubscribed.user_id='$user_id' ORDER BY name ASC";
  216.         $this->query($sql);
  217.         return $this->num_rows();
  218.     }
  219.  
  220.     function get_schedulers()
  221.     {
  222.         $sql = "SELECT * FROM scSchedulers ORDER BY name ASC";
  223.         $this->query($sql);
  224.         return $this->num_rows();
  225.     }
  226.  
  227.     function get_authorized_schedulers($user_id)
  228.     {
  229.         $sql = "SELECT scSchedulers.* FROM scSchedulers, acl, users_groups WHERE (".
  230.                 "scSchedulers.acl_read = acl.acl_id OR scSchedulers.acl_write = acl.acl_id".
  231.                 ") AND ( ( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  232.                 "acl.group_id = 0 AND acl.user_id = ".$user_id." ) ) GROUP BY scSchedulers.id ORDER BY scSchedulers.name ASC";
  233.         $this->query($sql);
  234.         return $this->num_rows();
  235.     }
  236.  
  237.     function set_event_status($event_id, $status, $email)
  238.     {
  239.         $sql = "UPDATE scParticipants SET status='$status' WHERE email='".smart_addslashes($email)."' AND event_id='$event_id'";
  240.         return $this->query($sql);
  241.     }
  242.  
  243.     function get_event_status($event_id, $email)
  244.     {
  245.         $sql = "SELECT status FROM scParticipants WHERE email='".smart_addslashes($email)."' AND event_id='$event_id'";
  246.         if($this->query($sql))
  247.         {
  248.             if($this->next_record())
  249.             {
  250.                 return $this->f('status');
  251.             }
  252.         }
  253.         return false;
  254.     }
  255.  
  256.     function save_event($user_id, $title, $description='', $start_day='', $start_month='', $start_year='', $start_hour='', $start_min='', $end_day='', $end_month='', $end_year='', $end_hour='', $end_min='', $monday='0', $tuesday='0', $wednesday='0', $thursday='0', $friday='0', $saturday='0', $sunday='0', $month_time='0', $delete='0', $notime='0',$noend='0', $every='1', $type='daily',$starting_date='0', $ending_date='0', $location_text='0', $reminder='0', $contact_id='0', $background='')
  257.     {
  258.         $title = htmlentities($title);
  259.         $location_text = htmlentities($location_text);
  260.  
  261.         global $GO_SECURITY;
  262.         $acl_read = $GO_SECURITY->get_new_acl($user_id);
  263.         $acl_write = $GO_SECURITY->get_new_acl($user_id);
  264.         if ($acl_read > 0 && $acl_write > 0)
  265.         {
  266.             $start_date = $start_year.'-'.$start_month.'-'.$start_day;
  267.             $end_date = $end_year.'-'.$end_month.'-'.$end_day;
  268.  
  269.             $event_id = $this->nextid("scEvents");
  270.             if ($event_id > 0)
  271.             {
  272.                 $sql = "INSERT INTO scEvents (id, user_id, title, description, start_date, start_hour, start_min, end_date, end_hour, end_min, monday, tuesday, wednesday, thursday, friday, saturday, sunday, month_time, delete_record, notime, noend, every, type, starting_date, ending_date, acl_read, acl_write, location_text, reminder, contact_id, background) ";
  273.                 $sql .= "VALUES ('$event_id', '$user_id', '".smart_addslashes($title)."', '".smart_addslashes($description)."', '$start_date', '$start_hour', '$start_min', '$end_date', '$end_hour', '$end_min', '$monday', '$tuesday', '$wednesday', '$thursday', '$friday', '$saturday', '$sunday', '$month_time', '$delete', '$notime', '$noend', '$every', '$type','$starting_date', '$ending_date', '$acl_read', '$acl_write', '".smart_addslashes($location_text)."', '$reminder', '$contact_id', '$background')";
  274.                 if ($this->query($sql))
  275.                 {
  276.                     return $event_id;
  277.                 }
  278.             }else
  279.             {
  280.                 $GO_SECURITY->delete_acl($acl_read);
  281.                 $GO_SECURITY->delete_acl($acl_write);
  282.             }
  283.         }
  284.         return false;
  285.     }
  286.  
  287.     function subscribe_event($event_id, $scheduler_id)
  288.     {
  289.         $sql = "INSERT INTO scEventsSchedulers (scheduler_id, event_id) VALUES ('$scheduler_id','$event_id')";
  290.         return $this->query($sql);
  291.     }
  292.  
  293.     function unsubscribe_event($event_id, $scheduler_id)
  294.     {
  295.         $sql = "DELETE FROM scEventsSchedulers WHERE scheduler_id='$scheduler_id' AND event_id='$event_id'";
  296.         return $this->query($sql);
  297.     }
  298.  
  299.     function event_is_subscribed($event_id, $scheduler_id)
  300.     {
  301.         $sql = "SELECT * FROM scEventsSchedulers WHERE event_id='$event_id' AND scheduler_id='$scheduler_id'";
  302.         $this->query($sql);
  303.         return $this->next_record();
  304.     }
  305.  
  306.     function get_event_subscribtions($event_id)
  307.     {
  308.         $sql = "SELECT * FROM scEventsSchedulers WHERE event_id='$event_id'";
  309.         $this->query($sql);
  310.         return $this->num_rows();
  311.     }
  312.  
  313.     function update_event($event_id, $title, $description='', $start_day='', $start_month='', $start_year='', $start_hour='', $start_min='', $end_day='', $end_month='', $end_year='', $end_hour='', $end_min='', $monday='0', $tuesday='0', $wednesday='0', $thursday='0', $friday='0', $saturday='0', $sunday='0', $month_time='0', $delete='0', $notime='0',$noend='0', $every='1', $type='daily',$starting_date='0', $ending_date='0', $location_text='0', $reminder='0', $contact_id='0', $background='')
  314.     {
  315.         $title = htmlentities($title);
  316.         $location_text = htmlentities($location_text);
  317.  
  318.         //when an event is updated remind everybody again
  319.         $this->query("DELETE FROM scReminded WHERE event_id='$event_id'");
  320.  
  321.         $start_date = $start_year.'-'.$start_month.'-'.$start_day;
  322.         $end_date = $start_year.'-'.$end_month.'-'.$end_day;
  323.  
  324.         $sql = "UPDATE scEvents SET title='".smart_addslashes($title)."', description='".smart_addslashes($description)."', start_date='$start_date', start_hour='$start_hour', start_min='$start_min', end_date='$end_date', end_hour='$end_hour', end_min='$end_min', monday='$monday', tuesday='$tuesday', wednesday='$wednesday', thursday='$thursday', friday='$friday', saturday='$saturday', sunday='$sunday', month_time='$month_time', delete_record='$delete', notime='$notime', noend='$noend', every='$every', type='$type', starting_date='$starting_date', ending_date='$ending_date', location_text='".smart_addslashes($location_text)."', reminder='$reminder', contact_id='$contact_id', background='$background' ";
  325.         $sql .= "WHERE id='$event_id'";
  326.         return $this->query($sql);
  327.     }
  328.  
  329.     function set_reminded($user_id, $event_id, $occurence)
  330.     {
  331.         return $this->query("INSERT INTO scReminded (user_id, event_id, occurence) VALUES ('$user_id', '$event_id', '$occurence')");
  332.     }
  333.  
  334.     function is_reminded($user_id, $event_id, $occurence)
  335.     {
  336.         if ($this->query("SELECT * FROM scReminded WHERE user_id='$user_id' AND event_id='$event_id' AND occurence>='$occurence'"))
  337.         {
  338.             return $this->next_record();
  339.         }
  340.         return false;
  341.     }
  342.  
  343.     function get_events($scheduler_id, $day, $month, $year, $weekday)
  344.     {
  345.         $month_time = ceil($day/7);
  346.         if (strlen($month) == 1) $month='0'.$month;
  347.         if (strlen($day) == 1) $day='0'.$day;
  348.  
  349.         $fetch_date = $year.'-'.$month.'-'.$day;
  350.  
  351.         switch ($weekday)
  352.         {
  353.             case 0:
  354.                 $weekday = 'sunday';
  355.             break;
  356.  
  357.             case 1:
  358.                 $weekday = 'monday';
  359.             break;
  360.  
  361.             case 2:
  362.                 $weekday = 'tuesday';
  363.             break;
  364.  
  365.             case 3:
  366.                 $weekday = 'wednesday';
  367.             break;
  368.  
  369.             case 4:
  370.                 $weekday = 'thursday';
  371.             break;
  372.  
  373.             case 5:
  374.                 $weekday = 'friday';
  375.             break;
  376.  
  377.             case 6:
  378.                 $weekday = 'saturday';
  379.             break;
  380.         }
  381.         $sql  = "SELECT scEvents.* FROM scEvents LEFT JOIN scEventsSchedulers ON (scEvents.id=scEventsSchedulers.event_id) WHERE ".
  382.                 "((scEvents.start_date<='$fetch_date' AND scEvents.end_date>='$fetch_date' AND scEvents.type='once') OR".
  383.                 "(scEvents.start_date<='0000-00-$day' AND scEvents.end_date>='0000-00-$day' AND scEvents.type='month_date') OR ".
  384.                 "(scEvents.type='daily') OR ".
  385.                 "($weekday='1' AND scEvents.type='weekly') OR ".
  386.                 "($weekday='1' AND scEvents.type='month_day' AND scEvents.month_time='$month_time') OR ".
  387.                 "(scEvents.start_date<='0000-$month-$day' AND scEvents.end_date>='0000-$month-$day' AND scEvents.type='yearly')) ".
  388.                 "AND (scEvents.ending_date='0000-00-00' OR noend='1' OR scEvents.ending_date >= '$fetch_date') AND (scEvents.starting_date='0000-00-00' OR scEvents.starting_date <= '$fetch_date') AND scEventsSchedulers.scheduler_id='$scheduler_id' ORDER BY scEvents.start_hour AND scEvents.start_min ASC";
  389.         $this->query($sql);
  390.         return $this->num_rows();
  391.     }
  392.  
  393.     function get_all_events($scheduler_id)
  394.     {
  395.         $sql = "SELECT scEvents.* FROM scEvents LEFT JOIN scEventsSchedulers ON (scEvents.id=scEventsSchedulers.event_id) WHERE scEventsSchedulers.scheduler_id='$scheduler_id'";
  396.         $this->query($sql);
  397.         return $this->num_rows();
  398.     }
  399.  
  400.     //never tested or used
  401.     function is_available($scheduler_id, $day, $month, $year, $weekday, $start_hour, $start_min, $end_hour, $end_min)
  402.     {
  403.         if (strlen($start_min) == 1) $start_min='0'.$start_min;
  404.         if (strlen($end_min) == 1) $end_min='0'.$end_min;
  405.         $start_time = $start_hour.$start_min;
  406.         $end_time = $end_hour.$end_min;
  407.  
  408.         $month_time = ceil($day/7);
  409.         if (strlen($month) == 1) $month='0'.$month;
  410.         if (strlen($day) == 1) $day='0'.$day;
  411.  
  412.         $fetch_date = $year.'-'.$month.'-'.$day;
  413.  
  414.         switch ($weekday)
  415.         {
  416.             case 0:
  417.                 $weekday = 'sunday';
  418.             break;
  419.  
  420.             case 1:
  421.                 $weekday = 'monday';
  422.             break;
  423.  
  424.             case 2:
  425.                 $weekday = 'tuesday';
  426.             break;
  427.  
  428.             case 3:
  429.                 $weekday = 'wednesday';
  430.             break;
  431.  
  432.             case 4:
  433.                 $weekday = 'thursday';
  434.             break;
  435.  
  436.             case 5:
  437.                 $weekday = 'friday';
  438.             break;
  439.  
  440.             case 6:
  441.                 $weekday = 'saturday';
  442.             break;
  443.         }
  444.         $sql  = "SELECT scEvents.* FROM scEvents LEFT JOIN scEventsSchedulers ON (scEvents.id=scEventsSchedulers.event_id) WHERE ";
  445.         $sql .= "((scEvents.start_date<='$fetch_date' AND scEvents.end_date>='$fetch_date' AND scEvents.type='once') OR";
  446.         $sql .= "(scEvents.start_date<='0000-00-$day' AND scEvents.end_date>='0000-00-$day' AND scEvents.type='month_date') OR ";
  447.         $sql .= "(scEvents.type='daily') OR ";
  448.         $sql .= "($weekday='1' AND scEvents.type='weekly') OR ";
  449.         $sql .= "($weekday='1' AND scEvents.type='month_day' AND scEvents.month_time='$month_time') OR ";
  450.         $sql .= "(scEvents.start_date<='0000-$month-$day' AND scEvents.end_date>='0000-$month-$day' AND scEvents.type='yearly')) ";
  451.         $sql .= "AND (scEvents.ending_date='0000-00-00' OR scEvents.ending_date >= '$fetch_date') AND (scEvents.starting_date='0000-00-00' OR scEvents.starting_date <= '$fetch_date') AND scEventsSchedulers.scheduler_id='$scheduler_id' ORDER BY scEvents.start_hour ASC";
  452.         $this->query($sql);
  453.         while ($this->next_record())
  454.         {
  455.             if ($this->f('notime') == '0')
  456.             {
  457.                 $event_start_min = $this->f('start_min');
  458.                 if (strlen($event_start_min) == 1) $event_start_min='0'.$event_start_min;
  459.                 $event_end_min = $this->f('end_min');
  460.                 if (strlen($event_end_min) == 1) $event_end_min='0'.$event_end_min;
  461.  
  462.                 $event_start_time = $this->f('start_hour').$event_start_min;
  463.                 $event_end_time = $this->f('end_hour').$event_end_min;
  464.  
  465.                 if ($event_end_time > $start_time && $event_start_time < $end_time)
  466.                 {
  467.                     return false;
  468.                 }
  469.  
  470.             }
  471.         }
  472.         return true;
  473.     }
  474.  
  475.     function get_event($event_id)
  476.     {
  477.         $sql = "SELECT * FROM scEvents WHERE id='$event_id'";
  478.         $this->query($sql);
  479.         if($this->next_record())
  480.         {
  481.             return $this->Record;
  482.         }else
  483.         {
  484.             return false;
  485.         }
  486.     }
  487.  
  488.     function delete_event($event_id)
  489.     {
  490.         $event = $this->get_event($event_id);
  491.         global $GO_SECURITY;
  492.         $GO_SECURITY->delete_acl($event["acl_read"]);
  493.         $GO_SECURITY->delete_acl($event["acl_write"]);
  494.  
  495.         $sql = "DELETE FROM scEvents WHERE id='$event_id'";
  496.         $this->query($sql);
  497.         $sql = "DELETE FROM scEventsSchedulers WHERE event_id='$event_id'";
  498.         $this->query($sql);
  499.         $sql = "DELETE FROM scReminded WHERE event_id='$event_id'";
  500.         $this->query($sql);
  501.         $sql = "DELETE FROM scParticipants WHERE event_id='$event_id'";
  502.         return $this->query($sql);
  503.     }
  504.  
  505.     function delete_user($user_id)
  506.     {
  507.         $this->unsubscribe_all($user_id);
  508.  
  509.         $delete = new scheduler;
  510.         $sql = "SELECT * FROM scSchedulers WHERE user_id='$user_id'";
  511.         $this->query($sql);
  512.         while($this->next_record())
  513.         {
  514.             $delete->delete_scheduler($this->f('id'));
  515.         }
  516.  
  517.         $sql = "SELECT * FROM scEvents WHERE user_id='$user_id'";
  518.         $this->query($sql);
  519.  
  520.         while($this->next_record())
  521.         {
  522.             $delete->delete_event($this->f('id'));
  523.         }
  524.  
  525.     }
  526. }
  527. ?>
  528.