-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.php
265 lines (223 loc) · 9.36 KB
/
script.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?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
* ------------------------------------------------------------------------
*/
// No direct access to this file
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Log\Log;
Log::addLogger(
// Definiert den Namen der Datei, in welche der Logger protokolliert.
['text_file' => 'log_itcsResCal.php'],
// Welche Typen von Meldungen soll der Logger protokollieren? (INFO, ERROR, ...)
Log::ALL,
// Welche Kategorien von Meldungen soll der Logger protokollieren?
['itcsResCalMsg']
);
/**
* Script file of the mod_itcs_reservation_calendar plugin for migration to the new version
* https://docs.joomla.org/J4.x:Creating_a_Simple_Module#Creating_script.php
*
* Create Logging
* https://wicked-software.de/debuggen-mit-dem-joomla-logging
* https://docs.joomla.org/Using_JLog
*/
class mod_Itcs_Reservation_CalendarInstallerScript
{
/**
* Function called before extension installation/update/removal procedure commences
*
* @param string $type The type of change (install, update or discover_install, not uninstall)
* @param InstallerAdapter $parent The class calling this method
*
* @return boolean True on success
*/
function preflight($type, $parent) {
Log::add('Start Preflight', LOG::INFO, 'itcsResCalMsg');
$db = Factory::getDbo();
$msg = ''; // Message
// check old version
$query = $db->getQuery(true);
$query->select($db->quoteName('manifest_cache'));
$query->from($db->quoteName('#__extensions'));
$query->where($db->quoteName('element') . ' = ' . $db->quote('mod_itcs_reservation_calendar'));
$db->setQuery($query);
if($result = $db->loadResult()){
$res = json_decode($result);
// get old version params
if (version_compare($res->version, '4.0.0', '<')){
//Request used modules
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id','params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = ' . $db->quote('mod_itcs_reservation_calendar'));
$db->setQuery($query);
// Update Parameters
if($row = $db->loadAssocList()){
foreach($row AS $i){
$p = json_decode($i['params']);
Log::add('Old Params (id: '.$i['id'].'): ', LOG::INFO, 'itcsResCalMsg');
Log::add(print_r($p, true), LOG::INFO, 'itcsResCalMsg');
$new_params = new stdClass();
$new_params->colors = new stdClass();
$new_params->color_text = new stdClass();
foreach($p AS $item=>$value){
switch ($item) {
case 'color1':
case 'color2':
case 'color3':
$v = $this->hex2RGB($value, true, ',');
$new_params->colors->{$item} = 'rgb('.$v.')';
//$colors[$item] = $value;
break;
case 'color1_text':
case 'color2_text':
case 'color3_text':
$new_params->color_text->{$item} = $value;
//$ctext[$item] = $value;
break;
default:
$new_params->{$item} = $value;
}
}
Log::add('New Params (id: '.$i['id'].'): ', LOG::INFO, 'itcsResCalMsg');
Log::add(print_r($new_params, true), LOG::INFO, 'itcsResCalMsg');
// Update the params for new version
$object = new stdClass();
$object->id = $i['id'];
$object->params = json_encode($new_params);
$result = Factory::getDbo()->updateObject('#__modules', $object, 'id');
$msg .= '<br><i class="icon icon-ok"></i>Parameters for Modul with ID: '.$i['id'].' was updated';
Log::add('Module with id: '.$i['id'].' was updated ', LOG::INFO, 'itcsResCalMsg');
}
}
// Delete old files and folder
if(Folder::exists(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/helper')){
if(Folder::delete(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/helper')){
Log::add('Delete /helper Folder OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete /helper Folder Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(Folder::exists(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/php')){
if(Folder::delete(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/php')){
Log::add('Delete /assets/php Folder OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete /assets/php Folder Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(Folder::exists(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/images')){
if(Folder::delete(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/images')){
Log::add('Delete /assets/images Folder OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete /assets/images Folder Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(Folder::exists(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/language')){
if(Folder::delete(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/language')){
Log::add('Delete /language Folder OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete /language Folder Error', LOG::ERROR, 'itcsResCalMsg');
}
}
// Files
if(File::exists(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/elements/admin.css')){
if(File::delete(JPATH_ROOT.'/modules/mod_itcs_reservation_calendar/assets/elements/admin.css')){
Log::add('Delete /assets/elements/admin.css File OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete /assets/elements/admin.css File Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(File::exists(JPATH_ROOT.'/language/de-DE/de-DE.mod_itcs_reservation_calendar.ini')){
if(File::delete(JPATH_ROOT.'/language/de-DE/de-DE.mod_itcs_reservation_calendar.ini')){
Log::add('Delete DE-language file OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete DE-language file Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(File::exists(JPATH_ROOT.'/language/en-GB/en-GB.mod_itcs_reservation_calendar.ini')){
if(File::delete(JPATH_ROOT.'/language/en-GB/en-GB.mod_itcs_reservation_calendar.ini')){
Log::add('Delete EN-language file OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete EN-language file Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(File::exists(JPATH_ROOT.'/language/fi-FI/fi-FI.mod_itcs_reservation_calendar.ini')){
if(File::delete(JPATH_ROOT.'/language/fi-FI/fi-FI.mod_itcs_reservation_calendar.ini')){
Log::add('Delete FI-language file OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete FI-language file Error', LOG::ERROR, 'itcsResCalMsg');
}
}
if(File::exists(JPATH_ROOT.'/language/fr-FR/fr-FR.mod_itcs_reservation_calendar.ini')){
if(File::delete(JPATH_ROOT.'/language/fr-FR/fr-FR.mod_itcs_reservation_calendar.ini')){
Log::add('Delete FR-language file OK', LOG::INFO, 'itcsResCalMsg');
}
else{
Log::add('Delete FR-language file path Error', LOG::ERROR, 'itcsResCalMsg');
}
}
// Output Message
if(!empty($msg)){
echo '<div class="alert alert-info span6 offset3">';
echo '<p><strong>'.$type.'</strong></p>';
echo '<p>The module with parameters were migrated from '.$res->version.' to the new version.</p>';
echo '<p>'.$msg.'</p>';
echo '</div>';
}
Log::add('END Preflight with update', LOG::INFO, 'itcsResCalMsg');
return true;
}
Log::add('END Preflight without update', LOG::INFO, 'itcsResCalMsg');
return true;
}
Log::add('END Preflight with DB ERROR', LOG::ERROR, 'itcsResCalMsg');
return true;
}
/**
* Convert a hexa decimal color code to its RGB equivalent
*
* License - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* Code base: http://www.php.net/manual/en/function.hexdec.php#99478
*
* @param string $hexStr (hexadecimal color value)
* @param boolean $returnAsString (if set true, returns the value separated by the separator character. Otherwise returns associative array)
* @param string $seperator (to separate RGB values. Applicable only if second parameter is true.)
* @return array or string (depending on second parameter. Returns False if invalid hex color value)
*
*/
private function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {
$hexStr = preg_replace('/[^0-9a-f]/i', '', $hexStr); // Gets a proper hex string
//if shorthand notation, need some string manipulations
if(strlen($hexStr) == 3)
$hexStr = preg_replace('/([0-9A-F]{1})/i','$1$1',$hexStr);
if ( strlen($hexStr) != 6) {//Invalid hex color code
return false;
}
$rgbArray = array();
$colorVal = hexdec($hexStr);
$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
$rgbArray['blue'] = 0xFF & $colorVal;
// returns the rgb string or the associative array
return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray;
}
}