<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $mainframe;
require_once( $mainframe->getPath( 'class' ) );
// the class name must be the same as the file name without the .php at the end
class cf_profile  {
	//the next 3 fields must be defined for every plugin
	var $result_TITLE = "Profile Page";
	var $result_TOOLTIP = "Load data from some table to be shown on the form page using a very simple method! all you need to do is to put the field name between { and } , then it will be replaced by the same field value from the choosed table";
	var $plugin_name = "cf_profile"; // must be the same as the class name
	var $event = "ONLOAD"; // must be defined and in Uppercase, should be ONSUBMIT or ONLOAD
	// the next function must exist and will have the backend config code
	function show_conf($row, $id, $form_id, $option){
	global $mosConfig_live_site, $database;
	if(!$row->params){
			$row->params = "table_name=
			field_name=
			parameter=";
		}
	$paramsvalues = mosParseParams( $row->params );
	$tables = $database->getTableList();
	?>
	<?php echo "<script  type=\"text/javascript\" "
    . "src=\"$mosConfig_live_site/includes/js/overlib_mini.js\"></script>"; ?>
	<form action="index2.php" method="post" name="adminForm" id="adminForm" class="adminForm">
	<table border="0" cellpadding="3" cellspacing="0">
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "The table which will be used to get the data from" ); ?></td>
			<td><strong><?php echo "Table name"; ?>:</strong> </td>
			<td></td>
			<td>
			<select name="params[table_name]">
			<?php foreach($tables as $table){ ?>
			<option <?php if($paramsvalues->table_name == $table) echo "selected"; ?> value="<?php echo $table; ?>"><?php echo $table; ?></option>
			<?php } ?>
			</select>
			</td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "This is the name of the parameter coming in the page request url, like userid=128, so when you add here userid, the plugin will check for this parameter and loads results based on this value" ); ?></td>
			<td><strong><?php echo "REQUEST Parameter name"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[parameter]" value="<?php echo $paramsvalues->parameter; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "This is the name of table field which will be used at the SELECT statement, for best results, this field must be UNIQUE" ); ?></td>
			<td><strong><?php echo "Target field name"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[field_name]" value="<?php echo $paramsvalues->field_name; ?>" /></td>
		</tr>
		</tr>
	</table>
	<input type="hidden" name="id" value="<?php echo $id; ?>" />
	<input type="hidden" name="form_id" value="<?php echo $form_id; ?>" />
	<input type="hidden" name="name" value="<?php echo $this->plugin_name; ?>" />
	<input type="hidden" name="event" value="<?php echo $this->event; ?>" />
	<input type="hidden" name="option" value="<?php echo $option; ?>" />
	<input type="hidden" name="task" value="save_conf" />
	
	</form>
	<?php
	}
	// this function must exist and may not be changed unless you need to customize something
	function save_conf( $option ) {
		global $database;		
		
		$row = new mosChronoContact_Plugins( $database );
		if (!$row->bind( $_POST )) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		$params = mosGetParam( $_POST, 'params', '' );
		if (is_array( $params )) {
			$txt = array();
			foreach ( $params as $k=>$v) {
				$txt[] = "$k=$v";
			}
			$row->params = implode( "\n", $txt );
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		mosRedirect( "index2.php?option=".$option, "Config Saved" );
	}
	
	function onload( $option, $params, $html_string ) {
		global $database, $acl, $my;
		global $mosConfig_sitename, $mosConfig_live_site, $mosConfig_useractivation, $mosConfig_allowUserRegistration;
		global $mosConfig_mailfrom, $mosConfig_fromname, $mosConfig_mailfrom, $mosConfig_fromname;
	
		$parid 	= mosGetParam( $_REQUEST, $params->parameter, 0 );
		if($parid){
			$record_id = $parid;
		}else{
			$record_id = $my->id;
		}
		
		if($record_id){
			$database->setQuery( "SELECT * FROM ".$params->table_name." WHERE ".$params->field_name." = '".$record_id."'" );
			$rows = $database->loadObjectList();
			$row = $rows[0];
			$tables = array( $params->table_name );
			$result = $database->getTableFields( $tables );
			$table_fields = array_keys($result[$params->table_name]);
			foreach($table_fields as $table_field){
				$html_string = str_replace("{".$table_field."}", $row->$table_field, $html_string);
			}
		}
		
		return $html_string ;
		
	}

}
?>