<?php
/**
 * This file is to manage avatars
 *
 * PHP version 5.0
 *
 * @avatar	Agriya Answers
 * @package		Admin
 * @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-11-19
 */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/admin/logoSettings.php';
$CFG['mods']['include_files'][] = 'common/classes/class_Image.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_FormHandler.lib.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';

//compulsory
$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 ManageLogoHandler-------------------->>>
/**
 * @avatar	Agriya Answers
 * @package		ManageLogoHandler
 * @author 		karthiselvam_75ag08
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		Release: @package_version@
 * @since 		2008-11-18
 */
class ManageLogoHandler extends FormHandler
	{


		/**
		 * PhotoUpload::setIHObject()
		 *
		 * @param $imObj
		 * @return
		 **/
		public function setIHObject($imObj)
			{
				$this->imageObj = $imObj;
			}

		/**
		 * chkValidFileType()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkValidLogoFileType($field_name, $err_tip = '')
			{
				$this->EXTERN = strtolower($_FILES[$field_name]['type']);
				if (!in_array($this->EXTERN, $this->CFG['admin']['site_logo']['format_arr']))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}

		/**
		 * chkValidFileType()
		 *
		 * @param $field_name
		 * @param string $err_tip
		 * @return
		 **/
		public function chkValidFaviconFileType($field_name, $err_tip = '')
			{
				$this->EXTERN = strtolower($_FILES[$field_name]['type']);
				if (!in_array($this->EXTERN, $this->CFG['admin']['site_favicon']['format_arr']))
					{
						$this->fields_err_tip_arr[$field_name] = $err_tip;
						return false;
					}
				return true;
			}


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

		/**
		 * PhotoUpload::storeImagesTempServer()
		 *
		 * @param $uploadUrl
		 * @param $extern
		 * @return
		 **/
		public function storeLogoImagesTempServer($uploadUrl, $extern)
			{
				//GET THUMB IMAGE
				$this->CFG['admin']['site_logo']['width'].'.'. $extern;
				$this->imageObj->resize($this->CFG['admin']['site_logo']['width'], $this->CFG['admin']['site_logo']['height'], '-');
				$this->imageObj->output_resized($uploadUrl.'.'.$extern, strtoupper($extern));
			}

		/**
		 * EditPersonalProfileFormHandler::photoUpload()
		 *
		 * @return default_screen
		 **/
		public function logoUpload()
			{
				$extern = strtolower(substr($_FILES['logo']['name'], strrpos($_FILES['logo']['name'], '.')+1));
				$this->setFormField('photo_ext',$extern);
				$image_name = 'logo';
				$imageObj = new ImageHandler($_FILES['logo']['tmp_name']);
				$this->setIHObject($imageObj);
				$temp_dir = '../'.$this->CFG['admin']['site_logo']['temp_folder'];
				$this->chkAndCreateFolder($temp_dir);
				$temp_file = $temp_dir.$image_name;
				$this->storeLogoImagesTempServer($temp_file, $extern);
				$local_upload = true;
				if($local_upload)
					{
						$dir = '../design/templates/'.$this->getFormField('template').'/root/images/'.$this->getFormField('default_screen').'/';
						$uploadUrl = $dir.$image_name;

						if($this->CFG['admin']['site_logo']['name']=='SL')
							{
								copy($temp_file.'.'.$extern, $uploadUrl.'.'.$extern);
								unlink($temp_file.'.'.$extern);

							}
					}
			}

		/**
		 * EditPersonalProfileFormHandler::photoUpload()
		 *
		 * @return
		 **/
		public function faviconUpload()
			{
				$extern = strtolower(substr($_FILES['favicon']['name'], strrpos($_FILES['favicon']['name'], '.')+1));
				$dir = '../design/templates/'.$this->getFormField('template').'/';
				$image_name = 'favicon';
				$target_path = $dir.$image_name.'.'.$extern;
				move_uploaded_file($_FILES['favicon']['tmp_name'], $target_path);
			}

		public function populateTemplateCssDetails()
			{
				$dir_array = readDirectory($this->CFG['site']['project_path']. 'design/templates/', 'dir');
				$template_arr = array();
				foreach($dir_array as $template)
					{
						$css_array = readDirectory($this->CFG['site']['project_path']. 'design/templates/'.$template.'/members/css/', 'file');
						foreach($css_array as $css)
							{
								$template_arr[$template][] = substr($css, 0, strrpos($css, '.'));
							}
					}
				return $template_arr;
			}

		public function getTemplateName($css_file)
			{
				$dir_array = readDirectory($this->CFG['site']['project_path']. 'design/templates/', 'dir');
				$template_name = '';
				foreach($dir_array as $template)
					{
					       if(is_file($this->CFG['site']['project_path']. 'design/templates/'.$template.'/members/css/'.$css_file.'.css'))
     						{
                                        $template_name = $template;
                                        break;
                                   }
					}
				return $template_name;
			}
	}
//<<<<<---------------class ManageLogoHandler------///
//--------------------Code begins-------------->>>>>//
$logofrm = new ManageLogoHandler();
$logofrm->setPageBlockNames(array('form_add_logo'));
// Set the form fields
$logofrm->setFormField('logo', '');
$logofrm->setFormField('favicon', '');
$logofrm->setFormField('add_logo', '');
$logofrm->setFormField('msg', '');
$logofrm->setFormField('default_screen', $CFG['html']['stylesheet']['screen']['default']);
$logofrm->setFormField('template', $CFG['html']['template']['default']);
$logofrm->setFormField('success', 1);
// Default page block
$logofrm->setAllPageBlocksHide();
$logofrm->setPageBlockShow('form_add_logo');
$logofrm->sanitizeFormInputs($_REQUEST);
$logofrm->setFormField('template',$logofrm->getTemplateName($logofrm->getFormField('default_screen')));
if($logofrm->isFormGETed($_GET, 'msg'))
	{
		$logofrm->setAllPageBlocksHide();
		$logofrm->setPageBlockShow('form_add_logo');
		$logofrm->setPageBlockShow('block_msg_form_success');
		$logofrm->setCommonSuccessMsg($LANG['logo_success_message']);
	}
if (isDemoSite() AND $logofrm->isFormPOSTed($_POST, 'add_logo'))
	{
		$logofrm->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$logofrm->setPageBlockShow('block_msg_form_error');
		$logofrm->setPageBlockShow('form_add_logo');
	}
elseif ($logofrm->isFormPOSTed($_POST, 'add_logo'))
	{
		// Validations
		if ((isset($_FILES['logo']) and $_FILES['logo']['tmp_name'])  || (isset($_FILES['favicon']) and $_FILES['favicon']['tmp_name']))
			{
				if(isset($_FILES['logo']) and $_FILES['logo']['tmp_name'])
					{
						$logofrm->chkValidLogoFileType('logo',$LANG['err_tip_invalid_file_type']) and
							$logofrm->chkErrorInFile('logo',$LANG['err_tip_invalid_file']);
						if ($logofrm->isValidFormInputs())
							{
								$logofrm->logoUpload();
								$logofrm->setFormField('success', 1);
							}
						else
							{
								$logofrm->setFormField('success', 0);
								$logofrm->setAllPageBlocksHide();
								$logofrm->setPageBlockShow('form_add_logo');
								$logofrm->setPageBlockShow('block_msg_form_error');
								$logofrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
							}
					}

				if(isset($_FILES['favicon']) and $_FILES['favicon']['tmp_name'])
					{
						$logofrm->chkValidFaviconFileType('favicon',$LANG['err_tip_invalid_file_type']) and
							$logofrm->chkErrorInFile('favicon',$LANG['err_tip_invalid_file']);
							if($logofrm->getFormField('success')== '1')
							{
								if ($logofrm->isValidFormInputs())
									{
										$logofrm->faviconUpload();
										$logofrm->setFormField('success', 1);
									}
								else
									{
										$logofrm->setFormField('success', 0);
										$logofrm->setAllPageBlocksHide();
										$logofrm->setPageBlockShow('form_add_logo');
										$logofrm->setPageBlockShow('block_msg_form_error');
										$logofrm->setCommonErrorMsg($LANG['common_msg_error_sorry']);
									}
							}
					}

				if($logofrm->getFormField('success')== '1')
					{
						Redirect2URL($CFG['site']['url'].'admin/logoSettings.php?msg=uploaded&default_screen='.$logofrm->getFormField('default_screen'));
					}
			}

		 else
		 	{
				$logofrm->setPageBlockShow('block_msg_form_error');
				$logofrm->setCommonErrorMsg($LANG['logo_settings_error']);
			}
	}

if ($logofrm->isShowPageBlock('form_add_logo'))
	{
	    $template_arr = $logofrm->populateTemplateCssDetails();
		$smartyObj->assign('template_arr', $template_arr);
		$allowed_image_formats = implode(', ', $CFG['admin']['site_logo']['format_arr']);
		$allowed_image_formats = str_replace('image/', '', $allowed_image_formats);
  		$allowed_type = str_ireplace('VAR_FORMAT', $allowed_image_formats ,$LANG['attachment_format_tip']);

		$allowed_favicon_image_formats = implode(', ', $CFG['admin']['site_favicon']['format_arr']);
		$allowed_favicon_image_formats = str_replace('image/', '', $allowed_favicon_image_formats);
  		$allowed_favicon_type = str_ireplace('VAR_FORMAT', $allowed_favicon_image_formats ,$LANG['attachment_format_tip']);
		$smartyObj->assign('allowed_type', $allowed_type);
		$smartyObj->assign('allowed_fav_type', $allowed_favicon_type);
	}

//<<<<--------------------Code Ends----------------------//



//include the header file
$logofrm->includeHeader();
//include the content of the page
setTemplateFolder('admin/');
$smartyObj->display('logoSettings.tpl');
//includ the footer of the page
?>

<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$logofrm->includeFooter();
?>