Welcome to Geeklog Thursday, April 22 2021 @ 09:27 pm EDT

Geeklog Forums

changing calendar.php for extended options


tokyoahead

Anonymous
Hi,

I have written the following code some time ago.
its a calendar function that allows to set any day as the first day of the week.

I think it should be possible to replace the current code with an adaption of this one. Please tell me what you think:


PHP Formatted Code

<?php
$timestamp = $_GET['timestamp'];
if (empty($timestamp)) {$timestamp = strtotime("now");}
getdatepicker($timestamp, "./index.php?action=editday", 1);

function getdatepicker($month_ts, $linkout, $firstwday)
        {
        $weekarray = array("Su","Mo","Tu","We","Th","Fr","Sa","Su","Mo","Tu","We","Th","Fr","Sa");
       
        $datum = getdate($month_ts);
        $thatday = $datum[mday];
        $thatmonth = date("M", $month_ts);
        $thatyear = date("Y", $month_ts);
        $firstofmonth_ts = strtotime("1 $thatmonth $thatyear", $month_ts);
        $numberofdays = date(t, $month_ts);
        $firstmonthday = date(w, $firstofmonth_ts);

        $output = "$output<Table border=1>n";

        $nextmonth_ts = strtotime("+1 month", $month_ts);
        $lastmonth_ts = strtotime("-1 month", $month_ts);
        $nextyear_ts = strtotime("+1 year", $month_ts);
        $lastyear_ts = strtotime("-1 year", $month_ts);
        $output = "$output<TR>n <TD colspan=7>n  <Table width="100%">";
        $output = "$output<TR><TD align=center>n<A HREF="./datepicker.inc.php?timestamp=$lastmonth_ts"><</A>";
        $output = "$output $thatmonth <A HREF="./datepicker.inc.php?timestamp=$nextmonth_ts">></A></TD>n";
        $output = "$output   <TD align=center><A HREF="./datepicker.inc.php?timestamp=$lastyear_ts"><</A>";
        $output = "$output $thatyear <A HREF="./datepicker.inc.php?timestamp=$nextyear_ts">></A></TD></TR>";
        $output = "$output </Table>n</TD></TR>nn";
        $output = "$output<TR>n";
       
        for ($x=$firstwday;$x<=($firstwday+6);$x++) {$output = "$output<TD align=center><B>$weekarray[$x]</B></TD>n";}
               
        $output = "$output</TR>nn<TR>";
        $firstweek=true;
        for($x=0;$x<$numberofdays;$x++) # iterate through all days of the month
                {
                $day_ts = strtotime("+$x day",$firstofmonth_ts);
                $weekday = date("w", $day_ts);
                $day = date("j", $day_ts);
                if ($firstweek==true) {$firstweekgap=$t;}
                if ($weekday==$firstwday)  #first day of the week new line start
                        {
                        if ($firstweek==true) {$output2 = "$output2</TR>nn";}
                        $firstweek=false;
                        $t=0; # set first day of week
                        $output2 = "$output2<TR>n";
                        }
                $t++;
                $output2 = "$output2  <TD align=center><A HREF="$linkout?timestamp=$day_ts" target="tree">$day</A></TD>n";
                if ($t==7) #last day of the week display
                        {
                        $output2 = "$output2</TR>nn";
                        }
                }
        $gap = 7 - $firstweekgap;
        if (($gap>0) AND ($gap<7)) {$output = "$outputn  <TD colspan="$gap"></TD>n$output2";} else {$output = "$output$output2";}
        $output = "$output</TR></Table>n";
        echo $output;
        }

?>



 
 Quote

Status: offline

bond_anton

Forum User
Newbie
Registered: 13/07/04
Posts: 2
Hi!
Yesterday I faced the same problem. I solve it by little modification of config.php, calendar.php and calendar.class.php.
In config.php I added new parameter $_CONF['week_start']
PHP Formatted Code

// +---------------------------------------------------------------------------+
// | LOCALE SETTINGS                                                           |
// |                                                                           |
// | see docs/config.html#locale for details                                   |
// +---------------------------------------------------------------------------+
$_CONF['language']  = 'russian_utf-8';
$_CONF['locale']    = 'ru-ru';
$_CONF['date']      = '%A, %d %B %Y @ %H:%M:%S';
$_CONF['daytime']   = '%d.%m.%y %H:%M';
$_CONF['shortdate'] = '%x';
$_CONF['dateonly']  = '%d %b';
$_CONF['timeonly']  = '%H:%M:%S';
$_CONF['default_charset'] = 'utf-8';
$_CONF['week_start'] = 'Sun';  // Set Sun or Mon here

 

Then I changed calendar.class.php.
First I added new private property of class calendar
PHP Formatted Code

........
/**
* @access private
*/

var $_lang_days;
/**
* @access private
*/

var $_lang_months;
/**
* @access private
*/

var $_week_start;
.....

 

according to that I changed the setLanguage function
PHP Formatted Code

function setLanguage($lang_days='', $lang_months='', $week_start='Sun')
    {
        if (empty($lang_days)) {
            $this->_lang_days['sunday'] = 'Sunday';
            $this->_lang_days['monday'] = 'Monday';
            $this->_lang_days['tuesday'] = 'Tuesday';
            $this->_lang_days['wednesday'] = 'Wednesday';
            $this->_lang_days['thursday'] = 'Thursday';
            $this->_lang_days['friday'] = 'Friday';
            $this->_lang_days['saturday'] = 'Saturday';
        } else {
            $this->_lang_days = $lang_days;
        }

        if (empty($lang_months)) {
            $this->_lang_months['january'] = 'January';
            $this->_lang_months['february'] = 'February';
            $this->_lang_months['march'] = 'March';
            $this->_lang_months['april'] = 'April';
            $this->_lang_months['may'] = 'May';
            $this->_lang_months['june'] = 'June';
            $this->_lang_months['july'] = 'July';
            $this->_lang_months['august'] = 'August';
            $this->_lang_months['september'] = 'September';
            $this->_lang_months['october'] = 'October';
            $this->_lang_months['november'] = 'November';
            $this->_lang_months['december'] = 'December';
        } else {
            $this->_lang_months = $lang_months;
        }
        if ($week_start != 'Mon') {
            $this->_week_start = 'Sun';
        } else {
            $this->_week_start = $week_start;
        }
}

 

and getDayName function
PHP Formatted Code

function getDayName($day = 1) {
        if ($this->_week_start != 'Mon') {
            switch ($day - 1) {
            case 0:
                return $this->_lang_days['sunday'];
                break;
            case 1:
   ............................................
          } else {
             switch ($day - 1) {
             case 0:
                  return $this->_lang_days['monday'];
                  break;
             case 1:
    ...........................................
          }
}

 

And the last change I made in calendar.class.php in function getDayOfWeek
PHP Formatted Code

    /**
    * Returns the day of the week (0-6) for given date
    *
    * @param    int     $day        Number of day in month (1-31)
    * @param    int     $month      Number of the month (1-12)
    * @param    int     $year       Four digit year
    * @return   int     Returns integer for day of week 0 = Sunday through 6 = Saturday (or 0 = Monday through 6 = Sunday, if week starts from Monday)
    *
    */

    function getDayOfWeek($day = 1, $month = 1, $year = '')
    {
        if (empty($year)) {
            $year = $this->_default_year;
        }

        $dateArray = getdate(mktime(0,0,0,$month,$day,$year));
        $result = $dateArray['wday'];
        if ($this->_week_start == 'Mon') {
            $result = ($result == 0)?6:$result - 1;
        }
        return $result;
    }

 

After that I applied 3 chages to calendar.php
1)
PHP Formatted Code

function setCalendarLanguage (&$aCalendar)
{
    global $_CONF, $LANG30;

    $lang_days = array('sunday'=>$LANG30[1],
                        'monday'=>$LANG30[2],
                        'tuesday'=>$LANG30[3],
                        'wednesday'=>$LANG30[4],
                        'thursday'=>$LANG30[5],
                        'friday'=>$LANG30[6],
                        'saturday'=>$LANG30[7]);
    $lang_months = array('january'=>$LANG30[13],
                         'february'=>$LANG30[14],
                         'march'=>$LANG30[15],
                         'april'=>$LANG30[16],
                         'may'=>$LANG30[17],
                         'june'=>$LANG30[18],
                         'july'=>$LANG30[19],
                         'august'=>$LANG30[20],
                         'september'=>$LANG30[21],
                         'october'=>$LANG30[22],
                         'november'=>$LANG30[23],
                         'december'=>$LANG30[24]);
    $week_start = $_CONF['week_start'];

    $aCalendar->setLanguage($lang_days, $lang_months, $week_start);
}

 

2)
PHP Formatted Code

function makeDaysHeadline ()
{
    global $_CONF, $LANG30;
    $retval = '';
    if ($_CONF['week_start'] == 'Mon') {
            $retval = '<tr><th>'
            . substr ($LANG30[2], 0, 2) . '</th><th>'
            . substr ($LANG30[3], 0, 2) . '</th><th>'
            . substr ($LANG30[4], 0, 2) . '</th><th>'
            . substr ($LANG30[5], 0, 2) . '</th><th>'
            . substr ($LANG30[6], 0, 2) . '</th><th>'
            . substr ($LANG30[7], 0, 2) . '</th><th>'
            . substr ($LANG30[1], 0, 2) . '</th></tr>';
    } else {
            $retval = '<tr><th>'
            . substr ($LANG30[1], 0, 2) . '</th><th>'
            . substr ($LANG30[2], 0, 2) . '</th><th>'
            . substr ($LANG30[3], 0, 2) . '</th><th>'
            . substr ($LANG30[4], 0, 2) . '</th><th>'
            . substr ($LANG30[5], 0, 2) . '</th><th>'
            . substr ($LANG30[6], 0, 2) . '</th><th>'
            . substr ($LANG30[7], 0, 2) . '</th></tr>';
    }
    return $retval;
}

 

3)
PHP Formatted Code

default:
// Load templates
$cal_templates = new Template($_CONF['path_layout'] . 'calendar');
$cal_templates->set_file(array('calendar'=>'calendar.thtml',
                                'week' => 'calendarweek.thtml',
                                'day' => 'calendarday.thtml',
                                'event' => 'calendarevent.thtml',
                                'mastercal'=>'mastercalendaroption.thtml',
                                'personalcal'=>'personalcalendaroption.thtml',
                                'addevent'=>'addeventoption.thtml'));

$cal_templates->set_var('site_url', $_CONF['site_url']);
$cal_templates->set_var ('layout_url', $_CONF['layout_url']);
$cal_templates->set_var('mode', $mode);
.................................................
if ($_CONF['week_start'] != 'Mon') {
    $cal_templates->set_var('lang_sunday', $LANG30[1]);
    $cal_templates->set_var('lang_monday', $LANG30[2]);
    $cal_templates->set_var('lang_tuesday', $LANG30[3]);
    $cal_templates->set_var('lang_wednesday', $LANG30[4]);
    $cal_templates->set_var('lang_thursday', $LANG30[5]);
    $cal_templates->set_var('lang_friday', $LANG30[6]);
    $cal_templates->set_var('lang_saturday', $LANG30[7]);
} else {
    $cal_templates->set_var('lang_sunday', $LANG30[2]);
    $cal_templates->set_var('lang_monday', $LANG30[3]);
    $cal_templates->set_var('lang_tuesday', $LANG30[4]);
    $cal_templates->set_var('lang_wednesday', $LANG30[5]);
    $cal_templates->set_var('lang_thursday', $LANG30[6]);
    $cal_templates->set_var('lang_friday', $LANG30[7]);
    $cal_templates->set_var('lang_saturday', $LANG30[1]);
}
.................................................

 

After those modifications it became simple to set if week startrs from Monday or Sunday it set up by $_CONF['week_start'] parameter in config.php.
 Quote

Status: offline

bond_anton

Forum User
Newbie
Registered: 13/07/04
Posts: 2
I'm sorry I forgot 'week' case in calendar.php
PHP Formatted Code

........................
case 'week':
    $cal_templates = new Template($_CONF['path_layout'] . 'calendar');
.....................................
    $start_mname = ($_CONF['week_start'] != 'Mon')?strftime('%B', mktime(0,0,0,$month,$day,$year)):strftime('%B', mktime(0,0,0,$month,$day+1,$year));
    $eday = ($_CONF['week_start'] != 'Mon')?strftime('%e', mktime(0,0,0,$month,$day+6,$year)):strftime('%e', mktime(0,0,0,$month,$day+7,$year));
    $end_mname = ($_CONF['week_start'] != 'Mon')?strftime('%B', mktime(0,0,0,$month,$day+6,$year)):strftime('%B', mktime(0,0,0,$month,$day+7,$year));
    $end_ynum = ($_CONF['week_start'] != 'Mon')?strftime('%Y', mktime (0,0,0,$month,$day+6,$year)):strftime('%Y', mktime (0,0,0,$month,$day+7,$year));
    $date_range = ($_CONF['week_start'] != 'Mon')?$start_mname . ' ' . $day:$start_mname . ' ' . strftime('%e', mktime(0,0,0,$month,$day+1,$year));
    if ($year <> $end_ynum) {
        $date_range .= ', ' . $year . ' - ';
    } else {
        $date_range .= ' - ';
    }
    if ($start_mname <> $end_mname) {
        $date_range .= $end_mname . ' ' . $eday . ', ' . $end_ynum;
    } else {
        $date_range .= $eday . ', ' . $end_ynum;
    }
    $cal_templates->set_var('date_range', $date_range);
    $thedate = ($_CONF['week_start'] != 'Mon')?COM_getUserDateTimeFormat(mktime(0,0,0,$month,$day,$year)):COM_getUserDateTimeFormat(mktime(0,0,0,$month,$day+1,$year));
    $cal_templates->set_var('week_num',$thedate[1]);
    if ($_CONF['week_start'] == 'Mon') {
        for ($i = 1; $i <= 7; $i++) {
            $dayname = (date('w',$thedate[1]) == 0)?$cal->getDayName(7):$cal->getDayName(date('w',$thedate[1]));
            $monthnum = date('m', $thedate[1]);
            $daynum = date('d', $thedate[1]);
            $yearnum = date('Y', $thedate[1]);
            if ($yearnum . '-' . $monthnum . '-' . $daynum == date('Y-m-d',time())) {
                $cal_templates->set_var('class'.$i,'weekview-curday');
            } else {
                $cal_templates->set_var('class'.$i,'weekview-offday');
            }
            $monthname = $cal->getMonthName($monthnum);
            $cal_templates->set_var('day'.$i,$dayname . ", <a href="{$_CONF['site_url']}/calendar.php?mode=$mode&view=day&day=$daynum&month=$monthnum&year=$yearnum">" . strftime ("%x", $thedate[1]) . '</a>');
            $cal_templates->set_var('langlink_addevent'.$i, '<a href="' . $_CONF['site_url'] . "/submit.php?type=event&mode=$mode&day=$daynum&month=$monthnum&year=$yearnum" . '">' . $LANG30[8] . '</a>');
            if ($mode == 'personal') {
                $calsql = "SELECT * FROM {$_TABLES["personal_events"]} WHERE (uid = {$_USER["uid"]}) AND ((allday=1 AND datestart = "$yearnum-$monthnum-$daynum") OR (datestart >= "$yearnum-$monthnum-$daynum 00:00:00" AND datestart <= "$yearnum-$monthnum-$daynum 23:59:59") OR (dateend >= "$yearnum-$monthnum-$daynum 00:00:00" AND dateend <= "$yearnum-$monthnum-$daynum 23:59:59") OR ("$yearnum-$monthnum-$daynum" between datestart and dateend)) ORDER BY datestart,timestart";
            } else {
                $calsql = "SELECT * FROM {$_TABLES["events"]} WHERE ((allday=1 AND datestart = "$yearnum-$monthnum-$daynum") OR (datestart >= "$yearnum-$monthnum-$daynum 00:00:00" AND datestart <= "$yearnum-$monthnum-$daynum 23:59:59") OR (dateend >= "$yearnum-$monthnum-$daynum 00:00:00" AND dateend <= "$yearnum-$monthnum-$daynum 23:59:59") OR ("$yearnum-$monthnum-$daynum" between datestart and dateend)) ORDER BY datestart,timestart";
            }
            $result = DB_query($calsql);
            $nrows = DB_numRows($result);
            for ($j = 1; $j <= $nrows; $j++) {
                $A = DB_fetchArray($result);
                if ($A['allday'] == 1) {
                    $cal_templates->set_var('event_starttime', $LANG30[26]);
                    $cal_templates->set_var('event_endtime','');
                } else {
                    $startstamp = strtotime($A['datestart'] . ' ' . $A['timestart']);
                    $endstamp = strtotime($A['dateend'] . ' ' . $A['timeend']);
                    $startday = date('d',$startstamp);
                    $startmonth = date('n',$startstamp);
                    $endday = date('d', $endstamp);
                    $endmonth = date('n',$endstamp);
                    if (($startmonth == $monthnum && $daynum > $startday) OR ($startmonth <> $monthnum)) {
                        $starttime = date('n/j g:i a',$startstamp);
                    } else {
                        $starttime = date('g:i a', $startstamp);
                    }
                    if (($endmonth == $monthnum && $daynum < $endday) OR ($endmonth <> $monthnum)) {
                        $endtime = date('n/j g:i a', $endstamp);
                    } else {
                        $endtime = date('g:i a', $endstamp);
                    }
                    $cal_templates->set_var('event_starttime', $starttime);
                    $cal_templates->set_var('event_endtime', ' - ' . $endtime);
                }
                $cal_templates->set_var('event_title_and_link', '<a href="' . $_CONF['site_url'] . '/calendar_event.php?mode=' . $mode . '&eid=' . $A['eid'] . '">' . stripslashes($A['title']) . '</a>');
                // Provide delete event link if user has access
                if (SEC_hasAccess($A['owner_id'],$A['group_id'],$A['perm_owner'],$A['perm_group'],$A['perm_members'],$A['perm_anon']) == 3 AND $mode == 'personal') {
                    $cal_templates->set_var('delete_imagelink','<a href="' . $_CONF['site_url'] . '/calendar_event.php?action=deleteevent&eid=' . $A['eid'] . '"><img alt="' . $LANG30[30] . '" border="0" src="' . $_CONF['layout_url'] . '/images/icons/delete_event.gif"></a>');
                } else {
                    $cal_templates->set_var('delete_imagelink','');
                }
                $cal_templates->parse('events_day'.$i,'events',true);
               
            }
            if ($nrows == 0) {
                $cal_templates->set_var('event_starttime',' ');
                $cal_templates->set_var('event_endtime','');
                $cal_templates->set_var('event_title_and_link','');
                $cal_templates->set_var('delete_imagelink','');
                $cal_templates->parse('events_day'.$i,'events',true);
            }
            // Go to next day
            $thedate = COM_getUserDateTimeFormat(mktime(0,0,0,$monthnum, $daynum + 1, $yearnum));
        }
    } else {
        for ($i = 1; $i <= 7; $i++) {
            $dayname = $cal->getDayName(date('w',$thedate[1]) + 1);
            $monthnum = date('m', $thedate[1]);
.........................
}
..........................

 

 Quote

Status: offline

varian vega

Forum User
Newbie
Registered: 21/05/04
Posts: 10
Thank you for sharing this great code! Tried out and it worked from the start

I wanted to change the calendar all the time but couldnt figure this out... so well, thanks again,

bye varian

---
infectedradio.com
 Quote

Hakan Törehan

Anonymous
Thank you for sharing this great code!
 Quote

All times are EDT. The time is now 09:27 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content