PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/site/components/com_chronocontact/plugins/cf_profile.php

https://bitbucket.org/manchas/jrobotz
PHP | 424 lines | 354 code | 28 blank | 42 comment | 38 complexity | 4b3c8722acfe24eb403a3790ea79c4b1 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /*
  3. /**
  4. * CHRONOFORMS version 3.0
  5. * Copyright (c) 2008 Chrono_Man, ChronoEngine.com. All rights reserved.
  6. * Author: Chrono_Man (ChronoEngine.com)
  7. You are not allowed to copy or use or rebrand or sell any code at this page under your own name or any other identity!
  8. Unlike the Chronoforms extension license, This file is NOT released under the GNU/GPL license and it can be distributed with the Chronoforms package distributed by ChronoEngine.com ONLY according to a written permission from "this" file's owner, if you want to include this file in any packages then you MUST get a written permission from the owner, contact webmaster@chronoengine.com for more details!
  9. * See readme.html.
  10. * Visit http://www.ChronoEngine.com for regular update and information.
  11. **/
  12. defined('_JEXEC') or die('Restricted access');
  13. global $mainframe;
  14. require_once( $mainframe->getPath( 'class', 'com_chronocontact' ) );
  15. // the class name must be the same as the file name without the .php at the end
  16. class cf_profile
  17. {
  18. //the next four fields must be defined for every plugin
  19. var $result_TITLE = "Profile page";
  20. 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";
  21. var $plugin_name = "cf_profile"; // must be the same as the class name
  22. var $event = "ONLOAD"; // must be defined and in Uppercase, should be ONSUBMIT or ONLOAD
  23. // the next function must exist and will have the backend config code
  24. function show_conf($row, $id, $form_id, $option)
  25. {
  26. global $mainframe;
  27. require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'plugin.php');
  28. $helper = new ChronoContactHelperPlugin();
  29. // identify and initialise the parameters used in this plugin
  30. $params_array = array(
  31. 'table_name' => '',
  32. 'field_name' => '',
  33. 'parameter' => '',
  34. 'editable' => '0',
  35. 'evaluate' => '0' );
  36. $params = $helper->loadParams($row, $params_array);
  37. $tables = $db->getTableList();
  38. $script .= "
  39. function loadfields2()
  40. {
  41. var table = $('table_name').value;
  42. var test = $('field_name').getParent();
  43. if (table == '' || table == '--?--' ) {
  44. test.setHTML('<div id=\'field_name\'>Please select table name first</div>')
  45. return false;
  46. }
  47. var url = 'index3.php?option=com_chronocontact&task=ajax&plugin=cf_profile&method=ajaxFields&format=raw';
  48. myAjax = new Ajax(url, {
  49. method: 'post',
  50. postBody: 'table='+table,
  51. onRequest: function() {
  52. test.setHTML('<div id=\'field_name\'>Loading . . .</div>')
  53. },
  54. onSuccess: function(req) {
  55. test = $('field_name').getParent();
  56. test.setHTML('<div id=\'field_name\'></div>');
  57. test = $('field_name').getParent();
  58. test.setHTML(req);
  59. }
  60. }).request();
  61. }
  62. ";
  63. ?>
  64. <form action="index2.php" method="post" name="adminForm" id="adminForm"
  65. class="adminForm" >
  66. <?php
  67. echo $pane->startPane("cf_profile");
  68. echo $pane->startPanel( 'Configure', 'Configure' );
  69. ?>
  70. <?php
  71. //$input = $helper->createHeaderTD('Configure Profile Table plugin', '', true, $attribs['header']);
  72. echo $helper->openTableLegend('Configure Profile Table plugin');
  73. $tooltip = "Choose the table to get the data from.";
  74. foreach ( $tables as $k => $table ) {
  75. $tables_array[$table] = JHTML::_('select.option', JText::_($table));
  76. }
  77. $input = $helper->createSelectTD('Table name',
  78. 'params[table_name]', $tables_array, $params->get('table_name'),
  79. array('onChange' => 'loadfields2()', 'class' => 'cf_select'), $tooltip, 'table_name' );
  80. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  81. $tooltip = "The table column name which to be matched by the parameter;
  82. for best results, this field must be UNIQUE";
  83. if ( $id == 0 ) {
  84. $input = $helper->createTextTD('Target field name',
  85. '<div id="field_name">Select Table name first</div>', '', $tooltip);
  86. } else {
  87. $input = "<td></td>";
  88. $fields_array = array();
  89. $table_fields = $db->getTableFields( $params->get('table_name') );
  90. foreach ( $table_fields[$params->get('table_name')] as $k => $v) {
  91. $fields_array[$k] = JHTML::_('select.option', JText::_($k));
  92. }
  93. $input = $helper->createSelectTD('Target field name',
  94. 'params[field_name]', $fields_array,
  95. $params->get('field_name'), array('class' => 'cf_select'), $tooltip, 'field_name' );
  96. }
  97. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  98. $tooltip = "The name of the parameter provided in the page url e.g. userid=128.";
  99. $input = $helper->createInputTD("'Request' parameter name", 'params[parameter]',
  100. $params->get('parameter'), '', $attribs['input'], $tooltip);
  101. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  102. $tooltip = "This will be the default value of the requested parameter in case nothing was in the request!";
  103. $input = $helper->createInputTD("Default Request Parameter value", 'params[default_param_value]',
  104. $params->get('default_param_value'), '', $attribs['input'], $tooltip);
  105. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  106. $tooltip = "The type of ordering for the records loaded, this will control if the first or last record is loaded in case the query meets more than 1 record";
  107. $input = $helper->createSelectTD("Oder type", 'params[ordertype]', array("ASC" => JHTML::_('select.option', JText::_("ASC")), "DESC" => JHTML::_('select.option', JText::_("DESC"))),
  108. $params->get('ordertype', 'ASC'), array('class' => 'cf_select'), $tooltip, 'ordertype');
  109. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  110. $tooltip = "Will this profile page be editable by users?";
  111. $input = $helper->createYesNoTD("Editable", "params[editable]", '',
  112. $params->get('editable', '0'), '', $tooltip);
  113. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  114. $tooltip = "Skip populating these fields";
  115. $input = $helper->createInputTD("Skipped fields list", 'params[skippedarray]',
  116. $params->get('skippedarray'), '', $attribs['input'], $tooltip);
  117. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  118. $tooltip = "Will the profile page evaluate code before doing its routine?
  119. This may need to be enabled if you are generating some fields using PHP and want to load their data!?";
  120. $input = $helper->createYesNoTD("Evaluate code", "params[evaluate]", '',
  121. $params->get('evaluate', '0'), '', $tooltip);
  122. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  123. echo $helper->closeTableLegend();
  124. ?>
  125. <?php
  126. $hidden_array = array (
  127. 'id' => $id,
  128. 'form_id' => $form_id,
  129. 'name' => $this->plugin_name,
  130. 'event' => $this->event,
  131. 'option' => $option,
  132. 'task' => 'save_conf');
  133. echo $helper->createHiddenArray( $hidden_array );
  134. echo $pane->endPanel();
  135. echo $pane->startPanel( 'Help', "help" );
  136. ?>
  137. <table border="0" cellpadding="3" cellspacing="0" class='cf_table' >
  138. <?php
  139. $input = $helper->createHeaderTD('Help for Profile Table plugin', '', true, $attribs['header']);
  140. echo $helper->wrapTR($input);
  141. ?>
  142. <tr>
  143. <td colspan='4' style='border:1px solid silver; padding:6px;'>
  144. <div>The plugin allows you to read values from any table in the database
  145. and include them in your form.</div>
  146. <div>It was originally designed to allow access to the jos_users table to create member
  147. profiles but it is capable of much more.</div>
  148. <div>To use the plugin effectively you will need to call the form from a link on your site.
  149. This could be from - for example a list of users, or topics, or events where you have
  150. some related information in a database table.</div>
  151. <ul><li>Choose the table you want to use in the first drop-down
  152. e.g. jos_users to get a user's name and email.</li>
  153. <li>Select a field or column name from the table in the second drop down.
  154. This should be a field that will uniquely identify the record you want to use
  155. e.g. 'id' or 'username' for the jos_user table. NB This drop-down will not appear
  156. until you select a table in the first drop-down.</li>
  157. <li>In the Target field name box put the name of the field you will use to identify the
  158. record e.g. user_id. You will need to add this field to a url calling the form
  159. e.g. . . . &chronoformname=my_form&user_id=99 </li>
  160. <li>You can then use information from this record in your form by putting {column_name} where
  161. you want it to appear e.g. {name} for a users name from the jos_users table.</li>
  162. <li>Once this plugin is configured you must enable it in the Form 'Plugins'' tab.</li></ul>
  163. </td>
  164. </tr>
  165. </table>
  166. <?php
  167. echo $pane->endPanel();
  168. echo $pane->endPane();
  169. ?>
  170. </form>
  171. <?php
  172. if ( $style ) $doc->addStyleDeclaration($style);
  173. if ( $script ) $doc->addScriptDeclaration($script);
  174. }
  175. /**
  176. * The function executed when the form is loaded
  177. * Returns an amended $html_string
  178. *
  179. */
  180. function onload( $option, $pluginrow, $params, $html_string )
  181. {
  182. global $mainframe;
  183. $my =& JFactory::getUser();
  184. $database =& JFactory::getDBO();
  185. //$parid = JRequest::getVar($params->parameter, '', 'request', 'int', 0 );
  186. if ( $params->get('evaluate') ) {
  187. ob_start();
  188. eval( "?>".$html_string );
  189. $html_string = ob_get_clean();
  190. }
  191. $parid = JRequest::getVar($params->get('parameter'));
  192. if ( $parid ) {
  193. $record_id = $parid;
  194. } else if ( $params->get('default_param_value') ) {
  195. $record_id = $params->get('default_param_value');
  196. } else {
  197. $record_id = $my->id;
  198. if ( $record_id == 0 ) {
  199. //$record_id = '##guest##';
  200. }
  201. }
  202. if ( !$record_id ) {
  203. $result = $database->getTableFields( $params->get('table_name', '#__users') );
  204. $table_fields = array_keys($result[$params->get('table_name', '#__users')]);
  205. foreach ( $table_fields as $table_field ) {
  206. $html_string = str_replace("{".$table_field."}", '', $html_string);
  207. }
  208. } else {
  209. $query = "SELECT * FROM `".$params->get('table_name')."` WHERE `".$params->get('field_name')."` = '$record_id' ORDER BY `".$params->get('field_name')."` ".$params->get('ordertype', 'ASC').";";
  210. //$mainframe->enqueuemessage($query);
  211. $database->setQuery($query);
  212. $row = $database->loadObject();
  213. if($row){
  214. $tables = array( $params->get('table_name') );
  215. $result = $database->getTableFields( $tables );
  216. $table_fields = array_keys($result[$params->get('table_name')]);
  217. foreach ( $table_fields as $table_field ) {
  218. $html_string = str_replace("{".$table_field."}", $row->$table_field, $html_string);
  219. }
  220. }else{
  221. $tables = array( $params->get('table_name') );
  222. $result = $database->getTableFields( $tables );
  223. $table_fields = array_keys($result[$params->get('table_name')]);
  224. foreach ( $table_fields as $table_field ) {
  225. $html_string = str_replace("{".$table_field."}", '', $html_string);
  226. }
  227. }
  228. }
  229. if ( $params->get('editable') ) {
  230. $query = "SELECT * FROM `".$params->get('table_name')."` WHERE `".$params->get('field_name')."` = '$record_id' ORDER BY `".$params->get('field_name')."` ".$params->get('ordertype', 'ASC').";";
  231. $database->setQuery($query);
  232. $datarow = $database->loadAssoc();
  233. $formname = JRequest::getVar( 'chronoformname');
  234. if ( !$formname ) {
  235. $params =& $mainframe->getPageParameters('com_chronocontact');
  236. $formname = $params->get('formname');
  237. }
  238. $MyForm =& CFChronoForm::getInstance($formname);
  239. $MyForm->posted = $datarow;
  240. $skippedarray = explode(",", $params->get('skippedarray'));
  241. //get all fields names
  242. preg_match_all('/name=("|\')([^(>|"|\')]*?)("|\')/i', $html_string, $fieldsnamesmatches);
  243. $allfieldsnames = array();
  244. foreach ( $fieldsnamesmatches[2] as $fieldsnamesmatche ) {
  245. if ( strpos($fieldsnamesmatche, '[]') ) {
  246. $fieldsnamesmatche = str_replace('[]', '', $fieldsnamesmatche);
  247. }
  248. $allfieldsnames[] = trim($fieldsnamesmatche);
  249. }
  250. $allfieldsnames = array_unique($allfieldsnames);
  251. //print_r($allfieldsnames );
  252. foreach($allfieldsnames as $allfieldsname){
  253. if(!isset($MyForm->posted[$allfieldsname])){
  254. $MyForm->posted[$allfieldsname] = '';
  255. }else{
  256. $MyForm->posted[$allfieldsname] = htmlentities($MyForm->posted[$allfieldsname], ENT_QUOTES, 'UTF-8');
  257. }
  258. }
  259. //end fields names
  260. if ( count($datarow) ) {
  261. //text fields
  262. $pattern_input = '/<input([^>]*?)type=("|\')(text|password)("|\')([^>]*?)>/is';
  263. $matches = array();
  264. preg_match_all($pattern_input, $html_string, $matches);
  265. foreach ( $matches[0] as $match ) {
  266. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  267. $pattern_name = '/name=("|\')(.*?)("|\')/i';
  268. preg_match($pattern_name, $match, $matches_name);
  269. if(!in_array($matches_name[2], $skippedarray)){
  270. $valuematch = preg_replace($pattern_value, '', $match);
  271. $namematch = preg_replace($pattern_name, 'name="${2}" value="<?php echo $MyForm->posted[\'${2}\']; ?>"', $valuematch);
  272. $html_string = str_replace($match, $namematch, $html_string);
  273. }
  274. }
  275. //hidden fields
  276. $pattern_input = '/<input([^>]*?)type=("|\')hidden("|\')([^>]*?)>/is';
  277. $matches = array();
  278. preg_match_all($pattern_input, $html_string, $matches);
  279. foreach ($matches[0] as $match) {
  280. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  281. $pattern_name = '/name=("|\')(.*?)("|\')/i';
  282. preg_match($pattern_name, $match, $matches_name);
  283. if(!in_array($matches_name[2], $skippedarray)){
  284. $valuematch = preg_replace($pattern_value, '', $match);
  285. $namematch = preg_replace($pattern_name, 'name="${2}" value="<?php echo $MyForm->posted[\'${2}\']; ?>"', $valuematch);
  286. $html_string = str_replace($match, $namematch, $html_string);
  287. }
  288. }
  289. //checkboxes or radios fields
  290. $pattern_input = '/<input([^>]*?)type=("|\')(checkbox|radio)("|\')([^>]*?)>/is';
  291. $matches = array();
  292. preg_match_all($pattern_input, $html_string, $matches);
  293. foreach ($matches[0] as $match) {
  294. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  295. $pattern_name = '/name=("|\')(.*?)("|\')/i';
  296. preg_match($pattern_name, $match, $matches_name);
  297. preg_match($pattern_value, $match, $matches_value);
  298. if ( !in_array(str_replace('[]', '', $matches_name[2]), $skippedarray) ) {
  299. //multi values
  300. if ( strpos($matches_name[2], '[]') ) {
  301. $namematch = preg_replace($this->skipregex($pattern_name), 'name="${2}" <?php if(in_array("'.$matches_value[2].'", explode(", ", $MyForm->posted["'.str_replace('[]', '', $matches_name[2]).'"])))echo \'checked="checked"\'; ?>', $match);
  302. //single values
  303. } else {
  304. $namematch = preg_replace($pattern_name, 'name="${2}" <?php if($MyForm->posted["'.$matches_name[2].'"] == "'.$matches_value[2].'")echo \'checked="checked"\'; ?>', $match);
  305. }
  306. $html_string = str_replace($match, $namematch, $html_string);
  307. }
  308. }
  309. //textarea fields
  310. $pattern_textarea = '/<textarea([^>]*?)>(.*?)<\/textarea>/is';
  311. $matches = array();
  312. preg_match_all($pattern_textarea, $html_string, $matches);
  313. $namematch = '';
  314. foreach ( $matches[0] as $match ) {
  315. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  316. $pattern_name = '/name=("|\')(.*?)("|\')/i';
  317. preg_match($pattern_name, $match, $matches_name);
  318. if(!in_array($matches_name[2], $skippedarray)){
  319. $pattern_textarea2 = '/(<textarea(.*?)>)(.*?)(<\/textarea>)/is';
  320. $newtextarea_match = preg_replace($pattern_textarea2, '${1}<?php echo $MyForm->posted[\''.$matches_name[2].'\']; ?>${4}', $match);
  321. $html_string = str_replace($match, $newtextarea_match, $html_string);
  322. }
  323. }
  324. //select boxes
  325. $pattern_select = '/<select(.*?)select>/is';
  326. $matches = array();
  327. preg_match_all($pattern_select, $html_string, $matches);
  328. foreach ($matches[0] as $match) {
  329. $selectmatch = $match;
  330. $pattern_select2 = '/<select([^>]*?)>/is';
  331. preg_match_all($pattern_select2, $match, $matches2);
  332. $options = preg_replace(array('/'.$this->skipregex($matches2[0][0]).'/is', '/<\/select>/i'), array('', ''), $match);
  333. $pattern_name = '/name=("|\')(.*?)("|\')/i';
  334. preg_match($pattern_name, $matches2[0][0], $matches_name);
  335. if(!in_array(str_replace('[]', '', $matches_name[2]), $skippedarray)){
  336. //multi select
  337. if(strpos($matches_name[2], '[]')){
  338. $pattern_options = '/<option(.*?)<\/option>/is';
  339. preg_match_all($pattern_options, $options, $matches_options);
  340. foreach($matches_options[0] as $matches_option){
  341. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  342. preg_match($pattern_value, $matches_option, $matches_value);
  343. $optionmatch = preg_replace('/<option/i', '<option <?php if(in_array("'.$matches_value[2].'", explode(", ", $MyForm->posted["'.str_replace('[]', '', $matches_name[2]).'"])))echo \'selected="selected"\'; ?>', $matches_option);
  344. $selectmatch = str_replace($matches_option, $optionmatch, $selectmatch);
  345. }
  346. //single select
  347. } else {
  348. $pattern_options = '/<option(.*?)<\/option>/is';
  349. preg_match_all($pattern_options, $options, $matches_options);
  350. foreach($matches_options[0] as $matches_option){
  351. $pattern_value = '/value=("|\')(.*?)("|\')/i';
  352. preg_match($pattern_value, $matches_option, $matches_value);
  353. $optionmatch = preg_replace('/<option/i', '<option <?php if($MyForm->posted["'.$matches_name[2].'"] == "'.$matches_value[2].'")echo \'selected="selected"\'; ?>', $matches_option);
  354. $selectmatch = str_replace($matches_option, $optionmatch, $selectmatch);
  355. }
  356. }
  357. $html_string = str_replace($match, $selectmatch, $html_string);
  358. }
  359. }
  360. }
  361. }
  362. return $html_string ;
  363. }
  364. /////
  365. /**
  366. * Returns a select list of the fields in a table specified in a request param
  367. *
  368. *
  369. */
  370. function ajaxFields()
  371. {
  372. $db =& JFactory::getDBO();
  373. $tablename = JRequest::getVar('table');
  374. $fields_array = array();
  375. $table_fields = $db->getTableFields( $tablename );
  376. foreach ( $table_fields[$tablename] as $k => $v) {
  377. $fields_array[$k] = JHTML::_('select.option', $k);
  378. }
  379. echo JHTML::_('select.genericlist', $fields_array,
  380. 'params[field_name]', 'class="cf_select"', 'value', 'text',
  381. '', 'field_name' );
  382. }
  383. // this function must exist and may not be changed unless you need to customize something
  384. function save_conf( $option )
  385. {
  386. require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'plugin.php');
  387. $helper = new ChronoContactHelperPlugin();
  388. $helper->save_conf($option);
  389. }
  390. function skipregex($regex){
  391. $reserved = array('[', ']');
  392. $replace = array('\[', '\]');
  393. return str_replace($reserved, $replace, $regex);
  394. }
  395. }
  396. ?>