Fix and update widgets

This commit is contained in:
Jean-Christian Paul Denis 2021-09-07 16:00:49 +02:00
parent b21e286961
commit 02d56c5c96

View File

@ -12,17 +12,16 @@
*/ */
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
return null; return null;
} }
$core->addBehavior( $core->addBehavior(
'initWidgets', 'initWidgets',
array('cinecturlink2Widget', 'adminLinks') ['cinecturlink2Widget', 'adminLinks']
); );
$core->addBehavior( $core->addBehavior(
'initWidgets', 'initWidgets',
array('cinecturlink2Widget', 'adminCats') ['cinecturlink2Widget', 'adminCats']
); );
class cinecturlink2Widget class cinecturlink2Widget
@ -33,140 +32,128 @@ class cinecturlink2Widget
$C2 = new cinecturlink2($core); $C2 = new cinecturlink2($core);
$categories_combo = array('' => '', __('Uncategorized') => 'null'); $categories_combo = ['' => '', __('Uncategorized') => 'null'];
$categories = $C2->getCategories(); $categories = $C2->getCategories();
while($categories->fetch()) { while($categories->fetch()) {
$cat_title = html::escapeHTML($categories->cat_title); $cat_title = html::escapeHTML($categories->cat_title);
$categories_combo[$cat_title] = $categories->cat_id; $categories_combo[$cat_title] = $categories->cat_id;
} }
$sortby_combo = array( $sortby_combo = [
__('Update date') => 'link_upddt', __('Update date') => 'link_upddt',
__('My rating') => 'link_note', __('My rating') => 'link_note',
__('Title') => 'link_title', __('Title') => 'link_title',
__('Random') => 'RANDOM', __('Random') => 'RANDOM',
__('Number of views') => 'COUNTER' __('Number of views') => 'COUNTER'
); ];
$order_combo = array( $order_combo = [
__('Ascending') => 'asc', __('Ascending') => 'asc',
__('Descending') => 'desc' __('Descending') => 'desc'
); ];
$w->create( $w
->create(
'cinecturlink2links', 'cinecturlink2links',
__('My cinecturlink'), __('My cinecturlink'),
array('cinecturlink2Widget', 'publicLinks'), ['cinecturlink2Widget', 'publicLinks'],
'null', null,
__('Show selection of cinecturlinks') __('Show selection of cinecturlinks')
); )
$w->cinecturlink2links->setting( ->addTitle(
'title',
__('Title:'),
__('My cinecturlink'), __('My cinecturlink'),
'text' )
); ->setting(
$w->cinecturlink2links->setting(
'category', 'category',
__('Category:'), __('Category:'),
'', '',
'combo', 'combo',
$categories_combo $categories_combo
); )
$w->cinecturlink2links->setting( ->setting(
'sortby', 'sortby',
__('Order by:'), __('Order by:'),
'link_upddt', 'link_upddt',
'combo', 'combo',
$sortby_combo $sortby_combo
); )
$w->cinecturlink2links->setting( ->setting(
'sort', 'sort',
__('Sort: (only for date, note and title)'), __('Sort: (only for date, note and title)'),
'desc', 'desc',
'combo', 'combo',
$order_combo $order_combo
); )
$w->cinecturlink2links->setting( ->setting(
'limit', 'limit',
__('Limit:'), __('Limit:'),
10, 10,
'text' 'text'
); )
$w->cinecturlink2links->setting( ->setting(
'withlink', 'withlink',
__('Enable link'), __('Enable link'),
1, 1,
'check' 'check'
); )
$w->cinecturlink2links->setting( ->setting(
'showauthor', 'showauthor',
__('Show author'), __('Show author'),
1, 1,
'check' 'check'
); )
$w->cinecturlink2links->setting( ->setting(
'shownote', 'shownote',
__('Show my rating'), __('Show my rating'),
0, 0,
'check' 'check'
); )
$w->cinecturlink2links->setting( ->setting(
'showdesc', 'showdesc',
__('Show description'), __('Show description'),
0, 0,
'check' 'check'
); )
$w->cinecturlink2links->setting( ->setting(
'showpagelink', 'showpagelink',
__('Show a link to cinecturlink page'), __('Show a link to cinecturlink page'),
0, 0,
'check' 'check'
);
$w->cinecturlink2links->setting(
'homeonly',
__('Display on:'),
1,
'combo',
array(
__('All pages') => 0,
__('Home page only') => 1,
__('Except on home page') => 2
) )
); ->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
} }
public static function adminCats($w) public static function adminCats($w)
{ {
$w->create( $w
->create(
'cinecturlink2cats', 'cinecturlink2cats',
__('List of categories of cinecturlink'), __('List of categories of cinecturlink'),
array('cinecturlink2Widget', 'publicCats'), ['cinecturlink2Widget', 'publicCats'],
null, null,
__('List of categories of cinecturlink') __('List of categories of cinecturlink')
); )
$w->cinecturlink2cats->setting( ->addTitle(
__('My cinecturlink by categories')
)
->setting(
'title', 'title',
__('Title:'), __('Title:'),
__('My cinecturlink by categories'), __('My cinecturlink by categories'),
'text' 'text'
); )
$w->cinecturlink2cats->setting( ->setting(
'shownumlink', 'shownumlink',
__('Show number of links'), __('Show number of links'),
0, 0,
'check' 'check'
);
$w->cinecturlink2cats->setting(
'homeonly',
__('Display on:'),
1,
'combo',
array(
__('All pages') => 0,
__('Home page only') => 1,
__('Except on home page') => 2
) )
); ->addHomeOnly()
->addContentOnly()
->addClass()
->addOffline();
} }
public static function publicLinks($w) public static function publicLinks($w)
@ -176,8 +163,8 @@ class cinecturlink2Widget
$core->blog->settings->addNamespace('cinecturlink2'); $core->blog->settings->addNamespace('cinecturlink2');
if (!$core->blog->settings->cinecturlink2->cinecturlink2_active if (!$core->blog->settings->cinecturlink2->cinecturlink2_active
|| $w->homeonly == 1 && $core->url->type != 'default' || $w->homeonly == 1 && !$core->url->isHome($core->url->type)
|| $w->homeonly == 2 && $core->url->type == 'default' || $w->homeonly == 2 && $core->url->isHome($core->url->type)
) { ) {
return null; return null;
} }
@ -195,33 +182,29 @@ class cinecturlink2Widget
$limit = abs((integer) $w->limit); $limit = abs((integer) $w->limit);
# Tirage aléatoire // Tirage aléatoire: Consomme beaucoup de ressources!
# Consomme beaucoup de ressources!
if ($w->sortby == 'RANDOM') { if ($w->sortby == 'RANDOM') {
$big_rs = $C2->getLinks($params); $big_rs = $C2->getLinks($params);
if ($big_rs->isEmpty()) { if ($big_rs->isEmpty()) {
return null; return null;
} }
$ids= array(); $ids= [];
while($big_rs->fetch()) { while($big_rs->fetch()) {
$ids[] = $big_rs->link_id; $ids[] = $big_rs->link_id;
} }
shuffle($ids); shuffle($ids);
$ids = array_slice($ids, 0, $limit); $ids = array_slice($ids, 0, $limit);
$params['link_id'] = array(); $params['link_id'] = [];
foreach($ids as $id) { foreach($ids as $id) {
$params['link_id'][] = $id; $params['link_id'][] = $id;
} }
} } elseif ($w->sortby == 'COUNTER') {
elseif ($w->sortby == 'COUNTER') {
$params['order'] = 'link_count asc'; $params['order'] = 'link_count asc';
$params['limit'] = $limit; $params['limit'] = $limit;
} } else {
else {
$params['order'] = $w->sortby; $params['order'] = $w->sortby;
$params['order'] .= $w->sort == 'asc' ? ' asc' : ' desc'; $params['order'] .= $w->sort == 'asc' ? ' asc' : ' desc';
$params['limit'] = $limit; $params['limit'] = $limit;
@ -230,44 +213,42 @@ class cinecturlink2Widget
$rs = $C2->getLinks($params); $rs = $C2->getLinks($params);
if ($rs->isEmpty()) { if ($rs->isEmpty()) {
return null; return null;
} }
$widthmax = (integer) $core->blog->settings->cinecturlink2->cinecturlink2_widthmax; $widthmax = (integer) $core->blog->settings->cinecturlink2->cinecturlink2_widthmax;
$style = $widthmax ? ' style="width:'.$widthmax.'px;"' : ''; $style = $widthmax ? ' style="width:' . $widthmax . 'px;"' : '';
$entries = array(); $entries = [];
while($rs->fetch()) { while($rs->fetch()) {
$url = $rs->link_url; $url = $rs->link_url;
$img = $rs->link_img; $img = $rs->link_img;
$title = html::escapeHTML($rs->link_title); $title = html::escapeHTML($rs->link_title);
$author = html::escapeHTML($rs->link_author); $author = html::escapeHTML($rs->link_author);
$cat = html::escapeHTML($rs->cat_title); $cat = html::escapeHTML($rs->cat_title);
$note = $w->shownote ? ' <em>('.$rs->link_note.'/20)</em>' : ''; $note = $w->shownote ? ' <em>(' . $rs->link_note . '/20)</em>' : '';
$desc = $w->showdesc ? '<br /><em>'.html::escapeHTML($rs->link_desc).'</em>' : ''; $desc = $w->showdesc ? '<br /><em>' . html::escapeHTML($rs->link_desc) . '</em>' : '';
$lang = $rs->link_lang ? ' hreflang="'.$rs->link_lang.'"' : ''; $lang = $rs->link_lang ? ' hreflang="' . $rs->link_lang . '"' : '';
$count = abs((integer) $rs->link_count); $count = abs((integer) $rs->link_count);
# --BEHAVIOR-- cinecturlink2WidgetLinks # --BEHAVIOR-- cinecturlink2WidgetLinks
$bhv = $core->callBehavior('cinecturlink2WidgetLinks', $rs->link_id); $bhv = $core->callBehavior('cinecturlink2WidgetLinks', $rs->link_id);
$entries[] = $entries[] =
'<p style="text-align:center;">'. '<p style="text-align:center;">' .
($w->withlink && !empty($url) ? '<a href="'.$url.'"'.$lang.' title="'.$cat.'">' : ''). ($w->withlink && !empty($url) ? '<a href="' . $url . '"' . $lang . ' title="' . $cat . '">' : '') .
'<strong>'.$title.'</strong>'.$note.'<br />'. '<strong>' . $title . '</strong>' . $note . '<br />' .
($w->showauthor ? $author.'<br />' : '').'<br />'. ($w->showauthor ? $author . '<br />' : '') . '<br />' .
'<img src="'.$img.'" alt="'.$title.' - '.$author.'"'.$style.' />'. '<img src="' . $img . '" alt="' . $title . ' - ' . $author . '"' . $style . ' />' .
$desc. $desc .
($w->withlink && !empty($url) ? '</a>' : ''). ($w->withlink && !empty($url) ? '</a>' : '') .
'</p>'.$bhv; '</p>' . $bhv;
try { try {
$cur = $core->con->openCursor($C2->table); $cur = $core->con->openCursor($C2->table);
$cur->link_count = ($count + 1); $cur->link_count = ($count + 1);
$C2->updLink($rs->link_id, $cur, false); $C2->updLink($rs->link_id, $cur, false);
} } catch (Exception $e) {
catch (Exception $e) {
} }
} }
@ -281,14 +262,15 @@ class cinecturlink2Widget
} }
} }
return return $w->renderDiv(
'<div class="cinecturlink2list">'. $w->content_only,
($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). 'cinecturlink2list '. $w->class,
implode(' ',$entries). '',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . implode(' ',$entries) .
($w->showpagelink && $core->blog->settings->cinecturlink2->cinecturlink2_public_active ? ($w->showpagelink && $core->blog->settings->cinecturlink2->cinecturlink2_public_active ?
'<p><a href="'.$core->blog->url.$core->url->getBase('cinecturlink2').'" title="'.__('view all links').'">'.__('More links').'</a></p>' : '' '<p><a href="' . $core->blog->url . $core->url->getBase('cinecturlink2') . '" title="' . __('view all links') . '">' . __('More links') . '</a></p>' : ''
). )
'</div>'; );
} }
public static function publicCats($w) public static function publicCats($w)
@ -299,48 +281,45 @@ class cinecturlink2Widget
if (!$core->blog->settings->cinecturlink2->cinecturlink2_active if (!$core->blog->settings->cinecturlink2->cinecturlink2_active
|| !$core->blog->settings->cinecturlink2->cinecturlink2_public_active || !$core->blog->settings->cinecturlink2->cinecturlink2_public_active
|| $w->homeonly == 1 && $core->url->type != 'default' || $w->homeonly == 1 && !$core->url->isHome($core->url->type)
|| $w->homeonly == 2 && $core->url->type == 'default' || $w->homeonly == 2 && $core->url->isHome($core->url->type)
) { ) {
return null; return null;
} }
$C2 = new cinecturlink2($core); $C2 = new cinecturlink2($core);
$rs = $C2->getCategories([]);
$rs = $C2->getCategories(array());
if ($rs->isEmpty()) { if ($rs->isEmpty()) {
return null; return null;
} }
$res = $res = [];
'<li><a href="'. $res[] =
$core->blog->url.$core->url->getBase('cinecturlink2'). '<li><a href="' .
'" title="'.__('view all links').'">'.__('all links'). $core->blog->url . $core->url->getBase('cinecturlink2') .
'</a>'; '" title="' . __('view all links') . '">' . __('all links') .
if ($w->shownumlink) { '</a>'. ($w->shownumlink ? ' ('. ($C2->getLinks([], true)->f(0)) . ')' : '') .
$res .= ' ('.($C2->getLinks(array(), true)->f(0)).')'; '</li>';
}
$res .= '</li>';
while($rs->fetch()) { while($rs->fetch()) {
$res .= $res[] =
'<li><a href="'. '<li><a href="' .
$core->blog->url.$core->url->getBase('cinecturlink2').'/'.$core->blog->settings->cinecturlink2->cinecturlink2_public_caturl.'/'.urlencode($rs->cat_title). $core->blog->url . $core->url->getBase('cinecturlink2') . '/' .
'" title="'.__('view links of this category').'">'. $core->blog->settings->cinecturlink2->cinecturlink2_public_caturl . '/' .
html::escapeHTML($rs->cat_title). urlencode($rs->cat_title) .
'</a>'; '" title="'.__('view links of this category') . '">' .
if ($w->shownumlink) { html::escapeHTML($rs->cat_title) .
$res .= ' ('.($C2->getLinks(array('cat_id'=>$rs->cat_id), true)->f(0)).')'; '</a>'. ($w->shownumlink ? ' (' .
} ($C2->getLinks(['cat_id' => $rs->cat_id], true)->f(0)) . ')' : '') .
$res .= '</li>'; '</li>';
} }
return return $w->renderDiv(
'<div class="cinecturlink2cat">'. $w->content_only,
($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : ''). 'cinecturlink2cat '. $w->class,
'<ul>'.$res.'</ul>'. '',
'</div>'; ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
'<ul>' . implode(' ',$res) . '</ul>'
);
} }
} }