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

/administrator/components/com_jforms/models/form.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 813 lines | 493 code | 149 blank | 171 comment | 77 complexity | 150e400b6f5eced94bacaef41bf3c289 MD5 | raw file
  1. <?php
  2. /**
  3. * Model for JForms Component (Backend)
  4. *
  5. * @version $Id: form.php 170 2009-08-12 07:29:38Z dr_drsh $
  6. * @package Joomla
  7. * @subpackage JForms
  8. * @copyright Copyright (C) 2008 Mostafa Muhammad. All rights reserved.
  9. * @license GNU/GPL
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined('_JEXEC') or die();
  13. //Clean up is left to the bottom teir , I.E plugins , not the best design choice but helps avoid confusion
  14. jimport('joomla.application.component.model');
  15. /**
  16. * JFroms "Form" Model
  17. *
  18. * @package Joomla
  19. * @subpackage JForms
  20. */
  21. class BackendModelForm extends JModel
  22. {
  23. /**
  24. * Constructor
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * searches through forms
  35. *
  36. * @access public
  37. * @param int $start starting records
  38. * @param int $count number of records to return
  39. * @param string $keyword keywords used for the LIKE clause
  40. @param bool $onlyPublished whether or not to return only the published forms
  41. * @return array 'total' => total number the request returned
  42. 'forms' => list of forms
  43. */
  44. function searchForms( $start=0, $count=0, $keyword='', $onlyPublished=false ){
  45. $db =& JFactory::getDBO();
  46. $start = intval( $start );
  47. $count = intval( $count );
  48. $onlyPublished = (bool) $onlyPublished;
  49. $keyword = $db->getEscaped( $keyword, true );
  50. $limit = '';
  51. if( $count != 0 ){
  52. $limit = "LIMIT $start, $rpp ";
  53. }
  54. $whereFragments = array();
  55. if( $keyword != ''){
  56. $whereFragments[] = "f.name LIKE '%$keyword%'";
  57. }
  58. if( $onlyPublished ){
  59. $jnow =& JFactory::getDate();
  60. $now = $jnow->toMySQL();
  61. $nullDate = $db->getNullDate();
  62. $whereFragments[] = ' ( f.state = 1 OR f.state = -1)' .
  63. ' AND ( f.publish_up = '.$db->Quote($nullDate).' OR f.publish_up <= '.$db->Quote($now).' )' .
  64. ' AND ( f.publish_down = '.$db->Quote($nullDate).' OR f.publish_down >= '.$db->Quote($now).' )';
  65. }
  66. $where = '';
  67. if( count($whereFragments)){
  68. $where = 'WHERE'.implode( ' AND ', $whereFragments );
  69. }
  70. $sql =
  71. "SELECT f.* FROM #__jforms_forms as f"
  72. ."\n".$where;
  73. $db->setQuery($sql);
  74. $count = $db->loadResult();
  75. $sql =
  76. "SELECT f.*,p.parameter_value as table_name, v.name as editor, u.name as author"
  77. ."\nFROM #__jforms_forms as f "
  78. ."\nLEFT JOIN #__users AS u on f.created_by = u.id "
  79. ."\nLEFT JOIN #__users AS v ON f.checked_out= v.id "
  80. ."\nLEFT JOIN #__jforms_parameters as p on (p.fid = f.id AND `plugin_name` = 'Database' AND `parameter_name`='tableName')"
  81. ."\n".$where
  82. ."\n"."ORDER BY id ASC"
  83. ."\n".$limit;
  84. $db->setQuery( $sql );
  85. return array('total' => $count,
  86. 'forms' => $db->loadObjectList() );
  87. }
  88. /**
  89. * Searches through records
  90. *
  91. * @access public
  92. * @param int $fid Form id
  93. * @param array $fields fields to load
  94. * @param int $start starting row
  95. * @param int $count number of records to load
  96. @param string $keyword Keywords used for the LIKE clause
  97. * @return string formated as follows "X;Y" where X=Total Record count for this request and Y=JSON Encoded records, null on failure
  98. */
  99. function searchRecords( $fid, $fields=null, $start=-1, $count=-1, $criteria=null,$translationMode='html', $rawData=false ){
  100. global $JFormGlobals;
  101. require_once (JPATH_COMPONENT.DS.'helper'.DS.'JSON.php');
  102. //Load storage and element plugins
  103. $pManager =& $JFormGlobals['JFormsPlugin'];
  104. //For data retrieval
  105. $pManager->loadStoragePlugins();
  106. //For data translation
  107. $pManager->loadElementPlugins();
  108. //Load form data from the DB
  109. $form = $this->getForm( $fid );
  110. if( $form == null ){
  111. if( $rawData )return null;
  112. else return '0;';
  113. }
  114. //Send work to the Database plugin
  115. $response = $pManager->invokeMethod('searchRecords', JFORM_PLUGIN_STORAGE, array('Database'),
  116. array( $form, $fields, $start, $count, $criteria ) );
  117. //Use response coming from Database plugin
  118. $result = $response['Database'];
  119. if( $result['total'] == 0 ){
  120. if( $rawData )return null;
  121. else return '0;';
  122. }
  123. //We need to pass the data received from the DB to element Data translator to make them human-readable
  124. // The data received from the plugin is a non-indexed plain array where fields are arranged in the same order of $_GET['fields']
  125. // I use this fact to identify the type of each array element and pass it to its translation handler
  126. //That's exactly what the next loop does, Needs clean up?, totally agreed!
  127. //Index this form fields by hash value (Field name)
  128. $indexedFields = indexByHash ( $form->fields );
  129. $fieldsLoaded = $result['loaded_fields'];
  130. //Match each field with its element
  131. for( $i=0; $i<count($result['records']); $i++){
  132. for( $j=0; $j<count($fieldsLoaded); $j++ ){
  133. $f = $fieldsLoaded[$j];
  134. if( array_key_exists($f, $indexedFields) ){
  135. $type = $indexedFields[$f]->type;
  136. $result['records'][$i][$j] = $pManager->invokeMethod('translate', JFORM_PLUGIN_ELEMENT, array($type),
  137. array( $indexedFields[$f], $result['records'][$i][$j], $translationMode ) );
  138. }
  139. }
  140. }
  141. if( $rawData ){
  142. $result['form'] = $form;
  143. return $result;
  144. } else{
  145. $json = new Services_JSON();
  146. return $result['total'].';'.$json->encode( $result['records']);
  147. }
  148. }
  149. /**
  150. * Returns a selected group of records based on IDs
  151. *
  152. * @access public
  153. * @param int $fid Form id
  154. * @param array $ids ids of the records to be returned
  155. * @return object form object that has "records" property loaded with requested records,null on failure
  156. */
  157. function getRecords( $fid, $ids=null ){
  158. global $JFormGlobals;
  159. $pManager =& $JFormGlobals['JFormsPlugin'];
  160. $pManager->loadStoragePlugins();
  161. $form = $this->getForm( $fid );
  162. if( $form == null ){
  163. return null;
  164. }
  165. $response = $pManager->invokeMethod('getRecords', JFORM_PLUGIN_STORAGE, array('Database'),
  166. array( $form, $ids ) );
  167. //Use response coming from Database plugin
  168. $form->records = $response['Database'];
  169. return $form;
  170. }
  171. /**
  172. * Returns all data about one form based on ID
  173. *
  174. * @access public
  175. * @param int $fid Form id
  176. * @return object form object loaded with all data from the 3 core tables, null on failure
  177. */
  178. function getForm( $fid )
  179. {
  180. $db = & JFactory::getDBO();
  181. $form = & JTable::getInstance('Forms','Table');
  182. $form->load($fid);
  183. if( $form == null ){
  184. return null;
  185. }
  186. //Load Related Fields
  187. $db->setQuery('SELECT * FROM #__jforms_fields WHERE pid=' . $fid.' ORDER BY position ASC');
  188. $form->fields = $db->loadObjectList();
  189. if( $form->fields == null ){
  190. return null;
  191. }
  192. //Load normal Paramters
  193. $db->setQuery('SELECT * FROM #__jforms_parameters WHERE fid=' . $fid);
  194. $nParameters = $db->loadObjectList();
  195. //Load Translated paramters
  196. $db->setQuery('SELECT * FROM #__jforms_tparameters WHERE fid=' . $fid);
  197. $tParameters = $db->loadObjectList();
  198. if( $nParameters == null && $tParameters == null ){
  199. return null;
  200. }
  201. $parameters = array_merge( $tParameters, $nParameters );
  202. $parametersIdList = array();
  203. $form->storagePluginParameters = array();
  204. //Join each field or plugin with its parameters
  205. foreach( $parameters as $p ){
  206. switch( $p->plugin_type ){
  207. case JFORM_PLUGIN_STORAGE :
  208. $form->storagePluginParameters[$p->plugin_name][$p->parameter_name] = $p->parameter_value;
  209. break;
  210. case JFORM_PLUGIN_ELEMENT :
  211. //Loop through fields
  212. for($i=0; $i<count($form->fields); $i++){
  213. //Does this parameter belong to this field?
  214. if( $p->pid == $form->fields[$i]->id ){
  215. //First cycle for this field?
  216. if(!isset($form->fields[$i]->parameters) || !is_array($form->fields[$i]->parameters)){
  217. //Make sure we have storage for these parameters
  218. $form->fields[$i]->parameters = array();
  219. $form->fields[$i]->parametersId = array();
  220. }
  221. //Assign this parameter to the field
  222. $form->fields[$i]->parameters [$p->parameter_name] = $p->parameter_value;
  223. $form->fields[$i]->parametersId[$p->parameter_name] = $p->id;
  224. //$form->fields[$i]->parameters[$p->parameter_name] = new stdClass();
  225. //$form->fields[$i]->parameters[$p->parameter_name]->id = $p->id;
  226. //$form->fields[$i]->parameters[$p->parameter_name]->value = $p->parameter_value;
  227. break;
  228. }
  229. }
  230. break;
  231. }
  232. }
  233. return $form;
  234. }
  235. /**
  236. * "Checks" in the form when the user clicks "cancel"
  237. *
  238. * @access public
  239. * @param int $fid Form id
  240. * @return void
  241. */
  242. function close( $fid )
  243. {
  244. if( $fid ){
  245. $row = & JTable::getInstance('Forms','Table');
  246. $row->load( $fid );
  247. $row->checkin();
  248. }
  249. }
  250. /**
  251. * stores form meta information in core tables,this will trigger "onFormSave" event for the selected storage plugins
  252. *
  253. * @access public
  254. * @param object $data Form Data to be saved
  255. * @return form ID on success , 0 on failure
  256. */
  257. function save( $data )
  258. {
  259. global $JFormGlobals;
  260. // TODO : Make save process Atomic
  261. $user = & JFactory::getUser();
  262. $db = & JFactory::getDBO();
  263. $row = & JTable::getInstance('Forms','Table');
  264. $nullDate = $db->getNullDate();
  265. $pManager =& $JFormGlobals['JFormsPlugin'];
  266. if (!$row->bind($data)) {
  267. JError::raiseError( 500, $db->stderr() );
  268. return 0;
  269. }
  270. $row->id = intval($row->id);
  271. $newEntry = $row->id?false:true;
  272. //Copied directly from com_content saveContent()
  273. // Are we saving from an item edit?
  274. if (!$newEntry) {
  275. $datenow =& JFactory::getDate();
  276. $row->modified = $datenow->toMySQL();
  277. $row->modified_by = $user->get('id');
  278. }
  279. $row->created_by = $row->created_by ? $row->created_by : $user->get('id');
  280. if ($row->created && strlen(trim( $row->created )) <= 10) {
  281. $row->created .= ' 00:00:00';
  282. }
  283. $config =& JFactory::getConfig();
  284. $tzoffset = $config->getValue('config.offset');
  285. $date =& JFactory::getDate($row->created, $tzoffset);
  286. $row->created = $date->toMySQL();
  287. // Append time if not added to publish date
  288. if (strlen(trim($row->publish_up)) <= 10) {
  289. $row->publish_up .= ' 00:00:00';
  290. }
  291. $date =& JFactory::getDate($row->publish_up, $tzoffset);
  292. $row->publish_up = $date->toMySQL();
  293. // Handle never unpublish date
  294. if (trim($row->publish_down) == JText::_('Never') || trim( $row->publish_down ) == '')
  295. {
  296. $row->publish_down = $nullDate;
  297. }
  298. else
  299. {
  300. if (strlen(trim( $row->publish_down )) <= 10) {
  301. $row->publish_down .= ' 00:00:00';
  302. }
  303. $date =& JFactory::getDate($row->publish_down, $tzoffset);
  304. $row->publish_down = $date->toMySQL();
  305. }
  306. $row->groups = implode( ',', $data['groups']);
  307. // Make sure the data is valid
  308. if (!$row->check()) {
  309. JError::raiseError( 500, $db->stderr() );
  310. return 0;
  311. }
  312. // Store the content to the database
  313. if (!$row->store()) {
  314. JError::raiseError( 500, $db->stderr() );
  315. return 0;
  316. }
  317. // Check the form
  318. $row->checkin();
  319. //End of faithful copy
  320. if($newEntry){
  321. $row->id = $this->_lastInsertId();
  322. }
  323. if(!$newEntry){
  324. //Delete fields of this form
  325. $db->setQuery('DELETE FROM #__jforms_fields WHERE pid=' . $row->id);
  326. if (!$db->query()){
  327. JError::raiseError( 500, $db->getErrorMsg() );
  328. return 0;
  329. }
  330. //Delete parameters for this form
  331. $db->setQuery('DELETE FROM #__jforms_parameters WHERE fid=' . $row->id);
  332. if (!$db->query()){
  333. JError::raiseError( 500, $db->getErrorMsg() );
  334. return 0;
  335. }
  336. //Delete parameters for this form
  337. $db->setQuery('DELETE FROM #__jforms_tparameters WHERE fid=' . $row->id);
  338. if (!$db->query()){
  339. JError::raiseError( 500, $db->getErrorMsg() );
  340. return 0;
  341. }
  342. }
  343. //TODO :Should be placed in controller , not here
  344. if( $newEntry ){
  345. $data['id'] = $row->id;
  346. $pManager->invokeMethod('onFormCreate', JFORM_PLUGIN_STORAGE, null,
  347. array( &$data, $pManager->element_plugins ) );
  348. } else {
  349. $pManager->invokeMethod('onFormSave', JFORM_PLUGIN_STORAGE, null,
  350. array( &$data, $pManager->element_plugins ) );
  351. }
  352. //Start saving Fields and their paramters
  353. //Start with saving Storage Plugin parameters
  354. //TODO : Multilingual support
  355. foreach( $data['storagePluginParameters'] as $plugin_name => $plugin_object ){
  356. foreach( $plugin_object as $param_name => $param_value ){
  357. $parameterRow = & JTable::getInstance('Parameters','Table');
  358. $parameterRow->fid = $row->id;
  359. //Storage Plugins are not realted to a certain field
  360. $parameterRow->pid = 0;
  361. $parameterRow->plugin_name = $plugin_name;
  362. $parameterRow->plugin_type = JFORM_PLUGIN_STORAGE;
  363. $parameterRow->parameter_name = $param_name;
  364. $parameterRow->parameter_value = $param_value;
  365. // Make sure the data is valid
  366. if (!$parameterRow->check()) {
  367. JError::raiseError( 500, $db->stderr() );
  368. return 0;
  369. }
  370. // Store the content to the database
  371. if (!$parameterRow->store()) {
  372. JError::raiseError( 500, $db->stderr() );
  373. return 0;
  374. }
  375. }
  376. }
  377. //Now Saving Fields and the their paramters
  378. foreach( $data['fieldInformation'] as $f ){
  379. $fieldsRow = & JTable::getInstance('Fields','Table');
  380. $fieldsRow->pid = $row->id;
  381. $fieldsRow->type = $f->type;
  382. $fieldsRow->position = $f->position;
  383. // Make sure the data is valid
  384. if (!$fieldsRow->check()) {
  385. JError::raiseError( 500, $db->stderr() );
  386. return 0;
  387. }
  388. // Store the field to the database
  389. if (!$fieldsRow->store()) {
  390. JError::raiseError( 500, $db->stderr() );
  391. return 0;
  392. }
  393. $pid = $this->_lastInsertId();
  394. $plugin_name = $f->type;
  395. $plugin_type = JFORM_PLUGIN_ELEMENT;
  396. //PHP 5 call
  397. //To avoid affecting the $form reference passed from controller we create a shallow copy of the current field
  398. //More info , here http://acko.net/node/54
  399. $tempField = clone($f);
  400. unset($tempField->position);
  401. unset($tempField->type);
  402. $parameters = JArrayHelper::fromObject($tempField);
  403. $idList = null;
  404. if( $data['paramListId'] ){
  405. $idList = $data['paramListId'][$parameters['hash']];
  406. }
  407. //Now store the field's parameters
  408. foreach($parameters as $name => $value){
  409. $parameterRow = null;
  410. //If the parameters is translatable , put it in the translated table
  411. if( $pManager->element_plugins[$plugin_name]->parameters[$name]->translate ) {
  412. $parameterRow = & JTable::getInstance('Tparameters','Table');
  413. } else {
  414. $parameterRow = & JTable::getInstance('Parameters','Table');
  415. }
  416. if($idList){
  417. $parameterRow->id = $idList[$name] ;
  418. }
  419. $parameterRow->fid = $row->id;
  420. $parameterRow->pid = $pid;
  421. $parameterRow->plugin_name = $plugin_name;
  422. $parameterRow->plugin_type = $plugin_type;
  423. $parameterRow->parameter_name = $name;
  424. $parameterRow->parameter_value = $value;
  425. // Make sure the data is valid
  426. if (!$parameterRow->check()) {
  427. JError::raiseError( 500, $db->stderr() );
  428. return 0;
  429. }
  430. // Store the parameter to the database
  431. if (!$parameterRow->store()) {
  432. JError::raiseError( 500, $db->stderr() );
  433. return 0;
  434. }
  435. }
  436. }
  437. return $row->id;
  438. }
  439. /**
  440. * Deletes a form.
  441. *
  442. * @access public
  443. * @param array$ids list of form IDs to delete
  444. * @return bool true on success , false on failure
  445. */
  446. function delete( $ids )
  447. {
  448. $db = & JFactory::getDBO();
  449. JArrayHelper::toInteger( $ids );
  450. $additionalParameters = array();
  451. if( !is_array( $ids ) || !count( $ids ) ){
  452. return false;
  453. }
  454. $idText = implode( ',', $ids );
  455. //Delete From the main Forms table
  456. $db->setQuery('DELETE FROM #__jforms_forms WHERE id IN (' . $idText . ')');
  457. if (!$db->query()){
  458. JError::raiseError( 500, $db->getErrorMsg() );
  459. return false;
  460. }
  461. //Delete From the Fields table
  462. $db->setQuery('DELETE FROM #__jforms_fields WHERE pid IN (' . $idText . ')');
  463. if (!$db->query()){
  464. JError::raiseError( 500, $db->getErrorMsg() );
  465. return false;
  466. }
  467. //Delete parameters for this form
  468. $db->setQuery('DELETE FROM #__jforms_parameters WHERE fid IN (' . $idText . ')');
  469. if (!$db->query()){
  470. JError::raiseError( 500, $db->getErrorMsg() );
  471. return false;
  472. }
  473. return true;
  474. }
  475. /**
  476. * Unpublishes the forms whose IDs are passed
  477. *
  478. * @access public
  479. * @param array$ids list of form IDs to unpublish
  480. * @return bool true on success , false on failure
  481. */
  482. function unpublish($ids){
  483. JArrayHelper::toInteger( $ids );
  484. if( !is_array( $ids ) || !count( $ids ) ){
  485. return false;
  486. }
  487. $idText = implode( ',', $ids );
  488. $db = & JFactory::getDBO();
  489. //Delete From the main Forms table
  490. $db->setQuery('UPDATE #__jforms_forms SET state=0 WHERE id IN (' . $idText . ') ');
  491. if (!$db->query()){
  492. JError::raiseError( 500, $db->getErrorMsg() );
  493. return false;
  494. }
  495. return true;
  496. }
  497. /**
  498. * Publishes the forms whose IDs are passed
  499. *
  500. * @access public
  501. * @param array$ids list of form IDs to publish
  502. * @return bool true on success , false on failure
  503. */
  504. function publish($ids){
  505. JArrayHelper::toInteger( $ids );
  506. if( !is_array( $ids ) || !count( $ids ) ){
  507. return false;
  508. }
  509. $idText = implode( ',', $ids );
  510. $db = & JFactory::getDBO();
  511. //Delete From the main Forms table
  512. $db->setQuery('UPDATE #__jforms_forms SET state=1 WHERE id IN (' . $idText . ') ');
  513. if (!$db->query()){
  514. JError::raiseError( 500, $db->getErrorMsg() );
  515. return false;
  516. }
  517. return true;
  518. }
  519. /**
  520. * Deletes records from a form table
  521. *
  522. * @access public
  523. * @param int $fid Form id
  524. * @param array $rids ids of records to delete
  525. * @return bool , true on success, false on failure
  526. */
  527. function deleteRecords( $fid, $rids ){
  528. global $JFormGlobals;
  529. $pManager =& $JFormGlobals['JFormsPlugin'];
  530. $pManager->loadStoragePlugins();
  531. $db = & JFactory::getDBO();
  532. $form = $this->getForm( $fid );
  533. if( $form == null ){
  534. JError::raiseError( 500, 'Form not found' );
  535. return false;
  536. }
  537. $response = $pManager->invokeMethod('deleteRecords', JFORM_PLUGIN_STORAGE, null,
  538. array( $form, $rids ) );
  539. return $response['Database'];
  540. }
  541. function _getNextInsertID( $tableName ){
  542. $db =& JFactory::getDBO();
  543. $db->setQuery('SHOW CREATE TABLE `'.$tableName.'`');
  544. $result = $db->loadRow();
  545. $nextAutoIndex = 1;
  546. $matches = array();
  547. preg_match('/AUTO_INCREMENT=(\d+)/', $result[1], $matches );
  548. if( count( $matches ) ){
  549. $nextAutoIndex = intval( $matches[1] );
  550. }
  551. return $nextAutoIndex;
  552. }
  553. function _setNextAutoIndex( $tableName, $id ){
  554. $db =& JFactory::getDBO();
  555. $db->setQuery('ALTER TABLE `'.$tableName.'` auto_increment='.intval($id));
  556. $db->query();
  557. }
  558. /**
  559. * gets lastInsertId
  560. *
  561. * @access private
  562. * @return int mySQL last insert ID
  563. */
  564. function _lastInsertId(){
  565. $db = & JFactory::getDBO();
  566. $db->setQuery( "SELECT LAST_INSERT_ID()" );
  567. return intval( $db->loadResult() );
  568. }
  569. function copy( $cids, $newName ){
  570. $id = intval($cids[0]);
  571. $newName = str_replace( array("\"","'",",",";"),"",$newName);
  572. $db = & JFactory::getDBO();
  573. $query =
  574. "INSERT INTO #__jforms_forms"
  575. ."\n(`title`, `type`, `plugins`, `state`, `created`, `created_by`, `modified`,"
  576. ."\n`modified_by`, `checked_out`, `checked_out_time`, `publish_up`, `publish_down`, `groups`, `hits`, `thank`)"
  577. ."\n(SELECT"
  578. ."\n'$newName',`type`, `plugins`, `state`, `created`, `created_by`, `modified`,"
  579. ."\n`modified_by`, `checked_out`, `checked_out_time`, `publish_up`, `publish_down`, `groups`, `hits`, `thank`"
  580. ."\nFROM #__jforms_forms WHERE id=$id)";
  581. $db->setQuery($query);
  582. $db->query();
  583. $newFormId = $this->_lastInsertId();
  584. $oldFormId = $id;
  585. //Form parameters
  586. $query =
  587. "INSERT INTO #__jforms_parameters"
  588. ."\n(`fid`, `pid`, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value`)"
  589. ."\n(SELECT"
  590. ."\n$newFormId, 0, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value` FROM #__jforms_parameters WHERE pid=0 AND fid=$oldFormId)";
  591. $db->setQuery($query);
  592. $db->query();
  593. $query =
  594. "INSERT INTO #__jforms_tparameters"
  595. ."\n(`fid`, `pid`, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value`)"
  596. ."\n(SELECT"
  597. ."\n$newFormId, 0, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value` FROM #__jforms_parameters WHERE pid=0 AND fid=$oldFormId)";
  598. $db->setQuery($query);
  599. $db->query();
  600. //Field Parameters
  601. $query = "SELECT `id` FROM #__jforms_fields WHERE pid=$oldFormId";
  602. $db->setQuery($query);
  603. $result = $db->loadResultArray(0);
  604. foreach( $result as $oldFieldId ){
  605. $query =
  606. "INSERT INTO #__jforms_fields"
  607. ."\n(`pid`, `type`, `position`)"
  608. ."\n(SELECT"
  609. ."\n$newFormId, `type`, `position` FROM #__jforms_fields WHERE id=$oldFieldId)";
  610. $db->setQuery($query);
  611. $db->query();
  612. $newFieldId = $this->_lastInsertId();
  613. $query =
  614. "INSERT INTO #__jforms_parameters"
  615. ."\n(`fid`, `pid`, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value`)"
  616. ."\n(SELECT"
  617. ."\n$newFormId, $newFieldId, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value` FROM #__jforms_parameters WHERE pid=$oldFieldId)";
  618. $db->setQuery($query);
  619. $db->query();
  620. $query =
  621. "INSERT INTO #__jforms_tparameters"
  622. ."\n(`fid`, `pid`, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value`)"
  623. ."\n(SELECT"
  624. ."\n$newFormId, $newFieldId, `plugin_name`, `plugin_type`, `parameter_name`, `parameter_value`"
  625. ."\nFROM #__jforms_tparameters WHERE pid=$oldFieldId)";
  626. $db->setQuery($query);
  627. $db->query();
  628. }
  629. $query = "SELECT parameter_value FROM #__jforms_parameters WHERE parameter_name='tableName' AND fid=$oldFormId";
  630. $db->setQuery($query);
  631. $oldTableName = $db->loadResult();
  632. $newTableName = substr( md5(uniqid(rand(), true)), 0, 5 );
  633. $query = "CREATE TABLE #__jforms_$newTableName LIKE #__jforms_$oldTableName";
  634. $db->setQuery($query);
  635. $db->query();
  636. $query = "UPDATE #__jforms_parameters SET parameter_value='$newTableName' WHERE parameter_name='tableName' AND fid=$newFormId";
  637. $db->setQuery($query);
  638. $db->query();
  639. return true;
  640. }
  641. }