cinecturlink2/src/Widgets.php

311 lines
9.5 KiB
PHP
Raw Normal View History

2021-09-07 12:33:18 +00:00
<?php
2023-10-15 18:49:39 +00:00
2023-08-24 11:55:44 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\cinecturlink2;
2021-09-07 12:33:18 +00:00
2023-10-15 18:49:39 +00:00
use Dotclear\App;
2023-08-24 11:55:44 +00:00
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2023-10-23 20:54:27 +00:00
use Exception;
2021-09-07 12:33:18 +00:00
2023-10-15 18:49:39 +00:00
/**
* @brief cinecturlink2 widgets class.
* @ingroup cinecturlink2
*
* @author Jean-Christian Denis (author)
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-08-24 11:55:44 +00:00
class Widgets
2021-09-07 12:33:18 +00:00
{
2023-10-23 20:54:27 +00:00
/**
* 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';
2023-10-15 18:49:39 +00:00
public static function init(WidgetsStack $w): void
2021-09-07 13:21:38 +00:00
{
2023-08-24 11:55:44 +00:00
$categories_combo = array_merge(
Combo::categoriesCombo(),
[__('Uncategorized') => 'null']
);
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
$sortby_combo = [
2021-11-02 22:55:41 +00:00
__('Update date') => 'link_upddt',
__('My rating') => 'link_note',
__('Title') => 'link_title',
__('Random') => 'RANDOM',
2022-11-17 21:01:00 +00:00
__('Number of views') => 'COUNTER',
2021-09-07 14:00:49 +00:00
];
$order_combo = [
2021-11-02 22:55:41 +00:00
__('Ascending') => 'asc',
2022-11-17 21:01:00 +00:00
__('Descending') => 'desc',
2021-09-07 14:00:49 +00:00
];
$w
->create(
2023-10-23 20:54:27 +00:00
self::WIDGET_ID_LINKS,
2021-09-07 14:00:49 +00:00
__('My cinecturlink'),
2023-10-15 18:49:39 +00:00
self::parseLinks(...),
2021-09-07 14:00:49 +00:00
null,
__('Show selection of cinecturlinks')
2021-09-07 13:21:38 +00:00
)
2021-09-07 14:00:49 +00:00
->addTitle(
__('My cinecturlink'),
)
->setting(
'category',
__('Category:'),
'',
'combo',
$categories_combo
)
->setting(
'sortby',
__('Order by:'),
'link_upddt',
'combo',
$sortby_combo
)
->setting(
'sort',
__('Sort: (only for date, note and title)'),
'desc',
'combo',
$order_combo
)
->setting(
'limit',
__('Limit:'),
10,
'text'
)
->setting(
'withlink',
__('Enable link'),
1,
'check'
)
->setting(
'showauthor',
__('Show author'),
1,
'check'
)
->setting(
'shownote',
__('Show my rating'),
0,
'check'
)
->setting(
'showdesc',
__('Show description'),
0,
'check'
)
->setting(
'showpagelink',
__('Show a link to cinecturlink page'),
0,
'check'
)
->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
$w
->create(
2023-10-23 20:54:27 +00:00
self::WIDGET_ID_CATS,
2021-09-07 14:00:49 +00:00
__('List of categories of cinecturlink'),
2023-10-15 18:49:39 +00:00
self::parseCats(...),
2021-09-07 14:00:49 +00:00
null,
__('List of categories of cinecturlink')
2021-09-07 13:21:38 +00:00
)
2021-09-07 14:00:49 +00:00
->addTitle(
__('My cinecturlink by categories')
)
->setting(
'title',
__('Title:'),
__('My cinecturlink by categories'),
'text'
)
->setting(
'shownumlink',
__('Show number of links'),
0,
'check'
)
->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
public static function parseLinks(WidgetsElement $widget): string
2021-09-07 13:21:38 +00:00
{
2023-10-23 20:54:27 +00:00
if (!My::settings()->get('active')
|| !$widget->checkHomeOnly(App::url()->type)
2021-09-07 13:21:38 +00:00
) {
2023-08-24 11:55:44 +00:00
return '';
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
$wdesc = new WidgetLinksDescriptor($widget);
$utils = new Utils();
2023-08-24 11:55:44 +00:00
$params = [];
2021-09-07 13:21:38 +00:00
2023-10-23 20:54:27 +00:00
if ($wdesc->category) {
if ($wdesc->category == 'null') {
2021-09-07 13:21:38 +00:00
$params['sql'] = ' AND L.cat_id IS NULL ';
2023-10-23 20:54:27 +00:00
} elseif (is_numeric($wdesc->category)) {
$params['cat_id'] = (int) $wdesc->category;
2021-09-07 13:21:38 +00:00
}
}
2021-09-07 14:00:49 +00:00
// Tirage al<61>atoire: Consomme beaucoup de ressources!
2023-10-23 20:54:27 +00:00
if ($wdesc->sortby == 'RANDOM') {
$big_rs = $utils->getLinks($params);
2021-09-07 13:21:38 +00:00
if ($big_rs->isEmpty()) {
2023-08-24 11:55:44 +00:00
return '';
2021-09-07 13:21:38 +00:00
}
2021-11-02 22:55:41 +00:00
$ids = [];
while ($big_rs->fetch()) {
2021-09-07 13:21:38 +00:00
$ids[] = $big_rs->link_id;
}
shuffle($ids);
2023-10-23 20:54:27 +00:00
$ids = array_slice($ids, 0, $wdesc->limit);
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
$params['link_id'] = [];
2021-11-02 22:55:41 +00:00
foreach ($ids as $id) {
2021-09-07 13:21:38 +00:00
$params['link_id'][] = $id;
}
2023-10-23 20:54:27 +00:00
} elseif ($wdesc->sortby == 'COUNTER') {
2021-09-07 13:21:38 +00:00
$params['order'] = 'link_count asc';
2023-10-23 20:54:27 +00:00
$params['limit'] = $wdesc->limit;
2021-09-07 14:00:49 +00:00
} else {
2023-10-23 20:54:27 +00:00
$params['order'] = $wdesc->sortby . ' ' . $wdesc->sort;
$params['limit'] = $wdesc->limit;
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
$rs = $utils->getLinks($params);
2021-09-07 13:21:38 +00:00
if ($rs->isEmpty()) {
2023-08-24 11:55:44 +00:00
return '';
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
$widthmax = (int) My::settings()->get('widthmax');
2021-11-02 22:55:41 +00:00
$style = $widthmax ? ' style="width:' . $widthmax . 'px;"' : '';
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
$entries = [];
2021-11-02 22:55:41 +00:00
while ($rs->fetch()) {
2023-10-23 20:54:27 +00:00
$row = new RecordLinksRow($rs);
2021-09-07 13:21:38 +00:00
# --BEHAVIOR-- cinecturlink2WidgetLinks
2023-10-23 20:54:27 +00:00
$bhv = App::behavior()->callBehavior('cinecturlink2WidgetLinks', $row->link_id);
$tmp = '';
if ($wdesc->withlink && !empty($row->link_url)) {
$tmp .= '<a href="' . $row->link_url . '"' . ($row->link_lang ? ' hreflang="' . $row->link_lang . '"' : '') . ' title="' . Html::escapeHTML($row->cat_title) . '">';
}
$tmp .= '<strong>' . Html::escapeHTML($row->link_title) . '</strong>' . ($wdesc->shownote ? ' <em>(' . $row->link_note . '/20)</em>' : '') . '<br />';
if ($wdesc->showauthor) {
$tmp .= Html::escapeHTML($row->link_author) . '<br />';
}
$tmp .= '<br /><img src="' . $row->link_img . '" alt="' . Html::escapeHTML($row->link_title) . ' - ' . Html::escapeHTML($row->link_author) . '"' . $style . ' />';
if ($wdesc->showdesc) {
$tmp .= '<br /><em>' . Html::escapeHTML($row->link_desc) . '</em>';
}
if ($wdesc->withlink && !empty($row->link_url)) {
$tmp .= '</a>';
}
2021-09-07 13:21:38 +00:00
2023-10-23 20:54:27 +00:00
$entries[] = '<p style="text-align:center;">' . $tmp . '</p>' . $bhv;
2021-09-07 13:21:38 +00:00
try {
2023-10-23 20:54:27 +00:00
$cur = App::con()->openCursor($utils->table);
$cur->setField('link_count', ($row->link_count + 1));
$utils->updLink($row->link_id, $cur, false);
} catch (Exception) {
2021-09-07 13:21:38 +00:00
}
}
# Tirage al<61>atoire
2023-10-23 20:54:27 +00:00
if (in_array($wdesc->sortby, ['RANDOM', 'COUNTER'])) {
2021-09-07 13:21:38 +00:00
shuffle($entries);
2023-10-23 20:54:27 +00:00
if (My::settings()->get('triggeronrandom')) {
2023-10-15 18:49:39 +00:00
App::blog()->triggerBlog();
2021-09-07 13:21:38 +00:00
}
}
2023-10-23 20:54:27 +00:00
return $widget->renderDiv(
$wdesc->content_only,
$widget->id() . ' ' . $wdesc->class,
2021-11-02 22:55:41 +00:00
'',
2023-10-23 20:54:27 +00:00
($wdesc->title ? $widget->renderTitle(Html::escapeHTML($wdesc->title)) : '') . implode(' ', $entries) .
2021-11-02 22:55:41 +00:00
(
2023-10-23 20:54:27 +00:00
$wdesc->showpagelink && My::settings()->get('public_active') ?
2023-10-15 18:49:39 +00:00
'<p><a href="' . App::blog()->url() . App::url()->getBase(My::id()) . '" title="' . __('view all links') . '">' . __('More links') . '</a></p>' : ''
2021-09-07 14:00:49 +00:00
)
);
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
public static function parseCats(WidgetsElement $widget): string
2021-09-07 13:21:38 +00:00
{
2023-10-23 20:54:27 +00:00
if (!My::settings()->get('active')
|| !My::settings()->get('public_active')
|| !$widget->checkHomeOnly(App::url()->type)
2021-09-07 13:21:38 +00:00
) {
2023-08-24 11:55:44 +00:00
return '';
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
$wdesc = new WidgetCatsDescriptor($widget);
$utils = new Utils();
$rs = $utils->getCategories([]);
2021-09-07 13:21:38 +00:00
if ($rs->isEmpty()) {
2023-08-24 11:55:44 +00:00
return '';
2021-09-07 13:21:38 +00:00
}
2021-11-02 22:55:41 +00:00
$res = [];
$res[] = '<li><a href="' .
2023-10-15 18:49:39 +00:00
App::blog()->url() . App::url()->getBase(My::id()) .
2021-09-07 14:00:49 +00:00
'" title="' . __('view all links') . '">' . __('all links') .
2023-10-23 20:54:27 +00:00
'</a>' . ($wdesc->shownumlink ? ' (' . ($utils->getLinks([], true)->f(0)) . ')' : '') .
2021-09-07 14:00:49 +00:00
'</li>';
2021-09-07 13:21:38 +00:00
2021-11-02 22:55:41 +00:00
while ($rs->fetch()) {
2023-10-23 20:54:27 +00:00
$row = new RecordCatsRow($rs);
2021-11-02 22:55:41 +00:00
$res[] = '<li><a href="' .
2023-10-15 18:49:39 +00:00
App::blog()->url() . App::url()->getBase('cinecturlink2') . '/' .
2023-10-23 20:54:27 +00:00
My::settings()->get('public_caturl') . '/' .
urlencode($row->cat_title) .
2021-11-02 22:55:41 +00:00
'" title="' . __('view links of this category') . '">' .
2023-10-23 20:54:27 +00:00
Html::escapeHTML($row->cat_title) .
'</a>' . ($wdesc->shownumlink ? ' (' .
($utils->getLinks(['cat_id' => $row->cat_id], true)->f(0)) . ')' : '') .
2021-09-07 14:00:49 +00:00
'</li>';
2021-09-07 13:21:38 +00:00
}
2023-10-23 20:54:27 +00:00
return $widget->renderDiv(
$wdesc->content_only,
$widget->id() . ' ' . $wdesc->class,
2021-11-02 22:55:41 +00:00
'',
2023-10-23 20:54:27 +00:00
($wdesc->title ? $widget->renderTitle(Html::escapeHTML($wdesc->title)) : '') .
2021-11-02 22:55:41 +00:00
'<ul>' . implode(' ', $res) . '</ul>'
2021-09-07 14:00:49 +00:00
);
2021-09-07 13:21:38 +00:00
}
2021-11-02 22:55:41 +00:00
}