use namespace

This commit is contained in:
Jean-Christian Denis 2023-04-25 23:19:05 +02:00
parent a41fcf19f6
commit 121b6d8c85
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
7 changed files with 243 additions and 105 deletions

View file

@ -10,15 +10,12 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "LunarPhase: moon phases" msgid "Moon phases"
msgstr "LunarPhase : phases de la Lune" msgstr "Phases de la Lune"
msgid "Display the moon phases" msgid "Display the moon phases"
msgstr "Afficher les phases de la Lune et autres données" 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" msgid "Display actual phase of moon"
msgstr "Afficher la phase actuelle de la Lune" msgstr "Afficher la phase actuelle de la Lune"

View file

@ -7,11 +7,39 @@
* *
* @author Tomtom, Pierre Van Glabeke and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
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;
}
}

View file

@ -7,16 +7,45 @@
* *
* @author Tomtom, Pierre Van Glabeke and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
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'));
});

View file

@ -7,14 +7,16 @@
* *
* @author Tomtom, Pierre Van Glabeke and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
class lunarPhase namespace Dotclear\Plugin\lunarPhase;
use ArrayObject;
class LunarPhase
{ {
# Astronomical constants. # Astronomical constants.
public const epoch = 2444238.5; # 1980 January 0.0 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 mParallax = 0.9507; # parallax at distance a from Earth
public const synodic = 29.53058868; # synodic month (new Moon to new Moon) public const synodic = 29.53058868; # synodic month (new Moon to new Moon)
protected $live; protected ArrayObject $live;
protected $previsions; protected ArrayObject $previsions;
public function __construct() public function __construct()
{ {
@ -49,17 +51,17 @@ class lunarPhase
$this->setPrevisions(); $this->setPrevisions();
} }
public function getLive() public function getLive(): ArrayObject
{ {
return $this->live; return $this->live;
} }
public function getPrevisions() public function getPrevisions(): ArrayObject
{ {
return $this->previsions; return $this->previsions;
} }
private function setLive() private function setLive(): void
{ {
$day = $this->jTime(time()) - self::epoch; $day = $this->jTime(time()) - self::epoch;
@ -104,7 +106,7 @@ class lunarPhase
$this->setPhase(); $this->setPhase();
} }
private function setPhase() private function setPhase(): void
{ {
if ($this->live['age'] >= self::synodic || $this->live['age'] <= self::synodic / 8) { if ($this->live['age'] >= self::synodic || $this->live['age'] <= self::synodic / 8) {
$this->live['id'] = 'new_moon'; $this->live['id'] = 'new_moon';
@ -133,7 +135,7 @@ class lunarPhase
} }
} }
private function setPrevisions() private function setPrevisions(): void
{ {
$ts_day = 24 * 60 * 60; $ts_day = 24 * 60 * 60;
$ts_synodic = self::synodic * $ts_day; $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))); return ($x - 360.0 * (floor($x / 360.0)));
} }
private function toRad($x) private function toRad(float $x): float
{ {
return ($x * (M_PI / 180.0)); return ($x * (M_PI / 180.0));
} }
private function toDeg($x) private function toDeg(float $x): float
{ {
return ($x * (180.0 / M_PI)); return ($x * (180.0 / M_PI));
} }
private function jTime($t) private function jTime(float $t): float
{ {
return ($t / 86400) + 2440587.5; return ($t / 86400) + 2440587.5;
} }
private function kepler($m, $ecc) private function kepler(float $m, float $ecc): float
{ {
$delta = null; $delta = null;
$EPSILON = 1e-6; $EPSILON = 1e-6;

62
src/My.php Normal file
View file

@ -0,0 +1,62 @@
<?php
/**
* @brief lunarPhase, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Tomtom, Pierre Van Glabeke and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\lunarPhase;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/** @var array List of lunar phase => 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, '>=');
}
}

View file

@ -7,44 +7,57 @@
* *
* @author Tomtom, Pierre Van Glabeke and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
Clearbricks::lib()->autoload(['lunarPhase' => __DIR__ . '/inc/class.lunarphase.php']); namespace Dotclear\Plugin\lunarPhase;
// Register lunarphase CSS URL use dcCore;
dcCore::app()->url->register( use dcNsProcess;
'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',
];
header('Content-Type: text/css; charset=UTF-8'); class Prepend extends dcNsProcess
echo "/* lunarphase widget style */\n"; {
public static function init(): bool
{
static::$init = My::phpCompliant();
foreach ($phases as $phase => $image) { return static::$init;
echo }
sprintf(
'#sidebar .lunarphase ul li.%s{background:transparent url(%s) no-repeat left 0.2em;padding-left:2em;}', public static function process(): bool
$phase, {
dcCore::app()->blog->getPF(basename(__dir__) . '/img/' . $image . '.png') if (!static::$init) {
) . "\n"; 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;
} }
); }

View file

@ -7,23 +7,27 @@
* *
* @author Tomtom, Pierre Van Glabeke and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
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( $w->create(
'lunarphase', 'lunarphase',
__('LunarPhase: moon phases'), __('Moon phases'),
['lunarPhaseWidgets','widget'], [self::class, 'parseWidget'],
null, null,
__('Display the moon phases') __('Display the moon phases')
) )
@ -43,36 +47,33 @@ class lunarPhaseWidgets
->addOffline(); ->addOffline();
} }
public static function widget($w) public static function parseWidget(WidgetsElement $w): string
{ {
if ($w->offline) { if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return; return '';
} }
if (!$w->checkHomeOnly(dcCore::app()->url->type)) { $lp = new LunarPhase();
return;
}
$lp = new lunarPhase();
return $w->renderDiv( return $w->renderDiv(
$w->content_only, (bool) $w->content_only,
'lunarphase ' . $w->class, 'lunarphase ' . $w->class,
'', '',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') .
self::getLive($w, $lp) . self::getLive($w, $lp) .
self::getPrevisions($w, $lp) self::getPrevisions($w, $lp)
); );
} }
/** /**
* Returns "live" part of lunarphase widget * Returns "live" part of lunarphase widget.
* *
* @param dcWidget $w dcWidget object * @param WidgetsElement $w Widget instance
* @param lunarPhase $lp lunarPhase object * @param LunarPhase $lp LunarPhase instance
* @return string Live HTML part *
* @return string Live HTML part
*/ */
public static function getLive($w, $lp) public static function getLive(WidgetsElement $w, LunarPhase $lp): string
{ {
$li = '<li class="%2$s">%1$s</li>'; $li = '<li class="%2$s">%1$s</li>';
$live = $lp->getLive(); $live = $lp->getLive();
@ -166,13 +167,14 @@ class lunarPhaseWidgets
} }
/** /**
* Returns "previsions" part of lunarphase widget * Returns "previsions" part of lunarphase widget.
* *
* @param dcWidget $w dcWidget object * @param WidgetsElement $w Widget instance
* @param lunarPhase $lp lunarPhase object * @param LunarPhase $lp LunarPhase instance
* @return string Previsions HTML part *
* @return string Previsions HTML part
*/ */
public static function getPrevisions($w, $lp) public static function getPrevisions(WidgetsElement $w, LunarPhase $lp): string
{ {
$li = '<li class="%s" title="%s">%s</li>'; $li = '<li class="%s" title="%s">%s</li>';
$res = ''; $res = '';
@ -189,24 +191,29 @@ class lunarPhaseWidgets
} }
/** /**
* Returns value passed in argument with a correct format * Returns value passed in argument with a correct format.
* *
* @param string $type Type of convertion * @param string $type Type of convertion
* @param mixed $value Value to convert * @param mixed $value Value to convert
* @return mixed Converted value *
* @return mixed Converted value
*/ */
public static function formatValue($type, $value) public static function formatValue(string $type, mixed $value): mixed
{ {
if (is_null(dcCore::app()->blog)) {
return null;
}
$res = ''; $res = '';
$format = dcCore::app()->blog->settings->system->date_format . ' - '; $format = dcCore::app()->blog->settings->get('system')->get('date_format') . ' - ';
$format .= dcCore::app()->blog->settings->system->time_format; $format .= dcCore::app()->blog->settings->get('system')->get('time_format');
$tz = dcCore::app()->blog->settings->system->blog_timezone; $tz = dcCore::app()->blog->settings->get('system')->get('blog_timezone');
return match ($type) { return match ($type) {
'int' => number_format($value, 0), 'int' => number_format($value, 0),
'float' => number_format($value, 2), 'float' => number_format($value, 2),
'percent' => number_format($value * 100, 0), 'percent' => number_format($value * 100, 0),
'date' => dt::str($format, $value, $tz), 'date' => Date::str($format, (int) $value, $tz),
'deg' => number_format(($value * (180.0 / M_PI)), 2), 'deg' => number_format(($value * (180.0 / M_PI)), 2),
default => $value, default => $value,
}; };