<script>
/*
* This file is part of the Symfocal Calendar v1.0.
*
* (c) Symfocal http://www.symfocal.com
* alban@symfocal.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var gap{{item}} = 0; //global variable to follow what months to display
var month{{item}} = {{ month }};
var year{{item}} = {{ year }};
var item{{item}} = {{ item }};//item is set to first element
{% if admin %}
var clickMethod = 0;//defines click method to change states of bookings in admin
{% endif %}
jQuery(document).ready(function () {
var cal1 = $('#cal1{{item}}');
var cal2 = $('#cal2{{item}}');
var cal3 = $('#cal3{{item}}');
var cal4 = $('#cal4{{item}}');
var cal5 = $('#cal5{{item}}');
var cal6 = $('#cal6{{item}}');
var $select = $('#items_select{{item}}');
{% if admin %}
var $selectMethod = $('#states_select');//this is needed in admin to select click method applied for changing states
{% endif %}
$('#link_next{{item}}').attr('onclick', '').click(function (e) {
e.preventDefault();
changeMonths(1)
});
$('#link_prev{{item}}').attr('onclick', '').click(function (e) {
e.preventDefault();
changeMonths(-1)
});
function fillCalendar(mois, annee, cal)
{
cal.css('display', 'none');
cal.find('a').removeClass().unbind();//clear classes and events of <a> elements inside the calendar before applying the new styles and events based on ajax response
{% if admin == false %}
cal.find('a').click(function (e) {
e.preventDefault();
});//prevents the page from moving if <a> elements from calendar are clicked outside of admin mode.
{% endif %}
var url = "{{ path('dp_calendar_ajax2') }}";
var id = cal.attr('id');
$.post(url, {
month: mois,
year: annee,
itemId: item{{item}}
}, function (data) {
//the response is in the data variable
if (data.responseCode == 200)
{
var days_previous_month = data.days_previous_month;
var nb_days_prev = data.nb_days_prev;
$('#' + id + '_title').html(data.current_month);
var count = 1;
while (count < 43)
{
{% if admin %}
if (data.calendar[(count - 1)].dates != "")//in admin, if the date is valid, we attach an action to change date state on the click event
{
$('#' + id + '_cell_' + count).attr("title", data.calendar[(count - 1)].dates).click(changeState);//This is a trick to pass an event to a non-anonymous function, which is not posible with the classic .click(function(e){myFunction()})
} else
{
$('#' + id + '_cell_' + count).click(function (e) {
e.preventDefault();
});
}
{% endif %}
if (data.calendar[(count - 1)].booked == -1)
$('#' + id + '_cell_' + count).html(days_previous_month - nb_days_prev + count);
else
$('#' + id + '_cell_' + count).html(data.calendar[(count - 1)].fill);
if (data.calendar[(count - 1)].booked == 1)
$('#' + id + '_cell_' + count).addClass(data.calendar[(count - 1)].classe);
else if (data.calendar[(count - 1)].booked < 0)
$('#' + id + '_cell_' + count).addClass("dates_preview");
count++;
}
cal.css('display', 'block');
} else if (data.responseCode == 400)//bad request
{
alert(data.message);
} else
{
//any other error is treated here
alert("An unexpeded error occured.");
}
});
return false;
}
function changeMonths(step)//this function is set for displaying 3 months - to do: needs to be flexible based on number of months displayed
{
gap{{item}} = gap{{item}} + step;
var newMonth = month{{item}} + (3 * gap{{item}}) % 12;
//var newYear = year{{item}} + Math.floor((3 * gap{{item}})/12);
var gapYear = (3 * gap{{item}}) / 12;
var newYear = year{{item}} + gapYear - gapYear % 1;
if (newMonth <= 0)
{
newMonth += 12;
}
fillCalendar(newMonth - 12 * Math.floor((newMonth) / 13), newYear + Math.floor((newMonth) / 13), cal1);
fillCalendar(newMonth + 1 - 12 * Math.floor((newMonth + 1) / 13), newYear + Math.floor((newMonth + 1) / 13), cal2);
fillCalendar(newMonth + 2 - 12 * Math.floor((newMonth + 2) / 13), newYear + Math.floor((newMonth + 2) / 13), cal3);
fillCalendar(newMonth + 3 - 12 * Math.floor((newMonth + 3) / 13), newYear + Math.floor((newMonth + 3) / 13), cal4);
fillCalendar(newMonth + 4 - 12 * Math.floor((newMonth + 4) / 13), newYear + Math.floor((newMonth + 4) / 13), cal5);
fillCalendar(newMonth + 5 - 12 * Math.floor((newMonth + 5) / 13), newYear + Math.floor((newMonth + 5) / 13), cal6);
changeLegend();
}
$select.change(function () {//change months displayed when a new item is selected
$select.find("option:selected").each(function () {
item{{item}} = $(this).val();//change the javascript global variable following the item selected
changeMonths(0);
});
})
function changeLegend()
{
var url = "{{ path('dp_calendar_ajax_change_state') }}";
$.post(url, {
itemId: item{{item}}
}, function (data) {
if (data.responseCode == 200) {
html = '';
$('.stateChange').remove();
// esborrar el select states_select
$("#states_select").find('option[value!="0"]').remove();
for (index = 0; index < data.state.length; index++)
{
html = html + '<li class="stateChange white">';
html = html + '<span><a href="#" class="' + data.state[index].class_returned + '"></a></span>';
html = html + '<span class="legend_calendar">' + data.state[index].name_returned + '</span>';
html = html + '</li>';
$('#states_select').append('<option value="' + data.state[index].id_returned + '" ' + (index == 0 ? 'selected="selected"' : '') + '>' + data.state[index].name_returned + '</option>');
if (index == 0)
clickMethod = data.state[index].id_returned;
}
$('#stateContainer').html(html);
} else if (data.responseCode == 400) {//bad request
alert(data.message);
} else {
alert("An unexpeded error occured.");
}
});
}
{% if admin %}
$selectMethod.change(function () {//change the click method in admin based on select drop down
$selectMethod.find("option:selected").each(function () {
clickMethod = $(this).val();
});
})
function changeState(e)//admn function to change class of the date that has been clicked on based on the new state
{
e.preventDefault();
var date = $(this).attr("title");
var id = $(this).attr("id");
var url = "{{ path('dp_calendar_ajax_admin') }}";
$.post(url, {
date: date,
id: id,
itemId: item{{item}},
clickMethod: clickMethod
}, function (data) {
if (data.responseCode == 200) {
var id_returned = data.id_returned;
var date_returned = data.date_returned;
var state = data.state;
$('#' + id_returned).removeClass();
$('#' + id_returned).addClass(state);
} else if (data.responseCode == 400) {//bad request
alert(data.message);
} else {
alert("An unexpeded error occured.");
}
});
}
{% endif %}
$(window).ready(function () {//fill calendars for the first time once page is loaded
changeMonths(0);
});
});
</script>
<div id="manager">
{#
<div id="selects">
<select id="items_select">
{% for item in items %}
<option value="{{ item.id }}">{{ item.name }}</option>
{% endfor %}
</select>
{% if admin %}
<select id="states_select">
<option value="0">Esborrar estat</option>
{% for state in states %}
<option value="{{ state.id }}">{{ state.name }}</option>
{% endfor %}
</select>
{% endif %}
</div>
#}
{#
{% if admin %}
<div id="symfocal_admin_menu">
<ul>
<li><a class="admin_menu_selected" href="{{ path('dp_calendar_admin') }}">Manage bookings</a></li>
<li><a href="{{ path('item') }}">Manage items</a></li>
<li><a href="{{ path('state') }}">Manage states</a></li>
</ul>
</div>
{% endif %}
#}
</div>
<div id="control_nav{{item}}" class="control_nav" style="display:block!important;">
<a id="link_prev{{item}}" href="#">{{"Previ"|trans}}</a>
<a id="link_next{{item}}" href="#">{{"Següent"|trans}}</a>
</div>
<div id="calendars_container{{item}}">
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal1'~item} %}
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal2'~item} %}
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal3'~item} %}
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal4'~item} %}
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal5'~item} %}
{% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal6'~item} %}
</div>
<div id="legend{{item}}" class="calendar legend">
<div class="large-12 medium 12 cell">
<div class="callout">
<div class="grid-x grid-padding-x" id="stateContainer{{item}}">
<div class="large-12 medium 12 cell">
<h3><span>{{"Llegenda"|trans}}</span></h3>
</div>
{% for state in statesLellenda %}
<div class="large-3 medium-3 cell">
<a href="#" class="{{ state.class }}" style="cursor:default!important;"></a>
<span class="legend_calendar">{{ (app.request.locale == 'ca'?state.name:attribute(state, 'name'~(app.request.locale|capitalize))) }}</span>
</div>
{% endfor %}
</div>
</div>
</div>
</div>