view.html.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments monthly calendar view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewcalendar extends JViewVAP |
| 20 | { |
| 21 | /** |
| 22 | * VikAppointments view display method. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | function display($tpl = null) |
| 27 | { |
| 28 | $app = JFactory::getApplication(); |
| 29 | $dbo = JFactory::getDbo(); |
| 30 | |
| 31 | // get view model |
| 32 | $model = JModelVAP::getInstance('calendar'); |
| 33 | |
| 34 | // set the toolbar |
| 35 | $this->addToolBar(); |
| 36 | |
| 37 | $filters = array(); |
| 38 | |
| 39 | $filters['id_emp'] = $app->getUserStateFromRequest('calendar.id_emp', 'id_emp', null, 'uint'); |
| 40 | $filters['id_ser'] = $app->getUserStateFromRequest('calendar.id_ser', 'id_ser', null, 'uint'); |
| 41 | $filters['year'] = $app->getUserStateFromRequest('calendar.year', 'year', null, 'uint'); |
| 42 | |
| 43 | if (empty($filters['year'])) |
| 44 | { |
| 45 | // use current year |
| 46 | $filters['year'] = (int) JHtml::fetch('date', 'now', 'Y'); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Fixed an issue that was not properly displaying the calendars |
| 51 | * of the selected year from the first month. |
| 52 | * |
| 53 | * @since 1.7.6 |
| 54 | */ |
| 55 | $filters['start'] = JFactory::getDate("{$filters['year']}-01-01")->format('Y-m-d'); |
| 56 | |
| 57 | $employees = array(); |
| 58 | |
| 59 | $q = $dbo->getQuery(true) |
| 60 | ->select($dbo->qn(array('e.id', 'e.nickname'))) |
| 61 | ->from($dbo->qn('#__vikappointments_employee', 'e')) |
| 62 | ->leftjoin($dbo->qn('#__vikappointments_reservation', 'r') . ' ON ' . $dbo->qn('e.id') . ' = ' . $dbo->qn('r.id_employee')) |
| 63 | ->group($dbo->qn('e.id')) |
| 64 | ->order('COUNT(' . $dbo->qn('r.id') . ') DESC'); |
| 65 | |
| 66 | $dbo->setQuery($q); |
| 67 | $dbo->execute(); |
| 68 | |
| 69 | if ($dbo->getNumRows()) |
| 70 | { |
| 71 | $employees = $dbo->loadObjectList(); |
| 72 | |
| 73 | if (!$filters['id_emp']) |
| 74 | { |
| 75 | // use first available employee |
| 76 | $filters['id_emp'] = $employees[0]->id; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $services = array(); |
| 81 | |
| 82 | // get employee model |
| 83 | $empModel = JModelVAP::getInstance('employee'); |
| 84 | |
| 85 | // get assigned services |
| 86 | $services = $empModel->getServices($filters['id_emp']); |
| 87 | |
| 88 | if ($services) |
| 89 | { |
| 90 | /** |
| 91 | * After switching search mode, we can face this scenario: |
| 92 | * - id_service = X |
| 93 | * - id_employee = ALL EMPLOYEES |
| 94 | * |
| 95 | * is switched to: |
| 96 | * - id_employee = Y (the first employee available) |
| 97 | * - id_service = X |
| 98 | * |
| 99 | * Since the service ID is not changed, we need to make |
| 100 | * sure that it is supported by the selected employee. |
| 101 | * If it isn't, we need to use the first available service. |
| 102 | * |
| 103 | * @since 1.6 |
| 104 | */ |
| 105 | if (!$filters['id_ser'] || !$this->isSupported($filters['id_ser'], $services)) |
| 106 | { |
| 107 | // use first available service |
| 108 | $filters['id_ser'] = $services[0]->id; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | $this->services = $services; |
| 113 | $this->employees = $employees; |
| 114 | |
| 115 | // fetch calendar by using the view model |
| 116 | $this->calendar = $model->getCalendar($filters); |
| 117 | |
| 118 | $this->filters = $filters; |
| 119 | |
| 120 | // display the template |
| 121 | parent::display($tpl); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Setting the toolbar. |
| 126 | * |
| 127 | * @return void |
| 128 | */ |
| 129 | protected function addToolBar() |
| 130 | { |
| 131 | // add menu title and some buttons to the page |
| 132 | JToolBarHelper::title(JText::translate('VAPMAINTITLEVIEWCALENDAR'), 'vikappointments'); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Checks if the specified service/employee is contained within the list. |
| 137 | * |
| 138 | * @param integer $needle The service/employee to search. |
| 139 | * @param array $haystack The haystack. |
| 140 | * |
| 141 | * @return boolean True if supported, otherwise false. |
| 142 | * |
| 143 | * @since 1.6 |
| 144 | */ |
| 145 | protected function isSupported($needle, array $haystack) |
| 146 | { |
| 147 | foreach ($haystack as $tmp) |
| 148 | { |
| 149 | if ($tmp->id == $needle) |
| 150 | { |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 |