stl-statilque-litterateur/var/lib/stl/scripts/manage__sidebar
2022-07-20 19:37:21 +02:00

186 lines
5 KiB
Bash

#!/bin/bash
# Name: Statique Littérateur
# Type: Sidebar manager
# file: manage__sidebar
# Folder: /var/lib/stl/scripts/
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#======================================================================
# Sidebar: Manage arguments
# Check Article, WIP and Position
#======================================================================
sidebar__OPTIONS() {
case "$2" in
create)
domain__get
sidebar__create_HTML
return
;;
add)
# Article
article__hash "$4"
# Database
db_file="$domain_db_articles/$article_id.db"
source "$db_file"
# Already in sidebar at same position
[[ "$3" == $sidebar_position ]] \
&& echo "! Article is in sidebar at same position: $3" \
&& return
# Already in sidebar, different position
[[ $sidebar_position ]] \
&& echo "! Article is in sidebar at position: $sidebar_position" \
&& return
# WIP check
! [[ $article_wip_hash -gt 0 ]] \
&& echo "! Article not yet converted in HTML (wip)" \
&& return
# Position. Must be 1 to max_items
if [[ "$3" == *[!0-9]* ]];then
[[ $sidebar ]] \
&& echo "Use position 1 to $domain_siddebar_items" \
|| noarg "$3" "Use position 1 to $domain_siddebar_items"
fi
if (( "$3" == 0 || "$3" > $domain_sidebar_items ));then
[[ $sidebar ]] \
&& echo "Use position 1 to $domain_siddebar_items" \
|| noarg "$3" "Use position 1 to $domain_siddebar_items"
fi
position="$3"
sidebar_HTML_item="$domain_dir_wip_tpl/.$3.sidebar"
;;
latest|oldest)
domain__get
;;
*)
noarg "$2" "Use: add,latest,oldest"
;;
esac
# Start process
sidebar__manage "$2" "$position" "$sidebar_HTML_item"
}
#======================================================================
# Sidebar: create /templates/sidebar.html
# $1: Process (add, replace...)
# $2: Position
# $3: file (.POS.sidebar) in wip template
#======================================================================
sidebar__manage() {
case "$1" in
latest|-l) sort_arg='-k2' ;;
oldest|-o) sort_arg='-k2r' ;;
esac
case "$1" in
latest|oldest)
sidebar_pos=0
while IFS=: read -r 'file' 'timestamp'
do
((position++))
(( $position > $domain_sidebar_items )) \
&& break
file=`basename "$file"`
db_file="$domain_db_articles/$file"
source "$db_file"
! [[ $article_wip_hash -gt 0 ]] \
&& continue
sidebar_HTML_item="$domain_dir_wip_tpl/.$position.sidebar"
sidebar__create_HTML_item "$sidebar_HTML_item"
done < <(grep -H "article_timestamp" "$domain_db_articles/"*.db \
| sort -t= $sort_arg )
;;
add)
sidebar__create_HTML_item "$3"
;;
esac
sidebar__create_HTML "$gsl_dir_domain_tpl/sidebar.html"
}
#======================================================================
# Called from sidebar__create in gsl__post_makers
# file: /var/lib/gsl/domains/DOMAIN/X-tra/sidebar/[POSITION].html
# You can edit this HTML code for your needs
# It can get all Values from Database post
# $1: file ($sidebar_HTML_item)
#======================================================================
sidebar__create_HTML_item() {
create__HTML_translation
cat <<EOSIDEBARITEM > "$1"
<li class="${domain_css}_list-post-item">
<a href="$article_uri_srv"
class="${domain_css}_list-post-link"
title="$stl_read $article_author: $article_title">
<span class="${domain_css}_list-post-title">$article_title</span>
<div class="${domain_css}_list-post-metas">
$stl_the $stl_date $stl_by $article_author
</div>
<div class="${domain_css}_list-post-info">
$article_about
</div>
</a>
</li>
EOSIDEBARITEM
# Log added
log__add -i -S -A "$article_title added to position: $position"
# Unset sidebar position in DB if an article is at position
db_file_sidebar=`
grep -H "sidebar_position=$position" \
"$domain_db_articles/"*.db \
| awk -F: '{print $1}'`
if [[ $db_file_sidebar ]];then
source "$db_file_sidebar"
sed -i "s^sidebar_position=.*^sidebar_position=^" "$db_file_sidebar"
log__add -w -S -A "$article_title removed from position: $position"
fi
# Add value to DB
sed -i "s^sidebar_position=.*^sidebar_position=$position^" "$db_file"
}
#======================================================================
# Create sidebar.html from files .1-max.sidebar
#======================================================================
sidebar__create_HTML() {
[[ -f "$domain_dir_wip_tpl/sidebar.html" ]] \
&& rm -f "$domain_dir_wip_tpl/sidebar.html"
for i in `seq 1 $domain_sidebar_items`
do
[[ -f "$domain_dir_wip_tpl/.$i.sidebar" ]] \
&& cat "$domain_dir_wip_tpl/.$i.sidebar" \
>> "$domain_dir_wip_tpl/sidebar.html"
done
this_article="$domain_dir_wip_tpl/sidebar.html"
log__add -i -S WIP "Created items from 1 to $domain_sidebar_items"
}