<?php

/******************************************************************************
 *  
 *  PROJECT: Flynax Classifieds Software
 *  VERSION: 4.5.2
 *  LICENSE: FL4HLD2A8696 - http://www.flynax.com/license-agreement.html
 *  PRODUCT: Real Estate Classifieds
 *  DOMAIN: savingrack.com
 *  FILE: RLCOMMON.CLASS.PHP
 *  
 *  The software is a commercial product delivered under single, non-exclusive,
 *  non-transferable license for one domain or IP address. Therefore distribution,
 *  sale or transfer of the file in whole or in part without permission of Flynax
 *  respective owners is considered to be illegal and breach of Flynax License End
 *  User Agreement.
 *  
 *  You are not allowed to remove this information from the file without permission
 *  of Flynax respective owners.
 *  
 *  Flynax Classifieds Software 2017 | All copyrights reserved.
 *  
 *  http://www.flynax.com/
 ******************************************************************************/

class rlCommon extends reefless 
{
	/**
	* @var block keys
	**/
	var $block_keys;
	
	/**
	* @var error fields string
	**/
	var $error_fields;

	var $listings = array();

	var $tmp = array();

	/**
	* define blocks existing for template sides
	*
	* @param array $blocks - blocks array
	*
	* @return mixed array
	**/
	function defineBlocksExist( &$blocks )
	{
		global $l_block_sides, $rlSmarty;
			
		/* unset all sides */
		foreach ($l_block_sides as $key => $value)
		{
			unset($blocks[$key]);
		}
		
		/* set available blocks sides */
		foreach ($blocks as $key => $value)
		{
			if ( array_key_exists($value['Side'], $l_block_sides) )
			{
				$blocks[$value['Side']] = true;
			}
		}

		/* detect wide mode */
		$wide_mode = true;
		if ( $blocks['right'] && $blocks['left'] )
		{
			$wide_mode = false;
		}
		$rlSmarty -> assign('wide_mode', $wide_mode);
	}
	
	/**
	* Get bread crumbs details
	*
    * Prepares the breat crumbs data depending of the current page settings,
    * in case of the page has parent page the method will be called requrcive
    *
	* @param  array $cPage - current page information
	* @return array        - page bread crumbs data
	*/
	public static function getBreadCrumbs($cPage)
	{
		global $lang;

		$bread_crumbs[] = array( 
			'name' => $lang['pages+name+home'],
			'title' => $lang['pages+title+home']
		);
		
		if ($cPage['Parent_ID']) {
			$add_bread_crumb = $this->fetch(
                array('Parent_ID', 'Path', 'Key'),
                array(
                    'ID' => $cPage['Parent_ID'],
                    'Status' => 'active'
                ),
                null, 1, 'pages', 'row'
            );

			$add_bread_crumb = $GLOBALS['rlLang']->replaceLangKeys($add_bread_crumb, 'pages', array('name', 'title'));

			if ($add_bread_crumb['Parent_ID']) {
				return $this -> getBreadCrumbs($bread_crumbs);
			}
			
			$bread_crumbs[] = array(
				'name' => $add_bread_crumb['name'],
				'title' => $add_bread_crumb['title'],
				'path' => $add_bread_crumb['Path']
			);
		}

        // set proper name for view details page
        if ($cPage['Key'] == 'view_details' && $cPage['Controller'] == 'listing_type') {
            $bc_listing_type = array_search($cPage['Path'], $GLOBALS['pages']);
            $cPage['name'] = $GLOBALS['lang']['pages+name+'.$bc_listing_type];
            $cPage['title'] = $GLOBALS['lang']['pages+title+'.$bc_listing_type];
        }
		
		$bread_crumbs[] = array(
			'name' => $cPage['name'],
			'title' => $cPage['title'],
			'path' => $cPage['Path']
		);

		return $bread_crumbs;
	}
	
	/**
	* Generates page title data
    *
    * @todo Generates the title data and assigns it to the SMARTY as array
	*
	* @param array $bread_crumbs - page bread crumbs
	*/
	public static function pageTitle($bread_crumbs)
	{
		// single title controllers
		$single_title_controllers = array('listing_details', 'listing_type', 'news', 'recently_added');

        // define title variable
        $title = array();
		
        if (count($bread_crumbs) > 1) {
            if (in_array($GLOBALS['page_info']['Controller'], $single_title_controllers)) {
                $bread_crumbs = array_reverse($bread_crumbs);
                $title = array();
                $title[] = $bread_crumbs[0]['title'] ?: $bread_crumbs[0]['name'];
            } else {
                foreach ($bread_crumbs as $index => &$item) {
                    if ($index > 0) {
                        $title[] = $item['title'] ?: $item['name'];
                    }
                }
            }
        }
		
		$GLOBALS['rlHook']->load('pageTitle', $title, $bread_crumbs, $single_title_controllers); // params added from >  4.1.0

		$title = array_reverse($title);
		$GLOBALS['rlSmarty']->assign_by_ref('title', $title);
	}
	
	/**
	* build menus
	*
	* @todo generates and assigns menus to SMARTY
	**/
	function buildMenus()
	{
		global $rlSmarty, $fields, $main_menu, $tpl_settings, $account_info, $deny_pages, $config, $account_menu;
		
		$fields = array( 'ID', 'Page_type', 'Key', 'Path', 'Get_vars', 'Controller', 'No_follow', 'Menus', 'Deny', 'Login' );

		$this -> setTable('pages');
		$menus = $this -> fetch( $fields, array('Status' => 'active'), "ORDER BY `Position`" );
		$menus = $GLOBALS['rlLang'] -> replaceLangKeys( $menus, 'pages', array( 'name', 'title' ) );
		
		foreach ( $menus as $key => $value )
		{			
			/* generate main menu */
			if ( in_array(1, explode(',', $value['Menus'])) )
			{
				$main_menu[] = $value;
			}
			
			/* generate footer menu */
			if ( in_array(3, explode(',', $value['Menus'])) )
			{
				$footer_menu[] = $value;
			}
			
			/* generate account menu */
			if ( in_array(2, explode(',', $value['Menus'])) && (!in_array($account_info['Type_ID'], explode(',', $value['Deny'])) || !$account_info['Type_ID']) && (!in_array($value['Key'], $deny_pages) || !$deny_pages) )
			{
				if ($value['Key'] == 'my_packages' && $config['membership_module'] && !$config['allow_listing_plans']) {
					continue;
				}
				$account_menu[] = $value;
			}
			
			/* generate inventory menu */
			if ( $tpl_settings['inventory_menu'] === true )
			{
				if ( in_array(4, explode(',', $value['Menus'])) )
				{
					$inventory_menu[] = $value;
				}
			}			
		}

		$rlSmarty -> assign_by_ref('main_menu', $main_menu);
		$rlSmarty -> assign_by_ref('footer_menu', $footer_menu);
		$rlSmarty -> assign_by_ref('account_menu', $account_menu);
		$rlSmarty -> assign_by_ref('user_menu', $user_menu);
		
		if ( $tpl_settings['inventory_menu'] === true )
		{
			$rlSmarty -> assign_by_ref('inventory_menu', $inventory_menu);
		}
	}
	
	/**
	* check dynamic form
	*
	* @param array $data - the form data (field => value)
	* @param array $fields - fields
	* @param string $prefix - field name prefix
	* @param bool $admin - admin mode
	*
	* @return array $errors - errors
	**/
	function checkDynamicForm( $data = false, $fields = false, $prefix = 'f', $admin = false )
	{
		global $error_fields, $lang, $languages, $l_deny_files_regexp;
		
		$errors = false;
		
		$flStrlenFunc = 'strlen';
		
		if ( function_exists('mb_strlen') && function_exists('mb_internal_encoding') )
		{
			mb_internal_encoding('UTF-8');
			$flStrlenFunc = 'mb_strlen';
		}
		
		if ( !$data || !$fields )
			return false;

		foreach ( $fields as $fIndex => $fRow )
		{
			$sFields[$fIndex] = $fields[$fIndex]['Key'];
		}

		$step = 0;
		
		foreach ($data as $f2 => $v2)
		{
			$poss = array_search( $f2, $sFields );

			if ( false !== $poss )
			{	
				switch ($fields[$poss]['Type']){
				
					case 'text':
						if ( $fields[$poss]['Required'] )
						{
							if ( $fields[$poss]['Multilingual'] )
							{
								$ml_empty = 0;
								foreach ($data[$f2] as $ml_key => $ml_val)
								{
									$ml_val = trim( $ml_val );
									if ( empty($ml_val) )
									{
										$ml_empty++;
										if ( $admin )
										{
											$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][{$ml_key}]";
										}
										else
										{
											$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][{$ml_key}],";
										}
									}
								}
								
								if ( count($data[$f2]) == $ml_empty )
								{
									$errors[$step] = str_replace('{field}', $fields[$poss]['name'], $lang['required_multilingual_error']);
								}
							}
							else
							{
								$data[$f2] = trim($data[$f2]);
								$data[$f2] = $fields[$poss]['Condition'] == 'isUrl' && !(bool)preg_match('/https?\:\/\//', $data[$f2]) && !empty($data[$f2]) ? 'http://'. $data[$f2] : $data[$f2];
								
								if (empty($data[$f2])) {
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
								} elseif ($fields[$poss]['Condition']) {
									if (!$GLOBALS['rlValid'] -> {$fields[$poss]['Condition']}($data[$f2])) {
										$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_incorrect']);
									}
								}
							}
						}
						else 
						{
							$data[$f2] = trim($data[$f2]);
							$data[$f2] = $fields[$poss]['Condition'] == 'isUrl' && !(bool)preg_match('/https?\:\/\//', $data[$f2]) && !empty($data[$f2]) ? 'http://'. $data[$f2] : $data[$f2];

                            if ($fields[$poss]['Condition'] == 'isUrl' && (bool)preg_match('#^https?\://$#', $data[$f2])) {
                                //allow http or https as empty value for isUrl condition, it will be truncated after
                            } elseif ($fields[$poss]['Condition'] && !empty($data[$f2])) {
								if (!$GLOBALS['rlValid'] -> {$fields[$poss]['Condition']}($data[$f2])) {
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_incorrect']);
								}
							}
						}
						break;
					
					case 'textarea':
						$limit = (int)$fields[$poss]['Values'];
						
						if ( $fields[$poss]['Multilingual'] )
						{
							$ml_empty = 0;
							foreach ($data[$f2] as $ml_key => $ml_val)
							{
								$ml_val = trim( $ml_val );
								
								/* check for empty value */
								if ( empty($ml_val) && $fields[$poss]['Required'] )
								{
									$ml_empty++;
									if ( $admin )
									{
										$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][{$ml_key}]";
									}
									else
									{
										$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][{$ml_key}],";
									}
								}
								
								/* check for exceeded limit */
								if ( $limit && $flStrlenFunc(strip_tags($ml_val)) > $limit )
								{
									if ( $admin )
									{
										$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][{$ml_key}]";
									}
									else
									{
										$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][{$ml_key}],";
									}
									
									$errors[$step] = str_replace(array('{field}', '{limit}'), array($fields[$poss]['name'] .' ('. $languages[$ml_key]['name'] .')', $limit), $lang['error_textarea_limit_exceeded']);
								}
							}
							
							if ( count($data[$f2]) == $ml_empty && $fields[$poss]['Required'] )
							{
								$errors[$step] = str_replace('{field}', $fields[$poss]['name'], $lang['required_multilingual_error']);
							}
						}
						else
						{
							$data[$f2] = trim( $data[$f2] );
							
							/* check for empty value */
							if ( empty($data[$f2]) && $fields[$poss]['Required'] )
							{
								if ( $admin )
								{
									$error_fields[] = $prefix. "[{$fields[$poss]['Key']}]";
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}],";
								}
								$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
							}
							
							/* check for exceeded limit */
							if ( $limit && $flStrlenFunc(strip_tags($data[$f2])) > $limit )
							{
								if ( $admin )
								{
									$error_fields[] = $prefix. "[{$fields[$poss]['Key']}]";
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}],";
								}
								
								$errors[$step] = str_replace(array('{field}', '{limit}'), array($fields[$poss]['name'], $limit), $lang['error_textarea_limit_exceeded']);
							}
						}
						break;
					
					case 'number':
						$data[$f2] = trim($data[$f2]);
						
						if ( $fields[$poss]['Required'] || !empty($data[$f2]))
						{
							if ( $fields[$poss]['Values'] && $data[$f2] )
							{
								if ( strlen($data[$f2]) > $fields[$poss]['Values'] )
								{
									$errors[$step] = str_replace( array('{field}', '{max}'), array('<span class="field_error">"'.$fields[$poss]['name'].'"</span>', '<span class="field_error">"'.$fields[$poss]['Values'].'"</span>'), $GLOBALS['lang']['notice_number_incorrect']);
								}
							}
							else 
							{
								if ( empty($data[$f2]) )
								{
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
								}
							}
						}
						break;
						
					case 'phone':
						if ( $fields[$poss]['Required'] && ((empty($data[$f2]['code']) && $fields[$poss]['Opt1']) || empty($data[$f2]['area']) || empty($data[$f2]['number'])) )
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_phone_field_error']);
							
							if ( empty($data[$f2]['code']) && $fields[$poss]['Opt1'] )
							{
								if ( $admin )
								{
									$error_fields[$step] = $prefix. "[{$fields[$poss]['Key']}][code]";
									$step++;
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][code],";
								}
							}
							if ( empty($data[$f2]['area']) )
							{
								if ( $admin )
								{
									$error_fields[$step] = $prefix. "[{$fields[$poss]['Key']}][area]";
									$step++;
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][area],";
								}
							}
							if ( empty($data[$f2]['number']) )
							{
								if ( $admin )
								{
									$error_fields[$step] = $prefix. "[{$fields[$poss]['Key']}][number]";
									$step++;
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][number],";
								}
							}
						}
						elseif ( !$fields[$poss]['Required'] && (((!empty($data[$f2]['area']) && !$fields[$poss]['Condition']) || !empty($data[$f2]['number']) || (!empty($data[$f2]['code']) && $fields[$poss]['Opt1'])/* || (!empty($data[$f2]['ext']) && $fields[$poss]['Opt2'])*/) && (empty($data[$f2]['area']) || empty($data[$f2]['number']) || (empty($data[$f2]['code']) && $fields[$poss]['Opt1']))) )
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_phone_field_error']);
						}
						break;
					
					case 'date':
						if ( $fields[$poss]['Default'] == 'single' )
						{
							if ( $fields[$poss]['Required'] && empty($data[$f2]) )
							{
								$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
							}
							elseif ( !empty($data[$f2]) )
							{
								if( !(bool)preg_match('/^[0-9]{4}\-[0-1][0-9]\-[0-3][0-9]$/', $data[$f2]) )
								{
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_incorrect']);
								}
							}
						}
						elseif ( $fields[$poss]['Default'] == 'multi' )
						{
							if ( $fields[$poss]['Required'] && (empty($data[$f2]['from']) || empty($data[$f2]['to'])) )
							{
								$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
								if ( $admin )
								{
									$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][from]";
									$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][to]";
								}
								else
								{
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][from],";
									$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][to],";
								}
							}
							elseif ( !empty($data[$f2]['from']) || !empty($data[$f2]['to']) )
							{
								if( !(bool)preg_match('/^[0-9]{4}\-[0-1][0-9]\-[0-3][0-9]$/', $data[$f2]['from']) && !empty($data[$f2]['from']) )
								{
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'" ('.$GLOBALS['lang']['from'].')</span>', $GLOBALS['lang']['notice_field_incorrect']);
									if ( $admin )
									{
										$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][from]";
									}
									else
									{
										$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][from],";
									}
								}
								if( !(bool)preg_match('/^[0-9]{4}\-[0-1][0-9]\-[0-3][0-9]$/', $data[$f2]['to']) && !empty($data[$f2]['to']) )
								{
									$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'" ('.$GLOBALS['lang']['to'].')</span>', $GLOBALS['lang']['notice_field_incorrect']);
									if ( $admin )
									{
										$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][to]";
									}
									else
									{
										$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][to],";
									}
								}
							}
						}
						
                        break;
					
					case 'mixed':
					case 'price':
					case 'unit':
						$data[$f2]['value'] = trim( $data[$f2]['value'] );
						if ($fields[$poss]['Required'] && empty($data[$f2]['value']))
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_field_empty']);
							if ( $admin )
							{
								$error_fields[] = $prefix. "[{$fields[$poss]['Key']}][value]";
							}
							else
							{
								$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}][value],";
							}
						}
						break;
						
					case 'select':
						$data[$f2] = trim( $data[$f2] );
						if ( $fields[$poss]['Required'] && empty($data[$f2]) )
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_select_empty']);
						}
						break;
					
					case 'checkbox':
						unset($data[$f2][0]);
					case 'radio':
	
						if ($fields[$poss]['Required'] && empty($data[$f2]))
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_checkbox_empty']);
						}
						break;
						
					case 'accept':
						if ($fields[$poss]['Required'] && $data[$f2] != 'yes')
						{
							$errors[$step] = str_replace( '{field}', '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', $GLOBALS['lang']['notice_no_agree']);
							if ( $admin )
							{
								$error_fields[] = "{$fields[$poss]['Key']}";
							}
							else
							{
								$this -> error_fields .= "{$fields[$poss]['Key']},";
							}
						}
						break;
					
					case 'image':
						if ( ($fields[$poss]['Required'] && empty($_FILES[$f2]['name']) && empty($data[$f2])) || (!$fields[$poss]['Required'] && !empty($_FILES[$f2]['name'])) )
						{
							if ( empty($_FILES[$f2]['name']) )
							{
								$errors[$step] = str_replace( array( '{field}' ), array( '<span class="field_error">"'.$fields[$poss]['name'].'"</span>' ), $GLOBALS['lang']['notice_field_empty']);
							}
							else 
							{
								$file_ext = explode( '.', $_FILES[$f2]['name'] );
								$file_ext = array_reverse( $file_ext );
								$file_ext = $file_ext[0];
			
								if ( !$GLOBALS['rlValid'] -> isImage($file_ext) || preg_match($l_deny_files_regexp, $_FILES[$f2]['name']) )
								{
									$errors[$step] = str_replace( array( '{field}', '{ext}' ), array( '<span class="field_error">"'.$fields[$poss]['name'].'"</span>', '<span class="field_error">"'.$file_ext.'"</span>' ), $GLOBALS['lang']['notice_bad_file_ext']);
								}
							}
						}
						break;
					
					case 'file':
						if (!empty($_FILES[$f2]['name']) || ( $fields[$poss]['Required'] && empty($data[$f2]))) {
							if (empty($_FILES[$f2]['name'])) {
								$errors[$step] = str_replace(
                                    array('{field}' ), 
                                    array('<span class="field_error">"' . $fields[$poss]['name'] . '"</span>' ), 
                                    $GLOBALS['lang']['notice_field_empty']
                                );
							} else {
								$file_ext = explode( '.', $_FILES[$f2]['name'] );
								$file_ext = array_reverse( $file_ext );
								$file_ext = $file_ext[0];

								if (!$GLOBALS['rlValid']->isFile($fields[$poss]['Default'], $file_ext) 
                                    || preg_match($l_deny_files_regexp, $_FILES[$f2]['name'])
                                ) {
                                    $errors[$step] = str_replace(
                                        array('{field}', '{ext}'), 
                                        array(
                                            '<span class="field_error">"' . $fields[$poss]['name'] . '"</span>', 
                                            '<span class="field_error">"' . $file_ext . '"</span>'
                                        ), 
                                        $GLOBALS['lang']['notice_bad_file_ext']
                                    );

                                    // remove tmp file
                                    unset($_FILES[$f2]['name']);
								}
							}
						}
						break;
				}
			

				if ( $errors[$step] )
				{
					$step++;

					if ( !in_array($fields[$poss]['Type'], array('phone')) )
					{
						if ( $admin )
						{
							$error_fields[] = $prefix. "[{$fields[$poss]['Key']}]";
						}
						else
						{
							$this -> error_fields .= $prefix. "[{$fields[$poss]['Key']}],";
						}
					}
				}
				else
				{
					unset($errors[$step]);
				}
			}
		}

		return $errors;
	}

	/**
	* adapt value
	*
	* @param array $field - field info
	* @param mixed $value - original value
	* @param string $type - field type
	* @param int $id - item id
	* @param bool $tags - use html tags
	* @param bool $strip_tags - strip tags for html fileds
	* @param bool $edit_mode - edit mode
	*
	* @return mixed - category fields list
	**/
	function adaptValue( &$field, &$value, $type = 'listing', $id = false, $tags = true, $strip_tags = false, $edit_mode = false, $custom_lang = false, $account_id = 0, $form = 'short_form' )
	{
		global $lang, $config, $account_info, $rlMembershipPlan;
		
		$out = false;

        if ($config['membership_module'] 
            && $type == 'listing' 
            && $account_info['ID'] != $account_id 
            && !defined('REALM')
        ) {
            $this->loadClass('MembershipPlan');

            if (false !== $out = $rlMembershipPlan->fakeListingValue($field, $value, $form)) {
                return $out;
            }
        }

		$preferred_lang = $custom_lang ?: RL_LANG_CODE;

		if (empty($value) && $field['Type'] != 'bool') {
			return false;
		}

		switch ($field['Type']){
			case 'price':
				$price = null;
				$price = explode( '|', $value );
				$currency = $price[1] ? $lang['data_formats+name+'.$price[1]] : '';
				
				if ( $config['system_currency_position'] == 'before' )
				{
					$out = $currency .' '. $GLOBALS['rlValid'] -> str2money($price[0]);
				}
				else
				{
					$out = $GLOBALS['rlValid'] -> str2money($price[0]) .' '. $currency;
				}
				break;
			
			case 'mixed':
                if ($field['Key'] == 'escort_rates') {
                    continue;
                }

                $df = null;
                $df = explode('|', $value);

                $thousands_sep = $field['Opt2'];
                $value = $field['Opt1'] ? number_format($df[0], null, null, $thousands_sep) : $df[0];

                if (!empty($field['Condition'])) {
                	$out = $value . ' ' . $lang['data_formats+name+' . $df[1]];
                } else {
                	$out = $value . ' ' . $lang[$type . '_fields+name+' . $df[1]];
                }
                break;

			case 'date':
				if ( $field['Default'] == 'single' )
				{
                    if (!strtotime($value)) {
                        return false;
                    }

					list($d_year, $d_month, $d_day) = explode('-', $value);
					$d_timestamp = mktime(0, 0, 0, $d_month, $d_day, $d_year);
					$out = date(str_replace(array('%', 'b'), array('', 'M'), RL_DATE_FORMAT), $d_timestamp);
				}
				elseif ( $field['Default'] == 'multi' )
				{
					list($d_year, $d_month, $d_day) = explode('-', $value);
					$d_timestamp = mktime(0, 0, 0, $d_month, $d_day, $d_year);
					$d_date = date(str_replace(array('%', 'b'), array('', 'M'), RL_DATE_FORMAT), $d_timestamp);

					$out = $tags ? '<span class="grey_small">'. $GLOBALS['lang']['from'] .'</span> '. $d_date .' ' : $d_date;

					$id = (int)$id;
					if ( $id )
					{
						$multi_field = $this -> getOne($field['Key'].'_multi', "`ID` = '{$id}'", $type == 'listing' ? 'listings' : 'accounts');
						list($t_year, $t_month, $t_day) = explode('-', $multi_field);
						$t_timestamp = mktime(0, 0, 0, $t_month, $t_day, $t_year);

						$d_date_to = date(str_replace(array('%', 'b'), array('', 'M'), RL_DATE_FORMAT), $t_timestamp);
						$out .= $tags ? '<span class="grey_small">'. $GLOBALS['lang']['to'] .'</span> '. $d_date_to : ' - '.$d_date_to;
					}
				}
				break;
			
			case 'text':
				if (in_array($field['Condition'], array('isUrl', 'isDomain'))) {
					$value = !(bool)preg_match('/https?\:\/\//', $value) ? 'http://'.$value : $value;
					$out = $value;
				} else {
					if ($field['Multilingual'] || (bool) preg_match('/\{\|[\w]{2}\|\}/', $value)) {
						$out = $this -> parseMultilingual($value, $preferred_lang);
					} else {
						$out = $value;
					}
				}
				
				if ($strip_tags) {
					$out = strip_tags($out);
				}
				break;
			
			case 'textarea':
				if ($field['Multilingual'] || (bool) preg_match('/\{\|[\w]{2}\|\}/', $value)) {
					$out = $this->parseMultilingual($value, $preferred_lang);
				} else {
					$out = $value;
				}
				
				if ($field['Condition'] == 'html' && $strip_tags) {
					$out = strip_tags($out);
				}

                if ($field['Condition'] != 'html') {
                    $out = nl2br($out);
                }
				break;
				
			case 'phone':
				$out = $this -> parsePhone($value, $edit_mode ? false : $field);
				break;
				
			case 'number':			
				if ($field['Opt1']) {
					$thousands_sep = $field['Opt2'] ?: ',';

                    if (is_numeric(strpos($value, '.'))) {
                        $decimals = explode('.', $value);
                        $decimals = (int) strlen($decimals[1]);

                        $out = number_format($value, $decimals, '.', $thousands_sep);
                    } else {
                        $out = number_format($value, null, null, $thousands_sep);
                    }
				} else {
					$out = $value;
				}
				break;
			
			case 'bool':
				if ( (bool)$value )
				{
					$out = $GLOBALS['lang']['yes'];
				}
				else
				{
					$out = $GLOBALS['lang']['no'];
				}
				break;
			
			case 'select':
				if ( !empty($field['Condition']) )
				{
					if ( $field['Condition'] != 'years' )
					{
						$out = $GLOBALS['lang']['data_formats+name+'.$value];
					}
					else
					{
						$out = $value;
					}
				}
				else
				{
					if( $field['Key'] == 'Category_ID' )
					{
						$out = '';

						if ($GLOBALS['listing_data']['Parent_keys']) {
							$parents = explode(",", $GLOBALS['listing_data']['Parent_keys']);
							$last = $lang['categories+name+'.$GLOBALS['listing_data']['Cat_key']];
						} elseif ($this->listings[$id] && $this->listings[$id]['name']) {
							$parent_keys = $this->listings[$id]['Parent_keys'];
							$last =	$this->listings[$id]['name'];
						} elseif ($id) {
							$sql ="SELECT `Parent_keys`, `Key` FROM `".RL_DBPREFIX."categories` AS `T1` ";
							$sql .="JOIN `".RL_DBPREFIX."listings` AS `T2` ON `T2`.`Category_ID` = `T1`.`ID` ";
							$sql .="WHERE `T2`.`ID` = " . $id;
							$cat_info = $this->getRow($sql);

							$parent_keys = $this->listings[$id]['Parent_keys'];

							$last = $lang['categories+name+'.$cat_info['Key']];
						}

						if (trim($parent_keys)) {
							foreach(explode(",",$parent_keys) as $k => $parent) {
								$out .= $lang['categories+name+'.$parent].", ";
							}
                        }
						$out .= $last;
					}
					else
					{
						$out = $lang[$type . '_fields+name+'.$field['Key'].'_'.$value];
					}
				}
				break;
			
			case 'radio':
			case 'checkbox':
				if ( !empty($field['Condition']) )
				{
					if ( $field['Condition'] != 'years' )
					{
						$vals = explode(',', $value);
						foreach ($vals as $val_item)
						{
							if ( !empty($GLOBALS['lang']['data_formats+name+'.$val_item]) )
							{
								$out .= $GLOBALS['lang']['data_formats+name+'.$val_item] . ', ';
							}
						}
						$out = substr( $out, 0, -2 );
					}
					else
					{
						$out = $value;
					}
				}
				else
				{
					$multi_values = explode( ',', $value );
					
					if ( !empty($multi_values[0]) )
					{
						foreach ( $multi_values as $chKey => $chVal )
						{
							$out .= $GLOBALS['lang'][$type . '_fields+name+'.$field['Key'].'_'.$multi_values[$chKey]] . ', ';
						}
						$out = substr( $out, 0, -2 );
					}
				}
				break;
			
			case 'image':
				if ( !$strip_tags )
				{
					$out = '<img alt="" src="' . RL_URL_HOME . 'files/' . $value . '" />';
				}
				break;
			
			case 'file':
				if ( !$strip_tags )
				{
					$out = '<a class="static" href="' . RL_URL_HOME . 'files/' . $value . '">' . $GLOBALS['lang']['download'] . '</a>';
				}
				break;
		}

		$GLOBALS['rlHook'] -> load('adaptValueBottom', $value, $field, $out);

		return $out;
	}
	
	/**
	* fields types adaptation (select, checkBox, radio)
	*
	* @param array $values - type values
	* @param string $table - table name
	* @param string $listing_type - listing type
	*
	**/
	function fieldValuesAdaptation( $fields, $table, $listing_type = false )
	{
		global $config, $rlCache, $edit_mode;

		if ( !$GLOBALS['data_formats'] )
		{
			$this -> outputRowsMap = 'Key';
			$GLOBALS['data_formats'] = $data_formats = $this -> fetch(array('ID', 'Key', 'Order_type'), array('Parent_ID' => 0, 'Status' => 'active') , 'ORDER BY `Key`', null, 'data_formats');
		}
		else
		{
			$data_formats = $GLOBALS['data_formats'];
		}

		$GLOBALS['rlHook'] -> load('phpCommonFieldValuesAdaptationTop', $fields, $table, $listing_type); // >= v4.3

		if ( !empty($fields) )
		{
			foreach ($fields as $index => $value)
			{
				// if ($fields[$index]['Key'] != 'Category_ID' && $this -> tmp['adapted_fields'][$fields[$index]['Key']]) {
				//  	$fields[$index] = $this -> tmp['adapted_fields'][$fields[$index]['Key']];
				// } else {
				if ( $fields[$index]['Type'] == 'select' || $fields[$index]['Type'] == 'checkbox' || $fields[$index]['Type'] == 'radio' || $fields[$index]['Type'] == 'mixed' )
				{
					// bind with data formats
					if ( $data_formats[$fields[$index]['Condition']] )
					{
						$format_values = false;
						
						if ($fields[$index]['Condition'] == 'years')
						{
							$step = 0;
							for( $i = date('Y')+2; $i>= 1940; $i-- )
							{
								$format_values[$step]['name'] = $i;
								$format_values[$step]['Key'] = $i;
								
								$step++;
							}
						}
						else
						{
							$format_values = $GLOBALS['rlCategories'] -> getDF($fields[$index]['Condition'], $data_formats[$fields[$index]['Condition']]['Order_type']);
						}

						$fields[$index]['Values'] = $format_values;
						unset($format_values);
					}
                    // system fields
                    else if (intval($fields[$index]['ID']) < 0) {
                        switch ($fields[$index]['Key']) {
                            case 'sf_status':
                                $statuses = array('active','approval','pending','incomplete','expired');
                                foreach($statuses as $i => $status) {
                                    $fields[$index]['Values'][$status] = array(
                                        'pName' => $status,
                                    );
                                }
                                break;

                            case 'sf_active_till':
                                $field = $this->getSystemFields('active_till', true);
                                $fields[$index]['Values'] = $field['Values'];
                                break;

                            case 'sf_plan':
                                $this->loadClass('Plan');
                                $tmp_plans = $GLOBALS['rlPlan']->getPlans(array('listing', 'package'));

                                foreach ($tmp_plans as $plan) {
                                    $plans[$plan['ID']] = array(
                                        'pName' => 'listing_plans+name+' . $plan['Key']
                                    );
                                }
                                unset($tmp_plans);

                                $fields[$index]['Values'] = $plans;
                                break;
                        }
                    }
					else 
					{
						$adapted = array();
						switch ($fields[$index]['Key']){
							case 'Category_ID':
								$fields[$index]['Values'] = $GLOBALS['rlCategories'] -> getCategories(0, $listing_type);
								break;
								
							case 'posted_by':
								$this -> loadClass('Account');
								$tmp_account_types = $GLOBALS['rlAccount'] -> getAccountTypes('visitor');
								foreach ($tmp_account_types as $tmp_account_type)
								{
									if ( $tmp_account_type['Abilities'] )
									{
										$adapted[$tmp_account_type['Key']] = array(
											'ID' => $tmp_account_type['Key'],
											'pName' => 'account_types+name+'. $tmp_account_type['Key']
										);
									}
								}
								$fields[$index]['Values'] = $adapted;
								unset($tmp_account_types, $adapted);
								break;
								
							default:
								$values = explode(',', $fields[$index]['Values']);
								
								if ($fields[$index]['Type'] == 'checkbox')
								{
									$default = explode(',', $fields[$index]['Default']);
									$fields[$index]['Default'] = $default;
								}

								foreach ($values as $row)
								{
									$adapted[$row]['Key'] = $fields[$index]['Key'] . '_' . $row;
									$adapted[$row]['pName'] = $table .'+name+'. $adapted[$row]['Key'];
									$adapted[$row]['ID'] = $row;

									if ($fields[$index]['Default'] == $row && $fields[$index]['Type'] == 'mixed') {
										$adapted[$row]['Default'] = 1;
									}
								}
								
								$fields[$index]['Values'] = $adapted;
								unset($adapted);
								break;
						}
					}

						// if ($fields[$index]['Key'] != 'Category_ID') {
						//   $this -> tmp['adapted_fields'][$fields[$index]['Key']] = $fields[$index];
						// }
				}
				//}
			}
		}

		$GLOBALS['rlHook'] -> load('phpCommonFieldValuesAdaptationBottom', $fields, $table, $listing_type); // >= v4.3

		unset($data_formats);

		return $fields;
	}
	
	/**
	* check parent with enabled subcategories including | recursive method
	*
	* @param int $id - category id
	* @param array $mode - check in area
	*
	* @return bool
	**/
	function detectParentIncludes( $id, $mode = 'blocks' )
	{
		$id = (int)$id;
		if ( !$id )
			return false;

		/* get parent */
		$parent = (int)$this -> getOne('Parent_ID', "`ID` = {$id}", 'categories');

		if ( !empty($parent) )
		{
			/* check relations */
			$target = $this -> getOne('ID', "FIND_IN_SET({$parent}, `Category_ID`) > 0  AND `Status` = 'active' AND `Subcategories` = '1'", $mode);

			if ( empty($target) )
			{
				return $this -> detectParentIncludes($parent, $mode);
			}

			return $parent;
		}
		return false;
	}
	
	/**
	* get blocks
	*
	* @return array - blocks array
	**/
	function getBlocks()
	{
		global $page_info, $config;

		if ( $config['mod_rewrite'] )
		{
			$category_path = $_GET['rlVareables'];
			$category_id = 0;

			if ( !empty($category_path) )
			{
				if ( $_GET[$config['mod_rewrite'] ? 'listing_id' : 'id'] )
				{
					$category_path = preg_replace( '/\/[^\/]*$/', '', trim($category_path,'/') );
				}
				$GLOBALS['rlValid'] -> sql($category_path);

				/* get category id */
				$category_id = (int)$this -> getOne('ID', "`Path` = CONVERT('{$category_path}' USING utf8) AND `Status` = 'active'", 'categories');
			}
		}
		else
		{
			$category_id = (int)$_GET['category'];
		}

		/* redefine page ID with listing details page ID, 25 in this case */
		if ( $page_info['Controller'] == 'listing_type' && $_GET[$config['mod_rewrite'] ? 'listing_id' : 'id'] )
		{
			$page_info['ID'] = 25;
		}

		if ( $page_info['Controller'] == 'listing_type' )
		{
			if ($additional = $this -> detectParentIncludes($category_id))
			{
				$add_where = "OR (FIND_IN_SET('{$additional}', `Category_ID`) > 0 AND `Subcategories` = '1') ";
			}

			if ($page_info['ID'] == 25) {
				$add_where_no_cat = "OR `Category_ID` = '' ";
			}

			$sql = "SELECT `ID`, `Key`, `Side`, `Type`, `Content`, `Tpl`, `Header`, `Position`, `Plugin` FROM `". RL_DBPREFIX ."blocks`";
			$sql .= "WHERE (`Sticky` = '1' OR ";
			$sql .= "(FIND_IN_SET( '{$page_info['ID']}', `Page_ID` ) > 0 ";

			if ( $category_id )
			{
				$sql .= "AND ((`Category_ID` <> '' AND FIND_IN_SET('{$category_id}', `Category_ID`) > 0) OR `Cat_sticky` = '1' {$add_where}{$add_where_no_cat}) ";
			}
			else
			{
				$sql .= "AND (`Category_ID` = '' ) ";
			}

			$sql .= ") ";
			
			if ( $category_id )
			{
				$sql .= "OR ( `Page_ID` ='' AND ((`Category_ID` <> '' AND FIND_IN_SET('{$category_id}', `Category_ID`) > 0) OR `Cat_sticky` = '1' {$add_where}) ) ";
			}

			$sql .= " ) ";
			$sql .= "AND `Status` = 'active' {$where} ";
			$sql .= "ORDER BY `Position` ";

			$tmp_blocks = $this -> getAll($sql);
		}
		else
		{
			$tmp_blocks = $this -> fetch( array('ID', 'Key', 'Side', 'Type', 'Content', 'Tpl', 'Header', 'Position', 'Plugin'), array( 'Status' => 'active' ), "AND (FIND_IN_SET( '{$page_info['ID']}', `Page_ID` ) > '0' OR `Sticky` = '1') {$where} ORDER BY `Position`", null, 'blocks' );
		}

		if ( !empty($tmp_blocks) )
		{
			foreach ($tmp_blocks as $key => $value)
			{
				$block_keys[$value['Key']] = true;

				$blocks[$value['Key']] = $value;
				if ( $value['Type'] == 'html' )
				{
					$blocks[$value['Key']]['Content'] = $GLOBALS['lang']['blocks+content+'.$value['Key']];
				}
			}

			unset($tmp_blocks);
			$this -> block_keys = $block_keys;

			$blocks = $GLOBALS['rlLang'] -> replaceLangKeys($blocks, 'blocks', array('name'));
			$this -> defineBlocksExist($blocks);
		}

		return $blocks;
	}

    /**
    * check messages exist
    *
    * @return int - count of messages
    **/
    function checkMessages()
    {
        global $account_info;

        $account_id = (int) $account_info['ID'] ?: false;
        $additional_sql = '';

        if (!$account_id) {
            return false;
        }

        if ($GLOBALS['rlAccount']->isAdmin()) {
            $from = 'Admin';

            $additional_sql = " AND `To` = 0";
        } else {
            $from = 'To';
        }

        $sql = "SELECT COUNT(`ID`) AS `Count` FROM `" . RL_DBPREFIX . "messages` ";
        $sql .= "WHERE `{$from}` = {$account_id} AND `Status` = 'new' {$additional_sql}";
        $count = $this->getRow($sql);

        return intval($count['Count']);
    }
	
	/**
	* get children | requrcive method
	*
	* @param int $id - main item id
	* @param array $items - all items array
	* @param int $target - target item
	*
	* @return bool - search result
	**/
	function checkRelation($id, $items, $target)
	{
		if ( $parent = $items[$id] )
		{
			if ( $parent == $target )
			{
				return true;
			}
			
			if ( $poss = $items[$parent] )
			{
				if ( $poss == $target )
				{
					return true;
				}
				else
				{
					return $this -> checkRelation( $poss, $items, $target );
				}
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	* get available hooks
	*
	*	NOTE: DEPRECATED
	**/
	function getHooks()
	{
		$this->getInstalledPluginsList();
	}

	/**
     * Fetch all installed and active plugins.
     * 
	 * @since 4.5.1
     * @return array - [key => version]
	 **/
	public function getInstalledPluginsList()
	{
        $this->setTable('plugins');
        $this->outputRowsMap = array('Key', 'Version');
        $plugins = $this->fetch($this->outputRowsMap, array('Status' => 'active'));

        /* support for old logic */
		$GLOBALS['aHooks'] = $GLOBALS['rlHook']->aHooks = &$plugins;

        if (is_object($GLOBALS['rlSmarty'])) {
            $GLOBALS['rlSmarty']->assign('aHooks', $GLOBALS['aHooks']);
        }
        /* support end */

        return $plugins;
	}

	/**
    * @since 4.5.1
    *
    * simulate the additional category box
    *
    **/
    function simulateCatBlocks()
    {
        global $rlListingTypes, $blocks, $rlCache, $rlSmarty, $rlCategories, $page_info, $rlHook;
        
        foreach ( $rlListingTypes -> types as $key => $value )
        {
            if ( in_array($page_info['ID'], explode(',', $value['Ablock_pages'])) )
            {
                $cat_blocks[$value['Ablock_position']][] = $key;
                $categories[$key] = $rlCategories->getCategories(0, $key, null, $value['Cat_show_subcats']);
            }
        }

        if ( $cat_blocks )
        {
            $rlSmarty -> assign_by_ref('box_categories', $categories);
            
            foreach ($cat_blocks as $side => $types)
            {
                if ( $types[0] && $blocks['ltcb_'. $types[0]] )
                {
                    $blocks['ltcb_'. $types[0]]['Content'] = '{include file="blocks"|cat:$smarty.const.RL_DS|cat:"categories_block.tpl" types="'. implode(',', $types) .'"}';
                }
                
                if ( count($types) > 1 )
                {
                    foreach ($types as $key => $type)
                    {
                        if ( $key > 0 )
                        {
                            unset($blocks['ltcb_'. $type]);
                        }
                    }
                }
            }

            /**
             * @since 4.4
             *
             * @param $cat_blocks @since 4.5.1
            **/
            $rlHook->load('simulateCatBlocks', $blocks, $categories, $cat_blocks);
            
            $this -> defineBlocksExist( $blocks );
        }
    }
	
	/**
	* Custom php function strlen
	*
	* @param string &$string - string for checking
	* @param string $sign - sign [<,>,<=,>=,==,!=]
	* @param int $len - length
	*
	* @return bool or false
	**/
	function strLen( &$string, $sign = '<', $len = 3 )
	{
		if ( function_exists('mb_strlen') )
		{
			eval("\$res = ( mb_strlen(\$string) {$sign} \$len );");
			return $res;
		}

		eval("\$res = ( strlen(\$string) {$sign} \$len );");
		return $res;
	}

    /**
     * Description
     * @param string $key
     * @param bool $assoc
     * @return mixed
     */
    function getSystemFields($key = null, $assoc = false) {
        global $lang;

        // prepare system fields
        $system_fields = array(
            '-1' => array(
                'ID' => -1,
                'Key' => 'sf_status',
                'Type' => 'select',
            ),
            '-2' => array(
                'ID' => -2,
                'Key' => 'sf_active_till',
                'Type' => 'select',
            ),
            '-3' => array(
                'ID' => -3,
                'Key' => 'sf_plan',
                'Type' => 'select',
            ),
            '-4' => array(
                'ID' => -4,
                'Key' => 'sf_featured',
                'Type' => 'bool',
            ),
        );

        if (!is_null($key) && $key != "") {
            // mapping if necessary
            if ($assoc === true) {
                $f_map = array(
                    'status' => '-1',
                    'active_till' => '-2',
                    'plan' => '-3',
                    'featured' => '-4',
                );
                $key = $f_map[$key];
            }

            if ($key === '-2') {
                $system_fields[$key]['Values'] = array(
                    1  => array('pName' => 'sf_active_till_1days'),
                    2  => array('pName' => 'sf_active_till_2days'),
                    3  => array('pName' => 'sf_active_till_3days'),
                    7  => array('pName' => 'sf_active_till_1weeks'),
                    14 => array('pName' => 'sf_active_till_2weeks'),
                    21 => array('pName' => 'sf_active_till_3weeks'),
                    30 => array('pName' => 'sf_active_till_1months'),
                    60 => array('pName' => 'sf_active_till_2months'),
                    90 => array('pName' => 'sf_active_till_3months'),
                );

                if (!$GLOBALS['config']) {
                    $this->buildActiveTillPhrases();
                }
            }
            return $system_fields[$key] ?: false;
        }
        return $system_fields;
    }

    public function buildActiveTillPhrases()
    {
        global $lang;

        for ($i = 1; $i <= 3; $i++) {
            $lang['sf_active_till_' . $i . 'days']   = sprintf($lang['sf_active_till_ndays'],   $i);
            $lang['sf_active_till_' . $i . 'weeks']  = sprintf($lang['sf_active_till_nweeks'],  $i);
            $lang['sf_active_till_' . $i . 'months'] = sprintf($lang['sf_active_till_nmonths'], $i);
        }
    }

    public function tplBlocks() {
		global $page_info, $lang, $blocks, $rlCommon, $rlSmarty, $account_info, $search_results_url, $advanced_search_url, $rlValid, $rlListingTypes;

		if ( in_array($page_info['Controller'], array('account_type', 'my_messages')) ) {
			$account_address = (int)$_GET['id'] ? (int)$_GET['id'] : $_GET['nvar_1'];
			$rlValid -> sql($account_address);
			if ($account_address) {
				$account_id = is_numeric($account_address) ? $account_address : $this -> getOne('ID', "`Own_address` = '{$account_address}'", 'accounts');
			}
		}

		if ( $page_info['Controller'] != 'account_type' || ($account_id && $page_info['Controller'] == 'account_type') ) {
			unset($blocks['account_alphabetic_filter'], $blocks['account_search']);
			$recount = true;
		}

		if (in_array($page_info['Controller'], array('account_type', 'my_messages'))) {
			if ($account_id < 0) {
				unset($blocks['account_page_location']);
				$blocks['account_page_info']['name'] = $lang['website_visitor'];

				$recount = true;
			} elseif (!$account_id) {
				unset($blocks['account_page_info'], $blocks['account_page_location']);
				$recount = true;
			}
		}
		else {
			unset($blocks['account_page_info'], $blocks['account_page_location']);
			$recount = true;
		}

		if ($page_info['Controller'] != 'listing_type' || 
			($page_info['Controller'] == 'listing_type' && ($_GET['listing_id'] || $_GET['id'] || $_GET['nvar_1'] == $search_results_url || $_GET['nvar_1'] == $advanced_search_url))) {
			foreach ($rlListingTypes -> types as &$l_type) {
				if ($blocks['ltcategories_'.$l_type['Key']]) {
					unset($blocks['ltcategories_'.$l_type['Key']]);
					$recount = true;
				}
			}
		}

		if ( !$page_info['Controller'] == 'profile' || !defined('IS_LOGIN') ) {
			unset($blocks['my_profile_sidebar']);
			$recount = true;
		}
		else {
			$blocks['my_profile_sidebar']['name'] = $account_info['Full_name'];
		}

		if ( $recount ) {
			$rlCommon -> defineBlocksExist($blocks);
		}

		if ( $page_info['Controller'] == 'registration' ) {
			$rlSmarty -> assign('no_h1', true);
		}
	}

    /**
     * Remove an "Search in My Ads" box from stack
     * 
     * @since 4.5
     * @param mixed $ltype_key    - listing type key
     * @param mixed $box_key      - system box key in a stack
     * @param bool  $define_boxes - trigger to allow or not execute defineBlocksExist method
     */
    public function removeSearchInMyAdsBox($ltype_key = false, $box_key = false, $define_boxes = true)
    {
        global $blocks, $block_keys;

        if (!$ltype_key && !$box_key) {
            return;
        }

        if ($ltype_key) {
            $box_key = 'ltma_' . $ltype_key;
        }

        unset($block_keys[$box_key], $blocks[$box_key]);

        if ($define_boxes !== false) {
            $this->defineBlocksExist($blocks);
        }
    }

    /**
     * Remove all "Search in My Ads" boxes from stack
     * 
     * @since 4.5
     * @see removeSearchInMyAdsBox
     */
    public function removeAllSearchInMyAdsBoxes()
    {
        foreach ($GLOBALS['block_keys'] as $key => $value) {
            if (preg_match('/^ltma_/', $key)) {
                $this->removeSearchInMyAdsBox(false, $key, false);
            }
        }
        $this->defineBlocksExist($GLOBALS['blocks']);
    }

    /**
     * prepare special content for the home page
     * 
     * @since 4.5.1
     */
    public function homePageSpecialContent()
    {
        global $tpl_settings, $blocks, $rlSmarty;

        // define featured listings box to apply gellery affect to
        if ($tpl_settings['home_page_gallery']) {
            foreach ($blocks as $key => &$block) {
                if ($block['Side'] == 'top' && strpos($block['Key'], 'ltfb_') === 0) {
                    $content = $block['Content'];
                    unset($blocks[$key]);
                    break;
                }
            }
            if (!$content) {
                foreach ($blocks as $key => &$block) {
                    if ($block['Side'] == 'middle' && strpos($block['Key'], 'ltfb_') === 0) {
                        $content = $block['Content'];
                        unset($blocks[$key]);
                        break;
                    }
                }
            }
            if (!$content) {
                foreach ($blocks as $key => &$block) {
                    if ($block['Side'] == 'bottom' && strpos($block['Key'], 'ltfb_') === 0) {
                        $content = $block['Content'];
                        unset($blocks[$key]);
                        break;
                    }
                }
            }
            if (!$content) {
                $content = "{include file='blocks'|cat:\$smarty.const.RL_DS|cat:'featured.tpl' listings=\$featured_gallery type='demo_gallery_type' field='condition' value='2'}";

                $GLOBALS['rlListingTypes']->types['demo_gallery_type'] = array(
                    'Photo' => true,
                    'Status' => 'approval'
                );
            }

            $GLOBALS['rlCommon'] -> defineBlocksExist($blocks);
            $rlSmarty->assign('gallary_content', $content);

            preg_match('/listings=\\$([^\\s]+)/', $content, $matches);
            $featured_listings = $rlSmarty->get_template_vars($matches[1]);
            if (!$featured_listings) {
                $array = array(
                    'Main_photo' => RL_TPL_BASE .'img/no-thumb.jpg',
                    'custom' => true,
                    'listing_title' => 'Demo Gallery, please create feature listings'
                );
                $featured_listings = array_fill(0, 5, $array);
                $rlSmarty->assign_by_ref($matches[1], $featured_listings);
                $rlSmarty->assign('demo_gallery', true);
            }
        }

        // prepare the box data for special area on the home page
        if ($tpl_settings['home_page_special_block']) {
            if ($blocks[$tpl_settings['home_page_special_block']]) {
                $special_block = $blocks[$tpl_settings['home_page_special_block']];
                unset($blocks[$tpl_settings['home_page_special_block']]);
            }

            if (!$special_block) {
                reset($blocks);
                $special_block = current($blocks);

                unset($blocks[$special_block['Key']]);
            }

            if ($special_block) {
                $this->defineBlocksExist($blocks);
                $rlSmarty->assign_by_ref('home_page_special_block', $special_block);
            }
        }
    }
}