Commit bfc93792 authored by jan.koester's avatar jan.koester
Browse files

initial work templates

parent 7219ff0b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,3 +6,4 @@ add_subdirectory(youtube)
add_subdirectory(navbar)
add_subdirectory(media)
add_subdirectory(nginxfiler)
add_subdirectory(template)
+48 −11
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>
#include <cstring>
#include <string>

@@ -33,6 +33,8 @@
#include "theme.h"
#include "conf.h"

#include <htmlpp/exception.h>

namespace blogi {
    class Navbar : public PluginApi {
    public:
@@ -67,6 +69,7 @@ namespace blogi {
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                <<   "name character varying(255) NOT NULL,"
                <<   "url character varying(255) NOT NULL,"
                <<   "type integer default 0,"
                <<   "navbar_id integer,"
                <<   "FOREIGN KEY (navbar_id) REFERENCES navbar (id)"
                << ");";
@@ -91,24 +94,36 @@ namespace blogi {

            for (int i = 0; i < n; i++) {
                libhtmlpp::HtmlString buf;
                buf << "<div id=\"" << res[i][1] << "\">" << "<ul>";
                buf << "<ul>";

                sql2 << "select url,name from navbar_items WHERE navbar_id='" << res[i][0] << "' ORDER BY id";
                sql2 << "select url,name,type from navbar_items WHERE navbar_id='" << res[i][0] << "' ORDER BY id";

                int n2 = Args->database[tid]->exec(&sql2,res2);
                for (int ii = 0; ii < n2; ii++) {
                    std::cout << res2[ii][2] <<std::endl;
                    buf << "<li ";
                    if(turl.compare(0,strlen(res2[ii][0]),res2[ii][0]) == 0 )
                        buf << "class=\"active\"";
                    else
                        buf << "class=\"inactive\"";
                    if(strcmp(res2[ii][2],"0")==0)
                        buf << "><a href=\"" << res2[ii][0] << "\">" << res2[ii][1] << "</a></li>";
                    else
                        buf << "><span id=\"" << res2[ii][1] << "\" >" << res2[ii][0] << "</span></li>";
                }
                buf << "</ul></div>";
                buf << "</ul>";

                sql2.clear();
                if(curpage->getElementbyID(res[i][2]))
                    curpage->getElementbyID(res[i][2])->appendChild(buf.parse());
                if(curpage->getElementbyID(res[i][2])){
                    try{
                        libhtmlpp::Element *el=buf.parse();
                        curpage->getElementbyID(res[i][2])->appendChild(el);
                    }catch(libhtmlpp::HTMLException &e){
                        libhttppp::HTTPException excep;
                        excep[libhttppp::HTTPException::Critical] << e.what();
                        throw excep;
                    }
                }
            }

        }
@@ -163,7 +178,7 @@ namespace blogi {
            blogi::SQL sql;
            blogi::DBResult res;

            std::string navname,container_id,newitem_name,newitem_url;
            std::string navname,container_id,newitem_name,newitem_url,newitem_type;

            setdiv << "<div id=\"navsettings\"><span>Edit Navigation</span><br>";

@@ -214,11 +229,24 @@ namespace blogi {

            };

            auto changeItemType = [this,tid,navid](const char *key,const char *value){
                int iid;
                sscanf(key,"navitem_type_%d",&iid);
                blogi::SQL sql2;
                blogi::DBResult res2;
                sql2 << "UPDATE navbar_items SET type='"; sql2.escaped(value) << "' WHERE navbar_id='" << navid << "' AND id='" << iid << "'";
                Args->database[tid]->exec(&sql2,res2);
                sql2.clear();

            };

            for(libhttppp::HttpForm::UrlcodedForm::Data *cdat=formdat; cdat; cdat=cdat->nextData()){
                if(strcmp(cdat->getKey(),"navitem_name_")<0)
                    changeItemName(cdat->getKey(),cdat->getValue());
                else if(strcmp(cdat->getKey(),"navitem_url_")<0)
                    changeItemUrl(cdat->getKey(),cdat->getValue());
                else if(strcmp(cdat->getKey(),"navitem_type_")<0)
                    changeItemType(cdat->getKey(),cdat->getValue());
            }

            if(rem_itemid>=0){
@@ -240,8 +268,8 @@ namespace blogi {
            }


            if(!newitem_name.empty() && !newitem_url.empty()){
                sql << "INSERT INTO navbar_items (name,url,navbar_id) VALUES('"; sql.escaped(newitem_name.c_str()) << "','"; sql.escaped(newitem_url.c_str()) << "','" << navid << "')";
            if(!newitem_name.empty() && !newitem_url.empty() && !newitem_type.empty()){
                sql << "INSERT INTO navbar_items (name,url,navbar_id,type) VALUES('"; sql.escaped(newitem_name.c_str()) << "','"; sql.escaped(newitem_url.c_str()) << "','" << navid << "'," << newitem_type.c_str() <<")";
                Args->database[tid]->exec(&sql,res);
                sql.clear();
            }
@@ -261,18 +289,27 @@ namespace blogi {

            int n = Args->database[tid]->exec(&sql,res);

            setdiv << "<tr><td>item name:</td><td>item url:</td><td>actions:</td></tr>";
            setdiv << "<tr><td>item name:</td><td>item url:</td><td>Type:</td><td>actions:</td></tr>";

            for(int i=0; i<n; ++i){
                setdiv << "<tr>"
                       << "<td><input type=\"text\" name=\"navitem_name_"<< res[i][0] << "\" value=\"" << res[i][1] << "\" /></td>"
                       << "<td><input type=\"text\" name=\"navitem_url_"<< res[i][0] << "\" value=\"" << res[i][2] << "\" /></td>"
                       << "<td><select name=\"navitem_type_"<< res[i][0] << "\"/> ";
                if(res[i][2]==0)
                    setdiv << "<option value=\"link\" selected >link</option><option value=\"text\">text</option>";
                else
                    setdiv << "<option value=\"link\">link</option><option value=\"text\" selected>text</option>";
                setdiv << "<select></td>"
                       << "<td><a href=\"?navid="<< navid <<"&navitem_remove=" << res[i][0] << "\">remove</a></td></tr>";
            }

            setdiv << "<tr>"
                   << "<td><input type=\"text\" name=\"navitem_name_new\" value=\"\" /></td>"
                   << "<td><input type=\"text\" name=\"navitem_url_new\" value=\"\" /></td>"
                   << "<td><select name=\"navitem_type_new\" value=\"\"/> "
                   << "<option value=\"link\" selected >link</option><option value=\"text\">text</option>"
                   << "<select></td>"
                   << "<td> </td>"
                   << "</tr>";

+6 −0
Original line number Diff line number Diff line
add_library(template SHARED template.cpp)

target_link_libraries(template dl blogidev)

install (TARGETS template  DESTINATION lib/blogi/plugins)
#
+85 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2023, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>
#include <algorithm>

#include <htmlpp/html.h>

#include <conf.h>

#include <conf.h>
#include <editor.h>
#include <plugin.h>
#include <database.h>

namespace blogi {
    class Template : public PluginApi {
    public:
        Template(){

        };

        ~Template(){

        };

        void initPlugin(){
            SQL sql;
            DBResult res;
            sql << "CREATE TABLE IF NOT EXISTS static_content ("
                << "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                << "layer varchar(255),"
                << "html text"
                << ");";
            Args->database[0]->exec(&sql,res);
        };


        const char * getAuthor(){
            return "Jan Koester";
        }

        const char * getName(){
            return "template";
        }

        const char * getVersion(){
            return "20241020";
        }

    };


    extern "C" PluginApi* create() {
        return new Template();
    }

    extern "C" void destroy(PluginApi* p) {
        delete p;
    }
};
+2 −2
Original line number Diff line number Diff line
@@ -79,12 +79,12 @@ blogi::Blogi::Blogi(Config *blgcfg,netplus::socket *serversocket,bool debug) : H
    PlgArgs->auth=new Auth(PlgArgs->database,PlgArgs->session,PlgArgs->config);
    PlgArgs->edit=new Editor(PlgArgs->config);

    TemplateConfig tplcfg;
    ThemeConfig tplcfg;
    tplcfg.config=blgcfg;
    tplcfg.Theme=tplcfg.config->gettemplate();
    tplcfg.TDatabase=PlgArgs->database;

    PlgArgs->theme=new Template(tplcfg,debug);
    PlgArgs->theme=new Theme(tplcfg,debug);


    PlgArgs->theme->renderPage(0,"index.html",Page,Index);
Loading