<?php
/**
 * Edit Meta details
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Edit meta details###
 * @author 		selvaraj_47ag04
 * @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-22
 */
/**
 * Config file
 */
require_once('../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/admin/editMetaDetails.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 EditMetaHandler begins ------------------->>>>>//
/**
 * Edit language
 */
//<<<<<------------------ Class EditMetaHandler ends --------------------//
class EditMetaHandler extends FormHandler
	{
		public $file_path = '../languages/meta_details.php';

		/**
		 * Update file content
		 *
		 * Update edited file content
		 *
		 * @param		array $post_arr post array
		 * @return 		void
		 * @access		public
		 * @todo		Need to check and improve find and replacement. Because some time the
		 * 				language file has special characters too.
		 */
		public function updateFileContent($post_arr)
			{
				// set file name with path
				$file_path = $this->file_path;
				//read file content
				$content = file_get_contents($file_path);
				//include the file as
				require($file_path);
				// checks whether file edited
				$is_file_edited = false;
				foreach($post_arr as $varname => $value)
				  {
				       // checks variable value has been changed
				       if (isset($LANG[$varname]) and isset($post_arr[$varname]) and $LANG[$varname] != $post_arr[$varname])
						   {
								$is_file_edited = true;
								// replace value of file content
								// vaiable start pos
						        $variable_start = strpos($content, $varname , 0);
					         	// vaiable end pos
					        	$end_of_variable = strpos($content, '=', $variable_start ) + 1;
						       	// get content before value
					        	$content_before_value = trim(substr($content, 0, $end_of_variable));
						       	// vaiable line end pos
					            $variable_end_pos = strpos($content, ';', $end_of_variable);
					            // get content after value
					            $content_after_value = trim(substr($content, $variable_end_pos));
						       	// new content+
					            $content = $content_before_value. ' \''.addslashes($post_arr[$varname]) .'\''.$content_after_value;
						   }
				  }
				if ($is_file_edited )
					{
						$fw = fopen($file_path, 'w');
						fwrite($fw, $content);
						fclose($fw);
					}
			}

		/**
		 * EditMetaHandler::stripslashesNew()
		 *
		 * @param mixed $value
		 * @return
		 */
		public function stripslashesNew($value)
			{
				$value = str_replace('\\', '~*@', $value);
				$value = stripslashes($value);
				$value = str_replace('~*@', '\\', $value);
				$value = str_replace('\"', '"', $value);
				return $value;
			}
	}
//<<<<<<<---------------------------class EditMetaHandler--------------------//
//-------------------------------------Code begins-------------------------->>>>>//
$editMetaObj = new EditMetaHandler();
//set blocks
$editMetaObj->setPageBlockNames(array('msg_form_success', 'msg_form_error', 'form_edit_phrases', 'form_faq_list'));
//set Form Fields
$editMetaObj->setFormField('success_msg', '');
$editMetaObj->sanitizeFormInputs($_POST);
// Hide All Blocks and show default page
$editMetaObj->setAllPageBlocksHide();
$editMetaObj->setPageBlockShow('form_edit_phrases');
// submit file
if ($editMetaObj->isFormPOSTed($_POST, 'cancel_submit'))
	{
		Redirect2Url('index.php');
	}
if (isDemoSite() AND $editMetaObj->isFormPOSTed($_POST, 'submit_phrases'))
	{
		$editMetaObj->setAllPageBlocksHide();
		$editMetaObj->setPageBlockShow('msg_form_error');
		$editMetaObj->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$editMetaObj->setPageBlockShow('form_edit_phrases');
	}
elseif ($editMetaObj->isFormPOSTed($_POST, 'submit_phrases'))
	{
		$editMetaObj->updateFileContent($_POST);
		// Hide All Blocks and show default page
		$editMetaObj->setAllPageBlocksHide();
		$editMetaObj->setPageBlockShow('msg_form_success');
		$editMetaObj->setCommonSuccessMsg($LANG['langedit_msg_success']);
		$editMetaObj->setPageBlockShow('form_edit_phrases');
	}
else
	{
		$editMetaObj->setPageBlockShow('form_edit_phrases');
	}
//<<<<--------------------------------Code Ends-----------------------------//

//--------------------Page block templates begins-------------------->>>>>>>>>//
//include the header file
$editMetaObj->includeHeader();
?>
<div id="selHelpLangEdit">
  <div id="selManageLaguageFile">
    <h2><?php echo $LANG['langedit_lang_editing'];?></h2>
    <p class="clsSettingInfo"><?php echo $LANG['general_config_hint'];?></p>
    <?php
if ($editMetaObj->isShowPageBlock('msg_form_error'))
	{
?>
    <div id="selMsgError">
      <p><?php echo $LANG['common_msg_error_sorry'].' '. $editMetaObj->getCommonErrorMsg();?></p>
    </div>
    <?php
	}
if ($editMetaObj->isShowPageBlock('msg_form_success'))
	{
?>
    <div id="selMsgSuccess">
      <p><?php echo $editMetaObj->getCommonSuccessMsg();?></p>
    </div>
<?php
	}
if ($editMetaObj->isShowPageBlock('form_edit_phrases'))
	{
?>
    <div id="selEditForm">
      <form name="editFrm" id="editFrm" method="post" action="editMetaDetails.php">
        <table class="clsFormSection">
          <tr class="clsFormRow">
            <th><?php echo $LANG['langedit_variable_name'];?></th>
            <th><?php echo $LANG['langedit_new_value'];?></th>
          </tr>
<?php
		$lang_update = $LANG['common_update'];
		$lang_cancel = $LANG['common_reset'];
		unset($LANG);
		// include the file
		require($editMetaObj->file_path);
		// populate phrases
		foreach($LANG as $key => $value)
			{
?>
          <tr class="clsFormRow">
            <td class="<?php echo $editMetaObj->getCSSFormLabelCellClass('varable');?>">
              <label for="<?php echo $key;?>"><?php echo $key; ?></label>
            </td>
            <td class="<?php echo $editMetaObj->getCSSFormFieldCellClass('phrase');?>">
              <textarea name="<?php echo $key; ?>" id="<?php echo $key;?>" tabindex="1000" rows="3" cols="50"><?php echo $editMetaObj->stripslashesNew($value); ?></textarea>
            </td>
          </tr>
          <?php		}
?>
          <tr class="clsFormRow">
            <td></td>
			<td class="<?php echo $editMetaObj->getCSSFormFieldCellClass('submit');?>">
              <input type="submit" class="clsSubmitButton" name="submit_phrases" id="selSubmitPhrases" value="<?php echo $lang_update;?>" tabindex="1005" />
			  <input type="reset" class="clsCancelButton" name="cancel_submit" id="cancel_submit" value="<?php echo $lang_cancel;?>" tabindex="1010" />
            </td>
          </tr>
        </table>
      </form>
    </div>
<?php
	}
?>
  </div>
</div>
<?php
//<<<<<<--------------------Page block templates Ends--------------------//
$editMetaObj->includeFooter();
?>
