diff --git a/locales/fr/main.po b/locales/fr/main.po index 6b88301..8535f3b 100644 --- a/locales/fr/main.po +++ b/locales/fr/main.po @@ -10,15 +10,12 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -msgid "LunarPhase: moon phases" -msgstr "LunarPhase : phases de la Lune" +msgid "Moon phases" +msgstr "Phases de la Lune" msgid "Display the moon phases" msgstr "Afficher les phases de la Lune et autres données" -msgid "Moon phases" -msgstr "Phases de la Lune" - msgid "Display actual phase of moon" msgstr "Afficher la phase actuelle de la Lune" diff --git a/src/Backend.php b/src/Backend.php index 5651eab..928e86d 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -7,11 +7,39 @@ * * @author Tomtom, Pierre Van Glabeke and Contributors * - * @copyright Jean-Crhistian Denis + * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -require __DIR__ . '/_widgets.php'; +namespace Dotclear\Plugin\lunarPhase; + +use dcCore; +use dcNsProcess; +use Dotclear\Helper\Html\Form\{ + Checkbox, + Input, + Label, + Para +}; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') && My::phpCompliant(); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehavior('initWidgets', [Widgets::class, 'initWidgets']); + + return true; + } +} diff --git a/src/Frontend.php b/src/Frontend.php index d9dff90..4081882 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -7,16 +7,45 @@ * * @author Tomtom, Pierre Van Glabeke and Contributors * - * @copyright Jean-Crhistian Denis + * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\lunarPhase; + +use dcCore; +use dcNsProcess; +use dcUtils; + +class Frontend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = My::phpCompliant(); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->addBehaviors([ + // Add public header for lunarphase css + 'publicHeadContent' => function (): void { + if (is_null(dcCore::app()->blog)) { + return; + } + + echo dcUtils::cssLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('lunarphase')); + }, + // Widgets + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } } - -require __DIR__ . '/_widgets.php'; - -// Add public header for lunarphase css -dcCore::app()->addBehavior('publicHeadContent', function () { - echo dcUtils::cssLoad(dcCore::app()->blog->url . dcCore::app()->url->getURLFor('lunarphase')); -}); diff --git a/src/LunarPhase.php b/src/LunarPhase.php index 977315b..5f21b0b 100644 --- a/src/LunarPhase.php +++ b/src/LunarPhase.php @@ -7,14 +7,16 @@ * * @author Tomtom, Pierre Van Glabeke and Contributors * - * @copyright Jean-Crhistian Denis + * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -class lunarPhase +namespace Dotclear\Plugin\lunarPhase; + +use ArrayObject; + +class LunarPhase { # Astronomical constants. public const epoch = 2444238.5; # 1980 January 0.0 @@ -37,8 +39,8 @@ class lunarPhase public const mParallax = 0.9507; # parallax at distance a from Earth public const synodic = 29.53058868; # synodic month (new Moon to new Moon) - protected $live; - protected $previsions; + protected ArrayObject $live; + protected ArrayObject $previsions; public function __construct() { @@ -49,17 +51,17 @@ class lunarPhase $this->setPrevisions(); } - public function getLive() + public function getLive(): ArrayObject { return $this->live; } - public function getPrevisions() + public function getPrevisions(): ArrayObject { return $this->previsions; } - private function setLive() + private function setLive(): void { $day = $this->jTime(time()) - self::epoch; @@ -104,7 +106,7 @@ class lunarPhase $this->setPhase(); } - private function setPhase() + private function setPhase(): void { if ($this->live['age'] >= self::synodic || $this->live['age'] <= self::synodic / 8) { $this->live['id'] = 'new_moon'; @@ -133,7 +135,7 @@ class lunarPhase } } - private function setPrevisions() + private function setPrevisions(): void { $ts_day = 24 * 60 * 60; $ts_synodic = self::synodic * $ts_day; @@ -173,27 +175,27 @@ class lunarPhase ]; } - private function fixAngle($x) + private function fixAngle(float $x): float { return ($x - 360.0 * (floor($x / 360.0))); } - private function toRad($x) + private function toRad(float $x): float { return ($x * (M_PI / 180.0)); } - private function toDeg($x) + private function toDeg(float $x): float { return ($x * (180.0 / M_PI)); } - private function jTime($t) + private function jTime(float $t): float { return ($t / 86400) + 2440587.5; } - private function kepler($m, $ecc) + private function kepler(float $m, float $ecc): float { $delta = null; $EPSILON = 1e-6; diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..7106a1c --- /dev/null +++ b/src/My.php @@ -0,0 +1,62 @@ + image */ + public const LUNAR_PHASES = [ + 'new_moon' => 'nm.png', + 'waxing_crescent_moon' => 'wcm1.png', + 'first_quarter_moon' => 'fqm.png', + 'waxing_gibbous_moon' => 'wgm1.png', + 'full_moon' => 'fm.png', + 'waning_gibbous_moon' => 'wgm2.png', + 'last_quarter_moon' => 'tqm.png', + 'waning_crescent_moon' => 'wcm2.png', + ]; + + /** + * This module id + */ + public static function id(): string + { + return basename(dirname(__DIR__)); + } + + /** + * This module name + */ + public static function name(): string + { + return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name')); + } + + /** + * Check php version + */ + public static function phpCompliant(): bool + { + return version_compare(phpversion(), self::PHP_MIN, '>='); + } +} diff --git a/src/Prepend.php b/src/Prepend.php index 0cfee3f..1fb0a3b 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -7,44 +7,57 @@ * * @author Tomtom, Pierre Van Glabeke and Contributors * - * @copyright Jean-Crhistian Denis + * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -Clearbricks::lib()->autoload(['lunarPhase' => __DIR__ . '/inc/class.lunarphase.php']); +namespace Dotclear\Plugin\lunarPhase; -// Register lunarphase CSS URL -dcCore::app()->url->register( - 'lunarphase', - 'lunarphase.css', - '^lunarphase\.css', - function ($args) { - $phases = [ - 'new_moon' => 'nm', - 'waxing_crescent_moon' => 'wcm1', - 'first_quarter_moon' => 'fqm', - 'waxing_gibbous_moon' => 'wgm1', - 'full_moon' => 'fm', - 'waning_gibbous_moon' => 'wgm2', - 'last_quarter_moon' => 'tqm', - 'waning_crescent_moon' => 'wcm2', - ]; +use dcCore; +use dcNsProcess; - header('Content-Type: text/css; charset=UTF-8'); - echo "/* lunarphase widget style */\n"; +class Prepend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = My::phpCompliant(); - foreach ($phases as $phase => $image) { - echo - sprintf( - '#sidebar .lunarphase ul li.%s{background:transparent url(%s) no-repeat left 0.2em;padding-left:2em;}', - $phase, - dcCore::app()->blog->getPF(basename(__dir__) . '/img/' . $image . '.png') - ) . "\n"; + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; } - exit; + // Register lunarphase CSS URL + dcCore::app()->url->register( + 'lunarphase', + 'lunarphase.css', + '^lunarphase\.css', + function (?string $args): void { + // avoid null warning + if (is_null(dcCore::app()->blog)) { + return; + } + + header('Content-Type: text/css; charset=UTF-8'); + echo "/* lunarphase widget style */\n"; + + foreach (My::LUNAR_PHASES as $phase => $image) { + echo sprintf( + "#sidebar .lunarphase ul li.%s{background:transparent url(%s) no-repeat left 0.2em;padding-left:2em;}\n", + $phase, + dcCore::app()->blog->getPF(My::id() . '/img/' . $image) + ); + } + + exit; + } + ); + + return true; } -); +} diff --git a/src/Widgets.php b/src/Widgets.php index d5156fc..1d92769 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -7,23 +7,27 @@ * * @author Tomtom, Pierre Van Glabeke and Contributors * - * @copyright Jean-Crhistian Denis + * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -dcCore::app()->addBehavior('initWidgets', ['lunarPhaseWidgets','initWidgets']); +namespace Dotclear\Plugin\lunarPhase; -class lunarPhaseWidgets +use dcCore; +use Dotclear\Helper\Date; +use Dotclear\Helper\Html\Html; +use Dotclear\Plugin\widgets\WidgetsStack; +use Dotclear\Plugin\widgets\WidgetsElement; + +class Widgets { - public static function initWidgets($w) + public static function initWidgets(WidgetsStack $w): void { $w->create( 'lunarphase', - __('LunarPhase: moon phases'), - ['lunarPhaseWidgets','widget'], + __('Moon phases'), + [self::class, 'parseWidget'], null, __('Display the moon phases') ) @@ -43,36 +47,33 @@ class lunarPhaseWidgets ->addOffline(); } - public static function widget($w) + public static function parseWidget(WidgetsElement $w): string { - if ($w->offline) { - return; + if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) { + return ''; } - if (!$w->checkHomeOnly(dcCore::app()->url->type)) { - return; - } - - $lp = new lunarPhase(); + $lp = new LunarPhase(); return $w->renderDiv( - $w->content_only, + (bool) $w->content_only, 'lunarphase ' . $w->class, '', - ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . + ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . self::getLive($w, $lp) . self::getPrevisions($w, $lp) ); } /** - * Returns "live" part of lunarphase widget + * Returns "live" part of lunarphase widget. * - * @param dcWidget $w dcWidget object - * @param lunarPhase $lp lunarPhase object - * @return string Live HTML part + * @param WidgetsElement $w Widget instance + * @param LunarPhase $lp LunarPhase instance + * + * @return string Live HTML part */ - public static function getLive($w, $lp) + public static function getLive(WidgetsElement $w, LunarPhase $lp): string { $li = '