<?php
/**
 * This file is to upload the photos
 *
 * This file is having logoupload class to upload the photos
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###And video logo###
 * @author 		karthiselvam_75ag08
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-23
 *
 **/
require_once('../../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['anova_module'].'/admin/ansVideoLogo.php';
$CFG['mods']['include_files'][] = 'common/classes/class_Image.lib.php';

$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
//compulsory
$CFG['site']['is_module_page'] = $CFG['admin']['index']['anova_module'];
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
class logoupload extends AnovaHandler
	{
		/**
		 * logoupload::chkFileNameIsNotEmpty()
		 *
		 * @param string $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkFileNameIsNotEmpty($field_name, $err_tip = '')
			{
				if(!$_FILES[$field_name]['name'])
					{
						//$this->setFormFieldErrorTip($field_name,$err_tip);
						return false;
					}
				return true;
			}


          public function setIHObject($imObj)
			{
				$this->imageObj = $imObj;
			}

		/**
		 * logoupload::chkValidFileType()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkValidFileType($field_name, $err_tip = '')
			{
				$extern = strtolower(substr($_FILES[$field_name]['name'], strrpos($_FILES[$field_name]['name'], '.')+1));
				if (!in_array(strtolower($extern), $this->CFG['admin']['ans_videos']['logo_format_arr']))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * logoupload::chkValideFileSize()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkValideFileSize($field_name, $logo_size, $err_tip='')
			{
				$max_size = $logo_size * 1024;
				if ($_FILES[$field_name]['size'] > $max_size)
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * logoupload::chkErrorInFile()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkErrorInFile($field_name, $err_tip='')
			{
				if($_FILES[$field_name]['error'])
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * logoupload::resetFieldsArray()
		 *
		 * @return
		 */
		public function resetFieldsArray()
			{
				$this->setFormField('logo_id', '');
				$this->setFormField('logo_name', '');
				$this->setFormField('logo_description', '');
				$this->setFormField('logo_url', '');
				$this->setFormField('logo_position', 'Left_bottom');
				$this->setFormField('logo_transparency', '');
				$this->setFormField('logo_rollover_transparency', '');
				$this->setFormField('logo_image', '');
				$this->setFormField('logo_ext', '');
				$this->setFormField('logo_file', '');
				$this->setFormField('date_added', '');
				$this->setFormField('animated_logo', 'no');
				$this->setFormField('logo_status', 'yes');
			}

		/**
		 * PhotoEdit::populateLogoDetails()
		 *
		 * @return
		 **/
		public function populateLogoDetails()
			{
				$sql = 'SELECT logo_id, logo_name, logo_description, logo_url, logo_position, logo_transparency,'.
						' logo_rollover_transparency, logo_status, logo_image, logo_ext,'.
						' animated_logo FROM '.$this->CFG['db']['tbl']['ans_video_logo'];

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->fields_arr['logo_id'] = $row['logo_id'];
						$this->fields_arr['logo_name'] = $row['logo_name'];
						$this->fields_arr['logo_description'] = $row['logo_description'];
						$this->fields_arr['logo_url'] = $row['logo_url'];
						$this->fields_arr['logo_position'] = $row['logo_position'];
						$this->fields_arr['logo_transparency'] = $row['logo_transparency'];
						$this->fields_arr['logo_rollover_transparency'] = $row['logo_rollover_transparency'];
						$this->fields_arr['logo_image'] = $row['logo_image'];
						$this->fields_arr['logo_ext'] = $row['logo_ext'];
						$this->fields_arr['animated_logo'] = $row['animated_logo'];
						$this->fields_arr['logo_status'] = $row['logo_status'];
					}
			}

		/**
		 * logoupload::getLogoImage()
		 *
		 * @return
		 */
		public function getLogoImage()
			{
				switch($this->fields_arr['logo_ext'])
					{
						case 'jpg':
							$image_path = $this->CFG['site']['url'].$this->CFG['admin']['ans_videos']['logo_folder'].$this->fields_arr['logo_image'].'.'.$this->fields_arr['logo_ext'];
							break;

						case 'swf':
							$image_path = $this->CFG['site']['url'].'design/templates/'.$this->CFG['html']['template']['default'].'/images/swflogo.jpg';
							break;

						default:
							$image_path = $this->CFG['site']['url'].'design/templates/'.$this->CFG['html']['template']['default'].'/images/nologo.jpg';
							break;
					}
					return $image_path;
			}

		/**
		 * logoupload::updateLogoTable()
		 *
		 * @return
		 */
		public function updateLogoTable()
			{
				$add_arr = array();
				$add_field = '';
				if($this->logoUpload)
					{
     					$extern = strtolower(substr($_FILES['logo_file']['name'], strrpos($_FILES['logo_file']['name'], '.')+1));
     					$dir = '../'.$this->CFG['admin']['ans_videos']['logo_folder'];
     					$this->setFormField('logo_ext',$extern);
          				$this->setFormField('logo_image',$this->CFG['admin']['ans_videos']['logo_name']);
                              $this->chkAndCreateFolder($dir);

                              if($extern == 'swf' OR $extern == 'flv')
                                   {
                                        move_uploaded_file($_FILES['logo_file']['tmp_name'],$dir.$this->CFG['admin']['ans_videos']['logo_name'].'.'.strtolower($extern));
                                   }
                              else
                                   {
                    				$imageObj = new ImageHandler($_FILES['logo_file']['tmp_name']);
                    				$this->setIHObject($imageObj);
                    				$image_name = $this->CFG['admin']['ans_videos']['logo_name'];
                                        $file = $dir.'/'.$image_name;
                                        $this->imageObj->resize($this->CFG['admin']['ans_videos']['logo_width'], $this->CFG['admin']['ans_videos']['logo_height'], '0');
                    				$this->imageObj->output_resized($file.'.'.$extern, strtoupper($extern));
                                   }

                              $add_arr = array($this->CFG['admin']['ans_videos']['logo_name'], $extern);
     					$add_field = ', logo_image='.$this->dbObj->Param('logo_image').','.
     										' logo_ext='.$this->dbObj->Param('logo_ext');
                         }

				$sql = 'SELECT 1 FROM '.$this->CFG['db']['tbl']['ans_video_logo'];

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				if($rs->PO_RecordCount())
					{
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video_logo'].' SET'.
								' logo_name='.$this->dbObj->Param('logo_name').','.
								' logo_description='.$this->dbObj->Param('logo_description').','.
								' animated_logo='.$this->dbObj->Param('animated_logo').','.
								' logo_status='.$this->dbObj->Param('logo_status').','.
								' logo_url='.$this->dbObj->Param('logo_url').','.
								' logo_position='.$this->dbObj->Param('logo_position').','.
								' logo_transparency='.$this->dbObj->Param('logo_transparency').','.
								' logo_rollover_transparency='.$this->dbObj->Param('logo_rollover_transparency').$add_field;
						$array = array($this->fields_arr['logo_name'], $this->fields_arr['logo_description'],
										$this->fields_arr['animated_logo'], $this->fields_arr['logo_status'],
                                                  $this->fields_arr['logo_url'], $this->fields_arr['logo_position'],
                                                  $this->fields_arr['logo_transparency'], $this->fields_arr['logo_rollover_transparency']);

						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array_merge($array, $add_arr));
						if (!$rs)
							trigger_db_error($this->dbObj);
					}
				else
					{
						$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_video_logo'].' SET'.
								' logo_name='.$this->dbObj->Param('logo_name').','.
								' logo_description='.$this->dbObj->Param('logo_description').','.
								' animated_logo='.$this->dbObj->Param('animated_logo').','.
								' logo_status='.$this->dbObj->Param('logo_status').','.
								' logo_url='.$this->dbObj->Param('logo_url').','.
								' logo_position='.$this->dbObj->Param('logo_position').','.
								' logo_transparency='.$this->dbObj->Param('logo_transparency').','.
								' logo_rollover_transparency='.$this->dbObj->Param('logo_rollover_transparency').','.
								' date_added=NOW()'.$add_field;
						$array = array($this->fields_arr['logo_name'], $this->fields_arr['logo_description'],
										$this->fields_arr['animated_logo'], $this->fields_arr['logo_status'],
                                                  $this->fields_arr['logo_url'], $this->fields_arr['logo_position'],
                                                  $this->fields_arr['logo_transparency'], $this->fields_arr['logo_rollover_transparency']);

						$stmt = $this->dbObj->Prepare($sql);
						$rs = $this->dbObj->Execute($stmt, array_merge($array, $add_arr));
						if (!$rs)
							trigger_db_error($this->dbObj);
					}
			}

		/**
		 * logoupload::isValidURL()
		 *
		 * @param mixed $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function isValidURL($field_name,$err_tip='')
			{
				# $isValid = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $this->fields_arr[$field_name]);
				 $urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
				 if (eregi($urlregex, $this->fields_arr[$field_name]))
					{
						return true;
					}
				else
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
			}
	}
//<<<<<-------------- Class logoupload begins ---------------//
//-------------------- Code begins -------------->>>>>//
$logoupload = new logoupload();
$logoupload->left_navigation_div = 'anovaVideoPlayer';
$logoupload->logo_position_array = array('Left_bottom'=>$LANG['left_bottom'], 'Left_top'=>$LANG['left_top'], 'Right_bottom'=>$LANG['right_bottom'], 'Right_top'=>$LANG['right_top']);
$logoupload->setPageBlockNames(array('logo_upload_form'));
//default form fields and values...
$logoupload->resetFieldsArray();
$logoupload->setAllPageBlocksHide();
$logoupload->setPageBlockShow('logo_upload_form');
$logoupload->sanitizeFormInputs($_REQUEST);
if(isDemoSite() AND $logoupload->isFormPOSTed($_POST, 'upload'))
	{
		$logoupload->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$logoupload->setPageBlockShow('block_msg_form_error');
	}
elseif($logoupload->isFormPOSTed($_POST, 'upload'))
	{
		$logoupload->logoUpload = $logoupload->chkFileNameIsNotEmpty('logo_file', $LANG['err_tip_compulsory']) and
			$logoupload->chkValidFileType('logo_file',$LANG['err_tip_invalid_file_type']) and
			$logoupload->chkValideFileSize('logo_file', $CFG['admin']['ans_videos']['logo_max_size'],$LANG['err_tip_invalid_file_size']) and
			$logoupload->chkErrorInFile('logo_file',$LANG['err_tip_invalid_file']);
			$logoupload->isValidURL('logo_url',$LANG['err_tip_invalid_url']);
		$logoupload->getFormField('logo_transparency') and
			$logoupload->chkIsNumeric('logo_transparency',$LANG['err_tip_invalid']);
		$logoupload->getFormField('logo_rollover_transparency') and
			$logoupload->chkIsNumeric('logo_rollover_transparency',$LANG['err_tip_invalid']);
		if($logoupload->isValidFormInputs())
			{
				$logoupload->updateLogoTable();
				$logoupload->setCommonSuccessMsg($LANG['msg_success_updated']);
				$logoupload->setPageBlockShow('block_msg_form_success');
				Redirect2URL($CFG['site']['url'].'admin/ansVideoLogo.php?msg=success');
			}
		else
			{
				$logoupload->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$logoupload->setPageBlockShow('block_msg_form_error');
			}
	}
else
	{
		$logoupload->populateLogoDetails();
	}
if($logoupload->isFormPOSTed($_GET, 'msg'))
	{
		$logoupload->setCommonSuccessMsg($LANG['msg_success_updated']);
		$logoupload->setPageBlockShow('block_msg_form_success');
	}
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
if ($logoupload->isShowPageBlock('logo_upload_form'))
    {
		$logoupload->logo_upload_form['getLogoImage']	=	$logoupload->getLogoImage();
		$logoupload->logo_upload_form['implode_logo_format_arr']	=	implode(', ', $CFG['admin']['ans_videos']['logo_format_arr']);
		$logoupload->logo_upload_form['hidden_arr']	=	array('logo_ext', 'logo_image');
		$logoupload->LANG['logo_dimension'] = str_ireplace('VAR_HEIGHT',  $CFG['admin']['ans_videos']['logo_height'],  $LANG['logo_dimension']);
          $logoupload->LANG['logo_dimension']	=	str_ireplace('VAR_WIDTH',  $CFG['admin']['ans_videos']['logo_width'],  $logoupload->LANG['logo_dimension']);
	}
//include the header file
$logoupload->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('ansVideoLogo.tpl');
//includ the footer of the page
?>
 <script language="javascript" type="text/javascript">
	needLiveValidation('logo_url',Array('Presence'));
	needLiveValidation('logo_transparency',Array('Numericality'));
	needLiveValidation('logo_rollover_transparency',Array('Numericality'));
 </script>
<?php
//<<<<<-------------------- Page block templates ends -------------------//
$logoupload->includeFooter();
?>