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

/system/expressionengine/third_party/freeform/mcp.freeform.php

https://bitbucket.org/studiobreakfast/sync
PHP | 6572 lines | 3700 code | 1362 blank | 1510 comment | 411 complexity | 51f7cd09070ec34e760f2c338908caf7 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Solspace - Freeform
  4. *
  5. * @package Solspace:Freeform
  6. * @author Solspace DevTeam
  7. * @copyright Copyright (c) 2008-2012, Solspace, Inc.
  8. * @link http://solspace.com/docs/addon/c/Freeform/
  9. * @version 4.0.8
  10. * @filesource ./system/expressionengine/third_party/freeform/
  11. */
  12. /**
  13. * Freeform - Control Panel
  14. *
  15. * The Control Panel master class that handles all of the CP Requests and Displaying
  16. *
  17. * @package Solspace:Freeform
  18. * @author Solspace DevTeam
  19. * @filesource ./system/expressionengine/third_party/freeform/mcp.freeform.php
  20. */
  21. if ( ! class_exists('Module_builder_freeform'))
  22. {
  23. require_once 'addon_builder/module_builder.php';
  24. }
  25. class Freeform_mcp extends Module_builder_freeform
  26. {
  27. private $migration_batch_limit = 100;
  28. private $pro_update = FALSE;
  29. // --------------------------------------------------------------------
  30. /**
  31. * Constructor
  32. *
  33. * @access public
  34. * @param bool Enable calling of methods based on URI string
  35. * @return string
  36. */
  37. public function __construct( $switch = TRUE )
  38. {
  39. parent::__construct('freeform');
  40. // Install or Uninstall Request
  41. if ((bool) $switch === FALSE) return;
  42. if ( ! function_exists('lang'))
  43. {
  44. ee()->load->helper('language');
  45. }
  46. // --------------------------------------------
  47. // Module Menu Items
  48. // --------------------------------------------
  49. $menu = array(
  50. 'module_forms' => array(
  51. 'link' => $this->base,
  52. 'title' => lang('forms')
  53. ),
  54. 'module_fields' => array(
  55. 'link' => $this->base . AMP . 'method=fields',
  56. 'title' => lang('fields')
  57. ),
  58. 'module_fieldtypes' => array(
  59. 'link' => $this->base . AMP . 'method=fieldtypes',
  60. 'title' => lang('fieldtypes')
  61. ),
  62. 'module_notifications' => array(
  63. 'link' => $this->base . AMP . 'method=notifications',
  64. 'title' => lang('notifications')
  65. ),
  66. /*'module_export' => array(
  67. 'link' => $this->base . AMP . 'method=export',
  68. 'title' => lang('export')
  69. ),*/
  70. 'module_utilities' => array(
  71. 'link' => $this->base . AMP . 'method=utilities',
  72. 'title' => lang('utilities')
  73. ),
  74. 'module_preferences' => array(
  75. 'link' => $this->base . AMP . 'method=preferences',
  76. 'title' => lang('preferences')
  77. ),
  78. 'module_documentation' => array(
  79. 'link' => FREEFORM_DOCS_URL,
  80. 'title' => lang('help'),
  81. 'new_window' => TRUE
  82. ),
  83. );
  84. $this->cached_vars['lang_module_version'] = lang('freeform_module_version');
  85. $this->cached_vars['module_version'] = FREEFORM_VERSION;
  86. $this->cached_vars['module_menu_highlight'] = 'module_forms';
  87. $this->cached_vars['inner_nav_links'] = array();
  88. // -------------------------------------
  89. // css includes. WOOT!
  90. // -------------------------------------
  91. $this->cached_vars['cp_stylesheet'] = array(
  92. 'chosen',
  93. 'standard_cp'
  94. );
  95. $this->cached_vars['cp_javascript'] = array(
  96. 'standard_cp.min',
  97. 'chosen.jquery.min'
  98. );
  99. // -------------------------------------
  100. // custom CP?
  101. // -------------------------------------
  102. $debug_normal = (ee()->input->get_post('debug_normal') !== FALSE);
  103. $is_crappy_ie_version = FALSE;
  104. $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
  105. if (stristr($ua, 'msie 6') OR
  106. stristr($ua, 'msie 7') OR
  107. stristr($ua, 'msie 8'))
  108. {
  109. $is_crappy_ie_version = TRUE;
  110. }
  111. if ( ! $debug_normal AND
  112. ! $is_crappy_ie_version AND
  113. ! $this->check_no($this->preference('use_solspace_mcp_style')))
  114. {
  115. $this->cached_vars['cp_stylesheet'][] = 'custom_cp';
  116. }
  117. // -------------------------------------
  118. // Module Installed and What Version?
  119. // -------------------------------------
  120. if ($this->database_version() == FALSE)
  121. {
  122. return;
  123. }
  124. else if ($this->version_compare($this->database_version(), '<', FREEFORM_VERSION)
  125. OR ! $this->extensions_enabled())
  126. {
  127. // For EE 2.x, we need to redirect the request to
  128. // Update Routine
  129. $_GET['method'] = 'freeform_module_update';
  130. }
  131. //avoids AR collisions
  132. $this->data->get_module_preferences();
  133. $this->data->get_global_module_preferences();
  134. $this->data->show_all_sites();
  135. // -------------------------------------
  136. // run upgrade or downgrade scripts
  137. // -------------------------------------
  138. if (FREEFORM_PRO AND $this->data->global_preference('ffp') === 'n' OR
  139. ! FREEFORM_PRO AND $this->data->global_preference('ffp') === 'y')
  140. {
  141. $_GET['method'] = 'freeform_module_update';
  142. $this->pro_update = TRUE;
  143. }
  144. $this->cached_vars['module_menu'] = $menu;
  145. }
  146. // END Freeform_cp_base()
  147. //---------------------------------------------------------------------
  148. // begin views
  149. //---------------------------------------------------------------------
  150. // --------------------------------------------------------------------
  151. /**
  152. * Module's Main Homepage
  153. *
  154. * @access public
  155. * @param string
  156. * @return null
  157. */
  158. public function index ($message='')
  159. {
  160. if ($message == '' AND ee()->input->get('msg') !== FALSE)
  161. {
  162. $message = lang(ee()->input->get('msg'));
  163. }
  164. return $this->forms($message);
  165. }
  166. // END index()
  167. // --------------------------------------------------------------------
  168. /**
  169. * My Forms
  170. *
  171. * @access public
  172. * @param string $message incoming message for flash data
  173. * @return string html output
  174. */
  175. public function forms ( $message = '' )
  176. {
  177. // -------------------------------------
  178. // Messages
  179. // -------------------------------------
  180. if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  181. {
  182. $message = lang(ee()->input->get('msg'));
  183. }
  184. $this->cached_vars['message'] = $message;
  185. //--------------------------------------------
  186. // Crumbs and tab highlight
  187. //--------------------------------------------
  188. $new_form_link = $this->mod_link(array(
  189. 'method' => 'edit_form'
  190. ));
  191. $this->cached_vars['new_form_link'] = $new_form_link;
  192. $this->add_crumb( lang('forms') );
  193. $this->freeform_add_right_link(lang('new_form'), $new_form_link);
  194. $this->set_highlight('module_forms');
  195. //--------------------------------------
  196. // start vars
  197. //--------------------------------------
  198. $row_limit = $this->data->defaults['mcp_row_limit'];
  199. $paginate = '';
  200. $row_count = 0;
  201. // -------------------------------------
  202. // pagination?
  203. // -------------------------------------
  204. ee()->load->model('freeform_form_model');
  205. if ( ! $this->data->show_all_sites())
  206. {
  207. ee()->freeform_form_model->where(
  208. 'site_id',
  209. ee()->config->item('site_id')
  210. );
  211. }
  212. $total_results = ee()->freeform_form_model->count(array(), FALSE);
  213. // do we need pagination?
  214. if ( $total_results > $row_limit )
  215. {
  216. $row_count = $this->get_post_or_zero('row');
  217. $url = $this->mod_link(array(
  218. 'method' => 'forms'
  219. ));
  220. //get pagination info
  221. $pagination_data = $this->universal_pagination(array(
  222. 'total_results' => $total_results,
  223. 'limit' => $row_limit,
  224. 'current_page' => $row_count,
  225. 'pagination_config' => array('base_url' => $url),
  226. 'query_string_segment' => 'row'
  227. ));
  228. ee()->freeform_form_model->limit(
  229. $row_limit,
  230. $pagination_data['pagination_page']
  231. );
  232. $paginate = $pagination_data['pagination_links'];
  233. }
  234. ee()->freeform_form_model->order_by('form_label');
  235. $this->cached_vars['paginate'] = $paginate;
  236. // -------------------------------------
  237. // Did they upgrade from FF3?
  238. // -------------------------------------
  239. $this->cached_vars['legacy'] = FALSE;
  240. $this->cached_vars['migrate_link'] = '';
  241. ee()->load->library('freeform_migration');
  242. if ( ee()->freeform_migration->legacy() === TRUE )
  243. {
  244. $this->cached_vars['legacy'] = TRUE;
  245. $this->cached_vars['migrate_link'] = $this->mod_link(array('method' => 'utilities'));
  246. }
  247. // -------------------------------------
  248. // data
  249. // -------------------------------------
  250. $rows = ee()->freeform_form_model->get();
  251. $form_data = array();
  252. if ($rows !== FALSE)
  253. {
  254. // -------------------------------------
  255. // check for composer for each form
  256. // -------------------------------------
  257. $form_ids = array();
  258. $potential_composer_ids = array();
  259. foreach ($rows as $row)
  260. {
  261. $form_ids[] = $row['form_id'];
  262. if ($this->is_positive_intlike($row['composer_id']))
  263. {
  264. $potential_composer_ids[$row['form_id']] = $row['composer_id'];
  265. }
  266. }
  267. $has_composer = array();
  268. if ( ! empty($potential_composer_ids))
  269. {
  270. ee()->load->model('freeform_composer_model');
  271. $composer_ids = ee()->freeform_composer_model
  272. ->key('composer_id', 'composer_id')
  273. ->where('preview !=', 'y')
  274. ->where_in(
  275. 'composer_id',
  276. array_values($potential_composer_ids)
  277. )
  278. ->get();
  279. if ( ! empty($composer_ids))
  280. {
  281. foreach ($potential_composer_ids as $form_id => $composer_id)
  282. {
  283. if (in_array($composer_id, $composer_ids))
  284. {
  285. $has_composer[$form_id] = $composer_id;
  286. }
  287. }
  288. }
  289. }
  290. // -------------------------------------
  291. // suppliment rows
  292. // -------------------------------------
  293. foreach ($rows as $row)
  294. {
  295. $row['submissions_count'] = (
  296. $this->data->get_form_submissions_count($row['form_id'])
  297. );
  298. $row['moderate_count'] = (
  299. $this->data->get_form_needs_moderation_count($row['form_id'])
  300. );
  301. $row['has_composer'] = isset(
  302. $has_composer[$row['form_id']]
  303. );
  304. // -------------------------------------
  305. // piles o' links
  306. // -------------------------------------
  307. $row['form_submissions_link'] = $this->mod_link(array(
  308. 'method' => 'entries',
  309. 'form_id' => $row['form_id']
  310. ));
  311. $row['form_moderate_link'] = $this->mod_link(array(
  312. 'method' => 'moderate_entries',
  313. 'form_id' => $row['form_id'],
  314. 'search_status' => 'pending'
  315. ));
  316. $row['form_edit_composer_link'] = $this->mod_link(array(
  317. 'method' => 'form_composer',
  318. 'form_id' => $row['form_id']
  319. ));
  320. $row['form_settings_link'] = $this->mod_link(array(
  321. 'method' => 'edit_form',
  322. 'form_id' => $row['form_id']
  323. ));
  324. $row['form_duplicate_link'] = $this->mod_link(array(
  325. 'method' => 'edit_form',
  326. 'duplicate_id' => $row['form_id']
  327. ));
  328. $row['form_delete_link'] = $this->mod_link(array(
  329. 'method' => 'delete_confirm_form',
  330. 'form_id' => $row['form_id']
  331. ));
  332. $form_data[] = $row;
  333. }
  334. }
  335. $this->cached_vars['form_data'] = $form_data;
  336. $this->cached_vars['form_url'] = $this->mod_link(array(
  337. 'method' => 'delete_confirm_form'
  338. ));
  339. // ----------------------------------------
  340. // Load vars
  341. // ----------------------------------------
  342. // -------------------------------------
  343. // JS
  344. // -------------------------------------
  345. ee()->cp->add_js_script(
  346. array('plugin' => array('tooltip', 'dataTables'))
  347. );
  348. // --------------------------------------------
  349. // Load page
  350. // --------------------------------------------
  351. $this->cached_vars['current_page'] = $this->view(
  352. 'forms.html',
  353. NULL,
  354. TRUE
  355. );
  356. return $this->ee_cp_view('index.html');
  357. }
  358. //END forms
  359. // --------------------------------------------------------------------
  360. /**
  361. * delete_confirm_form
  362. *
  363. * @access public
  364. * @return string
  365. */
  366. public function delete_confirm_form ()
  367. {
  368. $form_ids = ee()->input->get_post('form_id', TRUE);
  369. if ( ! is_array($form_ids) AND
  370. ! $this->is_positive_intlike($form_ids) )
  371. {
  372. $this->actions()->full_stop(lang('no_form_ids_submitted'));
  373. }
  374. //already checked for numeric :p
  375. if ( ! is_array($form_ids))
  376. {
  377. $form_ids = array($form_ids);
  378. }
  379. return $this->delete_confirm(
  380. 'delete_forms',
  381. array('form_ids' => $form_ids),
  382. 'delete_form_confirmation'
  383. );
  384. }
  385. //END delete_confirm_form
  386. // --------------------------------------------------------------------
  387. /**
  388. * delete_forms
  389. *
  390. * @access public
  391. * @return string
  392. */
  393. public function delete_forms ($form_ids = array())
  394. {
  395. $message = 'delete_form_success';
  396. if ( empty($form_ids) )
  397. {
  398. $form_ids = ee()->input->get_post('form_ids');
  399. }
  400. if ( ! is_array($form_ids) AND
  401. $this->is_positive_intlike($form_ids))
  402. {
  403. $form_ids = array($form_ids);
  404. }
  405. //if everything is all nice and array like, DELORT
  406. //but one last check on each item to make sure its a number
  407. if ( is_array($form_ids))
  408. {
  409. ee()->load->library('freeform_forms');
  410. foreach ($form_ids as $form_id)
  411. {
  412. if ($this->is_positive_intlike($form_id))
  413. {
  414. ee()->freeform_forms->delete_form($form_id);
  415. }
  416. }
  417. }
  418. //the voyage home
  419. ee()->functions->redirect($this->mod_link(array(
  420. 'method' => 'index',
  421. 'msg' => $message
  422. )));
  423. }
  424. //END delete_forms
  425. // --------------------------------------------------------------------
  426. /**
  427. * Edit Form
  428. *
  429. * @access public
  430. * @return string html output
  431. */
  432. public function edit_form ()
  433. {
  434. // -------------------------------------
  435. // form ID? we must be editing
  436. // -------------------------------------
  437. $form_id = $this->get_post_or_zero('form_id');
  438. $update = $this->cached_vars['update'] = ($form_id != 0);
  439. // -------------------------------------
  440. // default data
  441. // -------------------------------------
  442. $inputs = array(
  443. 'form_id' => '0',
  444. 'form_name' => '',
  445. 'form_label' => '',
  446. 'default_status' => $this->data->defaults['default_form_status'],
  447. 'notify_admin' => 'n',
  448. 'notify_user' => 'n',
  449. 'user_email_field' => '',
  450. 'user_notification_id' => '0',
  451. 'admin_notification_id' => '0',
  452. 'admin_notification_email' => ee()->config->item('webmaster_email'),
  453. 'form_description' => '',
  454. 'template_id' => '0',
  455. 'composer_id' => '0',
  456. 'field_ids' => '',
  457. 'field_order' => '',
  458. );
  459. // -------------------------------------
  460. // updating?
  461. // -------------------------------------
  462. if ($update)
  463. {
  464. $form_data = $this->data->get_form_info($form_id);
  465. if ($form_data)
  466. {
  467. foreach ($form_data as $key => $value)
  468. {
  469. if ($key == 'admin_notification_email')
  470. {
  471. $value = str_replace('|', ', ', $value);
  472. }
  473. if ($key == 'field_ids' AND ! empty($value))
  474. {
  475. $value = implode('|', $value);
  476. }
  477. $inputs[$key] = form_prep($value);
  478. }
  479. }
  480. else
  481. {
  482. $this->actions()->full_stop(lang('invalid_form_id'));
  483. }
  484. }
  485. //--------------------------------------------
  486. // Crumbs and tab highlight
  487. //--------------------------------------------
  488. $this->add_crumb(
  489. lang('forms'),
  490. $this->mod_link(array('method' => 'forms'))
  491. );
  492. $this->add_crumb(
  493. $update ?
  494. lang('update_form') . ': ' . $form_data['form_label'] :
  495. lang('new_form')
  496. );
  497. $this->set_highlight('module_forms');
  498. // -------------------------------------
  499. // duplicating?
  500. // -------------------------------------
  501. $duplicate_id = $this->get_post_or_zero('duplicate_id');
  502. $this->cached_vars['duplicate_id'] = $duplicate_id;
  503. $this->cached_vars['duplicated'] = FALSE;
  504. if ( ! $update AND $duplicate_id > 0)
  505. {
  506. $form_data = $this->data->get_form_info($duplicate_id);
  507. if ($form_data)
  508. {
  509. foreach ($form_data as $key => $value)
  510. {
  511. if (in_array($key, array('form_id', 'form_label', 'form_name')))
  512. {
  513. continue;
  514. }
  515. if ($key == 'admin_notification_email')
  516. {
  517. $value = str_replace('|', ', ', $value);
  518. }
  519. $inputs[$key] = $value;
  520. }
  521. $this->cached_vars['duplicated'] = TRUE;
  522. $this->cached_vars['duplicated_from'] = $form_data['form_label'];
  523. }
  524. }
  525. if (isset($form_data['field_ids']) AND
  526. ! empty($form_data['field_ids']) AND
  527. isset($form_data['field_order']) AND
  528. ! empty($form_data['field_order']))
  529. {
  530. $field_ids = $form_data['field_ids'];
  531. if ( ! is_array($field_ids))
  532. {
  533. $field_ids = $this->actions()->pipe_split($field_ids);
  534. }
  535. $field_order = $form_data['field_order'];
  536. if ( ! is_array($field_order))
  537. {
  538. $field_order = $this->actions()->pipe_split($field_order);
  539. }
  540. $missing_ids = array_diff($field_ids, $field_order);
  541. $inputs['field_order'] = implode('|', array_merge($field_order, $missing_ids));
  542. }
  543. // -------------------------------------
  544. // load inputs
  545. // -------------------------------------
  546. foreach ($inputs as $key => $value)
  547. {
  548. $this->cached_vars[$key] = $value;
  549. }
  550. // -------------------------------------
  551. // select boxes
  552. // -------------------------------------
  553. $this->cached_vars['statuses'] = $this->data->get_form_statuses();
  554. ee()->load->model('freeform_field_model');
  555. $available_fields = ee()->freeform_field_model->get();
  556. $available_fields = ($available_fields !== FALSE) ?
  557. $available_fields :
  558. array();
  559. //fields
  560. $this->cached_vars['available_fields'] = $available_fields;
  561. //notifications
  562. $this->cached_vars['notifications'] = $this->data->get_available_notification_templates();
  563. // -------------------------------------
  564. // user email fields
  565. // -------------------------------------
  566. $user_email_fields = array('' => lang('choose_user_email_field'));
  567. $f_rows = ee()->freeform_field_model
  568. ->select('field_id, field_label, settings')
  569. ->get(array('field_type' => 'text'));
  570. //we only want fields that are being validated as email
  571. if ($f_rows)
  572. {
  573. foreach ($f_rows as $row)
  574. {
  575. $row_settings = json_decode($row['settings'], TRUE);
  576. $row_settings = (is_array($row_settings)) ? $row_settings : array();
  577. if (isset($row_settings['field_content_type']) AND
  578. $row_settings['field_content_type'] == 'email')
  579. {
  580. $user_email_fields[$row['field_id']] = $row['field_label'];
  581. }
  582. }
  583. }
  584. $this->cached_vars['user_email_fields'] = $user_email_fields;
  585. // ----------------------------------------
  586. // Load vars
  587. // ----------------------------------------
  588. $this->cached_vars['form_uri'] = $this->mod_link(array(
  589. 'method' => 'save_form'
  590. ));
  591. // -------------------------------------
  592. // js libs
  593. // -------------------------------------
  594. $this->load_fancybox();
  595. $this->cached_vars['cp_javascript'][] = 'jquery.smooth-scroll.min';
  596. ee()->cp->add_js_script(array(
  597. 'ui' => array('draggable', 'droppable', 'sortable')
  598. ));
  599. // --------------------------------------------
  600. // Load page
  601. // --------------------------------------------
  602. $this->cached_vars['current_page'] = $this->view(
  603. 'edit_form.html',
  604. NULL,
  605. TRUE
  606. );
  607. return $this->ee_cp_view('index.html');
  608. }
  609. //END edit_form
  610. // --------------------------------------------------------------------
  611. /**
  612. * form composer
  613. *
  614. * ajax form and field builder
  615. *
  616. * @access public
  617. * @param string message lang line
  618. * @return string html output
  619. */
  620. public function form_composer ( $message = '' )
  621. {
  622. // -------------------------------------
  623. // Messages
  624. // -------------------------------------
  625. if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  626. {
  627. $message = lang(ee()->input->get('msg'));
  628. }
  629. $this->cached_vars['message'] = $message;
  630. // -------------------------------------
  631. // form_id
  632. // -------------------------------------
  633. $form_id = ee()->input->get_post('form_id', TRUE);
  634. $form_data = $this->data->get_form_info($form_id);
  635. if ( ! $form_data)
  636. {
  637. return $this->actions()->full_stop(lang('invalid_form_id'));
  638. }
  639. $update = $form_data['composer_id'] != 0;
  640. //--------------------------------------------
  641. // Crumbs and tab highlight
  642. //--------------------------------------------
  643. $this->add_crumb( lang('forms'), $this->base );
  644. $this->add_crumb( lang('composer') . ': ' . $form_data['form_label'] );
  645. $this->set_highlight('module_forms');
  646. // -------------------------------------
  647. // data
  648. // -------------------------------------
  649. $this->cached_vars['form_data'] = $form_data;
  650. // -------------------------------------
  651. // fields for composer
  652. // -------------------------------------
  653. ee()->load->model('freeform_field_model');
  654. $available_fields = ee()->freeform_field_model
  655. ->where('composer_use', 'y')
  656. ->order_by('field_label')
  657. ->key('field_name')
  658. ->get();
  659. $available_fields = ($available_fields !== FALSE) ?
  660. $available_fields :
  661. array();
  662. // -------------------------------------
  663. // templates
  664. // -------------------------------------
  665. ee()->load->model('freeform_template_model');
  666. $available_templates = ee()->freeform_template_model
  667. ->where('enable_template', 'y')
  668. ->order_by('template_label')
  669. ->key('template_id', 'template_label')
  670. ->get();
  671. $available_templates = ($available_templates !== FALSE) ?
  672. $available_templates :
  673. array();
  674. // -------------------------------------
  675. // get field output for composer
  676. // -------------------------------------
  677. ee()->load->library('freeform_fields');
  678. $field_composer_output = array();
  679. $field_id_list = array();
  680. foreach ($available_fields as $field_name => $field_data)
  681. {
  682. $field_id_list[$field_data['field_id']] = $field_name;
  683. //encode to keep JS from running
  684. //camel case because its exposed in JS
  685. $field_composer_output[$field_name] = $this->composer_field_data(
  686. $field_data['field_id'],
  687. $field_data,
  688. TRUE
  689. );
  690. }
  691. $this->cached_vars['field_id_list'] = $this->json_encode($field_id_list);
  692. $this->cached_vars['field_composer_output_json'] = $this->json_encode($field_composer_output);
  693. $this->cached_vars['available_fields'] = $available_fields;
  694. $this->cached_vars['available_templates'] = $available_templates;
  695. $this->cached_vars['prohibited_field_names'] = $this->data->prohibited_names;
  696. $this->cached_vars['notifications'] = $this->data->get_available_notification_templates();
  697. // -------------------------------------
  698. // previous composer data?
  699. // -------------------------------------
  700. $composer_data = '{}';
  701. if ($form_data['composer_id'] > 0)
  702. {
  703. ee()->load->model('freeform_composer_model');
  704. $composer = ee()->freeform_composer_model
  705. ->select('composer_data')
  706. ->where('composer_id', $form_data['composer_id'])
  707. ->get_row();
  708. if ($composer !== FALSE)
  709. {
  710. $composer_data_test = $this->json_decode($composer['composer_data']);
  711. if ($composer_data_test)
  712. {
  713. $composer_data = $composer['composer_data'];
  714. }
  715. }
  716. }
  717. $this->cached_vars['composer_layout_data'] = $composer_data;
  718. // ----------------------------------------
  719. // Load vars
  720. // ----------------------------------------
  721. $this->cached_vars['lang_allowed_html_tags'] = (
  722. lang('allowed_html_tags') .
  723. "&lt;" . implode("&gt;, &lt;", $this->data->allowed_html_tags) . "&gt;"
  724. );
  725. $this->cached_vars['captcha_dummy_url'] = $this->sc->addon_theme_url .
  726. 'images/captcha.png';
  727. $this->cached_vars['new_field_url'] = $this->mod_link(array(
  728. 'method' => 'edit_field',
  729. //this builds a URL, so yes this is intentionally a string
  730. 'modal' => 'true'
  731. ), TRUE);
  732. $this->cached_vars['field_data_url'] = $this->mod_link(array(
  733. 'method' => 'composer_field_data'
  734. ), TRUE);
  735. $this->cached_vars['composer_preview_url'] = $this->mod_link(array(
  736. 'method' => 'composer_preview',
  737. 'form_id' => $form_id
  738. ), TRUE);
  739. $this->cached_vars['composer_ajax_save_url'] = $this->mod_link(array(
  740. 'method' => 'save_composer_data',
  741. 'form_id' => $form_id
  742. ), TRUE);
  743. //
  744. $this->cached_vars['composer_save_url'] = $this->mod_link(array(
  745. 'method' => 'save_composer_data',
  746. 'form_id' => $form_id
  747. ));
  748. $this->cached_vars['allowed_html_tags'] = "'" .
  749. implode("','", $this->data->allowed_html_tags) . "'";
  750. // -------------------------------------
  751. // js libs
  752. // -------------------------------------
  753. $this->load_fancybox();
  754. ee()->cp->add_js_script(array(
  755. 'ui' => array('sortable', 'draggable', 'droppable'),
  756. 'file' => array('underscore', 'json2')
  757. ));
  758. // --------------------------------------------
  759. // Load page
  760. // --------------------------------------------
  761. $this->cached_vars['cp_javascript'][] = 'composer_cp.min';
  762. $this->cached_vars['cp_javascript'][] = 'edit_field_cp.min';
  763. $this->cached_vars['cp_javascript'][] = 'security.min';
  764. $this->cached_vars['current_page'] = $this->view(
  765. 'composer.html',
  766. NULL,
  767. TRUE
  768. );
  769. return $this->ee_cp_view('index.html');
  770. }
  771. //END form_composer
  772. // --------------------------------------------------------------------
  773. /**
  774. * Composer preview
  775. *
  776. * @access public
  777. * @return mixed ajax return if detected or else html without cp
  778. */
  779. public function composer_preview ()
  780. {
  781. $form_id = $this->get_post_or_zero('form_id');
  782. $template_id = $this->get_post_or_zero('template_id');
  783. $composer_id = $this->get_post_or_zero('composer_id');
  784. $preview_id = $this->get_post_or_zero('preview_id');
  785. $subpreview = (ee()->input->get_post('subpreview') !== FALSE);
  786. $composer_page = $this->get_post_or_zero('composer_page');
  787. if ( ! $this->data->is_valid_form_id($form_id))
  788. {
  789. $this->actions()->full_stop(lang('invalid_form_id'));
  790. }
  791. // -------------------------------------
  792. // is this a preview?
  793. // -------------------------------------
  794. if ($preview_id > 0)
  795. {
  796. $preview_mode = TRUE;
  797. $composer_id = $preview_id;
  798. }
  799. $page_count = 1;
  800. // -------------------------------------
  801. // main output or sub output?
  802. // -------------------------------------
  803. if ( ! $subpreview)
  804. {
  805. // -------------------------------------
  806. // get composer data and build page count
  807. // -------------------------------------
  808. if ($composer_id > 0)
  809. {
  810. ee()->load->model('freeform_composer_model');
  811. $composer = ee()->freeform_composer_model
  812. ->select('composer_data')
  813. ->where('composer_id', $composer_id)
  814. ->get_row();
  815. if ($composer !== FALSE)
  816. {
  817. $composer_data = $this->json_decode(
  818. $composer['composer_data'],
  819. TRUE
  820. );
  821. if ($composer_data AND
  822. isset($composer_data['rows']) AND
  823. ! empty($composer_data['rows']))
  824. {
  825. foreach ($composer_data['rows'] as $row)
  826. {
  827. if ($row == 'page_break')
  828. {
  829. $page_count++;
  830. }
  831. }
  832. }
  833. }
  834. }
  835. $page_url = array();
  836. for ($i = 1, $l = $page_count; $i <= $l; $i++)
  837. {
  838. $page_url[] = $this->mod_link(array(
  839. 'method' => __FUNCTION__,
  840. 'form_id' => $form_id,
  841. 'template_id' => $template_id,
  842. 'preview_id' => $preview_id,
  843. 'subpreview' => 'true',
  844. 'composer_page' => $i
  845. ));
  846. }
  847. $this->cached_vars['page_url'] = $page_url;
  848. $this->cached_vars['default_preview_css'] = $this->sc->addon_theme_url .
  849. 'css/default_composer.css';
  850. $this->cached_vars['jquery_src'] = rtrim(ee()->config->item('theme_folder_url'), '/') .
  851. '/javascript/compressed/jquery/jquery.js';
  852. $html = $this->view('composer_preview.html', NULL, TRUE);
  853. }
  854. else
  855. {
  856. $subhtml = "{exp:freeform:composer form_id='$form_id'";
  857. $subhtml .= ($composer_page > 1) ? " multipage_page='" . $composer_page . "'" : '';
  858. $subhtml .= ($template_id > 0) ? " composer_template_id='" . $template_id . "'" : '';
  859. $subhtml .= ($preview_id > 0) ? " preview_id='" . $preview_id . "'" : '';
  860. $subhtml .= "}";
  861. $html = $this->actions()->template()->process_string_as_template($subhtml);
  862. }
  863. if ($this->is_ajax_request())
  864. {
  865. return $this->send_ajax_response(array(
  866. 'success' => TRUE,
  867. 'html' => $html
  868. ));
  869. }
  870. else
  871. {
  872. exit($html);
  873. }
  874. }
  875. //end composer preview
  876. // --------------------------------------------------------------------
  877. /**
  878. * entries
  879. *
  880. * @access public
  881. * @param string $message message lang line
  882. * @param bool $moderate are we moderating?
  883. * @param bool $export export?
  884. * @return string html output
  885. */
  886. public function entries ( $message = '' , $moderate = FALSE, $export = FALSE)
  887. {
  888. // -------------------------------------
  889. // Messages
  890. // -------------------------------------
  891. if ($message == '' AND
  892. ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  893. {
  894. $message = lang(ee()->input->get('msg', TRUE));
  895. }
  896. $this->cached_vars['message'] = $message;
  897. // -------------------------------------
  898. // moderate
  899. // -------------------------------------
  900. $search_status = ee()->input->get_post('search_status');
  901. $moderate = (
  902. $moderate AND
  903. ($search_status == 'pending' OR
  904. $search_status === FALSE
  905. )
  906. );
  907. //if moderate and search status was not submitted, fake into pending
  908. if ($moderate AND $search_status === FALSE)
  909. {
  910. $_POST['search_status'] = 'pending';
  911. }
  912. $this->cached_vars['moderate'] = $moderate;
  913. $this->cached_vars['method'] = $method = (
  914. $moderate ? 'moderate_entries' : 'entries'
  915. );
  916. // -------------------------------------
  917. // form data? legit? GTFO?
  918. // -------------------------------------
  919. $form_id = ee()->input->get_post('form_id');
  920. ee()->load->library('freeform_forms');
  921. ee()->load->model('freeform_form_model');
  922. //form data does all of the proper id validity checks for us
  923. $form_data = $this->data->get_form_info($form_id);
  924. if ( ! $form_data)
  925. {
  926. $this->actions()->full_stop(lang('invalid_form_id'));
  927. exit();
  928. }
  929. $this->cached_vars['form_id'] = $form_id;
  930. $this->cached_vars['form_label'] = $form_data['form_label'];
  931. //--------------------------------------------
  932. // Crumbs and tab highlight
  933. //--------------------------------------------
  934. $this->add_crumb(
  935. lang('forms'),
  936. $this->mod_link(array('method' => 'forms'))
  937. );
  938. $this->add_crumb(
  939. $form_data['form_label'] . ': ' .
  940. lang($moderate ? 'moderate' : 'entries')
  941. );
  942. $this->set_highlight('module_forms');
  943. $this->freeform_add_right_link(
  944. lang('new_entry'),
  945. $this->mod_link(array(
  946. 'method' => 'edit_entry',
  947. 'form_id' => $form_id
  948. ))
  949. );
  950. // -------------------------------------
  951. // status prefs
  952. // -------------------------------------
  953. $form_statuses = $this->data->get_form_statuses();
  954. $this->cached_vars['form_statuses'] = $form_statuses;
  955. // -------------------------------------
  956. // rest of models
  957. // -------------------------------------
  958. ee()->load->model('freeform_entry_model');
  959. ee()->freeform_entry_model->id($form_id);
  960. // -------------------------------------
  961. // custom field labels
  962. // -------------------------------------
  963. $standard_columns = $this->get_standard_column_names();
  964. //we want author instead of author id until we get data
  965. $possible_columns = $standard_columns;
  966. //key = value
  967. $all_columns = array_combine($standard_columns, $standard_columns);
  968. $column_labels = array();
  969. $field_column_names = array();
  970. //field prefix
  971. $f_prefix = ee()->freeform_form_model->form_field_prefix;
  972. //keyed labels for the front end
  973. foreach ($standard_columns as $column_name)
  974. {
  975. $column_labels[$column_name] = lang($column_name);
  976. }
  977. // -------------------------------------
  978. // check for fields with custom views for entry tables
  979. // -------------------------------------
  980. ee()->load->library('freeform_fields');
  981. //fields in this form
  982. foreach ($form_data['fields'] as $field_id => $field_data)
  983. {
  984. //outputs form_field_1, form_field_2, etc for ->select()
  985. $field_id_name = $f_prefix . $field_id;
  986. $field_column_names[$field_id_name] = $field_data['field_name'];
  987. $all_columns[$field_id_name] = $field_data['field_name'];
  988. $column_labels[$field_data['field_name']] = $field_data['field_label'];
  989. $column_labels[$field_id_name] = $field_data['field_label'];
  990. $possible_columns[] = $field_id;
  991. $instance =& ee()->freeform_fields->get_field_instance(array(
  992. 'field_id' => $field_id,
  993. 'field_data' => $field_data
  994. ));
  995. if ( ! empty($instance->entry_views))
  996. {
  997. foreach ($instance->entry_views as $e_lang => $e_method)
  998. {
  999. $this->freeform_add_right_link(
  1000. $e_lang,
  1001. $this->mod_link(array(
  1002. 'method' => 'field_method',
  1003. 'field_id' => $field_id,
  1004. 'field_method' => $e_method,
  1005. 'form_id' => $form_id
  1006. ))
  1007. );
  1008. }
  1009. }
  1010. }
  1011. // -------------------------------------
  1012. // visible columns
  1013. // -------------------------------------
  1014. $visible_columns = $this->visible_columns($standard_columns, $possible_columns);
  1015. $this->cached_vars['visible_columns'] = $visible_columns;
  1016. $this->cached_vars['column_labels'] = $column_labels;
  1017. $this->cached_vars['possible_columns'] = $possible_columns;
  1018. $this->cached_vars['all_columns'] = $all_columns;
  1019. // -------------------------------------
  1020. // prep unused from from possible
  1021. // -------------------------------------
  1022. //so so used
  1023. $un_used = array();
  1024. foreach ($possible_columns as $pcid)
  1025. {
  1026. $check = ($this->is_positive_intlike($pcid)) ?
  1027. $f_prefix . $pcid :
  1028. $pcid;
  1029. if ( ! in_array($check, $visible_columns))
  1030. {
  1031. $un_used[] = $check;
  1032. }
  1033. }
  1034. $this->cached_vars['unused_columns'] = $un_used;
  1035. // -------------------------------------
  1036. // build query
  1037. // -------------------------------------
  1038. //base url for pagination
  1039. $pag_url = array(
  1040. 'method' => $method,
  1041. 'form_id' => $form_id
  1042. );
  1043. //cleans out blank keys from unset
  1044. $find_columns = array_merge(array(), $visible_columns);
  1045. $must_haves = array('entry_id');
  1046. // -------------------------------------
  1047. // search criteria
  1048. // building query
  1049. // -------------------------------------
  1050. $has_search = FALSE;
  1051. $search_vars = array(
  1052. 'search_keywords',
  1053. 'search_status',
  1054. 'search_date_range',
  1055. 'search_date_range_start',
  1056. 'search_date_range_end',
  1057. 'search_on_field'
  1058. );
  1059. foreach ($search_vars as $search_var)
  1060. {
  1061. $$search_var = ee()->input->get_post($search_var, TRUE);
  1062. //set for output
  1063. $this->cached_vars[$search_var] = (
  1064. ($$search_var) ? trim($$search_var) : ''
  1065. );
  1066. }
  1067. // -------------------------------------
  1068. // search keywords
  1069. // -------------------------------------
  1070. if ($search_keywords AND
  1071. trim($search_keywords) !== '' AND
  1072. $search_on_field AND
  1073. in_array($search_on_field, $visible_columns))
  1074. {
  1075. ee()->freeform_entry_model->like(
  1076. $search_on_field,
  1077. $search_keywords
  1078. );
  1079. //pagination
  1080. $pag_url['search_keywords'] = $search_keywords;
  1081. $pag_url['search_on_field'] = $search_on_field;
  1082. $has_search = TRUE;
  1083. }
  1084. //no search on field? guess we had better search it all *gulp*
  1085. else if ($search_keywords AND trim($search_keywords) !== '')
  1086. {
  1087. $first = TRUE;
  1088. ee()->freeform_entry_model->group_like(
  1089. $search_keywords,
  1090. array_values($visible_columns)
  1091. );
  1092. $pag_url['search_keywords'] = $search_keywords;
  1093. $has_search = TRUE;
  1094. }
  1095. //status search?
  1096. if ($moderate)
  1097. {
  1098. ee()->freeform_entry_model->where('status', 'pending');
  1099. }
  1100. else if ($search_status AND in_array($search_status, array_flip( $form_statuses)))
  1101. {
  1102. ee()->freeform_entry_model->where('status', $search_status);
  1103. //pagination
  1104. $pag_url['search_status'] = $search_status;
  1105. $has_search = TRUE;
  1106. }
  1107. // -------------------------------------
  1108. // date range?
  1109. // -------------------------------------
  1110. //pagination
  1111. if ($search_date_range == 'date_range')
  1112. {
  1113. if ($search_date_range_start !== FALSE)
  1114. {
  1115. $pag_url['search_date_range_start'] = $search_date_range_start;
  1116. }
  1117. if ($search_date_range_end !== FALSE)
  1118. {
  1119. $pag_url['search_date_range_end'] = $search_date_range_end;
  1120. }
  1121. //pagination
  1122. if ($search_date_range_start OR $search_date_range_end)
  1123. {
  1124. $pag_url['search_date_range'] = 'date_range';
  1125. $has_search = TRUE;
  1126. }
  1127. }
  1128. else if ($search_date_range !== FALSE)
  1129. {
  1130. $pag_url['search_date_range'] = $search_date_range;
  1131. $has_search = TRUE;
  1132. }
  1133. ee()->freeform_entry_model->date_where(
  1134. $search_date_range,
  1135. $search_date_range_start,
  1136. $search_date_range_end
  1137. );
  1138. // -------------------------------------
  1139. // any searches?
  1140. // -------------------------------------
  1141. $this->cached_vars['has_search'] = $has_search;
  1142. // -------------------------------------
  1143. // data from all sites?
  1144. // -------------------------------------
  1145. if ( ! $this->data->show_all_sites())
  1146. {
  1147. ee()->freeform_entry_model->where(
  1148. 'site_id',
  1149. ee()->config->item('site_id')
  1150. );
  1151. }
  1152. //we need the counts for exports and end results
  1153. $total_entries = ee()->freeform_entry_model->count(array(), FALSE);
  1154. // -------------------------------------
  1155. // orderby
  1156. // -------------------------------------
  1157. $order_by = 'entry_date';
  1158. $p_order_by = ee()->input->get_post('order_by');
  1159. if ($p_order_by !== FALSE AND in_array($p_order_by, $all_columns))
  1160. {
  1161. $order_by = $p_order_by;
  1162. $pag_url['order_by'] = $order_by;
  1163. }
  1164. // -------------------------------------
  1165. // sort
  1166. // -------------------------------------
  1167. $sort = ($order_by == 'entry_date') ? 'desc' : 'asc';
  1168. $p_sort = ee()->input->get_post('sort');
  1169. if ($p_sort !== FALSE AND
  1170. in_array(strtolower($p_sort), array('asc', 'desc')))
  1171. {
  1172. $sort = strtolower($p_sort);
  1173. $pag_url['sort'] = $sort;
  1174. }
  1175. ee()->freeform_entry_model->order_by($order_by, $sort);
  1176. $this->cached_vars['order_by'] = $order_by;
  1177. $this->cached_vars['sort'] = $sort;
  1178. // -------------------------------------
  1179. // export button
  1180. // -------------------------------------
  1181. if ($total_entries > 0)
  1182. {
  1183. $this->freeform_add_right_link(
  1184. lang('export_entries'),
  1185. '#export_entries'
  1186. );
  1187. }
  1188. // -------------------------------------
  1189. // export url
  1190. // -------------------------------------
  1191. $export_url = $pag_url;
  1192. $export_url['moderate'] = $moderate ? 'true' : 'false';
  1193. $export_url['method'] = 'export_entries';
  1194. $this->cached_vars['export_url'] = $this->mod_link($export_url);
  1195. // -------------------------------------
  1196. // export?
  1197. // -------------------------------------
  1198. if ($export)
  1199. {
  1200. $export_fields = ee()->input->get_post('export_fields');
  1201. $export_labels = $column_labels;
  1202. // -------------------------------------
  1203. // build possible select alls
  1204. // -------------------------------------
  1205. $select = array();
  1206. //are we sending just the selected fields?
  1207. if ($export_fields != 'all')
  1208. {
  1209. $select = array_unique(array_merge($must_haves, $find_columns));
  1210. foreach ($export_labels as $key => $value)
  1211. {
  1212. //clean export labels for json
  1213. if ( ! in_array($key, $select))
  1214. {
  1215. unset($export_labels[$key]);
  1216. }
  1217. }
  1218. //get real names
  1219. foreach ($select as $key => $value)
  1220. {
  1221. if (isset($field_column_names[$value]))
  1222. {
  1223. $select[$key] = $field_column_names[$value];
  1224. }
  1225. }
  1226. }
  1227. //sending all fields means we need to still clean some labels
  1228. else
  1229. {
  1230. foreach ($all_columns as $field_id_name => $field_name)
  1231. {
  1232. //clean export labels for json
  1233. if ($field_id_name != $field_name)
  1234. {
  1235. unset($export_labels[$field_id_name]);
  1236. }
  1237. $select[] = $field_name;
  1238. }
  1239. }
  1240. foreach ($export_labels as $key => $value)
  1241. {
  1242. //fix entities
  1243. $value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
  1244. $export_labels[$key] = $value;
  1245. if (isset($field_column_names[$key]))
  1246. {
  1247. $export_labels[$field_column_names[$key]] = $value;
  1248. }
  1249. }
  1250. ee()->freeform_entry_model->select(implode(', ', $select));
  1251. // -------------------------------------
  1252. // check for chunking, etc
  1253. // -------------------------------------
  1254. ee()->load->library('freeform_export');
  1255. ee()->freeform_export->export(array(
  1256. 'method' => ee()->input->get_post('export_method'),
  1257. 'form_id' => $form_id,
  1258. 'form_name' => $form_data['form_name'],
  1259. 'output' => 'download',
  1260. 'model' => ee()->freeform_entry_model,
  1261. 'remove_entry_id' => ($export_fields != 'all' AND ! in_array('entry_id', $visible_columns)),
  1262. 'header_labels' => $export_labels,
  1263. 'total_entries' => $total_entries
  1264. ));
  1265. }
  1266. //END if ($export)
  1267. // -------------------------------------
  1268. // selects
  1269. // -------------------------------------
  1270. $needed_selects = array_unique(array_merge($must_haves, $find_columns));
  1271. ee()->freeform_entry_model->select(implode(', ', $needed_selects));
  1272. //--------------------------------------
  1273. // pagination start vars
  1274. //--------------------------------------
  1275. $pag_url = $this->mod_link($pag_url);
  1276. $row_limit = $this->data->defaults['mcp_row_limit'];
  1277. $paginate = '';
  1278. $row_count = 0;
  1279. //moved above exports
  1280. //$total_entries = ee()->freeform_entry_model->count(array(), FALSE);
  1281. $current_page = 0;
  1282. // -------------------------------------
  1283. // pagination?
  1284. // -------------------------------------
  1285. // do we need pagination?
  1286. if ($total_entries > $row_limit )
  1287. {
  1288. $row_count = $this->get_post_or_zero('row');
  1289. //get pagination info
  1290. $pagination_data = $this->universal_pagination(array(
  1291. 'total_results' => $total_entries,
  1292. 'limit' => $row_limit,
  1293. 'current_page' => $row_count,
  1294. 'pagination_config' => array('base_url' => $pag_url),
  1295. 'query_string_segment' => 'row'
  1296. ));
  1297. $paginate = $pagination_data['pagination_links'];
  1298. $current_page = $pagination_data['pagination_page'];
  1299. ee()->freeform_entry_model->limit($row_limit, $current_page);
  1300. }
  1301. $this->cached_vars['paginate'] = $paginate;
  1302. // -------------------------------------
  1303. // get data
  1304. // -------------------------------------
  1305. $result_array = ee()->freeform_entry_model->get();
  1306. $count = $row_count;
  1307. $entries = array();
  1308. if ( ! $result_array)
  1309. {
  1310. $result_array = array();
  1311. }
  1312. $entry_ids = array();
  1313. foreach ($result_array as $row)
  1314. {
  1315. $entry_ids[] = $row['entry_id'];
  1316. }
  1317. // -------------------------------------
  1318. // allow pre_process
  1319. // -------------------------------------
  1320. ee()->freeform_fields->apply_field_method(array(
  1321. 'method' => 'pre_process_entries',
  1322. 'form_id' => $form_id,
  1323. 'entry_id' => $entry_ids,
  1324. 'form_data' => $form_data,
  1325. 'field_data' => $form_data['fields']
  1326. ));
  1327. foreach ( $result_array as $row)
  1328. {
  1329. //apply display_entry_cp to our field data
  1330. $field_parse = ee()->freeform_fields->apply_field_method(array(
  1331. 'method' => 'display_entry_cp',
  1332. 'form_id' => $form_id,
  1333. 'entry_id' => $row['entry_id'],
  1334. 'form_data' => $form_data,
  1335. 'field_data' => $form_data['fields'],
  1336. 'field_input_data' => $row
  1337. ));
  1338. $row = array_merge($row, $field_parse['variables']);
  1339. $entry = array();
  1340. $entry['view_entry_link'] = $this->mod_link(array(
  1341. 'method' => 'view_entry',
  1342. 'form_id' => $form_id,
  1343. 'entry_id' => $row['entry_id']
  1344. ));
  1345. $entry['edit_entry_link'] = $this->mod_link(array(
  1346. 'method' => 'edit_entry',
  1347. 'form_id' => $form_id,
  1348. 'entry_id' => $row['entry_id']
  1349. ));
  1350. $entry['approve_link'] = $this->mod_link(array(
  1351. 'method' => 'approve_entries',
  1352. 'form_id' => $form_id,
  1353. 'entry_ids' => $row['entry_id']
  1354. ));
  1355. $entry['count'] = ++$count;
  1356. $entry['id'] = $row['entry_id'];
  1357. // -------------------------------------
  1358. // remove entry_id and author_id if we
  1359. // arent showing them
  1360. // -------------------------------------
  1361. if ( ! in_array('entry_id', $visible_columns))
  1362. {
  1363. unset($row['entry_id']);
  1364. }
  1365. // -------------------------------------
  1366. // dates
  1367. // -------------------------------------
  1368. if (in_array('entry_date', $visible_columns))
  1369. {
  1370. $row['entry_date'] = gmdate(
  1371. $this->preference('cp_date_formatting'),
  1372. ee()->localize->set_localized_time($row['entry_date'])
  1373. );
  1374. }
  1375. if (in_array('edit_date', $visible_columns))
  1376. {
  1377. $row['edit_date'] = (
  1378. ($row['edit_date'] > 0) ?
  1379. gmdate(
  1380. $this->preference('cp_date_formatting'),
  1381. ee()->localize->set_localized_time($row['edit_date'])
  1382. ) :
  1383. lang('n_a')
  1384. );
  1385. }
  1386. $entry['data'] = $row;
  1387. $entries[] = $entry;
  1388. }
  1389. $this->cached_vars['entries'] = $entries;
  1390. // -------------------------------------
  1391. // ajax request?
  1392. // -------------------------------------
  1393. if ($this->is_ajax_request())
  1394. {
  1395. $this->send_ajax_response(array(
  1396. 'entries' => $entries,
  1397. 'paginate' => $paginate,
  1398. 'visibleColumns' => $visible_columns,
  1399. 'allColumns' => $all_columns,
  1400. 'columnLabels' => $column_labels,
  1401. 'success' => TRUE
  1402. ));
  1403. exit();
  1404. }
  1405. // -------------------------------------
  1406. // moderation count?
  1407. // -------------------------------------
  1408. //lets not waste the query if we are already moderating
  1409. $moderation_count = (
  1410. ( ! $moderate) ?
  1411. $this->data->get_form_needs_moderation_count($form_id) :
  1412. 0
  1413. );
  1414. if ($moderation_count > 0)
  1415. {
  1416. $this->cached_vars['lang_num_items_awaiting_moderation'] = str_replace(
  1417. array('%num%', '%form_label%'),
  1418. array($moderation_count, $form_data['form_label']),
  1419. lang('num_items_awaiting_moderation')
  1420. );
  1421. }
  1422. $this->cached_vars['moderation_count'] = $moderation_count;
  1423. $this->cached_vars['moderation_link'] = $this->mod_link(array(
  1424. 'method' => 'moderate_entries',
  1425. 'form_id' => $form_id,
  1426. 'search_status' => 'pending'
  1427. ));
  1428. // -------------------------------------
  1429. // is admin?
  1430. // -------------------------------------
  1431. $this->cached_vars['is_admin'] = $is_admin = (
  1432. ee()->session->userdata('group_id') == 1
  1433. );
  1434. // -------------------------------------
  1435. // can save field layout?
  1436. // -------------------------------------
  1437. //$this->cached_vars['can_edit_layout'] = $can_edit_layout = (
  1438. // $is_admin OR
  1439. // $this->check_yes($this->preference('allow_user_field_layout'))
  1440. //);
  1441. //just in case
  1442. $this->cached_vars['can_edit_layout'] = TRUE;
  1443. $this->freeform_add_right_link(
  1444. lang('edit_field_layout'),
  1445. '#edit_field_layout'
  1446. );
  1447. // -------------------------------------
  1448. // member groups
  1449. // -------------------------------------
  1450. $member_groups = array();
  1451. if ($is_admin)
  1452. {
  1453. ee()->db->select('group_id, group_title');
  1454. $member_groups = $this->prepare_keyed_result(
  1455. ee()->db->get('member_groups'),
  1456. 'group_id',
  1457. 'group_title'
  1458. );
  1459. }
  1460. $this->cached_vars['member_groups'] = $member_groups;
  1461. // -------------------------------------
  1462. // lang items
  1463. // -------------------------------------
  1464. // -------------------------------------
  1465. // no results lang
  1466. // -------------------------------------
  1467. $this->cached_vars['lang_no_results_for_form'] = (
  1468. ($has_search) ?
  1469. lang('no_results_for_search') :
  1470. (
  1471. ($moderate) ?
  1472. lang('no_entries_awaiting_approval') :
  1473. lang('no_entries_for_form')
  1474. )
  1475. );
  1476. // -------------------------------------
  1477. // moderation lang
  1478. // -------------------------------------
  1479. $this->cached_vars['lang_viewing_moderation'] = str_replace(
  1480. '%form_label%',
  1481. $form_data['form_label'],
  1482. lang('viewing_moderation')
  1483. );
  1484. // -------------------------------------
  1485. // other vars
  1486. // -------------------------------------
  1487. $this->cached_vars['form_url'] = $this->mod_link(array(
  1488. 'method' => 'entries_action',
  1489. 'return_method' => (($moderate) ? 'moderate_' : '' ) . 'entries'
  1490. ));
  1491. $this->cached_vars['save_layout_url'] = $this->mod_link(array(
  1492. 'method' => 'save_field_layout'
  1493. ));
  1494. // -------------------------------------
  1495. // js libs
  1496. // -------------------------------------
  1497. $this->load_fancybox();
  1498. //$this->load_datatables();
  1499. ee()->cp->add_js_script(array(
  1500. 'ui' => array('datepicker', 'sortable'),
  1501. 'file' => 'underscore'
  1502. ));
  1503. // --------------------------------------------
  1504. // Load page
  1505. // --------------------------------------------
  1506. $this->cached_vars['current_page'] = $this->view(
  1507. 'entries.html',
  1508. NULL,
  1509. TRUE
  1510. );
  1511. return $this->ee_cp_view('index.html');
  1512. }
  1513. //END entries
  1514. // --------------------------------------------------------------------
  1515. public function field_method ($message = '')
  1516. {
  1517. // -------------------------------------
  1518. // Messages
  1519. // -------------------------------------
  1520. if ($message == '' AND
  1521. ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  1522. {
  1523. $message = lang(ee()->input->get('msg', TRUE));
  1524. }
  1525. $this->cached_vars['message'] = $message;
  1526. // -------------------------------------
  1527. // goods
  1528. // -------------------------------------
  1529. $form_id = $this->get_post_or_zero('form_id');
  1530. $field_id = $this->get_post_or_zero('field_id');
  1531. $field_method = ee()->input->get_post('field_method');
  1532. $instance = FALSE;
  1533. if ( $field_method == FALSE OR
  1534. ! $this->data->is_valid_form_id($form_id) OR
  1535. $field_id == 0)
  1536. {
  1537. ee()->functions->redirect($this->mod_link(array('method' => 'forms')));
  1538. }
  1539. ee()->load->library('freeform_fields');
  1540. $instance =& ee()->freeform_fields->get_field_instance(array(
  1541. 'form_id' => $form_id,
  1542. 'field_id' => $field_id
  1543. ));
  1544. //legit?
  1545. if ( ! is_object($instance) OR
  1546. empty($instance->entry_views) OR
  1547. //removed so you can post to this
  1548. //! in_array($field_method, $instance->entry_views) OR
  1549. ! is_callable(array($instance, $field_method)))
  1550. {
  1551. ee()->functions->redirect($this->mod_link(array('method' => 'forms')));
  1552. }
  1553. $method_lang = lang('field_entry_view');
  1554. foreach ($instance->entry_views as $e_lang => $e_method)
  1555. {
  1556. if ($field_method == $e_method)
  1557. {
  1558. $method_lang = $e_lang;
  1559. }
  1560. }
  1561. //--------------------------------------------
  1562. // Crumbs and tab highlight
  1563. //--------------------------------------------
  1564. $this->add_crumb(
  1565. lang('entries'),
  1566. $this->mod_link(array(
  1567. 'method' => 'entries',
  1568. 'form_id' => $form_id
  1569. ))
  1570. );
  1571. $this->add_crumb($method_lang);
  1572. $this->set_highlight('module_forms');
  1573. // --------------------------------------------
  1574. // Load page
  1575. // --------------------------------------------
  1576. $this->cached_vars['current_page'] = $instance->$field_method();
  1577. // -------------------------------------
  1578. // loading these after the instance
  1579. // incase the instance exits
  1580. // -------------------------------------
  1581. $this->load_fancybox();
  1582. return $this->ee_cp_view('index.html');
  1583. }
  1584. //END field_method
  1585. // --------------------------------------------------------------------
  1586. /**
  1587. * migrate collections
  1588. *
  1589. * @access public
  1590. * @return string
  1591. */
  1592. public function migrate_collections ( $message = '' )
  1593. {
  1594. if ($message == '' AND ee()->input->get('msg') !== FALSE)
  1595. {
  1596. $message = lang(ee()->input->get('msg'));
  1597. }
  1598. $this->cached_vars['message'] = $message;
  1599. //--------------------------------------
  1600. // Title and Crumbs
  1601. //--------------------------------------
  1602. $this->add_crumb(lang('utilities'));
  1603. $this->set_highlight('module_utilities');
  1604. //--------------------------------------
  1605. // Library
  1606. //--------------------------------------
  1607. ee()->load->library('freeform_migration');
  1608. //--------------------------------------
  1609. // Variables
  1610. //--------------------------------------
  1611. $migrate_empty_fields = 'n';
  1612. $migrate_attachments = 'n';
  1613. $this->cached_vars['total'] = 0;
  1614. $this->cached_vars['collections'] = '';
  1615. $collections = ee()->input->post('collections');
  1616. if ( $collections !== FALSE )
  1617. {
  1618. $this->cached_vars['total'] = ee()->freeform_migration
  1619. ->get_migration_count(
  1620. $collections
  1621. );
  1622. $this->cached_vars['collections'] = implode('|', $collections);
  1623. if ($this->check_yes( ee()->input->post('migrate_empty_fields') ) )
  1624. {
  1625. $migrate_empty_fields = 'y';
  1626. }
  1627. if ($this->check_yes( ee()->input->post('migrate_attachments') ) )
  1628. {
  1629. $migrate_attachments = 'y';
  1630. }
  1631. }
  1632. //--------------------------------------
  1633. // Migration ajax url
  1634. //--------------------------------------
  1635. $this->cached_vars['ajax_url'] = $this->base .
  1636. '&method=migrate_collections_ajax' .
  1637. '&migrate_empty_fields=' . $migrate_empty_fields .
  1638. '&migrate_attachments=' . $migrate_attachments .
  1639. '&collections=' .
  1640. urlencode( $this->cached_vars['collections'] ) .
  1641. '&total=' .
  1642. $this->cached_vars['total'] .
  1643. '&batch=0';
  1644. //--------------------------------------
  1645. // images
  1646. //--------------------------------------
  1647. //Success image
  1648. $this->cached_vars['success_png_url'] = $this->sc->addon_theme_url . 'images/success.png';
  1649. // Error image
  1650. $this->cached_vars['error_png_url'] = $this->sc->addon_theme_url . 'images/exclamation.png';
  1651. //--------------------------------------
  1652. // Javascript
  1653. //--------------------------------------
  1654. $this->cached_vars['cp_javascript'][] = 'migrate';
  1655. ee()->cp->add_js_script(
  1656. array('ui' => array('core', 'progressbar'))
  1657. );
  1658. //--------------------------------------
  1659. // Load page
  1660. //--------------------------------------
  1661. $this->cached_vars['current_page'] = $this->view(
  1662. 'migrate.html',
  1663. NULL,
  1664. TRUE
  1665. );
  1666. return $this->ee_cp_view('index.html');
  1667. }
  1668. // End migrate collections
  1669. // --------------------------------------------------------------------
  1670. /**
  1671. * migrate collections ajax
  1672. *
  1673. * @access public
  1674. * @return string
  1675. */
  1676. public function migrate_collections_ajax()
  1677. {
  1678. $upper_limit = 9999;
  1679. //--------------------------------------
  1680. // Base output
  1681. //--------------------------------------
  1682. $out = array(
  1683. 'done' => FALSE,
  1684. 'batch' => ee()->input->get('batch'),
  1685. 'total' => ee()->input->get('total')
  1686. );
  1687. //--------------------------------------
  1688. // Libraries
  1689. //--------------------------------------
  1690. ee()->load->library('freeform_migration');
  1691. //--------------------------------------
  1692. // Validate
  1693. //--------------------------------------
  1694. $collections = ee()->input->get('collections');
  1695. if ( empty( $collections ) OR
  1696. ee()->input->get('batch') === FALSE )
  1697. {
  1698. $out['error'] = TRUE;
  1699. $out['errors'] = array( 'no_collections' => lang('no_collections') );
  1700. $this->send_ajax_response($out);
  1701. exit();
  1702. }
  1703. //--------------------------------------
  1704. // Done?
  1705. //--------------------------------------
  1706. if ( ee()->input->get('batch') !== FALSE AND
  1707. ee()->input->get('total') !== FALSE AND
  1708. ee()->input->get('batch') >= ee()->input->get('total') )
  1709. {
  1710. $out['done'] = TRUE;
  1711. $this->send_ajax_response($out);
  1712. exit();
  1713. }
  1714. //--------------------------------------
  1715. // Anything?
  1716. //--------------------------------------
  1717. $collections = $this->actions()->pipe_split(
  1718. urldecode(
  1719. ee()->input->get('collections')
  1720. )
  1721. );
  1722. $counts = ee()->freeform_migration->get_collection_counts($collections);
  1723. if (empty($counts))
  1724. {
  1725. $out['error'] = TRUE;
  1726. $out['errors'] = array( 'no_collections' => lang('no_collections') );
  1727. $this->send_ajax_response($out);
  1728. exit();
  1729. }
  1730. //--------------------------------------
  1731. // Do any of the submitted collections have unmigrated entries?
  1732. //--------------------------------------
  1733. $migrate = FALSE;
  1734. foreach ( $counts as $form_name => $val )
  1735. {
  1736. if ( ! empty( $val['unmigrated'] ) )
  1737. {
  1738. $migrate = TRUE;
  1739. }
  1740. }
  1741. if ( empty( $migrate ) )
  1742. {
  1743. $out['done'] = TRUE;
  1744. $this->send_ajax_response($out);
  1745. exit();
  1746. }
  1747. //--------------------------------------
  1748. // Master arrays
  1749. //--------------------------------------
  1750. $forms = array();
  1751. $form_fields = array();
  1752. //--------------------------------------
  1753. // Loop and process
  1754. //--------------------------------------
  1755. foreach ( $counts as $form_name => $val )
  1756. {
  1757. //--------------------------------------
  1758. // For each collection, create a form
  1759. //--------------------------------------
  1760. $form_data = ee()->freeform_migration->create_form($form_name);
  1761. if ( $form_data !== FALSE )
  1762. {
  1763. $forms[ $form_data['form_name'] ]['form_id'] = $form_data['form_id'];
  1764. $forms[ $form_data['form_name'] ]['form_label'] = $form_data['form_label'];
  1765. }
  1766. else
  1767. {
  1768. $errors = ee()->freeform_migration->get_errors();
  1769. if ( $errors !== FALSE )
  1770. {
  1771. $out['error'] = TRUE;
  1772. $out['errors'] = $errors;
  1773. $this->send_ajax_response($out);
  1774. exit();
  1775. }
  1776. }
  1777. //--------------------------------------
  1778. // For each collection, determine fields
  1779. //--------------------------------------
  1780. $migrate_empty_fields = (
  1781. $this->check_yes(ee()->input->get('migrate_empty_fields'))
  1782. ) ? 'y': 'n';
  1783. $fields = ee()->freeform_migration->get_fields_for_collection(
  1784. $form_name,
  1785. $migrate_empty_fields
  1786. );
  1787. if ($fields !== FALSE)
  1788. {
  1789. $form_fields[ $form_name ]['fields'] = $fields;
  1790. }
  1791. else
  1792. {
  1793. $errors = ee()->freeform_migration->get_errors();
  1794. if ($errors !== FALSE)
  1795. {
  1796. $out['error'] = TRUE;
  1797. $out['errors'] = $errors;
  1798. $this->send_ajax_response($out);
  1799. exit();
  1800. }
  1801. }
  1802. //--------------------------------------
  1803. // For each collection, create necessary fields if they don't yet exist.
  1804. //--------------------------------------
  1805. $created_field_ids = array();
  1806. if ( ! empty( $form_fields[$form_name]['fields'] ) )
  1807. {
  1808. foreach ( $form_fields[$form_name]['fields'] as $name => $attr )
  1809. {
  1810. $field_id = ee()->freeform_migration->create_field(
  1811. $forms[$form_name]['form_id'],
  1812. $name,
  1813. $attr
  1814. );
  1815. if ($field_id !== FALSE )
  1816. {
  1817. $created_field_ids[] = $field_id;
  1818. }
  1819. else
  1820. {
  1821. $errors = ee()->freeform_migration->get_errors();
  1822. if ( $errors !== FALSE )
  1823. {
  1824. $out['error'] = TRUE;
  1825. $out['errors'] = $errors;
  1826. $this->send_ajax_response($out);
  1827. exit();
  1828. }
  1829. }
  1830. }
  1831. }
  1832. //--------------------------------------
  1833. // For each collection, create upload fields if needed.
  1834. //--------------------------------------
  1835. $attachment_profiles = ee()->freeform_migration->get_attachment_profiles( $form_name );
  1836. if ($this->check_yes( ee()->input->get('migrate_attachments') ) AND
  1837. $attachment_profiles !== FALSE )
  1838. {
  1839. foreach ( $attachment_profiles as $row )
  1840. {
  1841. $field_id = ee()->freeform_migration->create_upload_field(
  1842. $forms[ $form_name ]['form_id'],
  1843. $row['name'],
  1844. $row
  1845. );
  1846. if ($field_id !== FALSE)
  1847. {
  1848. $created_field_ids[] = $field_id;
  1849. $upload_pref_id_map[ $row['pref_id'] ] = array(
  1850. 'field_id' => $field_id,
  1851. 'field_name' => $row['name']
  1852. );
  1853. }
  1854. else
  1855. {
  1856. $errors = ee()->freeform_migration->get_errors();
  1857. if ( $errors !== FALSE )
  1858. {
  1859. $out['error'] = TRUE;
  1860. $out['errors'] = $errors;
  1861. $this->send_ajax_response($out);
  1862. exit();
  1863. }
  1864. }
  1865. }
  1866. if ( ! empty( $upload_pref_id_map ) )
  1867. {
  1868. ee()->freeform_migration->set_property(
  1869. 'upload_pref_id_map',
  1870. $upload_pref_id_map
  1871. );
  1872. }
  1873. }
  1874. //--------------------------------------
  1875. // Assign the fields to our form.
  1876. //--------------------------------------
  1877. ee()->freeform_migration->assign_fields_to_form(
  1878. $forms[ $form_data['form_name'] ]['form_id'],
  1879. $created_field_ids
  1880. );
  1881. //--------------------------------------
  1882. // Safeguard?
  1883. //--------------------------------------
  1884. if ( ee()->input->get('batch') == $upper_limit )
  1885. {
  1886. $this->send_ajax_response(array('done' => TRUE ));
  1887. exit();
  1888. }
  1889. //--------------------------------------
  1890. // Pass attachments pref
  1891. //--------------------------------------
  1892. if ($this->check_yes( ee()->input->get('migrate_attachments') ) )
  1893. {
  1894. ee()->freeform_migration->set_property('migrate_attachments', TRUE);
  1895. }
  1896. //--------------------------------------
  1897. // Grab entries
  1898. //--------------------------------------
  1899. ee()->freeform_migration->set_property(
  1900. 'batch_limit',
  1901. $this->migration_batch_limit
  1902. );
  1903. $entries = ee()->freeform_migration->get_legacy_entries($form_name);
  1904. if ( $entries !== FALSE )
  1905. {
  1906. foreach ( $entries as $entry )
  1907. {
  1908. //--------------------------------------
  1909. // Insert
  1910. //--------------------------------------
  1911. $entry_id = ee()->freeform_migration->set_legacy_entry(
  1912. $forms[ $form_name ]['form_id'],
  1913. $entry
  1914. );
  1915. if ( $entry_id === FALSE )
  1916. {
  1917. $errors = ee()->freeform_migration->get_errors();
  1918. if ( $errors !== FALSE )
  1919. {
  1920. $out['error'] = TRUE;
  1921. $out['errors'] = $errors;
  1922. $this->send_ajax_response($out);
  1923. exit();
  1924. }
  1925. }
  1926. else
  1927. {
  1928. $out['batch'] = $out['batch'] + 1;
  1929. }
  1930. }
  1931. }
  1932. }
  1933. //--------------------------------------
  1934. // Are we done?
  1935. //--------------------------------------
  1936. $this->send_ajax_response($out);
  1937. exit();
  1938. }
  1939. // End migrate collections ajax
  1940. // --------------------------------------------------------------------
  1941. /**
  1942. * moderate entries
  1943. *
  1944. * almost the same as entries but with some small modifications
  1945. *
  1946. * @access public
  1947. * @param string message lang line
  1948. * @return string html output
  1949. */
  1950. public function moderate_entries ( $message = '' )
  1951. {
  1952. return $this->entries($message, TRUE);
  1953. }
  1954. //END moderate_entries
  1955. // --------------------------------------------------------------------
  1956. /**
  1957. * Action from submitted entries links
  1958. *
  1959. * @access public
  1960. * @return string html output
  1961. */
  1962. public function entries_action ()
  1963. {
  1964. $action = ee()->input->get_post('submit_action');
  1965. if ($action == 'approve')
  1966. {
  1967. return $this->approve_entries();
  1968. }
  1969. else if ($action == 'delete')
  1970. {
  1971. return $this->delete_confirm_entries();
  1972. }
  1973. /*
  1974. else if ($action == 'edit')
  1975. {
  1976. $this->edit_entries();
  1977. }
  1978. */
  1979. }
  1980. //END entries_action
  1981. // --------------------------------------------------------------------
  1982. /**
  1983. * [edit_entry description]
  1984. *
  1985. * @access public
  1986. * @return string parsed html
  1987. */
  1988. public function edit_entry ($message = '')
  1989. {
  1990. // -------------------------------------
  1991. // Messages
  1992. // -------------------------------------
  1993. if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  1994. {
  1995. $message = lang(ee()->input->get('msg'));
  1996. }
  1997. $this->cached_vars['message'] = $message;
  1998. // -------------------------------------
  1999. // edit?
  2000. // -------------------------------------
  2001. $form_id = $this->get_post_or_zero('form_id');
  2002. $entry_id = $this->get_post_or_zero('entry_id');
  2003. ee()->load->model('freeform_form_model');
  2004. ee()->load->model('freeform_entry_model');
  2005. $form_data = $this->data->get_form_info($form_id);
  2006. if ( ! $form_data)
  2007. {
  2008. return $this->actions()->full_stop(lang('invalid_form_id'));
  2009. }
  2010. $entry_data = array();
  2011. $edit = FALSE;
  2012. if ($entry_id > 0)
  2013. {
  2014. $entry_data = ee()->freeform_entry_model
  2015. ->id($form_id)
  2016. ->where('entry_id', $entry_id)
  2017. ->get_row();
  2018. if ($entry_data == FALSE)
  2019. {
  2020. return $this->actions()->full_stop(lang('invalid_entry_id'));
  2021. }
  2022. $edit = TRUE;
  2023. }
  2024. //--------------------------------------------
  2025. // Crumbs and tab highlight
  2026. //--------------------------------------------
  2027. $this->add_crumb(
  2028. lang('forms'),
  2029. $this->mod_link(array('method' => 'forms'))
  2030. );
  2031. $this->add_crumb(
  2032. lang($edit ? 'edit_entry' : 'new_entry') . ': ' . $form_data['form_label']
  2033. );
  2034. $this->set_highlight('module_forms');
  2035. // -------------------------------------
  2036. // load the template library in case
  2037. // people get upset or something
  2038. // -------------------------------------
  2039. if ( ! isset($this->EE->TMPL) OR ! is_object($this->EE->TMPL))
  2040. {
  2041. $this->EE->load->library('template');
  2042. $globals['TMPL'] =& $this->EE->template;
  2043. $this->EE->TMPL =& $this->EE->template;
  2044. }
  2045. // -------------------------------------
  2046. // get fields
  2047. // -------------------------------------
  2048. ee()->load->library('freeform_fields');
  2049. $field_output_data = array();
  2050. $field_loop_ids = array_keys($form_data['fields']);
  2051. if ($form_data['field_order'] !== '')
  2052. {
  2053. $order_ids = $this->actions()->pipe_split($form_data['field_order']);
  2054. if ( ! empty($order_ids))
  2055. {
  2056. //this makes sure that any fields in 'fields' are in the
  2057. //order set as well. Will add missing at the end like this
  2058. $field_loop_ids = array_merge(
  2059. $order_ids,
  2060. array_diff($field_loop_ids, $order_ids)
  2061. );
  2062. }
  2063. }
  2064. foreach ($field_loop_ids as $field_id)
  2065. {
  2066. if ( ! isset($form_data['fields'][$field_id]))
  2067. {
  2068. continue;
  2069. }
  2070. $f_data = $form_data['fields'][$field_id];
  2071. $instance =& ee()->freeform_fields->get_field_instance(array(
  2072. 'field_id' => $field_id,
  2073. 'field_data' => $f_data,
  2074. 'form_id' => $form_id,
  2075. 'entry_id' => $entry_id,
  2076. 'edit' => $edit,
  2077. 'extra_settings' => array(
  2078. 'entry_id' => $entry_id
  2079. )
  2080. ));
  2081. $column_name = ee()->freeform_entry_model->form_field_prefix . $field_id;
  2082. $display_field_data = '';
  2083. if ($edit)
  2084. {
  2085. if (isset($entry_data[$column_name]))
  2086. {
  2087. $display_field_data = $entry_data[$column_name];
  2088. }
  2089. else if (isset($entry_data[$f_data['field_name']]))
  2090. {
  2091. $display_field_data = $entry_data[$f_data['field_name']];
  2092. }
  2093. }
  2094. $field_output_data[$field_id] = array(
  2095. 'field_display' => $instance->display_edit_cp($display_field_data),
  2096. 'field_label' => $f_data['field_label'],
  2097. 'field_description' => $f_data['field_description']
  2098. );
  2099. }
  2100. $entry_data['screen_name'] = '';
  2101. $entry_data['group_title'] = '';
  2102. if (!empty($entry_data['author_id']))
  2103. {
  2104. $member_data = ee()->db
  2105. ->select('group_title, screen_name')
  2106. ->from('members')
  2107. ->join('member_groups', 'members.group_id = member_groups.group_id', 'left')
  2108. ->where('member_id', $entry_data['author_id'])
  2109. ->limit(1)
  2110. ->get();
  2111. if ($member_data->num_rows() > 0)
  2112. {
  2113. $entry_data['screen_name'] = $member_data->row('screen_name');
  2114. $entry_data['group_title'] = $member_data->row('group_title');
  2115. }
  2116. }
  2117. if ($entry_data['screen_name'] == '')
  2118. {
  2119. $entry_data['screen_name'] = lang('guest');
  2120. $entry_data['group_title'] = lang('guest');
  2121. $guest_data = ee()->db
  2122. ->select('group_title')
  2123. ->from('member_groups')
  2124. ->where('group_id', 3)
  2125. ->limit(1)
  2126. ->get();
  2127. if ($guest_data->num_rows() > 0)
  2128. {
  2129. $entry_data['group_title'] = $guest_data->row('group_title');
  2130. }
  2131. }
  2132. $entry_data['entry_date'] = ( ! empty( $entry_data['entry_date'] ) ) ?
  2133. $entry_data['entry_date'] :
  2134. ee()->localize->now;
  2135. $entry_data['entry_date'] = gmdate(
  2136. $this->preference('cp_date_formatting'),
  2137. ee()->localize->set_localized_time($entry_data['entry_date'])
  2138. );
  2139. $entry_data['edit_date'] = (!empty($entry_data['edit_date'])) ?
  2140. gmdate(
  2141. $this->preference('cp_date_formatting'),
  2142. ee()->localize->set_localized_time($entry_data['edit_date'])
  2143. ) :
  2144. lang('n_a');
  2145. $this->cached_vars['entry_data'] = $entry_data;
  2146. $this->cached_vars['field_output_data'] = $field_output_data;
  2147. $this->cached_vars['form_uri'] = $this->mod_link(array(
  2148. 'method' => 'save_entry'
  2149. ));
  2150. $this->cached_vars['form_id'] = $form_id;
  2151. $this->cached_vars['entry_id'] = $entry_id;
  2152. $this->cached_vars['statuses'] = $this->data->get_form_statuses();
  2153. // --------------------------------------------
  2154. // Load page
  2155. // --------------------------------------------
  2156. $this->load_fancybox();
  2157. $this->cached_vars['cp_javascript'][] = 'jquery.smooth-scroll.min';
  2158. $this->cached_vars['current_page'] = $this->view(
  2159. 'edit_entry.html',
  2160. NULL,
  2161. TRUE
  2162. );
  2163. return $this->ee_cp_view('index.html');
  2164. }
  2165. //END edit_entry
  2166. // --------------------------------------------------------------------
  2167. /**
  2168. * fields
  2169. *
  2170. * @access public
  2171. * @param string message
  2172. * @return string
  2173. */
  2174. public function fields ( $message = '' )
  2175. {
  2176. // -------------------------------------
  2177. // Messages
  2178. // -------------------------------------
  2179. if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  2180. {
  2181. $message = lang(ee()->input->get('msg'));
  2182. }
  2183. $this->cached_vars['message'] = $message;
  2184. //--------------------------------------------
  2185. // Crumbs and tab highlight
  2186. //--------------------------------------------
  2187. $this->cached_vars['new_field_link'] = $this->mod_link(array(
  2188. 'method' => 'edit_field'
  2189. ));
  2190. $this->add_crumb( lang('fields') );
  2191. //optional
  2192. $this->freeform_add_right_link(lang('new_field'), $this->cached_vars['new_field_link']);
  2193. $this->set_highlight('module_fields');
  2194. // -------------------------------------
  2195. // data
  2196. // -------------------------------------
  2197. $row_limit = $this->data->defaults['mcp_row_limit'];
  2198. $paginate = '';
  2199. $row_count = 0;
  2200. $field_data = array();
  2201. $paginate_url = array('method' => 'fields');
  2202. ee()->load->model('freeform_field_model');
  2203. ee()->freeform_field_model->select(
  2204. 'field_label,
  2205. field_name,
  2206. field_type,
  2207. field_id,
  2208. field_description'
  2209. );
  2210. // -------------------------------------
  2211. // search?
  2212. // -------------------------------------
  2213. $field_search = '';
  2214. $field_search_on = '';
  2215. $search = ee()->input->get_post('field_search', TRUE);
  2216. if ($search)
  2217. {
  2218. $f_search_on = ee()->input->get_post('field_search_on');
  2219. if ($f_search_on == 'field_name')
  2220. {
  2221. ee()->freeform_field_model->like('field_name', $search);
  2222. $field_search_on = 'field_name';
  2223. }
  2224. else if ($f_search_on == 'field_label')
  2225. {
  2226. ee()->freeform_field_model->like('field_label', $search);
  2227. $field_search_on = 'field_label';
  2228. }
  2229. else if ($f_search_on == 'field_description')
  2230. {
  2231. ee()->freeform_field_model->like('field_description', $search);
  2232. $field_search_on = 'field_description';
  2233. }
  2234. else //if ($f_search_on == 'all')
  2235. {
  2236. ee()->freeform_field_model->like('field_name', $search);
  2237. ee()->freeform_field_model->or_like('field_label', $search);
  2238. ee()->freeform_field_model->or_like('field_description', $search);
  2239. $field_search_on = 'all';
  2240. }
  2241. $field_search = $search;
  2242. $paginate_url['field_search'] = $search;
  2243. $paginate_url['field_search_on'] = $field_search_on;
  2244. }
  2245. $this->cached_vars['field_search'] = $field_search;
  2246. $this->cached_vars['field_search_on'] = $field_search_on;
  2247. // -------------------------------------
  2248. // all sites?
  2249. // -------------------------------------
  2250. if ( ! $this->data->show_all_sites())
  2251. {
  2252. ee()->freeform_field_model->where(
  2253. 'site_id',
  2254. ee()->config->item('site_id')
  2255. );
  2256. }
  2257. ee()->freeform_field_model->order_by('field_name', 'asc');
  2258. $total_results = ee()->freeform_field_model->count(array(), FALSE);
  2259. $row_count = 0;
  2260. // do we need pagination?
  2261. if ( $total_results > $row_limit )
  2262. {
  2263. $row_count = $this->get_post_or_zero('row');
  2264. //get pagination info
  2265. $pagination_data = $this->universal_pagination(array(
  2266. 'total_results' => $total_results,
  2267. 'limit' => $row_limit,
  2268. 'current_page' => $row_count,
  2269. 'pagination_config' => array('base_url' => $this->mod_link($paginate_url)),
  2270. 'query_string_segment' => 'row'
  2271. ));
  2272. $paginate = $pagination_data['pagination_links'];
  2273. ee()->freeform_field_model->limit(
  2274. $row_limit,
  2275. $pagination_data['pagination_page']
  2276. );
  2277. }
  2278. $query = ee()->freeform_field_model->get();
  2279. $count = $row_count;
  2280. if ($query !== FALSE)
  2281. {
  2282. foreach ($query as $row)
  2283. {
  2284. $row['count'] = ++$count;
  2285. $row['field_edit_link'] = $this->mod_link(array(
  2286. 'method' => 'edit_field',
  2287. 'field_id' => $row['field_id']
  2288. ));
  2289. $row['field_duplicate_link'] = $this->mod_link(array(
  2290. 'method' => 'edit_field',
  2291. 'duplicate_id' => $row['field_id']
  2292. ));
  2293. $row['field_delete_link'] = $this->mod_link(array(
  2294. 'method' => 'delete_confirm_fields',
  2295. 'field_id' => $row['field_id']
  2296. ));
  2297. $field_data[] = $row;
  2298. }
  2299. }
  2300. $this->cached_vars['field_data'] = $field_data;
  2301. $this->cached_vars['paginate'] = $paginate;
  2302. // -------------------------------------
  2303. // ajax?
  2304. // -------------------------------------
  2305. if ($this->is_ajax_request())
  2306. {
  2307. return $this->send_ajax_response(array(
  2308. 'success' => TRUE,
  2309. 'field_data' => $field_data,
  2310. 'paginate' => $paginate
  2311. ));
  2312. }
  2313. // ----------------------------------------
  2314. // Load vars
  2315. // ----------------------------------------
  2316. $this->cached_vars['form_uri'] = $this->mod_link(array(
  2317. 'method' => 'delete_confirm_fields'
  2318. ));
  2319. // -------------------------------------
  2320. // js
  2321. // -------------------------------------
  2322. ee()->cp->add_js_script(array(
  2323. 'file' => array('underscore', 'json2')
  2324. ));
  2325. // --------------------------------------------
  2326. // Load page
  2327. // --------------------------------------------
  2328. $this->cached_vars['current_page'] = $this->view(
  2329. 'fields.html',
  2330. NULL,
  2331. TRUE
  2332. );
  2333. return $this->ee_cp_view('index.html');
  2334. }
  2335. //END fields
  2336. // --------------------------------------------------------------------
  2337. /**
  2338. * Edit Field
  2339. *
  2340. * @access public
  2341. * @param bool $modal is this a modal version?
  2342. * @return string
  2343. */
  2344. public function edit_field ($modal = FALSE)
  2345. {
  2346. // -------------------------------------
  2347. // field ID? we must be editing
  2348. // -------------------------------------
  2349. $field_id = $this->get_post_or_zero('field_id');
  2350. $update = ($field_id != 0);
  2351. $modal = ( ! $modal AND $this->check_yes(ee()->input->get_post('modal'))) ? TRUE : $modal;
  2352. $this->cached_vars['modal'] = $modal;
  2353. //--------------------------------------------
  2354. // Crumbs and tab highlight
  2355. //--------------------------------------------
  2356. $this->add_crumb( lang('fields') , $this->base . AMP . 'method=fields');
  2357. $this->add_crumb( lang(($update ? 'update_field' : 'new_field')) );
  2358. //optional
  2359. //$this->freeform_add_right_link(lang('home'), $this->base . AMP . 'method=some_method');
  2360. $this->set_highlight('module_fields');
  2361. // -------------------------------------
  2362. // default values
  2363. // -------------------------------------
  2364. $inputs = array(
  2365. 'field_id' => '0',
  2366. 'field_name' => '',
  2367. 'field_label' => '',
  2368. 'field_order' => '0',
  2369. 'field_type' => $this->data->defaults['field_type'],
  2370. 'field_length' => $this->data->defaults['field_length'],
  2371. 'field_description' => '',
  2372. 'submissions_page' => 'y',
  2373. 'moderation_page' => 'y',
  2374. 'composer_use' => 'y',
  2375. );
  2376. // -------------------------------------
  2377. // defaults
  2378. // -------------------------------------
  2379. $this->cached_vars['edit_warning'] = FALSE;
  2380. $field_in_forms = array();
  2381. $incoming_settings = FALSE;
  2382. if ($update)
  2383. {
  2384. $field_data = $this->data->get_field_info_by_id($field_id);
  2385. if (empty($field_data))
  2386. {
  2387. $this->actions()->full_stop(lang('invalid_field_id'));
  2388. }
  2389. foreach ($field_data as $key => $value)
  2390. {
  2391. $inputs[$key] = $value;
  2392. }
  2393. // -------------------------------------
  2394. // is this change going to affect any
  2395. // forms that use this field?
  2396. // -------------------------------------
  2397. $form_info = $this->data->get_form_info_by_field_id($field_id);
  2398. if ($form_info AND ! empty($form_info))
  2399. {
  2400. $this->cached_vars['edit_warning'] = TRUE;
  2401. $form_names = array();
  2402. foreach ($form_info as $row)
  2403. {
  2404. $field_in_forms[] = $row['form_id'];
  2405. $form_names[] = $row['form_label'];
  2406. }
  2407. $this->cached_vars['lang_field_edit_warning'] = lang('field_edit_warning');
  2408. $this->cached_vars['lang_field_edit_warning_desc'] = str_replace(
  2409. '%form_names%',
  2410. implode(', ', $form_names),
  2411. lang('field_edit_warning_desc')
  2412. );
  2413. }
  2414. }
  2415. $this->cached_vars['form_ids'] = implode('|', array_unique($field_in_forms));
  2416. // -------------------------------------
  2417. // duplicating?
  2418. // -------------------------------------
  2419. $duplicate_id = $this->get_post_or_zero('duplicate_id');
  2420. $duplicate = FALSE;
  2421. $this->cached_vars['duplicated'] = FALSE;
  2422. if ( ! $update AND $duplicate_id > 0)
  2423. {
  2424. $field_data = $this->data->get_field_info_by_id($duplicate_id);
  2425. if ($field_data)
  2426. {
  2427. $duplicate = TRUE;
  2428. foreach ($field_data as $key => $value)
  2429. {
  2430. if (in_array($key, array('field_id', 'field_label', 'field_name')))
  2431. {
  2432. continue;
  2433. }
  2434. $inputs[$key] = $value;
  2435. }
  2436. $this->cached_vars['duplicated'] = TRUE;
  2437. $this->cached_vars['duplicated_from'] = $field_data['field_label'];
  2438. }
  2439. }
  2440. // -------------------------------------
  2441. // get available field types
  2442. // -------------------------------------
  2443. ee()->load->library('freeform_fields');
  2444. $this->cached_vars['fieldtypes'] = ee()->freeform_fields->get_available_fieldtypes();
  2445. // -------------------------------------
  2446. // get available forms to add this to
  2447. // -------------------------------------
  2448. if ( ! $this->data->show_all_sites())
  2449. {
  2450. ee()->db->where('site_id', ee()->config->item('site_id'));
  2451. }
  2452. $this->cached_vars['available_forms'] = $this->prepare_keyed_result(
  2453. ee()->db->get('freeform_forms'),
  2454. 'form_id'
  2455. );
  2456. // -------------------------------------
  2457. // add desc and lang for field types
  2458. // -------------------------------------
  2459. foreach($this->cached_vars['fieldtypes'] as $fieldtype => $field_data)
  2460. {
  2461. //settings?
  2462. $settings = (
  2463. ($update OR $duplicate ) AND
  2464. $inputs['field_type'] == $fieldtype AND
  2465. isset($inputs['settings'])
  2466. ) ? json_decode($inputs['settings'], TRUE) : array();
  2467. //we are encoding this and decoding in JS because leaving the
  2468. //fields intact while in storage in the html makes dupes of fields.
  2469. //I could do some html moving around, but that would keep running
  2470. //individual settings JS over and over again when it should
  2471. //only be run on display.
  2472. $this->cached_vars['fieldtypes'][$fieldtype]['settings_output'] = base64_encode(
  2473. ee()->freeform_fields->fieldtype_display_settings_output(
  2474. $fieldtype,
  2475. $settings
  2476. )
  2477. );
  2478. }
  2479. if (isset($inputs['field_label']))
  2480. {
  2481. $inputs['field_label'] = form_prep($inputs['field_label']);
  2482. }
  2483. if (isset($inputs['field_description']))
  2484. {
  2485. $inputs['field_description'] = form_prep($inputs['field_description']);
  2486. }
  2487. // -------------------------------------
  2488. // load inputs
  2489. // -------------------------------------
  2490. foreach ($inputs as $key => $value)
  2491. {
  2492. $this->cached_vars[$key] = $value;
  2493. }
  2494. $this->cached_vars['form_uri'] = $this->mod_link(array(
  2495. 'method' => 'save_field'
  2496. ));
  2497. //----------------------------------------
  2498. // Load vars
  2499. //----------------------------------------
  2500. $this->cached_vars['lang_submit_word'] = lang(
  2501. ($update ? 'update_field' : 'create_field')
  2502. );
  2503. $this->cached_vars['prohibited_field_names'] = $this->data->prohibited_names;
  2504. // --------------------------------------------
  2505. // Load page
  2506. // --------------------------------------------
  2507. $this->cached_vars['cp_javascript'][] = 'edit_field_cp.min';
  2508. $this->cached_vars['current_page'] = $this->view(
  2509. 'edit_field.html',
  2510. NULL,
  2511. TRUE
  2512. );
  2513. if ($modal)
  2514. {
  2515. exit($this->cached_vars['current_page']);
  2516. }
  2517. $this->load_fancybox();
  2518. $this->cached_vars['cp_javascript'][] = 'jquery.smooth-scroll.min';
  2519. return $this->ee_cp_view('index.html');
  2520. }
  2521. //END edit_field
  2522. // --------------------------------------------------------------------
  2523. /**
  2524. * Field Types
  2525. *
  2526. * @access public
  2527. * @param message
  2528. * @return string
  2529. */
  2530. public function fieldtypes ( $message = '' )
  2531. {
  2532. // -------------------------------------
  2533. // Messages
  2534. // -------------------------------------
  2535. if ($message == '' AND ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  2536. {
  2537. $message = lang(ee()->input->get('msg'));
  2538. }
  2539. $this->cached_vars['message'] = $message;
  2540. //--------------------------------------------
  2541. // Crumbs and tab highlight
  2542. //--------------------------------------------
  2543. $this->add_crumb( lang('fieldtypes') );
  2544. $this->set_highlight('module_fieldtypes');
  2545. //--------------------------------------
  2546. // start vars
  2547. //--------------------------------------
  2548. $fieldtypes = array();
  2549. ee()->load->library('freeform_fields');
  2550. ee()->load->model('freeform_field_model');
  2551. ee()->load->model('freeform_fieldtype_model');
  2552. if ( ( $installed_fieldtypes = ee()->freeform_fieldtype_model->installed_fieldtypes() ) === FALSE )
  2553. {
  2554. $installed_fieldtypes = array();
  2555. }
  2556. $fieldtypes = ee()->freeform_fields->get_installable_fieldtypes();
  2557. // -------------------------------------
  2558. // missing fieldtype folders?
  2559. // -------------------------------------
  2560. $missing_fieldtypes = array();
  2561. foreach ($installed_fieldtypes as $name => $version)
  2562. {
  2563. if ( ! array_key_exists($name, $fieldtypes))
  2564. {
  2565. $missing_fieldtypes[] = $name;
  2566. }
  2567. }
  2568. // -------------------------------------
  2569. // add urls and crap
  2570. // -------------------------------------
  2571. $action_url = $this->base . AMP . 'method=freeform_fieldtype_action';
  2572. foreach ($fieldtypes as $name => $data)
  2573. {
  2574. $fieldtypes[$name]['installed_lang'] = lang($data['installed'] ? 'installed' : 'not_installed');
  2575. $action = ($data['installed'] ? 'uninstall' : 'install');
  2576. $fieldtypes[$name]['action_lang'] = lang($action);
  2577. $fieldtypes[$name]['action_url'] = $this->mod_link(array(
  2578. 'method' => 'freeform_fieldtype_action',
  2579. 'name' => $name,
  2580. 'action' => $action,
  2581. //some other time -gf
  2582. //'folder' => base64_encode($data['folder'])
  2583. ));
  2584. }
  2585. $this->cached_vars['fieldtypes'] = $fieldtypes;
  2586. $this->cached_vars['freeform_ft_docs_url'] = $this->data->doc_links['custom_fields'];
  2587. // --------------------------------------------
  2588. // Load page
  2589. // --------------------------------------------
  2590. $this->cached_vars['current_page'] = $this->view(
  2591. 'fieldtypes.html',
  2592. NULL,
  2593. TRUE
  2594. );
  2595. return $this->ee_cp_view('index.html');
  2596. }
  2597. //END field_types
  2598. // --------------------------------------------------------------------
  2599. /**
  2600. * notifications
  2601. *
  2602. * @access public
  2603. * @param string message to output
  2604. * @return string outputted template
  2605. */
  2606. public function notifications ( $message = '' )
  2607. {
  2608. // -------------------------------------
  2609. // Messages
  2610. // -------------------------------------
  2611. if ($message == '' AND
  2612. ! in_array(ee()->input->get('msg'), array(FALSE, '')) )
  2613. {
  2614. $message = lang(ee()->input->get('msg'));
  2615. }
  2616. $this->cached_vars['message'] = $message;
  2617. //--------------------------------------------
  2618. // Crumbs and tab highlight
  2619. //--------------------------------------------
  2620. $this->cached_vars['new_notification_link'] = $this->mod_link(array(
  2621. 'method' => 'edit_notification'
  2622. ));
  2623. $this->add_crumb( lang('notifications') );
  2624. $this->freeform_add_right_link(
  2625. lang('new_notification'),
  2626. $this->cached_vars['new_notification_link']
  2627. );
  2628. $this->set_highlight('module_notifications');
  2629. // -------------------------------------
  2630. // data
  2631. // -------------------------------------
  2632. $row_limit = $this->data->defaults['mcp_row_limit'];
  2633. $paginate = '';
  2634. $row_count = 0;
  2635. $notification_data = array();
  2636. ee()->db->start_cache();
  2637. ee()->db->order_by('notification_name', 'asc');
  2638. if ( ! $this->data->show_all_sites())
  2639. {
  2640. ee()->db->where('site_id', ee()->config->item('site_id'));
  2641. }
  2642. ee()->db->from('freeform_notification_templates');
  2643. ee()->db->stop_cache();
  2644. $total_results = ee()->db->count_all_results();
  2645. // do we need pagination?
  2646. if ( $total_results > $row_limit )
  2647. {
  2648. $row_count = $this->get_post_or_zero('row');
  2649. $url = $this->mod_link(array(
  2650. 'method' => 'notifications'
  2651. ));
  2652. //get pagination info
  2653. $pagination_data = $this->universal_pagination(array(
  2654. 'total_results' => $total_results,
  2655. 'limit' => $row_limit,
  2656. 'current_page' => $row_count,
  2657. 'pagination_config' => array('base_url' => $url),
  2658. 'query_string_segment' => 'row'
  2659. ));
  2660. $paginate = $pagination_data['pagination_links'];
  2661. ee()->db->limit($row_limit, $pagination_data['pagination_page']);
  2662. }
  2663. $query = ee()->db->get();
  2664. ee()->db->flush_cache();
  2665. if ($query->num_rows() > 0)
  2666. {
  2667. foreach ($query->result_array() as $row)
  2668. {
  2669. $row['notification_edit_link'] = $this->mod_link(array(
  2670. 'method' => 'edit_notification',
  2671. 'notification_id' => $row['notification_id'],
  2672. ));
  2673. $row['notification_duplicate_link'] = $this->mod_link(array(
  2674. 'method' => 'edit_notification',
  2675. 'duplicate_id' => $row['notification_id'],
  2676. ));
  2677. $row['notification_delete_link'] = $this->mod_link(array(
  2678. 'method' => 'delete_confirm_notification',
  2679. 'notification_id' => $row['notification_id'],
  2680. ));
  2681. $notification_data[] = $row;
  2682. }
  2683. }
  2684. $this->cached_vars['notification_data'] = $notification_data;
  2685. $this->cached_vars['paginate'] = $paginate;
  2686. // ----------------------------------------
  2687. // Load vars
  2688. // ----------------------------------------
  2689. $this->cached_vars['form_uri'] = $this->mod_link(array(
  2690. 'method' => 'delete_confirm_notification'
  2691. ));
  2692. // --------------------------------------------
  2693. // Load page
  2694. // --------------------------------------------
  2695. $this->cached_vars['current_page'] = $this->view(
  2696. 'notifications.html',
  2697. NULL,
  2698. TRUE
  2699. );
  2700. return $this->ee_cp_view('index.html');
  2701. }
  2702. //END notifications
  2703. // --------------------------------------------------------------------
  2704. /**
  2705. * edit_notification
  2706. *
  2707. * @access public
  2708. * @return string
  2709. */
  2710. public function edit_notification ()
  2711. {
  2712. // -------------------------------------
  2713. // notification ID? we must be editing
  2714. // -------------------------------------
  2715. $notification_id = $this->get_post_or_zero('notification_id');
  2716. $update = ($notification_id != 0);
  2717. //--------------------------------------
  2718. // Title and Crumbs
  2719. //--------------------------------------
  2720. $this->add_crumb(
  2721. lang('notifications'),
  2722. $this->base . AMP . 'method=notifications'
  2723. );
  2724. $this->add_crumb(
  2725. lang(($update ? 'update_notification' : 'new_notification'))
  2726. );
  2727. $this->set_highlight('module_notifications');
  2728. // -------------------------------------
  2729. // data items
  2730. // -------------------------------------
  2731. $inputs = array(
  2732. 'notification_id' => '0',
  2733. 'notification_name' => '',
  2734. 'notification_label' => '',
  2735. 'notification_description' => '',
  2736. 'wordwrap' => $this->data->defaults['wordwrap'],
  2737. 'allow_html' => $this->data->defaults['allow_html'],
  2738. 'from_name' => form_prep(ee()->config->item('webmaster_name')),
  2739. 'from_email' => ee()->config->item('webmaster_email'),
  2740. 'reply_to_email' => ee()->config->item('webmaster_email'),
  2741. 'email_subject' => '',
  2742. 'template_data' => '',
  2743. 'include_attachments' => 'n'
  2744. );
  2745. // -------------------------------------
  2746. // defaults
  2747. // -------------------------------------
  2748. $this->cached_vars['edit_warning'] = FALSE;
  2749. if ($update)
  2750. {
  2751. $notification_data = $this->data->get_notification_info_by_id($notification_id);
  2752. foreach ($notification_data as $key => $value)
  2753. {
  2754. $inputs[$key] = form_prep($value);
  2755. }
  2756. // -------------------------------------
  2757. // is this change going to affect any
  2758. // forms that use this field?
  2759. // -------------------------------------
  2760. $form_info = $this->data->get_form_info_by_notification_id($notification_id);
  2761. if ($form_info AND ! empty($form_info))
  2762. {
  2763. $this->cached_vars['edit_warning'] = TRUE;
  2764. $form_names = array();
  2765. foreach ($form_info as $row)
  2766. {
  2767. $form_names[] = $row['form_label'];
  2768. }
  2769. $this->cached_vars['lang_notification_edit_warning'] = str_replace(
  2770. '%form_names%',
  2771. implode(', ', $form_names),
  2772. lang('notification_edit_warning')
  2773. );
  2774. }
  2775. }
  2776. // -------------------------------------
  2777. // duplicating?
  2778. // -------------------------------------
  2779. $duplicate_id = $this->get_post_or_zero('duplicate_id');
  2780. $this->cached_vars['duplicated'] = FALSE;
  2781. if ( ! $update AND $duplicate_id > 0)
  2782. {
  2783. $notification_data = $this->data->get_notification_info_by_id($duplicate_id);
  2784. if ($notification_data)
  2785. {
  2786. foreach ($notification_data as $key => $value)
  2787. {
  2788. //TODO: remove other items that dont need to be duped?
  2789. if (in_array($key, array(
  2790. 'notification_id',
  2791. 'notification_label',
  2792. 'notification_name'
  2793. )))
  2794. {
  2795. continue;
  2796. }
  2797. $inputs[$key] = form_prep($value);
  2798. }
  2799. $this->cached_vars['duplicated'] = TRUE;
  2800. $this->cached_vars['duplicated_from'] = $notification_data['notification_label'];
  2801. }
  2802. }
  2803. // -------------------------------------
  2804. // get available fields
  2805. // -------------------------------------
  2806. ee()->load->model('freeform_field_model');
  2807. $this->cached_vars['available_fields'] = array();
  2808. if ( ( $fields = ee()->freeform_field_model->get() ) !== FALSE )
  2809. {
  2810. $this->cached_vars['available_fields'] = $fields;
  2811. }
  2812. $this->cached_vars['standard_tags'] = $this->data->standard_notification_tags;
  2813. // -------------------------------------
  2814. // load inputs
  2815. // -------------------------------------
  2816. foreach ($inputs as $key => $value)
  2817. {
  2818. $this->cached_vars[$key] = $value;
  2819. }
  2820. $this->cached_vars['form_uri'] = $this->base . AMP .
  2821. 'method=save_notification';
  2822. // ----------------------------------------
  2823. // Load vars
  2824. // ----------------------------------------
  2825. $this->cached_vars['lang_submit_word'] = lang(
  2826. ($update ? 'update_notification' : 'create_notification')
  2827. );
  2828. $this->load_fancybox();
  2829. $this->cached_vars['cp_javascript'][] = 'jquery.smooth-scroll.min';
  2830. // --------------------------------------------
  2831. // Load page
  2832. // --------------------------------------------
  2833. $this->cached_vars['current_page'] = $this->view(
  2834. 'edit_notification.html',
  2835. NULL,
  2836. TRUE
  2837. );
  2838. return $this->ee_cp_view('index.html');
  2839. }
  2840. //END edit_notification
  2841. // --------------------------------------------------------------------
  2842. /**
  2843. * preferences
  2844. *
  2845. * @access public
  2846. * @return string
  2847. */
  2848. public function preferences ( $message = '' )
  2849. {
  2850. if ($message == '' AND ee()->input->get('msg') !== FALSE)
  2851. {
  2852. $message = lang(ee()->input->get('msg'));
  2853. }
  2854. $this->cached_vars['message'] = $message;
  2855. //--------------------------------------
  2856. // Title and Crumbs
  2857. //--------------------------------------
  2858. $this->add_crumb(lang('preferences'));
  2859. $this->set_highlight('module_preferences');
  2860. // -------------------------------------
  2861. // global prefs
  2862. // -------------------------------------
  2863. $this->cached_vars['msm_enabled'] = $msm_enabled = $this->data->msm_enabled;
  2864. $is_admin = (ee()->session->userdata('group_id') == 1);
  2865. $this->cached_vars['show_global_prefs'] = ($is_admin AND $msm_enabled);
  2866. $global_pref_data = array();
  2867. $global_prefs = $this->data->get_global_module_preferences();
  2868. $default_global_prefs = array_keys($this->data->default_global_preferences);
  2869. //dynamically get prefs and lang so we can just add them to defaults
  2870. foreach( $global_prefs as $key => $value )
  2871. {
  2872. //this skips things that don't need to be shown on this page
  2873. //so we can use the prefs table for other areas of the addon
  2874. if ( ! in_array($key, $default_global_prefs))
  2875. {
  2876. continue;
  2877. }
  2878. $pref = array();
  2879. $pref['name'] = $key;
  2880. $pref['lang_label'] = lang($key);
  2881. $key_desc = $key . '_desc';
  2882. $pref['lang_desc'] = (lang($key_desc) == $key_desc) ? '' : lang($key_desc);
  2883. $pref['value'] = form_prep($value);
  2884. $pref['type'] = $this->data->default_global_preferences[$key]['type'];
  2885. $global_pref_data[] = $pref;
  2886. }
  2887. $this->cached_vars['global_pref_data'] = $global_pref_data;
  2888. // -------------------------------------
  2889. // these two will only be used if MSM
  2890. // is enabled, but setting them
  2891. // anyway to avoid potential PHP errors
  2892. // -------------------------------------
  2893. $prefs_all_sites = ! $this->check_no(
  2894. $this->data->global_preference('prefs_all_sites')
  2895. );
  2896. $this->cached_vars['lang_site_prefs_for_site'] = (
  2897. lang('site_prefs_for') . ' ' . (
  2898. ($prefs_all_sites) ?
  2899. lang('all_sites') :
  2900. ee()->config->item('site_label')
  2901. )
  2902. );
  2903. //--------------------------------------
  2904. // per site prefs
  2905. //--------------------------------------
  2906. $pref_data = array();
  2907. $prefs = $this->data->get_module_preferences();
  2908. $default_prefs = array_keys($this->data->default_preferences);
  2909. //dynamically get prefs and lang so we can just add them to defaults
  2910. foreach( $prefs as $key => $value )
  2911. {
  2912. //this skips things that don't need to be shown on this page
  2913. //so we can use the prefs table for other areas of the addon
  2914. if ( ! in_array($key, $default_prefs))
  2915. {
  2916. continue;
  2917. }
  2918. //admin only pref?
  2919. //MSM pref and no MSM?
  2920. if (
  2921. (ee()->session->userdata('group_id') != 1 AND
  2922. in_array($key, $this->data->admin_only_prefs)) OR
  2923. ( ! $msm_enabled AND
  2924. in_array($key, $this->data->msm_only_prefs))
  2925. )
  2926. {
  2927. continue;
  2928. }
  2929. $pref = array();
  2930. $pref['name'] = $key;
  2931. $pref['lang_label'] = lang($key);
  2932. $key_desc = $key . '_desc';
  2933. $pref['lang_desc'] = (lang($key_desc) == $key_desc) ? '' : lang($key_desc);
  2934. $pref['value'] = form_prep($value);
  2935. $pref['type'] = $this->data->default_preferences[$key]['type'];
  2936. $pref_data[] = $pref;
  2937. }
  2938. $this->cached_vars['pref_data'] = $pref_data;
  2939. // ----------------------------------------
  2940. // Load vars
  2941. // ----------------------------------------
  2942. $this->cached_vars['form_uri'] = $this->mod_link(array(
  2943. 'method' => 'save_preferences'
  2944. ));
  2945. // --------------------------------------------
  2946. // Load page
  2947. // --------------------------------------------
  2948. $this->cached_vars['current_page'] = $this->view(
  2949. 'preferences.html',
  2950. NULL,
  2951. TRUE
  2952. );
  2953. return $this->ee_cp_view('index.html');
  2954. }
  2955. //END preferences
  2956. // --------------------------------------------------------------------
  2957. /**
  2958. * delete confirm page abstractor
  2959. *
  2960. * @access private
  2961. * @param string method you want to post data to after confirm
  2962. * @param array all of the values you want to carry over to post
  2963. * @param string the lang line of the warning message to display
  2964. * @param string the lang line of the submit button for confirm
  2965. * @param bool $message_use_lang use the lang wrapper for the message?
  2966. * @return string
  2967. */
  2968. private function delete_confirm ($method, $hidden_values,
  2969. $message_line, $submit_line = 'delete',
  2970. $message_use_lang = TRUE)
  2971. {
  2972. $this->cached_vars = array_merge($this->cached_vars, array(
  2973. 'hidden_values' => $hidden_values,
  2974. 'lang_message' => ($message_use_lang ? lang($message_line) : $message_line),
  2975. 'lang_submit' => lang($submit_line),
  2976. 'form_url' => $this->mod_link(array('method' => $method))
  2977. ));
  2978. $this->cached_vars['current_page'] = $this->view(
  2979. 'delete_confirm.html',
  2980. NULL,
  2981. TRUE
  2982. );
  2983. return $this->ee_cp_view('index.html');
  2984. }
  2985. //END delete_confirm
  2986. //--------------------------------------------------------------------
  2987. // end views
  2988. //--------------------------------------------------------------------
  2989. // --------------------------------------------------------------------
  2990. /**
  2991. * Clean up inputted paragraph html
  2992. *
  2993. * @access public
  2994. * @param string $string string to clean
  2995. * @return mixed string if html or json if not
  2996. */
  2997. public function clean_paragraph_html ($string = '', $allow_ajax = TRUE)
  2998. {
  2999. if ( ! $string OR trim($string == ''))
  3000. {
  3001. $string = ee()->input->get_post('clean_string');
  3002. if ( $string === FALSE OR trim($string) === '')
  3003. {
  3004. $string = '';
  3005. }
  3006. }
  3007. $allowed_tags = '<' . implode('><', $this->data->allowed_html_tags) . '>';
  3008. $string = strip_tags($string, $allowed_tags);
  3009. $string = ee()->security->xss_clean($string);
  3010. return $string;
  3011. }
  3012. //END clean_paragraph_html
  3013. // --------------------------------------------------------------------
  3014. /**
  3015. * Fieldtype Actions
  3016. *
  3017. * this installs or uninstalles depending on the sent action
  3018. * this will redirect no matter what
  3019. *
  3020. * @access public
  3021. * @return null
  3022. */
  3023. public function freeform_fieldtype_action ()
  3024. {
  3025. $return_url = array('method' => 'fieldtypes');
  3026. $name = ee()->input->get_post('name', TRUE);
  3027. $action = ee()->input->get_post('action');
  3028. if ($name AND $action)
  3029. {
  3030. ee()->load->library('freeform_fields');
  3031. if ($action === 'install')
  3032. {
  3033. if (ee()->freeform_fields->install_fieldtype($name))
  3034. {
  3035. $return_url['msg'] = 'fieldtype_installed';
  3036. }
  3037. else
  3038. {
  3039. return $this->actions()->full_stop(lang('fieldtype_install_error'));
  3040. }
  3041. }
  3042. else if ($action === 'uninstall')
  3043. {
  3044. $uninstall = $this->uninstall_confirm_fieldtype($name);
  3045. //if its not a boolean true, its a confirmation
  3046. if ($uninstall !== TRUE)
  3047. {
  3048. return $uninstall;
  3049. }
  3050. else
  3051. {
  3052. $return_url['msg'] = 'fieldtype_uninstalled';
  3053. }
  3054. }
  3055. }
  3056. if ($this->is_ajax_request())
  3057. {
  3058. return $this->send_ajax_response(array(
  3059. 'success' => TRUE,
  3060. 'message' => lang('fieldtype_uninstalled')
  3061. ));
  3062. }
  3063. else
  3064. {
  3065. ee()->functions->redirect($this->mod_link($return_url));
  3066. }
  3067. }
  3068. //END freeform_fieldtype_action
  3069. // --------------------------------------------------------------------
  3070. /**
  3071. * save field layout
  3072. *
  3073. * ajax called method for saving field layout in the entries screen
  3074. *
  3075. * @access public
  3076. * @return string
  3077. */
  3078. public function save_field_layout ()
  3079. {
  3080. ee()->load->library('freeform_forms');
  3081. ee()->load->model('freeform_form_model');
  3082. $save_for = ee()->input->get_post('save_for', TRUE);
  3083. $shown_fields = ee()->input->get_post('shown_fields', TRUE);
  3084. $form_id = $this->get_post_or_zero('form_id');
  3085. $form_data = $this->data->get_form_info($form_id);
  3086. // -------------------------------------
  3087. // valid
  3088. // -------------------------------------
  3089. if (
  3090. ! $this->is_ajax_request() OR
  3091. ! is_array($save_for) OR
  3092. ! is_array($shown_fields) OR
  3093. ! $form_data
  3094. )
  3095. {
  3096. return $this->send_ajax_response(array(
  3097. 'success' => FALSE,
  3098. 'message' => lang('invalid_input')
  3099. ));
  3100. }
  3101. // -------------------------------------
  3102. // permissions?
  3103. // -------------------------------------
  3104. //if (ee()->session->userdata('group_id') != 1 AND
  3105. // ! $this->check_yes($this->preference('allow_user_field_layout'))
  3106. //)
  3107. //{
  3108. // return $this->send_ajax_response(array(
  3109. // 'success' => FALSE,
  3110. // 'message' => lang('invalid_permissions')
  3111. // ));
  3112. //}
  3113. // -------------------------------------
  3114. // save
  3115. // -------------------------------------
  3116. $field_layout_prefs = $this->preference('field_layout_prefs');
  3117. $original_prefs = (
  3118. is_array($field_layout_prefs) ?
  3119. $field_layout_prefs :
  3120. array()
  3121. );
  3122. // -------------------------------------
  3123. // who is it for?
  3124. // -------------------------------------
  3125. $for = array();
  3126. foreach ($save_for as $item)
  3127. {
  3128. //if this is for everyone, we can stop
  3129. if (in_array($item, array('just_me', 'everyone')))
  3130. {
  3131. $for = $item;
  3132. break;
  3133. }
  3134. if ($this->is_positive_intlike($item))
  3135. {
  3136. $for[] = $item;
  3137. }
  3138. }
  3139. // -------------------------------------
  3140. // what do they want to see?
  3141. // -------------------------------------
  3142. $standard_columns = $this->get_standard_column_names();
  3143. $possible_columns = $standard_columns;
  3144. //build possible columns
  3145. foreach ($form_data['fields'] as $field_id => $field_data)
  3146. {
  3147. $possible_columns[] = $field_id;
  3148. }
  3149. $data = array();
  3150. $prefix = ee()->freeform_form_model->form_field_prefix;
  3151. //check for field validity, no funny business
  3152. foreach ($shown_fields as $field_name)
  3153. {
  3154. $field_id = str_replace($prefix, '', $field_name);
  3155. if (in_array($field_name, $standard_columns))
  3156. {
  3157. $data[] = $field_name;
  3158. unset(
  3159. $possible_columns[
  3160. array_search(
  3161. $field_name,
  3162. $possible_columns
  3163. )
  3164. ]
  3165. );
  3166. }
  3167. else if (in_array($field_id , array_keys($form_data['fields'])))
  3168. {
  3169. $data[] = $field_id;
  3170. unset(
  3171. $possible_columns[
  3172. array_search(
  3173. $field_id,
  3174. $possible_columns
  3175. )
  3176. ]
  3177. );
  3178. }
  3179. }
  3180. //removes holes
  3181. sort($possible_columns);
  3182. // -------------------------------------
  3183. // insert the data per group or all
  3184. // -------------------------------------
  3185. $settings = array(
  3186. 'visible' => $data,
  3187. 'hidden' => $possible_columns
  3188. );
  3189. if ($for == 'just_me')
  3190. {
  3191. $id = ee()->session->userdata('member_id');
  3192. $original_prefs['entry_layout_prefs']['member'][$id] = $settings;
  3193. }
  3194. else if ($for == 'everyone')
  3195. {
  3196. $original_prefs['entry_layout_prefs']['all']['visible'] = $settings;
  3197. }
  3198. else
  3199. {
  3200. foreach ($for as $who)
  3201. {
  3202. $original_prefs['entry_layout_prefs']['group'][$who]['visible'] = $settings;
  3203. }
  3204. }
  3205. // -------------------------------------
  3206. // save
  3207. // -------------------------------------
  3208. $this->data->set_module_preferences(array(
  3209. 'field_layout_prefs' => json_encode($original_prefs)
  3210. ));
  3211. // -------------------------------------
  3212. // success!
  3213. // -------------------------------------
  3214. //TODO test for ajax request or redirect back
  3215. //don't want JS erorrs preventing this from
  3216. //working
  3217. $this->send_ajax_response(array(
  3218. 'success' => TRUE,
  3219. 'message' => lang('layout_saved'),
  3220. 'update_fields' => array()
  3221. ));
  3222. //prevent EE CP default spit out
  3223. exit();
  3224. }
  3225. //END save_field_layout
  3226. // --------------------------------------------------------------------
  3227. /**
  3228. * save_form
  3229. *
  3230. * @access public
  3231. * @return null
  3232. */
  3233. public function save_form ()
  3234. {
  3235. // -------------------------------------
  3236. // form ID? we must be editing
  3237. // -------------------------------------
  3238. $form_id = $this->get_post_or_zero('form_id');
  3239. $update = ($form_id != 0);
  3240. // -------------------------------------
  3241. // default status
  3242. // -------------------------------------
  3243. $default_status = ee()->input->get_post('default_status', TRUE);
  3244. $default_status = ($default_status AND trim($default_status) != '') ?
  3245. $default_status :
  3246. $this->data->defaults['default_form_status'];
  3247. // -------------------------------------
  3248. // composer return?
  3249. // -------------------------------------
  3250. $do_composer = (FREEFORM_PRO AND ee()->input->get_post('ret') == 'composer');
  3251. $composer_save_finish = (FREEFORM_PRO AND ee()->input->get_post('ret') == 'composer_save_finish');
  3252. if ($composer_save_finish)
  3253. {
  3254. $do_composer = TRUE;
  3255. }
  3256. // -------------------------------------
  3257. // error on empty items or bad data
  3258. // (doing this via ajax in the form as well)
  3259. // -------------------------------------
  3260. $errors = array();
  3261. // -------------------------------------
  3262. // field name
  3263. // -------------------------------------
  3264. $form_name = ee()->input->get_post('form_name', TRUE);
  3265. //if the field label is blank, make one for them
  3266. //we really dont want to do this, but here we are
  3267. if ( ! $form_name OR ! trim($form_name))
  3268. {
  3269. $errors['form_name'] = lang('form_name_required');
  3270. }
  3271. else
  3272. {
  3273. $form_name = strtolower(trim($form_name));
  3274. if ( in_array($form_name, $this->data->prohibited_names ) )
  3275. {
  3276. $errors['form_name'] = str_replace(
  3277. '%name%',
  3278. $form_name,
  3279. lang('reserved_form_name')
  3280. );
  3281. }
  3282. //if the form_name they submitted isn't like how a URL title may be
  3283. //also, cannot be numeric
  3284. if (preg_match('/[^a-z0-9\_\-]/i', $form_name) OR
  3285. is_numeric($form_name))
  3286. {
  3287. $errors['form_name'] = lang('form_name_can_only_contain');
  3288. }
  3289. ee()->load->model('freeform_form_model');
  3290. //get dupe from field names
  3291. $dupe_data = ee()->freeform_form_model->get_row(array(
  3292. 'form_name' => $form_name
  3293. ));
  3294. //if we are updating, we don't want to error on the same field id
  3295. if ( ! empty($dupe_data) AND
  3296. ! ($update AND $dupe_data['form_id'] == $form_id))
  3297. {
  3298. $errors['form_name'] = str_replace(
  3299. '%name%',
  3300. $form_name,
  3301. lang('form_name_exists')
  3302. );
  3303. }
  3304. }
  3305. // -------------------------------------
  3306. // form label
  3307. // -------------------------------------
  3308. $form_label = ee()->input->get_post('form_label', TRUE);
  3309. if ( ! $form_label OR ! trim($form_label) )
  3310. {
  3311. $errors['form_label'] = lang('form_label_required');
  3312. }
  3313. // -------------------------------------
  3314. // admin notification email
  3315. // -------------------------------------
  3316. $admin_notification_email = ee()->input->get_post('admin_notification_email', TRUE);
  3317. if ($admin_notification_email AND
  3318. trim($admin_notification_email) != '')
  3319. {
  3320. ee()->load->helper('email');
  3321. $emails = preg_split(
  3322. '/(\s+)?\,(\s+)?/',
  3323. $admin_notification_email,
  3324. -1,
  3325. PREG_SPLIT_NO_EMPTY
  3326. );
  3327. $errors['admin_notification_email'] = array();
  3328. foreach ($emails as $key => $email)
  3329. {
  3330. $emails[$key] = trim($email);
  3331. if ( ! valid_email($email))
  3332. {
  3333. $errors['admin_notification_email'][] = str_replace('%email%', $email, lang('non_valid_email'));
  3334. }
  3335. }
  3336. if (empty($errors['admin_notification_email']))
  3337. {
  3338. unset($errors['admin_notification_email']);
  3339. }
  3340. $admin_notification_email = implode('|', $emails);
  3341. }
  3342. else
  3343. {
  3344. $admin_notification_email = '';
  3345. }
  3346. // -------------------------------------
  3347. // user email field
  3348. // -------------------------------------
  3349. $user_email_field = ee()->input->get_post('user_email_field');
  3350. ee()->load->model('freeform_field_model');
  3351. $field_ids = ee()->freeform_field_model->key('field_id', 'field_id')->get();
  3352. if ($user_email_field AND
  3353. $user_email_field !== '--' AND
  3354. trim($user_email_field) !== '' AND
  3355. ! in_array($user_email_field, $field_ids ))
  3356. {
  3357. $errors['user_email_field'] = lang('invalid_user_email_field');
  3358. }
  3359. // -------------------------------------
  3360. // errors? For shame :(
  3361. // -------------------------------------
  3362. if ( ! empty($errors))
  3363. {
  3364. return $this->actions()->full_stop($errors);
  3365. }
  3366. //send ajax response exists
  3367. //but this is in case someone is using a replacer
  3368. //that uses
  3369. if ($this->check_yes(ee()->input->get_post('validate_only')))
  3370. {
  3371. if ($this->is_ajax_request())
  3372. {
  3373. $this->send_ajax_response(array(
  3374. 'success' => TRUE,
  3375. 'errors' => array()
  3376. ));
  3377. }
  3378. exit();
  3379. }
  3380. // -------------------------------------
  3381. // field ids
  3382. // -------------------------------------
  3383. $field_ids = array_filter(
  3384. $this->actions()->pipe_split(
  3385. ee()->input->get_post('field_ids', TRUE)
  3386. ),
  3387. array($this, 'is_positive_intlike')
  3388. );
  3389. $sorted_field_ids = $field_ids;
  3390. sort($sorted_field_ids);
  3391. // -------------------------------------
  3392. // insert data
  3393. // -------------------------------------
  3394. $data = array(
  3395. 'form_name' => strip_tags($form_name),
  3396. 'form_label' => strip_tags($form_label),
  3397. 'default_status' => $default_status,
  3398. 'user_notification_id' => $this->get_post_or_zero('user_notification_id'),
  3399. 'admin_notification_id' => $this->get_post_or_zero('admin_notification_id'),
  3400. 'admin_notification_email' => $admin_notification_email,
  3401. 'form_description' => strip_tags(ee()->input->get_post('form_description', TRUE)),
  3402. 'author_id' => ee()->session->userdata('member_id'),
  3403. 'field_ids' => implode('|', $sorted_field_ids),
  3404. 'field_order' => implode('|', $field_ids),
  3405. 'notify_admin' => (
  3406. (ee()->input->get_post('notify_admin') == 'y') ? 'y' : 'n'
  3407. ),
  3408. 'notify_user' => (
  3409. (ee()->input->get_post('notify_user') == 'y') ? 'y' : 'n'
  3410. ),
  3411. 'user_email_field' => $user_email_field,
  3412. );
  3413. //load the forms model if its not been already
  3414. ee()->load->library('freeform_forms');
  3415. if ($do_composer)
  3416. {
  3417. unset($data['field_ids']);
  3418. unset($data['field_order']);
  3419. }
  3420. if ($update)
  3421. {
  3422. unset($data['author_id']);
  3423. if ( ! $do_composer)
  3424. {
  3425. $data['composer_id'] = 0;
  3426. }
  3427. ee()->freeform_forms->update_form($form_id, $data);
  3428. }
  3429. else
  3430. {
  3431. //we don't want this running on update, will only happen for dupes
  3432. $composer_id = $this->get_post_or_zero('composer_id');
  3433. //this is a dupe and they want composer to dupe too?
  3434. if ($do_composer AND $composer_id > 0)
  3435. {
  3436. ee()->load->model('freeform_composer_model');
  3437. $composer_data = ee()->freeform_composer_model
  3438. ->select('composer_data')
  3439. ->where('composer_id', $composer_id)
  3440. ->get_row();
  3441. if ($composer_data !== FALSE)
  3442. {
  3443. $data['composer_id'] = ee()->freeform_composer_model->insert(
  3444. array(
  3445. 'composer_data' => $composer_data['composer_data'],
  3446. 'site_id' => ee()->config->item('site_id')
  3447. )
  3448. );
  3449. }
  3450. }
  3451. $form_id = ee()->freeform_forms->create_form($data);
  3452. }
  3453. // -------------------------------------
  3454. // return
  3455. // -------------------------------------
  3456. if ( ! $composer_save_finish AND $do_composer)
  3457. {
  3458. ee()->functions->redirect($this->mod_link(array(
  3459. 'method' => 'form_composer',
  3460. 'form_id' => $form_id,
  3461. 'msg' => 'edit_form_success'
  3462. )));
  3463. }
  3464. //'save and finish, default'
  3465. else
  3466. {
  3467. ee()->functions->redirect($this->mod_link(array(
  3468. 'method' => 'forms',
  3469. 'msg' => 'edit_form_success'
  3470. )));
  3471. }
  3472. }
  3473. //END save_form
  3474. // --------------------------------------------------------------------
  3475. /**
  3476. * Save Entry
  3477. *
  3478. * @access public
  3479. * @return null redirect
  3480. */
  3481. public function save_entry ()
  3482. {
  3483. // -------------------------------------
  3484. // edit?
  3485. // -------------------------------------
  3486. $form_id = $this->get_post_or_zero('form_id');
  3487. $entry_id = $this->get_post_or_zero('entry_id');
  3488. ee()->load->model('freeform_form_model');
  3489. ee()->load->model('freeform_entry_model');
  3490. ee()->load->library('freeform_forms');
  3491. ee()->load->library('freeform_fields');
  3492. $form_data = $this->data->get_form_info($form_id);
  3493. // -------------------------------------
  3494. // valid form id
  3495. // -------------------------------------
  3496. if ( ! $form_data)
  3497. {
  3498. return $this->actions()->full_stop(lang('invalid_form_id'));
  3499. }
  3500. $previous_inputs = array();
  3501. if ( $entry_id > 0)
  3502. {
  3503. $entry_data = ee()->freeform_entry_model
  3504. ->id($form_id)
  3505. ->where('entry_id', $entry_id)
  3506. ->get_row();
  3507. if ( ! $entry_data)
  3508. {
  3509. return $this->actions()->full_stop(lang('invalid_entry_id'));
  3510. }
  3511. $previous_inputs = $entry_data;
  3512. }
  3513. // -------------------------------------
  3514. // form data
  3515. // -------------------------------------
  3516. $field_labels = array();
  3517. $valid_fields = array();
  3518. foreach ( $form_data['fields'] as $row)
  3519. {
  3520. $field_labels[$row['field_name']] = $row['field_label'];
  3521. $valid_fields[] = $row['field_name'];
  3522. }
  3523. // -------------------------------------
  3524. // is this an edit? entry_id
  3525. // -------------------------------------
  3526. $edit = ($entry_id AND $entry_id != 0);
  3527. // -------------------------------------
  3528. // for hooks
  3529. // -------------------------------------
  3530. $this->edit = $edit;
  3531. // -------------------------------------
  3532. // prevalidate hook
  3533. // -------------------------------------
  3534. $errors = array();
  3535. if (ee()->extensions->active_hook('freeform_module_validate_begin') === TRUE)
  3536. {
  3537. $errors = ee()->extensions->universal_call(
  3538. 'freeform_module_validate_begin',
  3539. $errors,
  3540. $this
  3541. );
  3542. if (ee()->extensions->end_script === TRUE) return;
  3543. }
  3544. // -------------------------------------
  3545. // validate
  3546. // -------------------------------------
  3547. $field_input_data = array();
  3548. $field_list = array();
  3549. // -------------------------------------
  3550. // status?
  3551. // -------------------------------------
  3552. $available_statuses = $this->data->get_form_statuses();
  3553. $status = ee()->input->get_post('status');
  3554. if ( ! array_key_exists($status, $available_statuses))
  3555. {
  3556. $field_input_data['status'] = $this->data->defaults['default_form_status'];
  3557. }
  3558. else
  3559. {
  3560. $field_input_data['status'] = $status;
  3561. }
  3562. foreach ($form_data['fields'] as $field_id => $field_data)
  3563. {
  3564. $field_list[$field_data['field_name']] = $field_data['field_label'];
  3565. $field_post = ee()->input->get_post($field_data['field_name']);
  3566. //if it's not even in $_POST or $_GET, lets skip input
  3567. //unless its an uploaded file, then we'll send false anyway
  3568. //because its fieldtype will handle the rest of that work
  3569. if ($field_post !== FALSE OR
  3570. isset($_FILES[$field_data['field_name']]))
  3571. {
  3572. $field_input_data[$field_data['field_name']] = $field_post;
  3573. }
  3574. }
  3575. //form fields do thier own validation,
  3576. //so lets just get results! (sexy results?)
  3577. $errors = array_merge(
  3578. $errors,
  3579. ee()->freeform_fields->validate(
  3580. $form_id,
  3581. $field_input_data
  3582. )
  3583. );
  3584. // -------------------------------------
  3585. // post validate hook
  3586. // -------------------------------------
  3587. if (ee()->extensions->active_hook('freeform_module_validate_end') === TRUE)
  3588. {
  3589. $errors = ee()->extensions->universal_call(
  3590. 'freeform_module_validate_end',
  3591. $errors,
  3592. $this
  3593. );
  3594. if (ee()->extensions->end_script === TRUE) return;
  3595. }
  3596. // -------------------------------------
  3597. // halt on errors
  3598. // -------------------------------------
  3599. if (count($errors) > 0)
  3600. {
  3601. $this->actions()->full_stop($errors);
  3602. }
  3603. //send ajax response exists
  3604. //but this is in case someone is using a replacer
  3605. //that uses
  3606. if ($this->check_yes(ee()->input->get_post('validate_only')))
  3607. {
  3608. if ($this->is_ajax_request())
  3609. {
  3610. $this->send_ajax_response(array(
  3611. 'success' => TRUE,
  3612. 'errors' => array()
  3613. ));
  3614. }
  3615. exit();
  3616. }
  3617. // -------------------------------------
  3618. // entry insert begin hook
  3619. // -------------------------------------
  3620. if (ee()->extensions->active_hook('freeform_module_insert_begin') === TRUE)
  3621. {
  3622. $field_input_data = ee()->extensions->universal_call(
  3623. 'freeform_module_insert_begin',
  3624. $field_input_data,
  3625. $entry_id,
  3626. $form_id,
  3627. $this
  3628. );
  3629. if (ee()->extensions->end_script === TRUE) return;
  3630. }
  3631. // -------------------------------------
  3632. // insert/update data into db
  3633. // -------------------------------------
  3634. if ($edit)
  3635. {
  3636. ee()->freeform_forms->update_entry(
  3637. $form_id,
  3638. $entry_id,
  3639. $field_input_data
  3640. );
  3641. }
  3642. else
  3643. {
  3644. $entry_id = ee()->freeform_forms->insert_new_entry(
  3645. $form_id,
  3646. $field_input_data
  3647. );
  3648. }
  3649. // -------------------------------------
  3650. // entry insert begin hook
  3651. // -------------------------------------
  3652. if (ee()->extensions->active_hook('freeform_module_insert_end') === TRUE)
  3653. {
  3654. $edata = ee()->extensions->universal_call(
  3655. 'freeform_module_insert_end',
  3656. $field_input_data,
  3657. $entry_id,
  3658. $form_id,
  3659. $this
  3660. );
  3661. if (ee()->extensions->end_script === TRUE) return;
  3662. }
  3663. // -------------------------------------
  3664. // return
  3665. // -------------------------------------
  3666. $success_line = ($edit) ? 'edit_entry_success' : 'new_entry_success';
  3667. if ($this->is_ajax_request())
  3668. {
  3669. return $this->send_ajax_response(array(
  3670. 'form_id' => $form_id,
  3671. 'entry_id' => $entry_id,
  3672. 'message' => lang($success_line),
  3673. 'success' => TRUE
  3674. ));
  3675. }
  3676. //'save and finish, default'
  3677. else
  3678. {
  3679. ee()->functions->redirect($this->mod_link(array(
  3680. 'method' => 'entries',
  3681. 'form_id' => $form_id,
  3682. 'msg' => $success_line
  3683. )));
  3684. }
  3685. }
  3686. //END edit_entry
  3687. // --------------------------------------------------------------------
  3688. /**
  3689. * approve entries
  3690. *
  3691. * accepts ajax call to approve entries
  3692. * or can be called via another view function on post
  3693. *
  3694. * @access public
  3695. * @param int form id
  3696. * @param mixed array or int of entry ids to approve
  3697. * @return string
  3698. */
  3699. public function approve_entries ($form_id = 0, $entry_ids = array())
  3700. {
  3701. // -------------------------------------
  3702. // valid form id?
  3703. // -------------------------------------
  3704. if ( ! $form_id OR $form_id <= 0)
  3705. {
  3706. $form_id = $this->get_post_form_id();
  3707. }
  3708. if ( ! $form_id)
  3709. {
  3710. $this->actions()->full_stop(lang('invalid_form_id'));
  3711. }
  3712. // -------------------------------------
  3713. // entry ids?
  3714. // -------------------------------------
  3715. if ( ! $entry_ids OR empty($entry_ids) )
  3716. {
  3717. $entry_ids = $this->get_post_entry_ids();
  3718. }
  3719. //check
  3720. if ( ! $entry_ids)
  3721. {
  3722. $this->actions()->full_stop(lang('invalid_entry_id'));
  3723. }
  3724. // -------------------------------------
  3725. // approve!
  3726. // -------------------------------------
  3727. $updates = array();
  3728. foreach($entry_ids as $entry_id)
  3729. {
  3730. $updates[] = array(
  3731. 'entry_id' => $entry_id,
  3732. 'status' => 'open'
  3733. );
  3734. }
  3735. ee()->load->model('freeform_form_model');
  3736. ee()->db->update_batch(
  3737. ee()->freeform_form_model->table_name($form_id),
  3738. $updates,
  3739. 'entry_id'
  3740. );
  3741. // -------------------------------------
  3742. // success
  3743. // -------------------------------------
  3744. if ($this->is_ajax_request())
  3745. {
  3746. $this->send_ajax_response(array(
  3747. 'success' => TRUE
  3748. ));
  3749. exit();
  3750. }
  3751. else
  3752. {
  3753. $method = ee()->input->get_post('return_method');
  3754. $method = ($method AND is_callable(array($this, $method))) ?
  3755. $method :
  3756. 'moderate_entries';
  3757. ee()->functions->redirect($this->mod_link(array(
  3758. 'method' => $method,
  3759. 'form_id' => $form_id,
  3760. 'msg' => 'entries_approved'
  3761. )));
  3762. }
  3763. }
  3764. //END approve_entry
  3765. // --------------------------------------------------------------------
  3766. /**
  3767. * get/post form_id
  3768. *
  3769. * gets and validates the current form_id possibly passed
  3770. *
  3771. * @access private
  3772. * @param bool validate form_id
  3773. * @return mixed integer of passed in form_id or bool false
  3774. */
  3775. private function get_post_form_id ($validate = TRUE)
  3776. {
  3777. $form_id = $this->get_post_or_zero('form_id');
  3778. if ($form_id == 0 OR
  3779. ($validate AND
  3780. ! $this->data->is_valid_form_id($form_id))
  3781. )
  3782. {
  3783. return FALSE;
  3784. }
  3785. return $form_id;
  3786. }
  3787. //ENd get_post_form_id
  3788. // --------------------------------------------------------------------
  3789. /**
  3790. * get/post entry_ids
  3791. *
  3792. * gets and validates the current entry possibly passed
  3793. *
  3794. * @access private
  3795. * @return mixed array of passed in entry_ids or bool false
  3796. */
  3797. private function get_post_entry_ids ()
  3798. {
  3799. $entry_ids = ee()->input->get_post('entry_ids');
  3800. if ( ! is_array($entry_ids) AND
  3801. ! $this->is_positive_intlike($entry_ids))
  3802. {
  3803. return FALSE;
  3804. }
  3805. if ( ! is_array($entry_ids))
  3806. {
  3807. $entry_ids = array($entry_ids);
  3808. }
  3809. //clean and validate each as int
  3810. $entry_ids = array_filter($entry_ids, array($this, 'is_positive_intlike'));
  3811. if (empty($entry_ids))
  3812. {
  3813. return FALSE;
  3814. }
  3815. return $entry_ids;
  3816. }
  3817. //END get_post_entry_ids
  3818. // --------------------------------------------------------------------
  3819. /**
  3820. * delete_confirm_entries
  3821. *
  3822. * accepts ajax call to delete entry
  3823. * or can be called via another view function on post
  3824. *
  3825. * @access public
  3826. * @param int form id
  3827. * @param mixed array or int of entry ids to delete
  3828. * @return string
  3829. */
  3830. public function delete_confirm_entries ($form_id = 0, $entry_ids = array())
  3831. {
  3832. // -------------------------------------
  3833. // ajax requests should be doing front
  3834. // end delete confirm. This also handles
  3835. // the ajax errors properly
  3836. // -------------------------------------
  3837. if ( $this->is_ajax_request())
  3838. {
  3839. return $this->delete_entries();
  3840. }
  3841. // -------------------------------------
  3842. // form id?
  3843. // -------------------------------------
  3844. if ( ! $form_id OR $form_id <= 0)
  3845. {
  3846. $form_id = $this->get_post_form_id();
  3847. }
  3848. if ( ! $form_id)
  3849. {
  3850. ee()->output->show_user_error(
  3851. 'submission',
  3852. array(lang('invalid_form_id'))
  3853. );
  3854. }
  3855. // -------------------------------------
  3856. // entry ids?
  3857. // -------------------------------------
  3858. if ( ! $entry_ids OR empty($entry_ids) )
  3859. {
  3860. $entry_ids = $this->get_post_entry_ids();
  3861. }
  3862. //check
  3863. if ( ! $entry_ids)
  3864. {
  3865. ee()->output->show_user_error(
  3866. 'submission',
  3867. array(lang('invalid_entry_id'))
  3868. );
  3869. }
  3870. // -------------------------------------
  3871. // return method?
  3872. // -------------------------------------
  3873. $return_method = ee()->input->get_post('return_method');
  3874. $return_method = ($return_method AND
  3875. is_callable(array($this, $return_method))) ?
  3876. $return_method :
  3877. 'entries';
  3878. // -------------------------------------
  3879. // confirmation page
  3880. // -------------------------------------
  3881. return $this->delete_confirm(
  3882. 'delete_entries',
  3883. array(
  3884. 'form_id' => $form_id,
  3885. 'entry_ids' => $entry_ids,
  3886. 'return_method' => $return_method
  3887. ),
  3888. 'confirm_delete_entries'
  3889. );
  3890. }
  3891. //END delete_confirm_entries
  3892. // --------------------------------------------------------------------
  3893. /**
  3894. * delete entries
  3895. *
  3896. * accepts ajax call to delete entry
  3897. * or can be called via another view function on post
  3898. *
  3899. * @access public
  3900. * @param int form id
  3901. * @param mixed array or int of entry ids to delete
  3902. * @return string
  3903. */
  3904. public function delete_entries ($form_id = 0, $entry_ids = array())
  3905. {
  3906. // -------------------------------------
  3907. // valid form id?
  3908. // -------------------------------------
  3909. if ( ! $this->is_positive_intlike($form_id))
  3910. {
  3911. $form_id = $this->get_post_form_id();
  3912. }
  3913. if ( ! $form_id)
  3914. {
  3915. $this->actions()->full_stop(lang('invalid_form_id'));
  3916. }
  3917. // -------------------------------------
  3918. // entry ids?
  3919. // -------------------------------------
  3920. if ( ! $entry_ids OR empty($entry_ids) )
  3921. {
  3922. $entry_ids = $this->get_post_entry_ids();
  3923. }
  3924. //check
  3925. if ( ! $entry_ids)
  3926. {
  3927. $this->actions()->full_stop(lang('invalid_entry_id'));
  3928. }
  3929. ee()->load->library('freeform_forms');
  3930. $success = ee()->freeform_forms->delete_entries($form_id, $entry_ids);
  3931. // -------------------------------------
  3932. // success
  3933. // -------------------------------------
  3934. if ($this->is_ajax_request())
  3935. {
  3936. $this->send_ajax_response(array(
  3937. 'success' => $success
  3938. ));
  3939. }
  3940. else
  3941. {
  3942. $method = ee()->input->get_post('return_method');
  3943. $method = ($method AND is_callable(array($this, $method))) ?
  3944. $method : 'entries';
  3945. ee()->functions->redirect($this->mod_link(array(
  3946. 'method' => $method,
  3947. 'form_id' => $form_id,
  3948. 'msg' => 'entries_deleted'
  3949. )));
  3950. }
  3951. }
  3952. //END delete_entries
  3953. // --------------------------------------------------------------------
  3954. /**
  3955. * Confirm Delete Fields
  3956. *
  3957. * @access public
  3958. * @return html
  3959. */
  3960. public function delete_confirm_fields ()
  3961. {
  3962. //the following fields will be deleted
  3963. //the following forms will be affected
  3964. //they contain the forms..
  3965. $field_ids = ee()->input->get_post('field_id', TRUE);
  3966. if ( ! is_array($field_ids) AND
  3967. ! $this->is_positive_intlike($field_ids) )
  3968. {
  3969. $this->actions()->full_stop(lang('no_field_ids_submitted'));
  3970. }
  3971. //already checked for numeric :p
  3972. if ( ! is_array($field_ids))
  3973. {
  3974. $field_ids = array($field_ids);
  3975. }
  3976. $delete_field_confirmation = '';
  3977. $clean_field_ids = array();
  3978. foreach ($field_ids as $field_id)
  3979. {
  3980. if ($this->is_positive_intlike($field_id))
  3981. {
  3982. $clean_field_ids[] = $field_id;
  3983. }
  3984. }
  3985. if (empty($clean_field_ids))
  3986. {
  3987. $this->actions()->full_stop(lang('no_field_ids_submitted'));
  3988. }
  3989. // -------------------------------------
  3990. // build a list of forms affected by fields
  3991. // -------------------------------------
  3992. ee()->db->where_in('field_id', $clean_field_ids);
  3993. $all_field_data = ee()->db->get('freeform_fields');
  3994. $delete_field_confirmation = lang('delete_field_confirmation');
  3995. $extra_form_data = '';
  3996. foreach ($all_field_data->result_array() as $row)
  3997. {
  3998. //this doesn't get field data, so we had to above;
  3999. $field_form_data = $this->data->get_form_info_by_field_id($row['field_id']);
  4000. // -------------------------------------
  4001. // get each form affected by each field listed
  4002. // and show the user what forms will be affected
  4003. // -------------------------------------
  4004. if ( $field_form_data !== FALSE )
  4005. {
  4006. $freeform_affected = array();
  4007. foreach ($field_form_data as $form_id => $form_data)
  4008. {
  4009. $freeform_affected[] = $form_data['form_label'];
  4010. }
  4011. $extra_form_data .= '<p>' .
  4012. '<strong>' .
  4013. $row['field_label'] .
  4014. '</strong>: ' .
  4015. implode(', ', $freeform_affected) .
  4016. '</p>';
  4017. }
  4018. }
  4019. //if we have anything, add some extra warnings
  4020. if ($extra_form_data != '')
  4021. {
  4022. $delete_field_confirmation .= '<p>' .
  4023. lang('freeform_will_lose_data') .
  4024. '</p>' .
  4025. $extra_form_data;
  4026. }
  4027. return $this->delete_confirm(
  4028. 'delete_fields',
  4029. array('field_id' => $clean_field_ids),
  4030. $delete_field_confirmation,
  4031. 'delete',
  4032. FALSE
  4033. );
  4034. }
  4035. //END delete_confirm_fields
  4036. // --------------------------------------------------------------------
  4037. /**
  4038. * utilities
  4039. *
  4040. * @access public
  4041. * @return string
  4042. */
  4043. public function utilities ( $message = '' )
  4044. {
  4045. if ($message == '' AND ee()->input->get('msg') !== FALSE)
  4046. {
  4047. $message = lang(ee()->input->get('msg'));
  4048. }
  4049. $this->cached_vars['message'] = $message;
  4050. //--------------------------------------
  4051. // Title and Crumbs
  4052. //--------------------------------------
  4053. $this->add_crumb(lang('utilities'));
  4054. $this->set_highlight('module_utilities');
  4055. //--------------------------------------
  4056. // Counts
  4057. //--------------------------------------
  4058. ee()->load->library('freeform_migration');
  4059. $this->cached_vars['counts'] = ee()->freeform_migration->get_collection_counts();
  4060. // $test = ee()->freeform_migration->create_upload_field( 1, 'goff', array( 'file_upload_location' => '1' ) );
  4061. // print_r( $test ); print_r( ee()->freeform_migration->get_errors() );
  4062. //--------------------------------------
  4063. // File upload field installed?
  4064. //--------------------------------------
  4065. $this->cached_vars['file_upload_installed'] = ee()->freeform_migration->get_field_type_installed('file_upload');
  4066. $query = ee()->db->query( "SELECT * FROM exp_freeform_fields WHERE field_type = 'file_upload'" );
  4067. //--------------------------------------
  4068. // Load vars
  4069. //--------------------------------------
  4070. $this->cached_vars['form_uri'] = $this->mod_link(array(
  4071. 'method' => 'migrate_collections'
  4072. ));
  4073. //--------------------------------------
  4074. // Load page
  4075. //--------------------------------------
  4076. $this->cached_vars['current_page'] = $this->view(
  4077. 'utilities.html',
  4078. NULL,
  4079. TRUE
  4080. );
  4081. return $this->ee_cp_view('index.html');
  4082. }
  4083. // End utilities
  4084. // --------------------------------------------------------------------
  4085. /**
  4086. * Confirm Uninstall Fieldtypes
  4087. *
  4088. * @access public
  4089. * @return html
  4090. */
  4091. public function uninstall_confirm_fieldtype ($name = '')
  4092. {
  4093. $name = trim($name);
  4094. if ($name == '')
  4095. {
  4096. ee()->functions->redirect($this->base);
  4097. }
  4098. ee()->load->model('freeform_field_model');
  4099. $items = ee()->freeform_field_model
  4100. ->key('field_label', 'field_label')
  4101. ->get(array('field_type' => $name));
  4102. if ($items == FALSE)
  4103. {
  4104. return $this->uninstall_fieldtype($name);
  4105. }
  4106. else
  4107. {
  4108. $confirmation = '<p>' . lang('following_fields_converted') . ': <strong>';
  4109. $confirmation .= implode(', ', $items) . '</strong></p>';
  4110. return $this->delete_confirm(
  4111. 'uninstall_fieldtype',
  4112. array('fieldtype' => $name),
  4113. $confirmation,
  4114. 'uninstall',
  4115. FALSE
  4116. );
  4117. }
  4118. }
  4119. //END uninstall_confirm_fieldtype
  4120. // --------------------------------------------------------------------
  4121. /**
  4122. * Uninstalls fieldtypes
  4123. *
  4124. * @access public
  4125. * @param string $name fieldtype name to remove
  4126. * @return void
  4127. */
  4128. public function uninstall_fieldtype ($name = '', $redirect = TRUE)
  4129. {
  4130. if ($name == '')
  4131. {
  4132. $name = ee()->input->get_post('fieldtype', TRUE);
  4133. }
  4134. if ( ! $name)
  4135. {
  4136. $this->actions()->full_stop(lang('no_fieldtypes_submitted'));
  4137. }
  4138. ee()->load->library('freeform_fields');
  4139. $success = ee()->freeform_fields->uninstall_fieldtype($name);
  4140. if ( ! $redirect)
  4141. {
  4142. return $success;
  4143. }
  4144. if ($this->is_ajax_request())
  4145. {
  4146. $this->send_ajax_response(array(
  4147. 'success' => $success,
  4148. 'message' => lang('fieldtype_uninstalled'),
  4149. ));
  4150. }
  4151. else
  4152. {
  4153. ee()->functions->redirect($this->mod_link(array(
  4154. 'method' => 'fieldtypes',
  4155. 'msg' => 'fieldtype_uninstalled'
  4156. )));
  4157. }
  4158. }
  4159. //END uninstall_fieldtype
  4160. // --------------------------------------------------------------------
  4161. /**
  4162. * Delete Fields
  4163. *
  4164. * @access public
  4165. * @return void
  4166. */
  4167. public function delete_fields ()
  4168. {
  4169. // -------------------------------------
  4170. // safety goggles
  4171. // -------------------------------------
  4172. //
  4173. $field_ids = ee()->input->get_post('field_id', TRUE);
  4174. if ( ! is_array($field_ids) AND
  4175. ! $this->is_positive_intlike($field_ids) )
  4176. {
  4177. $this->actions()->full_stop(lang('no_field_ids_submitted'));
  4178. }
  4179. //already checked for numeric :p
  4180. if ( ! is_array($field_ids))
  4181. {
  4182. $field_ids = array($field_ids);
  4183. }
  4184. // -------------------------------------
  4185. // delete fields
  4186. // -------------------------------------
  4187. ee()->load->library('freeform_fields');
  4188. ee()->freeform_fields->delete_field($field_ids);
  4189. // -------------------------------------
  4190. // success
  4191. // -------------------------------------
  4192. if ($this->is_ajax_request())
  4193. {
  4194. $this->send_ajax_response(array(
  4195. 'success' => TRUE
  4196. ));
  4197. }
  4198. else
  4199. {
  4200. ee()->functions->redirect($this->mod_link(array(
  4201. 'method' => 'fields',
  4202. 'msg' => 'fields_deleted'
  4203. )));
  4204. }
  4205. }
  4206. //END delete_fields
  4207. // --------------------------------------------------------------------
  4208. /**
  4209. * Confirm deletion of notifications
  4210. *
  4211. * accepts ajax call to delete notification
  4212. *
  4213. * @access public
  4214. * @param int form id
  4215. * @param mixed array or int of notification ids to delete
  4216. * @return string
  4217. */
  4218. public function delete_confirm_notification ($notification_id = 0)
  4219. {
  4220. // -------------------------------------
  4221. // ajax requests should be doing front
  4222. // end delete confirm. This also handles
  4223. // the ajax errors properly
  4224. // -------------------------------------
  4225. if ( $this->is_ajax_request())
  4226. {
  4227. return $this->delete_notification();
  4228. }
  4229. // -------------------------------------
  4230. // entry ids?
  4231. // -------------------------------------
  4232. if ( ! is_array($notification_id) AND
  4233. ! $this->is_positive_intlike($notification_id))
  4234. {
  4235. $notification_id = ee()->input->get_post('notification_id');
  4236. }
  4237. if ( ! is_array($notification_id) AND
  4238. ! $this->is_positive_intlike($notification_id))
  4239. {
  4240. $this->actions()->full_stop(lang('invalid_notification_id'));
  4241. }
  4242. if ( is_array($notification_id))
  4243. {
  4244. $notification_id = array_filter(
  4245. $notification_id,
  4246. array($this, 'is_positive_intlike')
  4247. );
  4248. }
  4249. else
  4250. {
  4251. $notification_id = array($notification_id);
  4252. }
  4253. // -------------------------------------
  4254. // confirmation page
  4255. // -------------------------------------
  4256. return $this->delete_confirm(
  4257. 'delete_notification',
  4258. array(
  4259. 'notification_id' => $notification_id,
  4260. 'return_method' => 'notifications'
  4261. ),
  4262. 'confirm_delete_notification'
  4263. );
  4264. }
  4265. //END delete_confirm_notification
  4266. // --------------------------------------------------------------------
  4267. /**
  4268. * Delete Notifications
  4269. *
  4270. * @access public
  4271. * @param integer $notification_id notification
  4272. * @return null
  4273. */
  4274. public function delete_notification ($notification_id = 0)
  4275. {
  4276. // -------------------------------------
  4277. // entry ids?
  4278. // -------------------------------------
  4279. if ( ! is_array($notification_id) AND
  4280. ! $this->is_positive_intlike($notification_id))
  4281. {
  4282. $notification_id = ee()->input->get_post('notification_id');
  4283. }
  4284. if ( ! is_array($notification_id) AND
  4285. ! $this->is_positive_intlike($notification_id))
  4286. {
  4287. $this->actions()->full_stop(lang('invalid_notification_id'));
  4288. }
  4289. if ( is_array($notification_id))
  4290. {
  4291. $notification_id = array_filter(
  4292. $notification_id,
  4293. array($this, 'is_positive_intlike')
  4294. );
  4295. }
  4296. else
  4297. {
  4298. $notification_id = array($notification_id);
  4299. }
  4300. ee()->load->model('freeform_notification_model');
  4301. $success = ee()->freeform_notification_model
  4302. ->where_in('notification_id', $notification_id)
  4303. ->delete();
  4304. // -------------------------------------
  4305. // success
  4306. // -------------------------------------
  4307. if ($this->is_ajax_request())
  4308. {
  4309. $this->send_ajax_response(array(
  4310. 'success' => $success
  4311. ));
  4312. }
  4313. else
  4314. {
  4315. $method = ee()->input->get_post('return_method');
  4316. $method = ($method AND is_callable(array($this, $method))) ?
  4317. $method : 'notifications';
  4318. ee()->functions->redirect($this->mod_link(array(
  4319. 'method' => $method,
  4320. 'msg' => 'delete_notification_success'
  4321. )));
  4322. }
  4323. }
  4324. //END delete_notification
  4325. // --------------------------------------------------------------------
  4326. /**
  4327. * save_field
  4328. *
  4329. * @access public
  4330. * @return void (redirect)
  4331. */
  4332. public function save_field ()
  4333. {
  4334. // -------------------------------------
  4335. // field ID? we must be editing
  4336. // -------------------------------------
  4337. $field_id = $this->get_post_or_zero('field_id');
  4338. $update = ($field_id != 0);
  4339. // -------------------------------------
  4340. // yes or no items (all default yes)
  4341. // -------------------------------------
  4342. $y_or_n = array('submissions_page', 'moderation_page', 'composer_use');
  4343. foreach ($y_or_n as $item)
  4344. {
  4345. //set as local var
  4346. $$item = $this->check_no(ee()->input->get_post($item)) ? 'n' : 'y';
  4347. }
  4348. // -------------------------------------
  4349. // field instance
  4350. // -------------------------------------
  4351. $field_type = ee()->input->get_post('field_type', TRUE);
  4352. ee()->load->library('freeform_fields');
  4353. ee()->load->model('freeform_field_model');
  4354. $available_fieldtypes = ee()->freeform_fields->get_available_fieldtypes();
  4355. //get the update with previous settings if this is an edit
  4356. if ($update)
  4357. {
  4358. $field = ee()->freeform_field_model
  4359. ->where('field_id', $field_id)
  4360. ->where('field_type', $field_type)
  4361. ->count();
  4362. //make sure that we have the correct type just in case they
  4363. //are changing type like hooligans
  4364. if ($field)
  4365. {
  4366. $field_instance =& ee()->freeform_fields->get_field_instance($field_id);
  4367. }
  4368. else
  4369. {
  4370. $field_instance =& ee()->freeform_fields->get_fieldtype_instance($field_type);
  4371. }
  4372. }
  4373. else
  4374. {
  4375. $field_instance =& ee()->freeform_fields->get_fieldtype_instance($field_type);
  4376. }
  4377. // -------------------------------------
  4378. // error on empty items or bad data
  4379. // (doing this via ajax in the form as well)
  4380. // -------------------------------------
  4381. $errors = array();
  4382. // -------------------------------------
  4383. // field name
  4384. // -------------------------------------
  4385. $field_name = ee()->input->get_post('field_name', TRUE);
  4386. //if the field label is blank, make one for them
  4387. //we really dont want to do this, but here we are
  4388. if ( ! $field_name OR ! trim($field_name))
  4389. {
  4390. $errors['field_name'] = lang('field_name_required');
  4391. }
  4392. else
  4393. {
  4394. $field_name = strtolower(trim($field_name));
  4395. if ( in_array($field_name, $this->data->prohibited_names ) )
  4396. {
  4397. $errors['field_name'] = str_replace(
  4398. '%name%',
  4399. $field_name,
  4400. lang('freeform_reserved_field_name')
  4401. );
  4402. }
  4403. //if the field_name they submitted isn't like how a URL title may be
  4404. //also, cannot be numeric
  4405. if (preg_match('/[^a-z0-9\_\-]/i', $field_name) OR
  4406. is_numeric($field_name))
  4407. {
  4408. $errors['field_name'] = lang('field_name_can_only_contain');
  4409. }
  4410. //get dupe from field names
  4411. $f_query = ee()->db->select('field_name, field_id')->get_where(
  4412. 'freeform_fields',
  4413. array('field_name' => $field_name)
  4414. );
  4415. //if we are updating, we don't want to error on the same field id
  4416. if ($f_query->num_rows() > 0 AND
  4417. ! ($update AND $f_query->row('field_id') == $field_id))
  4418. {
  4419. $errors['field_name'] = str_replace(
  4420. '%name%',
  4421. $field_name,
  4422. lang('field_name_exists')
  4423. );
  4424. }
  4425. }
  4426. // -------------------------------------
  4427. // field label
  4428. // -------------------------------------
  4429. $field_label = ee()->input->get_post('field_label', TRUE);
  4430. if ( ! $field_label OR ! trim($field_label) )
  4431. {
  4432. $errors['field_label'] = lang('field_label_required');
  4433. }
  4434. // -------------------------------------
  4435. // field type
  4436. // -------------------------------------
  4437. if ( ! $field_type OR ! array_key_exists($field_type, $available_fieldtypes))
  4438. {
  4439. $errors['field_type'] = lang('invalid_fieldtype');
  4440. }
  4441. // -------------------------------------
  4442. // field settings errors?
  4443. // -------------------------------------
  4444. $field_settings_validate = $field_instance->validate_settings();
  4445. if ( $field_settings_validate !== TRUE)
  4446. {
  4447. if (is_array($field_settings_validate))
  4448. {
  4449. $errors['field_settings'] = $field_settings_validate;
  4450. }
  4451. else if (! empty($field_instance->errors))
  4452. {
  4453. $errors['field_settings'] = $field_instance->errors;
  4454. }
  4455. else
  4456. {
  4457. $errors['field_settings'] = lang('field_settings_error');
  4458. }
  4459. }
  4460. // -------------------------------------
  4461. // errors? For shame :(
  4462. // -------------------------------------
  4463. if ( ! empty($errors))
  4464. {
  4465. return $this->actions()->full_stop($errors);
  4466. }
  4467. if ($this->check_yes(ee()->input->get_post('validate_only')) AND
  4468. $this->is_ajax_request())
  4469. {
  4470. $this->send_ajax_response(array(
  4471. 'success' => TRUE
  4472. ));
  4473. }
  4474. // -------------------------------------
  4475. // insert data
  4476. // -------------------------------------
  4477. $data = array(
  4478. 'field_name' => strip_tags($field_name),
  4479. 'field_label' => strip_tags($field_label),
  4480. 'field_type' => $field_type,
  4481. 'edit_date' => '0', //overridden if update
  4482. 'field_description' => strip_tags(ee()->input->get_post('field_description', TRUE)),
  4483. 'submissions_page' => $submissions_page,
  4484. 'moderation_page' => $moderation_page,
  4485. 'composer_use' => $composer_use,
  4486. 'settings' => json_encode($field_instance->save_settings())
  4487. );
  4488. if ($update)
  4489. {
  4490. ee()->freeform_field_model->update(
  4491. array_merge(
  4492. $data,
  4493. array(
  4494. 'edit_date' => ee()->localize->now
  4495. )
  4496. ),
  4497. array('field_id' => $field_id)
  4498. );
  4499. }
  4500. else
  4501. {
  4502. $field_id = ee()->freeform_field_model->insert(
  4503. array_merge(
  4504. $data,
  4505. array(
  4506. 'author_id' => ee()->session->userdata('member_id'),
  4507. 'entry_date' => ee()->localize->now,
  4508. 'site_id' => ee()->config->item('site_id')
  4509. )
  4510. )
  4511. );
  4512. }
  4513. $field_instance->field_id = $field_id;
  4514. $field_instance->post_save_settings();
  4515. $field_in_forms = array();
  4516. if ($update)
  4517. {
  4518. $field_in_forms = $this->data->get_form_info_by_field_id($field_id);
  4519. if ($field_in_forms)
  4520. {
  4521. $field_in_forms = array_keys($field_in_forms);
  4522. }
  4523. else
  4524. {
  4525. $field_in_forms = array();
  4526. }
  4527. }
  4528. $form_ids = ee()->input->get_post('form_ids');
  4529. if ($form_ids !== FALSE)
  4530. {
  4531. $form_ids = preg_split(
  4532. '/\|/',
  4533. $form_ids,
  4534. -1,
  4535. PREG_SPLIT_NO_EMPTY
  4536. );
  4537. }
  4538. else
  4539. {
  4540. $form_ids = array();
  4541. }
  4542. if ( ! (empty($form_ids) AND empty($field_in_forms)))
  4543. {
  4544. $remove = array_unique(array_diff($field_in_forms, $form_ids));
  4545. $add = array_unique(array_diff($form_ids, $field_in_forms));
  4546. ee()->load->library('freeform_forms');
  4547. foreach ($add as $add_id)
  4548. {
  4549. ee()->freeform_forms->add_field_to_form($add_id, $field_id);
  4550. }
  4551. foreach ($remove as $remove_id)
  4552. {
  4553. ee()->freeform_forms->remove_field_from_form($remove_id, $field_id);
  4554. }
  4555. }
  4556. // -------------------------------------
  4557. // success
  4558. // -------------------------------------
  4559. if ($this->is_ajax_request())
  4560. {
  4561. $return = array(
  4562. 'success' => TRUE,
  4563. 'field_id' => $field_id,
  4564. );
  4565. if ($this->check_yes(ee()->input->get_post('include_field_data')))
  4566. {
  4567. $return['composerFieldData'] = $this->composer_field_data($field_id, NULL, TRUE);
  4568. }
  4569. $this->send_ajax_response($return);
  4570. }
  4571. else
  4572. {
  4573. //redirect back to fields on success
  4574. ee()->functions->redirect($this->mod_link(array(
  4575. 'method' => 'fields',
  4576. 'msg' => 'edit_field_success'
  4577. )));
  4578. }
  4579. }
  4580. //END save_field
  4581. // --------------------------------------------------------------------
  4582. /**
  4583. * save_notification
  4584. *
  4585. * @access public
  4586. * @return null (redirect)
  4587. */
  4588. public function save_notification ()
  4589. {
  4590. // -------------------------------------
  4591. // notification ID? we must be editing
  4592. // -------------------------------------
  4593. $notification_id = $this->get_post_or_zero('notification_id');
  4594. $update = ($notification_id != 0);
  4595. // -------------------------------------
  4596. // yes or no items (default yes)
  4597. // -------------------------------------
  4598. $y_or_n = array('wordwrap');
  4599. foreach ($y_or_n as $item)
  4600. {
  4601. //set as local var
  4602. $$item = $this->check_no(ee()->input->get_post($item)) ? 'n' : 'y';
  4603. }
  4604. // -------------------------------------
  4605. // yes or no items (default no)
  4606. // -------------------------------------
  4607. $n_or_y = array('allow_html', 'include_attachments');
  4608. foreach ($n_or_y as $item)
  4609. {
  4610. //set as local var
  4611. $$item = $this->check_yes(ee()->input->get_post($item)) ? 'y' : 'n';
  4612. }
  4613. // -------------------------------------
  4614. // error on empty items or bad data
  4615. // (doing this via ajax in the form as well)
  4616. // -------------------------------------
  4617. $errors = array();
  4618. // -------------------------------------
  4619. // notification name
  4620. // -------------------------------------
  4621. $notification_name = ee()->input->get_post('notification_name', TRUE);
  4622. //if the field label is blank, make one for them
  4623. //we really dont want to do this, but here we are
  4624. if ( ! $notification_name OR ! trim($notification_name))
  4625. {
  4626. $errors['notification_name'] = lang('notification_name_required');
  4627. }
  4628. else
  4629. {
  4630. $notification_name = strtolower(trim($notification_name));
  4631. if ( in_array($notification_name, $this->data->prohibited_names ) )
  4632. {
  4633. $errors['notification_name'] = str_replace(
  4634. '%name%',
  4635. $notification_name,
  4636. lang('reserved_notification_name')
  4637. );
  4638. }
  4639. //if the field_name they submitted isn't like how a URL title may be
  4640. //also, cannot be numeric
  4641. if (preg_match('/[^a-z0-9\_\-]/i', $notification_name) OR
  4642. is_numeric($notification_name))
  4643. {
  4644. $errors['notification_name'] = lang('notification_name_can_only_contain');
  4645. }
  4646. //get dupe from field names
  4647. ee()->db->select('notification_name, notification_id');
  4648. $f_query = ee()->db->get_where(
  4649. 'freeform_notification_templates',
  4650. array(
  4651. 'notification_name' => $notification_name
  4652. )
  4653. );
  4654. //if we are updating, we don't want to error on the same field id
  4655. if ($f_query->num_rows() > 0 AND
  4656. ! ($update AND $f_query->row('notification_id') == $notification_id))
  4657. {
  4658. $errors['notification_name'] = str_replace(
  4659. '%name%',
  4660. $notification_name,
  4661. lang('notification_name_exists')
  4662. );
  4663. }
  4664. }
  4665. // -------------------------------------
  4666. // notification label
  4667. // -------------------------------------
  4668. $notification_label = ee()->input->get_post('notification_label', TRUE);
  4669. if ( ! $notification_label OR ! trim($notification_label) )
  4670. {
  4671. $errors['notification_label'] = lang('notification_label_required');
  4672. }
  4673. ee()->load->helper('email');
  4674. // -------------------------------------
  4675. // notification email
  4676. // -------------------------------------
  4677. $from_email = ee()->input->get_post('from_email', TRUE);
  4678. if ($from_email AND trim($from_email) != '')
  4679. {
  4680. $from_email = trim($from_email);
  4681. //allow tags
  4682. if ( ! preg_match('/' . LD . '([a-zA-Z0-9\_]+)' . RD . '/is', $from_email))
  4683. {
  4684. if ( ! valid_email($from_email))
  4685. {
  4686. $errors['from_email'] = str_replace(
  4687. '%email%',
  4688. $from_email,
  4689. lang('non_valid_email')
  4690. );
  4691. }
  4692. }
  4693. }
  4694. // -------------------------------------
  4695. // from name
  4696. // -------------------------------------
  4697. $from_name = ee()->input->get_post('from_name', TRUE);
  4698. if ( ! $from_name OR ! trim($from_name) )
  4699. {
  4700. //$errors['from_name'] = lang('from_name_required');
  4701. }
  4702. // -------------------------------------
  4703. // reply to email
  4704. // -------------------------------------
  4705. $reply_to_email = ee()->input->get_post('reply_to_email', TRUE);
  4706. if ($reply_to_email AND trim($reply_to_email) != '')
  4707. {
  4708. $reply_to_email = trim($reply_to_email);
  4709. //allow tags
  4710. if ( ! preg_match('/' . LD . '([a-zA-Z0-9\_]+)' . RD . '/is', $reply_to_email))
  4711. {
  4712. if ( ! valid_email($reply_to_email))
  4713. {
  4714. $errors['reply_to_email'] = str_replace(
  4715. '%email%',
  4716. $reply_to_email,
  4717. lang('non_valid_email')
  4718. );
  4719. }
  4720. }
  4721. }
  4722. else
  4723. {
  4724. $reply_to_email = '';
  4725. }
  4726. // -------------------------------------
  4727. // email subject
  4728. // -------------------------------------
  4729. $email_subject = ee()->input->get_post('email_subject', TRUE);
  4730. if ( ! $email_subject OR ! trim($email_subject) )
  4731. {
  4732. $errors['email_subject'] = lang('email_subject_required');
  4733. }
  4734. // -------------------------------------
  4735. // errors? For shame :(
  4736. // -------------------------------------
  4737. if ( ! empty($errors))
  4738. {
  4739. return $this->actions()->full_stop($errors);
  4740. }
  4741. //ajax checking?
  4742. else if ($this->check_yes(ee()->input->get_post('validate_only')))
  4743. {
  4744. return $this->send_ajax_response(array(
  4745. 'success' => TRUE
  4746. ));
  4747. }
  4748. // -------------------------------------
  4749. // insert data
  4750. // -------------------------------------
  4751. $data = array(
  4752. 'notification_name' => strip_tags($notification_name),
  4753. 'notification_label' => strip_tags($notification_label),
  4754. 'notification_description' => strip_tags(ee()->input->get_post('notification_description', TRUE)),
  4755. 'wordwrap' => $wordwrap,
  4756. 'allow_html' => $allow_html,
  4757. 'from_name' => $from_name,
  4758. 'from_email' => $from_email,
  4759. 'reply_to_email' => $reply_to_email,
  4760. 'email_subject' => strip_tags($email_subject),
  4761. 'template_data' => ee()->input->get_post('template_data'),
  4762. 'include_attachments' => $include_attachments
  4763. );
  4764. ee()->load->model('freeform_notification_model');
  4765. if ($update)
  4766. {
  4767. ee()->freeform_notification_model->update(
  4768. $data,
  4769. array('notification_id' => $notification_id)
  4770. );
  4771. }
  4772. else
  4773. {
  4774. $notification_id = ee()->freeform_notification_model->insert(
  4775. array_merge(
  4776. $data,
  4777. array(
  4778. 'site_id' => ee()->config->item('site_id')
  4779. )
  4780. )
  4781. );
  4782. }
  4783. // -------------------------------------
  4784. // ajax?
  4785. // -------------------------------------
  4786. if ($this->is_ajax_request())
  4787. {
  4788. $this->send_ajax_response(array(
  4789. 'success' => TRUE,
  4790. 'notification_id' => $notification_id
  4791. ));
  4792. }
  4793. else
  4794. {
  4795. //redirect back to fields on success
  4796. ee()->functions->redirect($this->mod_link(array(
  4797. 'method' => 'notifications',
  4798. 'msg' => 'edit_notification_success'
  4799. )));
  4800. }
  4801. }
  4802. //END save_notification
  4803. // --------------------------------------------------------------------
  4804. /**
  4805. * Sets the menu highlight and assists with permissions (Freeform Pro)
  4806. *
  4807. * @access protected
  4808. * @param string $menu_item The menu item to highlight
  4809. */
  4810. protected function set_highlight ($menu_item = 'module_forms')
  4811. {
  4812. $this->cached_vars['module_menu_highlight'] = $menu_item;
  4813. }
  4814. //END set_highlight
  4815. // --------------------------------------------------------------------
  4816. /**
  4817. * save_preferences
  4818. *
  4819. * @access public
  4820. * @return null (redirect)
  4821. */
  4822. public function save_preferences ()
  4823. {
  4824. //defaults are in data.freeform.php
  4825. $prefs = array();
  4826. $all_prefs = array_merge(
  4827. $this->data->default_preferences,
  4828. $this->data->default_global_preferences
  4829. );
  4830. //check post input for all existing prefs and default if not present
  4831. foreach($all_prefs as $pref_name => $data)
  4832. {
  4833. $input = ee()->input->get_post($pref_name, TRUE);
  4834. //default
  4835. $output = $data['value'];
  4836. //int
  4837. if ($data['type'] == 'int' AND
  4838. $this->is_positive_intlike($input, -1))
  4839. {
  4840. $output = $input;
  4841. }
  4842. //yes or no
  4843. elseif ($data['type'] == 'y_or_n' AND
  4844. in_array(trim($input), array('y', 'n'), TRUE))
  4845. {
  4846. $output = trim($input);
  4847. }
  4848. //list of items
  4849. //this seems nutty, but this serializes the list of items
  4850. elseif ($data['type'] == 'list')
  4851. {
  4852. //lotses?
  4853. if (is_array($input))
  4854. {
  4855. $temp_input = array();
  4856. foreach ($input as $key => $value)
  4857. {
  4858. if (trim($value) !== '')
  4859. {
  4860. $temp_input[] = trim($value);
  4861. }
  4862. }
  4863. $output = json_encode($temp_input);
  4864. }
  4865. //just one :/
  4866. else if (trim($input) !== '')
  4867. {
  4868. $output = json_encode(array(trim($input)));
  4869. }
  4870. }
  4871. //text areas
  4872. elseif ($data['type'] == 'text' OR
  4873. $data['type'] == 'textarea' )
  4874. {
  4875. $output = trim($input);
  4876. }
  4877. $prefs[$pref_name] = $output;
  4878. }
  4879. //send all prefs to DB
  4880. $this->data->set_module_preferences($prefs);
  4881. // ----------------------------------
  4882. // Redirect to Homepage with Message
  4883. // ----------------------------------
  4884. ee()->functions->redirect(
  4885. $this->base .
  4886. AMP . 'method=preferences' .
  4887. AMP . 'msg=preferences_updated'
  4888. );
  4889. }
  4890. //END save_preferences
  4891. // --------------------------------------------------------------------
  4892. /**
  4893. * Export Entries
  4894. *
  4895. * Calls entries with proper flags to cue export
  4896. *
  4897. * @access public
  4898. * @return mixed forces a download of the exported items or error
  4899. */
  4900. public function export_entries ()
  4901. {
  4902. $moderate = (ee()->input->get_post('moderate') == 'true');
  4903. return $this->entries(NULL, $moderate, TRUE);
  4904. }
  4905. //END export_entries
  4906. // --------------------------------------------------------------------
  4907. /**
  4908. * get_standard_column_names
  4909. *
  4910. * gets the standard column names and replaces author_id with author
  4911. *
  4912. * @access private
  4913. * @return null
  4914. */
  4915. private function get_standard_column_names()
  4916. {
  4917. $standard_columns = array_keys(
  4918. ee()->freeform_form_model->default_form_table_columns
  4919. );
  4920. array_splice(
  4921. $standard_columns,
  4922. array_search('author_id', $standard_columns),
  4923. 1,
  4924. 'author'
  4925. );
  4926. return $standard_columns;
  4927. }
  4928. //END get_standard_column_names
  4929. // --------------------------------------------------------------------
  4930. /**
  4931. * mod_link
  4932. *
  4933. * makes $this->base . AMP . 'key=value' out of arrays
  4934. *
  4935. * @access public
  4936. * @param array key value pair of get vars to add to base
  4937. * @param bool $real_amp use a real ampersand?
  4938. * @return string
  4939. */
  4940. private function mod_link ($vars = array(), $real_amp = FALSE)
  4941. {
  4942. $link = $this->base;
  4943. $amp = $real_amp ? '&' : AMP;
  4944. if ( ! empty($vars))
  4945. {
  4946. foreach ($vars as $key => $value)
  4947. {
  4948. $link .= $amp . $key . '=' . $value;
  4949. }
  4950. }
  4951. return $link;
  4952. }
  4953. //END mod_link
  4954. // --------------------------------------------------------------------
  4955. /**
  4956. * load_fancybox
  4957. *
  4958. * loads fancybox jquery UI plugin and its needed css
  4959. *
  4960. * @access private
  4961. * @return null
  4962. */
  4963. private function load_fancybox()
  4964. {
  4965. //so currently the fancybox setup inlucded in EE doesn't get built
  4966. //automaticly and requires relying on the current CP theme.
  4967. //Dislike. Inlcuding our own version instead.
  4968. $css_link = $this->sc->addon_theme_url . 'fancybox/jquery.fancybox-1.3.4.css';
  4969. $js_link = $this->sc->addon_theme_url . 'fancybox/jquery.fancybox-1.3.4.pack.js';
  4970. ee()->cp->add_to_head('<link href="' . $css_link . '" type="text/css" rel="stylesheet" media="screen" />');
  4971. ee()->cp->add_to_head('<script src="' . $js_link . '" type="text/javascript"></script>');
  4972. }
  4973. //END load_fancybox
  4974. // --------------------------------------------------------------------
  4975. /**
  4976. * freeform_add_right_link
  4977. *
  4978. * abstractor for cp add_right_link so freeform can move it how it needs
  4979. * when an alternate style is chosen
  4980. *
  4981. * @access private
  4982. * @param string words of link to display
  4983. * @param string link to display
  4984. * @return null
  4985. */
  4986. private function freeform_add_right_link ($lang, $link)
  4987. {
  4988. $this->cached_vars['inner_nav_links'][$lang] = $link;
  4989. //return $this->add_right_link($lang, $link);
  4990. }
  4991. //END freeform_add_right_link
  4992. // --------------------------------------------------------------------
  4993. /**
  4994. * Module Installation
  4995. *
  4996. * Due to the nature of the 1.x branch of ExpressionEngine, this function is always required.
  4997. * However, because of the large size of the module the actual code for installing, uninstalling,
  4998. * and upgrading is located in a separate file to make coding easier
  4999. *
  5000. * @access public
  5001. * @return bool
  5002. */
  5003. public function freeform_module_install()
  5004. {
  5005. require_once $this->addon_path . 'upd.freeform.php';
  5006. $U = new Freeform_upd();
  5007. return $U->install();
  5008. }
  5009. // END freeform_module_install()
  5010. // --------------------------------------------------------------------
  5011. /**
  5012. * Module Uninstallation
  5013. *
  5014. * Due to the nature of the 1.x branch of ExpressionEngine, this function is always required.
  5015. * However, because of the large size of the module the actual code for installing, uninstalling,
  5016. * and upgrading is located in a separate file to make coding easier
  5017. *
  5018. * @access public
  5019. * @return bool
  5020. */
  5021. public function freeform_module_deinstall()
  5022. {
  5023. require_once $this->addon_path . 'upd.freeform.php';
  5024. $U = new Freeform_upd();
  5025. return $U->uninstall();
  5026. }
  5027. // END freeform_module_deinstall()
  5028. // --------------------------------------------------------------------
  5029. /**
  5030. * Module Upgrading
  5031. *
  5032. * This function is not required by the 1.x branch of ExpressionEngine by default. However,
  5033. * as the install and deinstall ones are, we are just going to keep the habit and include it
  5034. * anyhow.
  5035. * - Originally, the $current variable was going to be passed via parameter, but as there might
  5036. * be a further use for such a variable throughout the module at a later date we made it
  5037. * a class variable.
  5038. *
  5039. *
  5040. * @access public
  5041. * @return bool
  5042. */
  5043. public function freeform_module_update()
  5044. {
  5045. if ( ! isset($_POST['run_update']) OR $_POST['run_update'] != 'y')
  5046. {
  5047. $this->add_crumb(lang('update_freeform_module'));
  5048. $this->build_crumbs();
  5049. $this->cached_vars['form_url'] = $this->base . '&msg=update_successful';
  5050. if ($this->pro_update)
  5051. {
  5052. $this->cached_vars['form_url'] .= "&update_pro=true";
  5053. }
  5054. $this->cached_vars['current_page'] = $this->view(
  5055. 'update_module.html',
  5056. NULL,
  5057. TRUE
  5058. );
  5059. return $this->ee_cp_view('index.html');
  5060. }
  5061. require_once $this->addon_path . 'upd.freeform.php';
  5062. $U = new Freeform_upd();
  5063. if ($U->update() !== TRUE)
  5064. {
  5065. return ee()->functions->redirect($this->mod_link(array(
  5066. 'method' => 'index',
  5067. 'msg' => lang('update_failure')
  5068. )));
  5069. }
  5070. else
  5071. {
  5072. return ee()->functions->redirect($this->mod_link(array(
  5073. 'method' => 'index',
  5074. 'msg' => lang('update_successful')
  5075. )));
  5076. }
  5077. }
  5078. // END freeform_module_update()
  5079. // --------------------------------------------------------------------
  5080. /**
  5081. * Visible Columns
  5082. *
  5083. * @access protected
  5084. * @param $possible_columns possible columns
  5085. * @return array array of visible columns
  5086. */
  5087. protected function visible_columns ($standard_columns = array(),
  5088. $possible_columns = array())
  5089. {
  5090. // -------------------------------------
  5091. // get column settings
  5092. // -------------------------------------
  5093. $column_settings = array();
  5094. ee()->load->model('freeform_form_model');
  5095. $field_layout_prefs = $this->preference('field_layout_prefs');
  5096. $member_id = ee()->session->userdata('member_id');
  5097. $group_id = ee()->session->userdata('group_id');
  5098. $f_prefix = ee()->freeform_form_model->form_field_prefix;
  5099. //¿existe? Member? Group? all?
  5100. if ($field_layout_prefs)
  5101. {
  5102. //$field_layout_prefs = json_decode($field_layout_prefs, TRUE);
  5103. $entry_layout_prefs = (
  5104. isset($field_layout_prefs['entry_layout_prefs']) ?
  5105. $field_layout_prefs['entry_layout_prefs'] :
  5106. FALSE
  5107. );
  5108. if ($entry_layout_prefs)
  5109. {
  5110. if (isset($entry_layout_prefs['member'][$member_id]))
  5111. {
  5112. $column_settings = $entry_layout_prefs['member'][$member_id];
  5113. }
  5114. else if (isset($entry_layout_prefs['all']))
  5115. {
  5116. $column_settings = $entry_layout_prefs['all'];
  5117. }
  5118. else if (isset($entry_layout_prefs['group'][$group_id]))
  5119. {
  5120. $column_settings = $entry_layout_prefs['group'][$group_id];
  5121. }
  5122. }
  5123. }
  5124. //if a column is missing, we don't want to error
  5125. //and if its newer than the settings, show it by default
  5126. //settings are also in order of appearence here.
  5127. //we also store the field ids without the prefix
  5128. //in case someone changed it. That would probably
  5129. //hose everything, but who knows? ;)
  5130. if ( ! empty($column_settings))
  5131. {
  5132. $to_sort = array();
  5133. //we are going over possible instead of settings in case something
  5134. //is new or an old column is missing
  5135. foreach ($possible_columns as $cid)
  5136. {
  5137. //if these are new, put them at the end
  5138. if (! in_array($cid, $column_settings['visible']) AND
  5139. ! in_array($cid, $column_settings['hidden'])
  5140. )
  5141. {
  5142. $to_sort[$cid] = $cid;
  5143. }
  5144. }
  5145. //now we want columns from the settings order to go first
  5146. //this way stuff thats not been removed gets to keep settings
  5147. foreach ($column_settings['visible'] as $ecid)
  5148. {
  5149. if (in_array($ecid, $possible_columns))
  5150. {
  5151. //since we are getting our real results now
  5152. //we can add the prefixes
  5153. if ( ! in_array($ecid, $standard_columns) )
  5154. {
  5155. $ecid = $f_prefix . $ecid;
  5156. }
  5157. $visible_columns[] = $ecid;
  5158. }
  5159. }
  5160. //and if we have anything left over (new fields probably)
  5161. //its at the end
  5162. if (! empty($to_sort))
  5163. {
  5164. foreach ($to_sort as $tsid)
  5165. {
  5166. //since we are getting our real results now
  5167. //we can add the prefixes
  5168. if ( ! in_array($tsid, $standard_columns) )
  5169. {
  5170. $tsid = $f_prefix . $tsid;
  5171. }
  5172. $visible_columns[] = $tsid;
  5173. }
  5174. }
  5175. }
  5176. //if we don't have any settings, just toss it all in in order
  5177. else
  5178. {
  5179. foreach ($possible_columns as $pcid)
  5180. {
  5181. if ( ! in_array($pcid, $standard_columns) )
  5182. {
  5183. $pcid = $f_prefix . $pcid;
  5184. }
  5185. $visible_columns[] = $pcid;
  5186. }
  5187. //in theory it should always be there if prefs are empty ...
  5188. $default_hide = array('site_id', 'entry_id', 'complete');
  5189. foreach ($default_hide as $hide_me_seymour)
  5190. {
  5191. if (in_array($hide_me_seymour, $visible_columns))
  5192. {
  5193. unset(
  5194. $visible_columns[
  5195. array_search(
  5196. $hide_me_seymour,
  5197. $visible_columns
  5198. )
  5199. ]
  5200. );
  5201. }
  5202. }
  5203. //fix keys, but preserve order
  5204. $visible_columns = array_merge(array(), $visible_columns);
  5205. }
  5206. return $visible_columns;
  5207. }
  5208. //END visible_columns
  5209. }
  5210. // END CLASS Freeform