Location: PHPKode > projects > QuickTalk Forum > quicktalk/qtf_calendar.php
<?php

/**
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license.  If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to hide@address.com so we can mail you a copy immediately.
*
* @package    QuickTalk
* @author     Philippe Vandenberghe <hide@address.com>
* @copyright  2008-2012 The PHP Group
* @version    2.5 build:20100924
*/

session_start();
require_once('bin/qtf_init.php');
if ( !$oVIP->CanView('V2') ) HtmlPage(11);

$bShow = false;
if ( $_SESSION[QT]['show_calendar']=='V' ) $bShow = true;
if ( $_SESSION[QT]['show_calendar']=='U' && $oVIP->role!='V' ) $bShow = true;
if ( $_SESSION[QT]['show_calendar']=='M' && $oVIP->role=='M' ) $bShow = true;
if ( $oVIP->role=='A' ) $bShow = true;
if ( !$bShow ) HtmlPage(101);

// ---------
// FUNCTIONS
// ---------

include('bin/qtf_fn_sql.php');

function FirstDayDisplay($intYear,$intMonth,$intWeekstart=1)
{  
  // search date of the first 'monday' (or weekstart if not 1)
  // before the beginning of the month (to display gey-out in the calendar)
  if ( $intWeekstart<1 || $intWeekstart>7 ) die ('FirstDayDisplay: Arg #3 must be an int (1-7)');

  $arr = array(1=>'monday','tuesday','wednesday','thursday','friday','saturday','sunday'); // system weekdays reference
  $strWeekstart = $arr[$intWeekstart];
  $d = mktime(0,0,0,$intMonth,1,$intYear); // first day of the month
  if ( strtolower(date('l',$d))==$strWeekstart ) return $d;

  for($i=1;$i<8;$i++)
  {
    $d = strtotime('-1 day',$d);
    if ( strtolower(date('l',$d))==$strWeekstart ) return $d;
  }
  return $d;
}

function ArraySwap($arr,$n=1)
{
  // Move the first value to the end of the array. Action is repeated $n times. Keys are not moved.
  if ($n>0)
  {
    $arrK = array_keys($arr);
    while($n>0) { array_push($arr,array_shift($arr)); $n--; }
    $arrV = array_values($arr);
    $arr = array();
    for($i=0;$i<count($arrK);$i++) $arr[$arrK[$i]] = $arrV[$i];
  }
  return $arr;
}

// ---------
// INITIALISE
// ---------

$s = -1;
$v = 'birthday';
QThttpvar('s','int');

$intYear   = date('Y');  if ( isset($_GET['y']) ) $intYear = intval($_GET['y']);
$intYearN  = $intYear+1 ;
$intMonth  = date('n'); if ( isset($_GET['m']) ) $intMonth = intval($_GET['m']);
$intMonthN = $intMonth+1; if ( $intMonthN>12 ) { $intMonthN=1; $intYearN++; }
$strMonth  = '0'.$intMonth; $strMonth = substr($strMonth,-2,2);
$strMonthN = '0'.$intMonthN; $strMonthN = substr($strMonthN,-2,2);

$dToday = mktime(0,0,0,date('n'),date('j'),date('Y'));

if ( $intYear>2100 ) die('Invalid year');
if ( $intYear<1900 ) die('Invalid year');
if ( $intMonth>12 ) die('Invalid month');
if ( $intMonth<1 ) die('Invalid month');

$oVIP->selfurl = 'qtf_calendar.php';
$oVIP->selfname = $L['Birthdays_calendar'];

// Shift language names and cssWeek to match with weekstart setting, if not 1 (monday)

if ( QTF_WEEKSTART>1 )
{
  $L['dateDDD'] = ArraySwap($L['dateDDD'],intval(QTF_WEEKSTART)-1);
  $L['dateDD'] = ArraySwap($L['dateDD'],intval(QTF_WEEKSTART)-1);
  $L['dateD'] = ArraySwap($L['dateD'],intval(QTF_WEEKSTART)-1);
  $arrWeekCss = ArraySwap($arrWeekCss,intval(QTF_WEEKSTART)-1);
}

// --------
// LIST OF ITEMS
// --------

$arrEvents = array();
$arrEventsN = array();
$intEvents = 0;
$intEventsN = 0;

switch(substr($oDB->type,0,5))
{
// Select 2 months
case 'mysql': $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTRING('.$v.',5,2)="'.$strMonth.'" OR SUBSTRING('.$v.',5,2)="'.$strMonthN.'"' ); break;
case 'mssql': $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTRING('.$v.',5,2)="'.$strMonth.'" OR SUBSTRING('.$v.',5,2)="'.$strMonthN.'"' ); break;
case 'pg':    $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTRING('.$v.',5,2)="'.$strMonth.'" OR SUBSTRING('.$v.',5,2)="'.$strMonthN.'"' ); break;
case 'ibase': $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTRING('.$v.' FROM 5 FOR 2)="'.$strMonth.'" OR SUBSTRING('.$v.' FROM 5 FOR 2)="'.$strMonthN.'"' ); break;
case 'sqlit': $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTR('.$v.',5,2)="'.$strMonth.'" OR SUBSTR('.$v.',5,2)="'.$strMonthN.'"' ); break;
case 'db2':   $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTR('.$v.',5,2)="'.$strMonth.'" OR SUBSTR('.$v.',5,2)="'.$strMonthN.'"' ); break;
case 'oci':   $oDB->Query( 'SELECT id,name,role,'.$v.' FROM '.TABUSER.' WHERE SUBSTR('.$v.',5,2)="'.$strMonth.'" OR SUBSTR('.$v.',5,2)="'.$strMonthN.'"' ); break;
default: die('Unknown db type '.$oDB->type);
}
$i=0;
while($row=$oDB->Getrow())
{
  $i++;
  if ( strlen($row[$v])==8 )
  {
    $strM = substr($row[$v],4,2); $intM = intval($strM);
    $strD = substr($row[$v],6,2); $intD = intval($strD);
    if ( $strM==$strMonth ) { $arrEvents[$intD][]=$row; $intEvents++; }
    if ( $strM==$strMonthN ) { $arrEventsN[$intD]=1; $intEventsN++; }
  }
  if ( $i>8 ) break;
}

// --------
// HTML START
// --------

$oVIP->arrCss[] = 'qtf_main2.css';
 
$strHeadScript = '
<script type="text/javascript" src="bin/qt_jquery.js"></script>
<script type="text/javascript">
<!--
$(function() {
  $(".ajaxmouseover").mouseover(function() {
    $.post("qtf_j_user.php",
      {id:this.id,lang:"'.GetLang().'",dir:"'.QTF_DIR_PIC.'"},
      function(data) { if ( data.length>0 ) document.getElementById("popupinfo").innerHTML=data; });
  });
});
-->
</script>
';

include('qtf_p_header.php');

echo '
<p id="sectiondesc">',$oVIP->selfname,'</p>
';

// PREPARE MAIN CALENDAR

$dCurrentDate = mktime(0,0,0,$intMonth,1,$intYear);
$dMainDate = $dCurrentDate;
$dFirstDay = mktime(0,0,0,$intMonth,1,$intYear);
if ( date('l',$dFirstDay)!='Monday' )
{
  $dFirstDay = strtotime('-1 week',$dFirstDay);
  $dFirstMonday = strtotime('next monday',$dFirstDay);
  // correction for php 4.2
  // find last monday
  for ($i=date('j',$dFirstDay);$i<32;$i++)
  {
    $dI = mktime(0,0,0,date('n',$dFirstDay),$i,date('Y',$dFirstDay));
    if ( !$dI )
    {
    if ( date('N',$dI)==1 ) $dFirstMonday = $dI;
    }
  }
  $dFirstDay = $dFirstMonday;
}  
$intShiftWeek = intval(date('W',$dFirstDay)); if ( $intShiftWeek>53 ) $intShiftWeek==1;

// DISPLAY MAIN CALENDAR

echo '<table class="hidden" cellspacing="0" style="width:700px" summary="calendar">',N,'<tr class="hidden" style="vertical-align:middle">',N;
echo '<td><h2>';
if ( date('n',$dCurrentDate)>1 ) echo '<a href="',$oVIP->selfurl,'?m=',(date('n',$dCurrentDate)-1),'">[&lt;]</a>&nbsp;';
echo $L['dateMMM'][date('n',$dCurrentDate)].' '.date('Y',$dCurrentDate);
if ( date('n',$dCurrentDate)<12 ) echo '&nbsp;<a href="',$oVIP->selfurl,'?m=',(date('n',$dCurrentDate)+1),'">[&gt;]</a>';
echo '</h2></td>',N;

echo '<td style="text-align:right;">';
echo '<form method="get" action="',$oVIP->selfurl,'">';
echo '<input type="hidden" name="y" id="y" value="',$intYear,'"/> ',N;
echo $L['Month'],' <select name="m" class="small" onchange="document.getElementById(\'ok\').click();">';
for ($i=1;$i<13;$i++)
{
echo '<option',($i==date('n') ? ' class="bold"' : ''),' value="',$i,'"',($i==$intMonth ? QSEL : ''),'>',$L['dateMMM'][$i],'</option>',N;
}
echo '</select> <input type="submit" name="ok" id="ok" class="small" value="',$L['Ok'],'"/><script type="text/javascript">document.getElementById("ok").style.display="none";</script></td>',N;
echo '</form></td>',N;
echo '</tr>',N,'</table>',N;

echo '<table class="data_o" cellspacing="0" style="width:700px" summary="calendar">',N;
echo '<tr class="data_o">',N;
echo '<th class="week date_first">&nbsp;</th>';
for ($i=1;$i<8;$i++)
{
  echo '<th class="date',($i==7 ? ' date_last' : ''),'" style="width:95px">',$L['dateDDD'][$i],'</th>',N;
}
echo '</tr>',N;

  $iShift=0;
  for ($intWeek=0;$intWeek<6;$intWeek++)
  {
    echo '<tr class="data_o">',N;
    echo '<td class="week">',$intShiftWeek,'</td>'; $intShiftWeek++;
    for ($intDay=1;$intDay<8;$intDay++)
    {
      $d = strtotime("+$iShift days",$dFirstDay); $iShift++;
      $intShiftYear = date('Y',$d);
      $intShiftMonth = date('n',$d);
      $intShiftDay = date('j',$d);

      // date number
      if ( date('n',$dCurrentDate)==date('n',$d) )
      {
        echo '<td class="date ',$arrWeekCss[$intDay],'"',(date('z',$dToday)==date('z',$d) ? ' id="zone_today">' : '>');
        echo '<p class="datenumber">',$intShiftDay,'</p><p class="dateicon">&nbsp;';
        // date info topic
        if ( isset($arrEvents[$intShiftDay]) )
        { 
          $intDayEvents = 0;
          foreach($arrEvents[$intShiftDay] as $intKey=>$arrValues)
          {
            $intDayEvents++;
            $intAge = $intShiftYear - intval(substr($arrValues[$v],0,4));
            if ( $intDayEvents<4 )
            {
              echo '<a class="ajaxmouseover" id="u',$arrValues['id'],'" href="qtf_user.php?id=',$arrValues['id'],'">',$arrValues['name'],'</a> (',$intAge,')<br/>';
            }
            else
            {
              echo '<a class="ajaxmouseover" id="u',$arrValues['id'],'" href="qtf_user.php?id=',$arrValues['id'],'"><img class="ico i_user" src="',$_SESSION[QT]['skin_dir'],'/ico_user_p_1.gif" alt="-" title="',$arrValues['name'],' (',$intAge,')"/></a> ';
            }
            if ( $intDayEvents>7 ) break;
          }
        }
      }
      else
      {
        echo '<td class="date_out">';
        echo '<p class="datenumber">',$intShiftDay,'</p><p class="dateicon">&nbsp;';
      }
      echo '</p></td>',N;
    }
    echo '</tr>',N;
    if ( $intShiftMonth>$intMonth && $intShiftYear==$intYear ) break;    
  }

echo '</table>',N,N;

// DISPLAY SUBDATA

echo '<table class="hidden" cellspacing="0" summary="calendar">',N,'<tr class="hidden">',N;
echo '<td class="hidden" style="width:220px">',N;

  // PREPARE NEXT MONTH

  $dCurrentDate = mktime(0,0,0,$intMonthN,1,$intYearN);
  $dFirstDay = mktime(0,0,0,$intMonthN,1,$intYearN);
  if ( date("l",$dFirstDay)!='Monday' )
  {
    $dFirstDay = strtotime('-1 week',$dFirstDay);
    $dFirstMonday = strtotime('next monday',$dFirstDay);
    // correction for php 4.2
    // find last monday
    for ($i=date('j',$dFirstDay);$i<32;$i++)
    {
      $dI = mktime(0,0,0,date('n',$dFirstDay),$i,date('Y',$dFirstDay));
      if ( !$dI )
      {
      if ( date('N',$dI)==1 ) $dFirstMonday = $dI;
      }
    }
    $dFirstDay = $dFirstMonday;
  }  

  // DISPLAY NEXT MONTH
  
  echo '<h2>',$L['dateMMM'][date('n',$dCurrentDate)],'</h2>';
  echo '<table class="data_o" cellspacing="0" style="width:200px" summary="calendar">',N;
  echo '<tr class="data_o">',N;
  for ($intDay=1;$intDay<8;$intDay++)
  {
  echo '<th class="date_next">',$L['dateD'][$intDay],'</th>',N;
  }
  echo '</tr>',N;

    $iShift=0;
    for ($intWeek=0;$intWeek<6;$intWeek++)
    {
      echo '<tr class="data_o">',N;
      for ($intDay=1;$intDay<8;$intDay++)
      {
        $d = strtotime("+$iShift days",$dFirstDay); $iShift++;
        $intShiftYear = date('Y',$d);
        $intShiftMonth = date('n',$d);
        $intShiftDay = date('j',$d);
        // date number
        if ( date('n',$dCurrentDate)==date('n',$d) )
        {
          echo '<td class="date_next ',$arrWeekCss[$intDay],'"',(date('z',$dToday)==date('z',$d) ? ' id="zone_today"' : ''),'>';
          if ( isset($arrEventsN[$intShiftDay]) )
          {
            echo '<a class="date_next" href="qtf_calendar.php?m=',$intMonthN,'">',$intShiftDay,'</a> ';
          }
          else
          {
            echo $intShiftDay;
          }
        }
        else
        {
          echo '<td class="date_out_next">';
          echo $intShiftDay;
        }
        echo '</td>',N;
      }
      echo '</tr>',N;
      if ( $intShiftMonth>$intMonthN && $intShiftYear==$intYearN ) break;    
    }

  echo '</table>',N,N;

echo '</td>',N;
echo '<td class="hidden" style="width:220px">',N;

  // DISPLAY STATS
  
  echo '<h2>',$L['Total'],'</h2>',N;
  echo '<br/>';
  echo '<p class="preview_section"><b>',$L['dateMMM'][date('n',$dMainDate)],'</b></p>',N;
  echo '<p class="preview_section">',LangS('User',$intEvents),'</p>',N;
  echo '<br/>';
  echo '<p class="preview_section"><b>',$L['dateMMM'][date('n',$dCurrentDate)],'</b></p>',N;
  echo '<p class="preview_section">',LangS('User',$intEventsN),'</p>',N;

echo '</td>',N;
echo '<td class="hidden" style="width:220px">',N;

  // DISPLAY Preview
  
  echo '<h2>',$L['Information'],'</h2>';
  echo '<script type="text/javascript"></script><noscript>Your browser does not support JavaScript</noscript>';
  echo '<div style="width:250px" id="popupinfo"></div>',N;

echo '</td>',N;

echo '<td>&nbsp;</td>',N;

echo '</tr>',N,'</table>',N,N;

// HTML END

include('qtf_p_footer.php');

?>
Return current item: QuickTalk Forum