view.html.php
253 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 employee management view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewmanageemployee extends JViewVAP |
| 20 | { |
| 21 | /** |
| 22 | * VikAppointments view display method. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | function display($tpl = null) |
| 27 | { |
| 28 | $dbo = JFactory::getDbo(); |
| 29 | $app = JFactory::getApplication(); |
| 30 | $input = $app->input; |
| 31 | |
| 32 | $ids = $input->getUint('cid', array()); |
| 33 | $type = $ids ? 'edit' : 'new'; |
| 34 | |
| 35 | // set the toolbar |
| 36 | $this->addToolBar($type); |
| 37 | |
| 38 | // load wd options |
| 39 | |
| 40 | $wd_options = array(); |
| 41 | $wd_options['hide_past_wd'] = $input->cookie->getBool('vikappointments_hide_past_wd', true); |
| 42 | |
| 43 | $employee = array(); |
| 44 | |
| 45 | $worktime_week = array(); |
| 46 | $worktime_date = array(); |
| 47 | |
| 48 | if ($type == 'edit') |
| 49 | { |
| 50 | $q = $dbo->getQuery(true) |
| 51 | ->select('*') |
| 52 | ->from($dbo->qn('#__vikappointments_employee')) |
| 53 | ->where($dbo->qn('id') . ' = ' . $ids[0]); |
| 54 | |
| 55 | $dbo->setQuery($q, 0, 1); |
| 56 | $employee = $dbo->loadObject(); |
| 57 | |
| 58 | $config = VAPFactory::getConfig(); |
| 59 | |
| 60 | $q = $dbo->getQuery(true) |
| 61 | ->select('*') |
| 62 | ->from($dbo->qn('#__vikappointments_emp_worktime')) |
| 63 | ->where(array( |
| 64 | $dbo->qn('id_employee') . ' = ' . $ids[0], |
| 65 | $dbo->qn('id_service') . ' = -1', |
| 66 | )) |
| 67 | ->order(array( |
| 68 | $dbo->qn('ts') . ' ASC', |
| 69 | $dbo->qn('day') . ' ASC', |
| 70 | $dbo->qn('fromts') . ' ASC', |
| 71 | $dbo->qn('closed') . ' ASC', |
| 72 | )); |
| 73 | |
| 74 | if ($wd_options['hide_past_wd']) |
| 75 | { |
| 76 | // exclude all working days created for past days |
| 77 | $threshold = strtotime('00:00:00'); |
| 78 | |
| 79 | /** |
| 80 | * @todo Implement start_publishing and end_publishing restrictions. |
| 81 | * Exclude the working days with end publishing lower than the today. |
| 82 | */ |
| 83 | |
| 84 | $q->andWhere(array( |
| 85 | $dbo->qn('ts') . ' = -1', |
| 86 | $dbo->qn('ts') . ' >= ' . $threshold, |
| 87 | )); |
| 88 | } |
| 89 | |
| 90 | $dbo->setQuery($q); |
| 91 | |
| 92 | foreach ($dbo->loadObjectList() as $w) |
| 93 | { |
| 94 | // create shorten aliases |
| 95 | $w->from = $w->fromts; |
| 96 | $w->to = $w->endts; |
| 97 | |
| 98 | if ($w->ts == -1) |
| 99 | { |
| 100 | if (!isset($worktime_week[$w->day])) |
| 101 | { |
| 102 | $worktime_week[$w->day] = array(); |
| 103 | } |
| 104 | |
| 105 | // group under day of the week |
| 106 | $worktime_week[$w->day][] = $w; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | // create instance from GMT date |
| 111 | $dt = JFactory::getDate($w->tsdate); |
| 112 | |
| 113 | // create YMD date |
| 114 | $w->ymd = $dt->format('Ymd'); |
| 115 | // create system date |
| 116 | $w->date = $dt->format($config->get('dateformat')); |
| 117 | |
| 118 | if (!isset($worktime_date[$w->ymd])) |
| 119 | { |
| 120 | $worktime_date[$w->ymd] = array(); |
| 121 | } |
| 122 | |
| 123 | // group under special day |
| 124 | $worktime_date[$w->ymd][] = $w; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (empty($employee)) |
| 130 | { |
| 131 | $employee = (object) $this->getBlankItem(); |
| 132 | } |
| 133 | |
| 134 | // use employee data stored in user state |
| 135 | $this->injectUserStateData($employee, 'vap.employee.data'); |
| 136 | |
| 137 | // fetch CMS users |
| 138 | $inner = $dbo->getQuery(true) |
| 139 | ->select(1) |
| 140 | ->from($dbo->qn('#__vikappointments_employee', 'a')) |
| 141 | ->where($dbo->qn('a.jid') . ' = ' . $dbo->qn('u.id')); |
| 142 | |
| 143 | $q = $dbo->getQuery(true); |
| 144 | |
| 145 | $q->select($dbo->qn(array('u.id', 'u.name', 'u.username', 'u.email'))) |
| 146 | ->from($dbo->qn('#__users', 'u')) |
| 147 | ->where($dbo->qn('u.id') . ' = ' . (int) $employee->jid, 'OR') |
| 148 | ->where('NOT EXISTS (' . $inner . ')') |
| 149 | ->order($dbo->qn('u.name') . ' ASC'); |
| 150 | |
| 151 | $dbo->setQuery($q); |
| 152 | $users = $dbo->loadObjectList(); |
| 153 | |
| 154 | // subscriptions |
| 155 | |
| 156 | $hasSubscr = VikAppointments::isSubscriptions(); |
| 157 | |
| 158 | // import custom fields renderer and loader (as dependency) |
| 159 | VAPLoader::import('libraries.customfields.renderer'); |
| 160 | |
| 161 | // get relevant custom fields only |
| 162 | $this->customFields = VAPCustomFieldsLoader::getInstance() |
| 163 | ->employees() |
| 164 | ->translate() |
| 165 | ->noRequiredCheckbox() |
| 166 | ->fetch(); |
| 167 | |
| 168 | $this->employee = $employee; |
| 169 | $this->worktimeWeek = $worktime_week; |
| 170 | $this->worktimeDate = $worktime_date; |
| 171 | $this->wdOptions = $wd_options; |
| 172 | $this->users = $users; |
| 173 | $this->hasSubscr = $hasSubscr; |
| 174 | |
| 175 | // display the template |
| 176 | parent::display($tpl); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Setting the toolbar. |
| 181 | * |
| 182 | * @param string $type The request type (new or edit). |
| 183 | * |
| 184 | * @return void |
| 185 | */ |
| 186 | protected function addToolBar($type) |
| 187 | { |
| 188 | // add menu title and some buttons to the page |
| 189 | if ($type == 'edit') |
| 190 | { |
| 191 | JToolBarHelper::title(JText::translate('VAPMAINTITLEEDITEMPLOYEE'), 'vikappointments'); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | JToolBarHelper::title(JText::translate('VAPMAINTITLENEWEMPLOYEE'), 'vikappointments'); |
| 196 | } |
| 197 | |
| 198 | $user = JFactory::getUser(); |
| 199 | |
| 200 | if ($user->authorise('core.edit', 'com_vikappointments') |
| 201 | || $user->authorise('core.create', 'com_vikappointments')) |
| 202 | { |
| 203 | JToolbarHelper::apply('employee.save', JText::translate('VAPSAVE')); |
| 204 | JToolbarHelper::save('employee.saveclose', JText::translate('VAPSAVEANDCLOSE')); |
| 205 | } |
| 206 | |
| 207 | if ($user->authorise('core.edit', 'com_vikappointments') |
| 208 | && $user->authorise('core.create', 'com_vikappointments')) |
| 209 | { |
| 210 | JToolbarHelper::save2new('employee.savenew', JText::translate('VAPSAVEANDNEW')); |
| 211 | } |
| 212 | |
| 213 | if ($type == 'edit' && $user->authorise('core.create', 'com_vikappointments')) |
| 214 | { |
| 215 | JToolbarHelper::save2copy('employee.savecopy', JText::translate('VAPSAVEASCOPY')); |
| 216 | } |
| 217 | |
| 218 | JToolBarHelper::cancel('employee.cancel', $type == 'edit' ? 'JTOOLBAR_CLOSE' : 'JTOOLBAR_CANCEL'); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns a blank item. |
| 223 | * |
| 224 | * @return array A blank item for new requests. |
| 225 | */ |
| 226 | protected function getBlankItem() |
| 227 | { |
| 228 | return array( |
| 229 | 'id' => 0, |
| 230 | 'firstname' => '', |
| 231 | 'lastname' => '', |
| 232 | 'nickname' => '', |
| 233 | 'alias' => '', |
| 234 | 'email' => '', |
| 235 | 'phone' => '', |
| 236 | 'notify' => 0, |
| 237 | 'showphone' => 0, |
| 238 | 'showemail' => 1, |
| 239 | 'quick_contact' => 0, |
| 240 | 'listable' => 1, |
| 241 | 'image' => '', |
| 242 | 'note' => '', |
| 243 | 'jid' => -1, |
| 244 | 'id_group' => 0, |
| 245 | 'active_to' => -1, |
| 246 | 'active_to_date' => '', |
| 247 | 'timezone' => '', |
| 248 | 'ical_url' => '', |
| 249 | 'synckey' => VikAppointments::generateSerialCode(12, 'employee-synckey'), |
| 250 | ); |
| 251 | } |
| 252 | } |
| 253 |