<?php

/*
   +----------------------------------------------------------------------+
   | Album Category Class                                                 |
   | 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/>.	  |
   +----------------------------------------------------------------------+
   | This file is for editing album categories such as general, trips,	  |
   | places, etc ..							  |
   +----------------------------------------------------------------------+
	function AlbumCategory ($string=false) {
        function setInfo ($data) {
        function commit(){
        function delete() {
        function getCategoryList($string=false) {
        function _getInfo() {
	function _getNewType() {
   +----------------------------------------------------------------------+
*/

require_once 'Log.inc.php';

class AlbumCategory {

	var $title;
	var $type;
	var $new_album_category;
	var $info;

	/*****************************************************************
	 * Constructor
         *****************************************************************/
	function AlbumCategory ($string=false) {
		if (is_numeric($string)){
			$this->type = $string;
			$this->_getInfo();
		} elseif ($string) {
			$this->title = $string;
			$this->_getInfo();
		} else {
			$this->new_album_category = true;
		}
	}

	/*****************************************************************
	 * setInfo
         *****************************************************************/
        function setInfo ($data) {
	global $CONFIG;
	global $ERROR;

		if ($this->album_new_category) {

			#ERROR Checking
			if (empty($data['title'])) {
				$ERROR['title'] = 'Title is missing';
				return false;
			}

		}

		foreach ($data as $key => $value){
			$this->info[$key] = strip_tags($value);
		}
        
		return true;
	}

	/*****************************************************************
         * Adds a new album.
         *****************************************************************/
        function commit(){
        global $CONFIG;
        global $ERROR;
        global $db;
        
                if (!empty($ERROR)){
                        return false;
                }       
               
		#Build the query.
		foreach ($this->info as $key=>$value){

			if ($key === 'title' OR
			    $key === 'type') {	
				$insert_into .= "$key,";
				$values .= "'$value',";	
				$set .= "$key = '$value', ";
			}

		}
 
                if ($this->new_album_category){

			#Generate the current time
                        $timestamp = time();

			#Get the new album type
			$type = $this->_getNewType();

                        #strip the last comma
                        $insert_into = substr($insert_into,0,-1);
                        $values = substr($values,0,-1);

			#First, make a log
			$log = new Log($CONFIG['changelog_create_album_type']);
                	if (!$log->addEvent($this->info['title'],$timestamp,$this->info['title'])) {
				return false;
			}

                        $query = "INSERT INTO ".$CONFIG['tbl_album_types']."
                                   ($insert_into,type)
                                  VALUES ($values,'$type')";

		} else {

                        #strip the last comma
                        $set = substr($set,0,-2);


                        $query = "UPDATE ".$CONFIG['tbl_album_types']."
                                  SET $set
                                  WHERE type = '".$this->type."'";
                }

		$result = $db->Execute($query);
		
		if ($result) {
			return true;
		} else {
			return false;
		}
	}

	/*****************************************************************
         * Delete an album from the database
         *****************************************************************/
        function delete() {
        global $CONFIG;
        global $db;

		#Make sure info is set
		if (empty($this->info)) {
			return false;
		}

		#Only delete if there are no associated albums
		if ($this->info['num_albums'] > 0 OR
		    $this->info['num_photos'] > 0) {
			return false;
		}

                $query = "DELETE FROM ".$CONFIG['tbl_album_types']."
                          WHERE type = '".$this->type."'";
                $result = $db->Execute($query);

                if ($result) {
                        return true;
                } else {
                        return false;
                }
        }

        /*****************************************************************
         * Get Album Info
         *****************************************************************/
        function getCategoryList($string=false) {
        global $CONFIG;
        global $db;

                if ($string === 'norolls') {
                        $where = "WHERE at.type > 1";
                } else if (!is_numeric($string) AND $string) {
                        $where = "WHERE a.title = '$string'";
                } else  {
                        $where = "WHERE at.type > 2";
                }

                $query = "SELECT at.*,
				 COUNT(*) as num_albums,
				 SUM(num_photos) as num_photos
                          FROM ".$CONFIG['tbl_album_types']." at
			  LEFT JOIN ".$CONFIG['tbl_albums']." a ON (a.type = at.type)
                          $where
			  GROUP BY at.type
                         ";
                $results = $db->getAll($query);

                if ($results) {
                        if (count($results) == 1) {
                                return $results[0];
                        }

			for ($i=0; $i<count($results); $i++) {
				if ($results[$i]['num_albums'] == '1' AND $results[$i]['num_photos'] == '') {
					$results[$i]['num_albums'] = '0';
					$results[$i]['num_photos'] = '0';
				}
			}

                        return $results;
                } else {
                        return false;
                }
        }

	/*****************************************************************
	 * Retrieve all info about a particular album type
         *****************************************************************/
        function _getInfo() {
        global $CONFIG;
        global $db;

		#Determine if we should find this album type by
                #type or title
		if (isset($this->type)) {
			$where = "at.type = '".$this->type."'";
		} elseif (isset($this->title)) {
			$where = "at.title = '".$this->title."'";
		} else {
			return false;
		}

		$query = "SELECT at.*,
                                 COUNT(*) as num_albums,
                                 SUM(num_photos) as num_photos
                          FROM ".$CONFIG['tbl_album_types']." at
                          LEFT JOIN ".$CONFIG['tbl_albums']." a ON (a.type = at.type)
                          WHERE $where
                          GROUP BY at.type
                         ";
                $row = $db->getRow($query);

                if ($row) {
			$this->type = $row['type'];
			$this->title = $row['title'];
			if ($row['num_albums'] == '1' AND $row['num_photos'] == '') {
				$row['num_albums'] = '0';
				$row['num_photos'] = '0';
			}
			$this->info = $row;
                        return true;
                } else {
                        return false;
                }
        }

	/*****************************************************************
	 * Select the maximum type from the database and increment	
	 * by one so we have a unique type that we can use for a new 
	 * album type
         *****************************************************************/
	function _getNewType() {
	global $CONFIG;
	global $db;

		$query = "SELECT MAX(type) as num
			  FROM ".$CONFIG['tbl_album_types']."
			 ";
		$row = $db->getRow($query);

		if (!$row) {
			return false;
		} else {
			return $row['num']+1;
		}
	}

}
