<?php
/************************************************************************** *
 *																			*
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.	*
 *																			*
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.													*
 *																			*
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.			*
 ****************************************************************************/
//
//$Id: WatchvideoModel.php
//
class WatchvideoModel extends Model {
    public function saveWatchvideo($POST,$watchvideoID,$languageIDs) {
		$ObjImage = new ImageModel ;
		$productCategoryID = $POST['productCategoryID'];
		if (!empty($POST['attrOptionsGroup']))
        {
         foreach ($POST['attrOptionsGroup'] as $rowKey => $row) // Traverse through all posted options
            {
				$watchvideoDetID = (int) $row['watchvideoDetailID'];
				//$watchvideoID = (int) $row['watchvideoID'];
                 
				$row['hidThumb']   = isset($row['hidThumb'])?$row['hidThumb']:'';
				$hidThumb          = "public/" . $this->escape($row['hidThumb']);
				$winnerThumbImageType = $POST['OptionVariantThumb_' . $rowKey];
					if (($POST["OptionVariantThumb_" . $rowKey . "_temp"] != $hidThumb) && ($POST["OptionVariantThumb_" . $rowKey . "_temp"] != ''))
                        {
                            $winnerThumbImage = "";
                            if ($winnerThumbImageType)
                                $winnerThumbImage = "img-" . $this->generate_random_unique(4) . substr($winnerThumbImageType, strpos($winnerThumbImageType, "."));
                            if ($winnerThumbImage)
                            {
                                
                                $ObjImage->copy_file($POST["OptionVariantThumb_" . $rowKey . "_temp"], '/' . __UPLOADS_FOLDER . 'watchvideo/' .$winnerThumbImage);
                                copy($POST["OptionVariantThumb_" . $rowKey . "_temp"], __CORE_PUBLIC_PATH . "/uploads/watchvideo/thumb/" . $winnerThumbImage);
                                $ObjImage->resizeImage(__CORE_PUBLIC_PATH . "/uploads/watchvideo/thumb/" . $winnerThumbImage,__CORE_PUBLIC_PATH . "/uploads/watchvideo/thumb/" . $winnerThumbImage, 296, 270);
                                $ObjImage->copy_file(__DIR_PUBLIC . "/uploads/watchvideo/thumb/" . $winnerThumbImage, '/' . __UPLOADS_FOLDER . 'watchvideo/thumb/' .$winnerThumbImage);                                                
                                @unlink(__DIR_PUBLIC . '/' . __UPLOADS_FOLDER . 'temp/' . $POST["OptionVariantThumb_" . $rowKey . "_temp"]); 
                                 
                            }
                          
                        }
                        else
                        {
                            if ($winnerThumbImageType != '')
                                $winnerThumbImage = $winnerThumbImageType;
                            else
                                $winnerThumbImage = '';
                        }
						$videoLink = $POST['videolink_' . $rowKey];
				if ($watchvideoDetID > -1) // Option already present in db. Update it
                        {
							if($watchvideoDetID != $POST['productCategoryID']){
							$this->ObjDatabase->setQuery("DELETE FROM " . DB_PREFIX . "watch_video WHERE productCategoryID='" . $watchvideoDetID . "'");
									   if(!empty($winnerThumbImage) && !empty($videoLink)) {    
										   $sql    = "
									INSERT INTO " . DB_PREFIX . "watch_video 
										(
										productCategoryID,
										Image,
										videoUrl,
										status,
										createdDate
										)
									VALUES
										(
										'" . $POST['productCategoryID'] . "',
										'" . $winnerThumbImage . "',
										'" . $videoLink . "',
										'A',
										NOW()
										)
									";
								$this->ObjDatabase->setQuery($sql);	
									   }
							}
                            //Image options
                            if(!empty($winnerThumbImage) && !empty($videoLink)) {
								
                                $this->ObjDatabase->setQuery("UPDATE " . DB_PREFIX . "watch_video
                                SET
								productCategoryID='" . $POST['productCategoryID'] . "',
								Image='" . $winnerThumbImage . "',			
                                videoUrl = '" . $videoLink . "'
                                WHERE productCategoryID = '" . $productCategoryID . "'");
                            } else {
                                $this->ObjDatabase->setQuery("DELETE FROM " . DB_PREFIX . "watch_video WHERE productCategoryID='" . $productCategoryID . "'");
                            }
                           
                        }
                else // New option Insertion
                 {		
				if(!empty($winnerThumbImage) && !empty($videoLink)) {
					       //Image options
                $sql    = "
					INSERT INTO " . DB_PREFIX . "watch_video 
						(
						productCategoryID,
						Image,
						videoUrl,
						status,
						createdDate
						)
					VALUES
						(
						'" . $POST['productCategoryID'] . "',
						'" . $winnerThumbImage . "',
						'" . $videoLink . "',
						'A',
						NOW()
						)
					";
				$this->ObjDatabase->setQuery($sql);
				}
			}
			}
		}
	}
	function generate_random_unique($length)
    {
        $random = '';
        for ($i = 0; $i < $length; $i++)
        {
            $random .= rand(0, 1) ? rand(0, 9) : chr(rand(ord('a'), ord('z')));
        }
        return $random;
    }
	public function getWatchvideo($selCategoryID){
	 $sql = " SELECT * FROM " . DB_PREFIX . "watch_video where productCategoryID='".(int)$selCategoryID."'";
       return $this->ObjDatabase->readValues($sql);
	}
	public function listwatchvideo(){
	 $sql = " SELECT wachVde.*,pdtCat.productCategoryKey FROM " . DB_PREFIX . "watch_video wachVde 
						LEFT JOIN 
						" . DB_PREFIX . "product_categories  pdtCat
							ON  
							(
								wachVde.productCategoryID=pdtCat.productCategoryID
							) where 1 GROUP BY productCategoryID DESC";
       return $this->ObjDatabase->readValues($sql);
	}
	/* public function getWatchvideoByID($videoID){
	 $sql = " SELECT * FROM " . DB_PREFIX . "watch_video where watchvideoID='".(int)$videoID."'";
       return $this->ObjDatabase->readValues($sql);
	}	 */
	public function deletewatchVideo($id){
		  $this->ObjDatabase->setQuery("DELETE FROM " . DB_PREFIX . "watch_video WHERE productCategoryID='" . $id . "'");
                        
	}
	public function changeStatus($winnerIDs, $status, $deletePrivilege) {
        if ($status == 'Deleted' && !$deletePrivilege)
            $status = 'Active';
        
        for ($i = 0; $i < count($winnerIDs); $i++) {
            $winnerID        = $winnerIDs[$i]; //echo $bannerID[0];echo '<br>';
            $sql             = "UPDATE 
							" . DB_PREFIX . "watch_video 
							SET 
								status	=	'" . $status . "'	
						WHERE	
								productCategoryID	=	'" . $winnerID . "'"; //print_r($sql);echo '<br>';
            $resChangeStatus = $this->ObjDatabase->setQuery($sql);
           
        } //exit;
        return $resChangeStatus;
    }
}
		