add periods list columns to user pref

master
Jean-Christian Paul Denis 2021-10-23 14:44:54 +02:00
parent 7888922dce
commit 779b46aca8
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 156 additions and 127 deletions

View File

@ -29,6 +29,10 @@ $core->addBehavior(
'adminFiltersLists',
['adminPeriodical', 'adminFiltersLists']
);
$core->addBehavior(
'adminColumnsLists',
['adminPeriodical', 'adminColumnsLists']
);
if ($core->blog->settings->periodical->periodical_active) {
@ -139,6 +143,20 @@ class adminPeriodical
$blog_settings->periodical->put('periodical_updurl', !empty($_POST['periodical_updurl']));
}
public static function adminColumnsLists(dcCore $core, $cols)
{
$cols['periodical'] = [
__('Periodical'),
[
'curdt' => [true, __('Next update')],
'pub_int' => [true, __('Frequency')],
'pub_nb' => [true, __('Pub per update')],
'nbposts' => [true, __('Entries')],
'enddt' => [true, __('End date')]
]
];
}
public static function adminFiltersLists(dcCore $core, $sorts)
{
$sorts['periodical'] = [

View File

@ -22,91 +22,111 @@ if (!defined('DC_CONTEXT_ADMIN')) {
*/
class adminPeriodicalList extends adminGenericList
{
public function periodDisplay($page, $nb_per_page, $enclose_block='')
public function periodDisplay($filter, $enclose_block='')
{
$echo = '';
if ($this->rs->isEmpty()) {
$echo .= '<p><strong>' .__('No period') .'</strong></p>';
if ($filter->show()) {
echo '<p><strong>' . __('No period matches the filter') . '</strong></p>';
} else {
echo '<p><strong>' . __('No period') . '</strong></p>';
}
} else {
$pager = new dcPager($page, $this->rs_count, $nb_per_page, 10);
$pager->html_prev = $this->html_prev;
$pager->html_next = $this->html_next;
$pager = new dcPager($filter->page, $this->rs_count, $filter->nb, 10);
$pager->var_page = 'page';
$html_block =
'<div class="table-outer">' .
'<table class="clear">' .
'<tr>' .
'<th colspan="2" class="nowrap">' . __('Name') . '</th>' .
'<th class="nowrap">' . __('Next update') . '</th>' .
'<th class="nowrap">' . __('Frequency') . '</th>' .
'<th class="nowrap">' . __('Publications') . '</th>' .
'<th class="nowrap">' . __('Entries') . '</th>' .
'<th class="nowrap">' . __('End date') . '</th>' .
'</tr>%s</table>' .
'</div>';
$periods = [];
if (isset($_REQUEST['periods'])) {
foreach ($_REQUEST['periods'] as $v) {
$periods[(integer) $v] = true;
}
}
$html_block = '<div class="table-outer"><table><caption>' . ($filter->show() ?
sprintf(__('List of %s periods matching the filter.'), $this->rs_count) :
sprintf(__('List of %s periods.'), $this->rs_count)
). '</caption>';
$cols = new ArrayObject([
'name' => '<th colspan="2" class="first">' . __('Name') . '</th>',
'curdt' => '<th scope="col" class="nowrap">' . __('Next update') . '</th>',
'pub_int' => '<th scope="col" class="nowrap">' . __('Frequency') . '</th>',
'pub_nb' => '<th scope="col" class="nowrap">' . __('Pub per update') . '</th>',
'nbposts' => '<th scope="col" class="nowrap">' . __('Entries') . '</th>',
'enddt' => '<th scope="col" class="nowrap">' . __('End date') . '</th>'
]);
$this->userColumns('periodical', $cols);
$html_block .= '<tr>' . implode(iterator_to_array($cols)) . '</tr>%s</table>%s</div>';
if ($enclose_block) {
$html_block = sprintf($enclose_block, $html_block);
}
$echo .= $pager->getLinks();
$blocks = explode('%s', $html_block);
$echo .= $blocks[0];
echo $pager->getLinks() . $blocks[0];
while ($this->rs->fetch()) {
$echo .= $this->periodLine();
echo $this->periodLine(isset($periods[$this->rs->periodical_id]));
}
$echo .= $blocks[1];
$echo .= $pager->getLinks();
echo $blocks[1] . $blocks[2] . $pager->getLinks();
}
return $echo;
}
private function periodLine()
private function periodLine($checked)
{
$nb_posts = $this->rs->periodical->getPosts(['periodical_id' => $this->rs->periodical_id], true);
$nb_posts = $nb_posts->f(0);
$style = !$nb_posts ? ' offline' : '';
$posts_links = !$nb_posts ?
'0' :
'<a href="plugin.php?p=periodical&amp;part=period&amp;period_id=' . $this->rs->periodical_id . '#posts" title="' . __('view related entries') . '">' . $nb_posts . '</a>';
$nb_posts = $this->rs->periodical->getPosts(['periodical_id' => $this->rs->periodical_id], true)->f(0);
$url = $this->core->adminurl->get('admin.plugin.periodical', ['part' => 'period', 'period_id' => $this->rs->periodical_id]);
$pub_int = in_array($this->rs->periodical_pub_int, $this->rs->periodical->getTimesCombo()) ?
$name = '<a href="' . $url . '#period" title="' . __('edit period') . '">' . html::escapeHTML($this->rs->periodical_title) . '</a>';
$posts = $nb_posts ?
'<a href="' . $url . '#posts" title="' . __('view related entries') . '">' . $nb_posts . '</a>' :
'0';
$interval = in_array($this->rs->periodical_pub_int, $this->rs->periodical->getTimesCombo()) ?
__(array_search($this->rs->periodical_pub_int, $this->rs->periodical->getTimesCombo())) : __('Unknow frequence');
$res =
'<tr class="line' . $style . '">' .
'<td class="nowrap">' . form::checkbox(['periods[]'], $this->rs->periodical_id) . '</td>' .
'<td class="maximal"><a href="plugin.php?p=periodical&amp;part=period&amp;period_id=' . $this->rs->periodical_id . '#period" title="' .
__('edit period') . '">' . html::escapeHTML($this->rs->periodical_title) . '</a></td>' .
'<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_curdt) . '</td>' .
'<td class="nowrap">' . $pub_int . '</td>' .
'<td class="nowrap">' . $this->rs->periodical_pub_nb .'</td>' .
'<td class="nowrap">' . $posts_links . '</td>' .
'<td class="nowrap">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_enddt) . '</td>' .
'</tr>';
$cols = new ArrayObject([
'check' => '<td class="nowrap">' . form::checkbox(['periods[]'], $this->rs->periodical_id, ['checked' => $checked]) . '</td>',
'name' => '<td class="maximal">' . $name . '</td>',
'curdt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_curdt) . '</td>',
'pub_int' => '<td class="nowrap">' . $interval . '</td>',
'pub_nb' => '<td class="nowrap count">' . $this->rs->periodical_pub_nb . '</td>',
'nbposts' => '<td class="nowrap count">' . $posts. '</td>',
'enddt' => '<td class="nowrap count">' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->periodical_enddt) . '</td>'
]);
return $res;
$this->userColumns('periodical', $cols);
return
'<tr class="line ' . ($nb_posts ? '' : ' offline') . '" id="p' . $this->rs->periodical_id . '">' .
implode(iterator_to_array($cols)) .
'</tr>';
}
public function postDisplay($page, $nb_per_page, $base_url, $enclose_block='')
public function postDisplay($filter, $base_url, $enclose_block='')
{
$echo = '';
if ($this->rs->isEmpty()) {
$echo .= '<p><strong>' . __('No entry') . '</strong></p>';
if ($filter->show()) {
echo '<p><strong>' . __('No entry matches the filter') . '</strong></p>';
} else {
echo '<p><strong>' . __('No entry') . '</strong></p>';
}
} else {
$pager = new dcPager($page, $this->rs_count, $nb_per_page, 10);
$pager->html_prev = $this->html_prev;
$pager->html_next = $this->html_next;
$pager = new dcPager($filter->page, $this->rs_count, $filter->nb, 10);
$pager->base_url = $base_url;
$pager->var_page = 'page';
$periodical_entries = [];
if (isset($_REQUEST['periodical_entries'])) {
foreach ($_REQUEST['periodical_entries'] as $v) {
$periodical_entries[(integer) $v] = true;
}
}
$html_block =
'<table class="clear"><tr>' .
'<th colspan="2">' . __('Title') . '</th>' .
@ -128,7 +148,7 @@ class adminPeriodicalList extends adminGenericList
$echo .= $blocks[0];
while ($this->rs->fetch()) {
$echo .= $this->postLine();
$echo .= $this->postLine(isset($periodical_entries[$this->rs->post_id]));
}
$echo .= $blocks[1];
@ -139,7 +159,7 @@ class adminPeriodicalList extends adminGenericList
return $echo;
}
private function postLine()
private function postLine($checked)
{
if ($this->core->auth->check('categories', $this->core->blog->id)) {
$cat_link = '<a href="category.php?id=%s">%s</a>';

137
index.php
View File

@ -18,12 +18,10 @@ if (!defined('DC_CONTEXT_ADMIN')) {
dcPage::check('usage,contentadmin');
# Objects
$s = $core->blog->settings->periodical;
$per = new periodical($core);
# Default values
$action = isset($_POST['action']) ? $_POST['action'] : '';
$part = isset($_REQUEST['part']) && $_REQUEST['part'] == 'period' ? 'period' : 'periods';
############################################################
#
@ -31,17 +29,17 @@ $part = isset($_REQUEST['part']) && $_REQUEST['part'] == 'period' ? 'period' : '
#
############################################################
if ($part == 'period') {
if ($_REQUEST['part'] == 'period') {
$starting_script = '';
# Default value for period
$period_id = null;
$period_title = __('One post per day');
$period_pub_nb = 1;
$period_id = null;
$period_title = __('One post per day');
$period_pub_nb = 1;
$period_pub_int = 'day';
$period_curdt = date('Y-m-d H:i:00', time());
$period_enddt = date('Y-m-d H:i:00', time() + 31536000); //one year
$period_curdt = date('Y-m-d H:i:00', time());
$period_enddt = date('Y-m-d H:i:00', time() + 31536000); //one year
# Get period
if (!empty($_REQUEST['period_id'])) {
@ -52,20 +50,17 @@ if ($part == 'period') {
$core->error->add(__('This period does not exist.'));
$period_id = null;
} else {
$period_id = $rs->periodical_id;
$period_title = $rs->periodical_title;
$period_pub_nb = $rs->periodical_pub_nb;
$period_id = $rs->periodical_id;
$period_title = $rs->periodical_title;
$period_pub_nb = $rs->periodical_pub_nb;
$period_pub_int = $rs->periodical_pub_int;
$period_curdt = date('Y-m-d H:i', strtotime($rs->periodical_curdt));
$period_enddt = date('Y-m-d H:i', strtotime($rs->periodical_enddt));
//todo load related posts
$period_curdt = date('Y-m-d H:i', strtotime($rs->periodical_curdt));
$period_enddt = date('Y-m-d H:i', strtotime($rs->periodical_enddt));
}
}
# Set period
if ($action == 'setperiod') {
# Get POST values
if (!empty($_POST['period_title'])) {
$period_title = $_POST['period_title'];
@ -74,7 +69,8 @@ if ($part == 'period') {
$period_pub_nb = abs((integer) $_POST['period_pub_nb']);
}
if (!empty($_POST['period_pub_int'])
&& in_array($_POST['period_pub_int'], $per->getTimesCombo())) {
&& in_array($_POST['period_pub_int'], $per->getTimesCombo())
) {
$period_pub_int = $_POST['period_pub_int'];
}
if (!empty($_POST['period_curdt'])) {
@ -104,17 +100,15 @@ if ($part == 'period') {
# If no error, set period
if (!$core->error->flag()) {
$cur = $per->openCursor();
$cur->periodical_title = $period_title;
$cur->periodical_curdt = $period_curdt;
$cur->periodical_enddt = $period_enddt;
$cur->periodical_title = $period_title;
$cur->periodical_curdt = $period_curdt;
$cur->periodical_enddt = $period_enddt;
$cur->periodical_pub_int = $period_pub_int;
$cur->periodical_pub_nb = $period_pub_nb;
$cur->periodical_pub_nb = $period_pub_nb;
# Update period
if ($period_id) {
$per->updPeriod($period_id, $cur);
dcPage::addSuccessNotice(
@ -122,7 +116,6 @@ if ($part == 'period') {
);
# Create period
} else {
$period_id = $per->addPeriod($cur);
dcPage::addSuccessNotice(
@ -130,16 +123,16 @@ if ($part == 'period') {
);
}
http::redirect(empty($_POST['redir']) ?
$p_url . '&part=period&period_id=' . $period_id . '#period' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#period');
}
}
}
# Actions on related posts
if (!$core->error->flag() && $period_id && $action && !empty($_POST['periodical_entries'])) {
# Publish posts
if ($action == 'publish') {
try {
@ -153,10 +146,11 @@ if ($part == 'period') {
__('Entries successfully published.')
);
http::redirect(empty($_POST['redir']) ?
$p_url . '&part=period&period_id=' . $period_id . '#posts' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@ -175,10 +169,11 @@ if ($part == 'period') {
__('Entries successfully unpublished.')
);
http::redirect(empty($_POST['redir']) ?
$p_url . '&part=period&period_id=' . $period_id . '#posts' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@ -196,10 +191,11 @@ if ($part == 'period') {
__('Entries successfully removed.')
);
http::redirect(empty($_POST['redir']) ?
$p_url . '&part=period&period_id=' . $period_id . '#posts' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'period', 'period_id' => $period_id], '#posts');
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@ -213,7 +209,8 @@ if ($part == 'period') {
$post_filter->add('part', 'period');
$params = $post_filter->params();
$params['no_content'] = true;
$params['periodical_id'] = $period_id;
$params['no_content'] = true;
# Get posts
try {
@ -249,9 +246,7 @@ if ($part == 'period') {
# Period form
echo '
<div class="multi-part" title="' .
(null === $period_id ? __('New period') : __('Edit period')) .
'" id="period">
<div id="period"><h3>' . (null === $period_id ? __('New period') : __('Edit period')) . '</h3>
<form method="post" action="' . $p_url . '">
<p><label for="period_title">' . __('Title:') . '</label>' .
@ -311,22 +306,18 @@ if ($part == 'period') {
'#posts';
echo '
<div class="multi-part" title="' .
__('Entries linked to this period') .
'" id="posts">';
<div id="posts"><h3>' . __('Entries linked to this period') . '</h3>';
# Filters
$post_filter->display(['admin.plugin.periodical','#posts'],
$core->adminurl->getHiddenFormFields('admin.plugin.zoneclearFeedServer', [
'p' => 'periodical',
'part' => 'period',
'period_id' => $period_id
$post_filter->display(['admin.plugin.periodical', '#posts'],
$core->adminurl->getHiddenFormFields('admin.plugin.periodical', [
'period_id' => $period_id,
'part' => 'period'
])
);
# Posts list
echo
$post_list->postDisplay($post_filter->page, $post_filter->nb, $base_url,
echo $post_list->postDisplay($post_filter, $base_url,
'<form action="' . $p_url . '" method="post" id="form-entries">' .
'%s' .
@ -337,10 +328,10 @@ if ($part == 'period') {
'<p class="col right">' . __('Selected entries action:') . ' ' .
form::combo('action', $combo_action) .
'<input type="submit" value="' . __('ok') . '" /></p>' .
$core->adminurl->getHiddenFormFields('admin.plugin.periodical', $post_filter->values()) .
form::hidden(['period_id'], $period_id) .
form::hidden(['p'], 'periodical') .
form::hidden(['redir'], sprintf($base_url, $post_filter->page)) .
$core->adminurl->getHiddenFormFields('admin.plugin.periodical', array_merge($post_filter->values(), [
'period_id' => $period_id,
'redir' => sprintf($base_url, $post_filter->page)
])) .
$core->formNonce() .
'</div>' .
'</form>'
@ -371,10 +362,11 @@ if ($part == 'period') {
__('Periods removed.')
);
http::redirect(empty($_POST['redir']) ?
$p_url.'&part=periods' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'periods']);
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@ -391,10 +383,11 @@ if ($part == 'period') {
__('Periods emptied.')
);
http::redirect(empty($_POST['redir']) ?
$p_url.'&part=periods' :
$_POST['redir']
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
$core->adminurl->redirect('admin.plugin.periodical', ['part' => 'periods']);
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
@ -428,12 +421,10 @@ if ($part == 'period') {
'</head>' .
'<body>' .
dcPage::breadcrumb(
[
dcPage::breadcrumb([
html::escapeHTML($core->blog->name) => '',
__('Periodical') => ''
]
) .
]) .
dcPage::notices() .
'<p class="top-add">
@ -443,8 +434,8 @@ if ($part == 'period') {
# Filters
$p_filter->display('admin.plugin.periodical', form::hidden('p', 'periodical') . form::hidden('part', 'periods'));
# Posts list
echo $period_list->periodDisplay($p_filter->page, $p_filter->nb,
# Periods list
$period_list->periodDisplay($p_filter,
'<form action="' . $p_url . '" method="post" id="form-periods">' .
'%s' .