home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / modules / calendar / calendar.class.php < prev    next >
Encoding:
PHP Script  |  2003-12-15  |  14.3 KB  |  555 lines

  1. <?php /* CALENDAR $Id: calendar.class.php,v 1.12 2003/12/15 19:37:42 gregorerhardt Exp $ */
  2. ##
  3. ## Calendar classes
  4. ##
  5.  
  6. require_once( $AppUI->getLibraryClass( 'PEAR/Date' ) );
  7. require_once( $AppUI->getSystemClass ('dp' ) );
  8.  
  9. /**
  10. * Displays a configuration month calendar
  11. *
  12. * All Date objects are based on the PEAR Date package
  13. */
  14. class CMonthCalendar {
  15. /**#@+
  16. * @var Date
  17. */
  18.     var $this_month;
  19.     var $prev_month;
  20.     var $next_month;
  21.     var $prev_year;
  22.     var $next_year;
  23. /**#@-*/
  24.  
  25. /** @var string The css style name of the Title */
  26.     var $styleTitle;
  27.  
  28. /** @var string The css style name of the main calendar */
  29.     var $styleMain;
  30.  
  31. /** @var string The name of the javascript function that a 'day' link should call when clicked */
  32.     var $callback;
  33.  
  34. /** @var boolean Show the heading */
  35.     var $showHeader;
  36.  
  37. /** @var boolean Show the previous/next month arrows */
  38.     var $showArrows;
  39.  
  40. /** @var boolean Show the day name column headings */
  41.     var $showDays;
  42.  
  43. /** @var boolean Show the week link (no pun intended) in the first column */
  44.     var $showWeek;
  45.  
  46. /** @var boolean Show the month name as link */
  47.     var $clickMonth;
  48.  
  49. /** @var boolean Show events in the calendar boxes */
  50.     var $showEvents;
  51.  
  52. /** @var string */
  53.     var $dayFunc;
  54.  
  55. /** @var string */
  56.     var $weekFunc;
  57.  
  58. /**
  59. * @param Date $date
  60. */
  61.  function CMonthCalendar( $date=null ) {
  62.         $this->setDate( $date );
  63.  
  64.         $this->classes = array();
  65.         $this->callback = '';
  66.         $this->showTitle = true;
  67.         $this->showArrows = true;
  68.         $this->showDays = true;
  69.         $this->showWeek = true;
  70.         $this->showEvents = true;
  71.  
  72.         $this->styleTitle = '';
  73.         $this->styleMain = '';
  74.  
  75.         $this->dayFunc = '';
  76.         $this->weekFunc = '';
  77.  
  78.         $this->events = array();
  79.     }
  80. // setting functions
  81.  
  82. /**
  83.  * CMonthCalendar::setDate()
  84.  *
  85.  * { Description }
  86.  *
  87.  * @param [type] $date
  88.  */
  89.      function setDate( $date=null ) {
  90.         $this->this_month = new CDate( $date );
  91.  
  92.         $d = $this->this_month->getDay();
  93.         $m = $this->this_month->getMonth();
  94.         $y = $this->this_month->getYear();
  95.  
  96.         //$date = Date_Calc::beginOfPrevMonth( $d, $m, $y-1, FORMAT_ISO );
  97.         $this->prev_year = new CDate( $date );
  98.         $this->prev_year->setYear( $this->prev_year->getYear()-1 );
  99.  
  100.         $this->next_year = new CDate( $date );
  101.         $this->next_year->setYear( $this->next_year->getYear()+1 );
  102.  
  103.         $date = Date_Calc::beginOfPrevMonth( $d, $m, $y, FMT_TIMESTAMP_DATE );
  104.         $this->prev_month = new CDate( $date );
  105.  
  106.         $date = Date_Calc::beginOfNextMonth( $d, $m, $y, FMT_TIMESTAMP_DATE );
  107.         $this->next_month =  new CDate( $date );
  108.  
  109.     }
  110.  
  111. /**
  112.  * CMonthCalendar::setStyles()
  113.  *
  114.  * { Description }
  115.  *
  116.  */
  117.      function setStyles( $title, $main ) {
  118.         $this->styleTitle = $title;
  119.         $this->styleMain = $main;
  120.     }
  121.  
  122. /**
  123.  * CMonthCalendar::setLinkFunctions()
  124.  *
  125.  * { Description }
  126.  *
  127.  * @param string $day
  128.  * @param string $week
  129.  */
  130.     function setLinkFunctions( $day='', $week='' ) {
  131.         $this->dayFunc = $day;
  132.         $this->weekFunc = $week;
  133.     }
  134.  
  135. /**
  136.  * CMonthCalendar::setCallback()
  137.  *
  138.  * { Description }
  139.  *
  140.  */
  141.     function setCallback( $function ) {
  142.         $this->callback = $function;
  143.     }
  144.  
  145. /**
  146.  * CMonthCalendar::setEvents()
  147.  *
  148.  * { Description }
  149.  *
  150.  */
  151.  function setEvents( $e ) {
  152.         $this->events = $e;
  153.     }
  154. // drawing functions
  155. /**
  156.  * CMonthCalendar::show()
  157.  *
  158.  * { Description }
  159.  *
  160.  */
  161.      function show() {
  162.         $s = '';
  163.         if ($this->showTitle) {
  164.             $s .= $this->_drawTitle();
  165.         }
  166.         $s .= "<table border=\"0\" cellspacing=\"1\" cellpadding=\"2\" width=\"100%\" class=\"" . $this->styleMain . "\">\n";
  167.  
  168.         if ($this->showDays) {
  169.             $s .= $this->_drawDays();
  170.         }
  171.  
  172.         $s .= $this->_drawMain();
  173.  
  174.         $s .= "</table>\n";
  175.  
  176.         return $s;
  177.     }
  178.  
  179. /**
  180.  * CMonthCalendar::_drawTitle()
  181.  *
  182.  * { Description }
  183.  *
  184.  */
  185.      function _drawTitle() {
  186.         global $AppUI, $m, $a;
  187.         $url = "index.php?m=$m";
  188.         $url .= $a ? "&a=$a" : '';
  189.         $url .= isset( $_GET['dialog']) ? "&dialog=1" : '';
  190.  
  191.         $s = "\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" width=\"100%\" class=\"$this->styleTitle\">";
  192.         $s .= "\n\t<tr>";
  193.  
  194.         if ($this->showArrows) {
  195.             $href = $url.'&date='.$this->prev_month->format(FMT_TIMESTAMP_DATE).($this->callback ? '&callback='.$this->callback : '');
  196.             $s .= "\n\t\t<td align=\"left\">";
  197.             $s .= '<a href="'.$href.'"><img src="./images/prev.gif" width="16" height="16" alt="'.$AppUI->_('previous month').'" border="0" /></a>';
  198.             $s .= "</td>";
  199.  
  200.         }
  201.  
  202.  
  203.         $s .= "\n\t<th width=\"99%\" align=\"center\">";
  204.         if ($this->clickMonth) {
  205.             $href = $url.'&date='.$this->this_month->format(FMT_TIMESTAMP_DATE).($this->callback ? '&callback='.$this->callback : '');
  206.             $s .= '<a href="'.$href.'">';
  207.         }
  208.         $s .= $this->this_month->format( "%B %Y" );
  209.         $s .= "</th>";
  210.  
  211.         if ($this->showArrows) {
  212.             $href = $url.'&date='.$this->next_month->format(FMT_TIMESTAMP_DATE).($this->callback ? '&callback='.$this->callback : '');
  213.             $s .= "\n\t\t<td align=\"right\">";
  214.             $s .= '<a href="'.$href.'"><img src="./images/next.gif" width="16" height="16" alt="'.$AppUI->_('next month').'" border="0" /></a>';
  215.             $s .= "</td>";
  216.         }
  217.  
  218.         $s .= "\n\t</tr>";
  219.         $s .= "\n</table>";
  220.  
  221.         return $s;
  222.     }
  223. /**
  224. * CMonthCalendar::_drawDays()
  225. *
  226. * { Description }
  227. *
  228. * @return string Returns table a row with the day names
  229. */
  230.     function _drawDays() {
  231.         $bow = Date_Calc::beginOfWeek( null,null,null,null,LOCALE_FIRST_DAY );
  232.         $y = substr( $bow, 0, 4 );
  233.         $m = substr( $bow, 4, 2 );
  234.         $d = substr( $bow, 6, 2 );
  235.         $wk = Date_Calc::getCalendarWeek( $d, $m, $y, "%a", LOCALE_FIRST_DAY );
  236.  
  237.         $s = $this->showWeek ? "\n\t\t<th> </th>" : "";
  238.         foreach( $wk as $day ) {
  239.             $s .= "\n\t\t<th width=\"14%\">$day</th>";
  240.         }
  241.  
  242.         return "\n<tr>$s\n</tr>";
  243.     }
  244.  
  245. /**
  246.  * CMonthCalendar::_drawMain()
  247.  *
  248.  * { Description }
  249.  *
  250.  */
  251.      function _drawMain() {
  252.         GLOBAL $AppUI;
  253.         $today = new CDate();
  254.         $today = $today->format( "%Y%m%d%w" );
  255.  
  256.         $date = $this->this_month;
  257.         $this_day = $date->getDay();
  258.         $this_month = $date->getMonth();
  259.         $this_year = $date->getYear();
  260.         $cal = Date_Calc::getCalendarMonth( $this_month, $this_year, "%Y%m%d%w", LOCALE_FIRST_DAY );
  261.  
  262.         $df = $AppUI->getPref( 'SHDATEFORMAT' );
  263.  
  264.         $html = '';
  265.         foreach ($cal as $week) {
  266.             $html .= "\n<tr>";
  267.             if ($this->showWeek) {
  268.                 $html .=  "\n\t<td class=\"week\">";
  269.                 $html .= $this->dayFunc ? "<a href=\"javascript:$this->weekFunc('$week[0]')\">" : '';
  270.                 $html .= '<img src="./images/view.week.gif" width="16" height="15" border="0" alt="Week View" /></a>';
  271.                 $html .= $this->dayFunc ? "</a>" : '';
  272.                 $html .= "\n\t</td>";
  273.             }
  274.  
  275.             foreach ($week as $day) {
  276.                 $this_day = new CDate( $day );
  277.                 $y = intval( substr( $day, 0, 4 ) );
  278.                 $m = intval( substr( $day, 4, 2 ) );
  279.                 $d = intval( substr( $day, 6, 2 ) );
  280.                 $dow = intval( substr( $day, 8, 1 ) );
  281.  
  282.                 if ($m != $this_month) {
  283.                     $class = 'empty';
  284.                 } else if ($day == $today) {
  285.                     $class = 'today';
  286.                 } else if ($dow == 0 || $dow == 6) {
  287.                     $class = 'weekend';
  288.                 } else {
  289.                     $class = 'day';
  290.                 }
  291.                 $day = substr( $day, 0, 8 );
  292.                 $html .= "\n\t<td class=\"$class\">";
  293.                 if ($this->dayFunc) {
  294.                     $html .= "<a href=\"javascript:$this->dayFunc('$day','".$this_day->format( $df )."')\" class=\"$class\">";
  295.                     $html .= "$d";
  296.                     $html .= "</a>";
  297.                 } else {
  298.                     $html .= "$d";
  299.                 }
  300.                 if ($m == $this_month && $this->showEvents) {
  301.                     $html .= $this->_drawEvents( substr( $day, 0, 8 ) );
  302.                 }
  303.                 $html .= "\n\t</td>";
  304.             }
  305.             $html .= "\n</tr>";
  306.         }
  307.         return $html;
  308.     }
  309.  
  310. /**
  311.  * CMonthCalendar::_drawWeek()
  312.  *
  313.  * { Description }
  314.  *
  315.  */
  316.      function _drawWeek( $dateObj ) {
  317.         $href = "javascript:$this->weekFunc(".$dateObj->getTimestamp().",'".$dateObj->toString()."')";
  318.         $w = "        <td class=\"week\">";
  319.         $w .= $this->dayFunc ? "<a href=\"$href\">" : '';
  320.         $w .= '<img src="./images/view.week.gif" width="16" height="15" border="0" alt="Week View" /></a>';
  321.         $w .= $this->dayFunc ? "</a>" : '';
  322.         $w .= "</td>\n";
  323.         return $w;
  324.     }
  325.  
  326. /**
  327.  * CMonthCalendar::_drawEvents()
  328.  *
  329.  * { Description }
  330.  *
  331.  */
  332.      function _drawEvents( $day ) {
  333.         $s = '';
  334.         if (!isset( $this->events[$day] )) {
  335.             return '';
  336.         }
  337.         $events = $this->events[$day];
  338.         foreach ($events as $e) {
  339.             $href = isset($e['href']) ? $e['href'] : null;
  340.             $alt = isset($e['alt']) ? $e['alt'] : null;
  341.  
  342.             $s .= "<br />\n";
  343.             $s .= $href ? "<a href=\"$href\" class=\"event\" title=\"$alt\">" : '';
  344.             $s .= "{$e['text']}";
  345.             $s .= $href ? '</a>' : '';
  346.         }
  347.         return $s;
  348.     }
  349. }
  350.  
  351. /**
  352. * Event Class
  353. *
  354. * { Description }
  355. *
  356. */
  357. class CEvent extends CDpObject {
  358. /** @var int */
  359.     var $event_id = NULL;
  360.  
  361. /** @var string The title of the event */
  362.     var $event_title = NULL;
  363.  
  364.     var $event_start_date = NULL;
  365.     var $event_end_date = NULL;
  366.     var $event_parent = NULL;
  367.     var $event_description = NULL;
  368.     var $event_times_recuring = NULL;
  369.     var $event_recurs = NULL;
  370.     var $event_remind = NULL;
  371.     var $event_icon = NULL;
  372.     var $event_owner = NULL;
  373.     var $event_project = NULL;
  374.     var $event_private = NULL;
  375.     var $event_type = NULL;
  376.  
  377.     function CEvent() {
  378.         $this->CDpObject( 'events', 'event_id' );
  379.     }
  380.  
  381. // overload check operation
  382.     function check() {
  383.     // ensure changes to check boxes and select lists are honoured
  384.         $this->event_private = intval( $this->event_private );
  385.         $this->event_type = intval( $this->event_type );
  386.         return NULL;
  387.     }
  388.  
  389. /**
  390. * Calculating if an recurrent date is in the given period
  391. * @param Date Start date of the period
  392. * @param Date End date of the period
  393. * @param Date Start date of the Date Object
  394. * @param Date End date of the Date Object
  395. * @param integer Type of Recurrence
  396. * @param integer Times of Recurrence
  397. * @param integer Time of Recurrence
  398. * @return array Calculated Start and End Dates for the recurrent Event for the given Period
  399. */
  400.     function getRecurrentEventforPeriod( $start_date, $end_date, $event_start_date, $event_end_date, $event_recurs, $event_times_recuring, $j ) {
  401.  
  402.         //this array will be returned
  403.         $transferredEvent = array();
  404.  
  405.         //create Date Objects for Event Start and Event End
  406.         $eventStart = new CDate( $event_start_date );
  407.         $eventEnd = new CDate( $event_end_date );
  408.  
  409.         //Time of Recurence = 0 (first occurence of event) has to be checked, too.
  410.         if ($j>0) {
  411.             switch ($event_recurs) {
  412.                 case 1:
  413.                     $eventStart->addSpan(new Date_Span(3600));
  414.                     $eventEnd->addSpan(new Date_Span(3600));
  415.                     break;
  416.                 case 2:
  417.                     $eventStart->addDays( 1 );
  418.                     $eventEnd->addDays( 1 );
  419.                     break;
  420.                 case 3:
  421.                     $eventStart->addDays( 7 );
  422.                     $eventEnd->addDays( 7 );
  423.                     break;
  424.                 case 4:
  425.                     $eventStart->addDays( 14 );
  426.                     $eventEnd->addDays( 14 );
  427.                     break;
  428.                 case 5:
  429.                     $eventStart->addMonths( 1 );
  430.                     $eventEnd->addMonths( 1 );
  431.                     break;
  432.                 case 6:
  433.                     $eventStart->addMonths( 3 );
  434.                     $eventEnd->addMonths( 3 );
  435.                     break;
  436.                 case 7:
  437.                     $eventStart->addMonths( 6 );
  438.                     $eventEnd->addMonths( 6);
  439.                     break;
  440.                 case 8:
  441.                     $eventStart->addMonths( 12 );
  442.                     $eventEnd->addMonths( 12 );
  443.                     break;
  444.                 default:
  445.                     break;
  446.             }
  447.         }
  448.         // add temporarily moved Event Start and End dates to returnArray
  449.         $transferredEvent = array($eventStart, $eventEnd);
  450.  
  451.         // return array with event start and end dates for given period (positive case)
  452.         // or an empty array (negative case)
  453.         return $transferredEvent;
  454.     }
  455.  
  456.  
  457. /**
  458. * Utility function to return an array of events with a period
  459. * @param Date Start date of the period
  460. * @param Date End date of the period
  461. * @return array A list of events
  462. */
  463.     function getEventsForPeriod( $start_date, $end_date ) {
  464.         global $AppUI;
  465.  
  466.     // the event times are stored as unix time stamps, just to be different
  467.  
  468.     // convert to default db time stamp
  469.         $db_start = $start_date->format( FMT_DATETIME_MYSQL );
  470.         $db_end = $end_date->format( FMT_DATETIME_MYSQL );
  471.  
  472.         // Filter events not allowed
  473.         $where = '';
  474.         $join = winnow('projects', 'event_project', $where);
  475.  
  476.     // assemble query for non-recursive events
  477.         $sql = "
  478.         SELECT *
  479.         FROM events
  480.         $join
  481.         WHERE (
  482.                 event_start_date <= '$db_end' AND event_end_date >= '$db_start'
  483.                 OR event_start_date BETWEEN '$db_start' AND '$db_end'
  484.             )
  485.             AND ( event_private=0
  486.                 OR (event_private=1 AND event_owner=$AppUI->user_id)
  487.             )
  488.             AND ($where)
  489.             AND ( event_recurs <= 0 )
  490.         ";
  491.     //echo "<pre>$sql</pre>";
  492.     // execute
  493.     $eventList = db_loadList( $sql );
  494.  
  495.  
  496.     // assemble query for recursive events
  497.         $sql = "
  498.         SELECT *
  499.         FROM events
  500.         $join
  501.         WHERE  ( event_private=0  OR (event_private=1 AND event_owner=$AppUI->user_id) )
  502.             AND ( event_recurs > 0)
  503.             AND ($where)
  504.         ";
  505.     //echo "<pre>$sql</pre>";
  506.  
  507.     // execute
  508.         $eventListRec = db_loadList( $sql );
  509.  
  510.     //Calculate the Length of Period (Daily, Weekly, Monthly View)
  511.         $periodLength = Date_Calc::dateDiff($start_date->getDay(),$start_date->getMonth(),$start_date->getYear(),$end_date->getDay(),$end_date->getMonth(),$end_date->getYear());
  512.  
  513.  
  514.         for ($i=0; $i < sizeof($eventListRec)+1;  $i++) {
  515.  
  516.             for ($j=0; $j < intval($eventListRec[$i]['event_times_recuring']); $j++) {
  517.  
  518.                 //Daily View
  519.                 //show all
  520.                 if ($periodLength == 1){
  521.                 $recEventDate = CEvent::getRecurrentEventforPeriod( $start_date, $end_date, $eventListRec[$i]['event_start_date'], $eventListRec[$i]['event_end_date'], $eventListRec[$i]['event_recurs'], $eventListRec[$i]['event_times_recuring'], $j );
  522.                 }
  523.                 //Weekly or Monthly View and Hourly Recurrent Events
  524.                 //only show hourly recurrent event one time and add string 'hourly'
  525.                 elseif ($periodLength > 1 && $eventListRec[$i]['event_recurs'] == 1 && $j==0) {
  526.                 $recEventDate = CEvent::getRecurrentEventforPeriod( $start_date, $end_date, $eventListRec[$i]['event_start_date'], $eventListRec[$i]['event_end_date'], $eventListRec[$i]['event_recurs'], $eventListRec[$i]['event_times_recuring'], $j );
  527.                 $eventListRec[$i]['event_title'] = $eventListRec[$i]['event_title']." (".$AppUI->_('Hourly').")";
  528.                 }
  529.                 //Weekly and Monthly View and higher recurrence mode
  530.                 //show all events of recurrence > 1
  531.                 elseif ($periodLength > 1 && $eventListRec[$i]['event_recurs'] > 1) {
  532.                 $recEventDate = CEvent::getRecurrentEventforPeriod( $start_date, $end_date, $eventListRec[$i]['event_start_date'], $eventListRec[$i]['event_end_date'], $eventListRec[$i]['event_recurs'], $eventListRec[$i]['event_times_recuring'], $j );
  533.                 }
  534.                 //add values to the eventsArray if check for recurrent event was positive
  535.                 if ( sizeof($recEventDate) > 0 ) {
  536.                     $eventListRec[$i]['event_start_date'] = $recEventDate[0]->format( FMT_DATETIME_MYSQL );
  537.                     $eventListRec[$i]['event_end_date'] = $recEventDate[1]->format( FMT_DATETIME_MYSQL );
  538.                     $eList[0] = $eventListRec[$i];
  539.                     $eventList = array_merge($eventList,$eList);
  540.                 }
  541.                 // clear array of positive recurrent events for the case that next loop recEventDate is empty in order to avoid double display
  542.                 $recEventDate = array();
  543.             }
  544.  
  545.  
  546.  
  547.         }
  548.  
  549.         //return a list of non-recurrent and recurrent events
  550.         return $eventList;
  551.     }
  552. }
  553.  
  554. ?>
  555.