po/src/include/calendar.php

351 lines
9.7 KiB
PHP

<?php
// Copyright (C) 2002-2006 Balint Kis (balint@k-i-s.net)
// Copyright (C) 2005-2013 Solomon Peachy (pizza@shaftnet.org)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
function display_hour_combo($identifier, $value, $display_null) {
print "<select name=\"$identifier\">\n";
if ($display_null) {
print emit_option("", '&nbsp;', FALSE);
}
for ($i = 0; $i < 24; $i++) {
print emit_option($i, sprintf("%02d", $i), $value == $i);
}
print "</select>\n";
}
function display_minute_combo($identifier, $value) {
print "<select name=\"$identifier\">\n";
print emit_option("", '&nbsp;', FALSE);
for ($i = 0; $i < 60; $i += 5) {
print emit_option($i, sprintf("%02d", $i), $value == $i);
}
print "</select>\n";
}
function compare_timestamps($timestamp_1, $timestamp_2) {
if (($timestamp_1 == "") || ($timestamp_2 ==""))
return 0;
$year_1 = substr($timestamp_1, 0, 4);
$year_2 = substr($timestamp_2, 0, 4);
if ($year_1 < $year_2)
return 1;
if ($year_1 > $year_2)
return -1;
$month_1 = substr($timestamp_1, 5, 2);
$month_2 = substr($timestamp_2, 5, 2);
if ($month_1 < $month_2)
return 1;
if ($month_1 > $month_2)
return -1;
$day_1 = substr($timestamp_1, 8, 2);
$day_2 = substr($timestamp_2, 8, 2);
if ($day_1 < $day_2)
return 1;
if ($day_1 > $day_2)
return -1;
$hour_1 = substr($timestamp_1, 11, 2);
$hour_2 = substr($timestamp_2, 11, 2);
if ($hour_1 < $hour_2)
return 1;
if ($hour_1 > $hour_2)
return -1;
$minute_1 = substr($timestamp_1, 14, 2);
$minute_2 = substr($timestamp_2, 14, 2);
if ($minute_1 < $minute_2)
return 1;
if ($minute_1 > $minute_2)
return -1;
$second_1 = substr($timestamp_1, 17, 2);
$second_2 = substr($timestamp_2, 17, 2);
if ($second_1 < $second_2)
return 1;
if ($second_1 > $second_2)
return -1;
return 0;
}
function emit_calendar_include() {
global $strings;
global $cal_inc;
$rval = "";
if (!isset($cal_inc)) {
$cal_inc = TRUE;
$rval .= "<script type=\"text/javascript\" src=\"javascript/jscalendar/calendar.js\"></script>";
$rval .= "<script type=\"text/javascript\" src=\"javascript/jscalendar/lang/calendar-".$strings['formats_lang_xml'].".js\"></script>";
$rval .= "<script type=\"text/javascript\" src=\"javascript/jscalendar/calendar-setup.js\"></script>\n";
}
return $rval;
}
function emit_date_input_form($tag_id, $timestamp, $disp = 'full_date', $show_x = TRUE) {
global $strings;
$rval = emit_calendar_include();
if ($timestamp) {
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 5, 2);
$day = substr($timestamp, 8, 2);
$hour = substr($timestamp, 11, 2);
$min = substr($timestamp, 14, 2);
$sec = substr($timestamp, 17, 2);
if (!$hour)
$hour = 0;
if (!$min)
$min = 0;
if (!$sec)
$sec = 0;
$timestamp = "$year-$month-$day $hour:$min:$sec";
$display_timestamp = emit_date_html($timestamp, $disp, TRUE);
$display_title = $strings['datebook_adjust_date'];
} else {
$timestamp = "null";
$display_timestamp = $strings['datebook_blank'];
$display_title = $strings['datebook_set_date'];
}
$rval .= "<span id=\"show_$tag_id\" style=\"border-width: 1px; border-style: solid; border-color: transparent;\" title=\"$display_title\" onmouseover=\"this.style.borderColor = 'cornflowerblue';\" onmouseout=\"this.style.borderColor = 'transparent';\" >$display_timestamp</span>";
if ($show_x) {
$rval .= "<a type=\"nodecor\" onclick=\"document.getElementById('field_$tag_id').value='null'; document.getElementById('show_$tag_id').innerHTML='". $display_timestamp = $strings['datebook_blank'] ."'; \">[X]</a>";
}
$rval .= "<input type=\"hidden\" name=\"$tag_id\" id=\"field_$tag_id\" value=\"$timestamp\" readonly=\"readonly\" />";
$rval .= "<script type=\"text/javascript\">\n";
$rval .= "Calendar.setup({\n";
$rval .= "singleClick: true,\n";
$rval .= "inputField: \"field_$tag_id\",\n";
$rval .= "displayArea: \"show_$tag_id\",\n";
$rval .= "button: \"show_$tag_id\",\n";
$rval .= "daFormat: \"".$strings["formats_$disp"]."\",\n";
$rval .= "ifFormat: \"%Y-%m-%d %H:%M:%S\",\n";
$rval .= "align: \"Tl\",\n";
$rval .= "range: [1900, 2100]\n";
$rval .= "});\n";
$rval .= "</script>\n";
return $rval;
}
function check_date_validity($timestamp) {
if ($timestamp == "null")
return "null";
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 5, 2);
$day = substr($timestamp, 8, 2);
if (checkdate($month, $day, $year)) {
$date = $timestamp;
} else {
$today = getdate();
$date = sprintf("%04d-%02d-%02d", $today['year'], $today['mon'], $today['mday']);
}
return "'" . $date . "'";
}
function emit_date_html($timestamp, $format, $special = FALSE) {
global $po_user;
global $strings;
if ($special)
$link = FALSE;
else if ($po_user['type'] > PO_USER_TYPE_DISABLED)
$link = TRUE;
else
$link = FALSE;
if (!$timestamp)
return "";
$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 5, 2);
$day = substr($timestamp, 8, 2);
$hour = substr($timestamp, 11, 2);
$minute = substr($timestamp, 14, 2);
$timestamp = strtotime($timestamp);
$date_format = FALSE;
$time_format = FALSE;
switch ($format) {
case 'iso8601':
$date_format = '%Y-%m-%dT%H:%M:%S+00:00';
break;
case 'sql':
$date_format = '%Y-%m-%d %H:%M:%S';
break;
case 'full_date':
$date_format = $strings['formats_full_date'];
break;
case 'full':
$date_format = $strings['formats_date'];
$time_format = $strings['formats_time'];
break;
case 'date':
$date_format = $strings['formats_date'];
break;
case 'short_date':
$date_format = $strings['formats_short_date'];
break;
case 'time':
$link = FALSE;
$time_format = $strings['formats_time'];
break;
default:
$link = FALSE;
$time_format = $format;
break;
}
$html_string = "";
if ($date_format) {
$html_string .= date($date_format, $timestamp);
}
if ($link)
$html_string = emit_a("my.datebook.php?view=1&amp;year=$year&amp;month=$month&amp;day=$day", $html_string);
if ($time_format) {
if ($time_format == $format) {
$html_string .= $format;
} else {
if ($date_format)
$html_string .= " @ ";
$html_string .= date($time_format, $timestamp);
}
}
return $html_string;
}
function getDaysInMonth($year, $month) {
if ($month < 1 || $month > 12)
return 0;
$daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$day = $daysInMonth[$month - 1];
if ($month == 2) {
if ($year%4 == 0) {
if ($year%100 == 0) {
if ($year%400 == 0)
$day = 29;
} else {
$day = 29;
}
}
}
return $day;
}
function getDayNumberOfDate($year, $month, $day) {
$nday = 0;
for ($mcounter = 1; $mcounter < $month; $mcounter++)
$nday += getDaysInMonth($year, $mcounter);
return $nday + $day;
}
function getDateOfDayNumber($year, $day) {
$dateOfDay = array();
$dateOfDay[0] = $year;
for ($mcounter = 1; $mcounter < 13; $mcounter++) {
$daysInThisMonth = getDaysInMonth($year, $mcounter);
if ($day > $daysInThisMonth) {
$day -= $daysInThisMonth;
continue;
}
break;
}
$dateOfDay[1] = $mcounter;
$dateOfDay[2] = $day;
return adjustDay($dateOfDay[0], $dateOfDay[1], $dateOfDay[2]);;
}
function getWeekNumber($year, $month, $day, $startDay) {
// get timestamp of first day of the year at noon
$timestamp = mktime(12, 0, 0, 1, 1, $year);
// get first day index 0 Sunday
$date = getdate($timestamp);
$days = $date["wday"] + getDayNumberOfDate($year, $month, $day) - $startDay;
$weekNumber = ceil($days/7);
if ($weekNumber>52)
$weekNumber -= 52;
return $weekNumber;
}
function getFirstDayOfWeek($year, $week, $startDay) {
// get timestamp of first day of the year at noon
$timestamp = mktime(12, 0, 0, 1, 1, $year);
// get first day index 0 Sunday
$date = getdate($timestamp);
$firstDayOfWeek = ($week - 1) * 7 - $date["wday"] + 1 + $startDay;
return getDateOfDayNumber($year, $firstDayOfWeek);
}
function adjustMonth($year, $month) {
$newDate = array();
$newDate[0] = $year;
$newDate[1] = $month;
if ($month < 1) {
$newDate[0] = $year - 1;
$newDate[1] = 12 + $month;
}
if ($month > 12) {
$newDate[0] = $year + 1;
$newDate[1] = $month - 12;
}
return $newDate;
}
function adjustDay($year, $month, $day) {
$newDate = array();
$newDate[0] = $year;
$newDate[1] = $month;
$newDate[2] = $day;
$daysInCurrentMonth = getDaysInMonth($year, $month);
if ($day < 1) {
$previousMonth = adjustMonth($year, $month - 1);
$newDate[0] = $previousMonth[0];
$newDate[1] = $previousMonth[1];
$daysInPreviousMonth = getDaysInMonth($newDate[0], $newDate[1]);
$newDate[2] = $daysInPreviousMonth + $day;
}
if ($day > $daysInCurrentMonth) {
$nextMonth = adjustMonth($year, $month + 1);
$newDate[0] = $nextMonth[0];
$newDate[1] = $nextMonth[1];
$newDate[2] = $day - $daysInCurrentMonth;
}
return $newDate;
}
?>