-
Notifications
You must be signed in to change notification settings - Fork 1
/
ItcsReservationCalendarHelper.php
185 lines (149 loc) · 5.07 KB
/
ItcsReservationCalendarHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* mod_itcs_reservation_calendar - simple reservation calendar by it-conserv.de
* with bootstrap datepicker script https://github.com/uxsolutions/bootstrap-datepicker
* ------------------------------------------------------------------------
* @package itcs reservation calendar
* @author it-conserv.de
* @copyright 2022 it-conserv.de
* @license GNU/GPLv3 <http://www.gnu.org/licenses/gpl-3.0.de.html>
* @link https://it-conserv.de
* ------------------------------------------------------------------------
*/
namespace ITCS\Module\ItcsReservationCalendar\Site;
// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\Helper\ModuleHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Uri\Uri;
use \Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
class ItcsReservationCalendarHelper
{
/**
* Delete Days
*
* @param string $params as json String
* @param int $id as uniq id as integer
* @return object new params
*/
public static function deleteDays($params, $id)
{
$p = json_decode($params);
$del_days = $params->get('del_days')*-1; //change count days to negative integer
$today = date_create(date('d.m.Y'));
$i=0;
foreach($p AS $item=>$value){
if($item <> 'resdays'){
//Daten belassen
$new_params[$item] = $value;
}
else{
//--> old Day Params
foreach($value AS $cal_day){
$day = date_create($cal_day->cal_day);
$diff = date_diff($today,$day);
if ($diff->format("%R%a") >= $del_days){
//Daten behalten
//$d[$item.$i] = (object)array('cal_day'=>$cal_day->cal_day,'cal_res_color'=>$cal_day->cal_res_color,'cal_info'=>$cal_day->cal_info);
foreach($cal_day AS $index=>$v){
$new_day[$index]=$v;
}
$d[$item.$i] = (object)$new_day;
$i++;
}
}
$new_params[$item]= (object)$d;
}
}
//Update Data
$new = json_encode($new_params);
$new_params['json']=$new;
$db = Factory::getDbo();
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
$db->quoteName('params') . ' = ' . $db->quote($new)
);
// Conditions for which records should be updated.
$conditions = array(
$db->quoteName('id') . ' = ' . $db->quote($id)
);
$query->update($db->quoteName('#__modules'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
return (object)$new_params;
}
/**
* get Day - Preparing the days
*
* @param object $days containing cal_day, cal_day_count, cal_day_color, cal_day_info
* @param string $format contains the php date format e.g. 'd.m.Y'
* @param string $override the public info text
* @return array reservation days with informations
*/
public static function getDays($days, $format, $override)
{
$resdays = array();
$tz = Factory::getApplication()->get('offset');
foreach($days as $item){
if(!empty($item->cal_day)){
$cal_day_count = (!empty($item->cal_day_count))?intval($item->cal_day_count):1;
for($i=0; $i < $cal_day_count; $i++) {
$cal_day = new \DateTime($item->cal_day, new \DateTimeZone('UTC'));
$cal_day->setTimezone(new \DateTimeZone($tz));
$cal_day->setTime(0, 0, 0);
$cal_day->modify('+'.$i.' day');
$item_color = 'color' . $item->cal_day_color;
$resdays[] = (object)array(
't' => $cal_day->getTimestamp(),
'cal_day' => $cal_day->format($format),
'cal_day_info' => (!empty($override)) ? $override : $item->cal_day_info,
'cal_day_color' => $item_color
);
}
}
}
sort($resdays);
return $resdays;
}
/**
* getList - Create table List
*
* @param object $days containing cal_day, cal_day_count, cal_day_color, cal_day_info
* @param int $list_days_count Number of days to be displayed
* @param string $format contains the php date format e.g. 'd.m.Y'
* @return string html list next days
*/
public static function getList($days, $list_days_count, $format)
{
$counter = 1;
$today = new \DateTime();
$tz = Factory::getApplication()->get('offset');
$list = '';
foreach($days as $item){
if(!empty($item->cal_day)){
$cal_day_count = (!empty($item->cal_day_count))?intval($item->cal_day_count):1;
$day_from = new \DateTime($item->cal_day, new \DateTimeZone('UTC'));
$day_from->setTimezone(new \DateTimeZone($tz));
$day_from->setTime(0, 0, 0);
$date_to = new \DateTime($item->cal_day, new \DateTimeZone('UTC'));
$date_to->setTimezone(new \DateTimeZone($tz));
$date_to->setTime(0, 0, 0);
if($cal_day_count > 1){
date_add($date_to, date_interval_create_from_date_string(($cal_day_count - 1).' days'));
$to = ' - ' . $date_to->format($format);
}
else{
$to = '';
}
if ($list_days_count == 0 OR ($day_from > $today AND $counter <= $list_days_count)){
$list .= '<p style="line-height: 1em; font-size: 0.8em;"><strong>'.$day_from->format($format) . $to.'</strong><br />'.$item->cal_day_info.'</p>';
$counter ++;
}
}
}
return $list;
}
}