kUtRL/src/Widgets.php

260 lines
8 KiB
PHP
Raw Permalink Normal View History

2021-08-25 23:06:22 +00:00
<?php
2023-10-19 18:32:57 +00:00
2023-08-21 14:55:06 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\kUtRL;
2021-08-25 23:06:22 +00:00
2023-10-19 18:32:57 +00:00
use Dotclear\App;
2023-08-21 14:55:06 +00:00
use Dotclear\Helper\Html\Form\{
Form,
Hidden,
Input,
Label,
Para,
Submit
};
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2021-08-25 23:06:22 +00:00
2023-10-19 18:32:57 +00:00
/**
* @brief kUtRL widgets.
* @ingroup kUtRL
*
* @author Jean-Christian Denis (author)
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-08-21 14:55:06 +00:00
class Widgets
2021-08-25 23:06:22 +00:00
{
2023-10-19 18:32:57 +00:00
public static function init(WidgetsStack $w): void
2021-08-28 18:39:58 +00:00
{
$w
->create(
'shortenkutrl',
2023-08-21 14:55:06 +00:00
My::name(),
2023-10-19 18:32:57 +00:00
self::parseShorten(...)
2021-08-28 18:39:58 +00:00
)
->addTitle(__('Shorten link'))
->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
$w
->create(
'rankkutrl',
__('Top of short links'),
2023-10-19 18:32:57 +00:00
self::parseRank(...)
2021-08-28 18:39:58 +00:00
)
->addTitle(__('Top of short links'))
->setting(
'text',
__('Text: (Use wildcard %rank%, %hash%, %url%, %count%, %counttext%)'),
'%rank% - %url% - %counttext%',
'text'
)
->setting(
'urllen',
__('URL length (if truncate)'),
20,
'text'
)
->setting(
'type',
__('Type:'),
'all',
2021-11-06 15:43:02 +00:00
'combo',
[
__('All') => '-',
__('Mini URL') => 'localnormal',
__('Custom URL') => 'localcustom',
2022-11-20 16:15:36 +00:00
__('Semi-custom') => 'localmix',
2021-08-28 18:39:58 +00:00
]
)
->setting(
'mixprefix',
__('Semi-custom prefix: (only if you want limit to a particular prefix)'),
'',
'text'
)
->setting(
'sortby',
__('Sort by:'),
'kut_counter',
'combo',
[
__('Date') => 'kut_dt',
__('Rank') => 'kut_counter',
2022-11-20 16:15:36 +00:00
__('Hash') => 'kut_hash',
2021-08-28 18:39:58 +00:00
]
)
->setting(
'sort',
__('Sort:'),
'desc',
'combo',
[
2021-11-06 15:43:02 +00:00
__('Ascending') => 'asc',
2022-11-20 16:15:36 +00:00
__('Descending') => 'desc',
2021-08-28 18:39:58 +00:00
]
)
->setting(
'limit',
__('Limit:'),
'10',
'text'
)
->setting(
'hideempty',
__('Hide no followed links'),
0,
'check'
)
->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
}
2023-08-21 14:55:06 +00:00
public static function parseShorten(WidgetsElement $w): string
2021-08-28 18:39:58 +00:00
{
2023-08-21 14:55:06 +00:00
$s = My::settings();
2022-12-22 20:18:49 +00:00
if (!$s->get('active')
|| !$s->get('srv_local_public')
2023-10-19 18:32:57 +00:00
|| !$w->checkHomeOnly(App::url()->type)
|| App::url()->type == 'kutrl') {
2023-08-21 14:55:06 +00:00
return '';
2021-08-28 18:39:58 +00:00
}
2023-08-21 14:55:06 +00:00
$hmf = FrontendUtils::create();
$hmfp = FrontendUtils::protect($hmf);
2021-08-28 18:39:58 +00:00
return $w->renderDiv(
2023-08-21 14:55:06 +00:00
(bool) $w->content_only,
2021-08-28 18:39:58 +00:00
'shortenkutrlwidget ' . $w->class,
'',
2023-08-21 14:55:06 +00:00
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') .
(new Form(['shortenkutrlwidget']))
->method('post')
2023-10-19 18:32:57 +00:00
->action(App::blog()->url() . App::url()->getBase('kutrl'))
2023-08-21 14:55:06 +00:00
->fields([
(new Para())
->items([
(new Label(__('Long link:'), Label::OUTSIDE_LABEL_BEFORE))
->for('longurl'),
(new Input('longurl'))
->size(20)
->maxlenght(255)
->value(''),
]),
(new Para())
->items([
(new Label(sprintf(__('Rewrite "%s" in next field to show that you are not a robot:'), $hmf), Label::OUTSIDE_LABEL_BEFORE))
->for('hmf'),
(new Input('hmf'))
->size(20)
->maxlenght(255)
->value(''),
]),
(new Para())
->items([
(new Submit('submiturl'))
->value(__('Shorten')),
(new Hidden('hmfp', $hmfp)),
2023-10-19 18:32:57 +00:00
App::nonce()->formNonce(),
2023-08-21 14:55:06 +00:00
]),
])
->render()
2021-08-28 18:39:58 +00:00
);
}
2023-08-21 14:55:06 +00:00
public static function parseRank(WidgetsElement $w): string
2021-08-28 18:39:58 +00:00
{
2023-08-21 14:55:06 +00:00
$s = My::settings();
2021-08-28 18:39:58 +00:00
2023-10-19 18:32:57 +00:00
if (!$s->get('active') || !$w->checkHomeOnly(App::url()->type)) {
2023-08-21 14:55:06 +00:00
return '';
2021-08-28 18:39:58 +00:00
}
$type = in_array($w->type, ['localnormal', 'localmix', 'localcustom']) ?
"AND kut_type ='" . $w->type . "' " :
2023-10-19 18:32:57 +00:00
'AND kut_type ' . App::con()->in(['localnormal', 'localmix', 'localcustom']) . ' ';
2021-08-28 18:39:58 +00:00
2021-11-06 15:43:02 +00:00
$hide = (bool) $w->hideempty ? 'AND kut_counter > 0 ' : '';
2021-08-28 18:39:58 +00:00
$more = '';
if ($w->type == 'localmix' && '' != $w->mixprefix) {
2023-10-19 18:32:57 +00:00
$more = "AND kut_hash LIKE '" . App::con()->escapeStr((string) $w->mixprefix) . "%' ";
2021-08-28 18:39:58 +00:00
}
2021-11-06 15:43:02 +00:00
$order = ($w->sortby && in_array($w->sortby, ['kut_dt', 'kut_counter', 'kut_hash'])) ?
2021-08-28 18:39:58 +00:00
$w->sortby : 'kut_dt';
$order .= $w->sort == 'desc' ? ' DESC' : ' ASC';
2023-10-19 18:32:57 +00:00
$limit = App::con()->limit(abs((int) $w->limit));
2023-10-19 18:32:57 +00:00
$rs = App::con()->select(
2021-08-28 18:39:58 +00:00
'SELECT kut_counter, kut_hash ' .
2023-10-19 18:32:57 +00:00
'FROM ' . App::con()->prefix() . My::TABLE_NAME . ' ' .
"WHERE blog_id='" . App::con()->escapeStr(App::blog()->id()) . "' " .
2021-08-28 18:39:58 +00:00
"AND kut_service = 'local' " .
$type . $hide . $more . 'ORDER BY ' . $order . $limit
);
if ($rs->isEmpty()) {
2023-08-21 14:55:06 +00:00
return '';
2021-08-28 18:39:58 +00:00
}
$content = '';
2021-11-06 15:43:02 +00:00
$i = 0;
while ($rs->fetch()) {
2021-08-28 18:39:58 +00:00
$i++;
$rank = '<span class="rankkutrl-rank">' . $i . '</span>';
2021-11-06 15:43:02 +00:00
$hash = $rs->kut_hash;
2023-10-19 18:32:57 +00:00
$url = App::blog()->url() . App::url()->getBase('kutrl') . '/' . $hash;
2021-11-06 15:43:02 +00:00
$cut_len = - abs((int) $w->urllen);
2021-08-28 18:39:58 +00:00
if (strlen($url) > $cut_len) {
$url = '...' . substr($url, $cut_len);
}
2021-11-06 15:43:02 +00:00
/*
if (strlen($hash) > $cut_len) {
$url = '...'.substr($hash, $cut_len);
}
//*/
2021-08-28 18:39:58 +00:00
if ($rs->kut_counter == 0) {
$counttext = __('never followed');
} elseif ($rs->kut_counter == 1) {
$counttext = __('followed one time');
} else {
$counttext = sprintf(__('followed %s times'), $rs->kut_counter);
}
2021-11-06 15:43:02 +00:00
$content .= '<li><a href="' .
2023-10-19 18:32:57 +00:00
App::blog()->url() . App::url()->getBase('kutrl') . '/' . $rs->kut_hash .
2021-08-28 18:39:58 +00:00
'">' .
str_replace(
['%rank%', '%hash%', '%url%', '%count%', '%counttext%'],
[$rank, $hash, $url, $rs->kut_counter, $counttext],
$w->text
) .
'</a></li>';
}
2021-08-28 18:39:58 +00:00
if (empty($content)) {
2023-08-21 14:55:06 +00:00
return '';
2021-08-28 18:39:58 +00:00
}
return $w->renderDiv(
2023-08-21 14:55:06 +00:00
(bool) $w->content_only,
2021-08-28 18:39:58 +00:00
'lastblogupdate ' . $w->class,
'',
2023-08-21 14:55:06 +00:00
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') .
2021-08-28 18:39:58 +00:00
sprintf('<ul>%s</ul>', $content)
2021-11-06 15:43:02 +00:00
);
2021-08-28 18:39:58 +00:00
}
2021-11-06 15:43:02 +00:00
}