Passed
Push — master ( 1f7c29...9231af )
by Michael
09:42
created

class/Utility.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace XoopsModules\Extcal;
4
5
/**
6
 * classGenerator
7
 * walls_watermarks.
8
 *
9
 * You may not change or alter any portion of this comment or credits
10
 * of supporting developers from this source code or any supporting source code
11
 * which is considered copyrighted (c) material of the original comment or credit authors.
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 *
16
 *
17
 *
18
 * L'utilisation de ce formulaire d'adminitration suppose
19
 * que la classe correspondante de la table a été générées avec classGenerator
20
 **/
21
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
22
23
use XoopsModules\Extcal;
24
use XoopsModules\Extcal\Common;
25
use XoopsModules\Extcal\Constants;
26
27
/**
28
 * Class Utility
29
 */
30
class Utility extends Common\SysUtility
31
{
32
    //--------------- Custom module methods -----------------------------
33
34
    /**
35
     * @param $eventId
36
     *
37
     * @return array
38
     */
39
    public static function getEvent($eventId)
40
    {
41
        $eventHandler = Extcal\Helper::getInstance()->getHandler(\_EXTCAL_CLN_EVENT);
42
        $event        = $eventHandler->getEvent($eventId);
0 ignored issues
show
The method getEvent() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $event        = $eventHandler->getEvent($eventId);
Loading history...
43
        $t            = $event->getVars();
44
        $data         = [];
45
        //        while (list($key, $val) = each($t)) {
46
        foreach ($t as $key => $val) {
47
            $data[$key] = $val['value'];
48
        }
49
50
        return $data;
51
    }
52
53
    /**
54
     * @param $REQUEST
55
     * @param $event_picture1
56
     * @param $event_picture2
57
     */
58
    public static function loadImg($REQUEST, &$event_picture1, &$event_picture2)
59
    {
60
        ///////////////////////////////////////////////////////////////////////////////
61
        $uploaddir_event = XOOPS_ROOT_PATH . '/uploads/extcal/';
62
        $uploadurl_event = XOOPS_URL . '/uploads/extcal/';
63
        //$picture = '';
64
        for ($j = 1; $j < 3; ++$j) {
65
            $delimg = @$REQUEST['delimg_' . $j . ''];
66
            $delimg = isset($delimg) ? (int)$delimg : 0;
67
            if (0 == $delimg && !empty($REQUEST['xoops_upload_file'][$j])) {
68
                $upload = new \XoopsMediaUploader(
69
                    $uploaddir_event, [
70
                    'image/gif',
71
                    'image/jpeg',
72
                    'image/pjpeg',
73
                    'image/x-png',
74
                    'image/png',
75
                    'image/jpg',
76
                ], 3145728, null, null
77
                );
78
                if ($upload->fetchMedia($REQUEST['xoops_upload_file'][$j])) {
79
                    $upload->setPrefix('event_');
80
                    $upload->fetchMedia($REQUEST['xoops_upload_file'][$j]);
81
                    if (!$upload->upload()) {
82
                        $errors = $upload->getErrors();
83
                        \redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors);
84
                    } else {
85
                        if (1 == $j) {
86
                            $event_picture1 = $upload->getSavedFileName();
87
                        } elseif (2 == $j) {
88
                            $event_picture2 = $upload->getSavedFileName();
89
                        }
90
                    }
91
                } elseif (!empty($REQUEST['file' . $j])) {
92
                    if (1 == $j) {
93
                        $event_picture1 = $REQUEST['file' . $j];
94
                    } elseif (2 == $j) {
95
                        $event_picture2 = $REQUEST['file' . $j];
96
                    }
97
                }
98
            } else {
99
                $url_event = XOOPS_ROOT_PATH . '/uploads/extcal/' . $REQUEST['file' . $j];
100
                if (1 == $j) {
101
                    $event_picture1 = '';
102
                } elseif (2 == $j) {
103
                    $event_picture2 = '';
104
                }
105
                if (\is_file($url_event)) {
106
                    \chmod($url_event, 0777);
107
                    \unlink($url_event);
108
                }
109
            }
110
        }
111
        //exit;
112
        ///////////////////////////////////////////////////////////////////////////////
113
    }
114
115
    /*******************************************************************
116
     *
117
     ******************************************************************
118
     * @param        $cat
119
     * @param bool   $addNone
120
     * @param string $name
121
     * @return \XoopsFormSelect
122
     */
123
    public static function getListCategories($cat, $addNone = true, $name = 'cat')
124
    {
125
        global $xoopsUser;
126
        // Category selectbox
127
        $categoryHandler = Extcal\Helper::getInstance()->getHandler(\_EXTCAL_CLN_CAT);
128
129
        $catsList  = $categoryHandler->getAllCat($xoopsUser);
130
        $catSelect = new \XoopsFormSelect('', $name, $cat);
131
        if ($addNone) {
132
            $catSelect->addOption(0, ' ');
133
        }
134
135
        foreach ($catsList as $catList) {
136
            $catSelect->addOption($catList->getVar('cat_id'), $catList->getVar('cat_name'));
137
        }
138
139
        return $catSelect;
140
    }
141
142
    /*******************************************************************
143
     *
144
     ******************************************************************
145
     * @param string $name
146
     * @param        $cat
147
     * @return array
148
     */
149
    public static function getCheckeCategories($name = 'cat', $cat)
150
    {
151
        global $xoopsUser;
152
        // Category selectbox
153
        //<option style="background-color:#00FFFF;">VARCHAR</option>
154
155
        $categoryHandler = Extcal\Helper::getInstance()->getHandler(\_EXTCAL_CLN_CAT);
156
        $catsList   = $categoryHandler->getAllCat($xoopsUser);
157
158
        $t = [];
159
        foreach ($catsList as $catList) {
160
            $cat_id    = $catList->getVar('cat_id');
161
            $name      = $catList->getVar('cat_name');
162
            $cat_color = $catList->getVar('cat_color');
163
            $checked   = \in_array($cat_id, $cat) ? 'checked' : '';
164
            $cat       = ''
165
                         . "<div style='float:left; margin-left:5px;'>"
166
                         . "<input type='checkbox' name='{$name}[{$cat_id}]' value='1' {$checked}>"
167
                         . "<div style='absolute:left;height:12px; width:6px; background-color:#{$cat_color}; border:1px solid black; float:left; margin-right:5px;' ></div>"
168
                         . " {$name}"
169
                         . '</div>';
170
171
            $t[] = $cat;
172
        }
173
174
        return $t;
175
    }
176
177
    /*******************************************************************
178
     *
179
     ******************************************************************
180
     * @param string $name
181
     * @param string $caption
182
     * @param        $defaut
183
     * @param bool   $addNone
184
     * @return \XoopsFormSelect
185
     */
186
    public static function getListOrderBy($name = 'orderby', $caption = '', $defaut, $addNone = false)
187
    {
188
        global $xoopsUser;
189
190
        $select = new \XoopsFormSelect($caption, $name, $defaut);
191
        if ($addNone) {
192
            $select->addOption('', '');
193
        }
194
195
        $select->addOption('year ASC', \_MD_EXTCAL_YEAR . ' ' . \_MD_EXTCAL_ORDER_BY_ASC);
196
        $select->addOption('year DESC', \_MD_EXTCAL_YEAR . ' ' . \_MD_EXTCAL_ORDER_BY_DESC);
197
198
        $select->addOption('month ASC', \_MD_EXTCAL_MONTH . ' ' . \_MD_EXTCAL_ORDER_BY_ASC);
199
        $select->addOption('month DESC', \_MD_EXTCAL_MONTH . ' ' . \_MD_EXTCAL_ORDER_BY_DESC);
200
201
        $select->addOption('event_title ASC', \_MD_EXTCAL_ALPHA . ' ' . \_MD_EXTCAL_ORDER_BY_ASC);
202
        $select->addOption('event_title DESC', \_MD_EXTCAL_ALPHA . ' ' . \_MD_EXTCAL_ORDER_BY_DESC);
203
204
        $select->addOption('cat_name ASC', \_MD_EXTCAL_CATEGORY . ' ' . \_MD_EXTCAL_ORDER_BY_ASC);
205
        $select->addOption('cat_name DESC', \_MD_EXTCAL_CATEGORY . ' ' . \_MD_EXTCAL_ORDER_BY_DESC);
206
207
        return $select;
208
    }
209
210
    /*******************************************************************
211
     *
212
     ******************************************************************
213
     * @param string $name
214
     * @param string $caption
215
     * @param        $defaut
216
     * @return \XoopsFormSelect
217
     */
218
    public static function getListAndOr($name = 'andor', $caption = '', $defaut)
219
    {
220
        global $xoopsUser;
221
222
        $select = new \XoopsFormSelect($caption, $name, $defaut);
223
224
        $select->addOption('AND', \_MD_EXTCAL_AND);
225
        $select->addOption('OR', \_MD_EXTCAL_OR);
226
227
        return $select;
228
    }
229
230
    /*******************************************************************
231
     *
232
     ******************************************************************
233
     * @param        $name
234
     * @param        $caption
235
     * @param        $defaut
236
     * @param        $options
237
     * @param string $sep
238
     * @return \XoopsFormSelect
239
     */
240
    public static function getList($name, $caption, $defaut, $options, $sep = ';')
241
    {
242
        global $xoopsUser;
243
244
        $select = new \XoopsFormSelect($caption, $name, $defaut);
245
        if (!\is_array($options)) {
246
            $options = \explode($sep, $options);
247
        }
248
249
        foreach ($options as $h => $hValue) {
250
            $select->addOption($h, $options[$h]);
251
        }
252
253
        return $select;
254
    }
255
256
    /*******************************************************************
257
     *
258
     ******************************************************************
259
     * @param        $ts
260
     * @param        $startMonth
261
     * @param        $endMonth
262
     * @param string $mode
263
     * @return \DateTime
264
     * @throws \Exception
265
     * @throws \Exception
266
     */
267
    public static function getDateBetweenDates($ts, $startMonth, $endMonth, $mode = 'w')
268
    {
269
        $d = new \DateTime($periodStart);
270
        $d->setTimestamp($ts);
271
272
        //echo "<br>affichage des periodes : <br>";
273
        $begin = new \DateTime();
274
        $begin->setTimestamp($startMonth);
275
        //echo $begin->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53
276
277
        $end = new \DateTime();
278
        $end->setTimestamp($endMonth);
279
        //echo $end->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53
280
        //echo "<hr>";
281
        $interval = \DateInterval::createFromDateString('next sunday');
282
        $period   = new \DatePeriod($begin, $interval, $end);
283
        //echoDateArray($period);
284
285
        //echo "<hr>{$interval}";
286
        return $d;
287
        //echo mktime($heure, $minute, $seconde, $mois, $jour, $an);
288
289
        //
290
        //   $jour = date('d', $ts);
291
        //   $mois = date('m', $ts);
292
        //   $an = date('Y', $ts);
293
        //   $heure = date('H', $ts);
294
        //   $minute = date('i', $ts);
295
        //   $seconde = date('s', $ts);
296
        //   $d->setDate($heure,$minute,$seconde,$mois,$jour,$an);
297
298
        // <?php
299
        // $interval = DateInterval::createFromDateString('next sunday');
300
        // $period = new \DatePeriod($begin, $interval, $end);
301
        // foreach ($period as $dt) {
302
        //   echo $dt->format( "l Y-m-d H:i:s\n" );
303
    }
304
305
    /*
306
    Sunday 2009-11-01 00:00:00
307
    Sunday 2009-11-08 00:00:00
308
    Sunday 2009-11-15 00:00:00
309
    Sunday 2009-11-22 00:00:00
310
    Sunday 2009-11-29 00:00:00
311
    Sunday 2009-12-06 00:00:00
312
    ...
313
    */
314
315
    /**
316
     * @param $period
317
     */
318
    public static function echoDateArray($period)
319
    {
320
        foreach ($period as $dt) {
321
            echo $dt->format("l Y-m-d H:i:s\n") . '<br>';
322
        }
323
    }
324
325
    /*****************************************************************/
326
327
    /**
328
     * @param        $t
329
     * @param string $msg
330
     */
331
    public static function echoArray($t, $msg = '')
332
    {
333
        if ('' != $msg) {
334
            echo "<hr>{$msg}<hr>";
335
        }
336
337
        $txt = \print_r($t, true);
338
        echo '<pre>Number of items: ' . \count($t) . "<br>{$txt}</pre>";
339
    }
340
341
    /*****************************************************************/
342
343
    /**
344
     * @param        $line
345
     * @param string $msg
346
     */
347
    public static function extEcho($line, $msg = '')
348
    {
349
        if ('' != $msg) {
350
            echo "<hr>{$msg}<hr>";
351
        }
352
        echo $line . '<br>';
353
    }
354
355
    /*****************************************************************/
356
357
    /**
358
     * @param        $tsName
359
     * @param string $msg
360
     */
361
    public static function echoTsn($tsName, $msg = '')
362
    {
363
        global $$tsName;
364
        $ts = $$tsName;
365
        static::echoTsu($ts, $tsName, $msg = '');
366
    }
367
368
    /*****************************************************************/
369
370
    /**
371
     * @param        $ts
372
     * @param        $tsName
373
     * @param string $msg
374
     */
375
    public static function echoTsu($ts, $tsName, $msg = '')
376
    {
377
        if ('' != $msg) {
378
            echo "<hr>{$msg}<hr>";
379
        }
380
381
        echo 'date --->' . $tsName . ' = ' . $ts . ' - ' . \date('d-m-Y H:m:s', $ts) . '<br>';
382
    }
383
384
    /*****************************************************************/
385
    /*****************************************************************/
386
387
    /**
388
     * @param        $date
389
     * @param string $sep
390
     *
391
     * @return int
392
     */
393
    public static function convertDate($date, $sep = '-')
394
    {
395
        $lstSep = '/ .';
396
397
        for ($h = 0, $count = mb_strlen($lstSep); $h < $count; ++$h) {
398
            $sep2replace = mb_substr($lstSep, $h, 1);
399
            if (mb_strpos($date, $sep2replace)) {
400
                $date = \str_replace($sep2replace, $sep, $date);
401
            }
402
403
            return \strtotime($date);
404
        }
405
    }
406
407
    /**
408
     * @param     $givendate
409
     * @param int $day
410
     * @param int $mth
411
     * @param int $yr
412
     *
413
     * @return int
414
     */
415
    public static function addDate($givendate, $day = 0, $mth = 0, $yr = 0)
416
    {
417
        //$cd = strtotime($givendate);
418
        $cd      = $givendate;
419
        $newdate = \date('Y-m-d h:i:s', \mktime(\date('h', $cd), \date('i', $cd), \date('s', $cd), \date('m', $cd) + $mth, \date('d', $cd) + $day, \date('Y', $cd) + $yr));
420
421
        return \strtotime($newdate);
422
    }
423
424
    /**
425
     * @param $date
426
     * @param $number
427
     * @param $interval
428
     *
429
     * @return int
430
     */
431
    public static function addDate2($date, $number, $interval = 'd')
432
    {
433
        $date_time_array = \getdate($date);
434
        $hours           = $date_time_array['hours'];
435
        $minutes         = $date_time_array['minutes'];
436
        $seconds         = $date_time_array['seconds'];
437
        $month           = $date_time_array['mon'];
438
        $day             = $date_time_array['mday'];
439
        $year            = $date_time_array['year'];
440
441
        switch ($interval) {
442
            case 'yyyy':
443
                $year += $number;
444
                break;
445
            case 'q':
446
                $year += ($number * 3);
447
                break;
448
            case 'm':
449
                $month += $number;
450
                break;
451
            case 'y':
452
            case 'd':
453
            case 'w':
454
                $day += $number;
455
                break;
456
            case 'ww':
457
                $day += ($number * 7);
458
                break;
459
            case 'h':
460
                $hours += $number;
461
                break;
462
            case 'n':
463
                $minutes += $number;
464
                break;
465
            case 's':
466
                $seconds += $number;
467
                break;
468
        }
469
        $timestamp = \mktime($hours, $minutes, $seconds, $month, $day, $year);
470
471
        return $timestamp;
472
    }
473
474
    // function date_diff($date1, $date2) {
475
    //     $current = $date1;
476
    //     $datetime2 = date_create($date2);
477
    //     $count = 0;
478
    //     while (date_create($current) < $datetime2) {
479
    //         $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
480
    //         ++$count;
481
    //     }
482
    //     return $count;
483
    // }
484
485
    /**************************************************************************/
486
487
    /**
488
     * @param $color
489
     * @param $plancher
490
     * @param $plafond
491
     *
492
     * @return string
493
     */
494
    public static function getLighterColor($color, $plancher, $plafond)
495
    {
496
        //        require_once __DIR__ . '/ColorTools.php';
497
498
        //$ct = new \ColorTools();
499
        //return $ct->eclaircir($color,$plancher,$plafond);
500
        return ColorTools::eclaircir($color, $plancher, $plafond);
501
    }
502
503
    /**************************************************************************/
504
}
505