Task #10885: work on week view. only if the week starts on monday or sunday

This commit is contained in:
Juanjo Menent 2011-03-09 16:53:30 +00:00
parent 0068ed36a4
commit 24e4c3e38a
2 changed files with 150 additions and 55 deletions

View File

@ -146,7 +146,27 @@ if (empty($action) || $action=='show_month')
} }
if ($action=='show_week') if ($action=='show_week')
{ {
$prev = dol_get_first_day_week($day, $month, $year);
$prev_year = $prev['prev_year'];
$prev_month = $prev['prev_month'];
$prev_day = $prev['prev_day'];
$first_day = $prev['first_day'];
$week = $prev['week'];
$day =(int)$day;
$next = dol_get_next_week($day, $week, $month, $year);
$next_year = $next['year'];
$next_month = $next['month'];
$next_day = $next['day'];
// Define firstdaytoshow and lastdaytoshow
$firstdaytoshow=dol_mktime(0,0,0,$prev_month,$first_day,$prev_year);
$lastdaytoshow=dol_mktime(0,0,0,$next_month,$next_day,$next_year);
$max_day_in_month = date("t",dol_mktime(0,0,0,$month,1,$year));
$tmpday = $first_day;
} }
if ($action=='show_day') if ($action=='show_day')
{ {
@ -195,10 +215,10 @@ if (empty($action) || $action=='show_month')
} }
if ($action=='show_week') if ($action=='show_week')
{ {
$nav ="<a href=\"?year=".$prev_year."&amp;month=".$prev_month."&amp;week=".$prev_week."&amp;day=".$prev_day."&amp;region=".$region.$param."\">".img_previous($langs->trans("Previous"))."</a>\n"; $nav ="<a href=\"?year=".$prev_year."&amp;month=".$prev_month."&amp;day=".$prev_day."&amp;region=".$region.$param."\">".img_previous($langs->trans("Previous"))."</a>\n";
$nav.=" <span id=\"month_name\">".dol_print_date(dol_mktime(0,0,0,$month,1,$year),"%Y").", ".$langs->trans("Week")." ".$week; $nav.=" <span id=\"month_name\">".dol_print_date(dol_mktime(0,0,0,$month,1,$year),"%Y").", ".$langs->trans("Week")." ".$week;
$nav.=" </span>\n"; $nav.=" </span>\n";
$nav.="<a href=\"?year=".$next_year."&amp;month=".$next_month."&amp;week=".$next_week."&amp;day=".$next_day."&amp;region=".$region.$param."\">".img_next($langs->trans("Next"))."</a>\n"; $nav.="<a href=\"?year=".$next_year."&amp;month=".$next_month."&amp;day=".$next_day."&amp;region=".$region.$param."\">".img_next($langs->trans("Next"))."</a>\n";
} }
if ($action=='show_day') if ($action=='show_day')
{ {
@ -522,7 +542,52 @@ if (empty($action) || $action == 'show_month') // View by month
} }
elseif ($action == 'show_week') // View by week elseif ($action == 'show_week') // View by week
{ {
print $langs->trans("FeatureNotYetAvailable"); //Work in progress... //print $langs->trans("FeatureNotYetAvailable"); //Work in progress...
echo '<table width="100%" class="nocellnopadd">';
echo ' <tr class="liste_titre">';
$i=0;
while ($i < 7)
{
echo ' <td align="center">'.$langs->trans("Day".(($i+(isset($conf->global->MAIN_START_WEEK)?$conf->global->MAIN_START_WEEK:1)) % 7))."</td>\n";
$i++;
}
echo " </tr>\n";
// In loops, tmpday contains day nb in current month (can be zero or negative for days of previous month)
//var_dump($eventarray);
//print $tmpday;
echo " <tr>\n";
for($iter_day = 0; $iter_day < 7; $iter_day++)
{
if(($tmpday <= $max_day_in_month))
{
// Show days of the current week
$curtime = dol_mktime (0, 0, 0, $month, $tmpday, $year);
if($curtime == $now)
$style='cal_today';
else
$style='cal_current_month';
echo ' <td class="'.$style.'" width="14%" valign="top" nowrap="nowrap">';
show_day_events($db, $tmpday, $month, $year, $month, $style, $eventarray, 0, $maxlength, $param, 1, 300);
echo " </td>\n";
}
else
{
$style='cal_current_month';
echo ' <td class="'.$style.'" width="14%" valign="top" nowrap="nowrap">';
show_day_events($db, $tmpday - $max_day_in_month, $next_month, $next_year, $month, $style, $eventarray, 0, $maxlength, $param, 1, 300);
echo "</td>\n";
}
$tmpday++;
}
echo " </tr>\n";
echo "</table>\n";
} }
else // View by day else // View by day
{ {

View File

@ -204,22 +204,14 @@ function dol_get_next_month($month, $year)
* @param year Year * @param year Year
* @return array Previous year,month,week * @return array Previous year,month,week
*/ */
function dol_get_prev_week($week, $year) function dol_get_prev_week($day, $week, $month, $year)
{ {
if ($week == 1) $tmparray = dol_get_first_day_week($day, $month, $year);
{
$prev_week = 52; $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
$prev_month = 12; $time-=24*60*60*7;
$prev_year = $year - 1; $tmparray=dol_getdate($time,true);
} return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
else
{
$prev_week = $week-1;
$str_tmp = str_pad($prev_week, 2, 0, STR_PAD_LEFT);
$prev_month = date('n', strtotime($year.'-W'.$str_tmp));
$prev_year = $year;
}
return array('year' => $prev_year, 'month'=>$prev_month, 'week' => $prev_week);
} }
/** Return next week /** Return next week
@ -227,22 +219,16 @@ function dol_get_prev_week($week, $year)
* @param year Year * @param year Year
* @return array Next year,month,week * @return array Next year,month,week
*/ */
function dol_get_next_week($week, $year) function dol_get_next_week($day,$week, $month, $year)
{ {
if ($week == 52) $tmparray = dol_get_first_day_week($day, $month, $year);
{
$next_week = 1; $time=dol_mktime(12,0,0,$month,$tmparray['first_day'],$year,1,0);
$next_month = 1; $time+=24*60*60*7;
$next_year = $year + 1; $tmparray=dol_getdate($time,true);
}
else return array('year' => $tmparray['year'], 'month' => $tmparray['mon'], 'day' => $tmparray['mday']);
{
$next_week = $week + 1;
$str_tmp = str_pad($next_week, 2, 0, STR_PAD_LEFT);
$next_month = date('n', strtotime($year.'-W'.$str_tmp));
$next_year = $year;
}
return array('year' => $next_year, 'month'=>$next_month, 'week' => $next_week);
} }
/** Return GMT time for first day of a month or year /** Return GMT time for first day of a month or year
@ -284,71 +270,115 @@ function dol_get_last_day($year,$month=12,$gm=false)
return $datelim; return $datelim;
} }
/** Return first day of week for a week /** Return first day of week for a date
* @param day Day * @param day Day
* @param month Month * @param month Month
* @param year Year
* @param gm False = Return date to compare with server TZ, True to compare with GM date. * @param gm False = Return date to compare with server TZ, True to compare with GM date.
* @return Timestamp Date for first day of week * @return array year,month, week,first_day,prev_year,prev_month,prev_day
*/ */
function dol_get_first_day_of_week($day,$month,$year,$gm=false) function dol_get_first_day_week($day,$month,$year,$gm=false)
{ {
global $conf;
$date=dol_mktime(0,0,0,$month,$day,$year,$gm); $date=dol_mktime(0,0,0,$month,$day,$year,$gm);
// TODO: Modify->the week can begins with any day!
if (isset($conf->global->MAIN_START_WEEK)) if (isset($conf->global->MAIN_START_WEEK))
{ {
if ($conf->global->MAIN_START_WEEK==1) if ($conf->global->MAIN_START_WEEK==1)
{ {
$getdate = getdate($date); $tmparray = dol_getdate($date,true);
// How many days ahead monday are we? // How many days ahead monday are we?
switch ( $getdate['wday'] ) switch ( $tmparray['wday'] )
{ {
case 0: // we are on sunday case 0: // we are on sunday
$days = 6; $days = 6;
break; break;
default: // any other day default: // any other day
$days = $getdate['wday']-1; $days = $tmparray['wday']-1;
break; break;
} }
$seconds = $days*24*60*60; $seconds = $days*24*60*60;
$monday = date($getdate[0])-$seconds; $tmpday = date($tmparray[0])-$seconds;
return $monday;
} }
else else
{ {
$getdate = getdate($date); $tmparray = dol_getdate($date,true);
// substact as many days as days ahead sunday we are // substact as many days as days ahead sunday we are
$seconds = $getdate['wday']*24*60*60; $seconds = $tmparray['wday']*24*60*60;
$sunday = date($getdate[0])-$seconds; $tmpday = date($tmparray[0])-$seconds;
return $sunday;
} }
} }
else else
{ {
$getdate = getdate($date); $tmpday = dol_getdate($date,true);
// How many days ahead monday are we? // How many days ahead monday are we?
switch ( $getdate['wday'] ) switch ( $tmparray['wday'] )
{ {
case 0: // we are on sunday case 0: // we are on sunday
$days = 6; $days = 6;
break; break;
default: // any other day default: // any other day
$days = $getdate['wday']-1; $days = $tmparray['wday']-1;
break; break;
} }
$seconds = $days*24*60*60; $seconds = $days*24*60*60;
$monday = date($getdate[0])-$seconds; $tmpday = date($tmparray[0])-$seconds;
return $monday;
} }
$tmpday = date("d",$tmpday);
if ($tmpday>$day)
{
$prev_month = $month-1;
$prev_year = $year;
if ($prev_month==0)
{
$prev_month = 12;
$prev_year = $year-1;
}
}
else
{
$prev_month = $month;
$prev_year = $year;
}
$tmpday = $tmpday;
$prev_month = $prev_month;
$prev_year = $prev_year;
$tmptime=dol_mktime(12,0,0,$month,$tmpday,$year,1,0);
$tmptime-=24*60*60*7;
$tmparray=dol_getdate($tmptime,true);
$prev_day = $tmparray['mday'];
if ($prev_day>$tmpday)
{
$prev_month = $month-1;
$prev_year = $year;
if ($prev_month==0)
{
$prev_month = 12;
$prev_year = $year-1;
}
}
$week = date("W",dol_mktime(0,0,0,$month,$tmpday,$year,$gm));
return array('year' => $year, 'month' => $month, 'week' => $week, 'first_day' => $tmpday, 'prev_year' => $prev_year, 'prev_month' => $prev_month, 'prev_day' => $prev_day);
} }
/** /**