PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/admin/formbuilder.php

https://bitbucket.org/raremethod/rare-method-cms
PHP | 837 lines | 693 code | 111 blank | 33 comment | 53 complexity | edd9f264cead1d00519129f0820bb4b2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Formbuilder Controller
  4. *
  5. * @link http://www.raremethod.com
  6. */
  7. class Formbuilder extends CI_Controller
  8. {
  9. function __construct()
  10. {
  11. parent::__construct();
  12. $this->load->database();
  13. $this->load->library(array('tank_auth'));
  14. $this->load->helper(array('url','admin/admin', 'content/content'));
  15. $this->load->helper('form');
  16. ## Get the user's ID and add it to the config array
  17. $config = array('userID'=>$this->tank_auth->get_user_id());
  18. ## Load the ACL library and pass it the config array
  19. $this->load->library('acl',$config);
  20. if(!$this->acl->hasPermission('admin_access'))
  21. {
  22. $this->session->set_flashdata('prev_url', $this->uri->uri_string());
  23. redirect('auth/login');
  24. }
  25. ## If the user does not have permission either in 'acl_user_perms' or 'acl_role_perms' redirect to login, or restricted, etc
  26. if(!$this->acl->hasPermission('templates'))
  27. {
  28. $this->session->set_flashdata('error_message', 'Sorry, you do not have access to Templates.');
  29. redirect($this->config->item('admin_url').'/dashboard');
  30. }
  31. }
  32. function preload()
  33. {
  34. $template_id = (int)$this->uri->segment(4);
  35. $fields_query = $this->db->query("SELECT * FROM rm_template_fields WHERE field_template_id = $template_id ORDER BY field_order ASC");
  36. if($fields_query->num_rows()>0)
  37. {
  38. foreach($fields_query->result() as $f)
  39. {
  40. $data = array(
  41. 'type' => $f->field_type,
  42. 'id' => 'element_'.$f->field_id,
  43. 'label' => $f->field_label,
  44. 'value' => $f->field_default_value,
  45. 'field_name' => $f->field_name,
  46. 'field_operations'=> $f->field_operations,
  47. 'required' => $f->field_required,
  48. 'field_options' => $f->field_options,
  49. 'description' => $f->field_description,
  50. 'template_id' => $template_id
  51. );
  52. $this->element($data);
  53. }
  54. }
  55. }
  56. function preload_settings()
  57. {
  58. $fields_query = $this->db->query("SELECT * FROM rm_settings ORDER BY field_order ASC");
  59. if($fields_query->num_rows()>0)
  60. {
  61. foreach($fields_query->result() as $f)
  62. {
  63. $data = array(
  64. 'type' => $f->field_type,
  65. 'id' => 'element_'.$f->setting_id,
  66. 'label' => $f->setting_label,
  67. 'value' => $f->field_default_value,
  68. 'field_name' => $f->setting_name,
  69. 'field_operations'=> $f->field_operations,
  70. 'required' => $f->field_required,
  71. 'field_options' => $f->field_options,
  72. 'description' => $f->field_description
  73. );
  74. $this->element($data);
  75. }
  76. }
  77. }
  78. // Used to generate an HTML preview of the form in a popup
  79. function preview()
  80. {
  81. $data['items'] = $this->build($_POST);
  82. if (!empty($data['items']))
  83. {
  84. $this->load->view('admin/formbuilder_preview', $data);
  85. }
  86. else
  87. {
  88. echo '<div class="warning">No elements in form!</div>';
  89. }
  90. }
  91. // Build is used to output the forms data as an HTML form
  92. // $data is an array generated on post from the builder.
  93. // By default, this is only ever used for the preview window
  94. function build($data)
  95. {
  96. if (!isset($data['properties'])) return false;
  97. $elements = $data['properties'];
  98. foreach ($elements as $k => $val)
  99. {
  100. if (!isset($data[$k])) $data[$k] = NULL;
  101. if (!isset($val['values'])) $val['values'] = NULL;
  102. $elements[$k]['content'] = $data[$k];
  103. $name = $k;
  104. switch ($val['type'])
  105. {
  106. case 'text': $elements[$k]['html'] = $data[$k]; break;
  107. case 'textarea':
  108. $elements[$k]['html'] = form_textarea(array(
  109. 'name' => $name,
  110. 'rows' => 5,
  111. 'cols' => 50,
  112. 'value' => $data[$k],
  113. 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
  114. ));
  115. break;
  116. case 'textbox':
  117. $elements[$k]['html'] = form_input(array(
  118. 'name' =>$name,
  119. 'value' =>$data[$k],
  120. 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
  121. ));
  122. break;
  123. case 'dropdown':
  124. if (!$val['values']) { unset($elements[$k]); break; }
  125. $options = explode(';',$val['values']);
  126. if (empty($options)) { unset($elements[$k]); break; }
  127. $elements[$k]['html'] = form_dropdown($name,$options);
  128. break;
  129. case 'checkbox':
  130. $input = null;
  131. if (!$val['values']) { unset($elements[$k]); break; }
  132. $options = explode(';',$val['values']);
  133. if (empty($options)) { unset($elements[$k]); break; }
  134. foreach ($options as $option) {
  135. $input .= form_checkbox($name.'[]', $option).' '.$option.'<br/>';
  136. }
  137. $elements[$k]['html'] = $input;
  138. break;
  139. case 'radio':
  140. $input = null;
  141. if (!$val['values']) { unset($elements[$k]); break; }
  142. $options = explode(';',$val['values']);
  143. if (empty($options)) { unset($elements[$k]); break; }
  144. foreach ($options as $option) {
  145. $input .= form_radio($name.'[]', $option).' '.$option.'<br/>';
  146. }
  147. $elements[$k]['html'] = $input;
  148. break;
  149. case 'datetime':
  150. $elements[$k]['html'] = form_input(array(
  151. 'name'=>$name,
  152. 'value'=>$data[$k],
  153. 'class' => 'datepicker '.((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
  154. ));
  155. break;
  156. case 'fileupload':
  157. $elements[$k]['html'] = form_upload(array(
  158. 'name'=>$name,
  159. 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
  160. ));
  161. break;
  162. case 'button':
  163. $elements[$k]['html'] = form_input(array(
  164. 'name'=>$name,
  165. 'value'=>((isset($val['value']))?$val['value']:'Button'),
  166. 'type'=>'button'
  167. ));
  168. break;
  169. }
  170. }
  171. return $elements;
  172. }
  173. /*
  174. Element is generated and spat onscreen
  175. */
  176. function element_settings($data = NULL)
  177. {
  178. if(is_array($data))
  179. {
  180. foreach($data as $var=>$column_value)
  181. {
  182. $$var = $column_value;
  183. }
  184. }
  185. if(empty($type))
  186. {
  187. $type = $this->uri->segment(4);
  188. }
  189. ## IF there is no ID, they are generating a new field, so insert a placeholder field_id until they customize the field
  190. if(empty($id))
  191. {
  192. $field_query = $this->db->query("SELECT MAX(setting_id) AS max_field_value FROM rm_settings");
  193. $field_query = $field_query->row();
  194. $field_id = $field_query->max_field_value+1;
  195. $sort_query = $this->db->query("SELECT MAX(field_order) AS max_sort FROM rm_settings");
  196. $sort_query = $sort_query->row();
  197. $sort_order = $sort_query->max_sort+1;
  198. $this->db->query("INSERT INTO rm_settings
  199. (
  200. setting_id,
  201. field_order,
  202. setting_label,
  203. setting_name,
  204. setting_value,
  205. field_options,
  206. field_type,
  207. field_operations
  208. )
  209. VALUES
  210. (
  211. $field_id,
  212. $sort_order,
  213. '',
  214. '',
  215. '',
  216. '',
  217. 'textbox',
  218. ''
  219. )
  220. ");
  221. $id = 'element_'.$field_id;
  222. }
  223. if(empty($label))
  224. {
  225. $label = 'No Label';
  226. }
  227. if(empty($value))
  228. {
  229. $value = '';
  230. }
  231. switch($type)
  232. {
  233. case 'text':
  234. $element = form_textarea(array(
  235. 'class' => 'wysiwyg',
  236. 'id' => $id,
  237. 'name' => $id,
  238. 'rows' => 5,
  239. 'cols' => 50
  240. ));
  241. break;
  242. case 'textarea':
  243. $element = form_textarea(array(
  244. 'name' => $id,
  245. 'rows' => 5,
  246. 'cols' => 50
  247. ));
  248. break;
  249. case 'textbox' : $element = form_input(array('name' => $id, 'value' => $value)); break;
  250. case 'dropdown' :
  251. if(empty($field_options) && empty($field_operations))
  252. {
  253. $element = form_dropdown($id,array(''=>'No Content'));
  254. }
  255. elseif(!empty($field_operations))
  256. {
  257. $element = '<select name="'.$id.'">';
  258. $element .= '<option value="">&mdash;</option>';
  259. $post_type = $field_operations;
  260. $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
  261. foreach($post_results->result() as $p)
  262. {
  263. $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
  264. }
  265. $element .= '</select>';
  266. }
  267. else
  268. {
  269. $field_options_array = explode(';', $field_options);
  270. $element = form_dropdown($id,$field_options_array);
  271. }
  272. break;
  273. case 'dropdown_multi' :
  274. if(empty($field_options) && empty($field_operations))
  275. {
  276. $element = form_multiselect($id,array(''=>'No Content'), '');
  277. }
  278. elseif(!empty($field_operations))
  279. {
  280. $element = '<select name="'.$id.'" multiple="multiple" style="height:150px;">';
  281. $element .= '<option value="">&mdash;</option>';
  282. $post_type = $field_operations;
  283. $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
  284. foreach($post_results->result() as $p)
  285. {
  286. $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
  287. }
  288. $element .= '</select>';
  289. }
  290. else
  291. {
  292. $field_options_array = explode(';', $field_options);
  293. $element = form_multiselect($id,$field_options_array);
  294. }
  295. break;
  296. case 'checkbox' : $element = '<span class="values '.$id.'"><input type="checkbox"></span>'; break;
  297. case 'radio' : $element = '<span class="values '.$id.'"><input type="radio"></span>'; break;
  298. case 'datetime' : $element = form_input(array('name'=>$id,'class'=>'datepicker')); break;
  299. case 'fileupload' : $element = form_upload($id); break;
  300. case 'button' : $element = form_input(array('name'=>$id,'value'=>'No Content','type'=>'button')); break;
  301. default : $element = null; break;
  302. }
  303. // Basic output list element.
  304. $output = "
  305. <li id='loaded".$id."'>
  306. <label for='".$id."'><a href='#' rel='".$type."' class='properties tooltip' title='Edit'>".$label."</a></label>
  307. <div class='block'>
  308. <div class='handle'><span class='icon move'>Move</span></div>
  309. ".$element."
  310. <span class='note ".$id."'>".@$description."</span>
  311. </div>
  312. <div class='clear'></div>
  313. <div class='attrs clear ".$id."'>
  314. <input type='hidden' name='properties[".$id."][type]' value='".$type."'/>
  315. <input type='hidden' name='properties[".$id."][label]' class='label' value='".$label."' />";
  316. if(!empty($description))
  317. {
  318. $output .= "<input type='hidden' name='properties[".$id."][description]' class='description' value='".$description."' />";
  319. }
  320. if(!empty($required))
  321. {
  322. $output .= "<input type='hidden' name='properties[".$id."][required]' class='required' value='".$required."' />
  323. ";
  324. }
  325. if(!empty($field_options))
  326. {
  327. $output .= "<input type='hidden' name='properties[".$id."][values]' class='values' value='".$field_options."' />
  328. ";
  329. }
  330. if(!empty($field_operations))
  331. {
  332. $output .= "<input type='hidden' name='properties[".$id."][field_operations]' class='field_operations' value='".$field_operations."' />
  333. ";
  334. }
  335. $output .= " </div>
  336. </li>
  337. ";
  338. if ($element)
  339. {
  340. // Set output to AJAX
  341. echo $output;
  342. }
  343. else
  344. {
  345. echo 'Could not determine the element!';
  346. }
  347. }
  348. /*
  349. Element is generated and spat onscreen
  350. */
  351. function element($data = NULL)
  352. {
  353. if(is_array($data))
  354. {
  355. foreach($data as $var=>$column_value)
  356. {
  357. $$var = $column_value;
  358. }
  359. }
  360. if(empty($type))
  361. {
  362. $type = $this->uri->segment(4);
  363. }
  364. if(empty($template_id))
  365. {
  366. $template_id = $this->uri->segment(5);
  367. }
  368. ## IF there is no ID, they are generating a new field, so insert a placeholder field_id until they customize the field
  369. if(empty($id))
  370. {
  371. $field_query = $this->db->query("SELECT MAX(field_id) AS max_field_value FROM rm_template_fields");
  372. $field_query = $field_query->row();
  373. $field_id = $field_query->max_field_value+1;
  374. $sort_query = $this->db->query("SELECT MAX(field_order) AS max_sort FROM rm_template_fields WHERE field_template_id = $template_id");
  375. $sort_query = $sort_query->row();
  376. $sort_order = $sort_query->max_sort+1;
  377. $this->db->query("INSERT INTO rm_template_fields
  378. (
  379. field_id,
  380. field_template_id,
  381. field_label,
  382. field_name,
  383. field_options,
  384. field_order,
  385. field_type,
  386. field_validation,
  387. field_operations,
  388. field_description,
  389. field_default_value,
  390. field_required
  391. )
  392. VALUES
  393. (
  394. $field_id,
  395. $template_id,
  396. '',
  397. '',
  398. '',
  399. $sort_order,
  400. 'textbox',
  401. 'text',
  402. '',
  403. '',
  404. '',
  405. 0)
  406. ");
  407. $id = 'element_'.$field_id;
  408. }
  409. if(empty($label))
  410. {
  411. $label = 'No Label';
  412. }
  413. if(empty($value))
  414. {
  415. $value = '';
  416. }
  417. switch($type)
  418. {
  419. case 'text':
  420. $element = form_textarea(array(
  421. 'class' => 'wysiwyg',
  422. 'id' => $id,
  423. 'name' => $id,
  424. 'rows' => 5,
  425. 'cols' => 50
  426. ));
  427. break;
  428. case 'textarea':
  429. $element = form_textarea(array(
  430. 'name' => $id,
  431. 'rows' => 5,
  432. 'cols' => 50
  433. ));
  434. break;
  435. case 'textbox' : $element = form_input(array('name' => $id, 'value' => $value)); break;
  436. case 'dropdown' :
  437. if(empty($field_options) && empty($field_operations))
  438. {
  439. $element = form_dropdown($id,array(''=>'No Content'));
  440. }
  441. elseif(!empty($field_operations))
  442. {
  443. $element = '<select name="'.$id.'">';
  444. $element .= '<option value="">&mdash;</option>';
  445. $post_type = $field_operations;
  446. $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
  447. foreach($post_results->result() as $p)
  448. {
  449. $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
  450. }
  451. $element .= '</select>';
  452. }
  453. else
  454. {
  455. $field_options_array = explode(';', $field_options);
  456. $element = form_dropdown($id,$field_options_array);
  457. }
  458. break;
  459. case 'dropdown_multi' :
  460. if(empty($field_options) && empty($field_operations))
  461. {
  462. $element = form_multiselect($id,array(''=>'No Content'), '');
  463. }
  464. elseif(!empty($field_operations))
  465. {
  466. $element = '<select name="'.$id.'" multiple="multiple" style="height:150px;">';
  467. $element .= '<option value="">&mdash;</option>';
  468. $post_type = $field_operations;
  469. $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
  470. foreach($post_results->result() as $p)
  471. {
  472. $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
  473. }
  474. $element .= '</select>';
  475. }
  476. else
  477. {
  478. $field_options_array = explode(';', $field_options);
  479. $element = form_multiselect($id,$field_options_array);
  480. }
  481. break;
  482. case 'checkbox' : $element = '<span class="values '.$id.'"><input type="checkbox"></span>'; break;
  483. case 'radio' : $element = '<span class="values '.$id.'"><input type="radio"></span>'; break;
  484. case 'datetime' : $element = form_input(array('name'=>$id,'class'=>'datepicker')); break;
  485. case 'fileupload' : $element = form_upload($id); break;
  486. case 'button' : $element = form_input(array('name'=>$id,'value'=>'No Content','type'=>'button')); break;
  487. default : $element = null; break;
  488. }
  489. // Basic output list element.
  490. $output = "
  491. <li id='loaded".$id."'>
  492. <label for='".$id."'><a href='#' rel='".$type."' class='properties tooltip' title='Edit'>".$label."</a></label>
  493. <div class='block'>
  494. <div class='handle'><span class='icon move'>Move</span></div>
  495. ".$element."
  496. <span class='note ".$id."'>".@$description."</span>
  497. </div>
  498. <div class='clear'></div>
  499. <div class='attrs clear ".$id."'>
  500. <input type='hidden' name='properties[".$id."][type]' value='".$type."'/>
  501. <input type='hidden' name='properties[".$id."][label]' class='label' value='".$label."' />";
  502. if(!empty($description))
  503. {
  504. $output .= "<input type='hidden' name='properties[".$id."][description]' class='description' value='".$description."' />";
  505. }
  506. if(!empty($required))
  507. {
  508. $output .= "<input type='hidden' name='properties[".$id."][required]' class='required' value='".$required."' />
  509. ";
  510. }
  511. if(!empty($field_options))
  512. {
  513. $output .= "<input type='hidden' name='properties[".$id."][values]' class='values' value='".$field_options."' />
  514. ";
  515. }
  516. if(!empty($field_operations))
  517. {
  518. $output .= "<input type='hidden' name='properties[".$id."][field_operations]' class='field_operations' value='".$field_operations."' />
  519. ";
  520. }
  521. $output .= " </div>
  522. </li>
  523. ";
  524. if ($element)
  525. {
  526. // Set output to AJAX
  527. echo $output;
  528. }
  529. else
  530. {
  531. echo 'Could not determine the element!';
  532. }
  533. }
  534. /*
  535. Builds a list of properties for the builder to display.
  536. */
  537. function properties()
  538. {
  539. $type = $this->uri->segment(4);
  540. $id = $this->uri->segment(5);
  541. $output = null;
  542. //basic options
  543. $options = array(
  544. 'Delete' => form_input(
  545. array(
  546. 'rel' => $id,
  547. 'name' => 'remove',
  548. 'value' => 'Delete Element',
  549. 'type' => 'button',
  550. 'onclick' => 'formbuilder.remove(this);'
  551. )
  552. ),
  553. 'Label' => form_input(
  554. array(
  555. 'name' => 'label',
  556. 'rel' => 'label[for='.$id.'] a'
  557. )
  558. ),
  559. 'Required' => array(
  560. 'Yes' => form_checkbox('required','1'),
  561. 'Type' => form_dropdown('required_vars', array(
  562. '' => 'Text',
  563. 'email' => 'Email',
  564. 'number' => 'Number'
  565. )
  566. )
  567. ),
  568. 'Description' => form_input(
  569. array(
  570. 'name' => 'description',
  571. 'rel' => '.note[class~='.$id.']'
  572. )
  573. )
  574. );
  575. $options_help = '<br class="clr" /><span class="icon tooltip" title="Seperate multiple values with a semicolon;<br/>Eg: test;something;here">Help</span>';
  576. $dropdown_help = '<br class="clr" /><span class="icon tooltip" title="Can be any type of post. Will pull a list of posts as options.">Help</span>';
  577. //specific options
  578. switch($type)
  579. {
  580. case 'dropdown':
  581. $options['Options'] = form_input(
  582. array(
  583. 'name'=>'values',
  584. 'class'=>'dropdown',
  585. 'rel'=>'select[name='.$id.']'
  586. )
  587. ).$options_help;
  588. $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
  589. $posts_config[''] = '&mdash;';
  590. foreach($posts_query->result() as $r)
  591. {
  592. $val = $r->post_type;
  593. $posts_config[$val] = $val;
  594. }
  595. $options['Reference Posts'] = form_dropdown('field_operations',
  596. $posts_config
  597. ).$dropdown_help;
  598. break;
  599. break;
  600. case 'radio':
  601. $options['Options'] = form_input(array('name'=>'values','class'=>'radio','rel'=>'span.values[class~='.$id.']')).$options_help;
  602. break;
  603. case 'checkbox':
  604. $options['Options'] = form_input(array('name'=>'values','class'=>'checkbox','rel'=>'span.values[class~='.$id.']')).$options_help;
  605. $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
  606. $posts_config[''] = '&mdash;';
  607. foreach($posts_query->result() as $r)
  608. {
  609. $val = $r->post_type;
  610. $posts_config[$val] = $val;
  611. }
  612. $options['Reference Posts'] = form_dropdown('field_operations',
  613. $posts_config
  614. ).$dropdown_help;
  615. break;
  616. case 'button':
  617. $options['Value'] = form_input(array('name'=>'value','class'=>'button','rel'=>'input[name='.$id.']'));
  618. unset($options['Required']); //useless
  619. break;
  620. case 'text':
  621. //unset($options['Label']); //useless
  622. unset($options['Description']); //useless
  623. break;
  624. case 'dropdown_multi' :
  625. $options['Options'] = form_input(
  626. array(
  627. 'name'=>'values',
  628. 'class'=>'dropdown',
  629. 'rel'=>'select[name='.$id.']'
  630. )
  631. ).$options_help;
  632. $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
  633. $posts_config[''] = '&mdash;';
  634. foreach($posts_query->result() as $r)
  635. {
  636. $val = $r->post_type;
  637. $posts_config[$val] = $val;
  638. }
  639. $options['Reference Posts'] = form_dropdown('field_operations',
  640. $posts_config
  641. ).$dropdown_help;
  642. break;
  643. case 'fileupload' :
  644. $this->load->helper('file');
  645. $rm_config = read_file('./site/config/rm_config.php');
  646. preg_match_all('/\$config\[\'(.*)\'\]/', $rm_config, $config_result, PREG_SET_ORDER);
  647. $image_config[''] = '&mdash;';
  648. foreach($config_result as $r)
  649. {
  650. $val = $r[1];
  651. $image_config[$val] = $val;
  652. }
  653. $options['Image Config'] = form_dropdown('field_operations',
  654. $image_config
  655. ).'<br class="clr" /><span class="icon tooltip" title="Set your image options in rm_config.php">Help</span>';
  656. break;
  657. default: break;
  658. }
  659. // Spit out the options for ajax
  660. foreach ($options as $k => $option) {
  661. $output .= '<li class="'.$id.'">';
  662. $output .= '<b>'.$k.'</b>: ';
  663. $output .= '<ul>';
  664. if (is_array($option)) {
  665. foreach ($option as $sk => $sub) {
  666. $output .= '<li class="sub"><b>'.$sk.'</b>: '.$sub.'</li>';
  667. }
  668. } else {
  669. $output .= '<li class="sub">'.$option.'</li>';
  670. }
  671. $output .= '</ul>';
  672. $output .= '</li>';
  673. }
  674. echo $output;
  675. }
  676. function delete_field()
  677. {
  678. $element_id = $_POST['data'];
  679. $element_id = explode('_', $element_id);
  680. $field_id = $element_id[1];
  681. $fields_query = $this->db->query("SELECT * FROM rm_template_fields LEFT JOIN rm_template_fields_data ON rm_template_fields.field_id = rm_template_fields_data.field_id WHERE rm_template_fields.field_id = $field_id LIMIT 1");
  682. if($fields_query->num_rows()>0)
  683. {
  684. $field_info = $fields_query->row();
  685. $field_type = $field_info->field_type;
  686. $field_value = $field_info->field_content;
  687. if($field_type == 'fileupload')
  688. {
  689. $this->load->library('file_functions');
  690. ## Delete the selected file
  691. $this->file_functions->delete_files($field_value);
  692. }
  693. }
  694. $this->db->query("DELETE FROM rm_template_fields WHERE field_id = $field_id LIMIT 1");
  695. $this->db->query("DELETE FROM rm_template_fields_data WHERE field_id = $field_id");
  696. $data = array('success'=>TRUE);
  697. echo json_encode($data);
  698. }
  699. function delete_setting()
  700. {
  701. $element_id = $_POST['data'];
  702. $element_id = explode('_', $element_id);
  703. $field_id = $element_id[1];
  704. $setting_query = $this->db->query("SELECT * FROM rm_settings WHERE setting_id = '$field_id' LIMIT 1");
  705. if($setting_query->num_rows()>0)
  706. {
  707. $setting_info = $setting_query->row();
  708. $field_type = $setting_info->field_type;
  709. $setting_value = $setting_info->setting_value;
  710. if($field_type == 'fileupload')
  711. {
  712. $this->load->library('file_functions');
  713. ## Delete the selected file
  714. $this->file_functions->delete_files($setting_value);
  715. }
  716. $this->db->query("DELETE FROM rm_settings WHERE setting_id = '$field_id' LIMIT 1");
  717. $data = array('success'=>TRUE);
  718. }
  719. else
  720. {
  721. $data = array('success'=>FALSE);
  722. }
  723. echo json_encode($data);
  724. }
  725. }
  726. ?>