1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 *  @author    thirty bees <contact@thirtybees.com>
25 *  @author    PrestaShop SA <contact@prestashop.com>
26 *  @copyright 2017-2018 thirty bees
27 *  @copyright 2007-2016 PrestaShop SA
28 *  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * Class HelperCalendarCore
34 *
35 * @since 1.0.0
36 */
37class HelperCalendarCore extends Helper
38{
39    const DEFAULT_DATE_FORMAT    = 'Y-mm-dd';
40    const DEFAULT_COMPARE_OPTION = 1;
41
42    // @codingStandardsIgnoreStart
43    private $_actions;
44    private $_compare_actions;
45    private $_compare_date_from;
46    private $_compare_date_to;
47    private $_compare_date_option;
48    private $_date_format;
49    private $_date_from;
50    private $_date_to;
51    private $_rtl;
52    // @codingStandardsIgnoreEnd
53
54    /**
55     * HelperCalendarCore constructor.
56     *
57     * @since 1.0.0
58     * @version 1.0.0 Initial version
59     */
60    public function __construct()
61    {
62        $this->base_folder = 'helpers/calendar/';
63        $this->base_tpl = 'calendar.tpl';
64        parent::__construct();
65    }
66
67    /**
68     * @param Traversable[] $value
69     *
70     * @return $this
71     * @throws PrestaShopException
72     *
73     * @since 1.0.0
74     * @version 1.0.0 Initial version
75     */
76    public function setActions($value)
77    {
78        if (!is_array($value) && !$value instanceof Traversable) {
79            throw new PrestaShopException('Actions value must be an traversable array');
80        }
81
82        $this->_actions = $value;
83
84        return $this;
85    }
86
87    /**
88     * @return array
89     *
90     * @since 1.0.0
91     * @version 1.0.0 Initial version
92     */
93    public function getActions()
94    {
95        if (!isset($this->_actions)) {
96            $this->_actions = [];
97        }
98
99        return $this->_actions;
100    }
101
102    /**
103     * @param Traversable[] $value
104     *
105     * @return $this
106     * @throws PrestaShopException
107     *
108     * @since 1.0.0
109     * @version 1.0.0 Initial version
110     */
111    public function setCompareActions($value)
112    {
113        if (!is_array($value) && !$value instanceof Traversable) {
114            throw new PrestaShopException('Actions value must be an traversable array');
115        }
116
117        $this->_compare_actions = $value;
118
119        return $this;
120    }
121
122    /**
123     * @return array
124     *
125     * @since 1.0.0
126     * @version 1.0.0 Initial version
127     */
128    public function getCompareActions()
129    {
130        if (!isset($this->_compare_actions)) {
131            $this->_compare_actions = [];
132        }
133
134        return $this->_compare_actions;
135    }
136
137    /**
138     * @param string $value
139     *
140     * @return $this
141     *
142     * @since 1.0.0
143     * @version 1.0.0 Initial version
144     */
145    public function setCompareDateFrom($value)
146    {
147        $this->_compare_date_from = $value;
148
149        return $this;
150    }
151
152    /**
153     * @return string
154     *
155     * @since 1.0.0
156     * @version 1.0.0 Initial version
157     */
158    public function getCompareDateFrom()
159    {
160        return $this->_compare_date_from;
161    }
162
163    /**
164     * @param string $value
165     *
166     * @return $this
167     *
168     * @since 1.0.0
169     * @version 1.0.0 Initial version
170     */
171    public function setCompareDateTo($value)
172    {
173        $this->_compare_date_to = $value;
174
175        return $this;
176    }
177
178    /**
179     * @return string
180     *
181     * @since 1.0.0
182     * @version 1.0.0 Initial version
183     */
184    public function getCompareDateTo()
185    {
186        return $this->_compare_date_to;
187    }
188
189    /**
190     * @param int $value
191     *
192     * @return $this
193     *
194     * @since 1.0.0
195     * @version 1.0.0 Initial version
196     */
197    public function setCompareOption($value)
198    {
199        $this->_compare_date_option = (int) $value;
200
201        return $this;
202    }
203
204    /**
205     * @return int
206     *
207     * @since 1.0.0
208     * @version 1.0.0 Initial version
209     */
210    public function getCompareOption()
211    {
212        if (!isset($this->_compare_date_option)) {
213            $this->_compare_date_option = static::DEFAULT_COMPARE_OPTION;
214        }
215
216        return $this->_compare_date_option;
217    }
218
219    /**
220     * @param string $value
221     *
222     * @return $this
223     * @throws PrestaShopException
224     *
225     * @since 1.0.0
226     * @version 1.0.0 Initial version
227     */
228    public function setDateFormat($value)
229    {
230        if (!is_string($value)) {
231            throw new PrestaShopException('Date format must be a string');
232        }
233
234        $this->_date_format = $value;
235
236        return $this;
237    }
238
239    /**
240     * @return string
241     *
242     * @since 1.0.0
243     * @version 1.0.0 Initial version
244     */
245    public function getDateFormat()
246    {
247        if (!isset($this->_date_format)) {
248            $this->_date_format = static::DEFAULT_DATE_FORMAT;
249        }
250
251        return $this->_date_format;
252    }
253
254    /**
255     * @param string $value
256     *
257     * @return $this
258     * @throws PrestaShopException
259     *
260     * @since 1.0.0
261     * @version 1.0.0 Initial version
262     */
263    public function setDateFrom($value)
264    {
265        if (!isset($value) || $value == '') {
266            $value = date('Y-m-d', strtotime('-31 days'));
267        }
268
269        if (!is_string($value)) {
270            throw new PrestaShopException('Date must be a string');
271        }
272
273        $this->_date_from = $value;
274
275        return $this;
276    }
277
278    /**
279     * @return false|string
280     *
281     * @since 1.0.0
282     * @version 1.0.0 Initial version
283     */
284    public function getDateFrom()
285    {
286        if (!isset($this->_date_from)) {
287            $this->_date_from = date('Y-m-d', strtotime('-31 days'));
288        }
289
290        return $this->_date_from;
291    }
292
293    /**
294     * @param string $value
295     *
296     * @return $this
297     * @throws PrestaShopException
298     *
299     * @since 1.0.0
300     * @version 1.0.0 Initial version
301     */
302    public function setDateTo($value)
303    {
304        if (!isset($value) || $value == '') {
305            $value = date('Y-m-d');
306        }
307
308        if (!is_string($value)) {
309            throw new PrestaShopException('Date must be a string');
310        }
311
312        $this->_date_to = $value;
313
314        return $this;
315    }
316
317    /**
318     * @return false|string
319     *
320     * @since 1.0.0
321     * @version 1.0.0 Initial version
322     */
323    public function getDateTo()
324    {
325        if (!isset($this->_date_to)) {
326            $this->_date_to = date('Y-m-d');
327        }
328
329        return $this->_date_to;
330    }
331
332    /**
333     * @param bool $value
334     *
335     * @return $this
336     *
337     * @since 1.0.0
338     * @version 1.0.0 Initial version
339     */
340    public function setRTL($value)
341    {
342        $this->_rtl = (bool) $value;
343
344        return $this;
345    }
346
347    /**
348     * @param string $action
349     *
350     * @return $this
351     *
352     * @since 1.0.0
353     * @version 1.0.0 Initial version
354     */
355    public function addAction($action)
356    {
357        if (!isset($this->_actions)) {
358            $this->_actions = [];
359        }
360
361        $this->_actions[] = $action;
362
363        return $this;
364    }
365
366    /**
367     * @param string $action
368     *
369     * @return $this
370     *
371     * @since 1.0.0
372     * @version 1.0.0 Initial version
373     */
374    public function addCompareAction($action)
375    {
376        if (!isset($this->_compare_actions)) {
377            $this->_compare_actions = [];
378        }
379
380        $this->_compare_actions[] = $action;
381
382        return $this;
383    }
384
385    /**
386     * @return string
387     *
388     * @throws Exception
389     * @throws SmartyException
390     * @since   1.0.0
391     * @version 1.0.0 Initial version
392     */
393    public function generate()
394    {
395        $context = Context::getContext();
396        $adminWebpath = str_ireplace(_PS_CORE_DIR_, '', _PS_ADMIN_DIR_);
397        $adminWebpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $adminWebpath);
398        $boTheme = ((Validate::isLoadedObject($context->employee)
399            && $context->employee->bo_theme) ? $context->employee->bo_theme : 'default');
400
401        if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$boTheme.DIRECTORY_SEPARATOR.'template')) {
402            $boTheme = 'default';
403        }
404
405        if ($context->controller->ajax) {
406            $html = '<script type="text/javascript" src="'.__PS_BASE_URI__.$adminWebpath.'/themes/'.$boTheme.'/js/date-range-picker.js"></script>';
407            $html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$adminWebpath.'/themes/'.$boTheme.'/js/calendar.js"></script>';
408        } else {
409            $html = '';
410            $context->controller->addJs(__PS_BASE_URI__.$adminWebpath.'/themes/'.$boTheme.'/js/date-range-picker.js');
411            $context->controller->addJs(__PS_BASE_URI__.$adminWebpath.'/themes/'.$boTheme.'/js/calendar.js');
412        }
413
414        $this->tpl = $this->createTemplate($this->base_tpl);
415        $this->tpl->assign(
416            [
417                'date_format'       => $this->getDateFormat(),
418                'date_from'         => $this->getDateFrom(),
419                'date_to'           => $this->getDateTo(),
420                'compare_date_from' => $this->getCompareDateFrom(),
421                'compare_date_to'   => $this->getCompareDateTo(),
422                'actions'           => $this->getActions(),
423                'compare_actions'   => $this->getCompareActions(),
424                'compare_option'    => $this->getCompareOption(),
425                'is_rtl'            => $this->isRTL(),
426            ]
427        );
428
429        $html .= parent::generate();
430
431        return $html;
432    }
433
434    /**
435     * @return bool
436     *
437     * @since 1.0.0
438     * @version 1.0.0 Initial version
439     */
440    public function isRTL()
441    {
442        if (!isset($this->_rtl)) {
443            $this->_rtl = Context::getContext()->language->is_rtl;
444        }
445
446        return $this->_rtl;
447    }
448}
449