PageRenderTime 36ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ajency/Myshala
PHP | 1122 lines | 497 code | 114 blank | 511 comment | 160 complexity | 0030cf213ccf7af02aa263dd27aeb8e0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Formidable
  4. */
  5. class FrmProDisplaysController{
  6. function FrmProDisplaysController(){
  7. add_action('admin_menu', array( &$this, 'menu' ), 21);
  8. add_filter('frm_nav_array', array( &$this, 'frm_nav'), 2);
  9. add_filter('the_content', array(&$this, 'get_content'), 8);
  10. add_action('wp_ajax_frm_get_field_tags', array(&$this, 'get_tags') );
  11. add_action('wp_ajax_frm_get_entry_select', array(&$this, 'get_entry_select') );
  12. add_action('wp_ajax_frm_add_where_row', array(&$this, 'get_where_row'));
  13. add_action('wp_ajax_frm_add_where_options', array(&$this, 'get_where_options'));
  14. add_filter('frm_before_display_content', array(&$this, 'calendar_header'), 10, 3);
  15. add_filter('frm_display_entries_content', array(&$this, 'build_calendar'), 10, 5);
  16. add_filter('frm_after_display_content', array(&$this, 'calendar_footer'), 10, 3);
  17. add_filter('set-screen-option', array(&$this, 'save_per_page'), 10, 3);
  18. //Shortcodes
  19. add_shortcode('display-frm-data', array(&$this, 'get_shortcode'), 1);
  20. }
  21. function menu(){
  22. global $frm_settings;
  23. add_submenu_page('formidable', 'Formidable | '. __('Custom Displays', 'formidable'), __('Custom Displays', 'formidable'), 'frm_edit_displays', 'formidable-entry-templates', array(&$this, 'route'));
  24. if(class_exists('WP_List_Table') and (!isset($_GET['frm_action']) or $_GET['frm_action'] != 'edit')){
  25. add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-entry-templates_columns', array(&$this, 'manage_columns'));
  26. add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-entry-templates_sortable_columns', array(&$this, 'sortable_columns'));
  27. add_filter('get_user_option_manage'. sanitize_title($frm_settings->menu) .'_page_formidable-entry-templatescolumnshidden', array(&$this, 'hidden_columns'));
  28. }
  29. }
  30. function frm_nav($nav){
  31. if(current_user_can('frm_edit_displays')){
  32. $nav['formidable-entry-templates'] = __('Custom Displays', 'formidable');
  33. //$nav['formidable-entry-templates&frm_action=new'] = __('New Custom Display', 'formidable');
  34. }
  35. return $nav;
  36. }
  37. function get_content($content){
  38. global $post, $frmpro_display;
  39. if(!$post) return $content;
  40. $display = $entry_id = false;
  41. if(is_single() or is_page()){
  42. global $frmdb, $frmprodb;
  43. //get entry with post_id
  44. $entry = $frmdb->get_one_record($frmdb->entries, array('post_id' => $post->ID), $fields='id,item_key,form_id');
  45. if($entry){
  46. $display = FrmProDisplay::getAll( "form_id=".$entry->form_id." and show_count in ('single', 'dynamic', 'calendar')", '', ' LIMIT 1' );
  47. $entry_id = $entry->id;
  48. }
  49. }
  50. if(!$display)
  51. $display = $frmpro_display->getAll("insert_loc != 'none' and post_id=".$post->ID, '', ' LIMIT 1');
  52. if ($display){
  53. global $frm_displayed, $frm_display_position;
  54. $display->options = maybe_unserialize($display->options);
  55. if(!isset($display->options['insert_pos']))
  56. $display->options['insert_pos'] = 1;
  57. if(!$frm_displayed)
  58. $frm_displayed = array();
  59. if(!$frm_display_position)
  60. $frm_display_position = array();
  61. if(!isset($frm_display_position[$display->id]))
  62. $frm_display_position[$display->id] = 0;
  63. $frm_display_position[$display->id]++;
  64. //make sure this isn't loaded multiple times but still works with themes and plugins that call the_content multiple times
  65. if(in_the_loop() and !in_array($display->id, (array)$frm_displayed) and $frm_display_position[$display->id] >= (int)$display->options['insert_pos']){
  66. $entry_id = (in_array($display->show_count, array('dynamic', 'calendar')) and $display->type == 'display_key') ? ($entry_id ? $entry->item_key : $entry_id) : $entry_id;
  67. $frm_displayed[] = $display->id;
  68. $content = $this->get_display_data($display, $content, $entry_id);
  69. }
  70. }
  71. return $content;
  72. }
  73. function new_form(){
  74. global $frmpro_display, $frmpro_settings, $frm_settings, $frm_siteurl, $frm_ajax_url;
  75. $values = FrmProDisplaysHelper::setup_new_vars();
  76. $submit = __('Create', 'formidable');
  77. require_once(FRMPRO_VIEWS_PATH.'/displays/new.php');
  78. }
  79. function create(){
  80. global $frmpro_display, $frmpro_settings, $frm_settings, $frm_siteurl, $frm_ajax_url;
  81. $errors = $frmpro_display->validate($_POST);
  82. if( count($errors) > 0 ){
  83. $submit = __('Create', 'formidable');
  84. $values = FrmProDisplaysHelper::setup_new_vars();
  85. require_once(FRMPRO_VIEWS_PATH.'/displays/new.php');
  86. }else{
  87. $_SERVER['REQUEST_URI'] = str_replace('&frm_action=new', '', $_SERVER['REQUEST_URI']);
  88. if($record = $frmpro_display->create( $_POST ))
  89. $message = __('Custom Display was Successfully Created', 'formidable');
  90. else
  91. $message = __('Oops! There was a problem saving your Custom Display. Please try deactivating and reactivating Formidable to correct the problem.', 'formidable');
  92. return $this->display_list($this->get_params(), $message);
  93. }
  94. }
  95. function create_from_template($path){
  96. global $frmpro_display;
  97. $templates = glob($path."/*.php");
  98. for($i = count($templates) - 1; $i >= 0; $i--){
  99. $filename = str_replace('.php', '', str_replace($path.'/', '', $templates[$i]));
  100. $display = $frmpro_display->getAll(array('display_key' => $filename), '', 1);
  101. $values = FrmProDisplaysHelper::setup_new_vars();
  102. $values['display_key'] = $filename;
  103. include_once($templates[$i]);
  104. }
  105. }
  106. function edit(){
  107. $id = FrmAppHelper::get_param('id');
  108. return $this->get_edit_vars($id);
  109. }
  110. function update(){
  111. global $frmpro_display, $frmpro_settings;
  112. $errors = $frmpro_display->validate($_POST);
  113. $id = FrmAppHelper::get_param('id');
  114. if( count($errors) > 0 ){
  115. return $this->get_edit_vars($id, $errors);
  116. }else{
  117. $record = $frmpro_display->update( $id, $_POST );
  118. $message = __('Custom Display was Successfully Updated', 'formidable');
  119. return $this->get_edit_vars($id, '', $message);
  120. }
  121. }
  122. function duplicate(){
  123. global $frmpro_display;
  124. $params = $this->get_params();
  125. $record = $frmpro_display->duplicate( $params['id'] );
  126. $message = __('Custom Display was Successfully Copied', 'formidable');
  127. if ($record)
  128. return $this->get_edit_vars($record, '', $message);
  129. else
  130. return $this->display_list($params, __('There was a problem creating new Entry Display settings.', 'formidable'));
  131. }
  132. function destroy(){
  133. global $frmpro_display;
  134. $params = $this->get_params();
  135. $message = '';
  136. if ($frmpro_display->destroy( $params['id'] ))
  137. $message = __('Custom Display was Successfully Deleted', 'formidable');
  138. $this->display_list($params, $message, '', 1);
  139. }
  140. function bulk_actions($action=''){
  141. $params = $this->get_params();
  142. $errors = '';
  143. $bulkaction = '-1';
  144. if($action == 'list-form'){
  145. if($_POST['bulkaction'] != '-1')
  146. $bulkaction = $_POST['bulkaction'];
  147. else if($_POST['bulkaction2'] != '-1')
  148. $bulkaction = $_POST['bulkaction2'];
  149. }else{
  150. $bulkaction = str_replace('bulk_', '', $action);
  151. }
  152. if (empty($_REQUEST['item-action'])){
  153. $errors[] = __('No displays were specified', 'formidable');
  154. }else{
  155. $items = $_REQUEST['item-action'];
  156. if($bulkaction == 'delete'){
  157. if(!current_user_can('frm_edit_displays')){
  158. global $frm_settings;
  159. $errors[] = $frm_settings->admin_permission;
  160. }else{
  161. global $frmpro_display;
  162. if(is_array($items)){
  163. if($bulkaction == 'delete'){
  164. foreach($items as $item_id)
  165. $frmpro_display->destroy($item_id);
  166. }
  167. }
  168. }
  169. }else if($bulkaction == 'export'){
  170. $controller = 'displays';
  171. $ids = $items;
  172. $ids = implode(',', $ids);
  173. include_once(FRMPRO_VIEWS_PATH.'/shared/xml.php');
  174. }
  175. }
  176. $this->display_list($params, '', false, false, $errors);
  177. }
  178. function get_where_row(){
  179. $this->add_where_row($_POST['where_key'], $_POST['form_id']);
  180. die();
  181. }
  182. function add_where_row($where_key='', $form_id='', $where_field='', $where_is='', $where_val=''){
  183. require(FRMPRO_VIEWS_PATH .'/displays/where_row.php');
  184. }
  185. function get_where_options(){
  186. $this->add_where_options($_POST['field_id'],$_POST['where_key']);
  187. die();
  188. }
  189. function add_where_options($field_id, $where_key, $where_val=''){
  190. global $frm_field;
  191. if(is_numeric($field_id)){
  192. $field = $frm_field->getOne($field_id);
  193. $field->field_options = maybe_unserialize($field->field_options);
  194. }
  195. require(FRMPRO_VIEWS_PATH .'/displays/where_options.php');
  196. }
  197. function calendar_header($content, $display, $show='one'){
  198. if($display->show_count != 'calendar' or $show == 'one') return $content;
  199. global $frm_load_css, $wp_locale;
  200. $frm_load_css = true;
  201. $year = FrmAppHelper::get_param('frmcal-year', date_i18n('Y')); //4 digit year
  202. $month = FrmAppHelper::get_param('frmcal-month', date_i18n('m')); //Numeric month without leading zeros
  203. $month_names = $wp_locale->month;
  204. $prev_year = $next_year = $year;
  205. $prev_month = $month-1;
  206. $next_month = $month+1;
  207. if ($prev_month == 0 ) {
  208. $prev_month = 12;
  209. $prev_year = $year - 1;
  210. }
  211. if ($next_month == 13 ) {
  212. $next_month = 1;
  213. $next_year = $year + 1;
  214. }
  215. if($next_month < 10)
  216. $next_month = '0'. $next_month;
  217. if($prev_month < 10)
  218. $prev_month = '0'. $prev_month;
  219. ob_start();
  220. include(FRMPRO_VIEWS_PATH.'/displays/calendar-header.php');
  221. $content .= ob_get_contents();
  222. ob_end_clean();
  223. return $content;
  224. }
  225. function build_calendar($new_content, $entries, $shortcodes, $display, $show='one'){
  226. if(!$display or $display->show_count != 'calendar') return $new_content;
  227. global $frm_entry_meta, $wp_locale;
  228. $display_options = maybe_unserialize($display->options);
  229. $current_year = date_i18n('Y');
  230. $current_month = date_i18n('n');
  231. $year = FrmAppHelper::get_param('frmcal-year', date('Y')); //4 digit year
  232. $month = FrmAppHelper::get_param('frmcal-month', $current_month); //Numeric month without leading zeros
  233. $timestamp = mktime(0, 0, 0, $month, 1, $year);
  234. $maxday = date('t', $timestamp); //Number of days in the given month
  235. $this_month = getdate($timestamp);
  236. $startday = $this_month['wday'];
  237. if($current_year == $year and $current_month == $month)
  238. $today = date_i18n('j');
  239. $cal_end = $maxday+$startday;
  240. $t = ($cal_end > 35) ? 42 : (($cal_end == 28) ? 28 : 35);
  241. $extrarows = $t-$maxday-$startday;
  242. $show_entres = false;
  243. $daily_entries = array();
  244. if(isset($display_options['date_field_id']) and is_numeric($display_options['date_field_id']))
  245. $field = FrmField::getOne($display_options['date_field_id']);
  246. if(isset($display_options['edate_field_id']) and is_numeric($display_options['edate_field_id']))
  247. $efield = FrmField::getOne($display_options['edate_field_id']);
  248. else
  249. $efield = false;
  250. foreach ($entries as $entry){
  251. if(isset($display_options['date_field_id']) and is_numeric($display_options['date_field_id'])){
  252. if(isset($entry->metas))
  253. $date = isset($entry->metas[$display_options['date_field_id']]) ? $entry->metas[$display_options['date_field_id']] : false;
  254. else
  255. $date = $frm_entry_meta->get_entry_meta_by_field($entry->id, $display_options['date_field_id']);
  256. if($entry->post_id and !$date){
  257. if($field){
  258. $field->field_options = maybe_unserialize($field->field_options);
  259. if($field->field_options['post_field']){
  260. $date = FrmProEntryMetaHelper::get_post_value($entry->post_id, $field->field_options['post_field'], $field->field_options['custom_field'], array('form_id' => $display->form_id, 'type' => $field->type, 'field' => $field));
  261. }
  262. }
  263. }
  264. }else if($display_options['date_field_id'] == 'updated_at'){
  265. $date = $entry->updated_at;
  266. $i18n = true;
  267. }else{
  268. $date = $entry->created_at;
  269. $i18n = true;
  270. }
  271. if(empty($date)) continue;
  272. if(isset($il8n) and $il8n)
  273. $date = date_i18n('Y-m-d', strtotime($date));
  274. else
  275. $date = date('Y-m-d', strtotime($date));
  276. unset($i18n);
  277. $dates = array($date);
  278. if(isset($display_options['edate_field_id']) and !empty($display_options['edate_field_id'])){
  279. if(is_numeric($display_options['edate_field_id']) and $efield){
  280. $edate = FrmProEntryMetaHelper::get_post_or_meta_value($entry, $efield);
  281. if($efield and $efield->type == 'number' and is_numeric($edate))
  282. $edate = date('Y-m-d', strtotime('+'. $edate .' days', strtotime($date)));
  283. }else if($display_options['edate_field_id'] == 'updated_at'){
  284. $edate = date_i18n('Y-m-d', strtotime($entry->updated_at));
  285. }else{
  286. $edate = date_i18n('Y-m-d', strtotime($entry->created_at));
  287. }
  288. if($edate and !empty($edate)){
  289. $from_date = strtotime($date);
  290. $to_date = strtotime($edate);
  291. if(!empty($from_date) and $from_date < $to_date){
  292. for($current_ts = $from_date; $current_ts <= $to_date; $current_ts += (60*60*24))
  293. $dates[] = date('Y-m-d', $current_ts);
  294. unset($current_ts);
  295. }
  296. unset($from_date);
  297. unset($to_date);
  298. }
  299. unset($edate);
  300. $used_entries = array();
  301. }
  302. unset($date);
  303. $dates = apply_filters('frm_show_entry_dates', $dates, $entry);
  304. for ($i=0; $i<($maxday+$startday); $i++){
  305. $day = $i - $startday + 1;
  306. if(in_array(date('Y-m-d', strtotime("$year-$month-$day")), $dates)){
  307. $show_entres = true;
  308. $daily_entres[$i][] = $entry;
  309. }
  310. unset($day);
  311. }
  312. unset($dates);
  313. }
  314. $day_names = $wp_locale->weekday_abbrev;
  315. $day_names = FrmProAppHelper::reset_keys($day_names); //switch keys to order
  316. ob_start();
  317. include(FRMPRO_VIEWS_PATH.'/displays/calendar.php');
  318. $content = ob_get_contents();
  319. ob_end_clean();
  320. return $content;
  321. }
  322. function calendar_footer($content, $display, $show='one'){
  323. if($display->show_count != 'calendar' or $show == 'one') return $content;
  324. ob_start();
  325. include(FRMPRO_VIEWS_PATH.'/displays/calendar-footer.php');
  326. $content = ob_get_contents();
  327. ob_end_clean();
  328. return $content;
  329. }
  330. function manage_columns($columns){
  331. $columns['cb'] = '<input type="checkbox" />';
  332. $columns['id'] = 'ID';
  333. $columns['name'] = __('Name', 'formidable');
  334. $columns['description'] = __('Description', 'formidable');
  335. $columns['form_id'] = __('Form', 'formidable');
  336. $columns['show_count'] = __('Entry', 'formidable');
  337. $columns['post_id'] = __('Page', 'formidable');
  338. $columns['content'] = __('Content', 'formidable');
  339. $columns['dyncontent'] = __('Dynamic Content', 'formidable');
  340. $columns['created_at'] = __('Date', 'formidable');
  341. $columns['display_key'] = __('Key', 'formidable');
  342. $columns['shortcode'] = __('ShortCode', 'formidable');
  343. add_screen_option( 'per_page', array('label' => __('Custom Displays', 'formidable'), 'default' => 20, 'option' => 'formidable_page_formidable_entry_templates_per_page') );
  344. return $columns;
  345. }
  346. function save_per_page($save, $option, $value){
  347. if($option == 'formidable_page_formidable_entry_templates_per_page')
  348. $save = (int)$value;
  349. return $save;
  350. }
  351. function sortable_columns(){
  352. return array(
  353. 'id' => 'id',
  354. 'name' => 'name',
  355. 'description' => 'description',
  356. 'display_key' => 'display_key',
  357. 'show_count' => 'show_count',
  358. 'content' => 'content',
  359. 'dyncontent' => 'dyncontent',
  360. 'created_at' => 'created_at',
  361. 'shortcode' => 'id'
  362. );
  363. }
  364. function hidden_columns($result){
  365. $return = false;
  366. foreach((array)$result as $r){
  367. if(!empty($r)){
  368. $return = true;
  369. break;
  370. }
  371. }
  372. if($return)
  373. return $result;
  374. $result[] = 'content';
  375. $result[] = 'dyncontent';
  376. $result[] = 'created_at';
  377. return $result;
  378. }
  379. function display_list($params=false, $message='', $page_params_ov = false, $current_page_ov = false, $errors = array()){
  380. global $wpdb, $frmprodb, $frmpro_display, $frm_form, $frm_app_helper, $frm_page_size;
  381. if(!$params)
  382. $params = $this->get_params();
  383. if($message=='')
  384. $message = FrmAppHelper::frm_get_main_message();
  385. $form = $params['form'];
  386. if(class_exists('WP_List_Table')){
  387. require_once( FRMPRO_PATH .'/classes/helpers/FrmProListHelper.php' );
  388. $wp_list_table = new FrmProListHelper(array('singular' => 'display', 'plural' => 'displays', 'table_name' => $frmprodb->displays, 'page_name' => 'entry-templates', 'params' => $params));
  389. $pagenum = $wp_list_table->get_pagenum();
  390. $wp_list_table->prepare_items();
  391. $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
  392. if ( $pagenum > $total_pages && $total_pages > 0 ) {
  393. wp_redirect( add_query_arg( 'paged', $total_pages ) );
  394. exit;
  395. }
  396. }else{
  397. $page_params = '&action=0&frm_action=0';
  398. $where_clause = '';
  399. $form_vars = $this->get_form_sort_vars($params, $where_clause);
  400. $current_page = ($current_page_ov) ? $current_page_ov : $params['paged'];
  401. $page_params = ($page_params_ov) ? $page_params_ov : $form_vars['page_params'];
  402. $sort_str = $form_vars['sort_str'];
  403. $sdir_str = $form_vars['sdir_str'];
  404. $search_str = $form_vars['search_str'];
  405. $form = $form_vars['form'];
  406. $record_count = $frm_app_helper->getRecordCount($form_vars['where_clause'], $frmprodb->displays);
  407. $page_count = $frm_app_helper->getPageCount($frm_page_size, $record_count, $frmprodb->displays);
  408. $displays = $frm_app_helper->getPage($current_page, $frm_page_size, $form_vars['where_clause'], $form_vars['order_by'], $frmprodb->displays);
  409. $page_last_record = $frm_app_helper->getLastRecordNum($record_count,$current_page,$frm_page_size);
  410. $page_first_record = $frm_app_helper->getFirstRecordNum($record_count,$current_page,$frm_page_size);
  411. }
  412. require_once(FRMPRO_VIEWS_PATH.'/displays/list.php');
  413. }
  414. function get_form_sort_vars($params,$where_clause = ''){
  415. $order_by = '';
  416. $page_params = '';
  417. // These will have to work with both get and post
  418. $sort_str = $params['sort'];
  419. $sdir_str = $params['sdir'];
  420. $search_str = $params['search'];
  421. $form = $params['form'];
  422. // Insert search string
  423. if(!empty($search_str)){
  424. $search_params = explode(" ", $search_str);
  425. foreach($search_params as $search_param){
  426. if(!empty($where_clause))
  427. $where_clause .= " AND";
  428. $where_clause .= " (name like '%$search_param%' OR description like '%$search_param%' OR created_at like '%$search_param%' OR content like '%$search_param%' OR dyncontent like '%$search_param%')";
  429. }
  430. $page_params .="&search=$search_str";
  431. }
  432. // make sure page params stay correct
  433. if(!empty($sort_str))
  434. $page_params .="&sort=$sort_str";
  435. if(!empty($sdir_str))
  436. $page_params .= "&sdir=$sdir_str";
  437. if(!empty($form)){
  438. $page_params .= "&form=$form";
  439. if(!empty($where_clause))
  440. $where_clause .= " AND";
  441. $where_clause .= " form_id=". (int)$form;
  442. }
  443. // Add order by clause
  444. switch($sort_str){
  445. case "id":
  446. case "name":
  447. case "description":
  448. case "display_key":
  449. $order_by .= " ORDER BY $sort_str";
  450. break;
  451. default:
  452. $order_by .= " ORDER BY name";
  453. }
  454. // Toggle ascending / descending
  455. if((empty($sort_str) and empty($sdir_str)) or $sdir_str == 'asc'){
  456. $order_by .= ' ASC';
  457. $sdir_str = 'asc';
  458. }else{
  459. $order_by .= ' DESC';
  460. $sdir_str = 'desc';
  461. }
  462. return compact('order_by', 'sort_str', 'sdir_str', 'search_str', 'where_clause', 'page_params', 'form');
  463. }
  464. function get_edit_vars($id, $errors = '', $message=''){
  465. global $frmpro_display, $frmpro_displays_helper, $frmpro_settings, $frm_settings, $frm_siteurl, $frm_ajax_url;
  466. $record = $frmpro_display->getOne( $id );
  467. $values = $frmpro_displays_helper->setup_edit_vars($record);
  468. $submit = __('Update', 'formidable');
  469. require_once(FRMPRO_VIEWS_PATH.'/displays/edit.php');
  470. }
  471. function get_tags(){
  472. $target_id = FrmAppHelper::get_param('target_id', 'content');
  473. FrmProFieldsHelper::get_shortcode_select($_POST['form_id'], $target_id);
  474. die();
  475. }
  476. function get_entry_select(){
  477. echo FrmEntriesHelper::entries_dropdown($_POST['form_id'], 'entry_id');
  478. die();
  479. }
  480. function get_params(){
  481. $values = array();
  482. foreach (array('template' => 0, 'id' => '', 'paged' => 1, 'form' => '', 'search' => '', 'sort' => '', 'sdir' => '') as $var => $default)
  483. $values[$var] = FrmAppHelper::get_param($var, $default);
  484. return $values;
  485. }
  486. /* ShortCodes */
  487. function get_shortcode($atts){
  488. global $frmpro_display;
  489. $defaults = array(
  490. 'id' => '', 'entry_id' => '', 'filter' => false,
  491. 'user_id' => false, 'limit' => '', 'page_size' => '',
  492. 'order_by' => '', 'order' => '', 'get' => '', 'get_value' => ''
  493. );
  494. extract(shortcode_atts($defaults, $atts));
  495. //if (is_numeric($id))
  496. $display = $frmpro_display->getOne($id);
  497. $user_id = FrmProAppHelper::get_user_id_param($user_id);
  498. if(!empty($get))
  499. $_GET[$get] = $get_value;
  500. foreach($defaults as $unset => $val){
  501. unset($atts[$unset]);
  502. unset($unset);
  503. unset($val);
  504. }
  505. foreach($atts as $att => $val){
  506. $_GET[$att] = $val;
  507. unset($att);
  508. unset($val);
  509. }
  510. if ($display)
  511. return FrmProDisplaysController::get_display_data($display, '', $entry_id, compact('filter', 'user_id', 'limit', 'page_size', 'order_by', 'order'));
  512. else
  513. return __('That is not a valid custom display ID', 'formidable');
  514. }
  515. function custom_display($id){
  516. global $frmpro_display;
  517. if ($display = $frmpro_display->getOne($id))
  518. return $this->get_display_data($display);
  519. }
  520. function get_display_data($display, $content='', $entry_id=false, $extra_atts=array()){
  521. global $frmpro_display, $frm_entry, $frmpro_settings, $frm_entry_meta, $frm_forms_loaded;
  522. $frm_forms_loaded[] = true;
  523. $defaults = array(
  524. 'filter' => false, 'user_id' => '', 'limit' => '',
  525. 'page_size' => '', 'order_by' => '', 'order' => ''
  526. );
  527. extract(wp_parse_args( $extra_atts, $defaults ));
  528. if (FrmProAppHelper::rewriting_on() && $frmpro_settings->permalinks )
  529. $this->parse_pretty_entry_url();
  530. if (is_numeric($display->entry_id) && $display->entry_id > 0 and !$entry_id)
  531. $entry_id = $display->entry_id;
  532. $entry = false;
  533. $show = 'all';
  534. if (in_array($display->show_count, array('dynamic', 'calendar', 'one'))){
  535. $one_param = (isset($_GET['entry'])) ? $_GET['entry'] : $entry_id;
  536. $get_param = (isset($_GET[$display->param])) ? $_GET[$display->param] : (($display->show_count == 'one') ? $one_param : $entry_id);
  537. unset($one_param);
  538. if ($get_param){
  539. $where_entry = array('it.form_id' => $display->form_id);
  540. if(($display->type == 'id' or $display->show_count == 'one') and is_numeric($get_param))
  541. $where_entry['it.id'] = $get_param;
  542. else
  543. $where_entry['it.item_key'] = $get_param;
  544. $entry = $frm_entry->getAll($where_entry, '', 1, 0);
  545. if($entry)
  546. $entry = reset($entry);
  547. if($entry and $entry->post_id){
  548. //redirect to single post page if this entry is a post
  549. global $post;
  550. if(in_the_loop() and $display->show_count != 'one' and !is_single($entry->post_id) and $post->ID != $entry->post_id){
  551. $this_post = get_post($entry->post_id);
  552. if(in_array($this_post->post_status, array('publish', 'private')))
  553. die('<script type="text/javascript">window.location="'. get_permalink($entry->post_id) .'"</script>');
  554. }
  555. }
  556. }
  557. unset($get_param);
  558. }
  559. if($entry and in_array($display->show_count, array('dynamic', 'calendar'))){
  560. $new_content = stripslashes($display->dyncontent);
  561. $show = 'one';
  562. }else{
  563. $new_content = stripslashes($display->content);
  564. }
  565. $show = ($display->show_count == 'one' or ($entry_id and is_numeric($entry_id))) ? 'one' : $show;
  566. $shortcodes = FrmProDisplaysHelper::get_shortcodes($new_content, $display->form_id);
  567. $pagination = '';
  568. if ($entry and $entry->form_id == $display->form_id){
  569. $display_content = FrmProFieldsHelper::replace_shortcodes($new_content, $entry, $shortcodes, $display, $show);
  570. }else{
  571. global $frmdb, $wpdb;
  572. $options = maybe_unserialize($display->options);
  573. $empty_msg = '<div class="frm_no_entries">'. (isset($options['empty_msg']) ? stripslashes($options['empty_msg']) : '') .'</div>';
  574. $display_content = '';
  575. if($show == 'all')
  576. $display_content .= isset($options['before_content']) ? stripslashes($options['before_content']) : '';
  577. $display_content = apply_filters('frm_before_display_content', $display_content, $display, $show);
  578. $where = 'it.form_id='.$display->form_id;
  579. $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $display->form_id, 'post_id >' => 1), '', '', 'id,post_id');
  580. $entry_ids = $frmdb->get_col($frmdb->entries, array('form_id' => $display->form_id), 'id');
  581. $after_where = false;
  582. if($user_id and !empty($user_id)){
  583. $user_id = FrmProAppHelper::get_user_id_param($user_id);
  584. $uid_used = false;
  585. }
  586. if(isset($options['where']) and !empty($options['where'])){
  587. $options['where'] = apply_filters('frm_custom_where_opt', $options['where'], array('display' => $display, 'entry' => $entry));
  588. $continue = false;
  589. foreach($options['where'] as $where_key => $where_opt){
  590. $where_val = isset($options['where_val'][$where_key]) ? $options['where_val'][$where_key] : '';
  591. if (preg_match("/\[(get|get-(.?))\b(.*?)(?:(\/))?\]/s", $where_val)){
  592. $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true);
  593. //if this param doesn't exist, then don't include it
  594. if($where_val == '') {
  595. if(!$after_where)
  596. $continue = true;
  597. continue;
  598. }
  599. }else{
  600. $where_val = FrmProFieldsHelper::get_default_value($where_val, false, true, true);
  601. }
  602. $continue = false;
  603. if($where_val == 'current_user'){
  604. if($user_id and is_numeric($user_id)){
  605. $where_val = $user_id;
  606. $uid_used = true;
  607. }else{
  608. global $user_ID;
  609. $where_val = $user_ID;
  610. }
  611. }
  612. $where_val = do_shortcode($where_val);
  613. if(is_array($where_val) and !empty($where_val)){
  614. $new_where = '(';
  615. if(strpos($options['where_is'][$where_key], 'LIKE') !== false){
  616. foreach($where_val as $w){
  617. if($new_where != '(')
  618. $new_where .= ',';
  619. $new_where .= "'%". esc_sql(like_escape($w)). "%'";
  620. unset($w);
  621. }
  622. }else{
  623. foreach($where_val as $w){
  624. if($new_where != '(')
  625. $new_where .= ',';
  626. $new_where .= "'". esc_sql($w) ."'";
  627. unset($w);
  628. }
  629. }
  630. $new_where .= ')';
  631. $where_val = $new_where;
  632. unset($new_where);
  633. if(strpos($options['where_is'][$where_key], '!') === false and strpos($options['where_is'][$where_key], 'not') === false)
  634. $options['where_is'][$where_key] = ' in ';
  635. else
  636. $options['where_is'][$where_key] = ' not in ';
  637. }
  638. if(is_numeric($where_opt)){
  639. $entry_ids = FrmProAppHelper::filter_where($entry_ids, array(
  640. 'where_opt' => $where_opt, 'where_is' => $options['where_is'][$where_key],
  641. 'where_val' => $where_val, 'form_id' => $display->form_id, 'form_posts' => $form_posts,
  642. 'after_where' => $after_where, 'display' => $display
  643. ));
  644. $after_where = true;
  645. $continue = false;
  646. if(empty($entry_ids))
  647. break;
  648. }else if($where_opt == 'created_at'){
  649. if($where_val == 'NOW')
  650. $where_val = current_time('mysql', 1);
  651. $where_val = date('Y-m-d H:i:s', strtotime($where_val));
  652. $where .= " and it.created_at ". $options['where_is'][$where_key];
  653. if(strpos($options['where_is'][$where_key], 'in'))
  654. $where .= " $where_val";
  655. else
  656. $where .= " '". esc_sql($where_val) ."'";
  657. $continue = true;
  658. }else if($where_opt == 'id' or $where_opt == 'item_key'){
  659. $where .= " and it.{$where_opt} ". $options['where_is'][$where_key];
  660. if(strpos($options['where_is'][$where_key], 'in'))
  661. $where .= " $where_val";
  662. else
  663. $where .= " '". esc_sql($where_val) ."'";
  664. $continue = true;
  665. }
  666. }
  667. if(!$continue and empty($entry_ids)){
  668. if ($filter)
  669. $empty_msg = apply_filters('the_content', $empty_msg);
  670. return $content . ' '. $empty_msg;
  671. }
  672. }
  673. if($user_id and is_numeric($user_id) and !$uid_used)
  674. $where .= " AND it.user_id=". (int)$user_id;
  675. $s = FrmAppHelper::get_param('frm_search', false);
  676. if ($s){
  677. $new_ids = FrmProEntriesHelper::get_search_ids($s, $display->form_id);
  678. if($after_where and isset($entry_ids) and !empty($entry_ids))
  679. $entry_ids = array_intersect($new_ids, $entry_ids);
  680. else
  681. $entry_ids = $new_ids;
  682. if(empty($entry_ids))
  683. return $content . ' '. $empty_msg;
  684. }
  685. if(isset($entry_ids) and !empty($entry_ids))
  686. $where .= ' and it.id in ('.implode(',', $entry_ids).')';
  687. if ($entry_id)
  688. $where .= " and it.id in ($entry_id)";
  689. if($show == 'one'){
  690. $limit = ' LIMIT 1';
  691. }else if (isset($_GET['frm_cat']) and isset($_GET['frm_cat_id'])){
  692. //Get fields with specified field value 'frm_cat' = field key/id, 'frm_cat_id' = order position of selected option
  693. global $frm_field;
  694. if ($cat_field = $frm_field->getOne($_GET['frm_cat'])){
  695. $categories = maybe_unserialize($cat_field->options);
  696. if (isset($categories[$_GET['frm_cat_id']]))
  697. $cat_entry_ids = $frm_entry_meta->getEntryIds("meta_value='".$categories[$_GET['frm_cat_id']]."' and fi.field_key='$_GET[frm_cat]'");
  698. if ($cat_entry_ids)
  699. $where .= " and it.id in (".implode(',', $cat_entry_ids).")";
  700. }
  701. }
  702. if (is_array($options)){
  703. if (!empty($limit) and is_numeric($limit))
  704. $options['limit'] = (int)$limit;
  705. if (is_numeric($options['limit'])){
  706. $num_limit = (int)$options['limit'];
  707. $limit = ' LIMIT '. $options['limit'];
  708. }
  709. if (!empty($order_by))
  710. $options['order_by'] = $order_by;
  711. if (!empty($order))
  712. $options['order'] = $order;
  713. if (isset($options['order_by']) && $options['order_by'] != ''){
  714. /*if( $wpdb->has_cap( 'collation' ) ){
  715. $charset_collate = '';
  716. if( !empty($wpdb->charset) )
  717. $charset_collate .= "DEFAULT CHARACTER SET $wpdb->charset";
  718. if( !empty($wpdb->collate) )
  719. $charset_collate .= " COLLATE $wpdb->collate";
  720. }*/
  721. $order = (isset($options['order'])) ? ' '.$options['order'] : '';
  722. if ($options['order_by'] == 'rand'){
  723. $order_by = ' RAND()';
  724. }else if (is_numeric($options['order_by'])){
  725. global $frm_entry_meta, $frm_field;
  726. $order_field = $frm_field->getOne($options['order_by']);
  727. $order_field->field_options = maybe_unserialize($order_field->field_options);
  728. $meta_order = ($order_field->type == 'number') ? ' LENGTH(meta_value),' : '';
  729. if(isset($order_field->field_options['post_field']) and $order_field->field_options['post_field']){
  730. $posts = $form_posts; //$frmdb->get_records($frmdb->entries, array('form_id' => $display->form_id, 'post_id >' => 1), '', '', 'id, post_id');
  731. $linked_posts = array();
  732. foreach($posts as $post_meta)
  733. $linked_posts[$post_meta->post_id] = $post_meta->id;
  734. if($order_field->field_options['post_field'] == 'post_custom'){
  735. $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);
  736. $metas = array();
  737. foreach($ordered_ids as $ordered_id)
  738. $metas[] = array('item_id' => $linked_posts[$ordered_id]);
  739. }else if($order_field->field_options['post_field'] != 'post_category'){
  740. $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);
  741. $metas = array();
  742. foreach($ordered_ids as $ordered_id)
  743. $metas[] = array('item_id' => $linked_posts[$ordered_id]);
  744. }
  745. }else{
  746. if($order_field->type == 'number'){
  747. $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->form_id and fi.id={$options['order_by']}";
  748. if(isset($entry_ids) and !empty($entry_ids))
  749. $query .= " AND it.item_id in (". implode(',', $entry_ids) .")";
  750. $query .= " ORDER BY odr $order $limit";
  751. if ($limit == ' LIMIT 1')
  752. $metas = $wpdb->get_row($query);
  753. else
  754. $metas = $wpdb->get_results($query);
  755. }else{
  756. $metas = $frm_entry_meta->getAll('fi.form_id='.$display->form_id.' and fi.id='.$options['order_by'], ' ORDER BY '.$meta_order.' meta_value'.$order); //TODO: add previous $where and $limit
  757. }
  758. }
  759. if (isset($metas) and is_array($metas) and !empty($metas)){
  760. if($order_field->type == 'time' and (!isset($order_field->field_options['clock']) or
  761. ($order_field->field_options['clock'] == 12))){
  762. $new_order = array();
  763. foreach($metas as $key => $meta){
  764. $parts = str_replace(array(' PM',' AM'), '', $meta->meta_value);
  765. $parts = explode(':', $parts);
  766. if(is_array($parts)){
  767. if((preg_match('/PM/', $meta->meta_value) and ((int)$parts[0] != 12)) or
  768. (((int)$parts[0] == 12) and preg_match('/AM/', $meta->meta_value)))
  769. $parts[0] = ((int)$parts[0] + 12);
  770. }
  771. $new_order[$key] = (int)$parts[0] . $parts[1];
  772. unset($key);
  773. unset($meta);
  774. }
  775. //array with sorted times
  776. asort($new_order);
  777. $final_order = array();
  778. foreach($new_order as $key => $time){
  779. $final_order[] = $metas[$key];
  780. unset($key);
  781. unset($time);
  782. }
  783. $metas = $final_order;
  784. unset($final_order);
  785. }
  786. $rev_order = ($order == 'DESC' or $order == '') ? ' ASC' : ' DESC';
  787. foreach ($metas as $meta){
  788. $meta = (array)$meta;
  789. $order_by .= 'it.id='.$meta['item_id'] . $rev_order.', ';
  790. }
  791. $order_by = rtrim($order_by, ', ');
  792. }else
  793. $order_by .= 'it.created_at'.$order;
  794. }else
  795. $order_by = 'it.'.$options['order_by'].$order;
  796. $order_by = ' ORDER BY '.$order_by;
  797. }
  798. }
  799. if(!empty($page_size) and is_numeric($page_size))
  800. $options['page_size'] = (int)$page_size;
  801. if (isset($options['page_size']) && is_numeric($options['page_size'])){
  802. global $frm_app_helper;
  803. $current_page = FrmAppHelper::get_param('frm-page', 1);
  804. $record_where = ($where == "it.form_id=$display->form_id") ? $display->form_id : $where;
  805. $record_count = $frm_entry->getRecordCount($record_where);
  806. if(isset($num_limit) and ($record_count > (int)$num_limit))
  807. $record_count = (int)$num_limit;
  808. $page_count = $frm_entry->getPageCount($options['page_size'], $record_count);
  809. $entries = $frm_entry->getPage($current_page, $options['page_size'], $where, $order_by);
  810. $page_last_record = $frm_app_helper->getLastRecordNum($record_count, $current_page, $options['page_size']);
  811. $page_first_record = $frm_app_helper->getFirstRecordNum($record_count, $current_page, $options['page_size']);
  812. if($page_count > 1)
  813. $pagination = FrmProDisplaysController::get_pagination_file(FRMPRO_VIEWS_PATH.'/displays/pagination.php', compact('current_page', 'record_count', 'page_count', 'page_last_record', 'page_first_record'));
  814. }else{
  815. $entries = $frm_entry->getAll($where, $order_by, $limit, true, false);
  816. }
  817. $filtered_content = apply_filters('frm_display_entries_content', $new_content, $entries, $shortcodes, $display, $show);
  818. if($filtered_content != $new_content){
  819. $display_content .= $filtered_content;
  820. }else{
  821. $odd = 'odd';
  822. $count = 0;
  823. if(!empty($entries)){
  824. foreach ($entries as $entry){
  825. $count++; //TODO: use the count with conditionals
  826. $display_content .= apply_filters('frm_display_entry_content', $new_content, $entry, $shortcodes, $display, $show, $odd);
  827. $odd = ($odd == 'odd') ? 'even' : 'odd';
  828. unset($entry);
  829. }
  830. unset($count);
  831. }else{
  832. $display_content .= $empty_msg;
  833. }
  834. }
  835. if($show == 'all')
  836. $display_content .= isset($options['after_content']) ? stripslashes($options['after_content']) : '';
  837. }
  838. $display_content .= apply_filters('frm_after_display_content', $pagination, $display, $show);
  839. $display_content = FrmProFieldsHelper::get_default_value($display_content, false, true, true);
  840. if ($display->insert_loc == 'after'){
  841. $content .= $display_content;
  842. }else if ($display->insert_loc == 'before'){
  843. $content = $display_content . $content;
  844. }else{
  845. if ($filter)
  846. $display_content = apply_filters('the_content', $display_content);
  847. $content = $display_content;
  848. }
  849. return $content;
  850. }
  851. function parse_pretty_entry_url(){
  852. global $frm_entry, $wpdb, $post;
  853. $post_url = get_permalink($post->ID);
  854. $request_uri = FrmProAppHelper::current_url();
  855. $match_str = '#^'.$post_url.'(.*?)([\?/].*?)?$#';
  856. if(preg_match($match_str, $request_uri, $match_val)){
  857. // match short slugs (most common)
  858. if(isset($match_val[1]) and !empty($match_val[1]) and $frm_entry->exists($match_val[1])){
  859. // Artificially set the GET variable
  860. $_GET['entry'] = $match_val[1];
  861. }
  862. }
  863. }
  864. function route(){
  865. $action = FrmAppHelper::get_param('frm_action');
  866. if($action =='new')
  867. return $this->new_form();
  868. else if($action == 'create')
  869. return $this->create();
  870. else if($action == 'edit')
  871. return $this->edit();
  872. else if($action == 'update')
  873. return $this->update();
  874. else if($action == 'duplicate')
  875. return $this->duplicate();
  876. else if($action == 'destroy')
  877. return $this->destroy();
  878. else if($action == 'list-form')
  879. return $this->bulk_actions();
  880. else{
  881. $action = FrmAppHelper::get_param('action');
  882. if($action == -1)
  883. $action = FrmAppHelper::get_param('action2');
  884. if(strpos($action, 'bulk_') === 0){
  885. if(isset($_GET) and isset($_GET['action']))
  886. $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action'], '', $_SERVER['REQUEST_URI']);
  887. if(isset($_GET) and isset($_GET['action2']))
  888. $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action2'], '', $_SERVER['REQUEST_URI']);
  889. return $this->bulk_actions($action);
  890. }else{
  891. return $this->display_list();
  892. }
  893. }
  894. }
  895. function get_pagination_file($filename, $atts){
  896. extract($atts);
  897. if (is_file($filename)) {
  898. ob_start();
  899. include $filename;
  900. $contents = ob_get_contents();
  901. ob_end_clean();
  902. return $contents;
  903. }
  904. return false;
  905. }
  906. }
  907. ?>