<?php
/**
 * This file is to upload the photos
 *
 * This file is having advertisement class to upload the photos
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Ans video advertisment###
 * @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/ansVideoAdvertisement.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.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;
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
require($CFG['site']['project_path'].'common/application_top.inc.php');
class advertisement extends AnovaHandler
	{
		/**
		 * ManageFolder::buildSortQuery()
		 *
		 * @return
		 **/
		public function buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}

		/**
		 * advertisement::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;
			}

		/**
		 * advertisement::chkFileNameIsNotEmpty()
		 *
		 * @param string $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkFileNameIsNotEmptyEdit($field_name, $err_tip = '')
			{
				if(!$_FILES[$field_name]['name'])
					{
						//$this->setFormFieldErrorTip($field_name,$err_tip);
						return false;
					}
				return true;
			}

		/**
		 * advertisement::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']['advertisement_format_arr']))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

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

		/**
		 * advertisement::chkValideFileSize()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkValidVideoTime($field_name, $err_tip='')
			{
				$max_time = $this->CFG['admin']['ans_videos']['advertisement_max_time'];
				if ($this->getFormField('advertisement_duration') > $max_time)
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		public function CheckVideoURL($url, $err_tip='')
			{
				if (!strstr($this->getFormField('advertisement_url'), 'http://'))
					{
						$this->fields_arr['advertisement_url'] = 'http://'.$this->fields_arr['advertisement_url'];
					}
				$pattern = '#http\:\/\/[aA-zZ0-9\.]+\.[aA-zZ\.]+#';
				$pattern1 = '#https\:\/\/[aA-zZ0-9\.]+\.[aA-zZ\.]+#';
				if(preg_match($pattern, $this->getFormField('advertisement_url')) || preg_match($pattern1, $this->getFormField('advertisement_url')))
					return true;
				else
					{
						$this->fields_err_tip_arr[$url] = $err_tip;
						return false;
					}
			}

		/**
		 * advertisement::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;
			}

		/**
		 * advertisement::resetFieldsArray()
		 *
		 * @return
		 */
		public function resetFieldsArray()
			{
				$this->setFormField('aid', '');
				$this->setFormField('advertisement_name', '');
				$this->setFormField('advertisement_file', '');
				$this->setFormField('advertisement_description', '');
				$this->setFormField('advertisement_url', '');
				$this->setFormField('advertisement_duration', '');
				$this->setFormField('advertisement_expiry_date', '');
				$this->setFormField('advertisement_impressions', '0');
				$this->setFormField('advertisement_channel', array());
				$this->setFormField('advertisement_image', '');
				$this->setFormField('advertisement_ext', '');
				$this->setFormField('advertisement_show_at', 'Beginning');
				$this->setFormField('add_type', 'General');
				$this->setFormField('advertisement_status', '');
				$this->setFormField('date_added', '');
				$this->setFormField('month', '');
				$this->setFormField('day', '');
				$this->setFormField('year', '');
				$this->setFormField('act', '');
				$this->setFormField('views_revenue', '');
				$this->setFormField('clicks_revenue', '');
				$this->setFormField('msg', '');
			}

		/**
		 * PhotoEdit::populateadvertisementDetails()
		 *
		 * @return
		 **/
		public function populateadvertisementDetails()
			{
				$sql = 'SELECT advertisement_id, advertisement_name, advertisement_description, advertisement_url,'.
						' advertisement_duration, advertisement_expiry_date,'.
						' advertisement_impressions, advertisement_channel, advertisement_image,'.
						' advertisement_ext, advertisement_show_at, add_type, advertisement_status,'.
						' views_revenue, clicks_revenue'.
						' FROM '.$this->CFG['db']['tbl']['ans_video_advertisement'].' WHERE'.
						' advertisement_id='.$this->dbObj->Param('advertisement_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($this->fields_arr['aid']));
				if (!$rs)
					trigger_db_error($this->dbObj);

				if($row = $rs->FetchRow())
					{
						$this->fields_arr['advertisement_name'] = $row['advertisement_name'];
						$this->fields_arr['advertisement_description'] = $row['advertisement_description'];
						$this->fields_arr['advertisement_url'] = $row['advertisement_url'];
						$this->fields_arr['advertisement_duration'] = $row['advertisement_duration'];
						$this->fields_arr['advertisement_expiry_date'] = $row['advertisement_expiry_date'];
						$this->fields_arr['advertisement_impressions'] = $row['advertisement_impressions'];
						$this->fields_arr['advertisement_channel'] = $row['advertisement_channel'];
						$this->fields_arr['advertisement_image'] = $row['advertisement_image'];
						$this->fields_arr['advertisement_ext'] = $row['advertisement_ext'];
						$this->fields_arr['advertisement_show_at'] = $row['advertisement_show_at'];
						$this->fields_arr['add_type'] = $row['add_type'];
						$this->fields_arr['advertisement_status'] = $row['advertisement_status'];
						$this->fields_arr['views_revenue'] = $row['views_revenue'];
						$this->fields_arr['clicks_revenue'] = $row['clicks_revenue'];
						$advertisement_expiry_date = explode(' ', $row['advertisement_expiry_date']);
						$advertisement_expiry_date = explode('-', $advertisement_expiry_date[0]);
						$this->fields_arr['year'] = $advertisement_expiry_date[0];
						$this->fields_arr['month'] = $advertisement_expiry_date[1];
						$this->fields_arr['day'] = $advertisement_expiry_date[2];
						$this->fields_arr['advertisement_channel'] = explode(',',$row['advertisement_channel']);
						return true;
					}
				return false;
			}

		/**
		 * advertisement::getAdsDetailsImage()
		 *
		 * @return
		 */
		public function getAdsDetailsImage()
			{
				switch($this->fields_arr['advertisement_ext'])
					{
						case 'jpg':
							$image_path = $this->CFG['site']['url'].$this->CFG['admin']['ans_videos']['advertisement_folder'].$this->fields_arr['advertisement_image'].'.'.$this->fields_arr['advertisement_ext'];
							break;

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

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

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

		/**
		 * ManageDeleted::removeFiles()
		 *
		 * @param $file
		 * @return
		 **/
		public function removeFiles($file)
			{
				if(is_file($file))
					{
						unlink($file);
						return true;
					}
				return false;
			}

		/**
		 * advertisement::deleteFiles()
		 *
		 * @param mixed $image_name
		 * @return
		 */
		public function deleteFiles($image_name)
			{
				$this->removeFiles($image_name.'.flv');
				$this->removeFiles($image_name.'.jpg');
				$this->removeFiles($image_name.'.swf');
			}

		/**
		 * advertisement::updateAdvertisementTable()
		 *
		 * @return
		 */
		public function updateAdvertisementTable()
			{
				$add_arr = array();
				$add_field = '';
				$expiry_date = $this->getFormField('year').'-'.$this->getFormField('month').'-'.$this->getFormField('day');
				$channel = implode(',', $this->fields_arr['advertisement_channel']);
				if($this->advertisement)
					{
						$extern = strtolower(substr($_FILES['advertisement_file']['name'], strrpos($_FILES['advertisement_file']['name'], '.')+1));
						$image_name = getImageName($this->fields_arr['aid']);
						$dir = '../'.$this->CFG['admin']['ans_videos']['advertisement_folder'];
						$this->chkAndCreateFolder($dir);
						$this->deleteFiles($dir.$image_name);
						move_uploaded_file($_FILES['advertisement_file']['tmp_name'],$dir.$image_name.'.'.strtolower($extern));
						$add_arr = array($image_name, $extern);
						$add_field = ', advertisement_image='.$this->dbObj->Param('advertisement_image').','.
										' advertisement_ext='.$this->dbObj->Param('advertisement_ext');
					}
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video_advertisement'].' SET'.
						' advertisement_name='.$this->dbObj->Param('advertisement_name').','.
						' advertisement_description='.$this->dbObj->Param('advertisement_description').','.
						' advertisement_url='.$this->dbObj->Param('advertisement_url').','.
						' advertisement_duration='.$this->dbObj->Param('advertisement_duration').','.
						' advertisement_expiry_date='.$this->dbObj->Param('advertisement_expiry_date').','.
						' advertisement_impressions='.$this->dbObj->Param('advertisement_impressions').','.
						' advertisement_channel='.$this->dbObj->Param('advertisement_channel').','.
						' advertisement_show_at='.$this->dbObj->Param('advertisement_show_at').','.
						' add_type='.$this->dbObj->Param('add_type').','.
						' advertisement_status='.$this->dbObj->Param('advertisement_status').','.
						' views_revenue='.$this->dbObj->Param('views_revenue').','.
						' clicks_revenue='.$this->dbObj->Param('clicks_revenue').$add_field.
						' WHERE advertisement_id=\''.addslashes($this->fields_arr['aid']).'\'';

				$array = array($this->fields_arr['advertisement_name'], $this->fields_arr['advertisement_description'],
								 $this->fields_arr['advertisement_url'], $this->fields_arr['advertisement_duration'],
								 $expiry_date, $this->fields_arr['advertisement_impressions'],
								 $channel, $this->fields_arr['advertisement_show_at'],
								 $this->fields_arr['add_type'], $this->fields_arr['advertisement_status'],
								 $this->fields_arr['views_revenue'],
								 $this->fields_arr['clicks_revenue']
								 );

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

		/**
		 * advertisement::insertAdvertisementTable()
		 *
		 * @return
		 */
		public function insertAdvertisementTable()
			{
				$expiry_date = $this->getFormField('year').'-'.$this->getFormField('month').'-'.$this->getFormField('day');
				$channel = implode(',', $this->fields_arr['advertisement_channel']);
				$sql = 'INSERT INTO '.$this->CFG['db']['tbl']['ans_video_advertisement'].' SET'.
						' advertisement_name='.$this->dbObj->Param('advertisement_name').','.
						' user_id='.$this->dbObj->Param('user_id').','.
						' advertisement_description='.$this->dbObj->Param('advertisement_description').','.
						' advertisement_url='.$this->dbObj->Param('advertisement_url').','.
						' advertisement_duration='.$this->dbObj->Param('advertisement_duration').','.
						' advertisement_expiry_date='.$this->dbObj->Param('advertisement_expiry_date').','.
						' advertisement_impressions='.$this->dbObj->Param('advertisement_impressions').','.
						' advertisement_channel='.$this->dbObj->Param('advertisement_channel').','.
						' advertisement_show_at='.$this->dbObj->Param('advertisement_show_at').','.
						' add_type='.$this->dbObj->Param('add_type').','.
						' advertisement_status='.$this->dbObj->Param('advertisement_status').','.
						' views_revenue='.$this->dbObj->Param('views_revenue').','.
						' clicks_revenue='.$this->dbObj->Param('clicks_revenue').','.
						' date_added=NOW()';
				$array = array($this->fields_arr['advertisement_name'], $this->CFG['user']['user_id'], $this->fields_arr['advertisement_description'],
								 $this->fields_arr['advertisement_url'], $this->fields_arr['advertisement_duration'],
								 $expiry_date, $this->fields_arr['advertisement_impressions'],
								 $channel, $this->fields_arr['advertisement_show_at'],
								 $this->fields_arr['add_type'], $this->fields_arr['advertisement_status'],
								 $this->fields_arr['views_revenue'],
								 $this->fields_arr['clicks_revenue']
								 );

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

				if($advertisement_id = $this->dbObj->Insert_ID())
					{
						$extern = strtolower(substr($_FILES['advertisement_file']['name'], strrpos($_FILES['advertisement_file']['name'], '.')+1));
						$image_name = getImageName($advertisement_id);
						$dir = '../'.$this->CFG['admin']['ans_videos']['advertisement_folder'];
						$this->chkAndCreateFolder($dir);
						$this->deleteFiles($dir.$image_name);
						move_uploaded_file($_FILES['advertisement_file']['tmp_name'],$dir.$image_name.'.'.strtolower($extern));
						$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video_advertisement'].' SET'.
								' advertisement_image='.$this->dbObj->Param('advertisement_image').','.
								' advertisement_ext='.$this->dbObj->Param('advertisement_ext').' WHERE'.
								' advertisement_id='.$this->dbObj->Param('advertisement_id');

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

		/**
		 * advertisement::checkValidDate()
		 *
		 * @param mixed $err_tip
		 * @return
		 */
		public function checkValidDate($err_tip)
			{
				if(!$this->fields_arr['month'] and !$this->fields_arr['day'] and !$this->fields_arr['year'])
					return true;
				if($this->fields_arr['month'] and $this->fields_arr['day'] and $this->fields_arr['year'])
					{
						if(checkdate($this->fields_arr['month'],$this->fields_arr['day'],$this->fields_arr['year']))
							return true;
					}
				$this->fields_err_tip_arr['month'] = $err_tip;
				return false;
			}

		/**
		 * advertisement::chkIsEditMode()
		 *
		 * @return
		 */
		public function chkIsEditMode()
			{
				if($this->fields_arr['aid'] and $this->fields_arr['act']=='edit')
					return true;
				return false;
			}

		/**
		 * advertisement::populateAdvertisementList()
		 *
		 * @return
		 */
		public function populateAdvertisementList()
			{
				$this->populateAdvertisementList_arr	=	array();
				$inc	=	0;
				while($row = $this->fetchResultRecord())
					{
						$this->populateAdvertisementList_arr[$inc]['record']	=	$row;
						($row['advertisement_status']=='Activate')? $this->populateAdvertisementList_arr[$inc]['option']	=	$this->LANG['common_display_active'] : $this->populateAdvertisementList_arr[$inc]['option']	=	$this->LANG['common_display_inactive'];
						$this->populateAdvertisementList_arr[$inc]['ansVideoAdvertisement_url']	=	'ansVideoAdvertisement.php?act=edit&amp;aid='.$row['advertisement_id'].'&amp;start='.$this->fields_arr['start'];
						$inc++;
					}
				return 	$this->populateAdvertisementList_arr;
			}

		/**
		 * advertisement::deleteAdvertisement()
		 *
		 * @return
		 */
		public function deleteAdvertisement()
			{
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['ans_video_advertisement'].' WHERE'.
						' advertisement_id IN('.$this->fields_arr['aid'].')';

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

				$dir = '../'.$this->CFG['admin']['ans_videos']['advertisement_folder'];
				$array = explode(',', $this->fields_arr['aid']);
				foreach($array as $key=>$value)
					{
						$image_name = getImageName($value);
						$this->deleteFiles($dir.$image_name);
					}
			}

		/**
		 * advertisement::changeStatusAdvertisement()
		 *
		 * @param mixed $status
		 * @return
		 */
		public function changeStatusAdvertisement($status)
			{
				$sql = 'UPDATE '.$this->CFG['db']['tbl']['ans_video_advertisement'].' SET'.
						' advertisement_status='.$this->dbObj->Param('advertisement_status').' WHERE'.
						' advertisement_id IN('.$this->fields_arr['aid'].')';

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

		/**
		 * advertisement::defaultValidation()
		 *
		 * @return
		 */
		public function defaultValidation()
			{
				$this->checkValidDate($this->LANG['err_tip_invalid_date']);
				$this->chkIsNotEmpty('advertisement_name', $this->LANG['err_tip_compulsory']) and $this->chkIsNotEmpty('advertisement_name',$this->LANG['err_tip_invalid']);
				$this->chkIsNotEmpty('advertisement_duration', $this->LANG['err_tip_compulsory']) and $this->chkIsNumeric('advertisement_duration',$this->LANG['header_err_tip_invalid_number']);
			}
	}
//<<<<<-------------- Class advertisement begins ---------------//
//-------------------- Code begins -------------->>>>>//
$advertisement = new advertisement();
$advertisement->left_navigation_div = 'anovaVideoPlayer';
$advertisement->advertisement_status_array = array('Activate'=>$LANG['common_activate'], 'Inactive'=>$LANG['common_inactivate']);
$advertisement->setPageBlockNames(array('advertisement_list', 'advertisement_upload_form'));
//default form fields and values...
$advertisement->resetFieldsArray();
/*********** Page Navigation Start *********/
$advertisement->setFormField('start', '0');
$advertisement->setFormField('orderby', 'DESC');
$advertisement->setFormField('orderby_field', 'advertisement_id');
$advertisement->setFormField('numpg',$CFG['data_tbl']['numpg']);
$advertisement->setMinRecordSelectLimit(2);
$advertisement->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$advertisement->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
$advertisement->setTableNames(array($CFG['db']['tbl']['ans_video_advertisement']));
$advertisement->setReturnColumns(array('advertisement_id', 'user_id', 'advertisement_name', 'advertisement_description', 'advertisement_url', 'advertisement_duration', 'DATE_FORMAT(advertisement_expiry_date,\''.$CFG['format']['date'].'\') AS advertisement_expiry_date', 'advertisement_impressions', 'advertisement_channel', 'advertisement_image', 'advertisement_ext', 'advertisement_show_at', 'add_type', 'advertisement_status', 'DATE_FORMAT(date_added,\''.$CFG['format']['date'].'\') AS date_added', 'advertisement_current_impressions', 'views_revenue', 'clicks_revenue', 'site_earnings', 'members_earnings'));
/************ page Navigation stop *************/
$advertisement->setAllPageBlocksHide();
$advertisement->sanitizeFormInputs($_REQUEST);
$advertisement->setPageBlockShow('advertisement_list');
if(isDemoSite() AND ($advertisement->isFormPOSTed($_POST, 'add') OR $advertisement->isFormPOSTed($_POST, 'update') OR $advertisement->isFormPOSTed($_POST, 'yes')))
	{
		$advertisement->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$advertisement->setAllPageBlocksHide();
		$advertisement->setPageBlockShow('block_msg_form_error');
		$advertisement->setPageBlockShow('advertisement_list');
	}
elseif($advertisement->isFormPOSTed($_POST, 'add'))
	{
		$advertisement->chkFileNameIsNotEmpty('advertisement_file', $LANG['err_tip_compulsory']) and
			$advertisement->chkValidFileType('advertisement_file',$LANG['err_tip_invalid_file_type']) and
			$advertisement->chkValideFileSize('advertisement_file',$LANG['err_tip_invalid_file_size']) and
			$advertisement->chkErrorInFile('advertisement_file',$LANG['err_tip_invalid_file']);
		$advertisement->defaultValidation();
		$advertisement->chkValidVideoTime('advertisement_duration',$LANG['err_tip_invalid_file_time']);
		$advertisement->getFormField('advertisement_url') and
			$advertisement->CheckVideoURL('advertisement_url',$LANG['err_tip_invalid_video_url']);
		if($advertisement->isValidFormInputs())
			{
				$advertisement->insertAdvertisementTable();
				$advertisement->setCommonSuccessMsg($LANG['msg_success_added']);
				$advertisement->setPageBlockShow('block_msg_form_success');
				Redirect2URL($CFG['site']['relative_url'].'ansVideoAdvertisement.php?msg=added');
			}
		else
			{
				$advertisement->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$advertisement->setAllPageBlocksHide();
				$advertisement->setPageBlockShow('block_msg_form_error');
				$advertisement->setPageBlockShow('advertisement_upload_form');
			}
	}
else if($advertisement->isFormPOSTed($_POST, 'update'))
	{
		$advertisement->advertisement = $advertisement->chkFileNameIsNotEmptyEdit('advertisement_file', $LANG['err_tip_compulsory']) and
			$advertisement->chkValidFileType('advertisement_file',$LANG['err_tip_invalid_file_type']) and
			$advertisement->chkValideFileSize('advertisement_file',$LANG['err_tip_invalid_file_size']) and
			$advertisement->chkErrorInFile('advertisement_file',$LANG['err_tip_invalid_file']);
		$advertisement->defaultValidation();
		$advertisement->chkValidVideoTime('advertisement_duration',$LANG['err_tip_invalid_file_time']);
		$advertisement->CheckVideoURL('advertisement_url',$LANG['err_tip_invalid_video_url']);
		if($advertisement->isValidFormInputs())
			{
				$advertisement->updateAdvertisementTable();
				$advertisement->setCommonSuccessMsg($LANG['msg_success_updated']);
				$advertisement->setPageBlockShow('block_msg_form_success');
				Redirect2URL($CFG['site']['relative_url'].'ansVideoAdvertisement.php?msg=updated');
			}
		else
			{
				$advertisement->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$advertisement->setAllPageBlocksHide();
				$advertisement->setPageBlockShow('block_msg_form_error');
				$advertisement->setPageBlockShow('advertisement_upload_form');
			}
	}
else if($advertisement->isFormPOSTed($_POST, 'cancel'))
	{
		Redirect2URL($CFG['site']['relative_url'].'ansVideoAdvertisement.php');
	}
else if($advertisement->isFormPOSTed($_POST, 'yes'))
	{
		switch($advertisement->getFormField('act'))
			{
				case 'Delete':
					$advertisement->deleteAdvertisement();
					$advertisement->setCommonSuccessMsg($LANG['msg_success_deleted']);
					$advertisement->setPageBlockShow('block_msg_form_success');
					break;

				case 'Activate':
					$advertisement->changeStatusAdvertisement('Activate');
					$advertisement->setCommonSuccessMsg($LANG['msg_success_activated']);
					$advertisement->setPageBlockShow('block_msg_form_success');
					break;

				case 'Inactivate':
					$advertisement->changeStatusAdvertisement('Inactive');
					$advertisement->setCommonSuccessMsg($LANG['msg_success_inactivated']);
					$advertisement->setPageBlockShow('block_msg_form_success');
					break;
			}
	}
else if($advertisement->isFormPOSTed($_GET, 'act'))
	{
		switch($advertisement->getFormField('act'))
			{
				case 'add':
					$advertisement->LANG['page_title'] = $LANG['page_add_title'];
					$advertisement->setAllPageBlocksHide();
					$advertisement->setPageBlockShow('advertisement_upload_form');
					break;

				case 'edit':
					if($advertisement->chkIsEditMode())
						{
							if($advertisement->populateadvertisementDetails())
								{
									$advertisement->LANG['page_title'] = $LANG['page_edit_title'];
									$advertisement->setAllPageBlocksHide();
									$advertisement->setPageBlockShow('advertisement_upload_form');
								}
							else
								{
									$advertisement->setCommonErrorMsg($LANG['common_msg_error_sorry']);
									$advertisement->setPageBlockShow('block_msg_form_error');
								}
						}
					break;
			}
	}
if ($advertisement->isFormPOSTed($_GET, 'msg'))
	{
		$advertisement->setPageBlockShow('block_msg_form_success');
		switch($advertisement->getFormField('msg'))
			{
				case 'added':
					$advertisement->setCommonSuccessMsg($LANG['msg_success_added']);
					break;

				case 'updated':
					$advertisement->setCommonSuccessMsg($LANG['msg_success_updated']);
					break;
			}
	}
//<<<<<-------------------- Code ends----------------------//
//-------------------- Page block templates begins -------------------->>>>>//
if ($advertisement->isShowPageBlock('advertisement_upload_form'))
    {
		if($CFG['admin']['ans_video_advertisement_impressions'])
			{
				$advertisement->advertisement_upload_form['populateBWNumbers_arg1']	=	date('Y')-20;
				$advertisement->advertisement_upload_form['populateBWNumbers_arg2']	=	date('Y')+20;
			}
		if($advertisement->chkIsEditMode())
			{
				$advertisement->advertisement_upload_form['getAdsDetailsImage']	=	$advertisement->getAdsDetailsImage();
				$advertisement->chkIsEditMode['hidden_array']	=	array('advertisement_image', 'advertisement_ext', 'aid', 'start', 'act');
			}
		$advertisement->advertisement_upload_form['implode_ans_videos']	=	implode(', ', $CFG['admin']['ans_videos']['advertisement_format_arr']);
	}
if ($advertisement->isShowPageBlock('advertisement_list'))
    {
		/****** navigtion continue*********/
		$advertisement->buildSelectQuery();
		$advertisement->buildConditionQuery();
		$advertisement->buildSortQuery();
		$advertisement->buildQuery();
		$advertisement->executeQuery();
		if($advertisement->isResultsFound())
			{
				$advertisement->advertisement_list['RecordCount']	=	1;
				$advertisement->advertisement_list['hidden_array']	=	array('orderby','orderby_field','start');
				$advertisement->advertisement_list['anchor']	=	'dAltMlti';
				$advertisement->advertisement_list['populateAdvertisementList']	=	$advertisement->populateAdvertisementList();
				$advertisement->advertisement_list['delete_Confirmation']	=	'{Confirmation(\'selMsgConfirm\', \'actionForm\', Array(\'aid\', \'act\', \'selMsgText\'), Array(multiCheckValue, \'Delete\', \''.$LANG['delete_confirmation'].'\'), Array(\'value\', \'value\', \'innerHTML\'));}';
				$advertisement->advertisement_list['activate_Confirmation']	=	'{Confirmation(\'selMsgConfirm\', \'actionForm\', Array(\'aid\', \'act\', \'selMsgText\'), Array(multiCheckValue, \'Activate\', \''.$LANG['activate_confirmation'].'\'), Array(\'value\', \'value\', \'innerHTML\'));}';
				$advertisement->advertisement_list['inactivate_Confirmation']	=	'{Confirmation(\'selMsgConfirm\', \'actionForm\', Array(\'aid\', \'act\', \'selMsgText\'), Array(multiCheckValue, \'Inactivate\', \''.$LANG['inactivate_confirmation'].'\'), Array(\'value\', \'value\', \'innerHTML\'));}';
				$smartyObj->assign('smarty_paging_list', $advertisement->populatePageLinksGET($advertisement->getFormField('start')));
			}
		else
			{
				$advertisement->advertisement_list['RecordCount']	=	0;
			}
	}
//include the header file
$advertisement->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('ansVideoAdertisement.tpl');
//includ the footer of the page
?>
<script type="text/javascript" language="javascript">
	var block_arr= new Array('selMsgConfirm');
</script>
<?php
//<<<<<-------------------- Page block templates ends -------------------//
$advertisement->includeFooter();
?>