PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/formidable/pro/classes/controllers/FrmProEntriesController.php

https://github.com/rafapires/festival-de-ideias
PHP | 2066 lines | 1620 code | 407 blank | 39 comment | 427 complexity | 2477c36892b4fb4054e4eebba849f2ea MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class FrmProEntriesController{
  3. function FrmProEntriesController(){
  4. add_action('admin_menu', array( &$this, 'menu' ), 20);
  5. add_action('admin_init', array(&$this, 'admin_js'), 1);
  6. add_action('init', array(&$this, 'register_scripts'));
  7. add_action('wp_enqueue_scripts', array(&$this, 'add_js'));
  8. add_action('wp_footer', array(&$this, 'footer_js'), 1);
  9. add_action('admin_footer', array(&$this, 'footer_js'));
  10. add_filter('update_user_metadata', array(&$this, 'check_hidden_cols'), 10, 5);
  11. add_action('updated_user_meta', array(&$this, 'update_hidden_cols'), 10, 4);
  12. add_filter('set-screen-option', array(&$this, 'save_per_page'), 10, 3);
  13. add_action('frm_before_table', array( &$this, 'before_table'), 10, 2);
  14. add_action('wp_ajax_frm_import_csv', array( &$this, 'import_csv_entries') );
  15. add_action('frm_process_entry', array(&$this, 'process_update_entry'), 10, 3);
  16. add_action('frm_display_form_action', array(&$this, 'edit_update_form'), 10, 5);
  17. add_action('frm_submit_button_action', array($this, 'ajax_submit_button'), 10, 2);
  18. add_filter('frm_success_filter', array(&$this, 'get_confirmation_method'), 10, 2);
  19. add_action('frm_success_action', array(&$this, 'confirmation'), 10, 4);
  20. add_action('deleted_post', array(&$this, 'delete_entry'));
  21. add_action('untrashed_post', array(&$this, 'untrashed_post'));
  22. add_action('add_meta_boxes', array( &$this, 'create_entry_from_post_box'), 10, 2);
  23. add_action('wp_ajax_frm_create_post_entry', array( &$this, 'create_post_entry'));
  24. add_filter('frmpro_fields_replace_shortcodes', array(&$this, 'filter_shortcode_value'), 10, 4);
  25. add_filter('frm_display_value_custom', array(&$this, 'filter_display_value'), 10, 2);
  26. //Shortcodes
  27. add_shortcode('formresults', array(&$this, 'get_form_results'));
  28. add_shortcode('frm-search', array(&$this, 'get_search'));
  29. add_shortcode('frm-entry-links', array(&$this, 'entry_link_shortcode'));
  30. add_shortcode('frm-entry-edit-link', array(&$this, 'entry_edit_link'));
  31. add_shortcode('frm-entry-update-field', array(&$this, 'entry_update_field'));
  32. add_shortcode('frm-entry-delete-link', array(&$this, 'entry_delete_link'));
  33. add_shortcode('frm-field-value', array(&$this, 'get_field_value_shortcode'));
  34. add_shortcode('frm-show-entry', array(&$this, 'show_entry_shortcode'));
  35. }
  36. function menu(){
  37. global $frm_settings;
  38. if(current_user_can('administrator') and !current_user_can('frm_view_entries')){
  39. global $wp_roles;
  40. $frm_roles = FrmAppHelper::frm_capabilities();
  41. foreach($frm_roles as $frm_role => $frm_role_description){
  42. if(!in_array($frm_role, array('frm_view_forms', 'frm_edit_forms', 'frm_delete_forms', 'frm_change_settings')))
  43. $wp_roles->add_cap( 'administrator', $frm_role );
  44. }
  45. }
  46. add_submenu_page('formidable', $frm_settings->menu .' | '. __('Form Entries', 'formidable'), __('Form Entries', 'formidable'), 'frm_view_entries', 'formidable-entries', array(&$this, 'route'));
  47. if(class_exists('WP_List_Table') and (!isset($_GET['frm_action']) or !in_array($_GET['frm_action'], array('edit', 'show')))){
  48. add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-entries_columns', array(&$this, 'manage_columns'));
  49. add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-entries_sortable_columns', array(&$this, 'sortable_columns'));
  50. add_filter('get_user_option_manage'. sanitize_title($frm_settings->menu) .'_page_formidable-entriescolumnshidden', array(&$this, 'hidden_columns'));
  51. }
  52. //add_filter( 'bulk_actions-' . sanitize_title($frm_settings->menu) .'_page_formidable-entries', array(&$this, 'bulk_action_options'));
  53. add_action('admin_head-'. sanitize_title($frm_settings->menu) .'_page_formidable-entries', array(&$this, 'head'));
  54. }
  55. function head(){
  56. global $frmpro_settings;
  57. $css_file = array(FrmProAppHelper::jquery_css_url($frmpro_settings->theme_css));
  58. require(FRM_VIEWS_PATH . '/shared/head.php');
  59. }
  60. function admin_js(){
  61. if (isset($_GET) and isset($_GET['page']) and ($_GET['page'] == 'formidable-entries' or $_GET['page'] == 'formidable-entry-templates' or $_GET['page'] == 'formidable-import')){
  62. if(!function_exists('wp_editor')){
  63. add_action( 'admin_print_footer_scripts', 'wp_tiny_mce', 25 );
  64. add_filter('tiny_mce_before_init', array(&$this, 'remove_fullscreen'));
  65. if ( user_can_richedit() ){
  66. wp_enqueue_script('editor');
  67. wp_enqueue_script('media-upload');
  68. }
  69. wp_enqueue_script('common');
  70. wp_enqueue_script('post');
  71. }
  72. if($_GET['page'] == 'formidable-entries')
  73. wp_enqueue_script('jquery-ui-datepicker');
  74. }
  75. }
  76. function remove_fullscreen($init){
  77. if(isset($init['plugins'])){
  78. $init['plugins'] = str_replace('wpfullscreen,', '', $init['plugins']);
  79. $init['plugins'] = str_replace('fullscreen,', '', $init['plugins']);
  80. }
  81. return $init;
  82. }
  83. function register_scripts(){
  84. global $wp_scripts;
  85. wp_register_script('jquery-frm-rating', FRMPRO_URL . '/js/jquery.rating.min.js', array('jquery'), '3.13', true);
  86. wp_register_script('jquery-star-metadata', FRMPRO_URL . '/js/jquery.MetaData.js', array('jquery'), '', true);
  87. wp_register_script('jquery-maskedinput', FRMPRO_URL . '/js/jquery.maskedinput.min.js', array('jquery'), '1.3', true);
  88. wp_register_script('nicedit', FRMPRO_URL . '/js/nicedit.js', array(), '', true);
  89. wp_register_script('jquery-frmtimepicker', FRMPRO_URL . '/js/jquery.timePicker.min.js', array('jquery'), '0.3', true);
  90. //jquery-ui-datepicker registered in WP 3.3
  91. if(!isset($wp_scripts->registered) or !isset( $wp_scripts->registered['jquery-ui-datepicker'])){
  92. $date_ver = FrmProAppHelper::datepicker_version();
  93. wp_register_script('jquery-ui-datepicker', FRMPRO_URL . '/js/jquery.ui.datepicker'. $date_ver .'.js', array('jquery', 'jquery-ui-core'), empty($date_ver) ? '1.8.16' : trim($date_ver, '.'), true);
  94. }
  95. }
  96. function add_js(){
  97. if(is_admin())
  98. return;
  99. wp_enqueue_script('jquery-ui-core');
  100. global $frm_settings;
  101. if($frm_settings->accordion_js){
  102. wp_enqueue_script('jquery-ui-widget');
  103. wp_enqueue_script('jquery-ui-accordion', FRMPRO_URL.'/js/jquery.ui.accordion.js', array('jquery', 'jquery-ui-core'), '1.8.16', true);
  104. }
  105. }
  106. function footer_js(){
  107. global $frm_rte_loaded, $frm_datepicker_loaded, $frm_timepicker_loaded, $frm_star_loaded;
  108. global $frm_hidden_fields, $frm_forms_loaded, $frm_calc_fields, $frm_rules, $frm_input_masks;
  109. if(empty($frm_forms_loaded))
  110. return;
  111. $form_ids = '';
  112. foreach($frm_forms_loaded as $form){
  113. if(!is_object($form))
  114. continue;
  115. if($form_ids != '')
  116. $form_ids .= ',';
  117. $form_ids .= '#form_'. $form->form_key;
  118. }
  119. $scripts = array('formidable');
  120. if(!empty($frm_rte_loaded))
  121. $scripts[] = 'nicedit';
  122. if(!empty($frm_datepicker_loaded))
  123. $scripts[] = 'jquery-ui-datepicker';
  124. if(!empty($frm_timepicker_loaded))
  125. $scripts[] = 'jquery-frmtimepicker';
  126. if($frm_star_loaded){
  127. $scripts[] = 'jquery-frm-rating';
  128. if(is_array($frm_star_loaded) and in_array('split', $frm_star_loaded))
  129. $scripts[] = 'jquery-star-metadata'; //needed for spliting stars
  130. }
  131. $frm_input_masks = apply_filters('frm_input_masks', $frm_input_masks, $frm_forms_loaded);
  132. if(!empty($frm_input_masks))
  133. $scripts[] = 'jquery-maskedinput';
  134. if(!empty($scripts)){
  135. global $wp_scripts;
  136. $wp_scripts->do_items( $scripts );
  137. }
  138. unset($scripts);
  139. include_once(FRMPRO_VIEWS_PATH.'/frmpro-entries/footer_js.php');
  140. }
  141. function before_table($footer, $form_id=false){
  142. FrmProEntriesHelper::before_table($footer, $form_id);
  143. }
  144. /* Back End CRUD */
  145. function show($id = false){
  146. if(!current_user_can('frm_view_entries'))
  147. wp_die('You are not allowed to view entries');
  148. global $frm_entry, $frm_field, $frm_entry_meta, $user_ID;
  149. if(!$id)
  150. $id = FrmAppHelper::get_param('id');
  151. if(!$id)
  152. $id = FrmAppHelper::get_param('item_id');
  153. $entry = $frm_entry->getOne($id, true);
  154. $data = maybe_unserialize($entry->description);
  155. if(!is_array($data) or !isset($data['referrer']))
  156. $data = array('referrer' => $data);
  157. $fields = $frm_field->getAll("fi.type not in ('captcha','html') and fi.form_id=". (int)$entry->form_id, 'fi.field_order');
  158. $date_format = get_option('date_format');
  159. $time_format = get_option('time_format');
  160. $show_comments = true;
  161. if(isset($_POST) and isset($_POST['frm_comment']) and !empty($_POST['frm_comment'])){
  162. FrmEntryMeta::add_entry_meta($_POST['item_id'], 0, '', serialize(array('comment' => $_POST['frm_comment'], 'user_id' => $user_ID)));
  163. //send email notifications
  164. }
  165. if($show_comments){
  166. $comments = $frm_entry_meta->getAll("item_id=$id and field_id=0", ' ORDER BY it.created_at ASC');
  167. $to_emails = apply_filters('frm_to_email', array(), $entry, $entry->form_id);
  168. }
  169. require(FRMPRO_VIEWS_PATH.'/frmpro-entries/show.php');
  170. }
  171. function list_entries(){
  172. $params = $this->get_params();
  173. return $this->display_list($params);
  174. }
  175. function new_entry(){
  176. global $frm_form;
  177. if($form_id = FrmAppHelper::get_param('form')){
  178. $form = $frm_form->getOne($form_id);
  179. $this->get_new_vars('', $form);
  180. }else
  181. require(FRMPRO_VIEWS_PATH.'/frmpro-entries/new-selection.php');
  182. }
  183. function create(){
  184. global $frm_form, $frm_entry;
  185. $params = $this->get_params();
  186. if($params['form'])
  187. $form = $frm_form->getOne($params['form']);
  188. $errors = $frm_entry->validate($_POST);
  189. if( count($errors) > 0 ){
  190. $this->get_new_vars($errors, $form);
  191. }else{
  192. if (isset($_POST['frm_page_order_'.$form->id])){
  193. $this->get_new_vars('', $form);
  194. }else{
  195. $_SERVER['REQUEST_URI'] = str_replace('&frm_action=new', '', $_SERVER['REQUEST_URI']);
  196. $record = $frm_entry->create( $_POST );
  197. if ($record)
  198. $message = __('Entry was Successfully Created', 'formidable');
  199. $this->display_list($params, $message, '', 1);
  200. }
  201. }
  202. }
  203. function edit(){
  204. $id = FrmAppHelper::get_param('id');
  205. return $this->get_edit_vars($id);
  206. }
  207. function update(){
  208. global $frm_entry;
  209. $message = '';
  210. $errors = $frm_entry->validate($_POST);
  211. $id = FrmAppHelper::get_param('id');
  212. if( empty($errors) ){
  213. if (isset($_POST['form_id']) and isset($_POST['frm_page_order_'. $_POST['form_id']])){
  214. return $this->get_edit_vars($id);
  215. }else{
  216. $record = $frm_entry->update( $id, $_POST );
  217. //if ($record)
  218. $message = __('Entry was Successfully Updated', 'formidable') . "<br/> <a href='?page=formidable-entries&form=". $_POST['form_id'] ."'>&larr; ". __('Back to Entries', 'formidable') ."</a>";
  219. }
  220. }
  221. return $this->get_edit_vars($id,$errors,$message);
  222. }
  223. function import(){
  224. global $frm_field;
  225. if(!current_user_can('frm_create_entries'))
  226. wp_die($frm_settings->admin_permission);
  227. $step = FrmAppHelper::get_param('step', 'One');
  228. $csv_del = FrmAppHelper::get_param('csv_del', ',');
  229. $form_id = FrmAppHelper::get_param('form_id');
  230. if($step != 'One'){
  231. if($step == 'Two'){
  232. //validate
  233. if(empty($_POST['form_id']) or (empty($_POST['csv']) and (!isset($_FILES) or !isset($_FILES['csv']) or empty($_FILES['csv']['name']) or (int)$_FILES['csv']['size'] <= 0))){
  234. $errors = array(__('All Fields are required', 'formidable'));
  235. $step = 'One';
  236. }else{
  237. //upload
  238. $media_id = ($_POST['csv'] and is_numeric($_POST['csv'])) ? $_POST['csv'] : FrmProAppHelper::upload_file('csv');
  239. if($media_id and !is_wp_error($media_id)){
  240. $current_path = get_attached_file($media_id);
  241. $row = 1;
  242. $headers = $example = '';
  243. if (($f = fopen($current_path, "r")) !== FALSE) {
  244. $row = 0;
  245. while (($data = fgetcsv($f, 100000, $csv_del)) !== FALSE) {
  246. $row++;
  247. if($row == 1)
  248. $headers = $data;
  249. else if($row == 2)
  250. $example = $data;
  251. else
  252. continue;
  253. }
  254. fclose($f);
  255. }
  256. $fields = $frm_field->getAll("fi.type not in ('break','divider','captcha','html') and fi.form_id=". (int)$form_id, 'fi.field_order');
  257. }else if(is_wp_error($media_id)){
  258. echo $media_id->get_error_message();
  259. $step = 'One';
  260. }
  261. }
  262. }else if($step == 'import'){
  263. global $frm_ajax_url;
  264. //IMPORT NOW
  265. $media_id = FrmAppHelper::get_param('csv');
  266. $current_path = get_attached_file($media_id);
  267. $row = FrmAppHelper::get_param('row');
  268. $opts = get_option('frm_import_options');
  269. $left = ($opts and isset($opts[$media_id])) ? ((int)$row - (int)$opts[$media_id]['imported'] - 1) : ($row-1);
  270. $mapping = FrmAppHelper::get_param('data_array');
  271. $url_vars = "&csv_del=". urlencode($csv_del) ."&form_id={$form_id}&csv={$media_id}&row={$row}";
  272. foreach($mapping as $mkey => $map)
  273. $url_vars .= "&data_array[$mkey]=$map";
  274. }
  275. }
  276. $next_step = ($step == 'One') ? __('Step Two', 'formidable') : __('Import', 'formidable');
  277. if($step == 'One')
  278. $csvs = get_posts( array('post_type' => 'attachment', 'post_mime_type' => 'text/csv') );
  279. include(FRMPRO_VIEWS_PATH.'/frmpro-entries/import.php');
  280. }
  281. function import_csv_entries(){
  282. if(!current_user_can('frm_create_entries'))
  283. wp_die($frm_settings->admin_permission);
  284. extract($_POST);
  285. $opts = get_option('frm_import_options');
  286. if(!$opts)
  287. $opts = array();
  288. $current_path = get_attached_file($csv);
  289. $start_row = (isset($opts[$csv])) ? $opts[$csv]['imported'] : 1;
  290. $imported = FrmProAppHelper::import_csv($current_path, $form_id, $data_array, 0, $start_row+1, $csv_del);
  291. $opts[$csv] = compact('row', 'imported');
  292. echo $remaining = ((int)$row - (int)$imported);
  293. if(!$remaining)
  294. unset($opts[$csv]);
  295. update_option('frm_import_options', $opts);
  296. die();
  297. }
  298. function duplicate(){
  299. global $frm_entry, $frm_form;
  300. $params = $this->get_params();
  301. if($params['form'])
  302. $form = $frm_form->getOne($params['form']);
  303. $message = $errors = '';
  304. $record = $frm_entry->duplicate( $params['id'] );
  305. if ($record)
  306. $message = __('Entry was Successfully Duplicated', 'formidable');
  307. else
  308. $errors = __('There was a problem duplicating that entry', 'formidable');
  309. if(!empty($errors))
  310. return $this->display_list($params, $errors);
  311. else
  312. return $this->get_edit_vars($record, '', $message);
  313. }
  314. function destroy(){
  315. if(!current_user_can('frm_delete_entries')){
  316. global $frm_settings;
  317. wp_die($frm_settings->admin_permission);
  318. }
  319. global $frm_entry, $frm_form;
  320. $params = $this->get_params();
  321. if($params['form'])
  322. $form = $frm_form->getOne($params['form']);
  323. $message = '';
  324. if ($frm_entry->destroy( $params['id'] ))
  325. $message = __('Entry was Successfully Destroyed', 'formidable');
  326. $this->display_list($params, $message, '', 1);
  327. }
  328. function destroy_all(){
  329. if(!current_user_can('frm_delete_entries')){
  330. global $frm_settings;
  331. wp_die($frm_settings->admin_permission);
  332. }
  333. global $frm_entry, $frm_form, $frmdb;
  334. $params = $this->get_params();
  335. $message = '';
  336. $errors = array();
  337. if($params['form']){
  338. $form = $frm_form->getOne($params['form']);
  339. $entry_ids = $frmdb->get_col($frmdb->entries, array('form_id' => $form->id));
  340. foreach($entry_ids as $entry_id){
  341. if ($frm_entry->destroy( $entry_id ))
  342. $message = __('Entries were Successfully Destroyed', 'formidable');
  343. }
  344. }else{
  345. $errors = __('No entries were specified', 'formidable');
  346. }
  347. $this->display_list($params, $message, '', 0, $errors);
  348. }
  349. function bulk_actions($action='list-form'){
  350. global $frm_entry, $frm_settings;
  351. $params = $this->get_params();
  352. $errors = array();
  353. $bulkaction = '-1';
  354. if($action == 'list-form'){
  355. if($_REQUEST['bulkaction'] != '-1')
  356. $bulkaction = $_REQUEST['bulkaction'];
  357. else if($_POST['bulkaction2'] != '-1')
  358. $bulkaction = $_REQUEST['bulkaction2'];
  359. }else{
  360. $bulkaction = str_replace('bulk_', '', $action);
  361. }
  362. $items = FrmAppHelper::get_param('item-action', '');
  363. if (empty($items)){
  364. $errors[] = __('No entries were specified', 'formidable');
  365. }else{
  366. if(!is_array($items))
  367. $items = explode(',', $items);
  368. if($bulkaction == 'delete'){
  369. if(!current_user_can('frm_delete_entries')){
  370. $errors[] = $frm_settings->admin_permission;
  371. }else{
  372. if(is_array($items)){
  373. foreach($items as $item_id)
  374. $frm_entry->destroy($item_id);
  375. }
  376. }
  377. }else if($bulkaction == 'export'){
  378. $controller = 'items';
  379. $ids = $items;
  380. $ids = implode(',', $ids);
  381. include_once(FRMPRO_VIEWS_PATH.'/shared/xml.php');
  382. }else if($bulkaction == 'csv'){
  383. if(!current_user_can('frm_view_entries'))
  384. wp_die($frm_settings->admin_permission);
  385. global $frm_form;
  386. $form_id = $params['form'];
  387. if($form_id){
  388. $form = $frm_form->getOne($form_id);
  389. }else{
  390. $form = $frm_form->getAll("is_template=0 AND (status is NULL OR status = '' OR status = 'published')", ' ORDER BY name', ' LIMIT 1');
  391. if($form)
  392. $form_id = $form->id;
  393. else
  394. $errors[] = __('No form was found', 'formidable');
  395. }
  396. if($form_id and is_array($items)){
  397. echo '<script type="text/javascript">window.onload=function(){location.href="'. FRM_SCRIPT_URL .'&controller=entries&form='. $form_id .'&frm_action=csv&item_id='. implode(',', $items) .'";}</script>';
  398. }
  399. }
  400. }
  401. $this->display_list($params, '', false, false, $errors);
  402. }
  403. /* Front End CRUD */
  404. function process_update_entry($params, $errors, $form){
  405. global $frm_entry, $frm_saved_entries, $frm_created_entry;
  406. $form->options = stripslashes_deep(maybe_unserialize($form->options));
  407. if($params['action'] == 'update' and in_array((int)$params['id'], (array)$frm_saved_entries))
  408. return;
  409. if($params['action'] == 'create' and isset($frm_created_entry[$form->id]) and isset($frm_created_entry[$form->id]['entry_id']) and is_numeric($frm_created_entry[$form->id]['entry_id'])){
  410. $entry_id = $params['id'] = $frm_created_entry[$form->id]['entry_id'];
  411. FrmProEntriesController::set_cookie($entry_id, $form->id);
  412. $conf_method = apply_filters('frm_success_filter', 'message', $form, $form->options);
  413. if ($conf_method == 'redirect'){
  414. //do_action('frm_success_action', $conf_method, $form, $form->options, $params['id']);
  415. $success_url = apply_filters('frm_content', $form->options['success_url'], $form, $entry_id);
  416. $success_url = apply_filters('frm_redirect_url', $success_url, $form, $params);
  417. wp_redirect( $success_url );
  418. exit;
  419. }
  420. }else if ($params['action'] == 'update'){
  421. if(in_array((int)$params['id'], (array)$frm_saved_entries)){
  422. if(isset($_POST['item_meta']))
  423. unset($_POST['item_meta']);
  424. add_filter('frm_continue_to_new', create_function('', "return $continue;"), 15);
  425. return;
  426. }
  427. if (empty($errors)){
  428. if (isset($form->options['editable_role']) and !FrmAppHelper::user_has_permission($form->options['editable_role'])){
  429. global $frm_settings;
  430. wp_die(do_shortcode($frm_settings->login_msg));
  431. }
  432. if (!isset($_POST['frm_page_order_'. $form->id])){
  433. $frm_entry->update( $params['id'], $_POST );
  434. //check confirmation method
  435. $conf_method = apply_filters('frm_success_filter', 'message', $form);
  436. if ($conf_method == 'redirect'){
  437. //do_action('frm_success_action', $conf_method, $form, $form->options, $params['id']);
  438. $success_url = apply_filters('frm_content', $form->options['success_url'], $form, $params['id']);
  439. $success_url = apply_filters('frm_redirect_url', $success_url, $form, $params);
  440. wp_redirect( $success_url );
  441. exit;
  442. }
  443. }
  444. }
  445. }else if ($params['action'] == 'destroy'){
  446. //if the user who created the entry is deleting it
  447. $this->ajax_destroy($form->id, false, false);
  448. }
  449. }
  450. function edit_update_form($params, $fields, $form, $title, $description){
  451. global $frmdb, $wpdb, $frm_entry, $frm_entry_meta, $user_ID, $frm_editing_entry, $frmpro_settings, $frm_saved_entries;
  452. $message = '';
  453. $continue = true;
  454. $form->options = stripslashes_deep(maybe_unserialize($form->options));
  455. if ($params['action'] == 'edit'){
  456. $entry_key = FrmAppHelper::get_param('entry');
  457. $entry_key = esc_sql($entry_key);
  458. if($entry_key){
  459. $in_form = $wpdb->get_var("SELECT id FROM $frmdb->entries WHERE form_id=".(int)$form->id ." AND (id='{$entry_key}' OR item_key='{$entry_key}')");
  460. if(!$in_form)
  461. $entry_key = false;
  462. unset($in_form);
  463. }
  464. $entry = FrmProEntry::user_can_edit($entry_key, $form);
  465. if($entry and !is_array($entry)){
  466. $where = "fr.id='$form->id'";
  467. if ($entry_key)
  468. $where .= ' AND (it.id="'. $entry_key .'" OR it.item_key="'. $entry_key .'")';
  469. $entry = $frm_entry->getAll( $where, '', 1, true);
  470. }
  471. if ($entry and !empty($entry)){
  472. $entry = reset($entry);
  473. $frm_editing_entry = $entry->id;
  474. $this->show_responses($entry, $fields, $form, $title, $description);
  475. $continue = false;
  476. }
  477. }else if ($params['action'] == 'update' and ($params['posted_form_id'] == $form->id)){
  478. global $frm_created_entry;
  479. $errors = $frm_created_entry[$form->id]['errors'];
  480. if (empty($errors)){
  481. if (!isset($_POST['frm_page_order_'. $form->id])){
  482. //check confirmation method
  483. $conf_method = apply_filters('frm_success_filter', 'message', $form);
  484. if ($conf_method == 'message'){
  485. global $frmpro_settings;
  486. $message = '<div class="frm_message" id="message">'. do_shortcode(isset($form->options['edit_msg']) ? $form->options['edit_msg'] : $frmpro_settings->edit_msg).'</div>';
  487. }else{
  488. do_action('frm_success_action', $conf_method, $form, $form->options, $params['id']);
  489. add_filter('frm_continue_to_new', create_function('', "return false;"), 15);
  490. return;
  491. }
  492. }
  493. }else{
  494. $fields = FrmFieldsHelper::get_form_fields($form->id, true);
  495. }
  496. $this->show_responses($params['id'], $fields, $form, $title, $description, $message, $errors);
  497. $continue = false;
  498. }else if ($params['action'] == 'destroy'){
  499. //if the user who created the entry is deleting it
  500. $message = $this->ajax_destroy($form->id, false);
  501. }else if($frm_editing_entry){
  502. if(is_numeric($frm_editing_entry)){
  503. $entry_id = $frm_editing_entry; //get entry from shortcode
  504. }else{
  505. $entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE user_id='$user_ID' and form_id='$form->id'");
  506. if (isset($entry_ids) and !empty($entry_ids)){
  507. $where_options = $frm_editing_entry;
  508. if(!empty($where_options))
  509. $where_options .= ' and ';
  510. $where_options .= "it.item_id in (".implode(',', $entry_ids).")";
  511. $get_meta = $frm_entry_meta->getAll($where_options, ' ORDER BY it.created_at DESC', ' LIMIT 1');
  512. $entry_id = ($get_meta) ? $get_meta->item_id : false;
  513. }
  514. }
  515. if(isset($entry_id) and $entry_id){
  516. if($form->editable and isset($form->options['open_editable']) and $form->options['open_editable'] and isset($form->options['open_editable_role']) and FrmAppHelper::user_has_permission($form->options['open_editable_role']))
  517. $meta = true;
  518. else
  519. $meta = $frmdb->get_var($frmdb->entries, array('user_id' => $user_ID, 'id' => $entry_id, 'form_id' => $form->id ));
  520. if($meta){
  521. $frm_editing_entry = $entry_id;
  522. $this->show_responses($entry_id, $fields, $form, $title, $description);
  523. $continue = false;
  524. }
  525. }
  526. }else{
  527. //check to see if use is allowed to create another entry
  528. $can_submit = true;
  529. if (isset($form->options['single_entry']) and $form->options['single_entry']){
  530. if ($form->options['single_entry_type'] == 'cookie' and isset($_COOKIE['frm_form'. $form->id . '_' . COOKIEHASH])){
  531. $can_submit = false;
  532. }else if ($form->options['single_entry_type'] == 'ip'){
  533. $prev_entry = $frm_entry->getAll(array('it.form_id' => $form->id, 'it.ip' => $_SERVER['REMOTE_ADDR']), '', 1);
  534. if ($prev_entry)
  535. $can_submit = false;
  536. }else if ($form->options['single_entry_type'] == 'user' and !$form->editable and $user_ID){
  537. $meta = $frmdb->get_var($frmdb->entries, array('user_id' => $user_ID, 'form_id' => $form->id ));
  538. if ($meta)
  539. $can_submit = false;
  540. }
  541. if (!$can_submit){
  542. echo stripslashes($frmpro_settings->already_submitted);//TODO: DO SOMETHING IF USER CANNOT RESUBMIT FORM
  543. $continue = false;
  544. }
  545. }
  546. }
  547. add_filter('frm_continue_to_new', create_function('', "return $continue;"), 15);
  548. }
  549. function show_responses($id, $fields, $form, $title=false,$description=false, $message='', $errors=''){
  550. global $frm_form, $frm_field, $frm_entry, $frmpro_entry, $frm_entry_meta, $user_ID, $frmpro_settings, $frm_next_page, $frm_prev_page, $frm_load_css;
  551. if(is_object($id)){
  552. $item = $id;
  553. $id = $item->id;
  554. }else
  555. $item = $frm_entry->getOne($id, true);
  556. $values = FrmAppHelper::setup_edit_vars($item, 'entries', $fields);
  557. if($values['custom_style']) $frm_load_css = true;
  558. $show_form = true;
  559. $submit = (isset($frm_next_page[$form->id])) ? $frm_next_page[$form->id] : (isset($values['edit_value']) ? $values['edit_value'] : $frmpro_settings->update_value);
  560. if(!isset($frm_prev_page[$form->id]) and isset($_POST['item_meta']) and empty($errors) and $form->id == FrmAppHelper::get_param('form_id')){
  561. $form->options = stripslashes_deep(maybe_unserialize($form->options));
  562. $show_form = (isset($form->options['show_form'])) ? $form->options['show_form'] : true;
  563. $conf_method = apply_filters('frm_success_filter', 'message', $form);
  564. if ($conf_method != 'message')
  565. do_action('frm_success_action', $conf_method, $form, $form->options, $id);
  566. }else if(isset($frm_prev_page[$form->id]) or !empty($errors)){
  567. $jump_to_form = true;
  568. }
  569. require(FRMPRO_VIEWS_PATH.'/frmpro-entries/edit-front.php');
  570. add_filter('frm_continue_to_new', array($frmpro_entry, 'frmpro_editing'), 10, 3);
  571. }
  572. function ajax_submit_button($form, $action='create'){
  573. global $frm_novalidate;
  574. if($frm_novalidate)
  575. echo ' formnovalidate="formnovalidate"';
  576. //if form ajax submit
  577. //echo 'onsubmit="return false;" onclick="frm_submit_form(\''.FRM_SCRIPT_URL.'\',jQuery(\'#form_'. $form->form_key .'\').serialize(), \'form_'. $form->form_key .'\')"';
  578. }
  579. function get_confirmation_method($method, $form){
  580. $method = (isset($form->options['success_action']) and !empty($form->options['success_action'])) ? $form->options['success_action'] : $method;
  581. return $method;
  582. }
  583. function confirmation($method, $form, $form_options, $entry_id){
  584. //fire the alternate confirmation options ('page' or 'redirect')
  585. if($method == 'page' and is_numeric($form_options['success_page_id'])){
  586. global $post;
  587. if($form_options['success_page_id'] != $post->ID){
  588. $page = get_post($form_options['success_page_id']);
  589. $old_post = $post;
  590. $post = $page;
  591. $content = apply_filters('frm_content', $page->post_content, $form, $entry_id);
  592. echo apply_filters('the_content', $content);
  593. $post = $old_post;
  594. }
  595. }else if($method == 'redirect'){
  596. $success_url = apply_filters('frm_content', $form_options['success_url'], $form, $entry_id);
  597. $success_msg = isset($form_options['success_msg']) ? stripslashes($form_options['success_msg']) : __('Please wait while you are redirected.', 'formidable');
  598. $redirect_msg = '<div class="frm-redirect-msg frm_message">'. $success_msg .'<br/>'.
  599. sprintf(__('%1$sClick here%2$s if you are not automatically redirected.', 'formidable'), '<a href="'. esc_url($success_url) .'">', '</a>') .
  600. '</div>';
  601. echo apply_filters('frm_redirect_msg', $redirect_msg, array(
  602. 'entry_id' => $entry_id, 'form_id' => $form->id, 'form' => $form
  603. ));
  604. echo "<script type='text/javascript'> jQuery(document).ready(function($){ setTimeout(window.location='". $success_url ."', 5000); });</script>";
  605. }
  606. }
  607. function delete_entry($post_id){
  608. global $frmdb;
  609. $entry = $frmdb->get_one_record($frmdb->entries, array('post_id' => $post_id), 'id');
  610. if($entry){
  611. global $frm_entry;
  612. $frm_entry->destroy($entry->id);
  613. }
  614. }
  615. function untrashed_post($post_id){
  616. $display = FrmProDisplay::get_auto_custom_display(array('form_id' => $id, 'entry_id' => $entry_id));
  617. if($display)
  618. update_post_meta($post->ID, 'frm_display_id', $display->ID);
  619. else
  620. delete_post_meta($post->ID, 'frm_display_id');
  621. }
  622. function create_entry_from_post_box($post_type, $post=false){
  623. if(!$post or !isset($post->ID) or $post_type == 'attachment' or $post_type == 'link')
  624. return;
  625. global $frmdb, $wpdb, $frm_post_forms;
  626. //don't show the meta box if there is already an entry for this post
  627. $post_entry = $wpdb->get_var("SELECT id FROM $frmdb->entries WHERE post_id=". $post->ID);
  628. if($post_entry)
  629. return;
  630. //don't show meta box if no forms are set up to create this post type
  631. $forms = $wpdb->get_results("SELECT id, name FROM $frmdb->forms where options LIKE '%s:9:\"post_type\";s:". strlen($post_type) .":\"". $post_type ."\";%' AND options LIKE '%s:11:\"create_post\";s:1:\"1\";%'");
  632. if(!$forms)
  633. return;
  634. $frm_post_forms = $forms;
  635. add_meta_box('frm_create_entry', __('Create Entry in Form', 'formidable'), array(&$this, 'render_meta_box_content' ), null, 'side');
  636. }
  637. function render_meta_box_content($post){
  638. global $frm_post_forms, $frm_ajax_url;
  639. $count = count($frm_post_forms);
  640. $i = 1;
  641. echo '<p>';
  642. foreach($frm_post_forms as $form){
  643. if($i != 1)
  644. echo ' | ';
  645. $i++;
  646. echo '<a href="javascript:frm_create_post_entry('. $form->id .','. $post->ID .')">'. stripslashes(FrmAppHelper::truncate($form->name, 15)) .'</a>';
  647. unset($form);
  648. }
  649. unset($i);
  650. echo '</p>';
  651. echo "<script type='text/javascript'>function frm_create_post_entry(id,post_id){
  652. jQuery('#frm_create_entry p').replaceWith('<img src=\"". FRM_IMAGES_URL ."/wpspin_light.gif\" alt=\"". __('Loading...', 'formidable') ."\" />');
  653. jQuery.ajax({type:'POST',url:'{$frm_ajax_url}',data:'action=frm_create_post_entry&id='+id+'&post_id='+post_id,
  654. success:function(msg){jQuery('#frm_create_entry').fadeOut('slow');}
  655. });
  656. };</script>";
  657. }
  658. function create_post_entry($id=false, $post_id=false){
  659. if(!$id)
  660. $id = $_POST['id'];
  661. if(!$post_id)
  662. $post_id = $_POST['post_id'];
  663. if(!is_numeric($id) or !is_numeric($post_id))
  664. return;
  665. $post = get_post($post_id);
  666. global $frmdb, $wpdb, $frm_field;
  667. $values = array(
  668. 'description' => __('Copied from Post', 'formidable'),
  669. 'form_id' => $id,
  670. 'created_at' => $post->post_date_gmt,
  671. 'name' => $post->post_title,
  672. 'item_key' => FrmAppHelper::get_unique_key($post->post_name, $frmdb->entries, 'item_key'),
  673. 'user_id' => $post->post_author,
  674. 'post_id' => $post->ID
  675. );
  676. $results = $wpdb->insert( $frmdb->entries, $values );
  677. unset($values);
  678. if($results){
  679. $entry_id = $wpdb->insert_id;
  680. $user_id_field = $frm_field->getAll(array('fi.type' => 'user_id', 'fi.form_id' => $id), '', 1);
  681. if($user_id_field){
  682. $new_values = array(
  683. 'meta_value' => $post->post_author,
  684. 'item_id' => $entry_id,
  685. 'field_id' => $user_id_field->id,
  686. 'created_at' => current_time('mysql', 1)
  687. );
  688. $wpdb->insert( $frmdb->entry_metas, $new_values );
  689. }
  690. $display = FrmProDisplay::get_auto_custom_display(array('form_id' => $id, 'entry_id' => $entry_id));
  691. if($display)
  692. update_post_meta($post->ID, 'frm_display_id', $display->ID);
  693. }
  694. die();
  695. }
  696. /* Export to CSV */
  697. function csv($form_id, $search = '', $fid = ''){
  698. if(!current_user_can('frm_view_entries')){
  699. global $frm_settings;
  700. wp_die($frm_settings->admin_permission);
  701. }
  702. if( !ini_get('safe_mode') ){
  703. set_time_limit(0); //Remove time limit to execute this function
  704. ini_set('memory_limit', '256M');
  705. }
  706. global $current_user, $frm_form, $frm_field, $frm_entry, $frm_entry_meta, $wpdb, $frmpro_settings;
  707. $form = $frm_form->getOne($form_id);
  708. $form_name = sanitize_title_with_dashes($form->name);
  709. $form_cols = $frm_field->getAll("fi.type not in ('divider', 'captcha', 'break', 'html') and fi.form_id=".$form->id, 'field_order ASC');
  710. $item_id = FrmAppHelper::get_param('item_id', false);
  711. $where_clause = "it.form_id=". (int)$form_id;
  712. if($item_id){
  713. $where_clause .= " and it.id in (";
  714. $item_ids = explode(',', $item_id);
  715. foreach((array)$item_ids as $k => $it){
  716. if($k)
  717. $where_clause .= ",";
  718. $where_clause .= $it;
  719. unset($k);
  720. unset($it);
  721. }
  722. $where_clause .= ")";
  723. }else if(!empty($search)){
  724. $where_clause = $this->get_search_str($where_clause, $search, $form_id, $fid);
  725. }
  726. $where_clause = apply_filters('frm_csv_where', $where_clause, compact('form_id'));
  727. $entries = $frm_entry->getAll($where_clause, '', '', true, false);
  728. $filename = date("ymdHis",time()) . '_' . $form_name . '_formidable_entries.csv';
  729. $wp_date_format = apply_filters('frm_csv_date_format', 'Y-m-d H:i:s');
  730. $charset = get_option('blog_charset');
  731. $to_encoding = $frmpro_settings->csv_format;
  732. require(FRMPRO_VIEWS_PATH.'/frmpro-entries/csv.php');
  733. }
  734. /* Display in Back End */
  735. function manage_columns($columns){
  736. global $frm_field, $frm_cols;
  737. $form_id = FrmProAppHelper::get_current_form_id();
  738. $columns['cb'] = '<input type="checkbox" />';
  739. $columns[$form_id .'_id'] = 'ID';
  740. $columns[$form_id .'_item_key'] = __('Entry Key', 'formidable');
  741. $form_cols = $frm_field->getAll("fi.type not in ('divider', 'captcha', 'break', 'html') and fi.form_id=". $form_id, 'field_order ASC');
  742. foreach($form_cols as $form_col)
  743. $columns[$form_id .'_'. $form_col->field_key] = FrmAppHelper::truncate(stripslashes($form_col->name), 35);
  744. $columns[$form_id .'_post_id'] = __('Post', 'formidable');
  745. $columns[$form_id .'_created_at'] = __('Entry creation date', 'formidable');
  746. $columns[$form_id .'_updated_at'] = __('Entry update date', 'formidable');
  747. $columns[$form_id .'_ip'] = 'IP';
  748. //TODO: allow custom order of columns
  749. $frm_cols = $columns;
  750. add_screen_option( 'per_page', array('label' => __('Entries', 'formidable'), 'default' => 20, 'option' => 'formidable_page_formidable_entries_per_page') );
  751. return $columns;
  752. }
  753. function check_hidden_cols($check, $object_id, $meta_key, $meta_value, $prev_value){
  754. global $frm_settings;
  755. if($meta_key != 'manage'. sanitize_title($frm_settings->menu) .'_page_formidable-entriescolumnshidden' or $meta_value == $prev_value)
  756. return $check;
  757. if ( empty($prev_value) )
  758. $prev_value = get_metadata('user', $object_id, $meta_key, true);
  759. global $frm_prev_hidden_cols;
  760. $frm_prev_hidden_cols = ($frm_prev_hidden_cols) ? false : $prev_value; //add a check so we don't create a loop
  761. return $check;
  762. }
  763. //add hidden columns back from other forms
  764. function update_hidden_cols($meta_id, $object_id, $meta_key, $meta_value ){
  765. global $frm_settings;
  766. if($meta_key != 'manage'. sanitize_title($frm_settings->menu) .'_page_formidable-entriescolumnshidden')
  767. return;
  768. global $frm_prev_hidden_cols;
  769. if(!$frm_prev_hidden_cols)
  770. return; //don't continue if there's no previous value
  771. foreach($meta_value as $mk => $mv){
  772. //remove blank values
  773. if(empty($mv))
  774. unset($meta_value[$mk]);
  775. }
  776. $cur_form_prefix = reset($meta_value);
  777. $cur_form_prefix = explode('_', $cur_form_prefix);
  778. $cur_form_prefix = $cur_form_prefix[0];
  779. $save = false;
  780. foreach($frm_prev_hidden_cols as $prev_hidden){
  781. if(empty($prev_hidden) or in_array($prev_hidden, $meta_value)) //don't add blank cols or process included cols
  782. continue;
  783. $form_prefix = explode('_', $prev_hidden);
  784. $form_prefix = $form_prefix[0];
  785. if($form_prefix == $cur_form_prefix) //don't add back columns that are meant to be hidden
  786. continue;
  787. $meta_value[] = $prev_hidden;
  788. $save = true;
  789. unset($form_prefix);
  790. }
  791. if($save){
  792. $user = wp_get_current_user();
  793. update_user_option($user->ID, 'manage'. sanitize_title($frm_settings->menu) .'_page_formidable-entriescolumnshidden', $meta_value, true);
  794. }
  795. }
  796. function save_per_page($save, $option, $value){
  797. if($option == 'formidable_page_formidable_entries_per_page')
  798. $save = (int)$value;
  799. return $save;
  800. }
  801. function sortable_columns(){
  802. $form_id = FrmProAppHelper::get_current_form_id();
  803. return array(
  804. $form_id .'_id' => 'id',
  805. $form_id .'_created_at' => 'created_at',
  806. $form_id .'_updated_at' => 'updated_at',
  807. $form_id .'_ip' => 'ip',
  808. $form_id .'_item_key' => 'item_key'
  809. );
  810. }
  811. function hidden_columns($result){
  812. global $frm_cols;
  813. $form_id = FrmProAppHelper::get_current_form_id();
  814. $return = false;
  815. foreach((array)$result as $r){
  816. if(!empty($r)){
  817. $form_prefix = explode('_', $r);
  818. $form_prefix = $form_prefix[0];
  819. if((int)$form_prefix == (int)$form_id){
  820. $return = true;
  821. break;
  822. }
  823. unset($form_prefix);
  824. }
  825. }
  826. if($return)
  827. return $result;
  828. $i = count($frm_cols);
  829. $max_columns = 8;
  830. if($i > $max_columns){
  831. global $frm_current_form;
  832. if($frm_current_form)
  833. $frm_current_form->options = maybe_unserialize($frm_current_form->options);
  834. if($frm_current_form and isset($frm_current_form->options['hidden_cols']) and !empty($frm_current_form->options['hidden_cols'])){
  835. $result = $frm_current_form->options['hidden_cols'];
  836. }else{
  837. $cols = $frm_cols;
  838. $cols = array_reverse($cols, true);
  839. $result[] = $form_id .'_id';
  840. $i--;
  841. $result[] = $form_id .'_item_key';
  842. $i--;
  843. foreach($cols as $col_key => $col){
  844. if($i > $max_columns)
  845. $result[] = $col_key; //remove some columns by default
  846. $i--;
  847. }
  848. }
  849. }
  850. return $result;
  851. }
  852. function display_list($params=false, $message='', $page_params_ov = false, $current_page_ov = false, $errors = array()){
  853. global $wpdb, $frmdb, $frm_app_helper, $frm_form, $frm_entry, $frm_entry_meta, $frm_page_size, $frm_field, $frm_current_form;
  854. if(!$params)
  855. $params = $this->get_params();
  856. $errors = array();
  857. $form_select = $frm_form->getAll("is_template=0 AND (status is NULL OR status = '' OR status = 'published')", ' ORDER BY name');
  858. if($params['form'])
  859. $form = $frm_form->getOne($params['form']);
  860. else
  861. $form = (isset($form_select[0])) ? $form_select[0] : 0;
  862. if($form){
  863. $params['form'] = $form->id;
  864. $frm_current_form = $form;
  865. $where_clause = " it.form_id=$form->id";
  866. }else{
  867. $where_clause = '';
  868. }
  869. $page_params = "&action=0&frm_action=0&form=";
  870. $page_params .= ($form) ? $form->id : 0;
  871. if ( ! empty( $_REQUEST['s'] ) )
  872. $page_params .= '&s='. urlencode($_REQUEST['s']);
  873. if ( ! empty( $_REQUEST['search'] ) )
  874. $page_params .= '&search='. urlencode($_REQUEST['search']);
  875. if ( ! empty( $_REQUEST['fid'] ) )
  876. $page_params .= '&fid='. $_REQUEST['fid'];
  877. if(class_exists('WP_List_Table')){
  878. require_once(FRMPRO_PATH .'/classes/helpers/FrmProListHelper.php');
  879. $wp_list_table = new FrmProListHelper(array('singular' => 'entry', 'plural' => 'entries', 'table_name' => $frmdb->entries, 'page_name' => 'entries', 'params' => $params));
  880. $pagenum = $wp_list_table->get_pagenum();
  881. $wp_list_table->prepare_items();
  882. $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
  883. if ( $pagenum > $total_pages && $total_pages > 0 ) {
  884. wp_redirect( add_query_arg( 'paged', $total_pages ) );
  885. exit;
  886. }
  887. }else{
  888. $item_vars = $this->get_sort_vars($params, $where_clause);
  889. $page_params .= ($page_params_ov) ? $page_params_ov : $item_vars['page_params'];
  890. if($form){
  891. $form_cols = $frm_field->getAll("fi.type not in ('divider', 'captcha', 'break', 'html') and fi.form_id=". (int)$form->id, 'field_order ASC', ' LIMIT 7');
  892. $record_where = ($item_vars['where_clause'] == " it.form_id=$form->id") ? $form->id : $item_vars['where_clause'];
  893. }else{
  894. $form_cols = array();
  895. $record_where = $item_vars['where_clause'];
  896. }
  897. $current_page = ($current_page_ov) ? $current_page_ov: $params['paged'];
  898. $sort_str = $item_vars['sort_str'];
  899. $sdir_str = $item_vars['sdir_str'];
  900. $search_str = $item_vars['search_str'];
  901. $fid = $item_vars['fid'];
  902. $record_count = $frm_entry->getRecordCount($record_where);
  903. $page_count = $frm_entry->getPageCount($frm_page_size, $record_count);
  904. $items = $frm_entry->getPage($current_page, $frm_page_size, $item_vars['where_clause'], $item_vars['order_by']);
  905. $page_last_record = $frm_app_helper->getLastRecordNum($record_count, $current_page, $frm_page_size);
  906. $page_first_record = $frm_app_helper->getFirstRecordNum($record_count, $current_page, $frm_page_size);
  907. }
  908. require_once(FRMPRO_VIEWS_PATH.'/frmpro-entries/list.php');
  909. }
  910. function get_sort_vars($params=false, $where_clause = ''){
  911. global $frm_entry_meta, $frm_current_form;
  912. if(!$params)
  913. $params = $this->get_params($frm_current_form);
  914. $order_by = '';
  915. $page_params = '';
  916. // These will have to work with both get and post
  917. $sort_str = $params['sort'];
  918. $sdir_str = $params['sdir'];
  919. $search_str = $params['search']

Large files files are truncated, but you can click here to view the full file