<?php
/**
 * To edit the config_profile file
 *
 * TO show the config_profile variables from the file and edit
 *
 * PHP version 5.0
 *
 * @category	###Anove###
 * @package		###Edit Config Index###
 * @author 		senthil_52ag05
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2007-12-22
 */
/**
 * Including the config file
 */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/admin/editTemplateSettings.php';
$CFG['mods']['include_files'][] = 'common/classes/class_Parser.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 ConfigProfileEditFormHandler begins -------------------->>>>>//
class ConfigProfileEditFormHandler extends FormHandler
	{


		/**
		 * render a check box
		 *
		 * @param		string $name name used for id also
		 * @param		string $tab_index
		 * @param		string $highlight item to be checked when loading
		 * @return 		void
		 * @access		public
		*/
		public function isCheckBoxChecked($current_value, $field_array='')
			{


                    if(in_array($current_value,$field_array))
     				return 'checked';
     			else
          			return '';
			}

		/**
		 * ConfigProfileEditFormHandler::isCheckedRadio()
		 *
		 * @param $value
		 * @return
		 **/
		public function isCheckedRadio($current_field_value, $current_value)
			{
				//$current_value = ($current_value=='true')?1:($current_value=='false')?0:$current_value;
				$checked = ($current_value == $this->fields_arr[$current_field_value])? ' checked':'';
				return $checked;
			}


		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 populateTemplates()
			{
				$dir_array = readDirectory($this->CFG['site']['project_path']. 'design/templates/', 'dir');
				$template_arr = array();
				foreach($dir_array as $template)
					{
						$template_arr[] = $template;
					}
				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;
			}


          public function populateCssDetails($template)
			{
				$css_array = readDirectory($this->CFG['site']['project_path']. 'design/templates/'.$template.'/members/css/', 'file');
                    return $css_array;
			}

		public function chkIsValidTemplate()
     		{

                    $temp_ok = false;
                    if(!isset($this->fields_arr['temp_arr']))
                         return false;

                    $template_array = array();
                    $template_array = $this->fields_arr['temp_arr'];
                    $template_count = count($template_array);
                    $css_count = 0;
                    foreach($template_array as $templ)
                         {

                              if(!isset($this->fields_arr['css_arr']))
                                  return false;

                              $temp_ok = false;
                              $css_array = $this->populateCssDetails($templ);
                              foreach($this->fields_arr['css_arr'] as $cssfile)
                                   {
                                       if($temp_ok)
                                            break;
                                       if(in_array($cssfile,$css_array))
                                             {
                                                  $temp_ok = true;
                                                   $css_count++;

                                             }



                                   }
                         }
                    if($css_count != $template_count)
                              {
                              $this->fields_err_tip_arr['name'] = $this->LANG['err_tip_compulsory'];
                              return false;
                              }
                    return $temp_ok;
               }
          public function isValidCss()
               {
                    if(!isset($this->fields_arr['css_arr']))
                              return false;

                    if(!isset($this->fields_arr['temp_arr']))
                              return false;

                    $temp_array = $this->fields_arr['temp_arr'];
                    $css_array = $this->fields_arr['css_arr'];

                    foreach($css_array as $cssfile)
                         {
                              $cssfile = substr($cssfile, 0, -4);
                              $template_name = $this->getTemplateName($cssfile);
                               if(!in_array($template_name,$this->fields_arr['temp_arr']))
                                        {
                                             $this->fields_err_tip_arr['name'] = $this->LANG['err_tip_compulsory'];
                                             return false;
                                        }
                         }
                    return true;

               }

          public function resetFieldValues()
               {
                    $this->setFormField('is_template_change', $this->CFG['html']['template']['is_template_support']);
                    $this->setFormField('default_screen', $this->CFG['html']['stylesheet']['screen']['default']);
                    $this->setFormField('temp_arr', $this->CFG['html']['template']['allowed']);
                    $this->setFormField('css_arr', $this->CFG['html']['stylesheet']['allowed']);
               }

          public function writeTemplateConfig($is_support, $screen, $templ_arr, $style_arr)
               {

                 	$style_arr = implode("','",$style_arr);
                 	$templ_arr = implode("','",$templ_arr);

                 	$style_arr_form = 'array(\''.$style_arr.'\')';
                 	$templ_arr_form = 'array(\''.$templ_arr.'\')';

                 	$default_templ = $this->getTemplateName($screen);


     if (is_writable('../common/configs/config_templates.inc.php'))
  	{
  		if ($handle = fopen('../common/configs/config_templates.inc.php', 'w'))
  			{

$str = <<<CONT
<?php
\$CFG['html']['template']['is_template_support'] = {$is_support};
\$CFG['html']['template']['default'] = '{$default_templ}';
\$CFG['html']['stylesheet']['screen']['default'] = '{$screen}';
\$CFG['html']['template']['allowed']= {$templ_arr_form};
\$CFG['html']['stylesheet']['allowed'] = {$style_arr_form};
\$CFG['html']['stylesheet']['screen']['admindefault'] = '{$screen}';
?>
CONT;
  					fwrite($handle, $str);
  					fclose($handle);
  			}
  	}



               }

	}
//<<<<<--------------------- Class ConfigProfileEditFormHandler ends ------------------------//
//------------------ Code begins------------------>>>//
$editfilefrm = new ConfigProfileEditFormHandler();
$editfilefrm->setPageBlockNames(array('show_config_variable'));
$editfilefrm->setFormField('is_template_change', '');
$editfilefrm->setFormField('default_screen', '');
$editfilefrm->setFormField('temp_arr', array());
$editfilefrm->setFormField('css_arr', array());
$editfilefrm->setAllPageBlocksHide();
if (isDemoSite() AND $editfilefrm->isFormPOSTed($_POST, 'edit_submit'))
	{
		$editfilefrm->setAllPageBlocksHide();
		$editfilefrm->setPageBlockShow('block_msg_form_error');
		$editfilefrm->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
	}
elseif ($editfilefrm->isFormPOSTed($_POST, 'edit_submit'))
	{
          $editfilefrm->sanitizeFormInputs($_POST);
          $editfilefrm->chkIsNotEmpty('temp_arr', $LANG['err_tip_compulsory']) and
               $editfilefrm->chkIsNotEmpty('css_arr', $LANG['err_tip_compulsory']) and
                    $editfilefrm->chkIsValidTemplate() and
                         $editfilefrm->isValidCss();
          		if ($editfilefrm->isValidFormInputs())
          			{
                      	$val1 = $editfilefrm->getFormField('is_template_change');
                      	$val2 = $editfilefrm->getFormField('default_screen');
                      	$val3 = $editfilefrm->getFormField('temp_arr');
                      	$val4 = $editfilefrm->getFormField('css_arr');
                      	$editfilefrm->writeTemplateConfig($val1,$val2,$val3,$val4);
                        Redirect2URL($CFG['site']['relative_url'].'editTemplateSettings.php?succ=1');
          			}
               	else
                	{
                        $editfilefrm->setPageBlockShow('block_msg_form_success');
                		$editfilefrm->setCommonSuccessMsg($LANG['general_config_success_error']);
                    }
	}
else
     {
          $editfilefrm->resetFieldValues();
     }

if ($editfilefrm->isFormGETed($_GET, 'succ'))
	{
          $editfilefrm->setPageBlockShow('block_msg_form_success');
		$editfilefrm->setCommonSuccessMsg($LANG['general_config_success_msg']);
	}


$editfilefrm->setPageBlockShow('show_config_variable');
//<<<<<---------------------Code ends------------------//
//-----------------Page block template begins----------->>>>>//
if ($editfilefrm->isShowPageBlock('show_config_variable'))
	{
            $css_arr = '';
			$total_details = $editfilefrm->populateTemplates();
            $template_arr = $editfilefrm->populateTemplateCssDetails();
			$smartyObj->assign('template_arr', $template_arr);
			$smartyObj->assign('total_details', $total_details);
			$smartyObj->assign('css_arr', $css_arr);
     }

//<<<<<<--------------------Page block templates Ends--------------------//
$editfilefrm->includeHeader();
setTemplateFolder('admin/');
$smartyObj->display('editTemplateSettings.tpl');
$editfilefrm->includeFooter();
?>