<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2007                                            */
/* http://www.php-maximus.org                                           */
/* dev@php-maximus.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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/* Portage Coppermine Photo Gallery                                     */
/************************************************************************/
defined('MAXIMUS') or require_once("mainfile.php");

if ( !defined('USER_ALLOW') ) {
	redirect_url('modules.php?name=Coppermine');
}

$pagetitle = "- Coppermine";


define('IN_COPPERMINE', true);
define('MODIFYALB_PHP', true);

switch($func) {
default:
incoh();

include("modules/Coppermine/include/init.inc.php");

if (!(GALLERY_ADMIN_MODE || USER_ADMIN_MODE)) {
    cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);
}
new_cache();
// Type 0 => input
//      1 => yes/no
//      2 => Category
//		3 => Textarea
//      4 => Album thumbnail
//		5 => Album visibility
$data = array(
	$lang_modifyalb_php['general_settings'],
	array($lang_modifyalb_php['alb_title'], 'title', 0),
	array($lang_modifyalb_php['alb_cat'], 'category', 2),
	array($lang_modifyalb_php['alb_desc'], 'description', 3),
	array($lang_modifyalb_php['alb_thumb'], 'thumb', 4),
	$lang_modifyalb_php['alb_perm'],
	array($lang_modifyalb_php['can_view'], 'visibility', 5),
	array($lang_modifyalb_php['can_upload'], 'uploads', 1),
	array($lang_modifyalb_php['can_post_comments'], 'comments', 1),
	array($lang_modifyalb_php['can_rate'], 'votes', 1)
);

function get_subcat_data($parent, $ident='')
{
    global $CAT_LIST;
	$parent = secure( $parent, 'num', 1, 'sec' );
	$result = $GLOBALS['db']->sql_query("SELECT `cid`, `namee`, `description` FROM `".TABLE_CPG_CATEGORIES."` WHERE `parent` = '$parent' AND `cid` != '1' ORDER BY `pos`");
	if ($GLOBALS['db']->sql_numrows($result) > 0){
		$rowset = $GLOBALS['db']->sql_fetch_rowset($result);
		foreach ($rowset as $subcat){
			$CAT_LIST[]=array($subcat['cid'], $ident.$subcat['namee']);
			get_subcat_data($subcat['cid'], $ident.'&nbsp;&nbsp;&nbsp;');
		}
	}
}

function form_label($text)
{
	echo <<<EOT
	<tr>
		<td class="tableh2" colspan="2">
			<b>$text</b>
		</td>
	</tr>

EOT;
}

function form_input($text, $namee)
{
	global $ALBUM_DATA;

	$value = $ALBUM_DATA[$namee];

	echo <<<EOT
	<tr>
    	<td width="40%" class="tableb">
			$text
        </td>
        <td width="60%" class="tableb" valign="top">
        	<input type="text" style="width: 100%" name="$namee" value="$value" class="textinput" />
		</td>
	</tr>

EOT;
}

function form_yes_no($text, $namee)
{
	global $ALBUM_DATA, $lang_yes, $lang_no;

	if ($namee == 'uploads' && USER_ADMIN_MODE){
		echo "	<input type=\"hidden\" name=\"$namee\" value=\"{$ALBUM_DATA['uploads']}\" />";
		return;
	}

	$value = isset($ALBUM_DATA[$namee]) ? $ALBUM_DATA[$namee] : false;
	$yes_selected = $value=='YES' ? 'selected' : '';
	$no_selected  = $value=='NO' ? 'selected' : '';

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$namee" class="listbox">
				<option value="YES" $yes_selected>$lang_yes</option>
				<option value="NO" $no_selected>$lang_no</option>
			</select>
		</td>
	</tr>

EOT;
}

function form_category($text, $namee)
{
	global $ALBUM_DATA, $CAT_LIST, $USER_DATA, $lang_modifyalb_php;

	if (!GALLERY_ADMIN_MODE || $ALBUM_DATA['category'] > FIRST_USER_CAT){
		echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<i>{$lang_modifyalb_php['user_gal']}</i>
			<input type="hidden" name="$namee" value="{$ALBUM_DATA['category']}" />
		</td>

EOT;
		return;
	}

	$CAT_LIST = array();
	$CAT_LIST[] = array(0, $lang_modifyalb_php['no_cat']);
	get_subcat_data(0,'');

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$namee" class="listbox">
EOT;
	foreach($CAT_LIST as $category){
		echo '<option value="'.$category[0].'"'.($ALBUM_DATA['category'] == $category[0] ? ' selected': '').">".$category[1]."</option>\n";
	}
	echo <<<EOT
			</select>
		</td>
	</tr>

EOT;
}

function form_textarea($text, $namee)
{
	global $ALBUM_DATA;

	$value = $ALBUM_DATA[$namee];

	echo <<<EOT
	<tr>
		<td class="tableb" valign="top">
			$text
		</td>
		<td class="tableb" valign="top">
			<textarea name="$namee" rows="5" cols="40" wrap="virtual" class="textinput" style="width: 100%;">{$ALBUM_DATA['description']}</textarea>
		</td>
	</tr>
EOT;
}

function form_alb_thumb($text, $namee)
{
	global $CONFIG, $ALBUM_DATA, $album, $lang_modifyalb_php;
	$album = secure( $album, 'num', 1, 'sec' );
	$results=$GLOBALS['db']->sql_query("SELECT `pid`, `filepath`, `filename`, `url_prefix` FROM `".TABLE_CPG_PICTURES."` WHERE `aid`='$album' AND `approved`='YES' ORDER BY `filename`");
	if ($GLOBALS['db']->sql_numrows($results) == 0) {
		echo <<<EOT
	<tr>
		<td class="tableb" valign="top">
			$text
		</td>
		<td class="tableb" valign="top">
			<i>{$lang_modifyalb_php['alb_empty']}</i>
			<input type="hidden" name="$namee" value="0" />
		</td>
	</tr>

EOT;
		return;
	}

	echo <<<EOT
<script type="text/JavaScript">
var Pic = new Array()

Pic[0] = 'modules/Coppermine/images/nopic.jpg'

EOT;

	$initial_thumb_url = 'modules/Coppermine/images/nopic.jpg';
	$img_list=array(0 => $lang_modifyalb_php['last_uploaded']);
	while($picture=$GLOBALS['db']->sql_fetchrow($results)){
		$thumb_url = get_pic_url($picture, 'thumb');
		echo "Pic[{$picture['pid']}] = '".$thumb_url."'\n";
		if ($picture['pid'] == $ALBUM_DATA[$namee]) $initial_thumb_url = $thumb_url;
		$img_list[$picture['pid']] = htmlspecialchars($picture['filename']);
	} // while
	echo <<<EOT

function ChangeThumb(index)
{
	document.images.Thumb.src = Pic[index]
}
</script>

EOT;
	$thumb_cell_height = $CONFIG['thumb_width'] + 17;
	echo <<<EOT
	<tr>
		<td class="tableb" valign="top">
			$text
		</td>
		<td class="tableb" align="center">
			<table cellspacing="0" cellpadding="5" border="0">
				<tr>
					<td width="$thumb_cell_height" height="$thumb_cell_height" align="center"><img src="$initial_thumb_url" name='Thumb' alt='' class='image' /><br /></td>
				</tr>
			</table>
			<select name="$namee" class="listbox" onChange="if(this.options[this.selectedIndex].value) ChangeThumb(this.options[this.selectedIndex].value);" onKeyUp="if(this.options[this.selectedIndex].value) ChangeThumb(this.options[this.selectedIndex].value);">

EOT;
			foreach($img_list as $pid => $pic_name){
				echo '				<option value="'.$pid.'"'.($pid == $ALBUM_DATA[$namee] ? ' selected':'').'>'.$pic_name."</option>\n";
			}
	echo <<<EOT
			</select>
		</td>
	</tr>

EOT;

}

function form_visibility($text, $namee)
{
	global $CONFIG, $USER_DATA, $ALBUM_DATA, $lang_modifyalb_php;

	if (!$CONFIG['allow_private_albums']) {
	    echo '<input type="hidden" name="'.$namee.'" value="0" />'."\n";
		return;
	}

	if (GALLERY_ADMIN_MODE) {
   		$options = array(0 => $lang_modifyalb_php['public_alb'],FIRST_USER_CAT + USER_ID => $lang_modifyalb_php['me_only']);
		if ($ALBUM_DATA['category'] > FIRST_USER_CAT) {
		    $result = $GLOBALS['db']->sql_query("SELECT `username` FROM `".TABLE_USERS."` WHERE `user_id`='".($ALBUM_DATA['category'] - FIRST_USER_CAT)."'");
			if ($GLOBALS['db']->sql_numrows($result)) {
			    $user = $GLOBALS['db']->sql_fetchrow($result);
				$options[$ALBUM_DATA['category']] = sprintf($lang_modifyalb_php['owner_only'], $user['username']);
			}
		}
		$result = $GLOBALS['db']->sql_query("SELECT `group_id`, `group_name` FROM `".TABLE_CPG_USERGROUPS."` WHERE 1");
		while($group = $GLOBALS['db']->sql_fetchrow($result)){
			$options[$group['group_id']] = sprintf($lang_modifyalb_php['groupp_only'], $group['group_name']);
		} // while
	} else {
		$options = array(
			0 => $lang_modifyalb_php['public_alb'],
			FIRST_USER_CAT + USER_ID => $lang_modifyalb_php['me_only'],
			$USER_DATA['group_id'] => sprintf($lang_modifyalb_php['groupp_only'], $USER_DATA['group_name'])
		);
	}
echo <<<EOT
	<tr>
		<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$namee" class="listbox">

EOT;
	foreach ($options as $value => $caption){
		echo '				<option value ="'.$value.'"'.($ALBUM_DATA['visibility'] == $value ? ' selected': '').'>'.$caption."</option>\n";
	}
echo <<<EOT
			</select>
		</td>
	</tr>

EOT;
}

function create_form(&$data)
{
	foreach($data as $element){
		if ((is_array($element))) {
		    switch($element[2]){
		    	case 0 :
		    		form_input($element[0], $element[1]);
		    		break;
		    	case 1 :
		    		form_yes_no($element[0], $element[1]);
		    		break;
		    	case 2 :
		    		form_category($element[0], $element[1]);
		    		break;
		    	case 3 :
		    		form_textarea($element[0], $element[1]);
		    		break;
		    	case 4 :
		    		form_alb_thumb($element[0], $element[1]);
		    		break;
		    	case 5 :
		    		form_visibility($element[0], $element[1]);
		    		break;
		    	default:
					cpg_die(CRITICAL_ERROR, 'Invalid action for form creation', __FILE__, __LINE__);
		    } // switch
		} else {
			form_label($element);
		}
	}
}

function alb_list_box()
{
	global $CONFIG, $album, $PHP_SELF;

	if (GALLERY_ADMIN_MODE) {
		$sql = "SELECT aid, IF(username IS NOT NULL, CONCAT('(', username, ') ', title), CONCAT(' - ', title)) AS title ".
			   "FROM `".TABLE_CPG_ALBUMS."` AS a ".
			   "LEFT JOIN `".TABLE_USERS."` AS u ON category = (".FIRST_USER_CAT." + user_id) ".
			   "ORDER BY title";
		$result = $GLOBALS['db']->sql_query($sql);
	} else {
		$result = $GLOBALS['db']->sql_query("SELECT `aid`, `title` FROM `".TABLE_CPG_ALBUMS."` WHERE `category` = '".(FIRST_USER_CAT + USER_ID)."' ORDER BY `title` ");
	}

	if ($GLOBALS['db']->sql_numrows($result) > 0 ){
		$lb = "<select name=\"album_listbox\" class=\"listbox\" onChange=\"if(this.options[this.selectedIndex].value) window.location.href='modules.php?name=Coppermine&op=modifyalb&album='+this.options[this.selectedIndex].value;\">\n";
		while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
			$selected = ($row['aid'] == $album) ? "SELECTED" : "";
			$lb .= "	<option value=\"" . $row['aid'] . "\" $selected>" . $row['title'] . "</option>\n";
		}
		$lb.= "</select>\n";
		return $lb;
	}
}


if (!isset($_GET['album'])) {
	if (GALLERY_ADMIN_MODE) {
	    $results = $GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_CPG_ALBUMS."` WHERE 1 LIMIT 1");
	} else {
	    $results = $GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_CPG_ALBUMS."` WHERE `category` = ".(FIRST_USER_CAT + USER_ID)." LIMIT 1");
	}
	if ($GLOBALS['db']->sql_numrows($results) == 0) cpg_die(ERROR, $lang_modifyalb_php['err_no_alb_to_modify'], __FILE__, __LINE__);
	$ALBUM_DATA = $GLOBALS['db']->sql_fetchrow($results);
	$album = $ALBUM_DATA['aid'];
} else {
	$album = secure( $_GET['album'], 'num', 1, 'sec' );
	$results = $GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_CPG_ALBUMS."` WHERE `aid`='$album'");
	if(!$GLOBALS['db']->sql_numrows($results)) cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
	$ALBUM_DATA = $GLOBALS['db']->sql_fetchrow($results);
}

$cat = $ALBUM_DATA['category'];
$actual_cat = $cat;

if (!GALLERY_ADMIN_MODE && $ALBUM_DATA['category'] != FIRST_USER_CAT + USER_ID) {
    cpg_die(ERROR, $lang_errors['perm_denied'], __FILE__, __LINE__);
}

pageheader(sprintf($lang_modifyalb_php['upd_alb_n'], $ALBUM_DATA['title']));
starttable("100%");

$album_lb =alb_list_box();

echo <<<EOT
	<tr>
		<td colspan="2" class="tableh1">
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
			<tr>
			<td class="statlink"><h2>{$lang_modifyalb_php['update']}</h2></td>
			<td align="right">$album_lb</td>
			</tr>
		</table>
		</td>
	</tr>
	<form method="post" action="modules.php?name=Coppermine&op=db_input">
	<input type="hidden" name="event" value="album_update" />
	<input type="hidden" name="albumid" value="$album" />

EOT;
create_form($data); // aid
echo <<<EOT
<tr>
	<td colspan="2" align="center" class="tablef">
	<input type="submit" class="button" value="{$lang_modifyalb_php['update']}" />
	</td>
	</form>
</tr>
EOT;

endtable();
pagefooter();
ob_end_flush();
incof();
break;
}