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

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

https://github.com/rafapires/festival-de-ideias
PHP | 1314 lines | 976 code | 244 blank | 94 comment | 238 complexity | 4f2e5b621d86c7a69fb47e0c187bc601 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. /**
  3. * @package Formidable
  4. */
  5. class FrmProDisplaysController{
  6. function FrmProDisplaysController(){
  7. add_action('init', array( &$this, 'register_post_types'), 0);
  8. add_action('admin_menu', array( &$this, 'menu' ), 21);
  9. add_filter('admin_head-post.php', array( &$this, 'highlight_menu' ));
  10. add_filter('admin_head-post-new.php', array( &$this, 'highlight_menu' ));
  11. add_action('restrict_manage_posts', array( &$this, 'switch_form_box'));
  12. add_filter('parse_query', array( &$this, 'filter_forms') );
  13. add_filter('views_edit-frm_display', array( &$this, 'add_form_nav') );
  14. add_filter('post_row_actions', array(&$this, 'post_row_actions'), 10, 2 );
  15. add_filter( 'default_content', array(&$this, 'default_content'), 10, 2 );
  16. add_filter( 'default_title', array(&$this, 'default_title'), 10, 2 );
  17. add_filter( 'default_excerpt', array(&$this, 'default_title'), 10, 2 );
  18. add_action('post_submitbox_misc_actions', array(&$this, 'submitbox_actions'));
  19. add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'), 10, 2);
  20. add_action('save_post', array(&$this, 'save_post'));
  21. add_action('before_delete_post', array(&$this, 'before_delete_post'));
  22. add_filter('the_content', array(&$this, 'get_content'), 8);
  23. add_action('wp_ajax_frm_get_cd_tags_box', array(&$this, 'get_tags_box'));
  24. add_action('wp_ajax_frm_get_entry_select', array(&$this, 'get_entry_select') );
  25. add_action('wp_ajax_frm_get_date_field_select', array(&$this, 'get_date_field_select') );
  26. add_action('wp_ajax_frm_add_where_row', array(&$this, 'get_where_row'));
  27. add_action('wp_ajax_frm_add_where_options', array(&$this, 'get_where_options'));
  28. add_filter('frm_before_display_content', array(&$this, 'calendar_header'), 10, 3);
  29. add_filter('frm_display_entries_content', array(&$this, 'build_calendar'), 10, 5);
  30. add_filter('frm_after_display_content', array(&$this, 'calendar_footer'), 10, 3);
  31. //Shortcodes
  32. add_shortcode('display-frm-data', array(&$this, 'get_shortcode'), 1);
  33. }
  34. function register_post_types(){
  35. register_post_type('frm_display', array(
  36. 'label' => __('Custom Displays', 'formidable'),
  37. 'description' => '',
  38. 'public' => true,
  39. 'exclude_from_search' => true,
  40. 'show_in_nav_menus' => false,
  41. 'show_in_menu' => false,
  42. 'menu_icon' => admin_url('images/icons32.png'),
  43. 'capability_type' => 'page',
  44. 'supports' => array(
  45. 'title', 'revisions'
  46. ),
  47. 'has_archive' => false,
  48. 'labels' => array(
  49. 'name' => __('Custom Displays', 'formidable'),
  50. 'singular_name' => __('Custom Display', 'formidable'),
  51. 'menu_name' => __('Custom Displays', 'formidable'),
  52. 'edit' => __('Edit', 'formidable'),
  53. 'search_items' => __('Search', 'formidable'),
  54. 'not_found' => __('No Custom Displays Found.', 'formidable'),
  55. 'edit_item' => __('Edit Custom Display', 'formidable')
  56. )
  57. ) );
  58. }
  59. function menu(){
  60. global $frm_settings;
  61. add_submenu_page('formidable', 'Formidable | '. __('Custom Displays', 'formidable'), __('Custom Displays', 'formidable'), 'frm_edit_displays', 'edit.php?post_type=frm_display');
  62. if(class_exists('WP_List_Table')){
  63. add_filter('manage_edit-frm_display_columns', array(&$this, 'manage_columns'));
  64. add_filter('manage_edit-frm_display_sortable_columns', array(&$this, 'sortable_columns'));
  65. add_filter('get_user_option_manageedit-frm_displaycolumnshidden', array(&$this, 'hidden_columns'));
  66. add_action('manage_frm_display_posts_custom_column', array(&$this, 'manage_custom_columns'), 10, 2);
  67. }
  68. }
  69. function highlight_menu(){
  70. global $post, $pagenow;
  71. if(($pagenow == 'post-new.php' and isset($_REQUEST['post_type']) and $_REQUEST['post_type'] == 'frm_display') or
  72. (is_object($post) and $post->post_type == 'frm_display')){
  73. echo <<<HTML
  74. <script type="text/javascript">
  75. jQuery(document).ready(function(){
  76. jQuery('#toplevel_page_formidable').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
  77. jQuery('#toplevel_page_formidable a.wp-has-submenu').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
  78. });
  79. </script>
  80. HTML;
  81. }
  82. }
  83. function switch_form_box(){
  84. global $post_type_object;
  85. if(!$post_type_object or $post_type_object->name != 'frm_display')
  86. return;
  87. $form_id = (isset($_GET['form'])) ? $_GET['form'] : '';
  88. echo FrmFormsHelper::forms_dropdown( 'form', $form_id, __('Show All Forms', 'formidable'));
  89. }
  90. function filter_forms($query){
  91. global $pagenow;
  92. if(!is_admin() or $pagenow != 'edit.php' or !isset($_GET['post_type']) or $_GET['post_type'] != 'frm_display')
  93. return $query;
  94. if(isset($_REQUEST['form']) and is_numeric($_REQUEST['form'])){
  95. $query->query_vars['meta_key'] = 'frm_form_id';
  96. $query->query_vars['meta_value'] = (int)$_REQUEST['form'];
  97. }
  98. return $query;
  99. }
  100. function add_form_nav($views){
  101. global $pagenow;
  102. if(!is_admin() or $pagenow != 'edit.php' or !isset($_GET['post_type']) or $_GET['post_type'] != 'frm_display')
  103. return $views;
  104. $form = (isset($_REQUEST['form']) and is_numeric($_REQUEST['form'])) ? $_REQUEST['form'] : false;
  105. if($form) FrmAppController::get_form_nav($form, true);
  106. return $views;
  107. }
  108. function post_row_actions($actions, $post){
  109. if($post->post_type == 'frm_display'){
  110. $actions['duplicate'] = '<a href="'. admin_url('post-new.php?post_type=frm_display&amp;copy_id='. $post->ID) .'" title="'. esc_attr( __( 'Duplicate', 'formidable' ) ) .'">'. __( 'Duplicate', 'formidable' ) .'</a>';
  111. }
  112. return $actions;
  113. }
  114. function create_from_template($path){
  115. global $frmpro_display;
  116. $templates = glob($path."/*.php");
  117. for($i = count($templates) - 1; $i >= 0; $i--){
  118. $filename = str_replace('.php', '', str_replace($path.'/', '', $templates[$i]));
  119. $display = get_page_by_path($filename, OBJECT, 'frm_display');
  120. $values = FrmProDisplaysHelper::setup_new_vars();
  121. $values['display_key'] = $filename;
  122. include_once($templates[$i]);
  123. }
  124. }
  125. function duplicate(){
  126. global $frmpro_display;
  127. $params = $this->get_params();
  128. $record = $frmpro_display->duplicate( $params['id'] );
  129. $message = __('Custom Display was Successfully Copied', 'formidable');
  130. if ($record)
  131. return $this->get_edit_vars($record, '', $message);
  132. else
  133. return $this->display_list($params, __('There was a problem creating new Entry Display settings.', 'formidable'));
  134. }
  135. /*
  136. function bulk_actions($action=''){
  137. $items = $_REQUEST['item-action'];
  138. if($bulkaction == 'export'){
  139. $controller = 'displays';
  140. $ids = $items;
  141. $ids = implode(',', $ids);
  142. include_once(FRMPRO_VIEWS_PATH.'/shared/xml.php');
  143. }
  144. }
  145. */
  146. function manage_columns($columns){
  147. unset($columns['title']);
  148. unset($columns['date']);
  149. $columns['id'] = 'ID';
  150. $columns['title'] = __('Name', 'formidable');
  151. $columns['description'] = __('Description', 'formidable');
  152. $columns['form_id'] = __('Form', 'formidable');
  153. $columns['show_count'] = __('Entry', 'formidable');
  154. $columns['post_id'] = __('Page', 'formidable');
  155. $columns['content'] = __('Content', 'formidable');
  156. $columns['dyncontent'] = __('Dynamic Content', 'formidable');
  157. $columns['date'] = __('Date', 'formidable');
  158. $columns['name'] = __('Key', 'formidable');
  159. $columns['old_id'] = __('Former ID', 'formidable');
  160. $columns['shortcode'] = __('ShortCode', 'formidable');
  161. return $columns;
  162. }
  163. function sortable_columns(){
  164. return array(
  165. 'id' => 'ID',
  166. 'title' => 'post_title',
  167. 'description' => 'post_excerpt',
  168. 'name' => 'post_name',
  169. 'content' => 'post_content',
  170. 'date' => 'post_date',
  171. 'shortcode' => 'ID'
  172. );
  173. }
  174. function hidden_columns($result){
  175. $return = false;
  176. foreach((array)$result as $r){
  177. if(!empty($r)){
  178. $return = true;
  179. break;
  180. }
  181. }
  182. if($return)
  183. return $result;
  184. $result[] = 'post_id';
  185. $result[] = 'content';
  186. $result[] = 'dyncontent';
  187. $result[] = 'old_id';
  188. return $result;
  189. }
  190. function manage_custom_columns($column_name, $id){
  191. $val = '';
  192. switch ( $column_name ) {
  193. case 'id':
  194. $val = $id;
  195. break;
  196. case 'old_id':
  197. $old_id = get_post_meta($id, 'frm_old_id', true);
  198. $val = ($old_id) ? $old_id : __('N/A', 'formidable');
  199. break;
  200. case 'name':
  201. case 'content':
  202. $post = get_post($id);
  203. $val = FrmAppHelper::truncate(strip_tags($post->{"post_$column_name"}), 100);
  204. break;
  205. case 'description':
  206. $post = get_post($id);
  207. $val = FrmAppHelper::truncate(strip_tags($post->post_excerpt), 100);
  208. break;
  209. case 'show_count':
  210. $val = ucwords(get_post_meta($id, 'frm_'. $column_name, true));
  211. break;
  212. case 'dyncontent':
  213. $val = FrmAppHelper::truncate(strip_tags(get_post_meta($id, 'frm_'. $column_name, true)), 100);
  214. break;
  215. case 'form_id':
  216. global $frm_form;
  217. $form_id = get_post_meta($id, 'frm_'. $column_name, true);
  218. $form = $frm_form->getName($form_id);
  219. if($form)
  220. $val = '<a href="'. admin_url('admin.php') .'?page=formidable&frm_action=edit&id='. $form_id .'">'. FrmAppHelper::truncate(stripslashes($form), 40) .'</a>';
  221. else
  222. $val = '';
  223. break;
  224. case 'post_id':
  225. $insert_loc = get_post_meta($id, 'frm_insert_loc', true);
  226. if(!$insert_loc or $insert_loc == 'none'){
  227. $val = '';
  228. break;
  229. }
  230. $post_id = get_post_meta($id, 'frm_'. $column_name, true);
  231. $auto_post = get_post($post_id);
  232. if($auto_post)
  233. $val = '<a href="'. admin_url('post.php') .'?post='. $post_id .'&amp;action=edit">'. FrmAppHelper::truncate($auto_post->post_title, 50) .'</a>';
  234. else
  235. $val = '';
  236. break;
  237. case 'shortcode':
  238. $code = "[display-frm-data id={$id} filter=1]";
  239. $val = "<input type='text' style='font-size:10px;width:100%;' readonly='true' onclick='this.select();' onfocus='this.select();' value='{$code}' />";
  240. break;
  241. default:
  242. $val = $column_name;
  243. break;
  244. }
  245. echo $val;
  246. }
  247. function submitbox_actions(){
  248. global $post;
  249. if($post->post_type != 'frm_display')
  250. return;
  251. include(FRMPRO_VIEWS_PATH.'/displays/submitbox_actions.php');
  252. }
  253. function default_content($content, $post){
  254. if($post->post_type == 'frm_display' and isset($_GET) and isset($_GET['copy_id'])){
  255. global $frmpro_display, $copy_display;
  256. $copy_display = $frmpro_display->getOne($_GET['copy_id']);
  257. if($copy_display)
  258. $content = $copy_display->post_content;
  259. }
  260. return $content;
  261. }
  262. function default_title($title, $post){
  263. if($post->post_type == 'frm_display' and isset($_GET) and isset($_GET['copy_id'])){
  264. global $copy_display;
  265. if($copy_display)
  266. $title = $copy_display->post_title;
  267. }
  268. return $title;
  269. }
  270. function default_excerpt($excerpt, $post){
  271. if($post->post_type == 'frm_display' and isset($_GET) and isset($_GET['copy_id'])){
  272. global $copy_display;
  273. if($copy_display)
  274. $excerpt = $copy_display->post_excerpt;
  275. }
  276. return $excerpt;
  277. }
  278. function add_meta_boxes($post_type, $post=false){
  279. if($post_type != 'frm_display')
  280. return;
  281. add_meta_box('frm_form_disp_type', __('Form and Display Type', 'formidable'), array(&$this, 'mb_form_disp_type'), 'frm_display', 'normal', 'high');
  282. add_meta_box('frm_dyncontent', __('Content', 'formidable'), array(&$this, 'mb_dyncontent'), 'frm_display', 'normal', 'high');
  283. add_meta_box('frm_excerpt', __('Description', 'formidable'), array(&$this, 'mb_excerpt'), 'frm_display', 'normal', 'high');
  284. add_meta_box('frm_advanced', __('Advanced', 'formidable'), array(&$this, 'mb_advanced'), 'frm_display', 'advanced');
  285. add_meta_box('frm_adv_info', __('Content Customization', 'formidable'), array(&$this, 'mb_adv_info'), 'frm_display', 'side', 'low');
  286. }
  287. function save_post($post_id){
  288. //Verify nonce
  289. if (empty($_POST) or (isset($_POST['frm_save_display']) and !wp_verify_nonce($_POST['frm_save_display'], 'frm_save_display_nonce')) or !isset($_POST['post_type']) or $_POST['post_type'] != 'frm_display' or (defined('DOING_AUTOSAVE') and DOING_AUTOSAVE) or !current_user_can('edit_post', $post_id))
  290. return;
  291. $post = get_post($post_id);
  292. if($post->post_status == 'inherit')
  293. return;
  294. global $frmpro_display;
  295. $record = $frmpro_display->update( $post_id, $_POST );
  296. do_action('frm_create_display', $post_id, $_POST);
  297. }
  298. function before_delete_post($post_id){
  299. $post = get_post($post_id);
  300. if($post->post_type != 'frm_display')
  301. return;
  302. global $wpdb, $frmpro_display;
  303. $used_by = $wpdb->get_col("SELECT post_ID FROM $wpdb->postmeta WHERE meta_key='frm_display_id' AND meta_value=$post_id");
  304. if(!$used_by)
  305. return;
  306. $form_id = get_post_meta($post_id, 'frm_form_id', true);
  307. $next_display = $frmpro_display->get_auto_custom_display(compact('form_id'));
  308. if($next_display and $next_display->ID){
  309. $wpdb->update($wpdb->postmeta,
  310. array('meta_value' => $next_display->ID),
  311. array('meta_key' => 'frm_display_id', 'meta_value' => $post_id)
  312. );
  313. }else{
  314. $wpdb->delete($wpdb->postmeta, array('meta_key' => 'frm_display_id', 'meta_value' => $post_id));
  315. }
  316. }
  317. /* META BOXES */
  318. function mb_dyncontent($post){
  319. global $frmpro_displays_helper, $copy_display;
  320. if($copy_display and isset($_GET) and isset($_GET['copy_id']))
  321. $post = $copy_display;
  322. $post = $frmpro_displays_helper->setup_edit_vars($post);
  323. include(FRMPRO_VIEWS_PATH.'/displays/mb_dyncontent.php');
  324. }
  325. function mb_excerpt($post){
  326. include(FRMPRO_VIEWS_PATH.'/displays/mb_excerpt.php');
  327. //add form nav via javascript
  328. $form = get_post_meta($post->ID, 'frm_form_id', true);
  329. if($form){
  330. echo '<div id="frm_nav_container" style="display:none;">';
  331. FrmAppController::get_form_nav($form, true);
  332. echo '</div>';
  333. echo '<script type="text/javascript">jQuery(document).ready(function($){ $(".wrap h2").after( $("#frm_nav_container").show());})</script>';
  334. }
  335. }
  336. function mb_form_disp_type($post){
  337. global $frmpro_displays_helper, $frm_ajax_url, $frm_siteurl, $frmpro_settings, $copy_display;
  338. if($copy_display and isset($_GET) and isset($_GET['copy_id']))
  339. $post = $copy_display;
  340. $post = $frmpro_displays_helper->setup_edit_vars($post);
  341. include(FRMPRO_VIEWS_PATH.'/displays/mb_form_disp_type.php');
  342. }
  343. function mb_advanced($post){
  344. global $frmpro_displays_helper, $frm_ajax_url, $copy_display;
  345. if($copy_display and isset($_GET) and isset($_GET['copy_id']))
  346. $post = $copy_display;
  347. $post = $frmpro_displays_helper->setup_edit_vars($post);
  348. include(FRMPRO_VIEWS_PATH.'/displays/mb_advanced.php');
  349. }
  350. function mb_adv_info($post){
  351. global $frmpro_displays_helper, $copy_display;
  352. if($copy_display and isset($_GET) and isset($_GET['copy_id']))
  353. $post = $copy_display;
  354. $post = $frmpro_displays_helper->setup_edit_vars($post);
  355. $this->mb_tags_box($post->frm_form_id);
  356. }
  357. function mb_tags_box($form_id){
  358. global $frm_field, $frmdb;
  359. $fields = array();
  360. if($form_id)
  361. $fields = $frm_field->getAll("fi.type not in ('divider','captcha','break','html') and fi.form_id=". (int)$form_id, 'field_order');
  362. $linked_forms = array();
  363. $col = 'one';
  364. $cond_shortcodes = array(
  365. 'equals=&#34;something&#34;' => __('Equals', 'formidable'),
  366. 'not_equal=&#34;something&#34;' => __('Does Not Equal', 'formidable'),
  367. 'equals=&#34;&#34;' => __('Is Blank', 'formidable'),
  368. 'not_equal=&#34;&#34;' => __('Is Not Blank', 'formidable'),
  369. 'like=&#34;something&#34;' => __('Is Like', 'formidable'),
  370. 'not_like=&#34;something&#34;' => __('Is Not Like', 'formidable'),
  371. 'greater_than=&#34;3&#34;' => __('Greater Than', 'formidable'),
  372. 'less_than=&#34;-1 month&#34;' => __('Less Than', 'formidable')
  373. );
  374. $adv_shortcodes = array(
  375. 'sep=&#34;, &#34;' => array('label' => __('Separator', 'formidable'), 'title' => __('Use a different separator for checkbox fields', 'formidable') ),
  376. 'clickable=1' => __('Clickable Links', 'formidable'),
  377. 'sanitize=1' => array('label' => __('Sanitize', 'formidable'), 'title' => __('Replaces spaces with dashes and lowercases all. Use if adding an HTML class or ID', 'formidable')),
  378. 'sanitize_url=1' => array('label' => __('Sanitize URL', 'formidable'), 'title' => __('Replaces all HTML entities with a URL safe string.', 'formidable')),
  379. 'truncate=40' => array('label' => __('Truncate', 'formidable'), 'title' => __('Truncate text with a link to view more. If using Both (dynamic), the link goes to the detail page. Otherwise, it will show in-place.', 'formidable')),
  380. 'truncate=100 more_text=&#34;More&#34;' => __('More Text', 'formidable'),
  381. 'time_ago=1' => array('label' => __('Time Ago', 'formidable'), 'title' => __('How long ago a date was in minutes, hours, days, months, or years.', 'formidable')),
  382. 'format=&#34;d-m-Y&#34;' => __('Date Format', 'formidable'),
  383. 'decimal=2 dec_point=&#34.&#34 thousands_sep=&#34,&#34' => __('Number Format', 'formidable'),
  384. 'show=&#34;field_label&#34;' => __('Field Label', 'formidable'),
  385. 'show=&#34;value&#34;' => array('label' => __('Saved Value', 'formidable'), 'title' => __('Show the saved value for fields with separate values.', 'formidable') ),
  386. 'wpautop=0' => __('No Auto P', 'formidable')
  387. );
  388. // __('Leave blank instead of defaulting to User Login', 'formidable') : blank=1
  389. $user_fields = array(
  390. 'ID' => __('User ID', 'formidable'), 'first_name' => __('First Name', 'formidable'),
  391. 'last_name' => __('Last Name', 'formidable'), 'display_name' => __('Display Name', 'formidable'),
  392. 'user_login' => __('User Login', 'formidable'), 'user_email' => __('Email', 'formidable'),
  393. 'avatar' => __('Avatar', 'formidable')
  394. );
  395. include(FRMPRO_VIEWS_PATH.'/displays/mb_adv_info.php');
  396. }
  397. function get_tags_box(){
  398. $this->mb_tags_box($_POST['form_id']);
  399. die();
  400. }
  401. /* FRONT END */
  402. function get_content($content){
  403. global $post, $frmpro_display;
  404. if(!$post) return $content;
  405. $display = $entry_id = false;
  406. if($post->post_type == 'frm_display' and in_the_loop()){
  407. global $frm_displayed;
  408. if(!$frm_displayed)
  409. $frm_displayed = array();
  410. if(in_array($post->ID, $frm_displayed))
  411. return $content;
  412. $frm_displayed[] = $post->ID;
  413. $display = FrmProDisplaysHelper::setup_edit_vars($post, false);
  414. return $this->get_display_data($post, $content, false, array('filter' => true));
  415. }
  416. $display_id = get_post_meta($post->ID, 'frm_display_id', true);
  417. if(!$display_id or (!is_single() and !is_page()))
  418. return $content;
  419. $display = $frmpro_display->getOne($display_id);
  420. if ($display){
  421. global $frm_displayed, $frm_display_position;
  422. if($post->post_type != 'frm_display')
  423. $display = FrmProDisplaysHelper::setup_edit_vars($display, false);
  424. if(!isset($display->frm_insert_pos))
  425. $display->frm_insert_pos = 1;
  426. if(!$frm_displayed)
  427. $frm_displayed = array();
  428. if(!$frm_display_position)
  429. $frm_display_position = array();
  430. if(!isset($frm_display_position[$display->ID]))
  431. $frm_display_position[$display->ID] = 0;
  432. $frm_display_position[$display->ID]++;
  433. //make sure this isn't loaded multiple times but still works with themes and plugins that call the_content multiple times
  434. if(in_the_loop() and !in_array($display->ID, (array)$frm_displayed) and $frm_display_position[$display->ID] >= (int)$display->frm_insert_pos){
  435. global $frmdb, $wpdb;
  436. if((is_single() or is_page()) and $post->post_type != 'frm_display'){
  437. $entry = $wpdb->get_row("SELECT id, item_key FROM $frmdb->entries WHERE post_id={$post->ID}");
  438. if(!$entry)
  439. return $content;
  440. $entry_id = $entry->id;
  441. if(in_array($display->frm_show_count, array('dynamic', 'calendar')) and $display->frm_type == 'display_key')
  442. $entry_id = $entry->item_key;
  443. }
  444. $frm_displayed[] = $display->ID;
  445. $content = $this->get_display_data($display, $content, $entry_id, array('filter' => true));
  446. }
  447. }
  448. return $content;
  449. }
  450. function get_where_row(){
  451. $this->add_where_row($_POST['where_key'], $_POST['form_id']);
  452. die();
  453. }
  454. function add_where_row($where_key='', $form_id='', $where_field='', $where_is='', $where_val=''){
  455. require(FRMPRO_VIEWS_PATH .'/displays/where_row.php');
  456. }
  457. function get_where_options(){
  458. $this->add_where_options($_POST['field_id'],$_POST['where_key']);
  459. die();
  460. }
  461. function add_where_options($field_id, $where_key, $where_val=''){
  462. global $frm_field;
  463. if(is_numeric($field_id)){
  464. $field = $frm_field->getOne($field_id);
  465. $field->field_options = maybe_unserialize($field->field_options);
  466. }
  467. require(FRMPRO_VIEWS_PATH .'/displays/where_options.php');
  468. }
  469. function calendar_header($content, $display, $show='one'){
  470. if($display->frm_show_count != 'calendar' or $show == 'one') return $content;
  471. global $frm_load_css, $wp_locale;
  472. $frm_load_css = true;
  473. $year = FrmAppHelper::get_param('frmcal-year', date_i18n('Y')); //4 digit year
  474. $month = FrmAppHelper::get_param('frmcal-month', date_i18n('m')); //Numeric month without leading zeros
  475. $month_names = $wp_locale->month;
  476. $prev_year = $next_year = $year;
  477. $prev_month = $month-1;
  478. $next_month = $month+1;
  479. if ($prev_month == 0 ) {
  480. $prev_month = 12;
  481. $prev_year = $year - 1;
  482. }
  483. if ($next_month == 13 ) {
  484. $next_month = 1;
  485. $next_year = $year + 1;
  486. }
  487. if($next_month < 10)
  488. $next_month = '0'. $next_month;
  489. if($prev_month < 10)
  490. $prev_month = '0'. $prev_month;
  491. ob_start();
  492. include(FRMPRO_VIEWS_PATH.'/displays/calendar-header.php');
  493. $content .= ob_get_contents();
  494. ob_end_clean();
  495. return $content;
  496. }
  497. function build_calendar($new_content, $entries, $shortcodes, $display, $show='one'){
  498. if(!$display or $display->frm_show_count != 'calendar') return $new_content;
  499. global $frm_entry_meta, $wp_locale;
  500. $current_year = date_i18n('Y');
  501. $current_month = date_i18n('n');
  502. $year = FrmAppHelper::get_param('frmcal-year', date('Y')); //4 digit year
  503. $month = FrmAppHelper::get_param('frmcal-month', $current_month); //Numeric month without leading zeros
  504. $timestamp = mktime(0, 0, 0, $month, 1, $year);
  505. $maxday = date('t', $timestamp); //Number of days in the given month
  506. $this_month = getdate($timestamp);
  507. $startday = $this_month['wday'];
  508. if($current_year == $year and $current_month == $month)
  509. $today = date_i18n('j');
  510. $cal_end = $maxday+$startday;
  511. $t = ($cal_end > 35) ? 42 : (($cal_end == 28) ? 28 : 35);
  512. $extrarows = $t-$maxday-$startday;
  513. $show_entres = false;
  514. $daily_entries = array();
  515. if(isset($display->frm_date_field_id) and is_numeric($display->frm_date_field_id))
  516. $field = FrmField::getOne($display->frm_date_field_id);
  517. if(isset($display->frm_edate_field_id) and is_numeric($display->frm_edate_field_id))
  518. $efield = FrmField::getOne($display->frm_edate_field_id);
  519. else
  520. $efield = false;
  521. foreach ($entries as $entry){
  522. if(isset($display->frm_date_field_id) and is_numeric($display->frm_date_field_id)){
  523. if(isset($entry->metas))
  524. $date = isset($entry->metas[$display->frm_date_field_id]) ? $entry->metas[$display->frm_date_field_id] : false;
  525. else
  526. $date = $frm_entry_meta->get_entry_meta_by_field($entry->id, $display->frm_date_field_id);
  527. if($entry->post_id and !$date){
  528. if($field){
  529. $field->field_options = maybe_unserialize($field->field_options);
  530. if($field->field_options['post_field']){
  531. $date = FrmProEntryMetaHelper::get_post_value($entry->post_id, $field->field_options['post_field'], $field->field_options['custom_field'], array('form_id' => $display->frm_form_id, 'type' => $field->type, 'field' => $field));
  532. }
  533. }
  534. }
  535. }else if($display->frm_date_field_id == 'updated_at'){
  536. $date = $entry->updated_at;
  537. $i18n = true;
  538. }else{
  539. $date = $entry->created_at;
  540. $i18n = true;
  541. }
  542. if(empty($date)) continue;
  543. if(isset($il8n) and $il8n)
  544. $date = date_i18n('Y-m-d', strtotime($date));
  545. else
  546. $date = date('Y-m-d', strtotime($date));
  547. unset($i18n);
  548. $dates = array($date);
  549. if(isset($display->frm_edate_field_id) and !empty($display->frm_edate_field_id)){
  550. if(is_numeric($display->frm_edate_field_id) and $efield){
  551. $edate = FrmProEntryMetaHelper::get_post_or_meta_value($entry, $efield);
  552. if($efield and $efield->type == 'number' and is_numeric($edate))
  553. $edate = date('Y-m-d', strtotime('+'. $edate .' days', strtotime($date)));
  554. }else if($display->frm_edate_field_id == 'updated_at'){
  555. $edate = date_i18n('Y-m-d', strtotime($entry->updated_at));
  556. }else{
  557. $edate = date_i18n('Y-m-d', strtotime($entry->created_at));
  558. }
  559. if($edate and !empty($edate)){
  560. $from_date = strtotime($date);
  561. $to_date = strtotime($edate);
  562. if(!empty($from_date) and $from_date < $to_date){
  563. for($current_ts = $from_date; $current_ts <= $to_date; $current_ts += (60*60*24))
  564. $dates[] = date('Y-m-d', $current_ts);
  565. unset($current_ts);
  566. }
  567. unset($from_date);
  568. unset($to_date);
  569. }
  570. unset($edate);
  571. $used_entries = array();
  572. }
  573. unset($date);
  574. $dates = apply_filters('frm_show_entry_dates', $dates, $entry);
  575. for ($i=0; $i<($maxday+$startday); $i++){
  576. $day = $i - $startday + 1;
  577. if(in_array(date('Y-m-d', strtotime("$year-$month-$day")), $dates)){
  578. $show_entres = true;
  579. $daily_entres[$i][] = $entry;
  580. }
  581. unset($day);
  582. }
  583. unset($dates);
  584. }
  585. // week_begins = 0 stands for Sunday
  586. $week_begins = apply_filters('frm_cal_week_begins', intval(get_option('start_of_week')), $display);
  587. $week_ends = 6 + (int)$week_begins;
  588. if($week_ends > 6)
  589. $week_ends = (int)$week_ends - 7;
  590. $day_names = $wp_locale->weekday_abbrev;
  591. $day_names = FrmProAppHelper::reset_keys($day_names); //switch keys to order
  592. if($week_begins){
  593. for ($i=$week_begins; $i<($week_begins+7); $i++){
  594. if(!isset($day_names[$i]))
  595. $day_names[$i] = $day_names[$i-7];
  596. }
  597. unset($i);
  598. }
  599. ob_start();
  600. include(FRMPRO_VIEWS_PATH.'/displays/calendar.php');
  601. $content = ob_get_contents();
  602. ob_end_clean();
  603. return $content;
  604. }
  605. function calendar_footer($content, $display, $show='one'){
  606. if($display->frm_show_count != 'calendar' or $show == 'one') return $content;
  607. ob_start();
  608. include(FRMPRO_VIEWS_PATH.'/displays/calendar-footer.php');
  609. $content = ob_get_contents();
  610. ob_end_clean();
  611. return $content;
  612. }
  613. function get_entry_select(){
  614. echo FrmEntriesHelper::entries_dropdown($_POST['form_id'], 'entry_id');
  615. die();
  616. }
  617. function get_date_field_select(){
  618. if(is_numeric($_POST['form_id'])){
  619. echo '<option value="created_at">'. __('Entry creation date', 'formidable') .'</option>';
  620. echo '<option value="updated_at">'. __('Entry update date', 'formidable') .'</option>';
  621. FrmProFieldsHelper::get_field_options($_POST['form_id'], '', '', "'date'");
  622. }
  623. die();
  624. }
  625. function get_params(){
  626. $values = array();
  627. foreach (array('template' => 0, 'id' => '', 'paged' => 1, 'form' => '', 'search' => '', 'sort' => '', 'sdir' => '') as $var => $default)
  628. $values[$var] = FrmAppHelper::get_param($var, $default);
  629. return $values;
  630. }
  631. /* ShortCodes */
  632. function get_shortcode($atts){
  633. global $frmpro_display;
  634. $defaults = array(
  635. 'id' => '', 'entry_id' => '', 'filter' => false,
  636. 'user_id' => false, 'limit' => '', 'page_size' => '',
  637. 'order_by' => '', 'order' => '', 'get' => '', 'get_value' => ''
  638. );
  639. extract(shortcode_atts($defaults, $atts));
  640. $display = $frmpro_display->getOne($id, false, true);
  641. $user_id = FrmProAppHelper::get_user_id_param($user_id);
  642. if(!empty($get))
  643. $_GET[$get] = $get_value;
  644. foreach($defaults as $unset => $val){
  645. unset($atts[$unset]);
  646. unset($unset);
  647. unset($val);
  648. }
  649. foreach($atts as $att => $val){
  650. $_GET[$att] = $val;
  651. unset($att);
  652. unset($val);
  653. }
  654. if ($display)
  655. return FrmProDisplaysController::get_display_data($display, '', $entry_id, compact('filter', 'user_id', 'limit', 'page_size', 'order_by', 'order'));
  656. else
  657. return __('That is not a valid custom display ID', 'formidable');
  658. }
  659. function custom_display($id){
  660. global $frmpro_display;
  661. if ($display = $frmpro_display->getOne($id))
  662. return $this->get_display_data($display);
  663. }
  664. function get_display_data($display, $content='', $entry_id=false, $extra_atts=array()){
  665. global $frmpro_display, $frm_entry, $frmpro_settings, $frm_entry_meta, $frm_forms_loaded, $post;
  666. $frm_forms_loaded[] = true;
  667. if(!isset($display->frm_form_id))
  668. $display = FrmProDisplaysHelper::setup_edit_vars($display, false);
  669. if(!isset($display->frm_form_id))
  670. return $content;
  671. //for backwards compatability
  672. $display->id = $display->ID;
  673. $display->display_key = $display->post_name;
  674. $defaults = array(
  675. 'filter' => false, 'user_id' => '', 'limit' => '',
  676. 'page_size' => '', 'order_by' => '', 'order' => ''
  677. );
  678. extract(wp_parse_args( $extra_atts, $defaults ));
  679. //if (FrmProAppHelper::rewriting_on() && $frmpro_settings->permalinks )
  680. // $this->parse_pretty_entry_url();
  681. if (is_numeric($display->frm_entry_id) and $display->frm_entry_id > 0 and !$entry_id)
  682. $entry_id = $display->frm_entry_id;
  683. $entry = false;
  684. $show = 'all';
  685. if (in_array($display->frm_show_count, array('dynamic', 'calendar', 'one'))){
  686. $one_param = (isset($_GET['entry'])) ? $_GET['entry'] : $entry_id;
  687. $get_param = (isset($_GET[$display->frm_param])) ? $_GET[$display->frm_param] : (($display->frm_show_count == 'one') ? $one_param : $entry_id);
  688. unset($one_param);
  689. if ($get_param){
  690. $where_entry = array('it.form_id' => $display->frm_form_id);
  691. if(($display->frm_type == 'id' or $display->frm_show_count == 'one') and is_numeric($get_param))
  692. $where_entry['it.id'] = $get_param;
  693. else
  694. $where_entry['it.item_key'] = $get_param;
  695. $entry = $frm_entry->getAll($where_entry, '', 1, 0);
  696. if($entry)
  697. $entry = reset($entry);
  698. if($entry and $entry->post_id){
  699. //redirect to single post page if this entry is a post
  700. if(in_the_loop() and $display->frm_show_count != 'one' and !is_single($entry->post_id) and $post->ID != $entry->post_id){
  701. $this_post = get_post($entry->post_id);
  702. if(in_array($this_post->post_status, array('publish', 'private')))
  703. die('<script type="text/javascript">window.location="'. get_permalink($entry->post_id) .'"</script>');
  704. }
  705. }
  706. }
  707. unset($get_param);
  708. }
  709. if($entry and in_array($display->frm_show_count, array('dynamic', 'calendar'))){
  710. $new_content = stripslashes($display->frm_dyncontent);
  711. $show = 'one';
  712. }else{
  713. $new_content = stripslashes($display->post_content);
  714. }
  715. $show = ($display->frm_show_count == 'one' or ($entry_id and is_numeric($entry_id))) ? 'one' : $show;
  716. $shortcodes = FrmProDisplaysHelper::get_shortcodes($new_content, $display->frm_form_id);
  717. //don't let page size and limit override single entry displays
  718. if($display->frm_show_count == 'one')
  719. $display->frm_page_size = $display->frm_limit = '';
  720. //don't keep current content if post type is frm_display
  721. if($post->post_type == 'frm_display')
  722. $display->frm_insert_loc = '';
  723. $pagination = '';
  724. if ($entry and $entry->form_id == $display->frm_form_id){
  725. $display_content = FrmProFieldsHelper::replace_shortcodes($new_content, $entry, $shortcodes, $display, $show);
  726. }else{
  727. global $frmdb, $wpdb;
  728. $empty_msg = '<div class="frm_no_entries">'. (isset($display->frm_empty_msg) ? stripslashes($display->frm_empty_msg) : '') .'</div>';
  729. $display_content = '';
  730. if($show == 'all')
  731. $display_content .= isset($display->frm_before_content) ? stripslashes($display->frm_before_content) : '';
  732. $display_content = apply_filters('frm_before_display_content', $display_content, $display, $show);
  733. $where = 'it.form_id='. $display->frm_form_id;
  734. $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $display->frm_form_id, 'post_id >' => 1), '', '', 'id,post_id');
  735. $entry_ids = $frmdb->get_col($frmdb->entries, array('form_id' => $display->frm_form_id), 'id');
  736. $after_where = false;
  737. if($user_id and !empty($user_id)){
  738. $user_id = FrmProAppHelper::get_user_id_param($user_id);
  739. $uid_used = false;
  740. }
  741. if(isset($display->frm_where) and !empty($display->frm_where)){
  742. $display->frm_where = apply_filters('frm_custom_where_opt', $display->frm_where, array('display' => $display, 'entry' => $entry));
  743. $continue = false;
  744. foreach($display->frm_where as $where_key => $where_opt){
  745. $where_val = isset($display->frm_where_val[$where_key]) ? $display->frm_where_val[$where_key] : '';
  746. if (preg_match("/\[(get|get-(.?))\b(.*?)(?:(\/))?\]/s", $where_val)){
  747. $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true);
  748. //if this param doesn't exist, then don't include it
  749. if($where_val == '') {
  750. if(!$after_where)
  751. $continue = true;
  752. continue;
  753. }
  754. }else{
  755. $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true);
  756. }
  757. $continue = false;
  758. if($where_val == 'current_user'){
  759. if($user_id and is_numeric($user_id)){
  760. $where_val = $user_id;
  761. $uid_used = true;
  762. }else{
  763. global $user_ID;
  764. $where_val = $user_ID;
  765. }
  766. }
  767. $where_val = do_shortcode($where_val);
  768. if(is_array($where_val) and !empty($where_val)){
  769. $new_where = '(';
  770. if(strpos($display->frm_where_is[$where_key], 'LIKE') !== false){
  771. foreach($where_val as $w){
  772. if($new_where != '(')
  773. $new_where .= ',';
  774. $new_where .= "'%". esc_sql(like_escape($w)). "%'";
  775. unset($w);
  776. }
  777. }else{
  778. foreach($where_val as $w){
  779. if($new_where != '(')
  780. $new_where .= ',';
  781. $new_where .= "'". esc_sql($w) ."'";
  782. unset($w);
  783. }
  784. }
  785. $new_where .= ')';
  786. $where_val = $new_where;
  787. unset($new_where);
  788. if(strpos($display->frm_where_is[$where_key], '!') === false and strpos($display->frm_where_is[$where_key], 'not') === false)
  789. $display->frm_where_is[$where_key] = ' in ';
  790. else
  791. $display->frm_where_is[$where_key] = ' not in ';
  792. }
  793. if(is_numeric($where_opt)){
  794. $entry_ids = FrmProAppHelper::filter_where($entry_ids, array(
  795. 'where_opt' => $where_opt, 'where_is' => $display->frm_where_is[$where_key],
  796. 'where_val' => $where_val, 'form_id' => $display->frm_form_id, 'form_posts' => $form_posts,
  797. 'after_where' => $after_where, 'display' => $display
  798. ));
  799. $after_where = true;
  800. $continue = false;
  801. if(empty($entry_ids))
  802. break;
  803. }else if($where_opt == 'created_at'){
  804. if($where_val == 'NOW')
  805. $where_val = current_time('mysql', 1);
  806. $where_val = date('Y-m-d H:i:s', strtotime($where_val));
  807. $where .= " and it.created_at ". $display->frm_where_is[$where_key];
  808. if(strpos($display->frm_where_is[$where_key], 'in'))
  809. $where .= " $where_val";
  810. else
  811. $where .= " '". esc_sql($where_val) ."'";
  812. $continue = true;
  813. }else if($where_opt == 'id' or $where_opt == 'item_key'){
  814. $where .= " and it.{$where_opt} ". $display->frm_where_is[$where_key];
  815. if(strpos($display->frm_where_is[$where_key], 'in'))
  816. $where .= " $where_val";
  817. else
  818. $where .= " '". esc_sql($where_val) ."'";
  819. $continue = true;
  820. }
  821. }
  822. if(!$continue and empty($entry_ids)){
  823. if ($display->frm_insert_loc == 'after'){
  824. $content .= $empty_msg;
  825. }else if ($display->frm_insert_loc == 'before'){
  826. $content = $empty_msg . $content;
  827. }else{
  828. if ($filter)
  829. $empty_msg = apply_filters('the_content', $empty_msg);
  830. $content .= $empty_msg;
  831. }
  832. return $content;
  833. }
  834. }
  835. if($user_id and is_numeric($user_id) and !$uid_used)
  836. $where .= " AND it.user_id=". (int)$user_id;
  837. $s = FrmAppHelper::get_param('frm_search', false);
  838. if ($s){
  839. $new_ids = FrmProEntriesHelper::get_search_ids($s, $display->frm_form_id);
  840. if($after_where and isset($entry_ids) and !empty($entry_ids))
  841. $entry_ids = array_intersect($new_ids, $entry_ids);
  842. else
  843. $entry_ids = $new_ids;
  844. if(empty($entry_ids))
  845. return $content . ' '. $empty_msg;
  846. }
  847. if(isset($entry_ids) and !empty($entry_ids))
  848. $where .= ' and it.id in ('.implode(',', $entry_ids).')';
  849. if ($entry_id)
  850. $where .= " and it.id in ($entry_id)";
  851. if($show == 'one'){
  852. $limit = ' LIMIT 1';
  853. }else if (isset($_GET['frm_cat']) and isset($_GET['frm_cat_id'])){
  854. //Get fields with specified field value 'frm_cat' = field key/id, 'frm_cat_id' = order position of selected option
  855. global $frm_field;
  856. if ($cat_field = $frm_field->getOne($_GET['frm_cat'])){
  857. $categories = maybe_unserialize($cat_field->options);
  858. if (isset($categories[$_GET['frm_cat_id']]))
  859. $cat_entry_ids = $frm_entry_meta->getEntryIds("meta_value='". $categories[$_GET['frm_cat_id']] ."' and fi.field_key='$_GET[frm_cat]'");
  860. if ($cat_entry_ids)
  861. $where .= " and it.id in (". implode(',', $cat_entry_ids) .")";
  862. }
  863. }
  864. if (!empty($limit) and is_numeric($limit))
  865. $display->frm_limit = (int)$limit;
  866. if (is_numeric($display->frm_limit)){
  867. $num_limit = (int)$display->frm_limit;
  868. $limit = ' LIMIT '. $display->frm_limit;
  869. }
  870. if (!empty($order_by)){
  871. $display->frm_order_by = $order_by;
  872. $order_by = '';
  873. }
  874. if (!empty($order))
  875. $display->frm_order = $order;
  876. if (isset($display->frm_order_by) && $display->frm_order_by != ''){
  877. $order = (isset($display->frm_order)) ? ' '. $display->frm_order : '';
  878. if ($display->frm_order_by == 'rand'){
  879. $order_by = ' RAND()';
  880. }else if (is_numeric($display->frm_order_by)){
  881. global $frm_entry_meta, $frm_field;
  882. $order_field = $frm_field->getOne($display->frm_order_by);
  883. $order_field->field_options = maybe_unserialize($order_field->field_options);
  884. $meta_order = ($order_field->type == 'number') ? ' LENGTH(meta_value),' : '';
  885. if(isset($order_field->field_options['post_field']) and $order_field->field_options['post_field']){
  886. $posts = $form_posts; //$frmdb->get_records($frmdb->entries, array('form_id' => $display->form_id, 'post_id >' => 1), '', '', 'id, post_id');
  887. $linked_posts = array();
  888. foreach($posts as $post_meta)
  889. $linked_posts[$post_meta->post_id] = $post_meta->id;
  890. if($order_field->field_options['post_field'] == 'post_custom'){
  891. $ordered_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='". $order_field->field_options['custom_field'] ."' AND post_id in (". implode(',', array_keys($linked_posts)).") ORDER BY meta_value". $order);
  892. $metas = array();
  893. foreach($ordered_ids as $ordered_id)
  894. $metas[] = array('item_id' => $linked_posts[$ordered_id]);
  895. }else if($order_field->field_options['post_field'] != 'post_category'){
  896. $ordered_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE ID in (". implode(',', array_keys($linked_posts)).") ORDER BY ". $order_field->field_options['post_field'] .' '. $order);
  897. $metas = array();
  898. foreach($ordered_ids as $ordered_id)
  899. $metas[] = array('item_id' => $linked_posts[$ordered_id]);
  900. }
  901. }else{
  902. if($order_field->type == 'number'){
  903. $query = "SELECT it.*, meta_value +0 as odr FROM $frmdb->entry_metas it LEFT OUTER JOIN $frmdb->fields fi ON it.field_id=fi.id WHERE fi.form_id=$display->frm_form_id and fi.id={$display->frm_order_by}";
  904. if(isset($entry_ids) and !empty($entry_ids))
  905. $query .= " AND it.item_id in (". implode(',', $entry_ids) .")";
  906. $query .= " ORDER BY odr $order $limit";
  907. $metas = $wpdb->get_results($query);
  908. }else{
  909. $metas = $frm_entry_meta->getAll('fi.form_id='. $display->frm_form_id .' and fi.id='. $display->frm_order_by, ' ORDER BY '. $meta_order .' meta_value'.$order); //TODO: add previous $where and $limit
  910. }
  911. }

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