<?php
define('IN_COPPERMINE', true);
define('SUBSCRIBE_PHP', true); //language file
require('include/init.inc.php');

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

if(isset($_POST['dowhat']))
{

	switch ($_POST['dowhat']) {
	case "delete_all":
			if (isset($_POST['all_subscribtions'])) {
			cpg_db_query("DELETE FROM {$CONFIG['TABLE_NOTIFY']} WHERE user_id='".(USER_ID)."'");
			header('Location: '.$PHP_SELF);
			}
	   break;
	case "delete_selected":
		$mark_list=$_POST['list'];
		while(list($key, $value) = each($mark_list)) 
			if ($value) 
			{
			cpg_db_query("DELETE FROM {$CONFIG['TABLE_NOTIFY']} WHERE picture_id ='".$key."' AND user_id='".(USER_ID)."'");
			}
			header('Location: '.$PHP_SELF);
	   break;
	}

}

$result = cpg_db_query("SELECT count(*) FROM  {$CONFIG['TABLE_NOTIFY']} WHERE user_id=".(USER_ID));
list($tot_subscribtions) = @mysql_fetch_array($result);
if ($tot_subscribtions > 0){


pageheader($lang_subscribe['manage']);
echo <<<EOT
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
//  End -->
</script>


<form action="subscribtion_manage.php" method="post">


<table class="maintable" cellpadding="6" cellspacing="1" border="0" width="100%" align="center">
	<tr>
		<td class="tableh1" colspan="2" style="padding: 6px 0 6px 6px"><strong>$lang_subscribe[stats]</strong></td>
	</tr>
	<tr>
		<td class="panelsurround" align="center">
		<span style="float: left">$lang_subscribe[cur_sub_text] $tot_subscribtions $lang_subscribe[pictures]!</span><br /><br />
		<div class="panel">
			<div style="width:480px" align="left">
			
			<fieldset class="fieldset">
				<legend>$lang_subscribe[unsubscribe_all]</legend>
				<table cellpadding="0" cellspacing="3" border="0">
				<tr>
					<td>$lang_subscribe[remove]</td>
				</tr>
				<tr>
					<td><label for="deletesub"><input type="checkbox" name="all_subscribtions" id="all_subscribtions" value="true" />$lang_subscribe[remove_submit]</label></td>
				</tr>
				</table>
			</fieldset>
			
			</div>
		</div>
		
		<div style="margin-top:6px">
			<input type="hidden" name="dowhat" value="delete_all" />
			<input type="submit" class="button" value="$lang_subscribe[remove_submit]" accesskey="s" />
		</div>
		</td>
	</tr>
</table>

</form>


<form action="subscribtion_manage.php" method="post" name="form">
<table class="maintable" cellpadding="0" cellspacing="1" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tableh1 graybox" colspan="4" style="padding: 6px 0 6px 6px">
			<span class="smallfont" style="float:right">
				<label for="checkall_all">
				$lang_subscribe[check]: <strong>$tot_subscribtions</strong>
				<input type="checkbox" name="allbox" id="checkall_all" title="Mark / unmark all messages" onclick="this.value=check(this.form)" />
				</label>			
			</span>
			<strong>$lang_subscribe[manage]</strong><span>:</span>			
		</td>
	</tr>
</thead>
<tr><td>
<table class="tborder" cellpadding="6" cellspacing="1" border="0" width="100%" align="center">
EOT;
?> <?php // just for editor display reasons
//tabs
    $sort_codes = array(
        'title_a' => 'title ASC',
        'title_d' => 'title DESC',
		'owner_name_a' => 'owner_name ASC',
        'owner_name_d' => 'owner_name DESC',
		'image_name_a' => 'filename ASC',
	    'image_name_d' => 'filename DESC',
		'date_a' => 'ctime ASC',
        'date_d' => 'ctime DESC',
        );

    $sort = (!isset($_GET['sort']) || !isset($sort_codes[$_GET['sort']])) ? 'title_a' : $_GET['sort'];

    $tab_tmpl = array('left_text' => '<td width="100%%" align="left" valign="middle" class="tableh1_compact" style="white-space: nowrap"><b>' . $lang_subscribe['u_subscr_on_p_pages'] . '</b></td>' . "\n",
        'tab_header' => '',
        'tab_trailer' => '',
        'active_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%d</b></td>',
        'inactive_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%d&sort=' . $sort . '"<b>%d</b></a></td>' . "\n",
		'active_next_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%s</b></td>',
		'inactive_next_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%s&sort=' . $sort . '"<b>Next</b></a></td>',
		'active_prev_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%s</b></td>',
		'inactive_prev_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%s&sort=' . $sort . '"<b>Prev</b></a></td>'
		);

    $subscr_per_page = 25;
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    $lower_limit = ($page-1) * $subscr_per_page;
    $total_pages = ceil($tot_subscribtions / $subscr_per_page);


//end tabs setup

$sql = 	"SELECT p.pid ,p.owner_id, p.owner_name, p.title, p.caption, p.filepath, p.filename, p.ctime, ".
		"u.user_id, u.picture_id".
		" FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_NOTIFY']} AS u ON pid=u.picture_id WHERE u.mail_sent=1 AND u.user_id=".(USER_ID).
		" ORDER BY " . $sort_codes[$sort] . " ".
        "LIMIT $lower_limit, $subscr_per_page;";


$tabs = create_tabs($tot_subscribtions, $page, $total_pages, $tab_tmpl);

$lb = "<select name=\"album_listbox\" class=\"listbox\" onChange=\"if(this.options[this.selectedIndex].value) window.location.href='$PHP_SELF?page=$page&sort='+this.options[this.selectedIndex].value;\">\n";
	foreach($sort_codes as $key => $value) {
		  $selected = ($key == $sort) ? "SELECTED" : "";
		  $lb .= "        <option value=\"" . $key . "\" $selected>" . $lang_subscribe[$key] . "</option>\n";
	}
$lb .= "</select>\n";

		
$result = cpg_db_query($sql);
$total= @mysql_num_rows($result);
while($i=mysql_fetch_array($result))
{

$pid=$i['pid'];

$this_date=localised_date($i['ctime'], $comment_date_fmt);	
if ($CONFIG['enable_mini_thumbs']){


$mime_content = cpg_get_type($i['filename']);
if ($mime_content['content'] != 'image' ) {

$preview = <<<EOT
<a href="displayimage.php?pos=-$i[pid]"><img src="{$THEME_DIR}images/thumb_mini_movie.gif" width="{$CONFIG['mini_thumb_width']}" height="{$CONFIG['mini_thumb_height']}" alt=""  class="image" /></a>
EOT;
}

else {
$preview = <<<EOT
<a href="displayimage.php?pos=-$i[pid]"><img src="{$CONFIG['fullpath']}$i[filepath]{$CONFIG['mini_pfx']}$i[filename]" width="{$CONFIG['mini_thumb_width']}" height="{$CONFIG['mini_thumb_height']}" alt=""  class="image" /></a>
EOT;
}
}
else $preview = "&nbsp;&nbsp;&nbsp;";
echo <<<EOT

	<tr>
	<td class="alt1"><img src="{$THEME_DIR}images/buddy_ok.gif" alt="" border="0" /></td>
	<td class="alt2">$preview</td>
	<td class="alt1Active" id="pmid" width="100%">

		<div>
			<span style="float:right" class="smallfont">$this_date</span>
			$lang_subscribe[title] <a href="displayimage.php?pos=-$i[pid]">$i[title]</a>
		</div>
		<div class="smallfont">
			<span style="float:right" class="time">$i[filename]</span>
			$lang_subscribe[uploader] <span style="cursor:pointer" onclick="window.location='profile.php?uid=$i[owner_id]';">$i[owner_name]</span>
		</div>
	
	</td>
	<td class="alt2" align="center" style="padding:0px"><input type="checkbox" id="checkall" name="list[$pid]" value="sub[$pid]" /></td>
</tr>

EOT;

}

echo <<<EOT
</tbody></table>
<table class="tableb" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<tr><td><br /></td></tr>
</table>

<table class="tablef graybox" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tablef" align="left" colspan="2">			
			<div class="smallfont">
				$lang_subscribe[selected]
				<select name="dowhat">
					<option value="do_nothing">$lang_subscribe[do_nothing]</option>
					<option value="delete_selected">$lang_subscribe[delete_selected]</option>
				</select>
				<input type="submit" class="button" value="$lang_subscribe[go]" />
			</div>		
		</td></form>
		<form method="post" action="$PHP_SELF?op=new_user">
		<td class="tablef" align="right" colspan="2">
			<div class="smallfont">
				$lang_subscribe[sort_by] $lb
			</div>		
		</td></form>
	</tr>
</thead>
      <tr>
              <td colspan="4" style="padding: 0px;">
                      <table width="100%" cellspacing="0" cellpadding="0">
                              <tr>
                                      $tabs
                              </tr>
                      </table>
              </td>
      </tr>
</table>




EOT;

}
else {
pageheader($lang_subscribe['manage']);
echo <<<EOT
<table class="maintable" cellpadding="6" cellspacing="1" border="0" width="90%" align="center">
	<tr>
		<td class="tableh1" colspan="2" style="padding: 6px 0 6px 6px"><strong>$lang_subscribe[stats]</strong><span class="normal">: $lang_subscribe[no]</span></td>
	</tr>
	<tr>
		<td class="panelsurround" align="center">
		<span style="float: left">$lang_subscribe[cur_sub_text] $tot_subscribtions $lang_subscribe[pictures]!</span><br /><br />
		<div class="panel">
			<div style="width:480px" align="left">
			
			<fieldset class="fieldset">
				<table cellpadding="0" cellspacing="3" border="0">
				<tr>
					<td>$lang_subscribe[sub_first]</td>
				</tr>
				</table>
			</fieldset>
			
			</div>
		</div>
		


EOT;
} 
endtable();
pagefooter();
ob_end_flush();


?>