<?php
class galleryClass extends MasterClass {
	
	
    //variables for image resizing
    protected $thumb_area;
    protected $new_thumb_W;
    protected $new_thumb_H;    
    
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function add images
    public function addImages(){
      
        $result = array();
        $now = time();
        $images = array();

        if($this->checkRef()){
            
            $data = $this->cleanData();
            $error = false;
            
            if($data['Album'] == '' || 
                ($data['Album'] == 'New' && $data['Album_Name'] == '') || 
                ($data['Category'] == 'New' && $data['Category_Name'] == '')){
                
                $result['Status'] = 0;
                $result['Class'] = 'error';
                $result['Msg'] = 'You must have a category and an album to continue.';
                $error = true;
                
            }else{

                if(isset($_FILES)) {

                    foreach($_FILES['Image']['tmp_name'] as $key=> $value){
                    
                        if(is_file($_FILES['Image']['tmp_name'][$key])){
                            
                            if($this->checkType($_FILES['Image']['type'][$key],ALLOWED_FILES_IMAGES)){
                                
                                $chars = array('!','@','#','$','%','^','&','*','(',')','=','{','}','[',']','<','>','?','/','\\',' ','\'');
                            
                                $canvas = $_FILES['Image']['tmp_name'][$key];
                                $imgName = $now.'_'.str_replace($chars,'_',$_FILES['Image']['name'][$key]);

                                $fileDimensions = @getimagesize($canvas);

                                if($fileDimensions[0] >= 1000){

                                    $dir = 'large';                                                

                                }else if($fileDimensions[0] >= 775){

                                    $dir = 'medium';

                                }else{

                                    $dir = 'small';

                                }   

                                if($img = move_uploaded_file($canvas,THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName)){                                                          

                                    if($dir == 'large'){

                                        $thumb = $this->createThumb(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName,750,GALLERY_IMAGES.'/medium',$imgName);                           
                                        if($thumb['Status'] === false){
                                            
                                            $error = true;                                  
                                            
                                        }

                                    }
                                                                                        
                                    if($dir != 'small' && $error === false){

                                        $thumb = $this->createThumb(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName,350,GALLERY_IMAGES.'/small',$imgName);
                                        if($thumb['Status'] === false){
                                            
                                            $error = true;
                                            
                                        }

                                    }
                                    
                                }else{
                                    
                                    $result['Status'] = 0;
                                    $result['Msg'] = 'Could not move the image into the correct directory. Please contact WebDuck Designs with this issues at 651.674.3834';
                                    $result['Msg'] .= '<br/>'.THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName;
                                    $result['Class'] = 'error';
                                    $error = true;
                                    break;
                                    
                                }

                            }else{
                                
                                $result['Status'] = 0;
                                $result['Msg'] = 'The image you are attempting to upload is not an approved image type. You can only upload jpeg, jpg and png images. For photos we recommend always using jpeg/jpg file type. For graphics we recommend using the png file type.';
                                $result['Class'] = 'error'; 
                                $error = true;
                                break;
                                
                            }
                                
                        }else{
                            
                            $result['Status'] = 0;
                            $result['Msg'] = 'There was no file uploaded.';
                            $result['Class'] = 'error';  
                            $error = true;
                            break;
                            
                        }                    

                        if($error === false){

                            $images[] = $imgName;

                        }
                    }

                }
                
            }
            
            if($error === false){
                
                $now = time();
                
                $this->dbConn();

                if($data['Category_Name'] != '' && $data['Category'] == 'New'){
                    
                    $this->query = "INSERT INTO Album_Categories VALUES ('','";
                    $this->query .= $data['Category_Name'];
                    $this->query .= "','0','0')";
                    
                    $this->db->query($this->query);
                    
                    $categoryId = $this->db->insert_id;
                    
                }else{
                    
                    $categoryId = $data['Category'];
                    
                }                 
                
                if($data['Album_Name'] != '' && $data['Album'] == 'New' && $categoryId != ''){
                    
                    $this->query = "INSERT INTO Albums VALUES ('','";
                    $this->query .= $data['Album_Name'];
                    $this->query .= "','";
                    $this->query .= $now;
                    $this->query .= "','','1','";
                    $this->query .= $categoryId;
                    $this->query .= "')";
                    
                    $this->db->query($this->query);
                    
                    $albumId = $this->db->insert_id;

                    $result['AlbumId'] = $albumId;
                    
                }else{
                    
                    $albumId = $data['Album'];
                    
                }                                   
                
                if($images[0] != ''){

                    for($i=0;$i<count($images);$i++){

                        $this->query = "INSERT INTO Images VALUES ('','";
                        $this->query .= $albumId;
                        $this->query .= "','";
                        $this->query .= $categoryId;
                        $this->query .= "','";                
                        $this->query .= $images[$i];
                        $this->query .= "','";
                        $this->query .= $data['Desc'];
                        $this->query .= "','1')";
                        
                        $this->db->query($this->query);

                    }

                }
                
                $this->dbClose();
                
                $result['Msg'] .= 'The image was successfully added to the database';
                $result['Class'] = 'success';
                $result['Status']= 1;                
                $result['CategoryId'] = $categoryId;
                
            }
            
        }
        
        return $result;
        
    }
    
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function get albums
    public function getAlbums($category=''){
        
        $info = array();
        
        if($this->checkRef()){
            
            $this->dbConn();
            
            if($category == ''){

                $this->query = "SELECT * FROM Albums ";
                $this->query .= "LEFT JOIN Images ON album_display_image = image_nbr ";
                $this->query .= "LEFT JOIN Album_Categories ON album_category = album_category_nbr ";
                $this->query .= "ORDER BY album_name ASC";
            
            }else{

                $this->query = "SELECT * FROM Albums ";
                $this->query .= "LEFT JOIN Images ON album_display_image = image_nbr ";
                $this->query .= "LEFT JOIN Album_Categories ON album_category = album_category_nbr ";
                $this->query .= "WHERE album_category = '";
                $this->query .= $this->db->real_escape_string($category);
                $this->query .= "' ORDER BY album_name ASC";

            }

            $getInfo = $this->db->query($this->query);
            
            if($getInfo->num_rows > 0){
                
               $i=0;
               while($row = $getInfo->fetch_assoc()){
                   
                   $info[$i]['Nbr'] = stripslashes($row['album_nbr']);
                   $info[$i]['Original_Id'] = stripslashes($row['album_original_id']);
                   $info[$i]['Name'] = stripslashes($row['album_name']);
                   $info[$i]['Category'] = stripslashes($row['album_category']);
                   $info[$i]['Description'] = stripslashes($row['album_description']);
                   $info[$i]['Category_Name'] = stripslashes($row['album_category_name']);
                   $info[$i]['Date_Created'] = stripslashes($row['album_date_created']);
                   $info[$i]['Status'] = stripslashes($row['album_status']);
                   $info[$i]['Display_Image'] = stripslashes($row['album_display_image']);
                   $info[$i]['Display_Image_Name'] = stripslashes($row['image_name']);
                   $i++;
                   
               }
                
            }
            
            $this->dbClose();
            
        }
        
        return $info;
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //getting categories list
    public function getAlbumCategories(){
        
        $cats = array();

        if($this->checkRef()){                        
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Album_Categories ";
            $this->query .= "LEFT JOIN Images ON album_category_display_image = image_nbr ";
            $this->query .= "ORDER BY album_category_name ASC";

            $getInfo = $this->db->query($this->query);

            if($getInfo->num_rows > 0){

                $k=0;
                while($row = $getInfo->fetch_assoc()){

                      $cats[$k]['Nbr'] = $row['album_category_nbr'];
                      $cats[$k]['Name'] = $row['album_category_name'];
                      $cats[$k]['Status'] = $row['album_category_status'];
                      $cats[$k]['Display_Image'] = $row['album_category_display_image'];
                      $cats[$k]['Display_Image_Name'] = $row['image_name'];
                      $k++;

                }

            }
            
            $this->dbClose();
            
        }
        
        return $cats;
        
    }
      
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function to determine the file type that was uploaded
    //can only use .gif, .png, and .jpg files
    private function imageType($name){

        $fileArray = explode('.',$name);

        $ext = strtolower($fileArray[1]);

        //use the extension to determine the right file type
        switch (strtolower($ext)){

            case "png":
                $ftype = 'png';
            break;

            case "gif":
                $ftype = 'gif';
            break;

            case "jpg":
                $ftype = 'jpeg';
            break;

            case "jpeg":
                $ftype = 'jpeg';
            break;

            default:
                $ftype = 'none';
            break;


        }

        return $ftype;

    }	

    ###################################################################################################################################################
    ###################################################################################################################################################

    //function to determine the thumb
    private function thumbDimensions($canvas,$mWidth){ 			

        //get the size of the canvas image
        $this->thumb_area = @getimagesize($canvas);

        if($this->thumb_area[0] > $mWidth){

            //figure out the current ratio
            $ratio  = ($this->thumb_area[0] / $mWidth);   

            $this->new_thumb_W = ($this->thumb_area[0] / $ratio);
            $this->new_thumb_H =($this->thumb_area[1] / $ratio);

        }else{

            $this->new_thumb_W = $this->thumb_area[0];
            $this->new_thumb_H = $this->thumb_area[1];

        }

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //create thumbnail image
    private function createThumb($canvas,$mWidth,$thDir,$imgName){

        $result = array();

        if(is_file($canvas)){

            $type = $this->imageType($imgName);

            //we have a legitimate file type, time to create the thumbnail image
            switch ($type){					
                case "jpeg":
                    $srcImg = @imagecreatefromjpeg($canvas);
                break;						
                case "gif":
                    $srcImg = @imagecreatefromgif($canvas);
                break;						
                case "png":
                    $srcImg = @imagecreatefrompng($canvas);
                break;						
            }

            if($srcImg){

                //determine the new width and height of the thumbnail image
                $dimensionCheck = $this->thumbDimensions($canvas,$mWidth);

                    //create the thumbnail image
                    $thumb = @imagecreatetruecolor($this->new_thumb_W,$this->new_thumb_H);

                    if($thumb){

                        if(@imagecopyresampled($thumb,$srcImg,0,0,0,0,$this->new_thumb_W,$this->new_thumb_H,$this->thumb_area[0],$this->thumb_area[1])){

                            $thumbPath = THIS_DOMAIN.'/'.$thDir.'/'.$imgName;									

                            //create the finished file and save it based on the file type
                            switch ($type){

                                case "jpeg":
                                    $final = @imagejpeg($thumb,$thumbPath,100);
                                break;

                                case "gif":
                                    $final = @imagegif($thumb,$thumbPath,0);
                                break;

                                case "png":
                                    $final = @imagepng($thumb,$thumbPath,-1);
                                break;

                            }

                            if($final){

                                //image was successfully created and saved. 
                                //delete the src image
                                @imagedestroy($srcImg);
                                @imagedestroy($thumb);

                                $result['imgName'] = $this->safeName;
                                $result['Msg'] = 'Image upload was successful';
                                $result['Status'] = true;

                            }else{

                                $result['Msg'] = 'Could not save thumbnail to the specified directory';
                                $result['Msg'] .= '<br/>'.$thumbPath;
                                $result['Msg'] .= '<br/><br/>File upload unsuccessful';
                                $result['Status'] = false;

                            }

                        }else{

                            $result['Msg'] = 'Could not resize the canvas to the new parameters';
                            $result['Msg'] .= '<br/><br/>File upload unsuccessful';
                            $result['Status'] = false;

                        }

                    }else{

                        $result['Msg'] = 'Could not create the thumbnail image from canvas';
                        $result['Msg'] .= '<br/><br/>File upload unsuccessful';
                        $result['Status'] = false;

                    }
                        
                }else{

                    $result['Msg'] = 'Could not create image';
                    $result['Msg'] .= '<br/><br/>File upload unsuccessful';
                    $result['Status'] = false;

                }

            }else{

                $result['Msg'] = 'There is no file to use as a canvas for alteration';
                $result['Msg'] .= '<br/><br/>File upload unsuccessful';
                $result['Status'] = false;

            }

        return $result;

    }	

    ####################################################################################################################################################
    ####################################################################################################################################################

    //getting album list with image icon
    public function getAlbumDisplayList($category=''){
        
        $albums = array();
        $images = array();
        
        if($this->checkRef()){
            
            $albums = $this->getAlbums($category);
            
            $this->dbConn();

            $this->query = "SELECT * FROM Images ";
            $this->query .= "LEFT JOIN Albums ON image_album = album_nbr ";
            $this->query .= "LEFT JOIN Album_Categories ON image_album_category = album_category_nbr ";
            $this->query .= " GROUP BY image_album";

            $getInfo = $this->db->query($this->query);

            if($getInfo->num_rows > 0){

                $k=0;
                while($row = $getInfo->fetch_assoc()){

                      $images[$k]['Album'] = $row['image_album'];
                      $images[$k]['Album_Name'] = $row['album_name'];
                      $images[$k]['Category'] = $row['image_album_category'];
                      $images[$k]['Category_Name'] = $row['image_category_name'];
                      $images[$k]['Nbr'] = $row['image_nbr'];
                      $images[$k]['Name'] = $row['image_name'];
                      $images[$k]['Status'] = $row['image_status'];
                      $k++;

                }

            }
            
            $this->dbClose();
            
            for($i=0;$i<count($albums);$i++){
                
               if($albums[$i]['Display_Image'] == 0 || $albums[$i]['Display_Image'] == ''){

                   for($k=0;$k<count($images);$k++){
                       
                       if($albums[$i]['Nbr'] == $images[$k]['Album']){
                           
                           $albums[$i]['Display_Image'] = $images[$k]['Nbr'];
                           $albums[$i]['Display_Image_Name'] = $images[$k]['Name'];
                           break;
                           
                       }
                       
                   }
                   
               }
                
            }
            
        }
        
        return $albums;
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //getting album category list with image icon
    public function getAlbumCategoryDisplayList(){
        
        $images = array();
        
        if($this->checkRef()){
            
            $categories = $this->getAlbumCategories();
            
            $this->dbConn();

            $this->query = "SELECT * FROM Images ";
            $this->query .= "LEFT JOIN Album_Categories ON image_album_category = album_category_nbr ";
            $this->query .= " GROUP BY image_album_category";

            $getInfo = $this->db->query($this->query);

            if($getInfo->num_rows > 0){

                $k=0;
                while($row = $getInfo->fetch_assoc()){

                      $images[$k]['Album'] = $row['image_album'];
                      $images[$k]['Album_Name'] = $row['album_name'];
                      $images[$k]['Category'] = $row['image_album_category'];
                      $images[$k]['Category_Name'] = $row['image_category_name'];
                      $images[$k]['Nbr'] = $row['image_nbr'];
                      $images[$k]['Name'] = $row['image_name'];
                      $images[$k]['Status'] = $row['image_status'];
                      $k++;

                }

            }
            
            $this->dbClose();
            
            for($i=0;$i<count($categories);$i++){
                
               if($categories[$i]['Display_Image'] == 0 || $categories[$i]['Display_Image'] == ''){

                   for($k=0;$k<count($images);$k++){
                       
                       if($categories[$i]['Nbr'] == $images[$k]['Category']){
                           
                           $categories[$i]['Display_Image'] = $images[$k]['Nbr'];
                           $categories[$i]['Display_Image_Name'] = $images[$k]['Name'];
                           break;
                           
                       }
                       
                   }
                   
               }
                
            }
            
        }
        
        return $categories;
        
    }    

    ####################################################################################################################################################
    ####################################################################################################################################################

    //getting category info
    public function getAlbumCategoryInfo($id){
        
        $info = array();

        if($this->checkRef()){                        
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Album_Categories ";
            $this->query .= "LEFT JOIN Images ON album_category_display_image = image_nbr ";
            $this->query .= "WHERE album_category_nbr = '";
            $this->query .= $this->db->real_escape_string($id);
            $this->query .= "'";

            $getInfo = $this->db->query($this->query);

            if($getInfo->num_rows > 0){

                $row = $getInfo->fetch_assoc();

                $info['Nbr'] = $row['album_category_nbr'];
                $info['Name'] = $row['album_category_name'];
                $info['Status'] = $row['album_category_status'];
                $info['Image_Nbr'] = $row['album_category_display_image'];
                $info['Total_Images'] = 0;
                $k++;

                $this->query = "SELECT * FROM Images WHERE image_album_category = '";
                $this->query .= $this->db->real_escape_string($id);
                $this->query .= "'";

                $getImages = $this->db->query($this->query);

                $info['Total_Images'] = $getImages->num_rows;

            }
            
            $this->dbClose();
            
        }
        
        return $info;
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //getting image list
    public function getDisplayImages($album='',$category=''){
        
        $images = array();
        
        if($this->checkRef()){
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Images ";
            $this->query .= "LEFT JOIN Albums ON image_album = album_nbr ";
            $this->query .= "LEFT JOIN Album_Categories ON image_album_category = album_category_nbr ";
            if($album != ''){
                
                $this->query .= "WHERE image_album = '";
                $this->query .= $this->db->real_escape_string($album);
                $this->query .= "' ";
                
            }else if($category != ''){
                
                $this->query .= "WHERE image_album_category = '";
                $this->query .= $this->db->real_escape_string($category);
                $this->query .= "' ";
                
            }
            $this->query .= "ORDER BY image_nbr DESC";
            
            $getImages = $this->db->query($this->query);
            
            if($getImages->num_rows > 0){
                
                $i=0;
                while($row = $getImages->fetch_assoc()){
                    
                    $images[$i]['Nbr'] = stripslashes($row['image_nbr']);
                    $images[$i]['Album'] = stripslashes($row['image_album']);
                    $images[$i]['Category'] = stripslashes($row['image_album_category']);
                    $images[$i]['Category_Name'] = stripslashes($row['album_category_name']);
                    $images[$i]['Name'] = stripslashes($row['image_name']);
                    $images[$i]['Desc'] = stripslashes($row['image_desc']);
                    $images[$i]['Status'] = stripslashes($row['image_status']);
                    $images[$i]['Display_Image'] = '';
                    $images[$i]['Album_Category_Display_Image'] = '';
                    if($row['album_display_image'] == $row['image_nbr']){
                        
                        $images[$i]['Display_Image'] = 'Yes';
                        
                    }
                    if($row['album_category_display_image'] == $row['image_nbr']){
                        
                        $images[$i]['Album_Category_Display_Image'] = 'Yes';
                        
                    }                    
                    $i++;
                    
                }
          
            }
            
            $this->dbClose();
        
        }
        
        return $images;
            
    }
 
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function get album info
    public function getAlbumInfo($id){
        
        $info = array();
        
        if($this->checkRef()){
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Albums ";
            $this->query .= "LEFT JOIN Images ON album_display_image = image_nbr ";
            $this->query .= "LEFT JOIN Album_Categories ON album_category = album_category_nbr ";
            $this->query .= "WHERE album_nbr = '";
            $this->query .= $this->db->real_escape_string($id);
            $this->query .= "'";
            
            $getInfo = $this->db->query($this->query);
            
            if($getInfo->num_rows > 0){
                
               $row = $getInfo->fetch_assoc();
                   
                $info['Nbr'] = stripslashes($row['album_nbr']);
                $info['Name'] = stripslashes($row['album_name']);
                $info['Category'] = stripslashes($row['album_category']);
                $info['Description'] = stripslashes($row['album_description']);
                $info['Category_Name'] = stripslashes($row['album_category_name']);
                $info['Date_Created'] = stripslashes($row['album_date_created']);
                $info['Status'] = stripslashes($row['album_status']);
                $info['Display_Image'] = stripslashes($row['album_display_image']);
                $info['Display_Image_Name'] = stripslashes($row['image_name']);  

                $this->query = "SELECT * FROM Images WHERE image_album = '";
                $this->query .= $this->db->real_escape_string($id);
                $this->query .= "'";
                
                $getImages = $this->db->query($this->query);
                
                $info['Total_Images'] = $getImages->num_rows;
                
            }
            
            $this->dbClose();
            
        }
        
        return $info;
        
    }
          
    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album name
    public function changeAlbumName(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['AlbumId'] != '' && $data['AlbumName'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Albums SET album_name = '";
               $this->query .= $data['AlbumName'];
               $this->query .= "' WHERE album_nbr = '";
               $this->query .= $data['AlbumId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'The album was successfully renamed';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }else{
                
                $result['Msg'] = 'You must enter a new name for your album before submitting';
                $result['Status'] = 0;
                $result['Class'] = 'error';
                
            }
        
        }
        
        return $result;
            
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album category name
    public function changeAlbumCategoryName(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['CategoryId'] != '' && $data['CategoryName'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Album_Categories SET album_category_name = '";
               $this->query .= $data['CategoryName'];
               $this->query .= "' WHERE album_category_nbr = '";
               $this->query .= $data['CategoryId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'The album category was successfully renamed';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }else{
                
                $result['Msg'] = 'You must enter a new name for your album category before submitting';
                $result['Status'] = 0;
                $result['Class'] = 'error';
                
            }
        
        }
        
        return $result;
            
    }    
    
    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album status
    public function changeAlbumStatus(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['AlbumId'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Albums SET album_status = '";
               $this->query .= $data['Status'];
               $this->query .= "' WHERE album_nbr = '";
               $this->query .= $data['AlbumId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'The album status was successfully changed';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }
        
        }
        
        return $result;
            
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album category status
    public function changeAlbumCategoryStatus(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['CategoryId'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Album_Categories SET album_category_status = '";
               $this->query .= $data['Status'];
               $this->query .= "' WHERE album_category_nbr = '";
               $this->query .= $data['CategoryId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'The album category status was successfully changed';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }
        
        }
        
        return $result;
            
    }    

    ####################################################################################################################################################
    ####################################################################################################################################################

    //set display image
    public function setDisplayImage(){

        if($this->checkRef()){
            
            $this->dbConn();
            
            $data = $this->cleanData();
            
            $this->query = "UPDATE Albums SET album_display_image = '";
            $this->query .= $data['ImageId'];
            $this->query .= "' WHERE album_nbr = '";
            $this->query .= $data['AlbumId'];
            $this->query .= "'";
            
            $this->db->query($this->query);
            
            $this->dbClose();
            
        }

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //set album category display image
    public function setAlbumCategoryDisplayImage(){

        if($this->checkRef()){
            
            $this->dbConn();
            
            $data = $this->cleanData();
            
            $this->query = "UPDATE Album_Categories SET album_category_display_image = '";
            $this->query .= $data['ImageId'];
            $this->query .= "' WHERE album_category_nbr = '";
            $this->query .= $data['CategoryId'];
            $this->query .= "'";
            
            $this->db->query($this->query);
            
            $this->dbClose();
            
        }

    }    

    ####################################################################################################################################################
    ####################################################################################################################################################

    //delete an album
    public function deleteAlbum(){
        
        $result = array();
        
        if($this->checkRef()){
        
            $data = $this->cleanData();
            
            if($data['AlbumId'] != ''){
                
                $images = $this->getDisplayImages($data['AlbumId']);
                
                if($images[0]['Name'] != ''){
                        
                        for($i=0;$i<count($images);$i++){
                            
                            if($images[$i]['Name'] != ''){
                                                                       
                                
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$images[$i]['Name']);
                                    
                                }
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$images[$i]['Name']);
                                    
                                }
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$images[$i]['Name']);
                                    
                                }                                
                                
                            }
                            
                        }
                    
                }
                                    
                $this->dbConn();

                $this->query = "DELETE FROM Images WHERE image_album = '";
                $this->query .= $data['AlbumId'];
                $this->query .= "'";

                $this->db->query($this->query);

                $this->query = "DELETE FROM Albums WHERE album_nbr = '";
                $this->query .= $data['AlbumId'];
                $this->query .= "'";

                $this->db->query($this->query);  

                $this->dbClose();
                
            }
            
        }    
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //delete an album category
    public function deleteAlbumCategory(){
        
        $result = array();
        
        if($this->checkRef()){
        
            $data = $this->cleanData();
            
            if($data['CategoryId'] != ''){
                
                $images = $this->getDisplayImages('',$data['CategoryId']);
                
                if($images[0]['Name'] != ''){
                        
                        for($i=0;$i<count($images);$i++){
                            
                            if($images[$i]['Name'] != ''){
                                                                       
                                
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$images[$i]['Name']);
                                    
                                }
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$images[$i]['Name']);
                                    
                                }
                                if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$images[$i]['Name'])){
                                    
                                    unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$images[$i]['Name']);
                                    
                                }                                
                                
                            }
                            
                        }
                    
                }
                                    
                $this->dbConn();

                $this->query = "DELETE FROM Images WHERE image_album_category = '";
                $this->query .= $data['CategoryId'];
                $this->query .= "'";

                $this->db->query($this->query);

                $this->query = "DELETE FROM Albums WHERE album_category = '";
                $this->query .= $data['CategoryId'];
                $this->query .= "'";

                $this->db->query($this->query);
                
                $this->query = "DELETE FROM Album_Categories WHERE album_category_nbr = '";
                $this->query .= $data['CategoryId'];
                $this->query .= "'";

                $this->db->query($this->query);                

                $this->dbClose();
                
            }
            
        }    
        
    }    
  
    ####################################################################################################################################################
    ####################################################################################################################################################

    //add album
    public function addAlbum(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['AlbumName'] != '' && $data['Category'] != ''){
                
                $now = time();
              
                $this->dbConn();
                
                $this->query = "INSERT INTO Albums VALUES ('','";
                $this->query .= $data['AlbumName'];
                $this->query .= "','";
                $this->query .= $now;
                $this->query .= "','0','0','";
                $this->query .= $data['Category'];
                $this->query .= "','')";                
                    
                $this->db->query($this->query);
                                
                $result['Msg'] = 'The new album was successfully created.';
                $result['Status'] = 1;
                $result['Class'] = 'success';
                $result['AlbumName'] = $data['AlbumName'];
                $result['CategoryId'] = $data['Category'];
                $result['AlbumId'] = $this->db->insert_id;
                
                $this->dbClose();
                
            }else{

                $result['Msg'] = 'You must have an album name and choose a category to create a new album.';
                $result['Status'] = 0;
                $result['Class'] = 'error';

            }
            
        }
        
        return $result;
        
    }
   
    ####################################################################################################################################################
    ####################################################################################################################################################

    //add album category
    public function addAlbumCategory(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['CategoryName'] != ''){
                
                $now = time();
              
                $this->dbConn();
                
                $this->query = "INSERT INTO Album_Categories VALUES ('','";
                $this->query .= $data['CategoryName'];
                $this->query .= "','0','')";
                    
                $this->db->query($this->query);
                
                $result['Msg'] = 'The new album category was successfully created.';
                $result['Status'] = 1;
                $result['Class'] = 'success';
                $result['CategoryName'] = $data['CategoryName'];
                $result['CategoryId'] = $this->db->insert_id;
                
                $this->dbClose();
                
            }
            
        }
        
        return $result;
        
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album for image
    public function changeAlbum(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['ImageId'] != '' && $data['Album'] != '' && $data['Category'] != ''){
                
                $this->dbConn();
                
                if($data['Album'] != 'New'){                
               
                    $this->query = "UPDATE Images SET image_album = '";
                    $this->query .= $data['Album'];
                    $this->query .= "', image_album_category = '";
                    $this->query .= $data['Category'];
                    $this->query .= "' WHERE image_nbr = '";
                    $this->query .= $data['ImageId'];
                    $this->query .= "'";
               
                    $this->db->query($this->query);
                    
                }else{
                    
                     if($data['Album_Name'] != ''){
                       
                         $now = time();
                         
                         $this->query = "INSERT INTO Albums VALUES ('','";
                         $this->query .= $data['Album_Name'];
                         $this->query .= "','";
                         $this->query .= $now;
                         $this->query .= "','0','0')";
                         
                         $this->db->query($this->query);
                         
                         $albumId = $this->db->insert_id;
                         
                        $this->query = "UPDATE Images SET image_album = '";
                        $this->query .= $albumId;
                        $this->query .= "', image_album_category = '";
                        $this->query .= $data['Category'];                        
                        $this->query .= "' WHERE image_nbr = '";
                        $this->query .= $data['ImageId'];
                        $this->query .= "'";

                        $this->db->query($this->query);                         
                         
                     }else{
                         
                         $result['Msg'] .= 'No album name was entered.';
                         $result['Status'] = 0;
                         $result['Class'] = 'error'; 
                         
                     }
                    
                }
               
               $this->dbClose();
               
                $result['Msg'] .= 'This image has been successfully moved to another album.';               
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }else{

                $result['Msg'] .= 'Please select an album category and choose or create an album before submitting your change.';               
                $result['Status'] = 0;
                $result['Class'] = 'error';   

            }
        
        }
        
        return $result;
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album category
    public function changeAlbumCategory(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['AlbumId'] != '' && $data['Category'] != ''){

                $albumInfo = $this->getAlbumInfo($data['AlbumId']);
                $categoryInfo = $this->getAlbumCategoryInfo($albumInfo['Category']);
                $imageInfo = $this->getImageInfo($categoryInfo['Image_Nbr']);
                
                $this->dbConn();    

                if($data['Category'] != $albumInfo['Category']){

                    if($imageInfo['Album'] == $data['AlbumId']){

                        //the existing category display image is from this album. Need to reset it to be empty
                        $this->query = "UPDATE Album_Categories SET album_category_display_image = '' WHERE album_category_nbr = '";
                        $this->query .= $albumInfo['Category'];
                        $this->query .= "'";

                        $this->db->query($this->query);

                        $result['Msg'] .= 'The previous category display image was reset to empty because the original image was in the album you just changed.<br/>';

                    }

                }                
                
                $this->query = "UPDATE Albums SET album_category = '";
                $this->query .= $data['Category'];
                $this->query .= "' WHERE album_nbr = '";
                $this->query .= $data['AlbumId'];
                $this->query .= "'";

                $this->db->query($this->query);
               
                $this->query = "UPDATE Images SET image_album_category = '";
                $this->query .= $data['Category'];
                $this->query .= "' WHERE image_album = '";
                $this->query .= $data['AlbumId'];
                $this->query .= "'";
            
                $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] .= 'This album has been successfully moved to another category.';               
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }
        
        }
        
        return $result;
    }    

    ####################################################################################################################################################
    ####################################################################################################################################################

    //change album category for image
    public function changeAlbumAndCategory(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['ImageId'] != ''){
                
                $this->dbConn();
                
                if($data['Category'] != 'New'){                
                                   
                    $categoryId = $this->query .= $data['Category'];
                    
                }else{
                    
                     if($data['Category_Name'] != ''){
                       
                         $now = time();
                         
                         $this->query = "INSERT INTO Album_Categories VALUES ('','";
                         $this->query .= $data['Category_Name'];
                         $this->query .= "','0','')";
                         
                         $this->db->query($this->query);
                         
                         $categoryId = $this->db->insert_id;

                     }else{
                         
                        $result['Msg'] .= 'No category name was entered.';
                        $result['Status'] = 0;
                        $result['Class'] = 'error'; 
                        
                    }
                         
                }

                if($data['Album'] != 'New'){                
               
                    $albumId = $data['Album'];
                    
                }else{
                
                    if($data['Album_Name'] != '' && $categoryId != ''){
                    
                        $now = time();
                        
                        $this->query = "INSERT INTO Albums VALUES ('','";
                        $this->query .= $data['Album_Name'];
                        $this->query .= "','";
                        $this->query .= $now;
                        $this->query .= "','0','0','";
                        $this->query .= $categoryId;
                        $this->query .= "')";
                        
                        $this->db->query($this->query);
                        
                        $albumId = $this->db->insert_id;                       
                        
                    }
                    
                }                

                if($categoryId != '' && $albumId != ''){

                    $this->query = "UPDATE Images SET image_album_category = '";
                    $this->query .= $categoryId;
                    $this->query .= "', image_album = '";
                    $this->query .= $albumId;
                    $this->query .= "' WHERE image_nbr = '";
                    $this->query .= $data['ImageId'];
                    $this->query .= "'";

                    $this->db->query($this->query);                                                        
                
                    $this->dbClose();

                    $result['Msg'] .= 'This image has been successfully moved to another album/category.';               
                    $result['Status'] = 1;
                    $result['Class'] = 'success';                     

                }                         
                
            }
        
        }
        
        return $result;
    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //edit image description
    public function editImageDesc(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['ImageId'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Images SET image_desc = '";
               $this->query .= $data['Desc'];
               $this->query .= "' WHERE image_nbr = '";
               $this->query .= $data['ImageId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'This image description has been successfully updated.';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }
        
        }
        
        return $result;
    }
  
    ####################################################################################################################################################
    ####################################################################################################################################################

    //change status of image
    public function changeImageStatus(){
        
        $result = array();
        
        if($this->checkRef()){
            
            $data = $this->cleanData();
            
            if($data['ImageId'] != ''){
                
               $this->dbConn();
               
               $this->query = "UPDATE Images SET image_status = '";
               $this->query .= $data['Status'];
               $this->query .= "' WHERE image_nbr = '";
               $this->query .= $data['ImageId'];
               $this->query .= "'";
               
               $this->db->query($this->query);
               
               $this->dbClose();
               
                $result['Msg'] = 'This image status has been successfully updated.';
                $result['Status'] = 1;
                $result['Class'] = 'success';               
                
            }
        
        }
        
        return $result;
    }
           
    ####################################################################################################################################################
    ####################################################################################################################################################

    //delete an image
    public function deleteImage(){
        
        $result = array();
        
        if($this->checkRef()){
        
            $data = $this->cleanData();
            
            if($data['ImageId'] != ''){
                                
                
                if($data['ImageName'] != ''){                                                                       
                                
                    if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$data['ImageName'])){

                        unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/small/'.$data['ImageName']);

                    }
                    if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$data['ImageName'])){

                        unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/medium/'.$data['ImageName']);

                    }
                    if(is_file(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$data['ImageName'])){

                        unlink(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/large/'.$data['ImageName']);

                    }                                

                    
                }
                                    
                $this->dbConn();

                $this->query = "DELETE FROM Images WHERE image_nbr = '";
                $this->query .= $data['ImageId'];
                $this->query .= "'";

                $this->db->query($this->query);
                
                $this->query = "UPDATE Albums SET album_display_image = '0' WHERE album_display_image = '";
                $this->query .= $data['ImageId'];
                $this->query .= "'";
                
                $this->db->query($this->query);

                $this->dbClose();
                
            }
            
        }    
        
    }
   
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function get image info
    public function getImageInfo($id){
        
        $info = array();
        
        if($this->checkRef()){
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Images ";
            $this->query .= "LEFT JOIN Albums ON image_album = album_nbr ";
            $this->query .= "LEFT JOIN Album_Categories ON image_album_category = album_category_nbr ";
            $this->query .= "WHERE image_nbr = '";
            $this->query .= $this->db->real_escape_string($id);
            $this->query .= "'";
            
            $getInfo = $this->db->query($this->query);
            
            if($getInfo->num_rows > 0){
                
               $row = $getInfo->fetch_assoc();
                   
                $info['Nbr'] = stripslashes($row['image_nbr']);
                $info['Name'] = stripslashes($row['image_name']);
                $info['Status'] = stripslashes($row['image_status']);
                $info['Desc'] = stripslashes($row['image_desc']);
                $info['Album'] = stripslashes($row['image_album']);  
                $info['Album_Name'] = stripslashes($row['album_name']);  
                $info['Category'] = stripslashes($row['image_album_category']);  
                $info['Category_Name'] = stripslashes($row['album_category_name']);  
                
            }
            
            $this->dbClose();
            
        }
        
        return $info;
        
    }           

    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function migration
    public function processGallery(){

        $this->dbConn();

        $this->query = "SELECT * FROM Albums LEFT JOIN Gallery_Albums ON album_original_id = gallery_album_id WHERE album_migrated = '0' LIMIT 1";

        $getAlbum = $this->db->query($this->query);

        if($getAlbum->num_rows > 0){

            $row = $getAlbum->fetch_assoc();

            $nbr = $row['album_nbr'];
            $category = $row['album_category'];
            $ogNbr = $row['album_original_id'];
            $path = $row['gallery_album_path'];

            $this->query = "SELECT * FROM Gallery_Pictures WHERE pic_parent_album = '";
            $this->query .= $ogNbr;
            $this->query .= "'";

            $getPics = $this->db->query($this->query);

            echo '<p>Total Pics: '.$getPics->num_rows.'</p>';

            if($getPics->num_rows > 0){

                $i=0;
                while($row = $getPics->fetch_assoc()){

                    $pics[$i]['Path'] = $row['pic_path'];
                    $pics[$i]['Desc'] = $row['pic_description'];
                    $i++;

                }

                for($i=0;$i<count($pics);$i++){  
                    
                    echo '<p>';

                    if($pics[$i]['Path'] != ''){

                        $canvas = THIS_DOMAIN.'/gallery/old-images/'.$pics[$i]['Path'];                    

                        if(is_file($canvas)){
                                                    
                            echo $canvas.'<br/>';

                            $now = time();
                            $imgNameArray = explode('/',$canvas);
                            $imgName = $imgNameArray[count($imgNameArray) - 1];

                            $chars = array('!','@','#','$','%','^','&','*','(',')','=','{','}','[',']','<','>','?','/','\\',' ','\'');
                            
                            $imgName = $now.'_'.str_replace($chars,'_',$imgName);

                            $fileDimensions = @getimagesize($canvas);

                            if($fileDimensions[0] >= 1000){

                                $dir = 'large';                                                

                            }else if($fileDimensions[0] >= 775){

                                $dir = 'medium';

                            }else{

                                $dir = 'small';

                            }  
                            
                            echo $dir.'<br/>';

                            if($img = copy($canvas,THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName)){                                                          

                                $error = false;
                                if($dir == 'large'){

                                    $thumb = $this->createThumb(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName,750,GALLERY_IMAGES.'/medium',$imgName);                           
                                    if($thumb['Status'] === false){
                                        
                                        $error = true;
                                        echo $thumb['Msg'].'<br/>';                               
                                        
                                    }

                                }
                                                                                    
                                if($dir != 'small' && $error === false){

                                    $thumb = $this->createThumb(THIS_DOMAIN.'/'.GALLERY_IMAGES.'/'.$dir.'/'.$imgName,350,GALLERY_IMAGES.'/small',$imgName);
                                    if($thumb['Status'] === false){
                                        
                                        $error = true;
                                        echo $thumb['Msg'].'<br/>';
                                        
                                    }

                                }

                                if($error === false){

                                    $this->query = "INSERT INTO Images VALUES ('','";
                                    $this->query .= $nbr;
                                    $this->query .= "','";
                                    $this->query .= $category;
                                    $this->query .= "','";
                                    $this->query .= $imgName;
                                    $this->query .= "','";
                                    $this->query .= $pics[$i]['Desc'];
                                    $this->query .= "','1')";

                                    $this->db->query($this->query);

                                }
                                
                            }else{

                               echo '<p>could not move file <br/>'.$canvas.'</p>';
                               //exit;

                            }

                        }


                    }

                    echo '</p>';

                }

                $this->query = "UPDATE Albums SET album_migrated = '1' WHERE album_nbr = '";
                $this->query .= $nbr;
                $this->query .= "'";

                $this->db->query($this->query);

            }else{

                echo 'No Pics';
                $this->query = "UPDATE Albums SET album_migrated = '1' WHERE album_nbr = '";
                $this->query .= $nbr;
                $this->query .= "'";

                $this->db->query($this->query);

            }


        }else{

            echo 'no albums left';
            //exit;

        }

        $this->dbClose();

        echo 'done';
        exit;

    }

    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function album check
    public function albumCheck(){

        $this->dbConn();

        $this->query = "SELECT * FROM Gallery_Albums LEFT JOIN Albums ON gallery_album_id = album_original_id";

        $getInfo = $this->db->query($this->query);

        if($getInfo->num_rows > 0){

            echo $getInfo->num_rows.'<br/>';
            $now = time();

            while($row = $getInfo->fetch_assoc()){

                if($row['album_nbr'] == ''){

                    $this->query = "INSERT INTO Albums VALUES ('','";
                    $this->query .= addslashes($row['gallery_album_name']);
                    $this->query .= "','";
                    $this->query .= $now;
                    $this->query .= "','','1','";
                    $this->query .= $row['gallery_album_parent_id'];
                    $this->query .= "','','";
                    $this->query .= $row['gallery_album_id'];
                    $this->query .= "','0')";

                    //echo $this->query.'<br/>';
                    $this->db->query($this->query);


                }

            }

        }

        $this->dbClose();

    }
}

?>