getPath( 'class', 'com_chronocontact' ) );
// the class name must be the same as the file name without the .php at the end
class cf_profile
{
//the next four fields must be defined for every plugin
var $result_TITLE = "Profile page";
var $result_TOOLTIP = "Load data from any 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 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 $mainframe;
require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'plugin.php');
$helper = new ChronoContactHelperPlugin();
// identify and initialise the parameters used in this plugin
$params_array = array(
'table_name' => '',
'field_name' => '',
'parameter' => '',
'editable' => '0',
'evaluate' => '0' );
$params = $helper->loadParams($row, $params_array);
$tables = $db->getTableList();
$script .= "
function loadfields2()
{
var table = $('table_name').value;
var test = $('field_name').getParent();
if (table == '' || table == '--?--' ) {
test.setHTML('
Please select table name first
')
return false;
}
var url = 'index3.php?option=com_chronocontact&task=ajax&plugin=cf_profile&method=ajaxFields&format=raw';
myAjax = new Ajax(url, {
method: 'post',
postBody: 'table='+table,
onRequest: function() {
test.setHTML('Loading . . .
')
},
onSuccess: function(req) {
test = $('field_name').getParent();
test.setHTML('
');
test = $('field_name').getParent();
test.setHTML(req);
}
}).request();
}
";
?>
addStyleDeclaration($style);
if ( $script ) $doc->addScriptDeclaration($script);
}
/**
* The function executed when the form is loaded
* Returns an amended $html_string
*
*/
function onload( $option, $pluginrow, $params, $html_string )
{
global $mainframe;
$my =& JFactory::getUser();
$database =& JFactory::getDBO();
//$parid = JRequest::getVar($params->parameter, '', 'request', 'int', 0 );
if ( $params->get('evaluate') ) {
ob_start();
eval( "?>".$html_string );
$html_string = ob_get_clean();
}
$parid = JRequest::getVar($params->get('parameter'));
if ( $parid ) {
$record_id = $parid;
} else if ( $params->get('default_param_value') ) {
$record_id = $params->get('default_param_value');
} else {
$record_id = $my->id;
if ( $record_id == 0 ) {
//$record_id = '##guest##';
}
}
if ( !$record_id ) {
$result = $database->getTableFields( $params->get('table_name', '#__users') );
$table_fields = array_keys($result[$params->get('table_name', '#__users')]);
foreach ( $table_fields as $table_field ) {
$html_string = str_replace("{".$table_field."}", '', $html_string);
}
} else {
$query = "SELECT * FROM `".$params->get('table_name')."` WHERE `".$params->get('field_name')."` = '$record_id' ORDER BY `".$params->get('field_name')."` ".$params->get('ordertype', 'ASC').";";
//$mainframe->enqueuemessage($query);
$database->setQuery($query);
$row = $database->loadObject();
if($row){
$tables = array( $params->get('table_name') );
$result = $database->getTableFields( $tables );
$table_fields = array_keys($result[$params->get('table_name')]);
foreach ( $table_fields as $table_field ) {
$html_string = str_replace("{".$table_field."}", $row->$table_field, $html_string);
}
}else{
$tables = array( $params->get('table_name') );
$result = $database->getTableFields( $tables );
$table_fields = array_keys($result[$params->get('table_name')]);
foreach ( $table_fields as $table_field ) {
$html_string = str_replace("{".$table_field."}", '', $html_string);
}
}
}
if ( $params->get('editable') ) {
$query = "SELECT * FROM `".$params->get('table_name')."` WHERE `".$params->get('field_name')."` = '$record_id' ORDER BY `".$params->get('field_name')."` ".$params->get('ordertype', 'ASC').";";
$database->setQuery($query);
$datarow = $database->loadAssoc();
$formname = JRequest::getVar( 'chronoformname');
if ( !$formname ) {
$params =& $mainframe->getPageParameters('com_chronocontact');
$formname = $params->get('formname');
}
$MyForm =& CFChronoForm::getInstance($formname);
$MyForm->posted = $datarow;
$skippedarray = explode(",", $params->get('skippedarray'));
//get all fields names
preg_match_all('/name=("|\')([^(>|"|\')]*?)("|\')/i', $html_string, $fieldsnamesmatches);
$allfieldsnames = array();
foreach ( $fieldsnamesmatches[2] as $fieldsnamesmatche ) {
if ( strpos($fieldsnamesmatche, '[]') ) {
$fieldsnamesmatche = str_replace('[]', '', $fieldsnamesmatche);
}
$allfieldsnames[] = trim($fieldsnamesmatche);
}
$allfieldsnames = array_unique($allfieldsnames);
//print_r($allfieldsnames );
foreach($allfieldsnames as $allfieldsname){
if(!isset($MyForm->posted[$allfieldsname])){
$MyForm->posted[$allfieldsname] = '';
}else{
$MyForm->posted[$allfieldsname] = htmlentities($MyForm->posted[$allfieldsname], ENT_QUOTES, 'UTF-8');
}
}
//end fields names
if ( count($datarow) ) {
//text fields
$pattern_input = '/ ]*?)type=("|\')(text|password)("|\')([^>]*?)>/is';
$matches = array();
preg_match_all($pattern_input, $html_string, $matches);
foreach ( $matches[0] as $match ) {
$pattern_value = '/value=("|\')(.*?)("|\')/i';
$pattern_name = '/name=("|\')(.*?)("|\')/i';
preg_match($pattern_name, $match, $matches_name);
if(!in_array($matches_name[2], $skippedarray)){
$valuematch = preg_replace($pattern_value, '', $match);
$namematch = preg_replace($pattern_name, 'name="${2}" value="posted[\'${2}\']; ?>"', $valuematch);
$html_string = str_replace($match, $namematch, $html_string);
}
}
//hidden fields
$pattern_input = '/ ]*?)type=("|\')hidden("|\')([^>]*?)>/is';
$matches = array();
preg_match_all($pattern_input, $html_string, $matches);
foreach ($matches[0] as $match) {
$pattern_value = '/value=("|\')(.*?)("|\')/i';
$pattern_name = '/name=("|\')(.*?)("|\')/i';
preg_match($pattern_name, $match, $matches_name);
if(!in_array($matches_name[2], $skippedarray)){
$valuematch = preg_replace($pattern_value, '', $match);
$namematch = preg_replace($pattern_name, 'name="${2}" value="posted[\'${2}\']; ?>"', $valuematch);
$html_string = str_replace($match, $namematch, $html_string);
}
}
//checkboxes or radios fields
$pattern_input = '/ ]*?)type=("|\')(checkbox|radio)("|\')([^>]*?)>/is';
$matches = array();
preg_match_all($pattern_input, $html_string, $matches);
foreach ($matches[0] as $match) {
$pattern_value = '/value=("|\')(.*?)("|\')/i';
$pattern_name = '/name=("|\')(.*?)("|\')/i';
preg_match($pattern_name, $match, $matches_name);
preg_match($pattern_value, $match, $matches_value);
if ( !in_array(str_replace('[]', '', $matches_name[2]), $skippedarray) ) {
//multi values
if ( strpos($matches_name[2], '[]') ) {
$namematch = preg_replace($this->skipregex($pattern_name), 'name="${2}" posted["'.str_replace('[]', '', $matches_name[2]).'"])))echo \'checked="checked"\'; ?>', $match);
//single values
} else {
$namematch = preg_replace($pattern_name, 'name="${2}" posted["'.$matches_name[2].'"] == "'.$matches_value[2].'")echo \'checked="checked"\'; ?>', $match);
}
$html_string = str_replace($match, $namematch, $html_string);
}
}
//textarea fields
$pattern_textarea = '/