I have overridden component com_content and category view inside, so it looks like:
/templates/my_template/html/com_content/category
needed file is blog.php. Here outputs my php calendar.
<?php
JHtml::_('jquery.framework');
$document = JFactory::getDocument();
$document->addScript('/space/media/media/js/mainscript.js');
function days_in_month($month, $year)
{
// calculate number of days in a month
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
$currentYear = date('Y');
$currentMonth = date('n');
$rusmonth = array('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');
$dayofmonth = days_in_month($currentMonth, $currentYear);;
$day_count = 1;
$num = 0;
for($i = 0; $i < 7; $i++)
{
$dayofweek = date('w',
mktime(0, 0, 0, date('m'), $day_count, date('Y')));
$dayofweek = $dayofweek - 1;
if($dayofweek == -1) $dayofweek = 6;
if($dayofweek == $i)
{
$week[$num][$i] = $day_count;
$day_count++;
}
else
{
$week[$num][$i] = "";
}
}
while(true)
{
$num++;
for($i = 0; $i < 7; $i++)
{
$week[$num][$i] = $day_count;
$day_count++;
if($day_count > $dayofmonth) break;
}
if($day_count > $dayofmonth) break;
}
echo '<div> <span id="prev" data-month="'.$currentMonth.'" data-year="'.$currentYear.'"><</span> '.$rusmonth[$currentMonth-1].' <span id="next" data-month="'.$currentMonth.'" data-year="'.$currentYear.'">></span> </div>';
echo '<div id="calendar_wrap">';
echo '<table border=1>';
echo '<tr>
<th>ПН</th>
<th>ВТ</th>
<th>СР</th>
<th>ЧТ</th>
<th>ПТ</th>
<th>СБ</th>
<th>ВС</th>
</tr>';
for($i = 0; $i < count($week); $i++)
{
echo "<tr>";
for($j = 0; $j < 7; $j++)
{
if(!empty($week[$i][$j]))
{
if($j == 5 || $j == 6)
echo "<td><font color=red>".$week[$i][$j]."</font></td>";
else echo "<td>".$week[$i][$j]."</td>";
}
else echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
echo '</div>';
?>
at the top of file i add my script which switching months.
Here is the code of mainscript.js
:
var j = jQuery.noConflict()
j(document).ready(function(){
j('#next').click(function(){
var month = j(this).data('month');
var year = j(this).data('year');
if(month == 12){
year +=1;
month = 1;
}
else{
month++;
}
j.ajax({
url: "index.php?option=com_content&view=category&task=getAjaxData&format=raw",
type: "POST",
async: true,
data: {
month: month,
year: year
},
success: function(data){
j('#calendar_wrap').html(data);
j(this).data('month', month);
j(this).data('year', year);
console.log('month: '+j(this).data('month')+' year: '+j(this).data('year'));
}
});
})
j('#prev').click(function(){
var month = j(this).data('month');
var year = j(this).data('year');
if(month == 1){
year -=1;
month = 12;
}
else{
month--;
}
j.ajax({
url: "index.php?option=com_content&view=category&task=getAjaxData&format=raw",
type: "POST",
async: true,
data: {
month: month,
year: year
},
success: function(data){
j('#calendar_wrap').html(data);
j(this).data('month', month);
j(this).data('year', year);
console.log('month: '+j(this).data('month')+' year: '+j(this).data('year'));
}
});
})
});
And to make my script work i have added to /com_content/controller.php
method
public function getAjaxData()
{
JHtml::_('jquery.framework');
$document = JFactory::getDocument();
$document->addScript('/space/media/media/js/mainscript.js');
function days_in_month($month, $year)
{
// calculate number of days in a month
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
// Вычисляем число дней в текущем месяце
$currentYear = date('Y');
$currentMonth = date('n');
if(isset($_REQUEST['year']))
$currentYear = $_REQUEST['year'];
if(isset($_REQUEST['month']))
$currentMonth = $_REQUEST['month'];
$rusmonth = array('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');
$dayofmonth = days_in_month($currentMonth, $currentYear);
// Счётчик для дней месяца
$day_count = 1;
// 1. Первая неделя
$num = 0;
for($i = 0; $i < 7; $i++)
{
// Вычисляем номер дня недели для числа
$dayofweek = date('w',
mktime(0, 0, 0, $currentMonth, $day_count, $currentYear));
// Приводим к числа к формату 1 - понедельник, ..., 6 - суббота
$dayofweek = $dayofweek - 1;
if($dayofweek == -1) $dayofweek = 6;
if($dayofweek == $i)
{
// Если дни недели совпадают,
// заполняем массив $week
// числами месяца
$week[$num][$i] = $day_count;
$day_count++;
}
else
{
$week[$num][$i] = "";
}
}
// 2. Последующие недели месяца
while(true)
{
$num++;
for($i = 0; $i < 7; $i++)
{
$week[$num][$i] = $day_count;
$day_count++;
// Если достигли конца месяца - выходим
// из цикла
if($day_count > $dayofmonth) break;
}
// Если достигли конца месяца - выходим
// из цикла
if($day_count > $dayofmonth) break;
}
// 3. Выводим содержимое массива $week
// в виде календаря
// Выводим таблицу
echo '<div> <span id="prev" data-month="'.$currentMonth.'" data-year="'.$currentYear.'"><</span> '.$rusmonth[$currentMonth-1].' <span id="next" data-month="'.$currentMonth.'" data-year="'.$currentYear.'">></span> </div>';
echo '<div id="calendar_wrap">';
echo '<table border=1>';
echo '<tr>
<th>ПН</th>
<th>ВТ</th>
<th>СР</th>
<th>ЧТ</th>
<th>ПТ</th>
<th>СБ</th>
<th>ВС</th>
</tr>';
for($i = 0; $i < count($week); $i++)
{
echo "<tr>";
for($j = 0; $j < 7; $j++)
{
if(!empty($week[$i][$j]))
{
// Если имеем дело с субботой и воскресенья
// подсвечиваем их
if($j == 5 || $j == 6)
echo "<td><font color=red>".$week[$i][$j]."</font></td>";
else echo "<td>".$week[$i][$j]."</td>";
}
else echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
echo '</div>';
exit;
}
It is doing exactly the same, as blog.php, but with new values that were transfered by ajax. So there are two ways i see to solve the problem.
- Find a way to use my method in controller like a bridge between script and blog.php, so i have only one place to output calendar.
- add my script to controller, so i could use my script not only from blog.php, but i don't like that way..
So i'll be happy to get any ideas from you