<?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/html/admin/modules/modules.php,v $
 * $Revision: 1.4.4.5 $
 * $Author: tora60 $
 * $Date: 2006/11/14 23:06:02 $
 */

if (!defined("mxAdminFileLoaded")) die("Access Denied");
if (!mxGetAdminPref("radminsuper")) {
    mxErrorScreen("Access Denied");
    die();
} 
// #################################################################################
function modules()
{
    global $prefix, $bgcolor1, $bgcolor2;

    $img_activate = mxCreateImage("images/activate.gif", _ACTIVATE);
    $img_deactivate = mxCreateImage("images/deactivate.gif", _DEACTIVATE);
    $img_edit = mxCreateImage("images/edit.gif", _EDIT);
    $img_home = mxCreateImage("images/inhome.gif", _PUTINHOME);
    $img_view = mxCreateImage("images/view.gif", _SHOW);
    $mainmod = mxGetMainModuleName();
    $qry = "SELECT title, mid, custom_title, active, view, main_id FROM ${prefix}_modules";
    $result = sql_query($qry);
    while ($mods = sql_fetch_array($result)) {
        if (!@file_exists("modules/" . $mods['title'] . "/")) {
            sql_query("DELETE FROM ${prefix}_groups_modules WHERE module_id=" . $mods['mid'] . "");
            sql_query("DELETE FROM ${prefix}_modules WHERE mid=" . $mods['mid'] . "");
        } else {
            $dbmodlist[strtolower($mods['title'])] = $mods;
        } 
    } 
    unset($mods);
    $doublemod = array();
    $handle = opendir('modules');
    while ($file = readdir($handle)) {
        if (in_array(strtolower($file), $doublemod)) {
            $doublerror[] = _ADMIN_WARNDOUBLEMOD1 . $file . _ADMIN_WARNDOUBLEMOD2 . "<br>";
        } 
        $doublemod[] = strtolower($file);
        if (!ereg("[.]", $file) && !isset($dbmodlist[strtolower($file)])) {
            /* If you copied a new module is the /modules/ directory, it will be added to the database */
            $ctitle = str_replace("_", " ", $file);
            $qry = "INSERT INTO ${prefix}_modules (title,custom_title,active,view) values ('" . $file . "', '" . $ctitle . "', 0, 0)";
            $result = sql_query($qry);
            if ($result) {
                $result1 = sql_query("select title, mid, custom_title, active, view, main_id from ${prefix}_modules WHERE title='" . $file . "'");
                $newmod = sql_fetch_array($result1);
                $dbmodlist[strtolower($newmod['title'])] = $newmod;
            } 
        } 
    } 
    closedir($handle);
    unset($doublemod);
    if (empty($dbmodlist)) {
        mxErrorScreen("no modules installed!");
    } 
    ksort($dbmodlist);

    include ("header.php");
    foreach ($dbmodlist as $module) {
        extract($module);
        $act = (empty($active)) ? 1 : 0;
        $change = (empty($active)) ? $img_activate : $img_deactivate;
        $custom_title = (empty($custom_title)) ? str_replace("_", " ", $title) : $custom_title;
        $main_id = (empty($main_id)) ? "Modules" : $main_id;
        $main_id = ($main_id == "hided") ? "<span class=\"tiny\"><i>" . _COSTUMNAVIBLOCKHID . "</i></span>" : $main_id;
        if ($view == 1) $who_view = _MVGROUPS2;
        elseif ($view == 2) $who_view = _MVADMIN;
        elseif ($view == 3) $who_view = _MVANON;
        else $who_view = _MVALL; 
        // if (empty($main)) {
        if ($title != $mainmod) {
            $change = "<a href=\"admin.php?op=module_status&amp;mid=" . $mid . "&amp;active=" . $act . "\">" . $change . "</a>";
            $puthome = "<a href=\"admin.php?op=home_module&amp;mid=" . $mid . "\">" . $img_home . "</a>";
            $clickit = "<a href=\"modules.php?name=" . $title . "\" target=\"_blank\">" . $img_view . "</a>";
            if ($active) {
                $out_active[] = "<tr bgcolor=\"$bgcolor1\"><td>" . $title . "</td><td>" . $custom_title . "</td><td>" . $main_id . "</td><td>" . $who_view . "</td><td align='center'><a href=\"admin.php?op=module_edit&amp;mid=" . $mid . "\">" . $img_edit . "</a> &nbsp;" . $change . " &nbsp;" . $puthome . " &nbsp;" . $clickit . "</td></tr>"; #<td>".$active."</td>
            } else {
                $out_deact[] = "<tr bgcolor=\"$bgcolor1\"><td>" . $title . "</td><td>" . $custom_title . "</td><td>" . $main_id . "</td><td>" . $who_view . "</td><td align='center'><a href=\"admin.php?op=module_edit&amp;mid=" . $mid . "\">" . $img_edit . "</a> &nbsp;" . $change . " &nbsp;" . $puthome . " &nbsp;" . $clickit . "</td></tr>";
            } 
        } else {
            $clickit = "<a href=\"index.php\" target=\"_blank\">" . $img_view . "</a>";
            $out_main[] = "<tr bgcolor=\"$bgcolor1\"><td>" . $title . "</td><td><b>" . _HOME . "</b></td><td>" . $main_id . "</td><td>" . _MVALL . "</td><td align='center'><a href=\"admin.php?op=module_edit&amp;mid=" . $mid . "\">" . $img_edit . "</a> &nbsp;" . $clickit . "</td></tr>";
        } 
    } 
    unset($dbmodlist);
    $headline = "<tr bgcolor=\"$bgcolor2\">
	<th class=\"content\">" . _TITLE . "</th>
	<th class=\"content\">" . _CUSTOMTITLE . "</th>
	<th class=\"content\">" . "Modul-Block" . "</th>
	<th class=\"content\">" . _VIEW . "</th>
	<th class=\"content\">" . _FUNCTIONS . "</th></tr>";
    GraphicAdmin();
    title(_MODULESADMIN);
    if (isset($doublerror)) {
        OpenTableAl();
        echo "<center>" . implode("\n", $doublerror) . "<br><b>" . _ADMIN_WARNDOUBLEMOD3 . "</b></center>";
        CloseTableAl();
        echo "<br>";
    } 
    OpenTable();
    echo "<table border=\"0\" width=\"100%\" align=\"center\" bgcolor=\"$bgcolor2\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>\n";
    echo "<table border=\"0\" align=\"center\" width=\"100%\" bgcolor=\"$bgcolor2\" cellpadding=\"2\" cellspacing=\"1\">\n";
    if (isset($out_active)) {
        echo "<tr bgcolor=\"$bgcolor2\"><td class=\"title\" colspan=5>" . _ACTIVEMODULES . ":</td></tr>\n";
        echo $headline;
        echo implode("\n", $out_active);
    } 
    if (isset($out_main)) {
        echo "<tr bgcolor=\"$bgcolor2\"><td class=\"title\" colspan=5>" . _DEFHOMEMODULE . ":</td></tr>\n";
        echo $headline;
        echo implode("\n", $out_main);
    } 
    if (isset($out_deact)) {
        echo "<tr bgcolor=\"$bgcolor2\"><td class=\"title\" colspan=5>" . _NOACTIVEMODULES . ":</td></tr>\n";
        echo $headline;
        echo implode("\n", $out_deact);
    } 
    echo "</table>\n";
    echo "</td></tr></table>\n";
    echo "<br><font class=\"content\">" . _MODULESACTIVATION . "<br><br>" . _MODULEHOMENOTE . "</font>";
    CloseTable();
    include ("footer.php");
} 
// #################################################################################
function home_module($gvs)
{
    global $prefix;

    $ok = (empty($gvs['ok'])) ? 0 : 1;
    $mid = (int)$gvs['mid'];
    $old_m = mxGetMainModuleName();

    $result = sql_query("select title from " . $prefix . "_modules where mid=" . $mid . "");
    list($new_m) = sql_fetch_row($result);

    if (empty($ok)) {
        include ("header.php");
        GraphicAdmin();
        title(_HOMECONFIG);
        OpenTable();
        echo "<center><b>" . _DEFHOMEMODULE . "</b><br><br>"
         . _SURETOCHANGEMOD . " <b>" . $old_m . "</b> " . _TO . " <b>" . $new_m . "</b>?<br><br>"
         . "[&nbsp;<a href=\"admin.php?op=modules\">" . _NO . "</a> | <a href=\"admin.php?op=home_module&amp;mid=" . $mid . "&ok=1\">" . _YES . "</a>&nbsp;]</center>";
        CloseTable();
        include("footer.php");
    } else {
        sql_query("DELETE FROM " . $prefix . "_main");
        sql_query("INSERT INTO " . $prefix . "_main values ('" . mxAddSlashesForSQL($new_m) . "')");
        sql_query("update " . $prefix . "_modules set active=1, view=0 where mid=" . $mid . "");
        header("Location: admin.php?op=modules");
    } 
} 
// #################################################################################
function module_status($mid, $active)
{
    global $prefix;
    $mid = (int)$mid;
    sql_query("update " . $prefix . "_modules set active='$active' where mid=" . $mid . "");
    header("Location: admin.php?op=modules");
} 
// #################################################################################
function module_edit($mid)
{
    global $prefix;
    $mid = (int)$mid;
    $main_module = mxGetMainModuleName();
    $result = sql_query("select title, custom_title, active, view, main_id from " . $prefix . "_modules where mid=" . $mid . "");
    list($title, $custom_title, $active, $view, $main_id) = sql_fetch_row($result);
    $custom_title = (empty($custom_title)) ? str_replace("_", " ", $title) : $custom_title;
    include ("header.php");
    GraphicAdmin();
    title("" . _MODULEEDIT . "");
    OpenTable();
    echo "<b>" . _CHANGEMODNAME . ": " . $title . "</b><br><br>"
     . "<form action=\"admin.php\" method=\"post\">"
     . "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">"
     . "<tr valign=\"top\"><td>" . _CUSTOMMODNAME . "</td>";
    if ($title == $main_module) {
        echo "<td><b>" . _HOME . "</b><input type=\"hidden\" name=\"custom_title\" value=\"" . mxEntityQuotes($custom_title) . "\"></td></td></tr>";
    } else {
        echo "<td><input type=\"text\" name=\"custom_title\" value=\"" . mxEntityQuotes($custom_title) . "\" size=\"50\" ></td></tr>";
    } 
    echo "<tr valign=\"top\"><td>" . _VIEWPRIV . "</td><td>";
    if ($title == $main_module) {
        echo "<b>" . _MVALL . "</b> (" . _DEFHOMEMODULE . ")"
         . "<input type=\"hidden\" name=\"view\" value=\"0\">"
         . "<input type=\"hidden\" name=\"active\" value=\"1\">"; 
        // ."<input type=\"hidden\" name=\"main_id\" value=\"\">";
    } else {
        echo getViewSelect($view)
         . "</td></tr>"
         . "<tr valign='top'><td><!-- " . _VIEWPRIV . " --></td><td>" . getGroupSelect($mid) . "</td></tr>"
         . "<tr valign=\"top\"><td>" . _ACTIVE . "</td>"
         . "<td>" . getActiveSelect($active) . "</td></tr>"
         . "<tr valign=\"top\"><td>&nbsp;</td>"
         . "<td><input type=\"checkbox\" name=\"putinhome\" value=\"1\">&nbsp; " . _PUTINHOME . "</td></tr>";
    } 
    echo "<tr valign=\"top\"><td>" . _COSTUMNAVIBLOCK . "</td>"
     . "<td>" . getModuleBlockSelect($main_id) . "</td></tr>";
    echo "</table><br><br>"
     . "<input type=\"hidden\" name=\"mid\" value=\"" . $mid . "\">"
     . "<input type=\"hidden\" name=\"title\" value=\"" . $title . "\">"
     . "<input type=\"hidden\" name=\"op\" value=\"module_edit_save\">"
     . "<input type=\"submit\" value=\"" . _SAVECHANGES . "\">"
     . "</form>"
     . "<br><br><center>" . _GOBACK . "</center>";
    CloseTable();
    include("footer.php");
} 
// #################################################################################
function module_edit_save($pvs)
{
    global $prefix;
    $pvs['mid'] = (int)$pvs['mid'];
    $pvs['putinhome'] = (empty($pvs['putinhome'])) ? 0 : 1;
    $pvs['active'] = ($pvs['putinhome']) ? 1 : (int)$pvs['active'];
    $pvs['view'] = ($pvs['putinhome']) ? 0 : (int)$pvs['view'];
    $pvs['custom_title'] = strip_tags($pvs['custom_title']);
    $pvs['custom_title'] = (empty($pvs['custom_title'])) ? str_replace("_", " ", $pvs['title']) : $pvs['custom_title'];
    $pvs['main_id'] = ($pvs['putinhome']) ? "" : $pvs['main_id'];
    $pvs['main_id'] = ($pvs['main_id'] == "Modules") ? "" : $pvs['main_id'];
    $qry = "update " . $prefix . "_modules set 
	        custom_title='" . $pvs['custom_title'] . "', 
					view=" . $pvs['view'] . ", 
					active=" . $pvs['active'] . ", 
					main_id='" . $pvs['main_id'] . "' 
					where mid=" . $pvs['mid'] . "";
    $result = sql_query($qry); 
    // mxDebugFuncVars($pvs);
    if ($result) {
        // Gruppen aktualisieren
        sql_query("DELETE FROM " . $prefix . "_groups_modules WHERE module_id=" . $pvs['mid'] . "");
        if ($pvs['view'] == 1 && !$pvs['putinhome']) {
            foreach ($pvs['groups'] as $groupid) {
                sql_query("INSERT INTO " . $prefix . "_groups_modules (group_id, module_id) VALUES (" . $groupid . ", " . $pvs['mid'] . ")");
            } 
        } 
        if ($pvs['putinhome']) {
            $old_m = mxGetMainModuleName();
            if (empty($old_m)) {
                $result = sql_query("INSERT INTO " . $prefix . "_main values ('" . mxAddSlashesForSQL($pvs['title']) . "')");
            } else {
                $result = sql_query("update " . $prefix . "_main set main_module='" . mxAddSlashesForSQL($pvs['title']) . "'");
            } 
        } 
    } 
    header("Location: admin.php?op=modules");
} 
// ###############################################################
function getActiveSelect($active = 1)
{
    $active = (int)$active;
    $out = "<input type=\"radio\" name=\"active\" value=\"1\"" . (($active == 1) ? " checked" : "") . ">" . _YES . " &nbsp;&nbsp;";
    $out .= "<input type=\"radio\" name=\"active\" value=\"0\"" . (($active == 0) ? " checked" : "") . ">" . _NO . "\n";
    return $out;
} 
// ###############################################################
function getViewSelect($view = 0)
{
    $view = (int)$view;
    $out = "<select name=\"view\">"
     . "<option value=\"0\"" . (($view == 0) ? ' selected="selected"' : '') . ">" . _MVALL . "</option>"
     . "<option value=\"1\"" . (($view == 1) ? ' selected="selected"' : '') . ">" . _MVGROUPS . "</option>"
     . "<option value=\"2\"" . (($view == 2) ? ' selected="selected"' : '') . ">" . _MVADMIN . "</option>"
     . "<option value=\"3\"" . (($view == 3) ? ' selected="selected"' : '') . ">" . _MVANON . "</option>"
     . "</select>";
    return $out;
} 
// ###############################################################
function getGroupSelect($modid = 0)
{
    global $prefix;
    $modid = (int)$modid;
    if (!empty($modid)) {
        $qry = "SELECT group_id FROM ${prefix}_groups_modules WHERE module_id=" . $modid . "";
        $result = sql_query($qry);
        while (list($groups[]) = sql_fetch_row($result));
    } 
    $groups = (empty($groups)) ? 1 : $groups;
    $groupoptions = getAllAccessLevelSelectOptions($groups);
    $cnt = substr_count($groupoptions, "<option") + 1;
    $out = "<select name='groups[]' id='groups' size='" . $cnt . "' multiple>" . $groupoptions . "</select>";
    return $out;
} 
// ###############################################################
function getModuleBlockSelect($main_id = "")
{
    global $prefix;
    $main_id = (empty($main_id)) ? "Modules" : $main_id;
    $result = sql_query("select title, blockfile from ${prefix}_blocks where blockfile like 'block-modules%.php'");
    while (list($btitle, $bfs) = sql_fetch_row($result)) {
        $dbblocks[$bfs] = $btitle;
    } 
    $blocksdir = dir("blocks");
    while ($block = $blocksdir->read()) {
        if (strtolower(substr($block, 0, 13)) == "block-modules") {
            $mode = (isset($dbblocks[$block])) ? 1 : 0;
            $is = ($mode) ? " (" . $dbblocks[$block] . ")": "  *";
            $bl = str_replace("block-", "", $block);
            $bl = str_replace(".php", "", $bl);
            $sel = ($main_id == $bl) ? " selected" : "";
            $options[$mode][strtolower($bl)] = "<option value=\"" . $bl . "\" " . $sel . ">" . $bl . $is . "</option>";
        } 
    } 
    closedir($blocksdir->handle); 
    // if (empty($main_id)) $options[] = "<option value=\"\" selected></option>";
    $view = "";
    $sel = ($main_id == "hided") ? " selected" : "";
    $options[1]["aaaaaa"] = "<option value=\"hided\" " . $sel . ">" . _COSTUMNAVIBLOCKHID . "</option>";
    if (isset($options[1])) {
        ksort($options[1]);
        $view .= implode("\n", $options[1]);
    } 
    if (isset($options[0])) {
        ksort($options[0]);
        $view .= implode("\n", $options[0]);
    } 
    $out = "\n<select name=\"main_id\">\n" . $view . "</select>\n";
    $out .= " <br>\n<span class=\"tiny\">" . _COSTUMNAVIEXAMPLE . "</span>\n";
    return $out;
} 
// ###############################################################
switch ($_REQUEST['op']) {
    case "modules":
        modules();
        break;

    case "module_status":
        module_status($mid, $active);
        break;

    case "module_edit":
        module_edit($mid);
        break;

    case "module_edit_save":
        module_edit_save($_POST);
        break;

    case "home_module":
        home_module($_GET);
        break;
} 

?>