From 52a60eb47a48b9e3cf022bdb81593c16be2299d3 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Mon, 23 Oct 2023 22:54:27 +0200 Subject: [PATCH] code review --- CHANGELOG.md | 8 ++ README.md | 4 +- _define.php | 2 +- dcstore.xml | 4 +- src/ActivityReportAction.php | 24 +++-- src/BackendActionsLinks.php | 8 +- src/BackendActionsLinksDefault.php | 15 ++- src/BackendListingLinks.php | 37 +++---- src/Combo.php | 2 +- src/Frontend.php | 1 - src/FrontendContext.php | 30 +++--- src/FrontendTemplate.php | 146 ++++++++++++++++++++++++++- src/FrontendUrl.php | 2 +- src/ManageCat.php | 77 ++++++--------- src/ManageCats.php | 17 ++-- src/ManageLink.php | 135 ++++++++++--------------- src/ManageLinks.php | 7 +- src/My.php | 4 +- src/PluginSitemaps.php | 13 ++- src/RecordCatsRow.php | 41 ++++++++ src/RecordLinksRow.php | 63 ++++++++++++ src/Utils.php | 48 ++++++--- src/WidgetCatsDescriptor.php | 32 ++++++ src/WidgetLinksDescriptor.php | 48 +++++++++ src/Widgets.php | 153 ++++++++++++++++------------- 25 files changed, 625 insertions(+), 296 deletions(-) create mode 100644 src/RecordCatsRow.php create mode 100644 src/RecordLinksRow.php create mode 100644 src/WidgetCatsDescriptor.php create mode 100644 src/WidgetLinksDescriptor.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8caecb2..7add08f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +cinecturlink2 2.3 - 2023.10.23 +=========================================================== +* Require Dotclear 2.28 +* Require PHP 8.1 +* Use specific class for records +* Use specific class for widgets +* Code review (phpstan) + cinecturlink2 2.2 - 2023.10.18 =========================================================== * Require Dotclear 2.28 diff --git a/README.md b/README.md index 6f150a9..9ca9b34 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # README -[![Release](https://img.shields.io/badge/release-2.2-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/cinecturlink2/releases) -![Date](https://img.shields.io/badge/date-2023.10.18-c44d58.svg)] +[![Release](https://img.shields.io/badge/release-2.3-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/cinecturlink2/releases) +![Date](https://img.shields.io/badge/date-2023.10.23-c44d58.svg)] [![Dotclear](https://img.shields.io/badge/dotclear-v2.28-137bbb.svg)](https://fr.dotclear.org/download) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-9ac123.svg)](https://plugins.dotaddict.org/dc2/details/cinecturlink2) [![License](https://img.shields.io/badge/license-GPL--2.0-ececec.svg)](https://git.dotclear.watch/JcDenis/cinecturlink2/src/branch/master/LICENSE) diff --git a/_define.php b/_define.php index 09f5829..13f030d 100644 --- a/_define.php +++ b/_define.php @@ -21,7 +21,7 @@ $this->registerModule( 'Cinecturlink 2', 'Widgets and pages about books, musics, films, blogs you are interested in', 'Jean-Christian Denis and Contributors', - '2.2', + '2.3', [ 'requires' => [['core', '2.28']], 'settings' => ['blog' => '#params.' . basename(__DIR__) . '_params'], diff --git a/dcstore.xml b/dcstore.xml index 0085f14..e224d4f 100644 --- a/dcstore.xml +++ b/dcstore.xml @@ -2,10 +2,10 @@ Cinecturlink 2 - 2.2 + 2.3 Jean-Christian Denis and Contributors Widgets and pages about books, musics, films, blogs you are interested in - https://git.dotclear.watch/JcDenis/cinecturlink2/releases/download/v2.2/plugin-cinecturlink2.zip + https://git.dotclear.watch/JcDenis/cinecturlink2/releases/download/v2.3/plugin-cinecturlink2.zip 2.28 https://git.dotclear.watch/JcDenis/cinecturlink2/src/branch/master/README.md https://git.dotclear.watch/JcDenis/cinecturlink2/issues diff --git a/src/ActivityReportAction.php b/src/ActivityReportAction.php index ba276bf..37f0883 100644 --- a/src/ActivityReportAction.php +++ b/src/ActivityReportAction.php @@ -65,35 +65,33 @@ class ActivityReportAction extends Process return true; } - public static function addLink(Cursor $cur) + public static function addLink(Cursor $cur): void { $logs = [ - $cur->link_title, - App::auth()->getInfo('user_cn'), + (string) $cur->getField('link_title'), + (string) App::auth()->getInfo('user_cn'), ]; ActivityReport::instance()->addLog('cinecturlink2', 'create', $logs); } - public static function updLink(Cursor $cur, int $id) + public static function updLink(Cursor $cur, int $id): void { - $C2 = new Utils(); - $rs = $C2->getLinks(['link_id' => $id]); + $rs = (new Utils())->getLinks(['link_id' => $id]); $logs = [ - $rs->link_title, - App::auth()->getInfo('user_cn'), + (string) $rs->field('link_title'), + (string) App::auth()->getInfo('user_cn'), ]; ActivityReport::instance()->addLog('cinecturlink2', 'update', $logs); } - public static function delLink(int $id) + public static function delLink(int $id): void { - $C2 = new Utils(); - $rs = $C2->getLinks(['link_id' => $id]); + $rs = (new Utils())->getLinks(['link_id' => $id]); $logs = [ - $rs->link_title, - App::auth()->getInfo('user_cn'), + (string) $rs->field('link_title'), + (string) App::auth()->getInfo('user_cn'), ]; ActivityReport::instance()->addLog('cinecturlink2', 'delete', $logs); } diff --git a/src/BackendActionsLinks.php b/src/BackendActionsLinks.php index 62760c6..975171f 100644 --- a/src/BackendActionsLinks.php +++ b/src/BackendActionsLinks.php @@ -27,6 +27,12 @@ class BackendActionsLinks extends Actions protected bool $use_render = true; public Utils $utils; + /** + * Constructs a new instance. + * + * @param string $uri The form uri + * @param array $redirect_args The redirection $_GET arguments, + */ public function __construct(string $uri, array $redirect_args = []) { $this->utils = new Utils(); @@ -92,7 +98,7 @@ class BackendActionsLinks extends Actions $rs = $this->utils->getLinks($params); while ($rs->fetch()) { - $this->entries[$rs->f('link_id')] = $rs->f('link_title'); + $this->entries[(string) $rs->f('link_id')] = $rs->f('link_title'); } $this->rs = $rs; } else { diff --git a/src/BackendActionsLinksDefault.php b/src/BackendActionsLinksDefault.php index 9be92fd..cdfb9b1 100644 --- a/src/BackendActionsLinksDefault.php +++ b/src/BackendActionsLinksDefault.php @@ -46,6 +46,9 @@ class BackendActionsLinksDefault ); } + /** + * @param ArrayObject $post + */ public static function doDeleteLinks(BackendActionsLinks $ap, ArrayObject $post): void { $ids = $ap->getIDs(); @@ -57,7 +60,7 @@ class BackendActionsLinksDefault } foreach ($ids as $id) { - $ap->utils->delLink($id); + $ap->utils->delLink((int) $id); } Notices::addSuccessNotice(sprintf( @@ -71,6 +74,9 @@ class BackendActionsLinksDefault $ap->redirect(true); } + /** + * @param ArrayObject $post + */ public static function doChangeCategory(BackendActionsLinks $ap, ArrayObject $post): void { if (isset($post['upd_cat_id'])) { @@ -88,7 +94,7 @@ class BackendActionsLinksDefault foreach ($ids as $id) { $cur->clean(); $cur->setField('cat_id', $cat_id == 0 ? null : $cat_id); - $ap->utils->updLink($id, $cur); + $ap->utils->updLink((int) $id, $cur); } Notices::addSuccessNotice(sprintf( @@ -133,6 +139,9 @@ class BackendActionsLinksDefault } } + /** + * @param ArrayObject $post + */ public static function doChangeNote(BackendActionsLinks $ap, ArrayObject $post): void { if (isset($post['upd_link_note'])) { @@ -153,7 +162,7 @@ class BackendActionsLinksDefault foreach ($ids as $id) { $cur->clean(); $cur->setField('link_note', $link_note); - $ap->utils->updLink($id, $cur); + $ap->utils->updLink((int) $id, $cur); } Notices::addSuccessNotice(sprintf( diff --git a/src/BackendListingLinks.php b/src/BackendListingLinks.php index 0afbae4..5e3754a 100644 --- a/src/BackendListingLinks.php +++ b/src/BackendListingLinks.php @@ -84,7 +84,7 @@ class BackendListingLinks extends Listing $lines = []; while ($this->rs->fetch()) { - $lines[] = $this->linkLine(isset($links[$this->rs->link_id])); + $lines[] = $this->linkLine(new RecordLinksRow($this->rs), isset($links[$this->rs->f('link_id')])); } echo @@ -112,60 +112,61 @@ class BackendListingLinks extends Listing $pager->getLinks(); } - private function linkLine(bool $checked): Para + private function linkLine(RecordLinksRow $row, bool $checked): Para { $cols = new ArrayObject([ 'check' => (new Td()) ->class('nowrap minimal') ->items([ (new Checkbox(['entries[]'], $checked)) - ->value($this->rs->link_id), + ->value((string) $row->link_id), ]), 'title' => (new Td()) ->class('maximal') ->items([ (new Link()) - ->href(My::manageUrl(['part' => 'link', 'linkid' => $this->rs->link_id, 'redir' => $this->redir])) + ->href(My::manageUrl(['part' => 'link', 'link_id' => $row->link_id, 'redir' => $this->redir])) ->title(__('Edit')) - ->text(Html::escapeHTML($this->rs->link_title)), + ->text(Html::escapeHTML($row->link_title)), ]), 'author' => (new Td()) - ->text(Html::escapeHTML($this->rs->link_author)) + ->text(Html::escapeHTML($row->link_author)) ->class('nowrap'), 'desc' => (new Td()) - ->text(Html::escapeHTML($this->rs->link_desc)) + ->text(Html::escapeHTML($row->link_desc)) ->class('nowrap'), - 'link' => (new Text('td')) + 'link' => (new Td()) ->separator(' ') ->items([ (new Link()) - ->href($this->rs->link_url) + ->href($row->link_url) ->title(__('URL')) - ->text(Html::escapeHTML($this->rs->link_title)), + ->text(Html::escapeHTML($row->link_title)), (new Link()) - ->href($this->rs->link_img) + ->href($row->link_img) ->title(__('image')) - ->text(Html::escapeHTML($this->rs->link_title)), - ]), + ->text(Html::escapeHTML($row->link_title)), + ]) + ->class('nowrap'), 'cat' => (new Td()) ->items([ (new Link()) - ->href(My::manageUrl(['part' => 'cat', 'catid' => $this->rs->cat_id, 'redir' => $this->redir])) + ->href(My::manageUrl(['part' => 'cat', 'cat_id' => (string) $row->cat_id, 'redir' => $this->redir])) ->title(__('Edit')) - ->text(Html::escapeHTML($this->rs->cat_title)), + ->text(Html::escapeHTML($row->cat_title)), ]), 'note' => (new Td()) - ->text(Html::escapeHTML($this->rs->link_note)) + ->text(Html::escapeHTML($row->link_note)) ->class('number'), 'date' => (new Td()) - ->text(Html::escapeHTML(Date::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->link_upddt, (string) App::auth()->getInfo('user_tz')))) + ->text(Html::escapeHTML(Date::dt2str(__('%Y-%m-%d %H:%M'), $row->link_upddt, (string) App::auth()->getInfo('user_tz')))) ->class('nowrap'), ]); $this->userColumns(My::id(), $cols); return - (new Para('p' . $this->rs->kut_id, 'tr')) + (new Para('p' . $row->link_id, 'tr')) ->class('line') ->items(iterator_to_array($cols)); } diff --git a/src/Combo.php b/src/Combo.php index 7c72e8c..efd8d1c 100644 --- a/src/Combo.php +++ b/src/Combo.php @@ -69,7 +69,7 @@ class Combo if (!in_array($file->extension, My::ALLOWED_MEDIA_EXTENSION)) { continue; } - $tmp[$file->media_title] = $file->file_url; + $tmp[(string) $file->media_title] = (string) $file->file_url; } if (!empty($tmp)) { $stack = array_merge(['-' => ''], $tmp); diff --git a/src/Frontend.php b/src/Frontend.php index e782a40..6114f63 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -96,7 +96,6 @@ class Frontend extends Process } } else { foreach (array_merge($blocks, $values) as $v) { - pdump($v); App::frontend()->template()->addBlock($v, FrontendTemplate::disable(...)); } } diff --git a/src/FrontendContext.php b/src/FrontendContext.php index 615e2d2..98a959c 100644 --- a/src/FrontendContext.php +++ b/src/FrontendContext.php @@ -15,26 +15,26 @@ use Dotclear\App; */ class FrontendContext { - public static function PaginationNbPages() + public static function PaginationNbPages(): int { if (App::frontend()->context()->c2_pagination === null) { - return false; + return 0; } $nb_posts = App::frontend()->context()->c2_pagination->f(0); $nb_per_page = App::frontend()->context()->c2_params['limit'][1]; $nb_pages = ceil($nb_posts / $nb_per_page); - return $nb_pages; + return (int) $nb_pages; } - public static function PaginationPosition($offset = 0) + public static function PaginationPosition(string|int $offset = 0): int { if (isset($GLOBALS['c2_page_number'])) { $p = $GLOBALS['c2_page_number']; } else { $p = 1; } - $p = $p + $offset; + $p = (int) $p + (int) $offset; $n = self::PaginationNbPages(); if (!$n) { return $p; @@ -43,25 +43,17 @@ class FrontendContext return $p > $n || $p <= 0 ? 1 : $p; } - public static function PaginationStart() + public static function PaginationStart(): bool { - if (isset($GLOBALS['c2_page_number'])) { - return self::PaginationPosition() == 1; - } - - return true; + return isset($GLOBALS['c2_page_number']) ? self::PaginationPosition() == 1 : true; } - public static function PaginationEnd() + public static function PaginationEnd(): bool { - if (isset($GLOBALS['c2_page_number'])) { - return self::PaginationPosition() == self::PaginationNbPages(); - } - - return false; + return isset($GLOBALS['c2_page_number']) ? self::PaginationPosition() == self::PaginationNbPages() : false; } - public static function PaginationURL($offset = 0) + public static function PaginationURL(int|string $offset = 0): string { $args = $_SERVER['URL_REQUEST_PART']; @@ -84,7 +76,7 @@ class FrontendContext return $url; } - public static function categoryCurrent() + public static function categoryCurrent(): bool { if (!isset(App::frontend()->context()->c2_page_params['cat_id']) && !isset(App::frontend()->context()->c2_page_params['cat_title']) diff --git a/src/FrontendTemplate.php b/src/FrontendTemplate.php index 5318288..55ea33b 100644 --- a/src/FrontendTemplate.php +++ b/src/FrontendTemplate.php @@ -18,36 +18,57 @@ use Dotclear\Helper\Html\Html; */ class FrontendTemplate { + /** + * @param ArrayObject $a The attributes + */ public static function disable(ArrayObject $a, ?string $c = null): string { return ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PageURL(ArrayObject $a): string { return 'template()->getFilters($a), 'App::blog()->url().App::url()->getBase(\'cinecturlink2\')') . '; ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PageTitle(ArrayObject $a): string { return "settings()->cinecturlink2->public_title; if (empty(\$title)) { \$title = __('My cinecturlink'); } echo " . sprintf(App::frontend()->template()->getFilters($a), '$title') . '; ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PageFeedURL(ArrayObject $a): string { return 'template()->getFilters($a), 'App::blog()->url().App::url()->getBase("' . My::id() . '")."/feed/' . (!empty($a['type']) && preg_match('#^(rss2|atom)$#', $a['type']) ? $a['type'] : 'atom') . '"') . '; ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PageFeedID(ArrayObject $a): string { return 'urn:md5:id()."' . My::id() . '"); ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PageDescription(ArrayObject $a): string { return 'settings()->cinecturlink2->public_description; echo ' . sprintf(App::frontend()->template()->getFilters($a), '$description') . '; ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2If(ArrayObject $a, string $c): string { $if = []; @@ -67,6 +88,9 @@ class FrontendTemplate return empty($if) ? $c : '\n" . $c . "\n"; } + /** + * @param ArrayObject $a The attributes + */ public static function c2Entries(ArrayObject $a, string $c): string { $lastn = isset($a['lastn']) ? abs((int) $a['lastn']) + 0 : -1; @@ -113,16 +137,25 @@ class FrontendTemplate "?>\n"; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntriesHeader(ArrayObject $a, string $c): string { return 'context()->c2_entries->isStart()) : ?>' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntriesFooter(ArrayObject $a, string $c): string { return 'context()->c2_entries->isEnd()) : ?>' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryIf(ArrayObject $a, string $c): string { $if = []; @@ -137,96 +170,153 @@ class FrontendTemplate return empty($if) ? $c : '\n" . $c . "\n"; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryIfFirst(ArrayObject $a): string { return 'context()->c2_entries->index() == 0) { echo "' . (isset($a['return']) ? addslashes(Html::escapeHTML($a['return'])) : 'first') . '"; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryIfOdd(ArrayObject $a): string { return 'context()->c2_entries->index()+1)%2 == 1) { echo "' . (isset($a['return']) ? addslashes(Html::escapeHTML($a['return'])) : 'odd') . '"; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryFeedID(ArrayObject $a): string { return 'urn:md5:context()->c2_entries->blog_id.App::frontend()->context()->c2_entries->link_id.App::frontend()->context()->c2_entries->link_creadt); ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryID(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_id', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryTitle(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_title', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryDescription(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_desc', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorCommonName(ArrayObject $a): string { return self::getGenericValue('App::users()->getUserCN(App::frontend()->context()->c2_entries->user_id,App::frontend()->context()->c2_entries->user_name,App::frontend()->context()->c2_entries->user_firstname,App::frontend()->context()->c2_entries->user_displayname)', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorDisplayName(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->user_displayname', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorID(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->user_id', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorEmail(ArrayObject $a): string { return self::getGenericValue((isset($a['spam_protected']) && !$a['spam_protected'] ? 'App::frontend()->context()->c2_entries->user_email' : "strtr(App::frontend()->context()->c2_entries->user_email,array('@'=>'%40','.'=>'%2e'))"), $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorLink(ArrayObject $a): string { return self::getGenericValue('sprintf((App::frontend()->context()->c2_entries->user_url ? \'%1$s\' : \'%1$s\'),html::escapeHTML(App::users()->getUserCN(App::frontend()->context()->c2_entries->user_id,App::frontend()->context()->c2_entries->user_name,App::frontend()->context()->c2_entries->user_firstname,App::frontend()->context()->c2_entries->user_displayname)),html::escapeHTML(App::frontend()->context()->c2_entries->user_url))', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryAuthorURL(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->user_url', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryFromAuthor(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_author', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryLang(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_lang', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryURL(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->link_url', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryCategory(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->cat_title', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryCategoryID(ArrayObject $a): string { return self::getGenericValue('App::frontend()->context()->c2_entries->cat_id', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryCategoryURL(ArrayObject $a): string { return self::getGenericValue('App::blog()->url().App::url()->getBase("' . My::id() . '")."/".App::blog()->settings()->cinecturlink2->public_caturl."/".urlencode(App::frontend()->context()->c2_entries->cat_title)', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryImg(ArrayObject $a): string { $f = App::frontend()->template()->getFilters($a); @@ -243,6 +333,9 @@ class FrontendTemplate 'echo ' . sprintf($f, '$img') . "; unset(\$img); } ?> \n"; } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryDate(ArrayObject $a): string { $format = !empty($a['format']) ? addslashes($a['format']) : ''; @@ -260,11 +353,17 @@ class FrontendTemplate return self::getGenericValue($p, $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2EntryTime(ArrayObject $a): string { return self::getGenericValue('dt::dt2str(' . (!empty($a['format']) ? "'" . addslashes($a['format']) . "'" : 'App::blog()->settings()->system->time_format') . ', App::frontend()->context()->c2_entries->link_creadt)', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2Pagination(ArrayObject $a, string $c): string { $p = "context()->c2_pagination->f(0) > App::frontend()->context()->c2_entries->count()) : ?>' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2PaginationCounter(ArrayObject $a): string { return self::getGenericValue(FrontendContext::class . '::PaginationNbPages()', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2PaginationCurrent(ArrayObject $a): string { return self::getGenericValue(FrontendContext::class . '::PaginationPosition(' . (isset($a['offset']) ? (int) $a['offset'] : 0) . ')', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2PaginationIf(ArrayObject $a, string $c): string { $if = []; @@ -301,11 +409,17 @@ class FrontendTemplate return empty($if) ? $c : '' . $c . ''; } - public static function c2PaginationURL($a): string + /** + * @param ArrayObject $a The attributes + */ + public static function c2PaginationURL(ArrayObject $a): string { return self::getGenericValue(FrontendContext::class . '::PaginationURL(' . (isset($a['offset']) ? (int) $a['offset'] : 0) . ')', $a); } + /** + * @param ArrayObject $a The attributes + */ public static function c2Categories(ArrayObject $a, string $c): string { return @@ -317,16 +431,25 @@ class FrontendTemplate "?>\n"; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoriesHeader(ArrayObject $a, string $c): string { return 'context()->c2_categories->isStart()) : ?>' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoriesFooter(ArrayObject $a, string $c): string { return 'context()->c2_categories->isEnd()) : ?>' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryIf(ArrayObject $a, string $c): string { $if = []; @@ -343,6 +466,9 @@ class FrontendTemplate return empty($if) ? $c : '' . $c . ''; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryFeedURL(ArrayObject $a): string { $p = !empty($a['type']) ? $a['type'] : 'atom'; @@ -354,31 +480,49 @@ class FrontendTemplate return 'template()->getFilters($a), 'App::blog()->url().App::url()->getBase("' . My::id() . '")."/".App::blog()->settings()->cinecturlink2->public_caturl."/".urlencode(App::frontend()->context()->c2_categories->cat_title)."/feed/' . $p . '"') . '; ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryFeedID(ArrayObject $a): string { return 'urn:md5:id()."' . My::id() . '".App::frontend()->context()->c2_categories->cat_id); ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryID(ArrayObject $a): string { return "context()->exists('c2_categories')) { echo " . sprintf(App::frontend()->template()->getFilters($a), 'App::frontend()->context()->c2_categories->cat_id') . '; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryTitle(ArrayObject $a): string { return "context()->exists('c2_categories')) { echo " . sprintf(App::frontend()->template()->getFilters($a), 'App::frontend()->context()->c2_categories->cat_title') . '; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryDescription(ArrayObject $a): string { return "context()->exists('c2_categories')) { echo " . sprintf(App::frontend()->template()->getFilters($a), 'App::frontend()->context()->c2_categories->cat_desc') . '; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ public static function c2CategoryURL(ArrayObject $a): string { return "context()->exists('c2_categories')) { echo " . sprintf(App::frontend()->template()->getFilters($a), 'App::blog()->url().App::url()->getBase("' . My::id() . '")."/".App::blog()->settings()->cinecturlink2->public_caturl."/".urlencode(App::frontend()->context()->c2_categories->cat_title)') . '; } ?>'; } + /** + * @param ArrayObject $a The attributes + */ protected static function getGenericValue(string $p, ArrayObject $a): string { return "context()->exists('c2_entries')) { echo " . sprintf(App::frontend()->template()->getFilters($a), "$p") . '; } ?>'; diff --git a/src/FrontendUrl.php b/src/FrontendUrl.php index 4d142b4..ea4101d 100644 --- a/src/FrontendUrl.php +++ b/src/FrontendUrl.php @@ -17,7 +17,7 @@ use Dotclear\Helper\File\Path; */ class FrontendUrl extends Url { - public static function c2Page(?string $args) + public static function c2Page(?string $args): null { $args = (string) $args; diff --git a/src/ManageCat.php b/src/ManageCat.php index 25cc6c8..6821766 100644 --- a/src/ManageCat.php +++ b/src/ManageCat.php @@ -32,9 +32,7 @@ use Exception; class ManageCat extends Process { private static string $module_redir = ''; - private static int $catid = 0; - private static string $cattitle = ''; - private static string $catdesc = ''; + private static RecordCatsRow $row; public static function init(): bool { @@ -47,25 +45,18 @@ class ManageCat extends Process return false; } - $utils = new Utils(); - self::$module_redir = $_REQUEST['redir'] ?? ''; - self::$catid = (int) ($_REQUEST['catid'] ?? 0); - self::$cattitle = $_POST['cattitle'] ?? ''; - self::$catdesc = $_POST['catdesc'] ?? ''; + self::$row = new RecordCatsRow(); + $utils = new Utils(); try { // create category - if (!empty($_POST['save']) && empty(self::$catid) && !empty(self::$cattitle) && !empty(self::$catdesc)) { - $exists = $utils->getCategories(['cat_title' => self::$cattitle], true)->f(0); + if (!empty($_POST['save']) && empty(self::$row->cat_id) && !empty(self::$row->cat_title) && !empty(self::$row->cat_desc)) { + $exists = $utils->getCategories(['cat_title' => self::$row->cat_title], true)->f(0); if ($exists) { throw new Exception(__('Category with same name already exists.')); } - $cur = App::con()->openCursor($utils->cat_table); - $cur->setField('cat_title', self::$cattitle); - $cur->setField('cat_desc', self::$catdesc); - - $catid = $utils->addCategory($cur); + $cat_id = $utils->addCategory(self::$row->getCursor()); Notices::addSuccessNotice( __('Category successfully created.') @@ -73,16 +64,12 @@ class ManageCat extends Process My::redirect(['part' => 'cats']); } // update category - if (!empty($_POST['save']) && !empty(self::$catid) && !empty(self::$cattitle) && !empty(self::$catdesc)) { - $exists = $utils->getCategories(['cat_title' => self::$cattitle, 'exclude_cat_id' => self::$catid], true)->f(0); + if (!empty($_POST['save']) && !empty(self::$row->cat_id) && !empty(self::$row->cat_title) && !empty(self::$row->cat_desc)) { + $exists = $utils->getCategories(['cat_title' => self::$row->cat_title, 'exclude_cat_id' => self::$row->cat_id], true)->f(0); if ($exists) { throw new Exception(__('Category with same name already exists.')); } - $cur = App::con()->openCursor($C2->cat_table); - $cur->setField('cat_title', self::$cattitle); - $cur->setField('cat_desc', self::$catdesc); - - $utils->updCategory(self::$catid, $cur); + $cat_id = $utils->updCategory(self::$row->cat_id, self::$row->getCursor()); Notices::addSuccessNotice( __('Category successfully updated.') @@ -90,14 +77,20 @@ class ManageCat extends Process My::redirect(['part' => 'cats']); } // delete category - if (!empty($_POST['delete']) && !empty(self::$catid)) { - $utils->delCategory(self::$catid); + if (!empty($_POST['delete']) && !empty(self::$row->cat_id)) { + $utils->delCategory(self::$row->cat_id); Notices::addSuccessNotice( __('Category successfully deleted.') ); My::redirect(['part' => 'cats']); } + + if (self::$row->cat_id) { + self::$row = new RecordCatsRow( + $utils->getCategories(['cat_id' => self::$row->cat_id]) + ); + } } catch (Exception $e) { App::error()->add($e->getMessage()); } @@ -113,21 +106,13 @@ class ManageCat extends Process $utils = new Utils(); - if (!empty(self::$catid)) { - $category = $utils->getCategories(['cat_id' => self::$catid]); - if (!$category->isEmpty()) { - self::$cattitle = (string) $category->f('cat_title'); - self::$catdesc = (string) $category->f('cat_desc'); - } - } - Page::openModule(My::name()); echo Page::breadcrumb([ - __('Plugins') => '', - My::name() => My::manageUrl(), - (empty(self::$catid) ? __('New category') : __('Edit category')) => '', + __('Plugins') => '', + My::name() => My::manageUrl(), + (empty(self::$row->cat_id) ? __('New category') : __('Edit category')) => '', ]) . Notices::getNotices(); @@ -142,8 +127,8 @@ class ManageCat extends Process ->render(); } - if (self::$catid) { - $links = (int) $utils->getLinks(['cat_id' => self::$catid], true)->f(0); + if (self::$row->cat_id) { + $links = (int) $utils->getLinks(['cat_id' => self::$row->cat_id], true)->f(0); echo (new Note()) ->class('info') ->text( @@ -161,20 +146,20 @@ class ManageCat extends Process (new Para()) ->items([ (new Label(__('Title:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('cattitle'), - (new Input('cattitle')) + ->for('cat_title'), + (new Input('cat_title')) ->size(65) ->maxlength(64) - ->value(Html::escapeHTML(self::$cattitle)), + ->value(Html::escapeHTML(self::$row->cat_title)), ]), (new Para()) ->items([ (new Label(__('Description:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('catdesc'), - (new Input('catdesc')) + ->for('cat_desc'), + (new Input('cat_desc')) ->size(65) ->maxlength(64) - ->value(Html::escapeHTML(self::$catdesc)), + ->value(Html::escapeHTML(self::$row->cat_desc)), ]), (new Para()) ->class('border-top') @@ -194,9 +179,9 @@ class ManageCat extends Process ->value(__('Delete') . ' (d)') ->accesskey('d'), ... My::hiddenFields([ - 'catid' => self::$catid, - 'part' => 'cat', - 'redir' => self::$module_redir, + 'cat_id' => self::$row->cat_id, + 'part' => 'cat', + 'redir' => self::$module_redir, ]), ]), ]) diff --git a/src/ManageCats.php b/src/ManageCats.php index 4e1d17c..62cb2de 100644 --- a/src/ManageCats.php +++ b/src/ManageCats.php @@ -107,7 +107,8 @@ class ManageCats extends Process $items = []; $i = 0; while ($categories->fetch()) { - $id = $categories->f('cat_id'); + $row = new RecordCatsRow($categories); + $id = (string) $row->cat_id; $items[] = (new Tr('l_' . $i)) ->class('line') @@ -120,7 +121,7 @@ class ManageCats extends Process ->max($categories->count()) ->value($i + 1) ->class('position') - ->title(Html::escapeHTML(sprintf(__('position of %s'), (string) $categories->f('cat_title')))), + ->title(Html::escapeHTML(sprintf(__('position of %s'), $row->cat_title))), (new Hidden(['dynorder[]', 'dynorder-' . $i], $id)), ]), (new Td()) @@ -134,19 +135,19 @@ class ManageCats extends Process ->items([ (new Link()) ->href(My::manageUrl([ - 'part' => 'cat', - 'catid' => $id, - 'redir' => My::manageUrl([ + 'part' => 'cat', + 'cat_id' => $id, + 'redir' => My::manageUrl([ 'part' => 'cats', 'redir' => self::$module_redir, ]), ])) ->title(__('Edit')) - ->text(Html::escapeHTML((string) $categories->f('cat_title'))), + ->text(Html::escapeHTML($row->cat_title)), ]), (new Td()) ->class('maximal') - ->text(Html::escapeHTML((string) $categories->f('cat_desc'))), + ->text(Html::escapeHTML($row->cat_desc)), ]); $i++; @@ -187,7 +188,7 @@ class ManageCats extends Process (new Link()) ->class('button add') ->href(My::manageUrl(['part' => 'cat', 'redir' => My::manageUrl(['part' => 'cats'])])) - ->text(__('New Link')), + ->text(__('New Category')), ]) ->render(); diff --git a/src/ManageLink.php b/src/ManageLink.php index 72548a0..532f060 100644 --- a/src/ManageLink.php +++ b/src/ManageLink.php @@ -36,15 +36,7 @@ use Exception; class ManageLink extends Process { private static string $module_redir = ''; - private static int $linkid = 0; - private static string $linktitle = ''; - private static string $linkdesc = ''; - private static string $linkauthor = ''; - private static string $linkurl = ''; - private static ?string $linkcat = ''; - private static string $linklang = ''; - private static string $linkimage = ''; - private static string $linknote = ''; + private static RecordLinksRow $row; public static function init(): bool { @@ -57,18 +49,9 @@ class ManageLink extends Process return false; } - $utils = new Utils(); - self::$module_redir = $_REQUEST['redir'] ?? ''; - self::$linkid = (int) ($_REQUEST['linkid'] ?? 0); - self::$linktitle = $_POST['linktitle'] ?? ''; - self::$linkdesc = $_POST['linkdesc'] ?? ''; - self::$linkauthor = $_POST['linkauthor'] ?? ''; - self::$linkurl = $_POST['linkurl'] ?? ''; - self::$linkcat = $_POST['linkcat'] ?? null; - self::$linklang = $_POST['linklang'] ?? App::auth()->getInfo('user_lang'); - self::$linkimage = $_POST['linkimage'] ?? ''; - self::$linknote = $_POST['linknote'] ?? ''; + self::$row = new RecordLinksRow(); + $utils = new Utils(); if (!empty($_POST['save'])) { try { @@ -76,44 +59,34 @@ class ManageLink extends Process App::config()->dotclearRoot() . '/' . App::blog()->settings()->system->get('public_path'), My::settings()->folder ); - if (empty(self::$linktitle)) { + if (empty(self::$row->link_title)) { throw new Exception(__('You must provide a title.')); } - if (empty(self::$linkauthor)) { + if (empty(self::$row->link_author)) { throw new Exception(__('You must provide an author.')); } - if (!preg_match('/https?:\/\/.+/', self::$linkimage)) { + if (!preg_match('/https?:\/\/.+/', self::$row->link_img)) { //throw new Exception(__('You must provide a link to an image.')); } - $cur = App::con()->openCursor($utils->table); - $cur->setField('link_title', self::$linktitle); - $cur->setField('link_desc', self::$linkdesc); - $cur->setField('link_author', self::$linkauthor); - $cur->setField('link_url', self::$linkurl); - $cur->setField('cat_id', self::$linkcat == '' ? null : self::$linkcat); - $cur->setField('link_lang', self::$linklang); - $cur->setField('link_img', self::$linkimage); - $cur->setField('link_note', self::$linknote); - // create a link - if (empty(self::$linkid)) { - $exists = $utils->getLinks(['link_title' => self::$linktitle], true)->f(0); + if (!self::$row->link_id) { + $exists = $utils->getLinks(['link_title' => self::$row->link_title], true)->f(0); if ($exists) { throw new Exception(__('Link with same name already exists.')); } - self::$linkid = $utils->addLink($cur); + $link_id = $utils->addLink(self::$row->getCursor()); Notices::addSuccessNotice( __('Link successfully created.') ); // update a link } else { - $exists = $utils->getLinks(['link_id' => self::$linkid], true)->f(0); + $exists = $utils->getLinks(['link_id' => self::$row->link_id], true)->f(0); if (!$exists) { throw new Exception(__('Unknown link.')); } - $utils->updLink(self::$linkid, $cur); + $link_id = $utils->updLink(self::$row->link_id, self::$row->getCursor()); Notices::addSuccessNotice( __('Link successfully updated.') @@ -121,9 +94,9 @@ class ManageLink extends Process } My::redirect( [ - 'part' => 'link', - 'linkid' => self::$linkid, - 'redir' => self::$module_redir, + 'part' => 'link', + 'link_id' => $link_id, + 'redir' => self::$module_redir, ] ); } catch (Exception $e) { @@ -131,9 +104,9 @@ class ManageLink extends Process } } - if (!empty($_POST['delete']) && !empty(self::$linkid)) { + if (!empty($_POST['delete']) && self::$row->link_id) { try { - $utils->delLink(self::$linkid); + $utils->delLink(self::$row->link_id); Notices::addSuccessNotice( __('Link successfully deleted.') @@ -148,18 +121,10 @@ class ManageLink extends Process } } - if (!empty(self::$linkid)) { - $link = $utils->getLinks(['link_id' => self::$linkid]); - if (!$link->isEmpty()) { - self::$linktitle = (string) $link->f('link_title'); - self::$linkdesc = (string) $link->f('link_desc'); - self::$linkauthor = (string) $link->f('link_author'); - self::$linkurl = (string) $link->f('link_url'); - self::$linkcat = (string) $link->f('cat_id'); - self::$linklang = (string) $link->f('link_lang'); - self::$linkimage = (string) $link->f('link_img'); - self::$linknote = (string) $link->f('link_note'); - } + if (self::$row->link_id) { + self::$row = new RecordLinksRow( + $utils->getLinks(['link_id' => self::$row->link_id]) + ); } return true; @@ -181,9 +146,9 @@ class ManageLink extends Process echo Page::breadcrumb([ - __('Plugins') => '', - My::name() => My::manageUrl(), - (empty(self::$linkid) ? __('New link') : __('Edit link')) => '', + __('Plugins') => '', + My::name() => My::manageUrl(), + (empty(self::$row->link_id) ? __('New link') : __('Edit link')) => '', ]) . Notices::getNotices(); @@ -213,38 +178,38 @@ class ManageLink extends Process (new Para()) ->items([ (new Label(__('Title:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linktitle'), - (new Input('linktitle')) + ->for('link_title'), + (new Input('link_title')) ->size(65) ->maxlength(255) - ->value(Html::escapeHTML(self::$linktitle)), + ->value(Html::escapeHTML(self::$row->link_title)), ]), (new Para()) ->items([ (new Label(__('Description:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linkdesc'), - (new Input('linkdesc')) + ->for('link_desc'), + (new Input('link_desc')) ->size(65) ->maxlength(255) - ->value(Html::escapeHTML(self::$linkdesc)), + ->value(Html::escapeHTML(self::$row->link_desc)), ]), (new Para()) ->items([ (new Label(__('Author:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linkauthor'), - (new Input('linkauthor')) + ->for('link_author'), + (new Input('link_author')) ->size(65) ->maxlength(255) - ->value(Html::escapeHTML(self::$linkauthor)), + ->value(Html::escapeHTML(self::$row->link_author)), ]), (new Para()) ->items([ (new Label(__('Details URL:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linkurl'), - (new Input('linkurl')) + ->for('link_url'), + (new Input('link_url')) ->size(65) ->maxlength(255) - ->value(Html::escapeHTML(self::$linkurl)), + ->value(Html::escapeHTML(self::$row->link_url)), (new Link('newlinksearch')) ->class('modal hidden-if-no-js') ->href('http://google.com') @@ -254,11 +219,11 @@ class ManageLink extends Process (new Para()) ->items([ (new Label(__('Image URL:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linkimage'), - (new Input('linkimage')) + ->for('link_img'), + (new Input('link_img')) ->size(65) ->maxlength(255) - ->value(Html::escapeHTML(self::$linkimage)), + ->value(Html::escapeHTML(self::$row->link_img)), (new Link('newimagesearch')) ->class('modal hidden-if-no-js') ->href('http://amazon.com') @@ -296,27 +261,27 @@ class ManageLink extends Process (new Para()) ->items([ (new Label(__('Category:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linkcat'), - (new Select('linkcat')) + ->for('cat_id'), + (new Select('cat_id')) ->items(Combo::categoriesCombo()) - ->default(self::$linkcat), + ->default((string) self::$row->cat_id), ]), (new Para()) ->items([ (new Label(__('Lang:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linklang'), - (new Select('linklang')) + ->for('link_lang'), + (new Select('link_lang')) ->items(Combo::langsCombo()) - ->default(self::$linklang), + ->default(self::$row->link_lang), ]), (new Para()) ->items([ (new Label(__('Rating:'), Label::OUTSIDE_LABEL_BEFORE)) - ->for('linknote'), - (new Number('linknote')) + ->for('link_note'), + (new Number('link_note')) ->min(0) ->max(20) - ->value(self::$linknote), + ->value(self::$row->link_note), ]), ]), ]), @@ -338,9 +303,9 @@ class ManageLink extends Process ->value(__('Delete') . ' (d)') ->accesskey('d'), ... My::hiddenFields([ - 'linkid' => self::$linkid, - 'part' => 'link', - 'redir' => self::$module_redir, + 'link_id' => self::$row->link_id, + 'part' => 'link', + 'redir' => self::$module_redir, ]), ]), ]), diff --git a/src/ManageLinks.php b/src/ManageLinks.php index 7755634..694a8e2 100644 --- a/src/ManageLinks.php +++ b/src/ManageLinks.php @@ -10,7 +10,6 @@ use Dotclear\Core\Backend\Filter\{ Filters, FiltersLibrary }; -use Dotclear\Core\Backend\Listing\Listing; use Dotclear\Core\Backend\{ Notices, Page @@ -40,7 +39,7 @@ class ManageLinks extends Process { private static Actions $module_action; private static Filters $module_filter; - private static Listing $module_listing; + private static BackendListingLinks $module_listing; private static int $module_counter = 0; private static ?bool $module_rendered = null; @@ -67,7 +66,7 @@ class ManageLinks extends Process self::$module_filter->add(FiltersLibrary::getPageFilter()); self::$module_filter->add(FiltersLibrary::getSearchFilter()); self::$module_filter->add(FiltersLibrary::getSelectFilter( - 'catid', + 'cat_id', __('Category:'), Combo::categoriesCombo(), 'cat_id' @@ -171,7 +170,7 @@ class ManageLinks extends Process (new Label(__('Selected links action:'), Label::OUTSIDE_LABEL_BEFORE)) ->for('action'), (new Select('action')) - ->items(self::$module_action->getCombo()), + ->items(self::$module_action->getCombo() ?? []), (new Submit('do-action')) ->value(__('ok')), ... My::hiddenFields(self::$module_filter->values(true)), diff --git a/src/My.php b/src/My.php index 6573758..2aed24e 100644 --- a/src/My.php +++ b/src/My.php @@ -17,14 +17,14 @@ use Dotclear\Module\MyPlugin; class My extends MyPlugin { /** - * Link table name. + * Links table name. * * @var string CINECTURLINK_TABLE_NAME */ public const CINECTURLINK_TABLE_NAME = 'cinecturlink2'; /** - * Category table name. + * Categories table name. * * @var string CATEGORY_TABLE_NAME */ diff --git a/src/PluginSitemaps.php b/src/PluginSitemaps.php index fb6ac4f..12cbc48 100644 --- a/src/PluginSitemaps.php +++ b/src/PluginSitemaps.php @@ -4,23 +4,30 @@ declare(strict_types=1); namespace Dotclear\Plugin\cinecturlink2; +use ArrayObject; use Dotclear\App; +use Dotclear\Plugin\sitemaps\Sitemap; /** * @brief cinecturlink2 sitemaps class. * @ingroup cinecturlink2 * + * Add Cinecturlink public main page and categories pages to plugin sitemap. + * * @author Jean-Christian Denis (author) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class PluginSitemaps { - public static function sitemapsDefineParts($map_parts) + /** + * @param ArrayObject $map_parts + */ + public static function sitemapsDefineParts(ArrayObject $map_parts): void { $map_parts->offsetSet(My::name(), My::id()); } - public static function sitemapsURLsCollect($sitemaps) + public static function sitemapsURLsCollect(Sitemap $sitemaps): void { if (App::plugins()->moduleExists('cinecturlink2') && App::blog()->settings()->get('sitemaps')->get('sitemaps_cinecturlink2_url') @@ -34,7 +41,7 @@ class PluginSitemaps $C2 = new Utils(); $cats = $C2->getCategories(); while ($cats->fetch()) { - $sitemaps->addEntry($base . '/' . My::settings()->get('public_caturl') . '/' . urlencode($cats->cat_title), $prio, $freq); + $sitemaps->addEntry($base . '/' . My::settings()->get('public_caturl') . '/' . urlencode((string) $cats->field('cat_title')), $prio, $freq); } } } diff --git a/src/RecordCatsRow.php b/src/RecordCatsRow.php new file mode 100644 index 0000000..320551d --- /dev/null +++ b/src/RecordCatsRow.php @@ -0,0 +1,41 @@ +cat_id = (int) ($rs?->field('cat_id') ?? $_REQUEST['cat_id'] ?? 0); + $this->cat_title = (string) ($rs?->field('cat_title') ?? $_POST['cat_title'] ?? ''); + $this->cat_desc = (string) ($rs?->field('cat_desc') ?? $_POST['cat_desc'] ?? ''); + $this->cat_pos = (int) ($rs?->field('cat_pos') ?? 0); + } + + public function getCursor(): Cursor + { + $cur = App::con()->openCursor(App::con()->prefix() . My::CATEGORY_TABLE_NAME); + $cur->setField('cat_title', $this->cat_title); + $cur->setField('cat_desc', $this->cat_desc); + + return $cur; + } +} diff --git a/src/RecordLinksRow.php b/src/RecordLinksRow.php new file mode 100644 index 0000000..bcd105c --- /dev/null +++ b/src/RecordLinksRow.php @@ -0,0 +1,63 @@ +link_id = (int) ($rs?->field('link_id') ?? $_REQUEST['link_id'] ?? 0); + $this->link_title = (string) ($rs?->field('link_title') ?? $_POST['link_title'] ?? ''); + $this->link_desc = (string) ($rs?->field('link_desc') ?? $_POST['link_desc'] ?? ''); + $this->link_author = (string) ($rs?->field('link_author') ?? $_POST['link_author'] ?? ''); + $this->link_url = (string) ($rs?->field('link_url') ?? $_POST['link_url'] ?? ''); + $this->cat_id = $rs?->field('cat_id') ? (int) $rs->field('cat_id') : (isset($_POST['cat_id']) ? (int) $_POST['cat_id'] : null); + $this->cat_title = (string) ($rs?->field('cat_title') ?? $_POST['cat_title'] ?? ''); + $this->link_lang = (string) ($rs?->field('link_lang') ?? $_POST['link_lang'] ?? App::auth()->getInfo('user_lang')); + $this->link_img = (string) ($rs?->field('link_img') ?? $_POST['link_img'] ?? ''); + $this->link_note = (string) ($rs?->field('link_note') ?? $_POST['link_note'] ?? ''); + $this->link_upddt = (string) ($rs?->field('link_upddt') ?? ''); + $this->link_count = abs((int) $rs?->field('link_count')); + } + + public function getCursor(): Cursor + { + $cur = App::con()->openCursor(App::con()->prefix() . My::CINECTURLINK_TABLE_NAME); + $cur->setField('link_title', $this->link_title); + $cur->setField('link_desc', $this->link_desc); + $cur->setField('link_author', $this->link_author); + $cur->setField('link_url', $this->link_url); + $cur->setField('cat_id', $this->cat_id); + $cur->setField('link_lang', $this->link_lang); + $cur->setField('link_img', $this->link_img); + $cur->setField('link_note', $this->link_note); + + return $cur; + } +} diff --git a/src/Utils.php b/src/Utils.php index 36eb769..5b30e43 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -72,8 +72,9 @@ class Utils /** * Get links. * - * @param array $params Query params - * @param bool $count_only Count only result + * @param array $params Query params + * @param bool $count_only Count only result + * * @return MetaRecord MetaRecord instance */ public function getLinks(array $params = [], bool $count_only = false): MetaRecord @@ -264,8 +265,10 @@ class Utils * @param int $id Link ID * @param Cursor $cur Cursor instance * @param bool $behavior Call related behaviors + * + * @return int The link ID */ - public function updLink(int $id, Cursor $cur, bool $behavior = true): void + public function updLink(int $id, Cursor $cur, bool $behavior = true): int { if (empty($id)) { throw new Exception(__('No such link ID')); @@ -283,6 +286,8 @@ class Utils # --BEHAVIOR-- cinecturlink2AfterUpdLink App::behavior()->callBehavior('cinecturlink2AfterUpdLink', $cur, $id); } + + return $id; } /** @@ -318,18 +323,20 @@ class Utils { $sql = new SelectStatement(); - return $sql + $rs = $sql ->column($sql->max('link_id')) ->from($this->table) - ->select() - ->f(0) + 1; + ->select(); + + return is_null($rs) ? 1 : (int) $rs->f(0) + 1; } /** * Get categories. * - * @param array $params Query params - * @param bool $count_only Count only result + * @param array $params Query params + * @param bool $count_only Count only result + * * @return MetaRecord Record instance */ public function getCategories(array $params = [], bool $count_only = false): MetaRecord @@ -451,8 +458,10 @@ class Utils * * @param int $id Category ID * @param Cursor $cur Cursor instance + * + * @return int The category ID */ - public function updCategory(int $id, Cursor $cur): void + public function updCategory(int $id, Cursor $cur): int { if (empty($id)) { throw new Exception(__('No such category ID')); @@ -467,6 +476,8 @@ class Utils ->update($cur); $this->trigger(); + + return $id; } /** @@ -510,11 +521,12 @@ class Utils { $sql = new SelectStatement(); - return $sql + $rs = $sql ->column($sql->max('cat_id')) ->from($this->cat_table) - ->select() - ->f(0) + 1; + ->select(); + + return is_null($rs) ? 1 : (int) $rs->f(0) + 1; } /** @@ -526,12 +538,13 @@ class Utils { $sql = new SelectStatement(); - return $sql + $rs = $sql ->column($sql->max('cat_pos')) ->from($this->cat_table) ->where('blog_id = ' . $sql->quote($this->blog)) - ->select() - ->f(0) + 1; + ->select(); + + return is_null($rs) ? 1 : (int) $rs->f(0) + 1; } /** @@ -569,12 +582,15 @@ class Utils /** * Get list of public directories. * - * @return array Directories + * @return array Directories */ public static function getPublicDirs(): array { $dirs = []; $all = Files::getDirList(App::blog()->publicPath()); + if (empty($all['dirs'])) { + return $dirs; + } foreach ($all['dirs'] as $dir) { $dir = substr($dir, strlen(App::blog()->publicPath()) + 1); $dirs[$dir] = $dir; diff --git a/src/WidgetCatsDescriptor.php b/src/WidgetCatsDescriptor.php new file mode 100644 index 0000000..00bc60e --- /dev/null +++ b/src/WidgetCatsDescriptor.php @@ -0,0 +1,32 @@ +title = (string) $w->get('title'); + $this->class = (string) $w->get('class'); + $this->content_only = !empty($w->get('content_only')); + + $this->shownumlink = !empty($w->get('shownumlink')); + } +} diff --git a/src/WidgetLinksDescriptor.php b/src/WidgetLinksDescriptor.php new file mode 100644 index 0000000..a2e91b3 --- /dev/null +++ b/src/WidgetLinksDescriptor.php @@ -0,0 +1,48 @@ +title = (string) $w->get('title'); + $this->class = (string) $w->get('class'); + $this->content_only = !empty($w->get('content_only')); + + $this->category = (string) $w->get('category'); + $this->sortby = (string) $w->get('sortby'); + $this->sort = $w->get('sort') == 'desc' ? 'desc' : 'asc'; + $this->limit = abs((int) $w->get('limit')); + $this->shownote = !empty($w->get('shownote')); + $this->showdesc = !empty($w->get('showdesc')); + $this->withlink = !empty($w->get('withlink')); + $this->showauthor = !empty($w->get('showauthor')); + $this->showpagelink = !empty($w->get('showpagelink')); + } +} diff --git a/src/Widgets.php b/src/Widgets.php index 215dbdd..d48e2d7 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -8,6 +8,7 @@ use Dotclear\App; use Dotclear\Helper\Html\Html; use Dotclear\Plugin\widgets\WidgetsStack; use Dotclear\Plugin\widgets\WidgetsElement; +use Exception; /** * @brief cinecturlink2 widgets class. @@ -18,6 +19,20 @@ use Dotclear\Plugin\widgets\WidgetsElement; */ class Widgets { + /** + * Widget cinecturlink links ID. + * + * @var string WIDGET_ID_LINKS + */ + private const WIDGET_ID_LINKS = 'cinecturlink2links'; + + /** + * Widget cinecturlink categories ID. + * + * @var string WIDGET_ID_CATS + */ + private const WIDGET_ID_CATS = 'cinecturlink2cats'; + public static function init(WidgetsStack $w): void { $categories_combo = array_merge( @@ -39,7 +54,7 @@ class Widgets $w ->create( - 'cinecturlink2links', + self::WIDGET_ID_LINKS, __('My cinecturlink'), self::parseLinks(...), null, @@ -112,7 +127,7 @@ class Widgets $w ->create( - 'cinecturlink2cats', + self::WIDGET_ID_CATS, __('List of categories of cinecturlink'), self::parseCats(...), null, @@ -139,30 +154,29 @@ class Widgets ->addOffline(); } - public static function parseLinks(WidgetsElement $w): string + public static function parseLinks(WidgetsElement $widget): string { - if (!My::settings()->avtive - || !$w->checkHomeOnly(App::url()->type) + if (!My::settings()->get('active') + || !$widget->checkHomeOnly(App::url()->type) ) { return ''; } - $C2 = new Utils(); + $wdesc = new WidgetLinksDescriptor($widget); + $utils = new Utils(); $params = []; - if ($w->category) { - if ($w->category == 'null') { + if ($wdesc->category) { + if ($wdesc->category == 'null') { $params['sql'] = ' AND L.cat_id IS NULL '; - } elseif (is_numeric($w->category)) { - $params['cat_id'] = (int) $w->category; + } elseif (is_numeric($wdesc->category)) { + $params['cat_id'] = (int) $wdesc->category; } } - $limit = abs((int) $w->limit); - // Tirage aléatoire: Consomme beaucoup de ressources! - if ($w->sortby == 'RANDOM') { - $big_rs = $C2->getLinks($params); + if ($wdesc->sortby == 'RANDOM') { + $big_rs = $utils->getLinks($params); if ($big_rs->isEmpty()) { return ''; @@ -173,94 +187,93 @@ class Widgets $ids[] = $big_rs->link_id; } shuffle($ids); - $ids = array_slice($ids, 0, $limit); + $ids = array_slice($ids, 0, $wdesc->limit); $params['link_id'] = []; foreach ($ids as $id) { $params['link_id'][] = $id; } - } elseif ($w->sortby == 'COUNTER') { + } elseif ($wdesc->sortby == 'COUNTER') { $params['order'] = 'link_count asc'; - $params['limit'] = $limit; + $params['limit'] = $wdesc->limit; } else { - $params['order'] = $w->sortby; - $params['order'] .= $w->sort == 'asc' ? ' asc' : ' desc'; - $params['limit'] = $limit; + $params['order'] = $wdesc->sortby . ' ' . $wdesc->sort; + $params['limit'] = $wdesc->limit; } - $rs = $C2->getLinks($params); + $rs = $utils->getLinks($params); if ($rs->isEmpty()) { return ''; } - $widthmax = (int) My::settings()->widthmax; + $widthmax = (int) My::settings()->get('widthmax'); $style = $widthmax ? ' style="width:' . $widthmax . 'px;"' : ''; $entries = []; while ($rs->fetch()) { - $url = $rs->link_url; - $img = $rs->link_img; - $title = Html::escapeHTML($rs->link_title); - $author = Html::escapeHTML($rs->link_author); - $cat = Html::escapeHTML($rs->cat_title); - $note = $w->shownote ? ' (' . $rs->link_note . '/20)' : ''; - $desc = $w->showdesc ? '
' . Html::escapeHTML($rs->link_desc) . '' : ''; - $lang = $rs->link_lang ? ' hreflang="' . $rs->link_lang . '"' : ''; - $count = abs((int) $rs->link_count); + $row = new RecordLinksRow($rs); # --BEHAVIOR-- cinecturlink2WidgetLinks - $bhv = App::behavior()->callBehavior('cinecturlink2WidgetLinks', $rs->link_id); + $bhv = App::behavior()->callBehavior('cinecturlink2WidgetLinks', $row->link_id); - $entries[] = '

' . - ($w->withlink && !empty($url) ? '' : '') . - '' . $title . '' . $note . '
' . - ($w->showauthor ? $author . '
' : '') . '
' . - '' . $title . ' - ' . $author . '' . - $desc . - ($w->withlink && !empty($url) ? '
' : '') . - '

' . $bhv; + $tmp = ''; + if ($wdesc->withlink && !empty($row->link_url)) { + $tmp .= 'link_lang ? ' hreflang="' . $row->link_lang . '"' : '') . ' title="' . Html::escapeHTML($row->cat_title) . '">'; + } + $tmp .= '' . Html::escapeHTML($row->link_title) . '' . ($wdesc->shownote ? ' (' . $row->link_note . '/20)' : '') . '
'; + if ($wdesc->showauthor) { + $tmp .= Html::escapeHTML($row->link_author) . '
'; + } + $tmp .= '
' . Html::escapeHTML($row->link_title) . ' - ' . Html::escapeHTML($row->link_author) . ''; + if ($wdesc->showdesc) { + $tmp .= '
' . Html::escapeHTML($row->link_desc) . ''; + } + if ($wdesc->withlink && !empty($row->link_url)) { + $tmp .= '
'; + } + + $entries[] = '

' . $tmp . '

' . $bhv; try { - $cur = App::con()->openCursor($C2->table); - $cur->link_count = ($count + 1); - $C2->updLink((int) $rs->link_id, $cur, false); - } catch (Exception $e) { + $cur = App::con()->openCursor($utils->table); + $cur->setField('link_count', ($row->link_count + 1)); + $utils->updLink($row->link_id, $cur, false); + } catch (Exception) { } } # Tirage aléatoire - if ($w->sortby == 'RANDOM' - || $w->sortby == 'COUNTER' - ) { + if (in_array($wdesc->sortby, ['RANDOM', 'COUNTER'])) { shuffle($entries); - if (My::settings()->triggeronrandom) { + if (My::settings()->get('triggeronrandom')) { App::blog()->triggerBlog(); } } - return $w->renderDiv( - (bool) $w->content_only, - 'cinecturlink2list ' . $w->class, + return $widget->renderDiv( + $wdesc->content_only, + $widget->id() . ' ' . $wdesc->class, '', - ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . implode(' ', $entries) . + ($wdesc->title ? $widget->renderTitle(Html::escapeHTML($wdesc->title)) : '') . implode(' ', $entries) . ( - $w->showpagelink && My::settings()->public_active ? + $wdesc->showpagelink && My::settings()->get('public_active') ? '

' . __('More links') . '

' : '' ) ); } - public static function parseCats(WidgetsElement $w): string + public static function parseCats(WidgetsElement $widget): string { - if (!My::settings()->avtive - || !My::settings()->public_active - || !$w->checkHomeOnly(App::url()->type) + if (!My::settings()->get('active') + || !My::settings()->get('public_active') + || !$widget->checkHomeOnly(App::url()->type) ) { return ''; } - $C2 = new Utils(); - $rs = $C2->getCategories([]); + $wdesc = new WidgetCatsDescriptor($widget); + $utils = new Utils(); + $rs = $utils->getCategories([]); if ($rs->isEmpty()) { return ''; } @@ -269,26 +282,28 @@ class Widgets $res[] = '
  • ' . __('all links') . - '' . ($w->shownumlink ? ' (' . ($C2->getLinks([], true)->f(0)) . ')' : '') . + '' . ($wdesc->shownumlink ? ' (' . ($utils->getLinks([], true)->f(0)) . ')' : '') . '
  • '; while ($rs->fetch()) { + $row = new RecordCatsRow($rs); + $res[] = '
  • ' . - Html::escapeHTML($rs->cat_title) . - '' . ($w->shownumlink ? ' (' . - ($C2->getLinks(['cat_id' => $rs->cat_id], true)->f(0)) . ')' : '') . + Html::escapeHTML($row->cat_title) . + '' . ($wdesc->shownumlink ? ' (' . + ($utils->getLinks(['cat_id' => $row->cat_id], true)->f(0)) . ')' : '') . '
  • '; } - return $w->renderDiv( - (bool) $w->content_only, - 'cinecturlink2cat ' . $w->class, + return $widget->renderDiv( + $wdesc->content_only, + $widget->id() . ' ' . $wdesc->class, '', - ($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') . + ($wdesc->title ? $widget->renderTitle(Html::escapeHTML($wdesc->title)) : '') . '
      ' . implode(' ', $res) . '
    ' ); }