| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.0.27 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2022 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Assignments; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Base\Assignment; |
| 20 |
|
| 21 |
class Datetimebase extends Assignment |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Server's Timezone |
| 25 |
* |
| 26 |
* @var DateTimeZone |
| 27 |
*/ |
| 28 |
protected $tz; |
| 29 |
|
| 30 |
/** |
| 31 |
* If set to True, dates will be constructed with modified offset based on the passed timezone |
| 32 |
* |
| 33 |
* @var Boolean |
| 34 |
*/ |
| 35 |
protected $modify_offset = true; |
| 36 |
|
| 37 |
/** |
| 38 |
* Class constructor |
| 39 |
* |
| 40 |
* @param object $assignment |
| 41 |
*/ |
| 42 |
public function __construct($assignment, $factory) |
| 43 |
{ |
| 44 |
parent::__construct($assignment, $factory); |
| 45 |
|
| 46 |
// Set timezone |
| 47 |
if (property_exists($assignment->params, 'timezone') && !empty($assignment->params->timezone)) |
| 48 |
{ |
| 49 |
$this->tz = new \DateTimeZone($assignment->params->timezone); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
$offset = \FPFramework\Base\Factory::getConfig()->get('offset'); |
| 54 |
$this->tz = new \DateTimeZone($offset); |
| 55 |
} |
| 56 |
|
| 57 |
// Set modify offset switch |
| 58 |
if (property_exists($this->params, 'modify_offset')) |
| 59 |
{ |
| 60 |
$this->modify_offset = (bool) $this->params->modify_offset; |
| 61 |
} |
| 62 |
|
| 63 |
// Set now date |
| 64 |
$now = property_exists($assignment->params, 'now') ? $assignment->params->now : 'now'; |
| 65 |
$this->date = $this->getDate($now); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Checks if the current datetime is between the specified range |
| 70 |
* |
| 71 |
* @param JDate &$up_date |
| 72 |
* @param JDate &$down_date |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
protected function checkRange(&$up_date, &$down_date) |
| 77 |
{ |
| 78 |
if (!$up_date && !$down_date) |
| 79 |
{ |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
// Set down date's hours to 23:59:59 |
| 84 |
if ($down_date && property_exists($this->params, 'publish_down_end_date') && $this->params->publish_down_end_date) |
| 85 |
{ |
| 86 |
$down_date->setTime(23, 59, 59); |
| 87 |
} |
| 88 |
|
| 89 |
$now = $this->date->getTimestamp(); |
| 90 |
|
| 91 |
if (((bool)$up_date && $up_date->getTimestamp() > $now) || |
| 92 |
((bool)$down_date && $down_date->getTimestamp() < $now)) |
| 93 |
{ |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Create a date object based on the given string and apply timezone. |
| 102 |
* |
| 103 |
* @param String $date |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
protected function getDate($date = 'now') |
| 108 |
{ |
| 109 |
// Fix the date string |
| 110 |
\FPFramework\Libs\Functions::fixDate($date); |
| 111 |
|
| 112 |
if ($this->modify_offset) |
| 113 |
{ |
| 114 |
// Create date, set timezone and modify offset |
| 115 |
$date = $this->factory->getDate($date)->setTimeZone($this->tz); |
| 116 |
} else |
| 117 |
{ |
| 118 |
// Create date and set timezone without modifyig offset |
| 119 |
$date = $this->factory->getDate($date, $this->tz); |
| 120 |
} |
| 121 |
|
| 122 |
return $date; |
| 123 |
} |
| 124 |
} |
| 125 |
|