<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Modified for EXOOPS by MAALBAK //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.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.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
include("admin_header.php");
include("../../../mainfile.php");
include(XOOPS_ROOT_PATH."/modules/myalbum/cache/config.php");
include(XOOPS_ROOT_PATH."/modules/myalbum/include/functions.php");
include_once(XOOPS_ROOT_PATH."/class/xoopstree.php");
include_once(XOOPS_ROOT_PATH."/class/module.errorhandler.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
include_once(XOOPS_ROOT_PATH."/class/xoopscomments.php");
include_once(XOOPS_ROOT_PATH."/class/xoopslists.php");
include(XOOPS_ROOT_PATH."/class/upload.class.php");
global $myts, $db, $bcoosConfig, $xoopsModule;
// MyTextSanitizer object
$eh = new ErrorHandler; //ErrorHandler object
$mytree = new XoopsTree($db->prefix("myalbum_cat"),"cid","pid");
function update($lid, $cid, $title, $desc, $valid, $ext = "", $x = "", $y = "") {
	global $myts, $db, $bcoosConfig, $xoopsModule;
	if($ext == "") {
		$db->query("UPDATE ".$db->prefix("myalbum_photos")."
			SET cid='$cid',title='$title', status='$valid', date=".time()."
			WHERE lid='$lid'");
	} else {
		$db->query("UPDATE ".$db->prefix("myalbum_photos")."
			SET cid='$cid',title='$title', status='$valid', date=
			".time().", ext='$ext',res_x='$x',res_y='$y'
			WHERE lid='$lid'");
	}
	$db->query("UPDATE ".$db->prefix("myalbum_text")." SET
		description='$desc' WHERE lid=".$lid."");
	redirect_header("photo.php?lid=$lid",0);
}
if(isset($delete) and $delete != "") {
	$delete = $myts->makeTareaData4Save($delete);
	$result = $db->query("SELECT l.lid, l.cid, l.title, l.ext, l.res_x, l.res_y, l.status, l.date, l.hits, l.rating, l.votes, l.comments, t.description FROM ".$db->prefix("myalbum_photos")." l, ".$db->prefix("myalbum_text")." t where l.lid=t.lid and l.lid=$delete ORDER BY date DESC",$mylinks_newlinks,0);
	list($lid, $cid, $ltitle, $ext, $res_x, $res_y, $status, $time, $hits, $rating, $votes, $comments, $description)=$db->fetchRow($result);
	$q = "DELETE FROM ".$db->prefix("myalbum_photos")." WHERE lid = $delete";
	$db->query($q) or $eh->show("0013");
	$q = "DELETE FROM ".$db->prefix("myalbum_text")." WHERE lid = $delete";
	$db->query($q) or $eh->show("0013");
	$q = "DELETE FROM ".$db->prefix("myalbum_votedata")." WHERE lid = $delete";
	$db->query($q) or $eh->show("0013");
	// delete comments for this photo
	$com = new XoopsComments($db->prefix("myalbum_comments"));
	$criteria = array("item_id=$delete", "pid=0");
	$commentsarray = $com->getAllComments($criteria);
	foreach($commentsarray as $comment){
		$comment->delete();
	}
	if(is_numeric($delete)) { // last security check
		unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/$delete.$ext");
		unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$delete.$ext");
	}
	redirect_header("index.php?op=linksConfigMenu",2,_MD_DELETINGPHOTO);
	exit();
}
if(isset($_POST['submit']) && $_POST['submit'] != ""){
	if(!$xoopsUser){
		redirect_header(XOOPS_URL."/user.php",2,_MUSTREGISTERFIRST);
		exit();
	}
   	if(!isset($_POST['submitter']) || $_POST['submitter']=="") {
   		$submitter = $xoopsUser->uid();
 	} else {
		$submitter = $_POST['submitter'];
	}
	if(isset($_POST['delete'])) {
		xoops_cp_header();
		OpenTable();
		echo "<h4 style='text-align:left;'>"._MD_PHOTODEL." $delete</h4>\n";
		echo "<table><tr><td>\n";
		echo myTextForm("photo.php?delete=$lid", _YES);
		echo "</td><td>\n";
		echo myTextForm("photo.php?lid=$lid", _NO);
		echo "</td></tr></table>\n";
		CloseTable();
		xoops_cp_footer();
		exit();
	}
// Check if Title exist
    if ($_POST["title"]=="") {
    	$eh->show("1001");
    }
// Check if Photo exist
//	$file = $_POST["uploadFileName"][0];
//   	if ($file=="" || !isset($file)) {
//        	$eh->show("7000");
//    	}
// Check if Description exist
    if ($_POST['description']=="") {
       	$eh->show("1008");
    }
	if ( !empty($_POST['cid']) ) {
    	$cid = $_POST['cid'];
	} else {
		$cid = 0;
	}
	if(isset($_POST['valid'])) {
		$valid = 1;
	} else {
		$valid = 0;
	}
	//echo "<h1>".$GLOBALS['uploadFileName'][0]."<br />".$_FILES[$field]['tmp_name']."<br />".$_FILES[$field]['name']."</h1>";
	if ( !empty($_FILES['image']['name']) ) {
		$upload = new Upload();
		$upload->setAllowedMimeTypes(".gif|.jpeg|.jpg|.png");
		$upload->setMaxImageSize($myalbum_width, $myalbum_heigth);
		$upload->setMaxFileSize($myalbum_fsize);
		$tmp_name = 'tmp_'.rand();
		$upload->setFileName($tmp_name);
;		$upload->setDestinationFileName("image");
		$upload->setUploadPath(XOOPS_ROOT_PATH."/modules/myalbum/photos");
		$result = $upload->doUpload();
		if ( $result ) {
			$title = $myts->makeTboxData4Save($_POST["title"]);
   			$description = $myts->makeTareaData4Save($_POST["description"]);
			$ext = $upload->getFileExtension();
			$dim = GetImageSize(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext");
			if(is_numeric($lid)) { // last security check
				unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/$lid.$ext");
				unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/thumbs/$lid.$ext");
			}
			createBig(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext", $lid, $ext);
			createThumb(XOOPS_ROOT_PATH."/modules/myalbum/photos/$lid.$ext", $lid, $ext);
			unlink(XOOPS_ROOT_PATH."/modules/myalbum/photos/$tmp_name.$ext");
			$dim1 = GetImageSize(XOOPS_ROOT_PATH."/modules/myalbum/photos/$lid.$ext");
			update($lid, $cid, $title, $description, $valid, $ext, $dim1[0], $dim1[1]);
			exit();
		} else {
			$errors = $upload->getUploadErrors();
			xoops_cp_header();
   			OpenTable();
    		echo "<p><strong>::Errors occured::</strong><br />\n";
			while(list($filename,$values) = each($errors)) {
				"File: " . print $filename . "<br />";
				$count = count($values);
				for($i=0; $i<$count; $i++) {
					echo "==>" . $values[$i] . "<br />";
				}
			}
			echo "</p>";
			CloseTable();
		    xoops_cp_footer();
       		exit();
		}
	} else {  //update without file upload
		$title = $myts->makeTboxData4Save($_POST["title"]);
		$description = $myts->makeTareaData4Save($_POST["description"]);
    	$cid = $_POST['cid'];
		update($lid, $cid, $title, $description, $valid);
	}
} else {
	if(!$xoopsUser){
		redirect_header(XOOPS_URL."/user.php",2,_MUSTREGISTERFIRST);
		exit();
	}
	xoops_cp_header();
   	OpenTable();
	$result = $db->query("SELECT l.lid, l.cid, l.title, l.ext, l.res_x, l.res_y, l.status, l.date, l.hits, l.rating, l.votes, l.comments, t.description FROM ".$db->prefix("myalbum_photos")." l, ".$db->prefix("myalbum_text")." t where l.lid=t.lid and l.lid=$lid ORDER BY date DESC",$mylinks_newlinks,0);
	list($lid, $cid, $ltitle, $ext, $res_x, $res_y, $status, $time, $hits, $rating, $votes, $comments, $description)=$db->fetchRow($result);
	include_once("../../../class/xoopsformloader.php");
	$form = new XoopsThemeForm(_MD_PHOTOUPLOAD, "uploadavatar", "photo.php?lid=$lid");
	$form->setExtra("enctype='multipart/form-data'");
	$question_text = new XoopsFormText(_TITLE, "title", 50, 255,$ltitle);
	$cat = new XoopsFormSelect(_CATEGORY, "cid", $cid);
	$tree = $mytree->getChildTreeArray();
	foreach ($tree as $leaf ) {
		$leaf['prefix'] = substr($leaf['prefix'], 0, -1);
		$leaf['prefix'] = str_replace(".","--",$leaf['prefix']);
		$cat -> addOption($leaf['cid'],$leaf['prefix'].$leaf['title']);
	}
	$desc_tarea = new XoopsFormTextarea(_DESCRIPTION, "description", $description);
	$file_form = new XoopsFormFile(_MD_SELECTFILE, "image", $myalbum_fsize);
	$op_hidden = new XoopsFormHidden("op", "submit");
	$counter_hidden = new XoopsFormHidden("fieldCounter", 1);
	if($status == 1)
		$status = "";
	$valid_box = new XoopsFormCheckBox(_MD_VALIDPHOTO, "valid", array($status));
	$valid_box -> addOption($status," ");
	$delete_box = new XoopsFormCheckBox(_MD_DELETEPHOTO, "delete");
	$delete_box -> addOption($status," ");
	$submit_button = new XoopsFormButton("", "submit", _SUBMIT, "submit");
	print "<center><img src=../photos/thumbs/$lid.$ext></center>";
	$form->addElement($question_text);
	$form->addElement($desc_tarea);
	$form->addElement($cat);
	$form->addElement($file_form);
	$form->addElement($counter_hidden);
	$form->addElement($op_hidden);
	$form->addElement($valid_box);
	$form->addElement($delete_box);
	$form->addElement($submit_button);
//	$form->setRequired("avatarfile");
	$form->display();
   	CloseTable();
}
	xoops_cp_footer();
?>
