<?php
/**
 * pragmaMx  Content Management System
 * Copyright (c) 2006 pragmaMx Dev Team - http://pragmaMx.org
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * $Source: /home/www/dps3311/home/cvsroot/pragmamx/pragmamx_01/admin/modules/content.php,v $
 * $Revision: 1.14 $
 * $Author: tora60 $
 * $Date: 2006/12/27 23:35:51 $
 * 
 * this file based on:
 * php-nuke Web Portal System - http://phpnuke.org/
 * Thatware - http://thatware.org/
 */

if (!defined("mxAdminFileLoaded")) die("Access Denied");
if (!mxGetAdminPref('radmincontent')) {
    mxErrorScreen("Access Denied");
    die();
} 

$wysiwyg = 0;
$wysiwyg_content = 0;
include_once('spaw/spaw_control.class.php');

/**
 * Sections Manager Functions
 */

function content()
{
    global $prefix, $currentlang, $multilingual, $bgcolor1, $bgcolor2, $bgcolor3;
    $img_activate = mxCreateImage("images/activate.gif", _ACTIVATE);
    $img_deactivate = mxCreateImage("images/deactivate.gif", _DEACTIVATE);
    $img_delete = mxCreateImage("images/delete.gif", _DELETE);
    $img_edit = mxCreateImage("images/edit.gif", _EDIT);
    include("header.php");
    GraphicAdmin();
    title(_CONTENTMANAGER);
    OpenTable();
    echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" bgcolor=\"$bgcolor1\"><tr bgcolor=\"$bgcolor2\">"
     . "<td><b>" . _TITLE . "</b></td><td align=\"center\"><b>" . _CURRENTSTATUS . "</b></td><td align=\"center\"><b>" . _CATEGORY . "</b></td><td align=\"center\"><b>" . _FUNCTIONS . "</b></td></tr>";
    $result = sql_query("select * from " . $prefix . "_pages order by pid");
    while ($mypages = sql_fetch_array($result)) {
        if ($mypages["cid"] == "0" OR $mypages["cid"] == "") {
            $cat_title = _NONE;
        } else {
            $res = sql_query("select title from " . $prefix . "_pages_categories where cid='" . $mypages["cid"] . "'");
            list($cat_title) = sql_fetch_row($res);
        } 
        if ($mypages["active"] == 1) {
            $status = _ACTIVE;
            $status_chng = $img_deactivate;
            $active = 1;
        } else {
            $status = "<i>" . _INACTIVE . "</i>";
            $status_chng = $img_activate;
            $active = 0;
        } 
        echo "<tr bgcolor=\"$bgcolor3\">
		<td><a href=\"modules.php?name=Content&amp;pa=showpage&amp;pid=" . $mypages["pid"] . "\">" . $mypages["title"] . "</a></td>
		<td>$status</td>
		<td>$cat_title</td>
		<td><a href=\"admin.php?op=content_edit&amp;pid=" . $mypages["pid"] . "\">" . $img_edit . "</a> <a href=\"admin.php?op=content_change_status&amp;pid=" . $mypages["pid"] . "&amp;active=$active\">$status_chng</a> <a href=\"admin.php?op=content_delete&amp;pid=" . $mypages["pid"] . "\">" . $img_delete . "</a></td>
		</tr>";
    } 
    echo "</table>";
    CloseTable();
    echo "<br>";
    OpenTable();
    $res = sql_query("select cid, title from " . $prefix . "_pages_categories order by title");
    echo "<center><b>" . _ADDANEWPAGE . "</b></center><br><br>"
     . "<form action=\"admin.php\" method=\"post\">"
     . "<b>" . _TITLE . ":</b><br>"
     . "<input type=\"text\" name=\"title\" size=\"50\"><br><br>";
    if (sql_num_rows($res) > 0) {
        echo "<b>" . _CATEGORY . ":</b>&nbsp;&nbsp;"
         . "<select name=\"cid\">"
         . "<option value=\"0\" selected>" . _NONE . "</option>";
        while (list($cid, $cat_title) = sql_fetch_row($res)) {
            echo "<option value=\"$cid\">$cat_title</option>";
        } 
        echo "</select><br><br>";
    } else {
        echo "<input type=\"hidden\" name=\"cid\" value=\"0\">";
    } 
    echo "<b>" . _CSUBTITLE . ":</b><br>"
     . "<input type=\"text\" name=\"subtitle\" size=\"50\"><br><br>"
     . "<b>" . _HEADERTEXT . ":</b><br>";

    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('page_header', '', _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '150');
        $sw->show();
    } else {
        echo "<textarea name=\"page_header\" cols=\"60\" rows=\"10\"></textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _PAGETEXT . ":</b><br>"
     . "<font class=\"tiny\">" . _PAGEBREAK . "</font><br>";
    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('text', '', _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '400');
        $sw->show();
    } else {
        echo "<textarea name=\"text\" cols=\"60\" rows=\"40\"></textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _FOOTERTEXT . ":</b><br>";
    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('page_footer', '', _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '150');
        $sw->show();
    } else {
        echo "<textarea name=\"page_footer\" cols=\"60\" rows=\"10\"></textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _SIGNATURE . ":</b><br>";
    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('signature', '', _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '100');
        $sw->show();
    } else {
        echo "<textarea name=\"signature\" cols=\"60\" rows=\"5\"></textarea>";
    } 

    echo "<br><br>";
    if ($multilingual == 1) {
        echo "<br><b>" . _LANGUAGE . ": </b>" . mxLanguageSelect('clanguage', $currentlang, 'language') . "<br><br>";
    } else {
        echo "<input type=\"hidden\" name=\"clanguage\" value=\"" . $currentlang . "\">";
    } 
    echo "<b>" . _ACTIVATEPAGE . "</b><br>"
     . "<input type=\"radio\" name=\"active\" value=\"1\" checked>&nbsp;" . _YES . "&nbsp;&nbsp;<input type=\"radio\" name=\"active\" value=\"0\">&nbsp;" . _NO . "<br><br>"
     . "<input type=\"hidden\" name=\"op\" value=\"content_save\">"
     . "<input type=\"submit\" value=\"" . _ADD . "\">"
     . "</form>";
    CloseTable();

    echo "<br><hr><br>";

    OpenTable();
    echo "<center><b>" . _ADDCATEGORY . "</b></center><br><br>"
     . "<form action=\"admin.php\" method=\"post\">"
     . "<b>" . _TITLE . ":</b><br><input type=\"text\" name=\"cat_title\" size=\"50\"><br><br>"
     . "<b>" . _DESCRIPTION . ":</b><br>";

    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('katcontent', '', _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '100');
        $sw->show();
    } else {
        echo "<textarea name=\"katcontent\" rows=\"10\" cols=\"50\"></textarea>";
    } 

    echo "<br><br>"
     . "<input type=\"hidden\" name=\"op\" value=\"add_category\">"
     . "<input type=\"submit\" value=\"" . _ADD . "\">"
     . "</form>";
    CloseTable();

    $rescat = sql_query("select cid, title from " . $prefix . "_pages_categories order by title");
    if (sql_num_rows($rescat) > 0) {
        echo "<br>";
        OpenTable();
        echo "<center><b>" . _EDITCATEGORY . "</b></center><br><br>"
         . "<form action=\"admin.php\" method=\"post\">"
         . "<b>" . _CATEGORY . ":</b> "
         . "<select name=\"cid\">";
        while (list($cid, $cat_title) = sql_fetch_row($rescat)) {
            echo "<option value=\"$cid\">$cat_title</option>";
        } 
        echo "</select>&nbsp;&nbsp;"
         . "<input type=\"hidden\" name=\"op\" value=\"edit_category\">"
         . "<input type=\"submit\" value=\"" . _EDIT . "\">"
         . "</form>";
        CloseTable();
    } 

    include("footer.php");
} 

function add_category($cat_title, $katcontent)
{
    global $prefix;
    sql_query("insert into " . $prefix . "_pages_categories values (NULL, '" . mxAddSlashesForSQL($cat_title) . "', '" . mxAddSlashesForSQL($katcontent) . "')");
    mxRedirect("admin.php?op=content");
} 

function edit_category($cid)
{
    global $prefix;
    include("header.php");
    GraphicAdmin();
    title(_CONTENTMANAGER);
    OpenTable();
    $result = sql_query("select title, description from " . $prefix . "_pages_categories where cid='" . intval($cid) . "'");
    list($title, $description) = sql_fetch_row($result);
    echo "<center><b>" . _EDITCATEGORY . "</b></center><br><br>"
     . "<form action=\"admin.php\" method=\"post\">"
     . "<b>" . _TITLE . "</b><br>"
     . "<input type=\"text\" name=\"cat_title\" value=\"" . mxEntityQuotes($title) . "\" size=\"50\"><br><br>"
     . "<b>" . _DESCRIPTION . "</b>:<br>";

    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $sw = new SPAW_Wysiwyg('katcontent', $description, _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '100');
        $sw->show();
    } else {
        echo "<textarea name=\"katcontent\" rows=\"10\" cols=\"50\">$description</textarea>";
    } 

    echo "<br><br>"
     . "<input type=\"hidden\" name=\"cid\" value=\"$cid\">"
     . "<input type=\"hidden\" name=\"op\" value=\"save_category\">"
     . "<input type=\"submit\" value=\"" . _SAVECHANGES . "\">&nbsp;&nbsp;"
     . "[&nbsp;<a href=\"admin.php?op=del_content_cat&amp;cid=$cid\">" . _DELETE . "</a>&nbsp;]"
     . "</form>";
    CloseTable();
    include("footer.php");
} 

function save_category($cid, $cat_title, $katcontent)
{
    global $prefix;
    sql_query("update " . $prefix . "_pages_categories set title='" . mxAddSlashesForSQL($cat_title) . "', description='" . mxAddSlashesForSQL($katcontent) . "' where cid=" . intval($cid));
    mxRedirect("admin.php?op=content");
} 

function del_content_cat($cid, $ok = 0)
{
    global $prefix;
    if ($ok == 1) {
        sql_query("delete from " . $prefix . "_pages_categories where cid='" . intval($cid) . "'");
        $result = sql_query("select pid from " . $prefix . "_pages where cid='" . intval($cid) . "'");
        while (list($pid) = sql_fetch_row($result)) {
            sql_query("update " . $prefix . "_pages set cid='0' where pid='" . intval($pid) . "'");
        } 
        mxRedirect("admin.php?op=content");
    } else {
        include("header.php");
        GraphicAdmin();
        title(_CONTENTMANAGER);
        $result = sql_query("select title from " . $prefix . "_pages_categories where cid='" . intval($cid) . "'");
        list($title) = sql_fetch_row($result);
        OpenTable();
        echo "<center><b>" . _DELCATEGORY . ": $title</b><br><br>"
         . _DELCONTENTCAT . "<br><br>"
         . "[&nbsp;<a href=\"admin.php?op=content\">" . _NO . "</a> | <a href=\"admin.php?op=del_content_cat&amp;cid=$cid&amp;ok=1\">" . _YES . "</a>&nbsp;]</center>";
        CloseTable();
        include("footer.php");
    } 
} 

function content_edit($pid)
{
    global $prefix, $multilingual, $bgcolor2;
    include("header.php");
    GraphicAdmin();
    title(_CONTENTMANAGER);
    $result = sql_query("select * from " . $prefix . "_pages WHERE pid='$pid'");
    $mypages = sql_fetch_array($result);
    if ($mypages["active"] == 1) {
        $sel1 = "checked";
        $sel2 = "";
    } else {
        $sel1 = "";
        $sel2 = "checked";
    } 
    OpenTable();
    echo "<center><b>" . _EDITPAGECONTENT . "</b></center><br><br>"
     . "<form action=\"admin.php\" method=\"post\">"
     . "<b>" . _TITLE . ":</b><br>"
     . "<input type=\"text\" name=\"title\" size=\"50\" value=\"" . mxEntityQuotes($mypages["title"]) . "\"><br><br>";
    $res = sql_query("select cid, title from " . $prefix . "_pages_categories");
    if (sql_num_rows($res) > 0) {
        echo "<b>" . _CATEGORY . ":</b>&nbsp;&nbsp;"
         . "<select name=\"cid\">";
        if ($mypages["cid"] == 0) {
            $sel = "selected";
        } else {
            $sel = "";
        } 
        echo "<option value=\"0\" $sel>" . _NONE . "</option>";
        while (list($cid, $cat_title) = sql_fetch_row($res)) {
            if ($mypages["cid"] == $cid) {
                $sel = "selected";
            } else {
                $sel = "";
            } 
            echo "<option value=\"$cid\" $sel>$cat_title</option>";
        } 
        echo "</select><br><br>";
    } else {
        echo "<input type=\"hidden\" name=\"cid\" value=\"0\">";
    } 

    echo "<b>" . _CSUBTITLE . ":</b><br>"
     . "<input type=\"text\" name=\"subtitle\" size=\"50\" value=\"" . mxEntityQuotes($mypages["subtitle"]) . "\"><br><br>"
     . "<b>" . _HEADERTEXT . ":</b><br>";

    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $pheader = $mypages['page_header'];
        $sw = new SPAW_Wysiwyg('page_header', $pheader, _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '150');
        $sw->show();
    } else {
        $pheader = htmlspecialchars($mypages["page_header"]);
        echo "<textarea name=\"page_header\" cols=\"60\" rows=\"10\">{$pheader}</textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _PAGETEXT . ":</b><br>"
     . "<font class=\"tiny\">" . _PAGEBREAK . "</font><br>";
    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $c_text = $mypages['text'];
        $sw = new SPAW_Wysiwyg('text', $c_text, _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '400');
        $sw->show();
    } else {
        $c_text = htmlspecialchars($mypages["text"]);
        echo "<textarea name=\"text\" cols=\"60\" rows=\"40\">{$c_text}</textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _FOOTERTEXT . ":</b><br>";
    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $page_footer = $mypages['page_footer'];
        $sw = new SPAW_Wysiwyg('page_footer', $page_footer, _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '150');
        $sw->show();
    } else {
        $page_footer = htmlspecialchars($mypages["page_footer"]);
        echo "<textarea name=\"page_footer\" cols=\"60\" rows=\"10\">{$page_footer}</textarea>";
    } 

    echo "<br><br>"
     . "<b>" . _SIGNATURE . ":</b><br>";

    if ($GLOBALS['wysiwyg'] && $GLOBALS['wysiwyg_content']) {
        $csignature = $mypages['signature'];
        $sw = new SPAW_Wysiwyg('signature', $csignature, _DOC_LANGUAGE, $GLOBALS['wysiwyg_toolbar'], $GLOBALS['wysiwyg_btnset'], '100%', '100');
        $sw->show();
    } else {
        $csignature = htmlspecialchars($mypages["signature"]);
        echo "<textarea name=\"signature\" cols=\"60\" rows=\"5\">{$csignature}</textarea>";
    } 

    echo "<br><br>";
    if ($multilingual == 1) {
        echo "<br><b>" . _LANGUAGE . ": </b>" . mxLanguageSelect('clanguage', $mypages['clanguage'], 'language', 1) . "<br><br>";
    } else {
        echo "<input type=\"hidden\" name=\"clanguage\" value=\"$mypages[clanguage]\">";
    } 
    echo "<b>" . _ACTIVATEPAGE . "</b><br>"
     . "<input type=\"radio\" name=\"active\" value=\"1\" $sel1>&nbsp;" . _YES . "&nbsp;&nbsp;<input type=\"radio\" name=\"active\" value=\"0\" $sel2>&nbsp;" . _NO . "<br><br>"
     . "<input type=\"hidden\" name=\"pid\" value=\"$pid\">"
     . "<input type=\"hidden\" name=\"op\" value=\"content_save_edit\">"
     . "<input type=\"submit\" value=\"" . _SAVECHANGES . "\">"
     . "</form>";
    CloseTable();
    include("footer.php");
} 

function content_save($title, $subtitle, $page_header, $text, $page_footer, $signature, $clanguage, $active, $cid)
{
    global $prefix;
    if (preg_match('#<body[^>]*>(.+)</body>#is', $text, $matches)) {
        $text = $matches[1];
    } 
    sql_query("insert into " . $prefix . "_pages values (NULL, 
            '" . intval($cid) . "', 
            '" . mxAddSlashesForSQL($title) . "', 
            '" . mxAddSlashesForSQL($subtitle) . "', 
            '" . intval($active) . "', 
            '" . mxAddSlashesForSQL($page_header) . "', 
            '" . mxAddSlashesForSQL($text) . "', 
            '" . mxAddSlashesForSQL($page_footer) . "', 
            '" . mxAddSlashesForSQL($signature) . "', 
            now(), 
            '0', 
            '" . mxAddSlashesForSQL($clanguage) . "'
            )");
    mxRedirect("admin.php?op=content");
} 

function content_save_edit($pid, $title, $subtitle, $page_header, $text, $page_footer, $signature, $clanguage, $active, $cid)
{
    global $prefix;
    if (preg_match('#<body[^>]*>(.+)</body>#is', $text, $matches)) {
        $text = $matches[1];
    } 
    sql_query("update " . $prefix . "_pages set 
               cid='" . intval($cid) . "', 
               title='" . mxAddSlashesForSQL($title) . "', 
               subtitle='" . mxAddSlashesForSQL($subtitle) . "', 
               active='" . intval($active) . "', 
               page_header='" . mxAddSlashesForSQL($page_header) . "', 
               text='" . mxAddSlashesForSQL($text) . "', 
               page_footer='" . mxAddSlashesForSQL($page_footer) . "', 
               signature='" . mxAddSlashesForSQL($signature) . "', 
               clanguage='" . mxAddSlashesForSQL($clanguage) . "' 
               where pid='" . intval($pid) . "'");
    mxRedirect("admin.php?op=content");
} 

function content_change_status($pid, $active)
{
    global $prefix;
    sql_query("update " . $prefix . "_pages set active='" . intval(!$active) . "' WHERE pid='" . intval($pid) . "'");
    mxRedirect("admin.php?op=content");
} 

function content_delete($pid, $ok = 0)
{
    global $prefix;
    if ($ok == 1) {
        sql_query("delete from " . $prefix . "_pages where pid='" . intval($pid) . "'");
        mxRedirect("admin.php?op=content");
    } else {
        include("header.php");
        GraphicAdmin();
        title(_CONTENTMANAGER);
        $result = sql_query("select title from " . $prefix . "_pages where pid='" . intval($pid) . "'");
        list($title) = sql_fetch_row($result);
        OpenTable();
        echo "<center><b>" . _DELCONTENT . ": $title</b><br><br>"
         . _DELCONTWARNING . " $title?<br><br>"
         . "[&nbsp;<a href=\"admin.php?op=content\">" . _NO . "</a> | <a href=\"admin.php?op=content_delete&amp;pid=$pid&amp;ok=1\">" . _YES . "</a>&nbsp;]</center>";
        CloseTable();
        include("footer.php");
    } 
} 

if (empty($ok)) $ok = 0;
switch ($op) {
    case "content_edit":
        content_edit($pid);
        break;

    case "content_delete":
        content_delete($pid, $ok);
        break;

    case "content_review":
        content_review($title, $subtitle, $page_header, $text, $page_footer, $signature, $clanguage, $active);
        break;

    case "content_save":
        content_save($title, $subtitle, $page_header, $text, $page_footer, $signature, $clanguage, $active, $cid);
        break;

    case "content_save_edit":
        content_save_edit($pid, $title, $subtitle, $page_header, $text, $page_footer, $signature, $clanguage, $active, $cid);
        break;

    case "content_change_status":
        content_change_status($pid, $active);
        break;

    case "add_category":
        add_category($cat_title, $katcontent);
        break;

    case "edit_category":
        edit_category($cid);
        break;

    case "save_category":
        save_category($cid, $cat_title, $katcontent);
        break;

    case "del_content_cat":
        del_content_cat($cid, $ok);
        break;
    case "content":
    default:
        content();
        break;
} 

?>