<commit_before><?php defined('ROOT') or die('No direct script access.');
/**
 * X3 CMS - A smart Content Management System
 *
 * @author		<NAME>
 * @copyright	(c) CBlu.net di <NAME>
 * @license		http://www.gnu.org/licenses/agpl.htm
 * @package		X4WEBAPP
 */

/**
 * Helper for Form handling
 * 
 * @package X4WEBAPP
 */
class X4Form_helper 
{
	// array of items to exclude
	public static $exclude = array('html', 'clear', 'loading', 'slider', 'button');
	
	/**
	 * Build a form
	 * each item of fields is an associative array with informations about the field
	 * field = array(
	 *	'label' => field's label string OR null
	 *	'type' => html | hidden | text | file | password | checkbox | radio | textarea | select | clear | loading | slider | button
	 *	'name' => the name of the field
	 *	'value' => the default value of the field
	 *	// after there are optionals
	 *	'rule' => rules for the field eg. required|mail see validation method
	 *	'extra' => attribute for the field, useful for CSS style
	 *	'suggestion' => a suggestion message for the field
	 *	// only for files
	 *	'old' => the old value of the field if not empty
	 *	'folder' => if set the form show a link to the file for check
	 *	'delete' => label for removal, if set you have a checkbox so you can remove old file without add a new file
	 *	// only for checkbox
	 *	'checked' => 0|1 set the default status
	 *	// only for select
	 *	'options' => an array(array of objects, attribute name for value, attribute name for option, optional value for empty option)
	 *	'disabled' => if set as name of boolean attribute of array of objects disable options where attribute is true
	 *	'multiple' => integer, set multiple select height
	 *
	 * @static
	 * @param	string	$name form name
	 * @param	string	$action form action
	 * @param	mixed	$fields array of form fields or prebuilded form string
	 * @param	array	$buttons array of form buttons (reset button, submit button, alignment)
	 * @param	string	$method form method
	 * @param	string	$extra extra value for the form (eg. enctype="multipart/form-data")
	 * @param	string	$submit_action javascript to run on submit
	 * @param	string	$reset_actionjavascript to run on reset
	 * @param	boolean	$inline label in the same line of the input field
	 * @return string
	 */
	public static function doform($name, $action, $fields, $buttons = array(_RESET, _SUBMIT, 'xcenter'), $method = 'post', $extra = '', $submit_action = '', $reset_action = '', $inline = false)
	{
		if (!defined('NOCACHE')) define('NOCACHE', true);
		
		if (!empty($submit_action)) 
			$extra .= ' onsubmit="return false"';
		
		// sanitize action
		$action = htmlentities(strip_tags($action), ENT_QUOTES, 'UTF-8', false);
		
		$btn = false;
		if (is_array($fields)) 
		{
			// add x4 token field security token
			$fields[] = array(
				'label' => null,
				'type' => 'hidden', 
				'value' => md5($_SESSION['token'].$name),
				'name' => 'x4token'
			);
			
			// check for buttons
			$btn = true;
			
			// build form tag
			$str = '<form id="'.$name.'" action="'.$action.'" method="'.$method.'" '.$extra.'>';
			
			foreach($fields as $i)
			{
				$tmp = $req = '';
				// check for hidden
				if (isset($i['hide']))
					$str .= '<div id="'.$i['hide'].'" class="hide">';
				
				// label
				if (!is_null($i['label']) && $i['type'] != 'button') 
				{
					$req = (isset($i['rule']) && (strstr($i['rule'], 'required') != '')) ? ' *' : '';
					$err = (isset($i['error'])) ? ' class="error"' : '';
					
					if ($inline || (isset($i['extra']) && strstr($i['extra'], 'inline') != ''))
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req;
					}
					else
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req.'</label>';
					}
				}
				
				switch($i['type'])
				{
				case 'clear':
					$str .= self::clear();
					break;
				case 'loading':
					$str .= '<div id="loading" style="visibility:hidden;"><img src="'.ROOT.'files/files/ajax-loader.gif" alt="Loading..." /></div>';
					break;
				case 'slider':
					$str .= self::slider($i['name']);
					break;
				case 'html':
					$str .= $i['value'];
					break;
				case 'button':
					$str .= '<div class="xcenter">'.self::button($i).'</div>';
					break;
				default:
					// for: fieldset, hidden, text, file, password, checkbox, radio, texarea, select
					$t = $i['type'];
					$str .= self::$t($i, $req);
					break;
				}
				
				// close label
				if (($inline || (isset($i['extra']) && strstr($i['extra'], 'inline') != '')) && !is_null($i['label']))
				{
					$str .= '</label>';
				}
				
				// close hidden
				if (isset($i['hide']))
				{
					$str .= '</div>';
				}
			}
		}
		else 
		{
			// if fields is not an array inject in a prebuilded form
			// build form tag
			$str = '<form id="'.$name.'"  action="'.$action.'" method="'.$method.'" '.$extra.'>
				'.$fields.'<input type="hidden" value="'.md5($_SESSION['token'].$name).'" name="x4token" id="x4token" />';
		}
		
		// buttons box
		if ($btn) 
		{
			$str .= self::buttons($buttons, $name, $submit_action, $reset_action);
		}
		
		$str .= '</form>';
		return $str;
	}
	
	/**
	 * Build a form section
	 *
	 * @static
	 * @param	mixed	$fields array of form fields or prebuilded form string
	 * @param	string	$form name
	 * @return string
	 */
	public static function doform_section($fields, $name = '')
	{
		$str = '';
		
		if (is_array($fields)) 
		{
			if (!empty($name))
			{
				// add x4 token field security token
				$fields[] = array(
					'label' => null,
					'type' => 'hidden', 
					'value' => md5($_SESSION['token'].$name),
					'name' => 'x4token'
				);
			}
			
			foreach($fields as $i)
			{
				$tmp = $req = '';
				// check for hidden
				if (isset($i['hide']))
					$str .= '<div id="'.$i['hide'].'" class="hide">';
				
				// label
				if (!is_null($i['label']) && $i['type'] != 'button') 
				{
					$req = (isset($i['rule']) && (strstr($i['rule'], 'required|') != '' || $i['rule'] == 'required')) ? ' *' : '';
					$err = (isset($i['error'])) ? ' class="error"' : '';
					
					if (isset($i['extra']) && $i['extra'] == 'inline')
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req;
					}
					else
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req.'</label>';
					}
				}
				
				switch($i['type'])
				{
				case 'clear':
					$str .= self::clear();
					break;
				case 'loading':
					$str .= '<div id="loading" style="visibility:hidden;"><img src="'.ROOT.'files/files/ajax-loader.gif" alt="Loading..." /></div>';
					break;
				case 'slider':
					$str .= self::slider($i['name']);
					break;
				case 'html':
					$str .= $i['value'];
					break;
				case 'button':
					$str .= '<div class="text-center">'.self::button($i).'</div>';
					break;
				default:
					// for: fieldset, hidden, text, file, password, checkbox, radio, texarea, select
					$t = $i['type'];
					$str .= self::$t($i, $req);
					break;
				}
				
				// close label
				if (isset($i['extra']) && $i['extra'] == 'inline' && !is_null($i['label']))
				{
					$str .= '</label>';
				}
				
				// close hidden
				if (isset($i['hide']))
				{
					$str .= '</div>';
				}
			}
		}
		
		return $str;
	}
	
	/**
	 * Return clear div
	 *
	 * @static
	 * @return string
	 */
	public static function clear()
	{
		return '<div class="clear"></div>';
	}
	
	/**
	 * Return slider
	 *
	 * @static
	 * @param string	$name id of the slider
	 * @return string
	 */
	public static function slider($name)
	{
		return '<div id="'.$name.'" class="slider"><div class="knob"></div></div>';
	}
	
	/**
	 * Return fieldset
	 *
	 * @static
	 * @param array		$e Fieldset data
	 * @return string
	 */
	public static function fieldset($e, $req = '')
	{
		if ($e['value'] == 'open') 
		{
			$iextra = (isset($e['extra'])) 
				? $e['extra'] 
				: '';
				
			return '<fieldset id="'.$e['name'].'" '.$iextra.'>';
		}
		else 
			return '</fieldset>';
	}
	
	/**
	 * Return hidden input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function hidden($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
		
		// replace with session value
		if ($e['value'] == 'SESSION')
		{
		    if (isset($_SESSION[$e['name']]))
		    {
		        $e['value'] = $_SESSION[$e['name']];
		    }
		    elseif (isset($_SESSION['id_'.$e['name']]))
		    {
		        $mod = new Log_model();
		        $tokens = explode('|', $e['extra']);
		        $e['value'] = $mod->get_var($_SESSION['id_'.$e['name']], $tokens[0], $tokens[1]);
		        $iextra = '';
		    }
		}
			
		return '<input type="hidden" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.' />';
	}
	
	/**
	 * Return text input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function text($e, $req = '')
	{
		$error = (is_null($e['label']) && isset($e['error']))
			? 'error '
			: '';
			
		$iextra = (isset($e['extra'])) 
			? str_replace('class="', 'class="'.$error, $e['extra']) 
			: '';
			
		if (!empty($error) && empty($iextra))
		{
			$iextra = 'class="error"';
		}
			
		$readonly = (isset($e['readonly'])) 
			? ' readonly="readonly"' 
			: '';
		
		$placeholder = (isset($e['placeholder']))
			? ' placeholder="'.$e['placeholder'].'"'
			: '';
			
		if ($e['name'] == 'captcha') 
			$e['value'] = '';
		
		return '<input type="text" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.$readonly.$placeholder.' />'.self::suggestion($e);
	}
	
	/**
	 * Return file input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function file($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
			
		if (isset($e['multiple']))
		{
			$fname = $e['name'].'[]';
			$multiple = 'multiple="multiple"';
		}
		else
		{
			$fname = $e['name'];
			$multiple = '';
		}
		$field = '<input '.$iextra.' type="file" name="'.$fname.'" id="'.$e['name'].'" value="'.$e['value'].'" '.$multiple.' />';
		
		// for delete option
		$tmp = '';
		if (isset($e['old']) && !empty($e['old'])) 
		{
			$tmp = '<p class="xsmall">';
			// can display the file only if knowns his path
			if (isset($e['folder'])) 
			{
				$tmp = ($e['folder'] == 'img') 
					? '<br /><img class="thumb dblock" src="'.FPATH.$e['folder'].'/'.$e['old'].'" alt="thumb" />' 
					: '';

				if (is_dir(APATH.'files/filemanager/'.$e['folder']))
				{
					$tmp .= ' <a target="_blank" href="'.FPATH.$e['folder'].'/'.$e['old'].'" title="">'.$e['old'].'</a><br />';
				}
				elseif (is_dir(APATH.'files/'.$e['folder']))
				{
					$tmp .= ' <a target="_blank" href="'.APATH.'files/'.$e['folder'].'/'.$e['old'].'" title="">'.$e['old'].'</a><br />';
				}
				
			}
			elseif (isset($e['aold']))
			{
				$tmp .= $e['aold'];
			}
			else 
			{
				$tmp .= $e['old'];
			}
			
			// for removal
			if (isset($e['delete']) && $req != ' *') 
			{
				$tmp .= '<label class="inline" for="delete_'.$e['name'].'"><input type="checkbox" class="check" name="delete_'.$e['name'].'" id="delete_'.$e['name'].'" value="1" /> '.$e['delete'].'</label>';
			}
			$tmp .= '<input type="hidden" name="old_'.$e['name'].'" id="old_'.$e['name'].'" value="'.$e['old'].'" />';
		}

		return $field.self::suggestion($e).$tmp;
	}
	
	/**
	 * Return password input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function password($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
			
		return '<input type="password" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.' />'.self::suggestion($e);
	}
	
	/**
	 * Return checkbox input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function checkbox($e, $req = '')
	{
		$inline = false;
		
		$checked = (isset($e['checked']) && intval($e['checked']) > 0) 
			? 'checked="checked"' 
			: '';
			
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="check ', $e['extra'])
				: 'class="check" '.$e['extra'];
				
			// is inline?
			$inline = (strstr($e['extra'], 'inline') != '');
		}
		else
		{
			$class = 'class="check"';
		}
		
		if ($inline && is_null($e['label']) && isset($e['suggestion']))
		{
			$label_class = isset($e['error'])
				? 'class="inline error"'
				: 'class="inline"';
			
			$tmp = '<label '.$label_class.' for="'.$e['name'].'"><input type="checkbox" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$checked.' /> &nbsp;&nbsp;'.stripslashes($e['suggestion']).'</label>';
		}
		else
		{
			$tmp = '<input type="checkbox" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$checked.' />'.self::suggestion($e);
		}	
		return $tmp;
	}
	
	/**
	 * Return multiple checkbox input fields
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function mcheckbox($e, $req = '')
	{
		$inline = false;
		
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="check ', $e['extra'])
				: 'class="check" '.$e['extra'];
				
			// is inline?
			$inline = (strstr($e['extra'], 'inline') != '');
		}
		else
		{
			$class = 'class="check"';
		}
		
		$tmp = '';
		if (!empty($e['options'][0])) 
		{
			$error = isset($e['error'])
				? 'class="error"'
				: '';
			
			$v = $e['options'][1];
			$o = $e['options'][2];
			
			$c = 0;
			foreach($e['options'][0] as $i)
			{
				$checked = (isset($e['checked']) && in_array($i->$v, $e['checked'])) 
					? 'checked="checked"' 
					: '';
					
				if ($inline && is_null($e['label']))
				{
					$tmp .= '<div class="checkbox"><input type="checkbox" '.$class.' name="'.$e['name'].'[]" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /><label '.$error.' for="'.$e['name'].'_'.$c.'">'.stripslashes($i->$v).'</label></div>';
				}
				else
				{
					$tmp .= '<div class="checkbox"><label '.$error.' for="'.$e['name'].'_'.$c.'"><input type="checkbox" '.$class.' name="'.$e['name'].'[]" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' />'.stripslashes($i->$v).'</label></div>';
				}
				$c++;
			}
		}
		return $tmp.self::suggestion($e);
	}
	
	/**
	 * Return radio input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function radio($e, $req = '')
	{
		$inline = false;
		$br = BR;
		
		$error = isset($e['error'])
            ? 'class="error"'
            : '';
				
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="radio ', $e['extra'])
				: 'class="radio" '.$e['extra'];
			
			// is inline
			if (strstr($e['extra'], 'inline') != '')
			{
				$inline = true;
				$br = '';
			}
		}
		else
		{
			$class = 'class="radio"';
		}
		
		$tmp = '';
		if (!empty($e['options'][0])) 
		{
			$v = $e['options'][1];
			$o = $e['options'][2];
			
			$c = 0;
			foreach($e['options'][0] as $i)
			{
				$checked = (isset($e['checked']) && $e['checked'] == $i->$v) 
					? 'checked="checked"' 
					: '';
					
				if ($inline)
				{
					$tmp .= '<div class="radiobox"><input type="radio" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /> <label for="'.$e['name'].'_'.$c.'" '.$error.'>'.stripslashes($i->$o).'</label></div>';
				}
				else
				{
					$tmp .= '<div class="radiobox"><label for="'.$e['name'].'_'.$c.'" '.$error.'><input type="radio" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /> '.stripslashes($i->$o).'</label> </div>';
				}
				$c++;
			}
		}
		
		if ($inline) 
		{
			$tmp = '<div class="inliner clearfix">'.$tmp.'</div>';
		}
		<commit_msg>added support to bootstrap construction for checkboxes<commit_after><?php defined('ROOT') or die('No direct script access.');
/**
 * X3 CMS - A smart Content Management System
 *
 * @author		<NAME>
 * @copyright	(c) CBlu.net di <NAME>
 * @license		http://www.gnu.org/licenses/agpl.htm
 * @package		X4WEBAPP
 */

/**
 * Helper for Form handling
 * 
 * @package X4WEBAPP
 */
class X4Form_helper 
{
	// array of items to exclude
	public static $exclude = array('html', 'clear', 'loading', 'slider', 'button');
	
	/**
	 * Build a form
	 * each item of fields is an associative array with informations about the field
	 * field = array(
	 *	'label' => field's label string OR null
	 *	'type' => html | hidden | text | file | password | checkbox | radio | textarea | select | clear | loading | slider | button
	 *	'name' => the name of the field
	 *	'value' => the default value of the field
	 *	// after there are optionals
	 *	'rule' => rules for the field eg. required|mail see validation method
	 *	'extra' => attribute for the field, useful for CSS style
	 *	'suggestion' => a suggestion message for the field
	 *	// only for files
	 *	'old' => the old value of the field if not empty
	 *	'folder' => if set the form show a link to the file for check
	 *	'delete' => label for removal, if set you have a checkbox so you can remove old file without add a new file
	 *	// only for checkbox
	 *	'checked' => 0|1 set the default status
	 *	// only for select
	 *	'options' => an array(array of objects, attribute name for value, attribute name for option, optional value for empty option)
	 *	'disabled' => if set as name of boolean attribute of array of objects disable options where attribute is true
	 *	'multiple' => integer, set multiple select height
	 *
	 * @static
	 * @param	string	$name form name
	 * @param	string	$action form action
	 * @param	mixed	$fields array of form fields or prebuilded form string
	 * @param	array	$buttons array of form buttons (reset button, submit button, alignment)
	 * @param	string	$method form method
	 * @param	string	$extra extra value for the form (eg. enctype="multipart/form-data")
	 * @param	string	$submit_action javascript to run on submit
	 * @param	string	$reset_actionjavascript to run on reset
	 * @param	boolean	$inline label in the same line of the input field
	 * @return string
	 */
	public static function doform($name, $action, $fields, $buttons = array(_RESET, _SUBMIT, 'xcenter'), $method = 'post', $extra = '', $submit_action = '', $reset_action = '', $inline = false)
	{
		if (!defined('NOCACHE')) define('NOCACHE', true);
		
		if (!empty($submit_action)) 
			$extra .= ' onsubmit="return false"';
		
		// sanitize action
		$action = htmlentities(strip_tags($action), ENT_QUOTES, 'UTF-8', false);
		
		$btn = false;
		if (is_array($fields)) 
		{
			// add x4 token field security token
			$fields[] = array(
				'label' => null,
				'type' => 'hidden', 
				'value' => md5($_SESSION['token'].$name),
				'name' => 'x4token'
			);
			
			// check for buttons
			$btn = true;
			
			// build form tag
			$str = '<form id="'.$name.'" action="'.$action.'" method="'.$method.'" '.$extra.'>';
			
			foreach($fields as $i)
			{
				$tmp = $req = '';
				// check for hidden
				if (isset($i['hide']))
					$str .= '<div id="'.$i['hide'].'" class="hide">';
				
				// label
				if (!is_null($i['label']) && $i['type'] != 'button') 
				{
					$req = (isset($i['rule']) && (strstr($i['rule'], 'required') != '')) ? ' *' : '';
					$err = (isset($i['error'])) ? ' class="error"' : '';
					
					if ($inline || (isset($i['extra']) && strstr($i['extra'], 'inline') != ''))
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req;
					}
					else
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req.'</label>';
					}
				}
				
				switch($i['type'])
				{
				case 'clear':
					$str .= self::clear();
					break;
				case 'loading':
					$str .= '<div id="loading" style="visibility:hidden;"><img src="'.ROOT.'files/files/ajax-loader.gif" alt="Loading..." /></div>';
					break;
				case 'slider':
					$str .= self::slider($i['name']);
					break;
				case 'html':
					$str .= $i['value'];
					break;
				case 'button':
					$str .= '<div class="xcenter">'.self::button($i).'</div>';
					break;
				default:
					// for: fieldset, hidden, text, file, password, checkbox, radio, texarea, select
					$t = $i['type'];
					$str .= self::$t($i, $req);
					break;
				}
				
				// close label
				if (($inline || (isset($i['extra']) && strstr($i['extra'], 'inline') != '')) && !is_null($i['label']))
				{
					$str .= '</label>';
				}
				
				// close hidden
				if (isset($i['hide']))
				{
					$str .= '</div>';
				}
			}
		}
		else 
		{
			// if fields is not an array inject in a prebuilded form
			// build form tag
			$str = '<form id="'.$name.'"  action="'.$action.'" method="'.$method.'" '.$extra.'>
				'.$fields.'<input type="hidden" value="'.md5($_SESSION['token'].$name).'" name="x4token" id="x4token" />';
		}
		
		// buttons box
		if ($btn) 
		{
			$str .= self::buttons($buttons, $name, $submit_action, $reset_action);
		}
		
		$str .= '</form>';
		return $str;
	}
	
	/**
	 * Build a form section
	 *
	 * @static
	 * @param	mixed	$fields array of form fields or prebuilded form string
	 * @param	string	$form name
	 * @return string
	 */
	public static function doform_section($fields, $name = '')
	{
		$str = '';
		
		if (is_array($fields)) 
		{
			if (!empty($name))
			{
				// add x4 token field security token
				$fields[] = array(
					'label' => null,
					'type' => 'hidden', 
					'value' => md5($_SESSION['token'].$name),
					'name' => 'x4token'
				);
			}
			
			foreach($fields as $i)
			{
				$tmp = $req = '';
				// check for hidden
				if (isset($i['hide']))
					$str .= '<div id="'.$i['hide'].'" class="hide">';
				
				// label
				if (!is_null($i['label']) && $i['type'] != 'button') 
				{
					$req = (isset($i['rule']) && (strstr($i['rule'], 'required|') != '' || $i['rule'] == 'required')) ? ' *' : '';
					$err = (isset($i['error'])) ? ' class="error"' : '';
					
					if (isset($i['extra']) && $i['extra'] == 'inline')
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req;
					}
					else
					{
						$str .= '
						<label for="'.$i['name'].'" '.$err.'>'.stripslashes($i['label']).$req.'</label>';
					}
				}
				
				switch($i['type'])
				{
				case 'clear':
					$str .= self::clear();
					break;
				case 'loading':
					$str .= '<div id="loading" style="visibility:hidden;"><img src="'.ROOT.'files/files/ajax-loader.gif" alt="Loading..." /></div>';
					break;
				case 'slider':
					$str .= self::slider($i['name']);
					break;
				case 'html':
					$str .= $i['value'];
					break;
				case 'button':
					$str .= '<div class="text-center">'.self::button($i).'</div>';
					break;
				default:
					// for: fieldset, hidden, text, file, password, checkbox, radio, texarea, select
					$t = $i['type'];
					$str .= self::$t($i, $req);
					break;
				}
				
				// close label
				if (isset($i['extra']) && $i['extra'] == 'inline' && !is_null($i['label']))
				{
					$str .= '</label>';
				}
				
				// close hidden
				if (isset($i['hide']))
				{
					$str .= '</div>';
				}
			}
		}
		
		return $str;
	}
	
	/**
	 * Return clear div
	 *
	 * @static
	 * @return string
	 */
	public static function clear()
	{
		return '<div class="clear"></div>';
	}
	
	/**
	 * Return slider
	 *
	 * @static
	 * @param string	$name id of the slider
	 * @return string
	 */
	public static function slider($name)
	{
		return '<div id="'.$name.'" class="slider"><div class="knob"></div></div>';
	}
	
	/**
	 * Return fieldset
	 *
	 * @static
	 * @param array		$e Fieldset data
	 * @return string
	 */
	public static function fieldset($e, $req = '')
	{
		if ($e['value'] == 'open') 
		{
			$iextra = (isset($e['extra'])) 
				? $e['extra'] 
				: '';
				
			return '<fieldset id="'.$e['name'].'" '.$iextra.'>';
		}
		else 
			return '</fieldset>';
	}
	
	/**
	 * Return hidden input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function hidden($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
		
		// replace with session value
		if ($e['value'] == 'SESSION')
		{
		    if (isset($_SESSION[$e['name']]))
		    {
		        $e['value'] = $_SESSION[$e['name']];
		    }
		    elseif (isset($_SESSION['id_'.$e['name']]))
		    {
		        $mod = new Log_model();
		        $tokens = explode('|', $e['extra']);
		        $e['value'] = $mod->get_var($_SESSION['id_'.$e['name']], $tokens[0], $tokens[1]);
		        $iextra = '';
		    }
		}
			
		return '<input type="hidden" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.' />';
	}
	
	/**
	 * Return text input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function text($e, $req = '')
	{
		$error = (is_null($e['label']) && isset($e['error']))
			? 'error '
			: '';
			
		$iextra = (isset($e['extra'])) 
			? str_replace('class="', 'class="'.$error, $e['extra']) 
			: '';
			
		if (!empty($error) && empty($iextra))
		{
			$iextra = 'class="error"';
		}
			
		$readonly = (isset($e['readonly'])) 
			? ' readonly="readonly"' 
			: '';
		
		$placeholder = (isset($e['placeholder']))
			? ' placeholder="'.$e['placeholder'].'"'
			: '';
			
		if ($e['name'] == 'captcha') 
			$e['value'] = '';
		
		return '<input type="text" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.$readonly.$placeholder.' />'.self::suggestion($e);
	}
	
	/**
	 * Return file input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function file($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
			
		if (isset($e['multiple']))
		{
			$fname = $e['name'].'[]';
			$multiple = 'multiple="multiple"';
		}
		else
		{
			$fname = $e['name'];
			$multiple = '';
		}
		$field = '<input '.$iextra.' type="file" name="'.$fname.'" id="'.$e['name'].'" value="'.$e['value'].'" '.$multiple.' />';
		
		// for delete option
		$tmp = '';
		if (isset($e['old']) && !empty($e['old'])) 
		{
			$tmp = '<p class="xsmall">';
			// can display the file only if knowns his path
			if (isset($e['folder'])) 
			{
				$tmp = ($e['folder'] == 'img') 
					? '<br /><img class="thumb dblock" src="'.FPATH.$e['folder'].'/'.$e['old'].'" alt="thumb" />' 
					: '';

				if (is_dir(APATH.'files/filemanager/'.$e['folder']))
				{
					$tmp .= ' <a target="_blank" href="'.FPATH.$e['folder'].'/'.$e['old'].'" title="">'.$e['old'].'</a><br />';
				}
				elseif (is_dir(APATH.'files/'.$e['folder']))
				{
					$tmp .= ' <a target="_blank" href="'.APATH.'files/'.$e['folder'].'/'.$e['old'].'" title="">'.$e['old'].'</a><br />';
				}
				
			}
			elseif (isset($e['aold']))
			{
				$tmp .= $e['aold'];
			}
			else 
			{
				$tmp .= $e['old'];
			}
			
			// for removal
			if (isset($e['delete']) && $req != ' *') 
			{
				$tmp .= '<label class="inline" for="delete_'.$e['name'].'"><input type="checkbox" class="check" name="delete_'.$e['name'].'" id="delete_'.$e['name'].'" value="1" /> '.$e['delete'].'</label>';
			}
			$tmp .= '<input type="hidden" name="old_'.$e['name'].'" id="old_'.$e['name'].'" value="'.$e['old'].'" />';
		}

		return $field.self::suggestion($e).$tmp;
	}
	
	/**
	 * Return password input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function password($e, $req = '')
	{
		$iextra = (isset($e['extra'])) 
			? $e['extra'] 
			: '';
			
		return '<input type="password" name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$iextra.' />'.self::suggestion($e);
	}
	
	/**
	 * Return checkbox input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function checkbox($e, $req = '')
	{
		$inline = false;
		
		$checked = (isset($e['checked']) && intval($e['checked']) > 0) 
			? 'checked="checked"' 
			: '';
			
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="check ', $e['extra'])
				: 'class="check" '.$e['extra'];
				
			// is inline?
			$inline = (strstr($e['extra'], 'inline') != '');
		}
		else
		{
			$class = 'class="check"';
		}
		
		if ($inline && is_null($e['label']) && isset($e['suggestion']))
		{
			$label_class = isset($e['error'])
				? 'class="inline error"'
				: 'class="inline"';
			
			$tmp = '<label '.$label_class.' for="'.$e['name'].'"><input type="checkbox" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$checked.' /> &nbsp;&nbsp;<span>'.stripslashes($e['suggestion']).'</span></label>';
		}
		else
		{
			$tmp = '<input type="checkbox" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'" value="'.$e['value'].'" '.$checked.' />'.self::suggestion($e);
		}	
		return $tmp;
	}
	
	/**
	 * Return multiple checkbox input fields
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function mcheckbox($e, $req = '')
	{
		$inline = false;
		
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="check ', $e['extra'])
				: 'class="check" '.$e['extra'];
				
			// is inline?
			$inline = (strstr($e['extra'], 'inline') != '');
		}
		else
		{
			$class = 'class="check"';
		}
		
		$tmp = '';
		if (!empty($e['options'][0])) 
		{
			$error = isset($e['error'])
				? 'class="error"'
				: '';
			
			$v = $e['options'][1];
			$o = $e['options'][2];
			
			$c = 0;
			foreach($e['options'][0] as $i)
			{
				$checked = (isset($e['checked']) && in_array($i->$v, $e['checked'])) 
					? 'checked="checked"' 
					: '';
					
				if ($inline && is_null($e['label']))
				{
					$tmp .= '<div class="checkbox"><input type="checkbox" '.$class.' name="'.$e['name'].'[]" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /><label '.$error.' for="'.$e['name'].'_'.$c.'">'.stripslashes($i->$v).'</label></div>';
				}
				else
				{
					$tmp .= '<div class="checkbox"><label '.$error.' for="'.$e['name'].'_'.$c.'"><input type="checkbox" '.$class.' name="'.$e['name'].'[]" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' />'.stripslashes($i->$v).'</label></div>';
				}
				$c++;
			}
		}
		return $tmp.self::suggestion($e);
	}
	
	/**
	 * Return radio input field
	 *
	 * @static
	 * @param array		$e Field data
	 * @return string
	 */
	public static function radio($e, $req = '')
	{
		$inline = false;
		$br = BR;
		
		$error = isset($e['error'])
            ? 'class="error"'
            : '';
				
		if (isset($e['extra']))
		{
			$class = (strstr($e['extra'], 'class="'))
				? str_replace('class="', 'class="radio ', $e['extra'])
				: 'class="radio" '.$e['extra'];
			
			// is inline
			if (strstr($e['extra'], 'inline') != '')
			{
				$inline = true;
				$br = '';
			}
		}
		else
		{
			$class = 'class="radio"';
		}
		
		$tmp = '';
		if (!empty($e['options'][0])) 
		{
			$v = $e['options'][1];
			$o = $e['options'][2];
			
			$c = 0;
			foreach($e['options'][0] as $i)
			{
				$checked = (isset($e['checked']) && $e['checked'] == $i->$v) 
					? 'checked="checked"' 
					: '';
					
				if ($inline)
				{
					if (isset($e['bootstrap']))
					{
						$tmp .= '<input type="radio" class="radio inline custom-control-input" name="'.$e['name'].'" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /> <label class="inline custom-control-label" for="'.$e['name'].'_'.$c.'" '.$error.'>'.stripslashes($i->$o).'</label>';
					}
					else
					{
						$tmp .= '<div class="radiobox"><input type="radio" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /> <label for="'.$e['name'].'_'.$c.'" '.$error.'>'.stripslashes($i->$o).'</label></div>';
					}
					
				}
				else
				{
					$tmp .= '<div class="radiobox"><label for="'.$e['name'].'_'.$c.'" '.$error.'><input type="radio" '.$class.' name="'.$e['name'].'" id="'.$e['name'].'_'.$c.'" value="'.$i->$v.'" '.$checked.' /> '.stripslashes($i->$o).'</label> </div>';
				}
				$c++;
			}
		}
		
		if ($inline && !isset($e['bootstrap'])) 
		{
			$tmp = '<div class="inliner clearfix">'.$tmp.'</div>';
		}
		
{'fraction_non_alphanumeric': 0.26028203936153727, 'fraction_numerical': 0.0018596001859600185, 'mean_word_length': 2.7623600746268657, 'pattern_counts': {'":': 0, '<': 168, '<?xml version=': 0, '>': 236, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '797834', 'n_tokens_mistral': 13829, 'n_tokens_neox': 11420, 'n_words': 2493}