PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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'];
  920. $fid = $params['fid'];
  921. // make sure page params stay correct
  922. if(!empty($sort_str))
  923. $page_params .="&sort=$sort_str";
  924. if(!empty($sdir_str))
  925. $page_params .= "&sdir=$sdir_str";
  926. if(!empty($search_str)){
  927. $where_clause = $this->get_search_str($where_clause, $search_str, $params['form'], $fid);
  928. $page_params .= "&search=$search_str";
  929. if(is_numeric($fid))
  930. $page_params .= "&fid=$fid";
  931. }
  932. // Add order by clause
  933. if(is_numeric($sort_str))
  934. $order_by .= " ORDER BY ID"; //update this to order by item meta
  935. else if ($sort_str == "item_key")
  936. $order_by .= " ORDER BY item_key";
  937. else
  938. $order_by .= " ORDER BY ID";
  939. // Toggle ascending / descending
  940. if((empty($sort_str) and empty($sdir_str)) or $sdir_str == 'desc'){
  941. $order_by .= ' DESC';
  942. $sdir_str = 'desc';
  943. }else{
  944. $order_by .= ' ASC';
  945. $sdir_str = 'asc';
  946. }
  947. return compact('order_by', 'sort_str', 'sdir_str', 'fid', 'search_str', 'where_clause', 'page_params');
  948. }
  949. function get_search_str($where_clause='', $search_str, $form_id=false, $fid=false){
  950. global $frm_entry_meta;
  951. $where_item = '';
  952. $join = ' (';
  953. if(!is_array($search_str))
  954. $search_str = explode(" ", $search_str);
  955. foreach($search_str as $search_param){
  956. $search_param = esc_sql( like_escape( $search_param ) );
  957. if(!is_numeric($fid)){
  958. $where_item .= (empty($where_item)) ? ' (' : ' OR';
  959. if(in_array($fid, array('created_at', 'user_id', 'updated_at'))){
  960. if($fid == 'user_id' and !is_numeric($search_param))
  961. $search_param = FrmProAppHelper::get_user_id_param($search_param);
  962. $where_item .= " it.{$fid} like '%$search_param%'";
  963. }else{
  964. $where_item .= " it.name like '%$search_param%' OR it.item_key like '%$search_param%' OR it.description like '%$search_param%' OR it.created_at like '%$search_param%'";
  965. }
  966. }
  967. if(empty($fid) or is_numeric($fid)){
  968. $where_entries = "(meta_value LIKE '%$search_param%'";
  969. if($data_fields = FrmProForm::has_field('data', $form_id, false)){
  970. $df_form_ids = array();
  971. //search the joined entry too
  972. foreach((array)$data_fields as $df){
  973. $df->field_options = maybe_unserialize($df->field_options);
  974. if (is_numeric($df->field_options['form_select']))
  975. $df_form_ids[] = $df->field_options['form_select'];
  976. unset($df);
  977. }
  978. unset($data_fields);
  979. global $wpdb, $frmdb;
  980. $data_form_ids = $wpdb->get_col("SELECT form_id FROM $frmdb->fields WHERE id in (". implode(',', $df_form_ids).")");
  981. unset($df_form_ids);
  982. if($data_form_ids){
  983. $data_entry_ids = $frm_entry_meta->getEntryIds("fi.form_id in (". implode(',', $data_form_ids).") and meta_value LIKE '%". $search_param ."%'");
  984. if(!empty($data_entry_ids))
  985. $where_entries .= " OR meta_value in (".implode(',', $data_entry_ids).")";
  986. }
  987. unset($data_form_ids);
  988. }
  989. $where_entries .= ")";
  990. if(is_numeric($fid))
  991. $where_entries .= " AND fi.id=$fid";
  992. $meta_ids = $frm_entry_meta->getEntryIds($where_entries);
  993. if (!empty($meta_ids)){
  994. if(!empty($where_clause)){
  995. $where_clause .= " AND" . $join;
  996. if(!empty($join)) $join = '';
  997. }
  998. $where_clause .= " it.id in (".implode(',', $meta_ids).")";
  999. }else{
  1000. if(!empty($where_clause)){
  1001. $where_clause .= " AND" . $join;
  1002. if(!empty($join)) $join = '';
  1003. }
  1004. $where_clause .= " it.id=0";
  1005. }
  1006. }
  1007. }
  1008. if(!empty($where_item)){
  1009. $where_item .= ')';
  1010. if(!empty($where_clause))
  1011. $where_clause .= empty($fid) ? ' OR' : ' AND';
  1012. $where_clause .= $where_item;
  1013. if(empty($join))
  1014. $where_clause .= ')';
  1015. }else{
  1016. if(empty($join))
  1017. $where_clause .= ')';
  1018. }
  1019. return $where_clause;
  1020. }
  1021. function get_new_vars($errors = '', $form = '',$message = ''){
  1022. global $frm_form, $frm_field, $frm_entry, $frm_settings, $frm_next_page;
  1023. $title = true;
  1024. $description = true;
  1025. $fields = FrmFieldsHelper::get_form_fields($form->id, !empty($errors));
  1026. $values = FrmEntriesHelper::setup_new_vars($fields, $form);
  1027. $submit = (isset($frm_next_page[$form->id])) ? $frm_next_page[$form->id] : (isset($values['submit_value']) ? $values['submit_value'] : $frm_settings->submit_value);
  1028. require_once(FRMPRO_VIEWS_PATH.'/frmpro-entries/new.php');
  1029. }
  1030. function get_edit_vars($id, $errors = '', $message= ''){
  1031. if(!current_user_can('frm_edit_entries'))
  1032. return $this->show($id);
  1033. global $frm_form, $frm_entry, $frm_field, $frm_next_page, $frmpro_settings, $frm_editing_entry;
  1034. $title = $description = true;
  1035. $record = $frm_entry->getOne( $id, true );
  1036. $frm_editing_entry = $id;
  1037. $form = $frm_form->getOne($record->form_id);
  1038. $fields = FrmFieldsHelper::get_form_fields($form->id, !empty($errors));
  1039. $values = FrmAppHelper::setup_edit_vars($record, 'entries', $fields);
  1040. $submit = (isset($frm_next_page[$form->id])) ? $frm_next_page[$form->id] : (isset($values['edit_value']) ? $values['edit_value'] : $frmpro_settings->update_value);
  1041. require(FRMPRO_VIEWS_PATH.'/frmpro-entries/edit.php');
  1042. }
  1043. function get_params($form=null){
  1044. global $frm_form;
  1045. if(!$form)
  1046. $form = $frm_form->getAll("is_template=0 AND (status is NULL OR status = '' OR status = 'published')", ' ORDER BY name', ' LIMIT 1');
  1047. $values = array();
  1048. foreach (array('id' => '', 'form_name' => '', 'paged' => 1, 'form' => (($form) ? $form->id : 0), 'field_id' => '', 'search' => '', 'sort' => '', 'sdir' => '', 'fid' => '') as $var => $default)
  1049. $values[$var] = FrmAppHelper::get_param($var, $default);
  1050. return $values;
  1051. }
  1052. function &filter_shortcode_value($value, $tag, $atts, $field){
  1053. if(isset($atts['show']) and $atts['show'] == 'value')
  1054. return $value;
  1055. $value = $this->filter_display_value($value, $field);
  1056. return $value;
  1057. }
  1058. function &filter_display_value($value, $field){
  1059. global $frm_entries_controller;
  1060. $value = $frm_entries_controller->filter_display_value($value, $field);
  1061. return $value;
  1062. }
  1063. function route(){
  1064. $action = FrmAppHelper::get_param('frm_action');
  1065. if($action == 'show')
  1066. return $this->show();
  1067. else if($action == 'new')
  1068. return $this->new_entry();
  1069. else if($action == 'create')
  1070. return $this->create();
  1071. else if($action == 'edit')
  1072. return $this->edit();
  1073. else if($action == 'update')
  1074. return $this->update();
  1075. else if($action == 'import')
  1076. return $this->import();
  1077. else if($action == 'duplicate')
  1078. return $this->duplicate();
  1079. else if($action == 'destroy')
  1080. return $this->destroy();
  1081. else if($action == 'destroy_all')
  1082. return $this->destroy_all();
  1083. else if($action == 'list-form')
  1084. return $this->bulk_actions($action);
  1085. else{
  1086. $action = FrmAppHelper::get_param('action');
  1087. if($action == -1)
  1088. $action = FrmAppHelper::get_param('action2');
  1089. if(strpos($action, 'bulk_') === 0){
  1090. if(isset($_GET) and isset($_GET['action']))
  1091. $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action'], '', $_SERVER['REQUEST_URI']);
  1092. if(isset($_GET) and isset($_GET['action2']))
  1093. $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action2'], '', $_SERVER['REQUEST_URI']);
  1094. return $this->bulk_actions($action);
  1095. }else{
  1096. return $this->display_list();
  1097. }
  1098. }
  1099. }
  1100. function get_form_results($atts){
  1101. extract(shortcode_atts(array('id' => false, 'cols' => 99, 'style' => true, 'no_entries' => __('No Entries Found', 'formidable'), 'fields' => false, 'clickable' => false, 'user_id' => false, 'google' => false, 'pagesize' => 20, 'sort' => true, 'edit_link' => false, 'page_id' => false), $atts));
  1102. if (!$id) return;
  1103. global $frm_form, $frm_field, $frm_entry, $frm_entry_meta, $frmpro_settings;
  1104. $form = $frm_form->getOne($id);
  1105. if (!$form) return;
  1106. $where = "fi.type not in ('divider', 'captcha', 'break', 'html') and fi.form_id=". (int)$form->id;
  1107. if($fields){
  1108. $fields = explode(',', $fields);
  1109. $f_list = array();
  1110. foreach($fields as $k => $f){
  1111. $f = trim($f);
  1112. $fields[$k] = $f;
  1113. $f_list[] = esc_sql(like_escape($f));
  1114. unset($k);
  1115. unset($f);
  1116. }
  1117. if(count($fields) == 1 and in_array('id', $fields))
  1118. $where .= ''; //don't search fields if only field id
  1119. else
  1120. $where .= " and (fi.id in ('". implode("','", $f_list) ."') or fi.field_key in ('". implode("','", $f_list) ."'))";
  1121. }
  1122. $fields = (array)$fields;
  1123. $form_cols = $frm_field->getAll($where, 'field_order ASC', $cols);
  1124. unset($where);
  1125. $where = 'it.form_id='. (int)$form->id;
  1126. if($user_id)
  1127. $where .= ' AND user_id='. (int)FrmProAppHelper::get_user_id_param($user_id);
  1128. $s = FrmAppHelper::get_param('frm_search', false);
  1129. if ($s){
  1130. $new_ids = FrmProEntriesHelper::get_search_ids($s, $form->id);
  1131. $where .= ' AND it.id in ('. implode(',', $new_ids) .')';
  1132. }
  1133. if(isset($new_ids) and empty($new_ids))
  1134. $entries = false;
  1135. else
  1136. $entries = $frm_entry->getAll($where, '', '', true, false);
  1137. if($edit_link){
  1138. $anchor = '';
  1139. if(!$page_id){
  1140. global $post;
  1141. $page_id = $post->ID;
  1142. $anchor = '#form_'. $form->form_key;
  1143. }
  1144. if($edit_link === '1')
  1145. $edit_link = __('Edit', 'formidable');
  1146. $permalink = get_permalink($page_id);
  1147. }
  1148. if($style){
  1149. global $frm_load_css;
  1150. $frm_load_css = true;
  1151. }
  1152. $filename = 'table';
  1153. if($google){
  1154. global $frm_google_chart;
  1155. $filename = 'google_table';
  1156. $options = array();
  1157. if($pagesize)
  1158. $options = array('page' => 'enable', 'pageSize' => (int)$pagesize);
  1159. $options['allowHtml'] = true;
  1160. $options['sort'] = ($sort) ? 'enable' : 'disable';
  1161. if($style)
  1162. $options['cssClassNames'] = array('oddTableRow' => 'frm_even');
  1163. }
  1164. ob_start();
  1165. include(FRMPRO_VIEWS_PATH .'/frmpro-entries/'. $filename .'.php');
  1166. $contents = ob_get_contents();
  1167. ob_end_clean();
  1168. if(!$google and $clickable)
  1169. $contents = make_clickable($contents);
  1170. return $contents;
  1171. }
  1172. function get_search($atts){
  1173. extract(shortcode_atts(array('id' => false, 'post_id' => '', 'label' => __('Search', 'formidable')), $atts));
  1174. //if (!$id) return;
  1175. if($post_id == ''){
  1176. global $post;
  1177. if($post)
  1178. $post_id = $post->ID;
  1179. }
  1180. if($post_id != '')
  1181. $action_link = get_permalink($post_id);
  1182. else
  1183. $action_link = '';
  1184. ob_start();
  1185. include(FRMPRO_VIEWS_PATH .'/frmpro-entries/search.php');
  1186. $contents = ob_get_contents();
  1187. ob_end_clean();
  1188. return $contents;
  1189. }
  1190. function entry_link_shortcode($atts){
  1191. global $user_ID, $frm_entry, $frm_entry_meta, $post;
  1192. extract(shortcode_atts(array('id' => false, 'field_key' => 'created_at', 'type' => 'list', 'logged_in' => true, 'edit' => true, 'class' => '', 'link_type' => 'page', 'blank_label' => '', 'param_name' => 'entry', 'param_value' => 'key', 'page_id' => false, 'show_delete' => false), $atts));
  1193. if (!$id or ($logged_in && !$user_ID)) return;
  1194. $id = (int)$id;
  1195. if($show_delete === 1) $show_delete = __('Delete', 'formidable');
  1196. $s = FrmAppHelper::get_param('frm_search', false);
  1197. if($s)
  1198. $entry_ids = FrmProEntriesHelper::get_search_ids($s, $id);
  1199. else
  1200. $entry_ids = $frm_entry_meta->getEntryIds("fi.form_id='$id'");
  1201. if ($entry_ids){
  1202. $id_list = implode(',', $entry_ids);
  1203. $order = ($type == 'collapse') ? ' ORDER BY it.created_at DESC' : '';
  1204. $where = "it.id in ($id_list)";
  1205. if ($logged_in)
  1206. $where .= " and it.form_id='". $id ."' and it.user_id='". (int)$user_ID ."'";
  1207. $entries = $frm_entry->getAll($where, $order, '', true);
  1208. }
  1209. if (!empty($entries)){
  1210. if ($type == 'list'){
  1211. $content = "<ul class='frm_entry_ul $class'>\n";
  1212. }else if($type == 'collapse'){
  1213. $content = '<div class="frm_collapse">';
  1214. $year = $month = '';
  1215. $prev_year = $prev_month = false;
  1216. }else{
  1217. $content = "<select id='frm_select_form_$id' name='frm_select_form_$id' class='$class' onchange='location=this.options[this.selectedIndex].value;'>\n <option value='".get_permalink($post->ID)."'>$blank_label</option>\n";
  1218. }
  1219. global $frm_field;
  1220. if($field_key != 'created_at')
  1221. $field = $frm_field->getOne($field_key);
  1222. foreach ($entries as $entry){
  1223. $action = (isset($_GET) and isset($_GET['frm_action'])) ? 'frm_action' : 'action';
  1224. if(isset($_GET) and isset($_GET[$action]) and $_GET[$action] == 'destroy'){
  1225. if(isset($_GET['entry']) and ($_GET['entry'] == $entry->item_key or $_GET['entry'] == $entry->id))
  1226. continue;
  1227. }
  1228. if($entry->post_id){
  1229. global $wpdb;
  1230. $post_status = $wpdb->get_var("SELECT post_status FROM $wpdb->posts WHERE ID=".$entry->post_id);
  1231. if($post_status != 'publish')
  1232. continue;
  1233. }
  1234. $value = '';
  1235. $meta = false;
  1236. if ($field_key && $field_key != 'created_at'){
  1237. if($entry->post_id and (($field and $field->field_options['post_field']) or $field->type == 'tag'))
  1238. $value = FrmProEntryMetaHelper::get_post_value($entry->post_id, $field->field_options['post_field'], $field->field_options['custom_field'], array('type' => $field->type, 'form_id' => $field->form_id, 'field' => $field));
  1239. else
  1240. $meta = isset($entry->metas[$field_key]) ? $entry->metas[$field_key] : '';
  1241. }else
  1242. $meta = reset($entry->metas);
  1243. $value = ($field_key == 'created_at' or !isset($meta) or !$meta) ? $value : (is_object($meta) ? $meta->meta_value : $meta);
  1244. if(empty($value))
  1245. $value = date_i18n(get_option('date_format'), strtotime($entry->created_at));
  1246. else
  1247. $value = FrmProEntryMetaHelper::display_value($value, $field, array('type' => $field->type, 'show_filename' => false));
  1248. if($param_value == 'key')
  1249. $args = array($param_name => $entry->item_key);
  1250. else
  1251. $args = array($param_name => $entry->id);
  1252. if ($edit)
  1253. $args['frm_action'] = 'edit';
  1254. if ($link_type == 'scroll'){
  1255. $link = '#'.$entry->item_key;
  1256. }else if ($link_type == 'admin'){
  1257. $link = add_query_arg($args, $_SERVER['REQUEST_URI']);
  1258. }else{
  1259. if($page_id)
  1260. $permalink = get_permalink($page_id);
  1261. else
  1262. $permalink = get_permalink($post->ID);
  1263. $link = add_query_arg($args, $permalink);
  1264. }
  1265. unset($args);
  1266. $current = (isset($_GET['entry']) && $_GET['entry'] == $entry->item_key) ? true : false;
  1267. if ($type == 'list'){
  1268. $content .= "<li><a href='$link'>".stripslashes($value)."</a>";
  1269. if($show_delete and isset($permalink) and FrmProEntriesHelper::allow_delete($entry))
  1270. $content .= " <a href='". add_query_arg(array('frm_action' => 'destroy', 'entry' => $entry->id), $permalink) ."' class='frm_delete_list'>$show_delete</a>\n";
  1271. $content .= "</li>\n";
  1272. }else if($type == 'collapse'){
  1273. $new_year = strftime('%G', strtotime($entry->created_at));
  1274. $new_month = strftime('%B', strtotime($entry->created_at));
  1275. if ($new_year != $year){
  1276. if($prev_year){
  1277. if($prev_month) $content .= '</ul></div>';
  1278. $content .= '</div>';
  1279. $prev_month = false;
  1280. }
  1281. $style = ($prev_year) ? " style='display:none'" : '';
  1282. $triangle = ($prev_year) ? "e" : "s";
  1283. $content .= "\n<div class='frm_year_heading frm_year_heading_$id'>
  1284. <span class='ui-icon ui-icon-triangle-1-$triangle'></span>\n
  1285. <a>$new_year</a></div>\n
  1286. <div class='frm_toggle_container' $style>\n";
  1287. $prev_year = true;
  1288. }
  1289. if ($new_month != $month){
  1290. if($prev_month)
  1291. $content .= '</ul></div>';
  1292. $style = ($prev_month) ? " style='display:none'" : '';
  1293. $triangle = ($prev_month) ? "e" : "s";
  1294. $content .= "<div class='frm_month_heading frm_month_heading_$id'>
  1295. <span class='ui-icon ui-icon-triangle-1-$triangle'></span>\n
  1296. <a>$new_month</a>\n</div>\n
  1297. <div class='frm_toggle_container frm_month_listing' $style><ul>\n";
  1298. $prev_month = true;
  1299. }
  1300. $content .= "<li><a href='$link'>".stripslashes($value)."</a></li>";
  1301. $year = $new_year;
  1302. $month = $new_month;
  1303. }else{
  1304. $selected = $current ? ' selected="selected"' : '';
  1305. $content .= "<option value='$link'$selected>" .stripslashes($value) . "</option>\n";
  1306. }
  1307. }
  1308. if ($type == 'list')
  1309. $content .= "</ul>\n";
  1310. else if($type == 'collapse'){
  1311. if($prev_year) $content .= '</div>';
  1312. if($prev_month) $content .= '</ul></div>';
  1313. $content .= '</div>';
  1314. $content .= "<script type='text/javascript'>jQuery(document).ready(function($){ $('.frm_month_heading_". $id . ", .frm_year_heading_". $id ."').toggle(function(){ $(this).children('.ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s'); $(this).children('.ui-icon-triangle-1-s').removeClass('ui-icon-triangle-1-e'); $(this).next('.frm_toggle_container').fadeIn('slow');},function(){ $(this).children('.ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e'); $(this).children('.ui-icon-triangle-1-e').removeClass('ui-icon-triangle-1-s'); $(this).next('.frm_toggle_container').hide();});})</script>\n";
  1315. }else{
  1316. $content .= "</select>\n";
  1317. if($show_delete and isset($_GET) and isset($_GET['entry']) and $_GET['entry'])
  1318. $content .= " <a href='".add_query_arg(array('frm_action' => 'destroy', 'entry' => $_GET['entry']), $permalink) ."' class='frm_delete_list'>$show_delete</a>\n";
  1319. }
  1320. }else
  1321. $content = '';
  1322. return $content;
  1323. }
  1324. function entry_edit_link($atts){
  1325. global $frm_editing_entry, $post, $frm_forms_loaded;
  1326. extract(shortcode_atts(array(
  1327. 'id' => $frm_editing_entry, 'label' => __('Edit', 'formidable'), 'cancel' => __('Cancel', 'formidable'),
  1328. 'class' => '', 'page_id' => (($post) ? $post->ID : 0), 'html_id' => false,
  1329. 'prefix' => '', 'form_id' => false
  1330. ), $atts));
  1331. $link = '';
  1332. $entry_id = ($id and is_numeric($id)) ? $id : FrmAppHelper::get_param('entry', false);
  1333. if($entry_id and !empty($entry_id)){
  1334. if(!$form_id){
  1335. global $frmdb;
  1336. $form_id = (int)$frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'form_id');
  1337. }
  1338. //if user is not allowed to edit, then don't show the link
  1339. if(!FrmProEntry::user_can_edit($entry_id, $form_id))
  1340. return $link;
  1341. if(empty($prefix)){
  1342. $link = add_query_arg(array('frm_action' => 'edit', 'entry' => $entry_id), get_permalink($page_id));
  1343. if($label)
  1344. $link = '<a href="'. $link .'" class="'. $class.'">'. $label .'</a>';
  1345. return $link;
  1346. }
  1347. $action = (isset($_POST) && isset($_POST['frm_action'])) ? 'frm_action' : 'action';
  1348. if (isset($_POST) and isset($_POST[$action]) and ($_POST[$action] =='update') and isset($_POST['form_id']) and ($_POST['form_id'] == $form_id) and isset($_POST['id']) and ($_POST['id'] == $entry_id)){
  1349. global $frm_created_entry;
  1350. $errors = $frm_created_entry[$form_id]['errors'];
  1351. if($errors)
  1352. return FrmAppController::get_form_shortcode(array('id' => $form_id, 'entry_id' => $entry_id));
  1353. $link .= "<script type='text/javascript'>window.onload= function(){var frm_pos=jQuery('#". $prefix . $entry_id ."').offset();window.scrollTo(frm_pos.left,frm_pos.top);}</script>";
  1354. }
  1355. if(!$html_id)
  1356. $html_id = "frm_edit_{$entry_id}";
  1357. $frm_forms_loaded[] = true;
  1358. $link .= "<a href='javascript:frmEditEntry($entry_id,\"". FRM_SCRIPT_URL."\",\"$prefix\",$page_id,$form_id,\"$cancel\",\"$class\")' class='frm_edit_link $class' id='$html_id'>$label</a>\n";
  1359. }
  1360. return $link;
  1361. }
  1362. function entry_update_field($atts){
  1363. global $frm_editing_entry, $post, $frmdb;
  1364. extract(shortcode_atts(array(
  1365. 'id' => $frm_editing_entry, 'field_id' => false, 'form_id' => false,
  1366. 'label' => 'Update', 'class' => '', 'value' => '', 'message' => ''
  1367. ), $atts));
  1368. $link = '';
  1369. $entry_id = (int)($id and is_numeric($id)) ? $id : FrmAppHelper::get_param('entry', false);
  1370. if(!$entry_id or empty($entry_id))
  1371. return $link;
  1372. if(!$form_id)
  1373. $form_id = (int)$frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'form_id');
  1374. if(!FrmProEntry::user_can_edit($entry_id, $form_id))
  1375. return $link;
  1376. if(!is_numeric($field_id))
  1377. $field_id = $frmdb->get_var($frmdb->fields, array('field_key' => $field_id));
  1378. if(!$field_id)
  1379. return 'no field'. $link;
  1380. $link = '<a href="#" onclick="frmUpdateField('. $entry_id .','. $field_id .',\''. $value .'\',\''. $message .'\',\''. FRM_SCRIPT_URL.'\');return false;" id="frm_update_field_'. $entry_id .'_'. $field_id .'" class="frm_update_field_link '. $class .'">'. $label .'</a>';
  1381. return $link;
  1382. }
  1383. function entry_delete_link($atts){
  1384. global $frm_editing_entry, $post, $frm_forms_loaded;
  1385. extract(shortcode_atts(array(
  1386. 'id' => $frm_editing_entry, 'label' => 'Delete',
  1387. 'confirm' => __('Are you sure you want to delete that entry?', 'formidable'),
  1388. 'class' => '', 'page_id' => (($post) ? $post->ID : 0), 'html_id' => false, 'prefix' => ''
  1389. ), $atts));
  1390. $frm_forms_loaded[] = true;
  1391. $link = '';
  1392. $entry_id = ($id and is_numeric($id)) ? $id : (is_admin() ? FrmAppHelper::get_param('id', false) : FrmAppHelper::get_param('entry', false));
  1393. if($entry_id and !empty($entry_id)){
  1394. if(empty($prefix)){
  1395. $action = FrmAppHelper::get_param('frm_action');
  1396. if($action == 'destroy'){
  1397. $entry_key = FrmAppHelper::get_param('entry');
  1398. if(is_numeric($entry_key) and $entry_key == $entry_id){
  1399. $link = FrmProEntriesController::ajax_destroy(false, false, false);
  1400. if(!empty($link)){
  1401. $new_link = '<div class="frm_message">'. $link .'</div>';
  1402. if($link == __('Your entry was successfully deleted', 'formidable'))
  1403. return $new_link;
  1404. else
  1405. $link = $new_link;
  1406. unset($new_link);
  1407. }
  1408. }
  1409. }
  1410. $link .= "<a href='". add_query_arg(array('frm_action' => 'destroy', 'entry' => $entry_id), get_permalink($page_id)) ."' class='$class' onclick='return confirm(\"". $confirm ."\")'>$label</a>\n";
  1411. }else{
  1412. if(!$html_id)
  1413. $html_id = "frm_delete_{$entry_id}";
  1414. $link = "<a href='javascript:frmDeleteEntry($entry_id,\"". FRM_SCRIPT_URL."\",\"$prefix\")' class='frm_delete_link $class' id='$html_id' onclick='return confirm(\"". $confirm ."\")'>$label</a>\n";
  1415. }
  1416. }
  1417. return $link;
  1418. }
  1419. function get_field_value_shortcode($atts){
  1420. extract(shortcode_atts(array('entry_id' => false, 'field_id' => false, 'user_id' => false, 'ip' => false), $atts));
  1421. if(!$field_id or (!$entry_id and !$user_id and !$ip))
  1422. return __('You are missing options in your shortcode. field_id and either user_id, entry_id, or ip are required.', 'formidable');
  1423. global $frm_field, $wpdb, $frmdb;
  1424. $field = $frm_field->getOne($field_id);
  1425. $query = "SELECT post_id, id FROM $frmdb->entries WHERE form_id=$field->form_id and ";
  1426. if($user_id)
  1427. $query .= "user_id=". (int)FrmProAppHelper::get_user_id_param($user_id);
  1428. if($entry_id)
  1429. $query .= "id=". (int)$entry_id;
  1430. if($ip)
  1431. $query .= "ip='". (($ip == true) ? $_SERVER['REMOTE_ADDR'] : $ip) ."'";
  1432. $query .= " ORDER BY created_at DESC LIMIT 1";
  1433. $entry = $wpdb->get_row($query);
  1434. if(!$entry)
  1435. return;
  1436. $atts = array();
  1437. $value = FrmProEntryMetaHelper::get_post_or_meta_value($entry, $field, $atts);
  1438. $value = FrmProEntryMetaHelper::display_value($value, $field,
  1439. array('type' => $field->type, 'post_id' => $entry->post_id, 'entry_id' => $entry->id)
  1440. );
  1441. return $value;
  1442. }
  1443. function show_entry_shortcode($atts){
  1444. extract(shortcode_atts(array(
  1445. 'id' => false, 'entry' => false, 'fields' => false, 'plain_text' => false,
  1446. 'user_info' => false, 'include_blank' => false
  1447. ), $atts));
  1448. global $frmpro_settings, $frm_entry;
  1449. if(!$entry or !is_object($entry)){
  1450. if(!$id)
  1451. return '';
  1452. $entry = $frm_entry->getOne($id, true);
  1453. }
  1454. if(!$fields or !is_array($fields)){
  1455. global $frm_field;
  1456. $fields = $frm_field->getAll(array('fi.form_id' => $entry->form_id), 'field_order');
  1457. }
  1458. $content = '';
  1459. $odd = true;
  1460. if(!$plain_text){
  1461. $content .= "<table cellspacing='0' style='font-size:12px;line-height:135%; border-bottom:{$frmpro_settings->field_border_width} solid #{$frmpro_settings->border_color};'><tbody>\r\n";
  1462. $bg_color = " style='background-color:#{$frmpro_settings->bg_color};'";
  1463. $bg_color_alt = " style='background-color:#{$frmpro_settings->bg_color_active};'";
  1464. }
  1465. foreach($fields as $f){
  1466. if(!isset($entry->metas[$f->id])){
  1467. if(!$include_blank)
  1468. continue;
  1469. $entry->metas[$f->id] = '';
  1470. }
  1471. $prev_val = maybe_unserialize($entry->metas[$f->id]);
  1472. $meta = array('item_id' => $entry->id, 'field_id' => $f->id, 'meta_value' => $prev_val, 'field_type' => $f->type);
  1473. $val = apply_filters('frm_email_value', $prev_val, (object)$meta, $entry);
  1474. if($f->type == 'textarea' and !$plain_text)
  1475. $val = str_replace(array("\r\n", "\r", "\n"), ' <br/>', $val);
  1476. if (is_array($val))
  1477. $val = implode(', ', $val);
  1478. if($plain_text){
  1479. $content .= $f->name . ': ' . $val . "\r\n\r\n";
  1480. }else{
  1481. $row_style = "valign='top' style='text-align:left;color:#{$frmpro_settings->text_color};padding:7px 9px;border-top:{$frmpro_settings->field_border_width} solid #{$frmpro_settings->border_color}'";
  1482. $content .= "<tr".(($odd) ? $bg_color : $bg_color_alt)."><th $row_style>$f->name</th><td $row_style>$val</td></tr>\r\n";
  1483. $odd = ($odd) ? false : true;
  1484. }
  1485. unset($f);
  1486. }
  1487. if($user_info){
  1488. $data = maybe_unserialize($entry->description);
  1489. if($plain_text){
  1490. $content .= "\r\n\r\n" . __('User Information', 'formidable') ."\r\n";
  1491. $content .= __('IP Address', 'formidable') . ": ". $entry->ip ."\r\n";
  1492. $content .= __('User-Agent (Browser/OS)', 'formidable') . ": ". $data['browser']."\r\n";
  1493. $content .= __('Referrer', 'formidable') . ": ". $data['referrer']."\r\n";
  1494. }else{
  1495. $content .= "<tr".(($odd) ? $bg_color : $bg_color_alt)."><th $row_style>". __('IP Address', 'formidable') . "</th><td $row_style>". $entry->ip ."</td></tr>\r\n";
  1496. $odd = ($odd) ? false : true;
  1497. $content .= "<tr".(($odd) ? $bg_color : $bg_color_alt)."><th $row_style>".__('User-Agent (Browser/OS)', 'formidable') . "</th><td $row_style>". $data['browser']."</td></tr>\r\n";
  1498. $odd = ($odd) ? false : true;
  1499. $content .= "<tr".(($odd) ? $bg_color : $bg_color_alt)."><th $row_style>".__('Referrer', 'formidable') . "</th><td $row_style>". str_replace("\r\n", '<br/>', $data['referrer']) ."</td></tr>\r\n";
  1500. }
  1501. }
  1502. if(!$plain_text)
  1503. $content .= "</tbody></table>";
  1504. return $content;
  1505. }
  1506. /* AJAX */
  1507. function set_cookie($entry_id, $form_id){
  1508. global $frm_form;
  1509. $form = $frm_form->getOne($form_id);
  1510. $form->options = maybe_unserialize($form->options);
  1511. $expiration = (isset($form->options['cookie_expiration'])) ? ((float)$form->options['cookie_expiration'] *60*60) : 30000000;
  1512. $expiration = apply_filters('frm_cookie_expiration', $expiration, $form_id, $entry_id);
  1513. setcookie('frm_form'.$form_id.'_' . COOKIEHASH, current_time('mysql', 1), time() + $expiration, COOKIEPATH, COOKIE_DOMAIN);
  1514. }
  1515. function ajax_create(){
  1516. global $frm_entry;
  1517. $errors = $frm_entry->validate($_POST, array('file','rte','captcha'));
  1518. if(empty($errors)){
  1519. echo false;
  1520. }else{
  1521. $errors = str_replace('"', '&quot;', stripslashes_deep($errors));
  1522. $obj = array();
  1523. foreach($errors as $field => $error){
  1524. $field_id = str_replace('field', '', $field);
  1525. $obj[$field_id] = $error;
  1526. }
  1527. echo json_encode($obj);
  1528. }
  1529. die();
  1530. }
  1531. function ajax_update(){
  1532. return $this->ajax_create();
  1533. }
  1534. function ajax_destroy($form_id=false, $ajax=true, $echo=true){
  1535. global $user_ID, $frmdb, $frm_entry, $frm_deleted_entries;
  1536. $entry_key = FrmAppHelper::get_param('entry');
  1537. if(!$form_id)
  1538. $form_id = FrmAppHelper::get_param('form_id');
  1539. if(!$entry_key)
  1540. return;
  1541. if(is_array($frm_deleted_entries) and in_array($entry_key, $frm_deleted_entries))
  1542. return;
  1543. $where = array();
  1544. if(!current_user_can('frm_delete_entries'))
  1545. $where['user_id'] = $user_ID;
  1546. if(is_numeric($entry_key))
  1547. $where['id'] = $entry_key;
  1548. else
  1549. $where['item_key'] = $entry_key;
  1550. $entry = $frmdb->get_one_record( $frmdb->entries, $where, 'id, form_id' );
  1551. if($form_id and $entry and $entry->form_id != (int)$form_id)
  1552. return;
  1553. $entry_id = $entry->id;
  1554. $entry_id = apply_filters('frm_allow_delete', $entry_id, $entry_key, $form_id);
  1555. if(!$entry_id){
  1556. $message = __('There was an error deleting that entry', 'formidable');
  1557. if($echo)
  1558. echo '<div class="frm_message">'. $message .'</div>';
  1559. }else{
  1560. $frm_entry->destroy( $entry_id );
  1561. if(!$frm_deleted_entries)
  1562. $frm_deleted_entries = array();
  1563. $frm_deleted_entries[] = $entry_id;
  1564. if($ajax){
  1565. if($echo)
  1566. echo $message = 'success';
  1567. }else{
  1568. $message = __('Your entry was successfully deleted', 'formidable');
  1569. if($echo)
  1570. echo '<div class="frm_message">'. $message .'</div>';
  1571. }
  1572. }
  1573. return $message;
  1574. }
  1575. function edit_entry_ajax($id, $entry_id=false, $post_id=false){
  1576. global $frm_ajax_edit;
  1577. $frm_ajax_edit = ($entry_id) ? $entry_id : true;
  1578. if($post_id and is_numeric($post_id)){
  1579. global $post;
  1580. if(!$post)
  1581. $post = get_post($post_id);
  1582. }
  1583. global $wp_scripts;
  1584. $wp_scripts->do_items( array('formidable') );
  1585. echo "<script type='text/javascript'>
  1586. //<![CDATA[
  1587. jQuery(document).ready(function($){
  1588. $('#frm_form_". $id ."_container .frm-show-form').submit(function(e){e.preventDefault();window.frmGetFormErrors(this,'". FRM_SCRIPT_URL ."');});
  1589. });
  1590. //]]>
  1591. </script>";
  1592. echo FrmAppController::get_form_shortcode(compact('id', 'entry_id'));
  1593. $frm_ajax_edit = false;
  1594. //if(!isset($_POST) or (!isset($_POST['action']) and !isset($_POST['frm_action])))
  1595. // echo FrmProEntriesController::footer_js();
  1596. die();
  1597. }
  1598. function update_field_ajax($entry_id, $field_id, $value){
  1599. global $frmdb, $wpdb, $frm_field;
  1600. $entry_id = (int)$entry_id;
  1601. if(!$entry_id)
  1602. return false;
  1603. $where = '';
  1604. if(is_numeric($field_id))
  1605. $where .= "fi.id=$field_id";
  1606. else
  1607. $where .= "field_key='$field_id'";
  1608. $field = $frm_field->getAll($where, '', ' LIMIT 1');
  1609. if(!$field or !FrmProEntry::user_can_edit($entry_id, $field->form_id))
  1610. return false;
  1611. $post_id = false;
  1612. $field->field_options = maybe_unserialize($field->field_options);
  1613. if(isset($field->field_options['post_field']) and !empty($field->field_options['post_field']))
  1614. $post_id = $frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'post_id');
  1615. if(!$post_id){
  1616. $updated = $wpdb->update( $frmdb->entry_metas,
  1617. array('meta_value' => $value),
  1618. array('item_id' => $entry_id, 'field_id' => $field_id)
  1619. );
  1620. if(!$updated){
  1621. $wpdb->query($wpdb->prepare("DELETE FROM $frmdb->entry_metas WHERE item_id = %d and field_id = %d", $entry_id, $field_id));
  1622. $updated = FrmEntryMeta::add_entry_meta($entry_id, $field_id, '', $value);
  1623. }
  1624. wp_cache_delete( $entry_id, 'frm_entry');
  1625. }else{
  1626. switch($field->field_options['post_field']){
  1627. case 'post_custom':
  1628. $updated = update_post_meta($post_id, $field->field_options['post_custom'], maybe_serialize($value));
  1629. break;
  1630. case 'post_category':
  1631. $taxonomy = (isset($field->field_options['taxonomy']) and !empty($field->field_options['taxonomy'])) ? $field->field_options['taxonomy'] : 'category';
  1632. $updated = wp_set_post_terms( $post_id, $value, $taxonomy );
  1633. break;
  1634. default:
  1635. $post = get_post($post_id, ARRAY_A);
  1636. $post[$field->field_options['post_field']] = maybe_serialize($value);
  1637. $updated = wp_insert_post( $post );
  1638. }
  1639. }
  1640. do_action('frm_after_update_field', compact('entry_id', 'field_id', 'value'));
  1641. return $updated;
  1642. }
  1643. function send_email($entry_id, $form_id, $type){
  1644. if(current_user_can('frm_view_forms') or current_user_can('frm_edit_forms') or current_user_can('frm_edit_entries')){
  1645. global $frmpro_notification;
  1646. if($type=='autoresponder')
  1647. $sent_to = $frmpro_notification->autoresponder($entry_id, $form_id);
  1648. else
  1649. $sent_to = $frmpro_notification->entry_created($entry_id, $form_id);
  1650. if(is_array($sent_to))
  1651. echo implode(', ', $sent_to);
  1652. else
  1653. echo $sent_to;
  1654. }else{
  1655. _e('No one! You do not have permission', 'formidable');
  1656. }
  1657. }
  1658. }
  1659. ?>