<?php
/*
   +----------------------------------------------------------------------+
   | Admin Page                                                           |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox 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 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox 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 Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Controls display and form submissions for the configure/admin page   |
   +----------------------------------------------------------------------+
*/

require_once 'config/global.inc.php';
require_once 'Photo.inc.php';
require_once 'Authenticate.inc.php';
require_once 'Album.inc.php';
require_once 'AlbumCategory.inc.php';

if ($_USER['level'] < $CONFIG['permission_view_activity']) {
	header('Location: /');
	exit(0);
}

if ($s === 'status') {
	$smarty->display($CONFIG['smarty_templates'].'/admin_status.tpl');
} elseif ($s === 'activity') {
	$log_list = Photo::getActivity(false,1000);
	$smarty->assign('log_list',$log_list);
	$smarty->display($CONFIG['smarty_templates'].'/admin_activity.tpl');

} elseif ($s === 'users') {

	$levels_list = Authenticate::getLevelsList();
	$smarty->assign('levels_list',$levels_list);

	#Add a new user
	if (isset($_POST['first']) AND isset($_POST['last']) AND isset($_POST['email'])) {
		$a = new Authenticate();
		$data = Array('first'	=>	$_POST['first'],
			      'last'	=>	$_POST['last'],
			      'email'	=>	$_POST['email'],
			      'level'	=>	$_POST['level']);
		$a->setInfo($data);
		if ($a->commit()) {
			header('Location: '.$_SERVER['REQUEST_URI']);
			exit(0);
		}
	}
	#Perform user-based updates
	if (isset($_POST['id'])) {
		$id = substr($_POST['id'],0,32);
		$type = substr($_POST['id'],33);

		#Delete a user
		if ($type === 'delete') {
			$a = new Authenticate($id);
			if ($a->delete()) {
				header('Location: '.$_SERVER['REQUEST_URI']);
	                        exit(0);
			}
		}

		#Update First Name
                if ($type === 'first') {
                        $a = new Authenticate($id);
                        $a->setInfo(Array('first' => $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update E-Mail
                if ($type === 'last') {
                        $a = new Authenticate($id);
                        $a->setInfo(Array('last' => $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update E-Mail
		if ($type === 'email') {
			$a = new Authenticate($id);
			$a->setInfo(Array('email' => $_POST['value']));
			if ($a->commit()) {
				print $_POST['value'];
				exit(0);
			}
		}

		#Update Levels
                if ($type === 'level') {
                        $a = new Authenticate($id);
                        $a->setInfo(Array('level' => $_POST['value']));
                        if ($a->commit()) {
                                print $_POST['value'];
                                exit(0);
                        }
                }

		#Enable or disable a user
		if ($type === 'enabled') {
                        $a = new Authenticate($id);
                        $a->setInfo(Array('enabled' => $_POST['value']));
                        if ($a->commit()) {
                                print $_POST['value'];
                                exit(0);
                        }
                }

	}

	$user_list = Authenticate::getUsers();
	$smarty->assign('user_list',$user_list);
	$smarty->display($CONFIG['smarty_templates'].'/admin_users.tpl');

} elseif ($s === 'albums' OR $s === 'rolls') {

	if ($s === 'albums') {        
		#Add a new album
		if (isset($_POST['title']) AND isset($_POST['type']) AND isset($_POST['level'])) {
			$a = new Album();
			$data = Array('title'   =>      $_POST['title'],
				      'type'    =>      $_POST['type'],
				      'level'   =>      $_POST['level']);
			$a->setInfo($data);
			if ($a->commit()) {
				header('Location: '.$_SERVER['REQUEST_URI']);
				exit(0);
			}
		}
	}

        #Perform user-based updates
        if (isset($_POST['id']) AND $_POST['id']) {
                $id = substr($_POST['id'],0,32);
                $type = substr($_POST['id'],33);

                #Edit the title
                if ($type === 'title' AND $s === 'albums') {
                        $a = new Album($id);
			$a->setInfo(Array('title'	=>	$_POST['value']));
			if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update album type
                if ($type === 'type' AND $s === 'albums') {
                        $a = new Album($id);
                        $a->setInfo(Array('type'       =>      $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update access level
                if ($type === 'level') {
                        $a = new Album($id);
                        $a->setInfo(Array('level'       =>      $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update creation date 
                if ($type === 'timestamp_created') {
                        $a = new Album($id);
                        $a->setInfo(Array('timestamp_created'       =>      $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Update modification date
                if ($type === 'timestamp_modified') {
                        $a = new Album($id);
                        $a->setInfo(Array('timestamp_modified'       =>      $_POST['value']));
                        if ($a->commit()) {
                                print stripslashes($_POST['value']);
                                exit(0);
                        }
                }

		#Delete an album
                if ($type === 'delete' AND $s === 'albums') {
                        $a = new Album($id);
                        if ($a->delete()) {
                                header('Location: '.$_SERVER['REQUEST_URI']);
                                exit(0);
                        }
                }

	}

	$levels_list = Authenticate::getLevelsList();
        $smarty->assign('levels_list',$levels_list);

	$type_list = AlbumCategory::getCategoryList('norolls');
	$smarty->assign('type_list',$type_list);

	if ($s === 'albums') {
		$album_list = Album::getCustomAlbums(false,false);
	} else {
		$album_list = Album::getAlbumList(1);
	}

	$smarty->assign('album_list',$album_list);

        $smarty->display($CONFIG['smarty_templates'].'/admin_albums.tpl');

} elseif ($s === 'categories') {
	#Delete an album
        if (isset($_POST['delete'])) {
                $ac = new AlbumCategory($_POST['type']);
                if ($ac->delete()) {
                        header('Location: '.$_SERVER['REQUEST_URI']);
                        exit(0);
                }
        }

	#AJAX Edit in Place
	elseif (isset($_POST['id'])) {
		$ac = new AlbumCategory($_POST['id']);
                $data = Array('title'   =>      $_POST['value']);
                $ac->setInfo($data);
                if ($ac->commit()) {
			print stripslashes($_POST['value']);
                        exit(0);
                }
	}

        #Add a new album
        elseif (isset($_POST['title']) OR isset($_POST['type'])) {
                $ac = new AlbumCategory();
                $data = Array('title'   =>      $_POST['title']);
                $ac->setInfo($data);
                if ($ac->commit()) {
                        header('Location: '.$_SERVER['REQUEST_URI']);
                        exit(0);
                }

	}

        $category_list = AlbumCategory::getCategoryList();
        $smarty->assign('category_list',$category_list);

        $smarty->display($CONFIG['smarty_templates'].'/admin_categories.tpl');


} elseif ($s === 'maintenance') {
	if (isset($_POST['operation']) AND $_POST['operation']) {
		$smarty->display($CONFIG['smarty_templates'].'/admin_status.tpl');
		$p = new Photo();
		switch($_POST['operation']) {
			case '1':
				$p->cleanPhotos('1',true);
				print "Photos deleted";
				exit(0);
			case '2':
				$p->cleanPhotos('2',true);
				exit(0);
			case '3':
				$p->cleanPhotos('3',true);
				exit(0);
			case '4':
				$p->cleanPhotos('4',true);
				exit(0);
			case '5':
				$p->cleanPhotos('5',true);
				exit(0);
			case '6':
				$p->cleanPhotos('6',true);
				exit(0);
			case '7':
				$p->cleanPhotos('7',true);
				exit(0);
			case '8':
                                $p->cleanPhotos('8',true);
                                exit(0);
			case '9':
                                $p->cleanPhotos('9',true);
                                exit(0);
			case '10':
                                $p->cleanPhotos('10',true);
                                exit(0);
			case '11':
                                $p->cleanPhotos('11',true);
                                exit(0);
			case '12':
                                $p->cleanPhotos('12',true);
                                exit(0);
			case '13':
				$p->cleanPhotos('13',true);
				exit(0);
                        case '14':
                                $p->cleanPhotos('14',true);
                                exit(0);
                        case '15':
                                $p->cleanPhotos('15',true);
                                exit(0);
                        case '16':
                                $p->cleanPhotos('16',true);
                                exit(0);
			default:
				exit(0);
		}
	} else {
	        $smarty->display($CONFIG['smarty_templates'].'/admin_maintenance.tpl');
	}

} elseif ($s === 'discover') {

	if (isset($_POST['operation']) AND $_POST['operation']) {
		$smarty->display($CONFIG['smarty_templates'].'/admin_status.tpl');
		$photo= new Photo();
                switch($_POST['operation']) {
                        case 'quick':
				$photo->discoverMedia(false,true,true);
                		$photo->cleanPhotos(5);
                                print "Quick scan finished";
                                exit(0);
			case 'full':
				$photo->discoverMedia(false,false,true);
				$photo->cleanPhotos(5);
                                print "Full scan finished";
                                exit(0);
			default:
				exit(0);
		}
	} else {
		$smarty->display($CONFIG['smarty_templates'].'/admin_discover.tpl');
	}

} else {
	#header('Location: /');
	exit(0);
}
?>
