cinecturlink2/_widgets.php

314 lines
9.8 KiB
PHP
Raw Normal View History

2021-09-07 12:33:18 +00:00
<?php
2021-09-07 13:21:38 +00:00
/**
* @brief cinecturlink2, a plugin for Dotclear 2
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-09-07 12:33:18 +00:00
if (!defined('DC_RC_PATH')) {
2021-09-07 13:21:38 +00:00
return null;
2021-09-07 12:33:18 +00:00
}
2022-11-17 21:01:00 +00:00
dcCore::app()->addBehavior(
2021-09-07 13:21:38 +00:00
'initWidgets',
2021-09-07 14:00:49 +00:00
['cinecturlink2Widget', 'adminLinks']
2021-09-07 12:33:18 +00:00
);
2022-11-17 21:01:00 +00:00
dcCore::app()->addBehavior(
2021-09-07 13:21:38 +00:00
'initWidgets',
2021-09-07 14:00:49 +00:00
['cinecturlink2Widget', 'adminCats']
2021-09-07 12:33:18 +00:00
);
class cinecturlink2Widget
{
2021-09-07 13:21:38 +00:00
public static function adminLinks($w)
{
2022-11-17 21:01:00 +00:00
$C2 = new cinecturlink2();
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
$categories_combo = ['' => '', __('Uncategorized') => 'null'];
2021-11-02 22:55:41 +00:00
$categories = $C2->getCategories();
while ($categories->fetch()) {
$cat_title = html::escapeHTML($categories->cat_title);
2021-09-07 13:21:38 +00:00
$categories_combo[$cat_title] = $categories->cat_id;
}
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(
'cinecturlink2links',
__('My cinecturlink'),
['cinecturlink2Widget', 'publicLinks'],
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
}
public static function adminCats($w)
{
2021-09-07 14:00:49 +00:00
$w
->create(
'cinecturlink2cats',
__('List of categories of cinecturlink'),
['cinecturlink2Widget', 'publicCats'],
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
}
public static function publicLinks($w)
{
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->settings->addNamespace('cinecturlink2');
2021-09-07 13:21:38 +00:00
2022-11-17 21:01:00 +00:00
if (!dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_active
2022-12-08 21:53:43 +00:00
|| !$w->checkHomeOnly(dcCore::app()->url->type)
2021-09-07 13:21:38 +00:00
) {
return null;
}
2022-12-20 20:25:13 +00:00
$C2 = new cinecturlink2();
$aprams = [];
2021-09-07 13:21:38 +00:00
if ($w->category) {
if ($w->category == 'null') {
$params['sql'] = ' AND L.cat_id IS NULL ';
2021-11-02 22:55:41 +00:00
} elseif (is_numeric($w->category)) {
$params['cat_id'] = (int) $w->category;
2021-09-07 13:21:38 +00:00
}
}
2021-11-02 22:55:41 +00:00
$limit = abs((int) $w->limit);
2021-09-07 13:21:38 +00:00
2021-09-07 14:00:49 +00:00
// Tirage al<61>atoire: Consomme beaucoup de ressources!
2021-09-07 13:21:38 +00:00
if ($w->sortby == 'RANDOM') {
$big_rs = $C2->getLinks($params);
if ($big_rs->isEmpty()) {
return null;
}
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);
$ids = array_slice($ids, 0, $limit);
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;
}
2021-09-07 14:00:49 +00:00
} elseif ($w->sortby == 'COUNTER') {
2021-09-07 13:21:38 +00:00
$params['order'] = 'link_count asc';
$params['limit'] = $limit;
2021-09-07 14:00:49 +00:00
} else {
2021-09-07 13:21:38 +00:00
$params['order'] = $w->sortby;
$params['order'] .= $w->sort == 'asc' ? ' asc' : ' desc';
$params['limit'] = $limit;
}
$rs = $C2->getLinks($params);
if ($rs->isEmpty()) {
return null;
}
2022-11-17 21:01:00 +00:00
$widthmax = (int) dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_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()) {
$url = $rs->link_url;
$img = $rs->link_img;
$title = html::escapeHTML($rs->link_title);
2021-09-07 14:00:49 +00:00
$author = html::escapeHTML($rs->link_author);
2021-11-02 22:55:41 +00:00
$cat = html::escapeHTML($rs->cat_title);
$note = $w->shownote ? ' <em>(' . $rs->link_note . '/20)</em>' : '';
$desc = $w->showdesc ? '<br /><em>' . html::escapeHTML($rs->link_desc) . '</em>' : '';
$lang = $rs->link_lang ? ' hreflang="' . $rs->link_lang . '"' : '';
$count = abs((int) $rs->link_count);
2021-09-07 13:21:38 +00:00
# --BEHAVIOR-- cinecturlink2WidgetLinks
2022-11-17 21:01:00 +00:00
$bhv = dcCore::app()->callBehavior('cinecturlink2WidgetLinks', $rs->link_id);
2021-09-07 13:21:38 +00:00
2021-11-02 22:55:41 +00:00
$entries[] = '<p style="text-align:center;">' .
2021-09-07 14:00:49 +00:00
($w->withlink && !empty($url) ? '<a href="' . $url . '"' . $lang . ' title="' . $cat . '">' : '') .
'<strong>' . $title . '</strong>' . $note . '<br />' .
($w->showauthor ? $author . '<br />' : '') . '<br />' .
'<img src="' . $img . '" alt="' . $title . ' - ' . $author . '"' . $style . ' />' .
$desc .
($w->withlink && !empty($url) ? '</a>' : '') .
'</p>' . $bhv;
2021-09-07 13:21:38 +00:00
try {
2022-11-17 21:01:00 +00:00
$cur = dcCore::app()->con->openCursor($C2->table);
2021-09-07 13:21:38 +00:00
$cur->link_count = ($count + 1);
$C2->updLink($rs->link_id, $cur, false);
2021-09-07 14:00:49 +00:00
} catch (Exception $e) {
2021-09-07 13:21:38 +00:00
}
}
# Tirage al<61>atoire
2022-11-17 21:01:00 +00:00
if ($w->sortby == 'RANDOM'
2021-09-07 14:00:49 +00:00
|| $w->sortby == 'COUNTER'
2021-09-07 13:21:38 +00:00
) {
shuffle($entries);
2022-11-17 21:01:00 +00:00
if (dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_triggeronrandom) {
dcCore::app()->blog->triggerBlog();
2021-09-07 13:21:38 +00:00
}
}
2021-09-07 14:00:49 +00:00
return $w->renderDiv(
2021-11-02 22:55:41 +00:00
$w->content_only,
'cinecturlink2list ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . implode(' ', $entries) .
(
2022-11-17 21:01:00 +00:00
$w->showpagelink && dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_public_active ?
'<p><a href="' . dcCore::app()->blog->url . dcCore::app()->url->getBase('cinecturlink2') . '" title="' . __('view all links') . '">' . __('More links') . '</a></p>' : ''
2021-09-07 14:00:49 +00:00
)
);
2021-09-07 13:21:38 +00:00
}
public static function publicCats($w)
{
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->settings->addNamespace('cinecturlink2');
2021-09-07 13:21:38 +00:00
2022-11-17 21:01:00 +00:00
if (!dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_active
|| !dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_public_active
2022-12-08 21:53:43 +00:00
|| !$w->checkHomeOnly(dcCore::app()->url->type)
2021-09-07 13:21:38 +00:00
) {
return null;
}
2022-11-17 21:01:00 +00:00
$C2 = new cinecturlink2();
2021-09-07 14:00:49 +00:00
$rs = $C2->getCategories([]);
2021-09-07 13:21:38 +00:00
if ($rs->isEmpty()) {
return null;
}
2021-11-02 22:55:41 +00:00
$res = [];
$res[] = '<li><a href="' .
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->url . dcCore::app()->url->getBase('cinecturlink2') .
2021-09-07 14:00:49 +00:00
'" title="' . __('view all links') . '">' . __('all links') .
2021-11-02 22:55:41 +00:00
'</a>' . ($w->shownumlink ? ' (' . ($C2->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()) {
$res[] = '<li><a href="' .
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->url . dcCore::app()->url->getBase('cinecturlink2') . '/' .
dcCore::app()->blog->settings->cinecturlink2->cinecturlink2_public_caturl . '/' .
2021-09-07 14:00:49 +00:00
urlencode($rs->cat_title) .
2021-11-02 22:55:41 +00:00
'" title="' . __('view links of this category') . '">' .
2021-09-07 14:00:49 +00:00
html::escapeHTML($rs->cat_title) .
2021-11-02 22:55:41 +00:00
'</a>' . ($w->shownumlink ? ' (' .
2021-09-07 14:00:49 +00:00
($C2->getLinks(['cat_id' => $rs->cat_id], true)->f(0)) . ')' : '') .
'</li>';
2021-09-07 13:21:38 +00:00
}
2021-09-07 14:00:49 +00:00
return $w->renderDiv(
2021-11-02 22:55:41 +00:00
$w->content_only,
'cinecturlink2cat ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
'<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
}