<?php
/**************************************************
  CPG Photo Shop Plugin for Coppermine Photo Gallery
  *************************************************
  Copyright (c) 2006 Thomas Lange <stramm@gmx.net>
  *************************************************
  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.
  *************************************************
  Coppermine version: 1.4.5
  Photo Shop version: 1.0
  $Revision: 1.0 $
  $Author: stramm $
***************************************************/
define('PHOTOSHOP_ADMIN_PHP', true);

require('include/init.inc.php');

if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

if (!(GALLERY_ADMIN_MODE))	cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

$op = @$_GET['op'];
switch ($op) {
    case 'moveu':
		$thisitem = @$_GET['p'];
		$thispriority = @$_GET['priority'];
        if (isset($thisitem) && ($thispriority > 0)) {
			
            // Move the item above down
            $sql = 'update '.$CONFIG['TABLE_SHOP_CONFIG'].' set priority='.($thispriority).' where priority='.($thispriority-1).';';
            cpg_db_query($sql);

            // Move this item up
            $sql = 'update '.$CONFIG['TABLE_SHOP_CONFIG'].' '.
                   'set priority='.($thispriority-1).' where id='.$thisitem.';';
            cpg_db_query($sql);
			
        }
    	photoshop_refresh($_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config');
        break;
    case 'moved':
		$thisitem = @$_GET['p'];
		$thispriority = @$_GET['priority'];
        if (isset($thisitem) && ($thispriority) < (count($SHOP_CONFIG)-1)) {

            // Move the item below up
            $sql = 'update '.$CONFIG['TABLE_SHOP_CONFIG'].' set priority='.($thispriority).' where priority='.($thispriority+1).';';
            cpg_db_query($sql);

            // Move this item down
            $sql = 'update '.$CONFIG['TABLE_SHOP_CONFIG'].' '.
                   'set priority='.($thispriority+1).' where id='.$thisitem.';';
            cpg_db_query($sql);
        }
    	photoshop_refresh($_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config');
        break;
    case 'delete':
		$thisitem = @$_GET['p'];
		$thispriority = @$_GET['priority'];
        if (isset($thisitem) && isset($thispriority)) {

            // Delete the item
            $sql = 'DELETE FROM '.$CONFIG['TABLE_SHOP_CONFIG'].' where id='.$thisitem.';';
            cpg_db_query($sql);

			if ($thispriority < (count($SHOP_CONFIG))){
	            // Move up all 
	            $sql = 'update '.$CONFIG['TABLE_SHOP_CONFIG'].' '.
	                   'set priority=priority-1  where priority > '.$thispriority.';';
	            cpg_db_query($sql);
			}
        }
    	photoshop_refresh($_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config');
        break;


}

if (isset($_POST) && count($_POST)) {
	if (isset($_POST['new_item'])) {
		$result = cpg_db_query("SELECT max(priority) FROM {$CONFIG['TABLE_SHOP_CONFIG']}");
		$temp = mysql_fetch_row($result);
		$priority = (is_numeric($temp[0])) ? ++$temp[0] : '';
		mysql_free_result($result);
        cpg_db_query("INSERT INTO {$CONFIG['TABLE_SHOP_CONFIG']} (priority) VALUES ('{$priority}')");
    	photoshop_refresh($_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config');
    } elseif (isset($_POST['apply_modifs'])) {
        photoshop_process_post_data();
    }
}

pageheader($lang_photoshop_config['title']);

$signature = 'Coppermine Photo Gallery ' . COPPERMINE_VERSION . ' ('. COPPERMINE_VERSION_STATUS . ')';

?>
<script type="text/javascript">
        onload = hideall;
</script>
<?php
echo "<form action=\"".$_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config'."\" method=\"post\">";
starttable('100%', "{$lang_photoshop_config['title']} - $signature", 7);

photoshop_config_create_form();


echo <<<EOT
                <tr>
                        <td align="left" class="tablef" colspan="7">
                            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                    <td width="100%" align="center">
                                        <input type="submit" class="button" name="apply_modifs" value="{$lang_photoshop_config['save_cfg']}" />
                                        <input type="submit" class="button" name="new_item" value="{$lang_photoshop_config['new_item']}" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                </tr>
EOT;
endtable();
echo '</form>';
pagefooter();
ob_end_flush();

//functions
function photoshop_config_create_form() {
	
	global $SHOP_CONFIG, $lang_photoshop_config;
	echo "<tr><td>{$lang_photoshop_config['type']}</td><td>{$lang_photoshop_config['name']}</td><td>{$lang_photoshop_config['price']}</td><td>{$lang_photoshop_config['max_items']}</td><td width=10% colspan=3 align=center>Order</td></tr>\n";
	foreach($SHOP_CONFIG as $key => $value) {

		 if ($value['type'] == 'photo') { //we 
		 	photoshop_config_create_form_image($value);
		} else {
		 	photoshop_config_create_form_image($value);
		}
	} 
}

function photoshop_config_create_form_image($data) {
	global $SHOP_CONFIG;

	$link = $_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config';
	echo "<tr>\n";
	echo "<input type=\"hidden\" name=\"item_id[]\" value=\"{$data['id']}\" />";
	foreach($data as $key => $value) {
		if ((!is_numeric($key))  && ($key != "id")) {
			if ($key == "type") {
            	$photo_selected = ($value == 'photo') ? 'selected="selected"' : '';
            	$cd_selected = ($value == 'cd') ? 'selected="selected"' : '';
		    	echo <<<EOT
                        <td width="10%" class="tableb" valign="top">
                            <select name="{$key}_{$data['id']}"/>
								<option value="photo" {$photo_selected}>Photo</option>
								<option value="cd" {$cd_selected}>CD</option>
								
							</select>
                        </td>

EOT;
			} elseif ($key == "priority") {
		
        if ($value > 0 && count($data) > 1) {
            echo <<<EOT
            <td class="tableb" width="2%" align="center" valign="middle">
                <a href="{$link}&amp;op=moveu&amp;p={$data['id']}&amp;priority={$value}"><img src="images/up.gif"  border="0" alt="" /></a>
            </td>
EOT;
        } else {
            echo '<td class="tableb" width="2%"><img src="images/spacer.gif" width="16" height="16" /></td>';
        }

        if (($value) < (count($SHOP_CONFIG)-1)) {
            echo <<<EOT
            <td class="tableb" width="2%" align="center" valign="middle">
                <a href="{$link}&amp;op=moved&amp;p={$data['id']}&amp;priority={$value}"><img src="images/down.gif"  border="0" alt="" /></a>
            </td>
EOT;
        } else {
            echo '<td class="tableb" width="2%"><img src="images/spacer.gif" width="16" height="16" /></td>';
        }

        echo <<<EOT
            <td class="tableb" width="2%" align="center" valign="middle">
                <a href="{$link}&amp;op=delete&amp;p={$data['id']}&amp;priority={$value}">
                    <img src="images/delete.gif"  border="0" alt="" />
                </a>
            </td>
EOT;

			} else {
		    echo <<<EOT
                        <td width="28%" class="tableb" valign="top">
                            <input type="text" class="textinput" maxlength="40" style="width: 100%" name="{$key}_{$data['id']}" value="{$value}"/>
                        </td>
EOT;
			}
		}
	}
	echo "<td></td></tr>\n";
	
}


function photoshop_process_post_data()
{
    global $CONFIG;

    $field_list = array('type', 'name', 'price', 'max_items');

    $item_id_array = photoshop_get_post_var('item_id');
    foreach ($item_id_array as $key => $item_id) {
        $set_statment = '';
        foreach ($field_list as $field) {
            if ($field == 'type' || $field == 'name' ) {
                $set_statment .= $field . "='" . addslashes($_POST[$field . '_' . $item_id]) . "',";
            } else {
                $set_statment .= $field . "='" . $_POST[$field . '_' . $item_id] . "',";
            }
        }
        $set_statment = substr($set_statment, 0, -1);
        cpg_db_query("UPDATE {$CONFIG['TABLE_SHOP_CONFIG']} SET $set_statment WHERE id = '$item_id' LIMIT 1");
    }
    	photoshop_refresh($_SERVER['PHP_SELF'].'?file=photo_shop/photo_shop_config');
			
}

?>