templates/Dp/CalendarBundle/Default/calendar.html.twig line 1

Open in your IDE?
  1. <script>
  2.     /*
  3.      * This file is part of the Symfocal Calendar v1.0.
  4.      *
  5.      * (c) Symfocal http://www.symfocal.com
  6.      * alban@symfocal.com
  7.      * 
  8.      * For the full copyright and license information, please view the LICENSE
  9.      * file that was distributed with this source code.
  10.      */
  11.     var gap{{item}} = 0; //global variable to follow what months to display
  12.             var month{{item}} = {{ month }};
  13.             var year{{item}} = {{ year }};
  14.             var item{{item}} = {{ item }};//item is set to first element
  15.     {% if admin %}
  16.         var clickMethod = 0;//defines click method to change states of bookings in admin
  17.     {% endif %}
  18.         jQuery(document).ready(function () {
  19.             var cal1 = $('#cal1{{item}}');
  20.             var cal2 = $('#cal2{{item}}');
  21.             var cal3 = $('#cal3{{item}}');
  22.             var cal4 = $('#cal4{{item}}');
  23.             var cal5 = $('#cal5{{item}}');
  24.             var cal6 = $('#cal6{{item}}');
  25.             var $select = $('#items_select{{item}}');
  26.     {% if admin %}
  27.             var $selectMethod = $('#states_select');//this is needed in admin to select click method applied for changing states
  28.     {% endif %}
  29.             $('#link_next{{item}}').attr('onclick', '').click(function (e) {
  30.                 e.preventDefault();
  31.                 changeMonths(1)
  32.             });
  33.             $('#link_prev{{item}}').attr('onclick', '').click(function (e) {
  34.                 e.preventDefault();
  35.                 changeMonths(-1)
  36.             });
  37.             function fillCalendar(mois, annee, cal)
  38.             {
  39.                 cal.css('display', 'none');
  40.                 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
  41.     {% if admin == false %}
  42.                 cal.find('a').click(function (e) {
  43.                     e.preventDefault();
  44.                 });//prevents the page from moving if <a> elements from calendar are clicked outside of admin mode.
  45.     {% endif %}
  46.                 var url = "{{ path('dp_calendar_ajax2') }}";
  47.                 var id = cal.attr('id');
  48.                 $.post(url, {
  49.                     month: mois,
  50.                     year: annee,
  51.                     itemId: item{{item}}
  52.                 }, function (data) {
  53.                     //the response is in the data variable
  54.                     if (data.responseCode == 200)
  55.                     {
  56.                         var days_previous_month = data.days_previous_month;
  57.                         var nb_days_prev = data.nb_days_prev;
  58.                         $('#' + id + '_title').html(data.current_month);
  59.                         var count = 1;
  60.                         while (count < 43)
  61.                         {
  62.     {% if admin %}
  63.                             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 
  64.                             {
  65.                                 $('#' + 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()})
  66.                             } else
  67.                             {
  68.                                 $('#' + id + '_cell_' + count).click(function (e) {
  69.                                     e.preventDefault();
  70.                                 });
  71.                             }
  72.     {% endif %}
  73.                             if (data.calendar[(count - 1)].booked == -1)
  74.                                 $('#' + id + '_cell_' + count).html(days_previous_month - nb_days_prev + count);
  75.                             else
  76.                                 $('#' + id + '_cell_' + count).html(data.calendar[(count - 1)].fill);
  77.                             if (data.calendar[(count - 1)].booked == 1)
  78.                                 $('#' + id + '_cell_' + count).addClass(data.calendar[(count - 1)].classe);
  79.                             else if (data.calendar[(count - 1)].booked < 0)
  80.                                 $('#' + id + '_cell_' + count).addClass("dates_preview");
  81.                             count++;
  82.                         }
  83.                         cal.css('display', 'block');
  84.                     } else if (data.responseCode == 400)//bad request
  85.                     {
  86.                         alert(data.message);
  87.                     } else
  88.                     {
  89.                         //any other error is treated here
  90.                         alert("An unexpeded error occured.");
  91.                     }
  92.                 });
  93.                 return false;
  94.             }
  95.             function changeMonths(step)//this function is set for displaying 3 months - to do: needs to be flexible based on number of months displayed
  96.             {
  97.                 gap{{item}} = gap{{item}} + step;
  98.                 var newMonth = month{{item}} + (3 * gap{{item}}) % 12;
  99.                 //var newYear = year{{item}} + Math.floor((3 * gap{{item}})/12);
  100.                 var gapYear = (3 * gap{{item}}) / 12;
  101.                 var newYear = year{{item}} + gapYear - gapYear % 1;
  102.                 if (newMonth <= 0)
  103.                 {
  104.                     newMonth += 12;
  105.                 }
  106.                 fillCalendar(newMonth - 12 * Math.floor((newMonth) / 13), newYear + Math.floor((newMonth) / 13), cal1);
  107.                 fillCalendar(newMonth + 1 - 12 * Math.floor((newMonth + 1) / 13), newYear + Math.floor((newMonth + 1) / 13), cal2);
  108.                 fillCalendar(newMonth + 2 - 12 * Math.floor((newMonth + 2) / 13), newYear + Math.floor((newMonth + 2) / 13), cal3);
  109.                 fillCalendar(newMonth + 3 - 12 * Math.floor((newMonth + 3) / 13), newYear + Math.floor((newMonth + 3) / 13), cal4);
  110.                 fillCalendar(newMonth + 4 - 12 * Math.floor((newMonth + 4) / 13), newYear + Math.floor((newMonth + 4) / 13), cal5);
  111.                 fillCalendar(newMonth + 5 - 12 * Math.floor((newMonth + 5) / 13), newYear + Math.floor((newMonth + 5) / 13), cal6);
  112.                 changeLegend();
  113.             }
  114.             $select.change(function () {//change months displayed when a new item is selected
  115.                 $select.find("option:selected").each(function () {
  116.                     item{{item}} = $(this).val();//change the javascript global variable following the item selected
  117.                     changeMonths(0);
  118.                 });
  119.             })
  120.             function changeLegend()
  121.             {
  122.                 var url = "{{ path('dp_calendar_ajax_change_state') }}";
  123.                 $.post(url, {
  124.                     itemId: item{{item}}
  125.                 }, function (data) {
  126.                     if (data.responseCode == 200) {
  127.                         html = '';
  128.                         $('.stateChange').remove();
  129.                         // esborrar el select states_select
  130.                         $("#states_select").find('option[value!="0"]').remove();
  131.                         for (index = 0; index < data.state.length; index++)
  132.                         {
  133.                             html = html + '<li class="stateChange white">';
  134.                             html = html + '<span><a href="#" class="' + data.state[index].class_returned + '"></a></span>';
  135.                             html = html + '<span class="legend_calendar">' + data.state[index].name_returned + '</span>';
  136.                             html = html + '</li>';
  137.                             $('#states_select').append('<option value="' + data.state[index].id_returned + '" ' + (index == 0 ? 'selected="selected"' : '') + '>' + data.state[index].name_returned + '</option>');
  138.                             if (index == 0)
  139.                                 clickMethod = data.state[index].id_returned;
  140.                         }
  141.                         $('#stateContainer').html(html);
  142.                     } else if (data.responseCode == 400) {//bad request
  143.                         alert(data.message);
  144.                     } else {
  145.                         alert("An unexpeded error occured.");
  146.                     }
  147.                 });
  148.             }
  149.     {% if admin %}
  150.             $selectMethod.change(function () {//change the click method in admin based on select drop down 
  151.                 $selectMethod.find("option:selected").each(function () {
  152.                     clickMethod = $(this).val();
  153.                 });
  154.             })
  155.             function changeState(e)//admn function to change class of the date that has been clicked on based on the new state
  156.             {
  157.                 e.preventDefault();
  158.                 var date = $(this).attr("title");
  159.                 var id = $(this).attr("id");
  160.                 var url = "{{ path('dp_calendar_ajax_admin') }}";
  161.                 $.post(url, {
  162.                     date: date,
  163.                     id: id,
  164.                     itemId: item{{item}},
  165.                     clickMethod: clickMethod
  166.                 }, function (data) {
  167.                     if (data.responseCode == 200) {
  168.                         var id_returned = data.id_returned;
  169.                         var date_returned = data.date_returned;
  170.                         var state = data.state;
  171.                         $('#' + id_returned).removeClass();
  172.                         $('#' + id_returned).addClass(state);
  173.                     } else if (data.responseCode == 400) {//bad request
  174.                         alert(data.message);
  175.                     } else {
  176.                         alert("An unexpeded error occured.");
  177.                     }
  178.                 });
  179.             }
  180.     {% endif %}
  181.             $(window).ready(function () {//fill calendars for the first time once page is loaded
  182.                 changeMonths(0);
  183.             });
  184.         });
  185. </script>
  186. <div id="manager">
  187.     {#
  188.     <div id="selects">
  189.         <select id="items_select">
  190.             {% for item in items %}
  191.                 <option value="{{ item.id }}">{{ item.name }}</option>
  192.             {% endfor %}
  193.         </select>
  194.         {% if admin %}
  195.             <select id="states_select">
  196.                 <option value="0">Esborrar estat</option>
  197.                 {% for state in states %}
  198.                     <option value="{{ state.id }}">{{ state.name }}</option>
  199.                 {% endfor %}
  200.             </select>
  201.         {% endif %}
  202.     </div>
  203.     #}
  204.     {#
  205.             {% if admin %}
  206.                 <div id="symfocal_admin_menu">
  207.                     <ul>
  208.                         <li><a class="admin_menu_selected" href="{{ path('dp_calendar_admin') }}">Manage bookings</a></li>
  209.                         <li><a href="{{ path('item') }}">Manage items</a></li>
  210.                         <li><a href="{{ path('state') }}">Manage states</a></li>
  211.                     </ul>
  212.                 </div>
  213.             {% endif %}
  214.     #}
  215. </div>
  216. <div id="control_nav{{item}}" class="control_nav" style="display:block!important;">
  217.     <a id="link_prev{{item}}" href="#">{{"Previ"|trans}}</a>
  218.     <a id="link_next{{item}}" href="#">{{"Següent"|trans}}</a>
  219. </div>
  220. <div id="calendars_container{{item}}">
  221.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal1'~item} %}
  222.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal2'~item} %}
  223.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal3'~item} %}
  224.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal4'~item} %}
  225.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal5'~item} %}
  226.     {% include 'Dp\\CalendarBundle\\Default\\cal.html.twig' with {'calId': 'cal6'~item} %}
  227. </div>
  228. <div id="legend{{item}}" class="calendar legend">
  229.     <div class="large-12 medium 12 cell">
  230.         <div class="callout">
  231.             <div class="grid-x grid-padding-x" id="stateContainer{{item}}">
  232.                 <div class="large-12 medium 12 cell">
  233.                     <h3><span>{{"Llegenda"|trans}}</span></h3>
  234.                 </div>
  235.                 {% for state in statesLellenda %}
  236.                     <div class="large-3 medium-3 cell">
  237.                         <a href="#" class="{{ state.class }}" style="cursor:default!important;"></a>
  238.                         <span class="legend_calendar">{{ (app.request.locale == 'ca'?state.name:attribute(state, 'name'~(app.request.locale|capitalize))) }}</span>
  239.                     </div>
  240.                 {% endfor %}
  241.             </div>
  242.         </div>   
  243.     </div>
  244. </div>