PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/formidable/pro/classes/helpers/FrmProFieldsHelper.php

https://github.com/rafapires/festival-de-ideias
PHP | 1901 lines | 1572 code | 312 blank | 17 comment | 515 complexity | 30c76fb64a5116bfaae42a69baf9b2cf MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. <?php
  2. class FrmProFieldsHelper{
  3. function FrmProFieldsHelper(){
  4. add_filter('frm_get_default_value', array(&$this, 'get_default_value'), 10, 3);
  5. add_filter('frm_setup_edit_field_vars', array(&$this, 'setup_new_field_vars'), 10);
  6. add_filter('frm_setup_new_fields_vars', array(&$this, 'setup_new_vars'), 10, 2);
  7. add_filter('frm_setup_edit_fields_vars', array(&$this, 'setup_edit_vars'), 10, 3);
  8. add_filter('frm_posted_field_ids', array(&$this, 'posted_field_ids'));
  9. add_action('frm_after_checkbox', array(&$this, 'get_child_checkboxes'));
  10. add_filter('frm_get_paged_fields', array(&$this, 'get_form_fields'), 10, 3);
  11. add_filter('frm_show_custom_html', array(&$this, 'show_custom_html'), 10, 2);
  12. add_filter('frm_other_custom_html', array(&$this, 'get_default_html'), 10, 2);
  13. add_filter('frm_conditional_value', array(&$this, 'conditional_replace_with_value'), 10, 4);
  14. add_filter('frm_before_replace_shortcodes', array(&$this, 'before_replace_shortcodes'), 10, 2);
  15. add_filter('frm_replace_shortcodes', array(&$this, 'replace_html_shortcodes'), 10, 2);
  16. add_filter('frm_display_entry_content', array(&$this, 'replace_shortcodes'), 10, 6);
  17. }
  18. function get_default_value($value, $field, $dynamic_default=true, $return_array=false){
  19. if (is_array(maybe_unserialize($value))) return $value;
  20. if($field and $dynamic_default){
  21. $field->field_options = maybe_unserialize($field->field_options);
  22. if(isset($field->field_options['dyn_default_value']) and !empty($field->field_options['dyn_default_value'])){
  23. $prev_val = $value;
  24. $value = $field->field_options['dyn_default_value'];
  25. }
  26. }
  27. preg_match_all( "/\[(date|time|email|login|display_name|first_name|last_name|user_meta|post_meta|post_id|post_title|post_author_email|ip|auto_id|get|get-(.?))\b(.*?)(?:(\/))?\]/s", $value, $matches, PREG_PATTERN_ORDER);
  28. if (!isset($matches[0])) return do_shortcode($value);
  29. foreach ($matches[0] as $match_key => $val){
  30. switch($val){
  31. case '[date]':
  32. global $frmpro_settings;
  33. $new_value = date_i18n($frmpro_settings->date_format, strtotime(current_time('mysql')));
  34. break;
  35. case '[time]':
  36. $new_value = date('H:i:s', strtotime(current_time('mysql')));
  37. break;
  38. case '[email]':
  39. global $current_user;
  40. $new_value = (isset($current_user->user_email)) ? $current_user->user_email : '';
  41. break;
  42. case '[login]':
  43. global $current_user;
  44. $new_value = (isset($current_user->user_login)) ? $current_user->user_login : '';
  45. break;
  46. case '[display_name]':
  47. global $current_user;
  48. $new_value = (isset($current_user->display_name)) ? $current_user->display_name : '';
  49. break;
  50. case '[first_name]':
  51. global $current_user;
  52. $new_value = (isset($current_user->user_firstname)) ? $current_user->user_firstname : '';
  53. break;
  54. case '[last_name]':
  55. global $current_user;
  56. $new_value = (isset($current_user->user_lastname)) ? $current_user->user_lastname : '';
  57. break;
  58. case '[post_id]':
  59. global $post;
  60. if($post)
  61. $new_value = $post->ID;
  62. break;
  63. case '[post_title]':
  64. global $post;
  65. if($post)
  66. $new_value = $post->post_title;
  67. break;
  68. case '[post_author_email]':
  69. $new_value = get_the_author_meta('user_email');
  70. break;
  71. case '[user_id]':
  72. global $user_ID;
  73. $new_value = $user_ID ? $user_ID : '';
  74. break;
  75. case '[ip]':
  76. $new_value = $_SERVER['REMOTE_ADDR'];
  77. break;
  78. default:
  79. $atts = shortcode_parse_atts(stripslashes($matches[3][$match_key]));
  80. $shortcode = $matches[1][$match_key];
  81. if (preg_match("/\[get-(.?)\b(.*?)?\]/s", $val)){
  82. $param = str_replace('[get-', '', $val);
  83. if (preg_match("/\[/s", $param))
  84. $val .= ']';
  85. else
  86. $param = trim($param, ']'); //only if is doesn't create an imbalanced []
  87. $new_value = FrmAppHelper::get_param($param);
  88. if(is_array($new_value) and !$return_array)
  89. $new_value = implode(', ', $new_value);
  90. }else{
  91. switch($shortcode){
  92. case 'get':
  93. $new_value = '';
  94. if(isset($atts['param'])){
  95. if(strpos($atts['param'], '&#91;')){
  96. $atts['param'] = str_replace('&#91;', '[', $atts['param']);
  97. $atts['param'] = str_replace('&#93;', ']', $atts['param']);
  98. }
  99. $new_value = FrmAppHelper::get_param($atts['param'], false);
  100. if(!$new_value){
  101. global $wp_query;
  102. if(isset($wp_query->query_vars[$atts['param']]))
  103. $new_value = $wp_query->query_vars[$atts['param']];
  104. }
  105. if(!$new_value and isset($atts['default']))
  106. $new_value = $atts['default'];
  107. else if(!$new_value and isset($prev_val))
  108. $new_value = $prev_val;
  109. }
  110. if(is_array($new_value) and !$return_array)
  111. $new_value = implode(', ', $new_value);
  112. break;
  113. case'auto_id':
  114. global $frmpro_entry_meta;
  115. $last_entry = $frmpro_entry_meta->get_max($field);
  116. if(!$last_entry and isset($atts['start']))
  117. $new_value = (int)$atts['start'];
  118. if (!isset($new_value)) $new_value = $last_entry + 1;
  119. break;
  120. case 'user_meta':
  121. if(isset($atts['key'])){
  122. global $current_user;
  123. $new_value = (isset($current_user->{$atts['key']})) ? $current_user->{$atts['key']} : '';
  124. }
  125. break;
  126. case 'post_meta':
  127. if(isset($atts['key'])){
  128. global $post;
  129. if($post){
  130. $post_meta = get_post_meta($post->ID, $atts['key'], true);
  131. if($post_meta)
  132. $new_value = $post_meta;
  133. }
  134. }
  135. break;
  136. default:
  137. //check for posted item_meta
  138. if(is_numeric($shortcode)){
  139. $new_value = FrmAppHelper::get_param('item_meta['. $shortcode .']', false);
  140. if(!$new_value and isset($atts['default']))
  141. $new_value = $atts['default'];
  142. if(is_array($new_value) and !$return_array)
  143. $new_value = implode(', ', $new_value);
  144. }else{
  145. //don't replace this if it's a shortcode that still needs to be processed
  146. $new_value = $val;
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. if (!isset($new_value)) $new_value = '';
  153. if(is_array($new_value)){
  154. if(count($new_value) === 1)
  155. $new_value = reset($new_value);
  156. $value = $new_value;
  157. }else{
  158. $value = str_replace($val, $new_value, $value);
  159. }
  160. unset($new_value);
  161. }
  162. unset($matches);
  163. preg_match_all( "/\[(\d*)\b(.*?)(?:(\/))?\]/s", $value, $matches, PREG_PATTERN_ORDER);
  164. if (isset($matches[0])){
  165. foreach ($matches[0] as $match_key => $val){
  166. $shortcode = $matches[1][$match_key];
  167. if(is_numeric($shortcode)){
  168. $new_value = FrmAppHelper::get_param('item_meta['. $shortcode .']', false);
  169. if(!$new_value and isset($atts['default']))
  170. $new_value = $atts['default'];
  171. if(is_array($new_value) and !$return_array)
  172. $new_value = implode(', ', $new_value);
  173. if(is_array($new_value))
  174. $value = $new_value;
  175. else
  176. $value = str_replace($val, $new_value, $value);
  177. }
  178. }
  179. }
  180. global $frm_skip_shortcode;
  181. $frm_skip_shortcode = true;
  182. $value = do_shortcode($value);
  183. $frm_skip_shortcode = false;
  184. return $value;
  185. }
  186. function setup_new_field_vars($values){
  187. $values['field_options'] = maybe_unserialize($values['field_options']);
  188. foreach (FrmProFieldsHelper::get_default_field_opts($values) as $opt => $default)
  189. $values[$opt] = (isset($values['field_options'][$opt])) ? $values['field_options'][$opt] : $default;
  190. return $values;
  191. }
  192. function setup_new_vars($values, $field){
  193. $values['use_key'] = false;
  194. $field->field_options = maybe_unserialize($field->field_options);
  195. foreach ($this->get_default_field_opts($values, $field) as $opt => $default)
  196. $values[$opt] = (isset($field->field_options[$opt]) && $field->field_options[$opt] != '') ? $field->field_options[$opt] : $default;
  197. $values['hide_field'] = (array)$values['hide_field'];
  198. $values['hide_field_cond'] = (array)$values['hide_field_cond'];
  199. $values['hide_opt'] = (array)$values['hide_opt'];
  200. if ($values['type'] == 'data' && in_array($values['data_type'], array('select', 'radio', 'checkbox')) && is_numeric($values['form_select'])){
  201. global $frm_entry_meta;
  202. $check = $this->check_data_values($values);
  203. if($check)
  204. $values['options'] = $this->get_linked_options($values, $field);
  205. else if(is_numeric($values['value']))
  206. $values['options'] = array($values['value'] => $frm_entry_meta->get_entry_meta_by_field($values['value'], $values['form_select']));
  207. unset($check);
  208. }else if ($values['type'] == '10radio' or $values['type'] == 'scale'){
  209. $values['minnum'] = 1;
  210. $values['maxnum'] = 10;
  211. }else if ($values['type'] == 'date'){
  212. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $values['value'])){
  213. global $frmpro_settings;
  214. $values['value'] = FrmProAppHelper::convert_date($values['value'], 'Y-m-d', $frmpro_settings->date_format);
  215. }
  216. }else if($values['type'] == 'user_id' and is_admin() and current_user_can('administrator') and ($_GET['page'] != 'formidable')){
  217. global $user_ID;
  218. $values['type'] = 'select';
  219. $values['options'] = $this->get_user_options();
  220. $values['use_key'] = true;
  221. $values['custom_html'] = FrmFieldsHelper::get_default_html('select');
  222. $values['value'] = $user_ID;
  223. }else if(!empty($values['options'])){
  224. foreach($values['options'] as $val_key => $val_opt){
  225. if(is_array($val_opt)){
  226. foreach($val_opt as $opt_key => $opt){
  227. $values['options'][$val_key][$opt_key] = $this->get_default_value($opt, $field, false);
  228. unset($opt_key);
  229. unset($opt);
  230. }
  231. }else{
  232. $values['options'][$val_key] = $this->get_default_value($val_opt, $field, false);
  233. }
  234. unset($val_key);
  235. unset($val_opt);
  236. }
  237. }
  238. if($values['post_field'] == 'post_category'){
  239. $values['use_key'] = true;
  240. $values['options'] = $this->get_category_options($values);
  241. }else if($values['post_field'] == 'post_status'){
  242. $values['use_key'] = true;
  243. $values['options'] = $this->get_status_options($field);
  244. }
  245. if(is_array($values['value'])){
  246. foreach($values['value'] as $val_key => $val)
  247. $values['value'][$val_key] = apply_filters('frm_get_default_value', $val, $field);
  248. }else if(!empty($values['value'])){
  249. $values['value'] = apply_filters('frm_get_default_value', $values['value'], $field);
  250. }
  251. FrmProFieldsHelper::setup_conditional_fields($values);
  252. return $values;
  253. }
  254. function setup_edit_vars($values, $field, $entry_id=false){
  255. $values['use_key'] = false;
  256. $field->field_options = maybe_unserialize($field->field_options);
  257. foreach ($this->get_default_field_opts($values, $field) as $opt => $default){
  258. $values[$opt] = stripslashes_deep(($_POST and isset($_POST['field_options'][$opt.'_'.$field->id]) ) ? $_POST['field_options'][$opt.'_'.$field->id] : (isset($field->field_options[$opt]) ? $field->field_options[$opt]: $default));
  259. }
  260. $values['hide_field'] = (array)$values['hide_field'];
  261. $values['hide_field_cond'] = (array)$values['hide_field_cond'];
  262. $values['hide_opt'] = (array)$values['hide_opt'];
  263. if ($values['type'] == 'data' && in_array($values['data_type'], array('select', 'radio', 'checkbox')) && is_numeric($values['form_select'])){
  264. global $frm_entry_meta;
  265. $check = $this->check_data_values($values);
  266. if($check)
  267. $values['options'] = $this->get_linked_options($values, $field, $entry_id);
  268. else if(is_numeric($values['value']))
  269. $values['options'] = array($values['value'] => $frm_entry_meta->get_entry_meta_by_field($values['value'], $values['form_select']));
  270. unset($check);
  271. }else if ($values['type'] == 'date'){
  272. global $frmpro_settings;
  273. if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $values['value']))
  274. $values['value'] = FrmProAppHelper::convert_date($values['value'], 'Y-m-d', $frmpro_settings->date_format);
  275. else if (preg_match('/^\d{4}-\d{2}-\d{2}/', $values['value']))
  276. $values['value'] = FrmProAppHelper::convert_date($values['value'], 'Y-m-d H:i:s', $frmpro_settings->date_format);
  277. }else if ($values['type'] == 'file'){
  278. //if (isset($_POST)) ???
  279. if($values['post_field'] != 'post_custom'){
  280. global $frm_entry_meta;
  281. $values['value'] = $frm_entry_meta->get_entry_meta_by_field($entry_id, $values['id']);
  282. }
  283. }else if($values['type'] == 'hidden' and is_admin() and current_user_can('administrator') and ($_GET['page'] != 'formidable')){
  284. $values['type'] = 'text';
  285. $values['custom_html'] = FrmFieldsHelper::get_default_html('text');
  286. }else if($values['type'] == 'user_id' and is_admin() and current_user_can('administrator') and ($_GET['page'] != 'formidable')){
  287. $values['type'] = 'select';
  288. $values['options'] = $this->get_user_options();
  289. $values['use_key'] = true;
  290. $values['custom_html'] = FrmFieldsHelper::get_default_html('select');
  291. }else if($values['type'] == 'tag'){
  292. if(empty($values['value'])){
  293. global $wpdb, $frmdb;
  294. $post_id = $wpdb->get_var("SELECT post_id FROM $frmdb->entries WHERE id=$entry_id");
  295. if($post_id and ($tags = get_the_terms( $post_id, $values['taxonomy'] ))){
  296. $names = array();
  297. foreach($tags as $tag)
  298. $names[] = $tag->name;
  299. $values['value'] = implode(', ', $names);
  300. }
  301. }
  302. }
  303. if($values['post_field'] == 'post_category'){
  304. $values['use_key'] = true;
  305. $values['options'] = $this->get_category_options($values);
  306. }else if($values['post_field'] == 'post_status'){
  307. $values['use_key'] = true;
  308. $values['options'] = $this->get_status_options($field);
  309. }
  310. FrmProFieldsHelper::setup_conditional_fields($values);
  311. return $values;
  312. }
  313. function get_default_field_opts($values=false, $field=false){
  314. global $frmpro_settings;
  315. $minnum = 1;
  316. $maxnum = 10;
  317. $step = 1;
  318. $align = 'block';
  319. if($values){
  320. if($values['type'] == 'number'){
  321. $minnum = 0;
  322. $maxnum = 9999;
  323. }else if($values['type'] == '10radio' or $values['type'] == 'scale' and $field){
  324. $range = maybe_unserialize($field->options);
  325. $minnum = $range[0];
  326. $maxnum = end($range);
  327. }else if ($values['type'] == 'time'){
  328. $step = 30;
  329. }else if($values['type'] == 'radio'){
  330. $align = $frmpro_settings->radio_align;
  331. }else if($values['type'] == 'checkbox'){
  332. $align = $frmpro_settings->check_align;
  333. }
  334. }
  335. $end_minute = 60 - (int)$step;
  336. unset($values);
  337. unset($field);
  338. return array(
  339. 'slide' => 0, 'form_select' => '', 'show_hide' => 'show', 'any_all' => 'any', 'align' => $align,
  340. 'hide_field' => array(), 'hide_field_cond' => array('=='), 'hide_opt' => array(), 'star' => 0,
  341. 'post_field' => '', 'custom_field' => '', 'taxonomy' => 'category', 'exclude_cat' => 0, 'ftypes' => array(),
  342. 'data_type' => '', 'restrict' => 0, 'start_year' => 2000, 'end_year' => 2020, 'read_only' => 0,
  343. 'admin_only' => 0, 'locale' => '', 'attach' => false, 'minnum' => $minnum, 'maxnum' => $maxnum,
  344. 'step' => $step, 'clock' => 12, 'start_time' => '00:00', 'end_time' => '23:'.$end_minute,
  345. 'dependent_fields' => 0, 'unique' => 0, 'use_calc' => 0, 'calc' => '', 'duplication' => 1, 'rte' => 'nicedit',
  346. 'dyn_default_value' => '', 'multiple' => 0
  347. );
  348. }
  349. function check_data_values($values){
  350. $check = true;
  351. if(!empty($values['hide_field']) and (!empty($values['hide_opt']) or !empty($values['form_select']))){
  352. foreach($values['hide_field'] as $hkey => $f){
  353. if(!$check or !empty($values['hide_opt'][$hkey])) continue;
  354. $f = FrmField::getOne($f);
  355. if($f and $f->type == 'data')
  356. $check = false;
  357. unset($f);
  358. unset($hkey);
  359. }
  360. }
  361. return $check;
  362. }
  363. function setup_conditional_fields($field){
  364. if(!empty($field['hide_field']) and (!empty($field['hide_opt']) or !empty($field['form_select']))){
  365. global $frm_rules, $frm_field;
  366. if(!$frm_rules)
  367. $frm_rules = array();
  368. $conditions = array();
  369. if(!isset($field['show_hide']))
  370. $field['show_hide'] = 'show';
  371. if(!isset($field['any_all']))
  372. $field['any_all'] = 'any';
  373. foreach($field['hide_field'] as $i => $cond){
  374. if(!is_numeric($cond))
  375. continue;
  376. $parent_field = $frm_field->getOne($cond);
  377. if(!$parent_field)
  378. continue;
  379. $parent_opts = maybe_unserialize($parent_field->field_options);
  380. if(empty($conditions)){
  381. foreach($field['hide_field'] as $i2 => $cond2){
  382. if(!is_numeric($cond2))
  383. continue;
  384. if((int)$cond2 == (int)$parent_field->id){
  385. $sub_field = $parent_field;
  386. $sub_opts = $parent_opts;
  387. }else{
  388. $sub_field = $frm_field->getOne($cond2);
  389. if($sub_field)
  390. $sub_opts = maybe_unserialize($sub_field->field_options);
  391. }
  392. $condition = array('FieldName' => $sub_field->id, 'Condition' => $field['hide_field_cond'][$i2]);
  393. if($sub_field->type == 'data' and $field['type'] == 'data' and (is_numeric($field['form_select']) or $field['form_select'] == 'taxonomy')){
  394. $condition['LinkedField'] = $field['form_select'];
  395. $condition['DataType'] = $field['data_type'];
  396. }else if(isset($field['hide_opt']) and (!empty($field['hide_opt'][$i2]) or $field['hide_opt'][$i2] == 0)){
  397. $condition['Value'] = stripslashes(str_replace('"', '&quot;', ( apply_filters('frm_get_default_value', $field['hide_opt'][$i2], $frm_field->getOne($field['id']) ))));
  398. }
  399. $condition['Type'] = $sub_field->type . (($sub_field->type == 'data') ? '-'. $sub_opts['data_type'] : '');
  400. $conditions[] = $condition;
  401. }
  402. }
  403. $rule = array('Show' => $field['show_hide'], 'MatchType' => $field['any_all']);
  404. $rule['Setting'] = array(
  405. 'FieldName' => $field['id']
  406. );
  407. $rule['Conditions'] = $conditions;
  408. if(!isset($frm_rules[$parent_field->id]))
  409. $frm_rules[$parent_field->id] = array();
  410. $frm_rules[$parent_field->id][] = $rule;
  411. unset($rule);
  412. unset($parent_field);
  413. unset($i);
  414. unset($cond);
  415. }
  416. }
  417. }
  418. function get_category_options($field){
  419. $field = (array)$field;
  420. $post_type = FrmProForm::post_type($field['form_id']);
  421. if(!isset($field['exclude_cat']))
  422. $field['exclude_cat'] = 0;
  423. $exclude = (is_array($field['exclude_cat'])) ? implode(',', $field['exclude_cat']) : $field['exclude_cat'];
  424. $exclude = apply_filters('frm_exclude_cats', $exclude, $field);
  425. $args = array(
  426. 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false,
  427. 'exclude' => $exclude, 'type' => $post_type
  428. );
  429. if($field['type'] != 'data')
  430. $args['parent'] = '0';
  431. if(function_exists('get_object_taxonomies')){
  432. $args['taxonomy'] = FrmProAppHelper::get_custom_taxonomy($post_type, $field);
  433. if(!$args['taxonomy'])
  434. return;
  435. }
  436. $categories = get_categories($args);
  437. $options = array();
  438. foreach($categories as $cat)
  439. $options[$cat->term_id] = $cat->name;
  440. return $options;
  441. }
  442. function get_child_checkboxes($args){
  443. $defaults = array(
  444. 'field' => 0, 'field_name' => false, 'opt_key' => 0, 'opt' => '',
  445. 'type' => 'checkbox', 'value' => false, 'exclude' => 0, 'hide_id' => false
  446. );
  447. extract(wp_parse_args($args, $defaults));
  448. if(!$field or !isset($field['post_field']) or $field['post_field'] != 'post_category') return;
  449. if(!$value) $value = (isset($field['value'])) ? $field['value'] : '';
  450. if(!$exclude) $exclude = (is_array($field['exclude_cat'])) ? implode(',', $field['exclude_cat']) : $field['exclude_cat'];
  451. if(!$field_name) $field_name = "item_meta[$field[id]]";
  452. if($type == 'checkbox'){
  453. $field_name .= '[]';
  454. $onchange = ' onchange="frmCheckParents(this.id)"';
  455. }else{
  456. $onchange = '';
  457. }
  458. $post_type = FrmProForm::post_type($field['form_id']);
  459. $taxonomy = 'category';
  460. $args = array(
  461. 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false,
  462. 'parent' => $opt_key, 'exclude' => $exclude, 'type' => $post_type
  463. );
  464. if(!$opt_key and function_exists('get_object_taxonomies')){
  465. $args['taxonomy'] = FrmProAppHelper::get_custom_taxonomy($post_type, $field);
  466. if(!$args['taxonomy']){
  467. echo '<p>'. __('No Categories', 'formidable' ) .'</p>';
  468. return;
  469. }
  470. $taxonomy = $args['taxonomy'];
  471. }
  472. $children = get_categories($args);
  473. $level = ($opt_key) ? 2 : 1;
  474. foreach($children as $key => $cat){ ?>
  475. <div class="frm_catlevel_<?php echo $level ?>"><?php FrmProFieldsHelper::_show_category(compact('cat', 'field', 'field_name', 'exclude', 'type', 'value', 'exclude', 'level', 'onchange', 'post_type', 'taxonomy', 'hide_id')) ?></div>
  476. <?php }
  477. }
  478. function _show_category($atts) {
  479. extract($atts);
  480. if(!is_object($cat)) return;
  481. $checked = '';
  482. if(is_array($value))
  483. $checked = (in_array($cat->cat_ID, $value)) ? 'checked="checked" ' : '';
  484. else if($cat->cat_ID == $value)
  485. $checked = 'checked="checked" ';
  486. else
  487. $checked = '';
  488. $class = '';
  489. //$class = ' class="frm_selectit"'; //TODO: option to check parent cats
  490. $sanitized_name = ((isset($field['id'])) ? $field['id'] : $field['field_options']['taxonomy']).'-'. $cat->cat_ID;
  491. ?>
  492. <div class="frm_<?php echo $type ?>" id="frm_<?php echo $type .'_'. $sanitized_name ?>">
  493. <label<?php echo $class ?> for="field_<?php echo $sanitized_name ?>"><input type="<?php echo $type ?>" name="<?php echo $field_name ?>" <?php echo (isset($hide_id) and $hide_id)? '' : 'id="field_'. $sanitized_name .'"'; ?> value="<?php echo $cat->cat_ID ?>" <?php echo $checked; do_action('frm_field_input_html', $field); //echo ($onchange); ?> /><?php echo $cat->cat_name ?></label>
  494. <?php
  495. $children = get_categories(array('type' => $post_type, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, 'exclude' => $exclude, 'parent' => $cat->cat_ID, 'taxonomy' => $taxonomy));
  496. if($children){
  497. $level++;
  498. foreach($children as $key => $cat){ ?>
  499. <div class="frm_catlevel_<?php echo $level ?>"><?php FrmProFieldsHelper::_show_category(compact('cat', 'field', 'field_name', 'exclude', 'type', 'value', 'exclude', 'level', 'onchange', 'post_type', 'taxonomy', 'hide_id')) ?></div>
  500. <?php }
  501. }
  502. echo '</div>';
  503. }
  504. function get_status_options($field){
  505. $post_type = FrmProForm::post_type($field->form_id);
  506. $post_type_object = get_post_type_object($post_type);
  507. $options = array();
  508. if(!$post_type_object)
  509. return $options;
  510. $can_publish = current_user_can($post_type_object->cap->publish_posts);
  511. $options = get_post_statuses(); //'draft', pending, publish, private
  512. if(!$can_publish){ // Contributors only get "Unpublished" and "Pending Review"
  513. unset($options['publish']);
  514. if(isset($options['future']))
  515. unset($options['future']);
  516. }
  517. return $options;
  518. }
  519. function get_user_options(){
  520. global $wpdb;
  521. $users = (function_exists('get_users')) ? get_users(array( 'fields' => array('ID','user_login','display_name'), 'blog_id' => $GLOBALS['blog_id'])) : get_users_of_blog();
  522. $options = array('' => '');
  523. foreach($users as $user)
  524. $options[$user->ID] = (!empty($user->display_name)) ? $user->display_name : $user->user_login;
  525. return $options;
  526. }
  527. function get_linked_options($values, $field, $entry_id=false){
  528. global $frm_entry_meta, $user_ID, $frm_field, $frmdb;
  529. $metas = array();
  530. $selected_field = $frm_field->getOne($values['form_select']);
  531. if(!$selected_field)
  532. return array();
  533. $selected_field->field_options = maybe_unserialize($selected_field->field_options);
  534. $linked_posts = (isset($selected_field->field_options['post_field']) and
  535. $selected_field->field_options['post_field'] and
  536. $selected_field->field_options['post_field'] != '') ? true : false;
  537. $post_ids = array();
  538. if (is_numeric($values['hide_field']) and (empty($values['hide_opt']))){
  539. global $frmpro_entry_meta;
  540. if (isset($_POST) and isset($_POST['item_meta']))
  541. $observed_field_val = (isset($_POST['item_meta'][$values['hide_field']])) ? $_POST['item_meta'][$values['hide_field']] : '';
  542. else if($entry_id)
  543. $observed_field_val = $frm_entry_meta->get_entry_meta_by_field($entry_id, $values['hide_field']);
  544. else
  545. $observed_field_val = '';
  546. $observed_field_val = maybe_unserialize($observed_field_val);
  547. $metas = $frmpro_entry_meta->meta_through_join($values['hide_field'], $selected_field, $observed_field_val);
  548. }else if ($values['restrict'] and $user_ID){
  549. $entry_user = $user_ID;
  550. if($entry_id and is_admin()){
  551. $entry_user = $frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'user_id');
  552. if(!$entry_user or empty($entry_user))
  553. $entry_user = $user_ID;
  554. }
  555. if (isset($selected_field->form_id)){
  556. $linked_where = array('form_id' => $selected_field->form_id, 'user_id' => $entry_user);
  557. if($linked_posts){
  558. $post_ids = $frmdb->get_records($frmdb->entries, $linked_where, '', '', 'id, post_id');
  559. }else{
  560. $entry_ids = $frmdb->get_col($frmdb->entries, $linked_where, 'id');
  561. }
  562. unset($linked_where);
  563. }
  564. if (isset($entry_ids) and !empty($entry_ids))
  565. $metas = $frm_entry_meta->getAll("it.item_id in (".implode(',', $entry_ids).") and field_id=". (int)$values['form_select'], ' ORDER BY meta_value');
  566. }else{
  567. $limit = '';
  568. if(is_admin() and isset($_GET) and $_GET['page'] == 'formidable')
  569. $limit = 500;
  570. $metas = $frmdb->get_records($frmdb->entry_metas, array('field_id' => $values['form_select']), 'meta_value', $limit, 'item_id, meta_value');
  571. $post_ids = $frmdb->get_records($frmdb->entries, array('form_id' => $selected_field->form_id), '', $limit, 'id, post_id');
  572. }
  573. if($linked_posts and !empty($post_ids)){
  574. foreach($post_ids as $entry){
  575. $meta_value = FrmProEntryMetaHelper::get_post_value($entry->post_id, $selected_field->field_options['post_field'], $selected_field->field_options['custom_field'], array('type' => $selected_field->type, 'form_id' => $selected_field->form_id, 'field' => $selected_field));
  576. $metas[] = array('meta_value' => $meta_value, 'item_id' => $entry->id);
  577. }
  578. }
  579. $options = array();
  580. foreach ($metas as $meta){
  581. $meta = (array)$meta;
  582. if(empty($meta['meta_value'])) continue;
  583. if($selected_field->type == 'image')
  584. $options[$meta['item_id']] = $meta['meta_value'];
  585. else
  586. $options[$meta['item_id']] = FrmProEntryMetaHelper::display_value($meta['meta_value'], $selected_field, array('type' => $selected_field->type, 'show_icon' => false, 'show_filename' => false));
  587. unset($meta);
  588. }
  589. unset($metas);
  590. natcasesort($options); //TODO: add sorting options
  591. return $options;
  592. }
  593. function posted_field_ids($where){
  594. if (isset($_POST['form_id']) and isset($_POST['frm_page_order_'. $_POST['form_id']]))
  595. $where .= ' and fi.field_order < '. (int)$_POST['frm_page_order_'. $_POST['form_id']];
  596. return $where;
  597. }
  598. function get_form_fields($fields, $form_id, $error=false){
  599. global $frm_prev_page, $frm_field, $frm_next_page;
  600. $prev_page = FrmAppHelper::get_param('frm_page_order_'. $form_id, false);
  601. $prev_page = (int)$prev_page;
  602. //$current_form_id = FrmAppHelper::get_param('form_id', false);
  603. $where = "fi.type='break' AND fi.form_id=". (int)$form_id;
  604. //if (is_numeric($current_form_id) and $current_form_id != $form_id)
  605. // return $fields;
  606. if ($error and !$prev_page)
  607. $prev_page = 999;
  608. if ($prev_page){
  609. if ($error){
  610. $where_error = $where . " AND fi.field_order <" . ($prev_page);
  611. $prev_page_obj = $frm_field->getAll($where_error, 'field_order DESC', 1);
  612. $prev_page = ($prev_page_obj) ? $prev_page_obj->field_order : false;
  613. }
  614. if ($prev_page and !isset($prev_page_obj)){
  615. $prev_where = $where . " AND fi.field_order=". $prev_page;
  616. $prev_page_obj = $frm_field->getAll($prev_where, 'field_order DESC', 1);
  617. }
  618. $frm_prev_page[$form_id] = $prev_page;
  619. $where .= ' AND fi.field_order >='. ($prev_page + 1);
  620. }else
  621. unset($frm_prev_page[$form_id]);
  622. $next_page = $frm_field->getAll($where, 'field_order', 1);
  623. unset($where);
  624. if ($next_page or $prev_page){
  625. $query = "(fi.type != 'break'";
  626. if ($next_page) $query .= " or fi.id = $next_page->id";
  627. if ($prev_page) $query .= " or fi.id = $prev_page_obj->id";
  628. $query .= ") and fi.form_id=$form_id";
  629. if ($prev_page) $query .= " and fi.field_order >= $prev_page";
  630. if ($next_page) $query .= " and fi.field_order <= $next_page->field_order";
  631. if (is_admin()) $query .= " and fi.type != 'captcha'";
  632. $fields = $frm_field->getAll($query, ' ORDER BY field_order');
  633. }
  634. if($next_page)
  635. $frm_next_page[$form_id] = $next_page->name;
  636. else
  637. unset($frm_next_page[$form_id]);
  638. return $fields;
  639. }
  640. function show_custom_html($show, $field_type){
  641. if (in_array($field_type, array('hidden', 'user_id')))
  642. $show = false;
  643. return $show;
  644. }
  645. function get_default_html($default_html, $type){
  646. if ($type == 'break'){
  647. $default_html = <<<DEFAULT_HTML
  648. <h2 class="frm_pos_[label_position]">[field_name]</h2>
  649. [if description]<div class="frm_description">[description]</div>[/if description]
  650. DEFAULT_HTML;
  651. }else if ($type == 'divider'){
  652. $default_html = <<<DEFAULT_HTML
  653. <div id="frm_field_[id]_container" class="frm_form_field form-field[error_class]">
  654. <h2 class="frm_pos_[label_position][collapse_class]">[field_name]</h2>
  655. [collapse_this]
  656. [if description]<div class="frm_description">[description]</div>[/if description]
  657. </div>
  658. DEFAULT_HTML;
  659. }else if($type == 'html'){
  660. $default_html = '<div id="frm_field_[id]_container" class="frm_form_field form-field">[description]</div>';
  661. }
  662. return $default_html;
  663. }
  664. function before_replace_shortcodes($html, $field){
  665. global $frmpro_settings;
  666. if(isset($field['align']) and ($field['type'] == 'radio' or $field['type'] == 'checkbox')){
  667. $required_class = '[required_class]';
  668. if(($field['type'] == 'radio' and $field['align'] != $frmpro_settings->radio_align) or
  669. ($field['type'] == 'checkbox' and $field['align'] != $frmpro_settings->check_align)){
  670. $required_class .= ($field['align'] == 'inline') ? ' horizontal_radio' : ' vertical_radio';
  671. $html = str_replace('[required_class]', $required_class, $html);
  672. }
  673. }
  674. if(isset($field['classes']) and strpos($field['classes'], 'frm_grid') !== false){
  675. $opt_count = count($field['options']) + 1;
  676. $html = str_replace('[required_class]', '[required_class] frm_grid_'. $opt_count, $html);
  677. unset($opt_count);
  678. }
  679. if($field['type'] == 'html' and isset($field['classes']))
  680. $html = str_replace('frm_form_field', 'frm_form_field '. $field['classes'], $html);
  681. return $html;
  682. }
  683. function replace_html_shortcodes($html, $field){
  684. if ($field['type'] == 'divider'){
  685. global $frm_div;
  686. $trigger = '';
  687. $html = str_replace(array('frm_none_container', 'frm_hidden_container', 'frm_top_container', 'frm_left_container', 'frm_right_container'), '', $html);
  688. $collapse_div = '<div>'."\n";
  689. if (isset($field['slide']) and $field['slide']){
  690. $trigger = ' frm_trigger" onclick="frmToggleSection(jQuery(this));';
  691. $collapse_div = '<div class="frm_toggle_container" style="display:none;">';
  692. if(preg_match('/\<\/div\>$/', $html)){
  693. if($frm_div and $frm_div != $field['id']){
  694. $html = "</div>\n". $html;
  695. $frm_div = false;
  696. }
  697. $frm_div = $field['id'];
  698. $html = preg_replace('/\<\/div\>$/', '', $html); //"</div>\n";
  699. }
  700. }else if($frm_div and $frm_div != $field['id']){
  701. $html = "</div>\n". $html;
  702. $html = preg_replace('/\<\/div\>$/', '', $html); //"</div>\n";
  703. }
  704. if (preg_match('/\[(collapse_this)\]/s', $html)){
  705. $html = "</div>\n". $html;
  706. $html = str_replace('[collapse_this]', $collapse_div, $html);
  707. }
  708. $html = str_replace('[collapse_class]', $trigger, $html);
  709. }else if($field['type'] == 'html'){
  710. if(apply_filters('frm_use_wpautop', true))
  711. $html = wpautop($html);
  712. $html = apply_filters('frm_get_default_value', $html, (object)$field, false);
  713. $html = do_shortcode($html);
  714. }
  715. if (preg_match('/\[(collapse_this)\]/s', $html))
  716. $html = str_replace('[collapse_this]', '', $html);
  717. return $html;
  718. }
  719. function get_file_icon($media_id){
  720. if ( !is_numeric($media_id) )
  721. return;
  722. $image = wp_get_attachment_image($media_id, 'thumbnail', true);
  723. if($image and !preg_match("/wp-content\/uploads/", $image)){ //if this is a mime type icon
  724. $attachment = get_post($media_id);
  725. $label = basename($attachment->guid);
  726. $image .= " <span id='frm_media_$media_id' class='frm_upload_label'>$label</span>";
  727. }
  728. return $image;
  729. }
  730. function get_file_name($media_ids, $short=true){
  731. $value = '';
  732. foreach((array)$media_ids as $media_id){
  733. if ( is_numeric($media_id) ){
  734. if ($short){
  735. $attachment = get_post($media_id);
  736. $label = basename($attachment->guid);
  737. }
  738. $url = wp_get_attachment_url($media_id);
  739. if (is_admin()){
  740. global $frm_siteurl;
  741. $url = '<a href="'.$url.'">'.$label.'</a>';
  742. if(isset($_GET) and isset($_GET['page']) and preg_match('/formidable*/', $_GET['page']))
  743. $url .= '<br/><a href="'.$frm_siteurl.'/wp-admin/media.php?action=edit&attachment_id='.$media_id.'">'. __('Edit Uploaded File', 'formidable') .'</a>';
  744. }
  745. $value .= $url;
  746. }
  747. }
  748. return $value;
  749. }
  750. function get_data_value($value, $field, $atts=array()){
  751. global $frm_field;
  752. if(!is_object($field))
  753. $field = $frm_field->getOne($field);
  754. $linked_field_id = isset($atts['show']) ? $atts['show'] : false;
  755. $field->field_options = maybe_unserialize($field->field_options);
  756. if (is_numeric($value) and (!isset($field->field_options['form_select']) or $field->field_options['form_select'] != 'taxonomy')){
  757. if (!$linked_field_id and is_numeric($field->field_options['form_select']))
  758. $linked_field_id = $field->field_options['form_select'];
  759. if ($linked_field_id){
  760. global $frm_entry_meta, $frmdb;
  761. $linked_field = $frm_field->getOne($linked_field_id);
  762. $linked_field->field_options = maybe_unserialize($linked_field->field_options);
  763. if(isset($linked_field->field_options['post_field']) and $linked_field->field_options['post_field']){
  764. global $frmdb;
  765. $post_id = $frmdb->get_var($frmdb->entries, array('id' => $value), 'post_id');
  766. if($post_id){
  767. if(!isset($atts['truncate']))
  768. $atts['truncate'] = false;
  769. $new_value = FrmProEntryMetaHelper::get_post_value($post_id, $linked_field->field_options['post_field'], $linked_field->field_options['custom_field'], array('form_id' => $linked_field->form_id, 'field' => $linked_field, 'type' => $linked_field->type, 'truncate' => $atts['truncate']));
  770. }else{
  771. $new_value = $frm_entry_meta->get_entry_meta_by_field($value, $linked_field->id);
  772. }
  773. }else{
  774. $new_value = $frm_entry_meta->get_entry_meta_by_field($value, $linked_field->id);
  775. }
  776. $value = (!empty($new_value) or $new_value == 0) ? $new_value : $value;
  777. if($linked_field){
  778. if(isset($atts['show']) and !is_numeric($atts['show']))
  779. $atts['show'] = $linked_field->id;
  780. else if(isset($atts['show']) and (int)$atts['show'] == $linked_field->id)
  781. unset($atts['show']);
  782. $value = FrmProFieldsHelper::get_display_value($value, $linked_field, $atts); //get display value
  783. }
  784. }
  785. }
  786. return $value;
  787. }
  788. function get_date($date, $date_format=false){
  789. if(empty($date))
  790. return $date;
  791. if (!$date_format)
  792. $date_format = get_option('date_format');
  793. if (preg_match('/^\d{1-2}\/\d{1-2}\/\d{4}$/', $date)){
  794. global $frmpro_settings;
  795. $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
  796. }
  797. return date_i18n($date_format, strtotime($date));
  798. }
  799. function get_display_name($user_id, $user_info='display_name', $args=array()){
  800. $defaults = array(
  801. 'blank' => false, 'link' => false, 'size' => 96
  802. );
  803. extract(wp_parse_args($args, $defaults));
  804. $user = get_userdata($user_id);
  805. $info = '';
  806. if($user){
  807. if($user_info == 'avatar'){
  808. $info = get_avatar( $user_id, $size );
  809. }else{
  810. $info = isset($user->$user_info) ? $user->$user_info : '';
  811. }
  812. if(empty($info) and !$blank)
  813. $info = $user->user_login;
  814. }
  815. if($link)
  816. $info = '<a href="'. admin_url('user-edit.php') .'?user_id='. $user_id .'">'. $info .'</a>';
  817. return $info;
  818. }
  819. function get_field_options($form_id, $value='', $include='not', $types="'break','divider','data','file','captcha'", $data_children=false){
  820. global $frm_field;
  821. $fields = $frm_field->getAll("fi.type $include in ($types) and fi.form_id=". (int)$form_id, 'fi.field_order');
  822. foreach ($fields as $field){
  823. $field->field_options = maybe_unserialize($field->field_options);
  824. if($field->type == 'data' and (!isset($field->field_options['data_type']) or $field->field_options['data_type'] == 'data' or $field->field_options['data_type'] == ''))
  825. continue;
  826. ?>
  827. <option value="<?php echo $field->id ?>" <?php selected($value, $field->id) ?>><?php echo FrmAppHelper::truncate($field->name, 50) ?></option>
  828. <?php
  829. }
  830. }
  831. function get_field_stats($id, $type='total', $user_id=false, $value=false, $round=100, $limit='', $atts=array()){
  832. global $frm_entry_meta, $wpdb, $frmdb, $frm_post_ids, $frm_field;
  833. $field = $frm_field->getOne($id);
  834. if(!$field)
  835. return 0;
  836. $field->field_options = maybe_unserialize($field->field_options);
  837. if($field->type == 'checkbox')
  838. $where_value = ($value) ? " AND meta_value LIKE '%".addslashes($value)."%'" : '';
  839. else
  840. $where_value = ($value) ? " AND meta_value='".addslashes($value)."'" : '';
  841. //if(!$frm_post_ids)
  842. $frm_post_ids = array();
  843. $post_ids = array();
  844. if(isset($frm_post_ids[$id])){
  845. $form_posts = $frm_post_ids[$id];
  846. }else{
  847. $where_post = array('form_id' => $field->form_id, 'post_id >' => 1);
  848. if($user_id)
  849. $where_post['user_id'] = $user_id;
  850. $form_posts = $frmdb->get_records($frmdb->entries, $where_post, '', '', 'id,post_id');
  851. $frm_post_ids[$id] = $form_posts;
  852. }
  853. if($form_posts){
  854. foreach($form_posts as $form_post)
  855. $post_ids[$form_post->id] = $form_post->post_id;
  856. }
  857. if(!empty($limit))
  858. $limit = " LIMIT ". $limit;
  859. if($value)
  860. $atts[$id] = $value;
  861. if(!empty($atts)){
  862. $entry_ids = array();
  863. if(isset($atts['entry_id']) and $atts['entry_id'] and is_numeric($atts['entry_id']))
  864. $entry_ids[] = $atts['entry_id'];
  865. $after_where = false;
  866. foreach($atts as $f => $val){
  867. if($f == 'entry_id')
  868. continue;
  869. //TODO: Make where_is dynamic
  870. $entry_ids = FrmProAppHelper::filter_where($entry_ids, array('where_opt' => $f, 'where_is' => '=', 'where_val' => $val, 'form_id' => $field->form_id, 'form_posts' => $form_posts, 'after_where' => $after_where));
  871. $after_where = true;
  872. }
  873. if(empty($entry_ids)){
  874. if($type == 'star'){
  875. $stat = '';
  876. ob_start();
  877. include(FRMPRO_VIEWS_PATH.'/frmpro-fields/star_disabled.php');
  878. $contents = ob_get_contents();
  879. ob_end_clean();
  880. return $contents;
  881. }else{
  882. return 0;
  883. }
  884. }
  885. foreach($post_ids as $entry_id => $post_id){
  886. if(!in_array($entry_id, $entry_ids))
  887. unset($post_ids[$entry_id]);
  888. }
  889. $where_value .= " AND it.item_id in (". implode(',', $entry_ids).")";
  890. }
  891. $join = '';
  892. if((is_numeric($id))){
  893. $where = "field_id='$id'";
  894. }else{
  895. $join .= " LEFT OUTER JOIN $frmdb->fields fi ON it.field_id=fi.id";
  896. $where = "fi.field_key='$id'";
  897. }
  898. $where .= $where_value;
  899. if($user_id){
  900. $where .= " AND en.user_id='$user_id'";
  901. $join .= " LEFT OUTER JOIN $frmdb->entries en ON en.id=it.item_id";
  902. }
  903. $field_metas = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas it $join WHERE $where ORDER BY it.created_at DESC". $limit);
  904. if(!empty($post_ids)){
  905. if(isset($field->field_options['post_field']) and $field->field_options['post_field']){
  906. if($field->field_options['post_field'] == 'post_custom'){ //get custom post field value
  907. $post_values = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key= %s AND post_id in (".implode(',', $post_ids) .")", $field->field_options['custom_field']));
  908. }else if($field->field_options['post_field'] == 'post_category'){
  909. //TODO count categories
  910. $post_values = array();
  911. }else{
  912. $post_values = $wpdb->get_col("SELECT {$field->field_options['post_field']} FROM $wpdb->posts WHERE ID in (".implode(',', $post_ids) .")");
  913. }
  914. $field_metas = array_merge($post_values, $field_metas);
  915. }
  916. }
  917. if($type != 'star')
  918. unset($field);
  919. if (empty($field_metas)){
  920. if($type == 'star'){
  921. $stat = '';
  922. ob_start();
  923. include(FRMPRO_VIEWS_PATH.'/frmpro-fields/star_disabled.php');
  924. $contents = ob_get_contents();
  925. ob_end_clean();
  926. return $contents;
  927. }else{
  928. return 0;
  929. }
  930. }
  931. $count = count($field_metas);
  932. $total = array_sum($field_metas);
  933. switch($type){
  934. case 'average':
  935. case 'mean':
  936. case 'star':
  937. $stat = ($total / $count);
  938. break;
  939. case 'median':
  940. rsort($field_metas);
  941. $n = ceil($count / 2); // Middle of the array
  942. if ($count % 2){
  943. $stat = $field_metas[$n-1]; // If number is odd
  944. }else{
  945. $n2 = floor($count / 2); // Other middle of the array
  946. $stat = ($field_metas[$n-1] + $field_metas[$n2-1]) / 2;
  947. }
  948. $stat = maybe_unserialize($stat);
  949. if (is_array($stat))
  950. $stat = 0;
  951. break;
  952. case 'deviation':
  953. $mean = ($total / $count);
  954. $stat = 0.0;
  955. foreach ($field_metas as $i)
  956. $stat += pow($i - $mean, 2);
  957. if($count > 1){
  958. $stat /= ( $count - 1 );
  959. $stat = sqrt($stat);
  960. }else{
  961. $stat = 0;
  962. }
  963. break;
  964. case 'minimum':
  965. $stat = min($field_metas);
  966. break;
  967. case 'maximum':
  968. $stat = max($field_metas);
  969. break;
  970. case 'count':
  971. $stat = $count;
  972. break;
  973. case 'total':
  974. default:
  975. $stat = $total;
  976. }
  977. $stat = round($stat, $round);
  978. if($type == 'star'){
  979. ob_start();
  980. include(FRMPRO_VIEWS_PATH.'/frmpro-fields/star_disabled.php');
  981. $contents = ob_get_contents();
  982. ob_end_clean();
  983. return $contents;
  984. }
  985. if($round and $round < 5)
  986. $stat = number_format($stat, $round);
  987. return $stat;
  988. }
  989. function value_meets_condition($observed_value, $cond, $hide_opt){
  990. if($hide_opt == '')
  991. return false;
  992. if(is_array($observed_value)){
  993. if($cond == '=='){
  994. if(is_array($hide_opt)){
  995. $m = array_intersect($hide_opt, $observed_value);
  996. $m = empty($m) ? false : true;
  997. }else{
  998. $m = in_array($hide_opt, $observed_value);
  999. }
  1000. }else if($cond == '!='){
  1001. $m = !in_array($hide_opt, $observed_value);
  1002. }else if($cond == '>'){
  1003. $min = min($observed_value);
  1004. $m = $min > $hide_opt;
  1005. }else if($cond == '<'){
  1006. $max = max($observed_value);
  1007. $m = $max < $hide_opt;
  1008. }
  1009. }else{
  1010. if($cond == '==')
  1011. $m = $observed_value == $hide_opt;
  1012. else if($cond == '!=')
  1013. $m = $observed_value != $hide_opt;
  1014. else if($cond == '>')
  1015. $m = $observed_value > $hide_opt;
  1016. else if($cond == '<')
  1017. $m = $observed_value < $hide_opt;
  1018. }
  1019. return $m;
  1020. }
  1021. function get_shortcode_select($form_id, $target_id='content', $type='all'){
  1022. global $frm_field, $frmdb;
  1023. $field_list = array();
  1024. if(is_numeric($form_id)){
  1025. $exclude = "'divider','captcha','break','html'";
  1026. if($type == 'field_opt')
  1027. $exclude .= ",'data','checkbox'";
  1028. else if($type == 'calc')
  1029. $exclude .= ",'data'";
  1030. $field_list = $frm_field->getAll("fi.type not in (". $exclude .") and fi.form_id=". (int)$form_id, 'field_order');
  1031. }
  1032. $linked_forms = array();
  1033. ?>
  1034. <select class="frm_shortcode_select" onchange="frmInsertFieldCode('<?php echo $target_id ?>',this.value);this.value='';">
  1035. <option value="">- <?php _e('Select a value to insert into the box below', 'formidable') ?> -</option>
  1036. <?php if($type != 'field_opt' and $type != 'calc'){ ?>
  1037. <option value="id"><?php _e('Entry ID', 'formidable') ?></option>
  1038. <option value="key"><?php _e('Entry Key', 'formidable') ?></option>
  1039. <option value="post_id"><?php _e('Post ID', 'formidable') ?></option>
  1040. <option value="ip"><?php _e('User IP', 'formidable') ?></option>
  1041. <option value="created-at"><?php _e('Entry creation date', 'formidable') ?></option>
  1042. <option value="updated-at"><?php _e('Entry update date', 'formidable') ?></option>
  1043. <optgroup label="<?php _e('Form Fields', 'formidable') ?>">
  1044. <?php }
  1045. if(!empty($field_list)){
  1046. foreach ($field_list as $field){
  1047. $field->field_options = maybe_unserialize($field->field_options);
  1048. if($field->type == 'data' and (!isset($field->field_options['data_type']) or $field->field_options['data_type'] == 'data' or $field->field_options['data_type'] == ''))
  1049. continue;
  1050. ?>
  1051. <option value="<?php echo $field->id ?>"><?php echo $field_name = FrmAppHelper::truncate($field->name, 60) ?> (<?php _e('ID', 'formidable') ?>)</option>
  1052. <option value="<?php echo $field->field_key ?>"><?php echo $field_name ?> (<?php _e('Key', 'formidable') ?>)</option>
  1053. <?php if ($field->type == 'file' and $type != 'field_opt' and $type != 'calc'){ ?>
  1054. <option class="frm_subopt" value="<?php echo $field->field_key ?> size=thumbnail"><?php _e('Thumbnail', 'formidable') ?></option>
  1055. <option class="frm_subopt" value="<?php echo $field->field_key ?> size=medium"><?php _e('Medium', 'formidable') ?></option>
  1056. <option class="frm_subopt" value="<?php echo $field->field_key ?> size=large"><?php _e('Large', 'formidable') ?></option>
  1057. <option class="frm_subopt" value="<?php echo $field->field_key ?> size=full"><?php _e('Full Size', 'formidable') ?></option>
  1058. <?php }else if($field->type == 'data'){ //get all fields from linked form
  1059. if (isset($field->field_options['form_select']) && is_numeric($field->field_options['form_select'])){
  1060. $linked_form = $frmdb->get_var($frmdb->fields, array('id' => $field->field_options['form_select']), 'form_id');
  1061. if(!in_array($linked_form, $linked_forms)){
  1062. $linked_forms[] = $linked_form;
  1063. $linked_fields = $frm_field->getAll("fi.type not in ('divider','captcha','break','html') and fi.form_id =". (int)$linked_form);
  1064. foreach ($linked_fields as $linked_field){ ?>
  1065. <option class="frm_subopt" value="<?php echo $field->id ?> show=<?php echo $linked_field->id ?>"><?php echo FrmAppHelper::truncate($linked_field->name, 60) ?> (<?php _e('ID', 'formidable') ?>)</option>
  1066. <option class="frm_subopt" value="<?php echo $field->field_key ?> show=<?php echo $linked_field->field_key ?>"><?php echo FrmAppHelper::truncate($linked_field->name, 60) ?> (<?php _e('Key', 'formidable') ?>)</option>
  1067. <?php
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. if($type != 'field_opt' and $type != 'calc'){ ?>
  1075. </optgroup>
  1076. <optgroup label="<?php _e('Helpers', 'formidable') ?>">
  1077. <option value="editlink"><?php _e('Admin link to edit the entry', 'formidable') ?></option>
  1078. <?php if ($target_id == 'content'){ ?>
  1079. <option value="detaillink"><?php _e('Link to view single page if showing dynamic entries', 'formidable') ?></option>
  1080. <?php }
  1081. if($type != 'email'){ ?>
  1082. <option value="evenodd"><?php _e('Add a rotating \'even\' or \'odd\' class', 'formidable') ?></option>
  1083. <?php }else if($target_id == 'email_message'){ ?>
  1084. <option value="default-message"><?php _e('Default Email Message', 'formidable') ?></option>
  1085. <?php } ?>
  1086. <option value="siteurl"><?php _e('Site URL', 'formidable') ?></option>
  1087. <option value="sitename"><?php _e('Site Name', 'formidable') ?></option>
  1088. </optgroup>
  1089. <?php } ?>
  1090. </select>
  1091. <?php
  1092. }
  1093. function replace_shortcodes($content, $entry, $shortcodes, $display=false, $show='one', $odd=''){
  1094. global $frm_field, $frm_entry_meta, $post, $frmpro_settings;
  1095. if($display){
  1096. $param_value = ($display->frm_type == 'id') ? $entry->id : $entry->item_key;
  1097. if($entry->post_id){
  1098. $detail_link = get_permalink($entry->post_id);
  1099. }else{
  1100. $param = (isset($display->frm_param) && !empty($display->frm_param)) ? $display->frm_param : 'entry';
  1101. if($post)
  1102. $detail_link = add_query_arg($param, $param_value, get_permalink($post->ID));
  1103. else
  1104. $detail_link = add_query_arg($param, $param_value);
  1105. //if( FrmProAppHelper::rewriting_on() && $frmpro_settings->permalinks )
  1106. // $detail_link = get_permalink($post->ID) .$param_value .'/';
  1107. }
  1108. }
  1109. foreach ($shortcodes[0] as $short_key => $tag){
  1110. $conditional = (preg_match('/^\[if/s', $shortcodes[0][$short_key])) ? true : false;
  1111. $atts = shortcode_parse_atts( $shortcodes[3][$short_key] );
  1112. if(!empty($shortcodes[3][$short_key])){
  1113. if($conditional)
  1114. $tag = str_replace('[if ', '', $shortcodes[0][$short_key]);
  1115. else
  1116. $tag = str_replace('[', '',$shortcodes[0][$short_key]);
  1117. $tag = str_replace(']', '', $tag);
  1118. $tags = explode(' ', $tag);
  1119. if(is_array($tags))
  1120. $tag = $tags[0];
  1121. }else
  1122. $tag = $shortcodes[2][$short_key];
  1123. switch($tag){
  1124. case 'detaillink':
  1125. if($display and $detail_link)
  1126. $content = str_replace($shortcodes[0][$short_key], $detail_link, $content);
  1127. break;
  1128. case 'id':
  1129. $content = str_replace($shortcodes[0][$short_key], $entry->id, $content);
  1130. break;
  1131. case 'post-id':
  1132. case 'post_id':
  1133. $content = str_replace($shortcodes[0][$short_key], $entry->post_id, $content);
  1134. break;
  1135. case 'key':
  1136. $content = str_replace($shortcodes[0][$short_key], $entry->item_key, $content);
  1137. break;
  1138. case 'ip':
  1139. $content = str_replace($shortcodes[0][$short_key], $entry->ip, $content);
  1140. break;
  1141. case 'user_agent':
  1142. case 'user-agent':
  1143. $entry->description = maybe_unserialize($entry->description);
  1144. $content = str_replace($shortcodes[0][$short_key], $entry->description['browser'], $content);
  1145. break;
  1146. case 'created-at':
  1147. case 'updated-at':
  1148. if(!isset($atts['format'])){
  1149. $atts['format'] = get_option('date_format');
  1150. $time_format = false;
  1151. }else{
  1152. $time_format = ' ';
  1153. }
  1154. $this_tag = str_replace('-', '_', $tag);
  1155. if ($conditional){
  1156. $replace_with = apply_filters('frm_conditional_value', $entry->{$this_tag}, $atts, false, $tag);
  1157. if($atts)
  1158. $content = str_replace($shortcodes[0][$short_key], '[if '.$tag.']', $content);
  1159. if (empty($replace_with)){
  1160. $content = preg_replace('/(\[if\s+'.$tag.'\])(.*?)(\[\/if\s+'.$tag.'\])/mis', '', $content);
  1161. }else{
  1162. $content = preg_replace('/(\[if\s+'.$tag.'\])/', '', $content, 1);
  1163. $content = preg_replace('/(\[\/if\s+'.$tag.'\])/', '', $content, 1);
  1164. }
  1165. }else{
  1166. if(isset($atts['time_ago']))
  1167. $date = FrmProAppHelper::human_time_diff( strtotime($entry->{$this_tag}) );
  1168. else
  1169. $date = FrmProAppHelper::get_formatted_time($entry->{$this_tag}, $atts['format'], $time_format);
  1170. $content = str_replace($shortcodes[0][$short_key], $date, $content);
  1171. }
  1172. unset($this_tag);
  1173. break;
  1174. case 'evenodd':
  1175. $content = str_replace($shortcodes[0][$short_key], $odd, $content);
  1176. break;
  1177. case 'siteurl':
  1178. global $frm_siteurl;
  1179. $content = str_replace($shortcodes[0][$short_key], $frm_siteurl, $content);
  1180. break;
  1181. case 'sitename':
  1182. $content = str_replace($shortcodes[0][$short_key], get_option('blogname'), $content);
  1183. break;
  1184. case 'get':
  1185. if(isset($atts['param'])){
  1186. $param = $atts['param'];
  1187. $replace_with = FrmAppHelper::get_param($param);
  1188. if(is_array($replace_with))
  1189. $replace_with = implode(', ', $replace_with);
  1190. $content = str_replace($shortcodes[0][$short_key], $replace_with, $content);
  1191. unset($param);
  1192. unset($replace_with);
  1193. }
  1194. break;
  1195. default:
  1196. if($tag == 'deletelink'){
  1197. $page_id = isset($atts['page_id']) ? $atts['page_id'] : $post->ID;
  1198. $can_delete = FrmProEntriesHelper::allow_delete($entry);
  1199. if($can_delete){
  1200. if(isset($atts['label'])){
  1201. $delete_atts = $atts;
  1202. $delete_atts['id'] = $entry->id;
  1203. $delete_atts['page_id'] = $page_id;
  1204. $replace_with = FrmProEntriesController::entry_delete_link($delete_atts);
  1205. unset($delete_atts);
  1206. }else{
  1207. $replace_with = add_query_arg(array('frm_action' => 'destroy', 'entry' => $entry->id), get_permalink($page_id));
  1208. }
  1209. }else{
  1210. $replace_with = '';
  1211. }
  1212. $field = false;
  1213. }else if($tag == 'editlink'){
  1214. $replace_with = '';
  1215. $link_text = (isset($atts['label'])) ? $atts['label'] : false;
  1216. if(!$link_text)
  1217. $link_text = (isset($atts['link_text'])) ? $atts['link_text'] : __('Edit', 'formidable');
  1218. $class = (isset($atts['class'])) ? $atts['class'] : '';
  1219. $page_id = isset($atts['page_id']) ? $atts['page_id'] : $post->ID;
  1220. if(isset($atts['location']) and $atts['location'] == 'front'){
  1221. $edit_atts = $atts;
  1222. $edit_atts['id'] = $entry->id;
  1223. $delete_atts['page_id'] = $page_id;
  1224. $replace_with = FrmProEntriesController::entry_edit_link($edit_atts);
  1225. }else{
  1226. if($entry->post_id){
  1227. $replace_with = get_edit_post_link($entry->post_id);
  1228. }else{
  1229. global $frm_siteurl;
  1230. if(current_user_can('frm_edit_entries'))
  1231. $replace_with = esc_url($frm_siteurl . '/wp-admin/admin.php?page=formidable-entries&frm_action=edit&id='.$entry->id );
  1232. }
  1233. if(!empty($replace_with))
  1234. $replace_with = '<a href="'. $replace_with . '" class="frm_edit_link '. $class .'">'. $link_text .'</a>';
  1235. }
  1236. unset($class);
  1237. }else{
  1238. $field = $frm_field->getOne( $tag );
  1239. }
  1240. $sep = (isset($atts['sep'])) ? $atts['sep'] : ', ';
  1241. if(!isset($field))
  1242. $field = false;
  1243. if($field){
  1244. $field->field_options = maybe_unserialize($field->field_options);
  1245. $replace_with = FrmProEntryMetaHelper::get_post_or_meta_value($entry, $field, $atts);
  1246. $replace_with = maybe_unserialize($replace_with);
  1247. $atts['entry_id'] = $entry->id;
  1248. $atts['entry_key'] = $entry->item_key;
  1249. $atts['post_id'] = $entry->post_id;
  1250. $replace_with = apply_filters('frmpro_fields_replace_shortcodes', $replace_with, $tag, $atts, $field);
  1251. }
  1252. if ($field and $field->type == 'file'){
  1253. //size options are thumbnail, medium, large, or full
  1254. $size = (isset($atts['size'])) ? $atts['size'] : (isset($atts['show']) ? $atts['show'] : 'thumbnail');
  1255. $inc_html = (isset($atts['html']) and $atts['html']) ? true : false;
  1256. $sep = (isset($atts['sep'])) ? $atts['sep'] : ' ';
  1257. if($size != 'id' and !empty($replace_with))
  1258. $replace_with = FrmProFieldsHelper::get_media_from_id($replace_with, $size, $inc_html);
  1259. unset($size);
  1260. }
  1261. if (isset($replace_with) and is_array($replace_with))
  1262. $replace_with = implode($sep, $replace_with);
  1263. if ($conditional){
  1264. $replace_with = apply_filters('frm_conditional_value', $replace_with, $atts, $field, $tag);
  1265. if($atts)
  1266. $content = str_replace($shortcodes[0][$short_key], '[if '.$tag.']', $content);
  1267. if (empty($replace_with)){
  1268. $content = preg_replace('/(\[if\s+'.$tag.'\])(.*?)(\[\/if\s+'.$tag.'\])/mis', '', $content);
  1269. }else{
  1270. $content = preg_replace('/(\[if\s+'.$tag.'\])/', '', $content, 1);
  1271. $content = preg_replace('/(\[\/if\s+'.$tag.'\])/', '', $content, 1);
  1272. }
  1273. }else{
  1274. if($field){
  1275. if (isset($atts['show']) and $atts['show'] == 'field_label'){
  1276. $replace_with = stripslashes($field->name);
  1277. }else if (empty($replace_with) and $replace_with != '0'){
  1278. $replace_with = '';
  1279. if ($field->type == 'number')
  1280. $replace_with = '0';
  1281. }else{
  1282. $replace_with = FrmProFieldsHelper::get_display_value($replace_with, $field, $atts);
  1283. }
  1284. }
  1285. if (isset($atts['sanitize']))
  1286. $replace_with = sanitize_title_with_dashes($replace_with);
  1287. if (isset($atts['sanitize_url']))
  1288. $replace_with = urlencode(htmlentities($replace_with));
  1289. if (isset($atts['truncate'])){
  1290. if(isset($atts['more_text']))
  1291. $more_link_text = $atts['more_text'];
  1292. else
  1293. $more_link_text = (isset($atts['more_link_text'])) ? $atts['more_link_text'] : '. . .';
  1294. if ($display and $display->frm_show_count == 'dynamic'){
  1295. $more_link_text = ' <a href="'. $detail_link .'">'. $more_link_text .'</a>';
  1296. $replace_with = FrmAppHelper::truncate($replace_with, (int)$atts['truncate'], 3, $more_link_text);
  1297. }else{
  1298. $replace_with = wp_specialchars_decode(strip_tags($replace_with), ENT_QUOTES);
  1299. $part_one = substr($replace_with, 0, (int)$atts['truncate']);
  1300. $part_two = substr($replace_with, (int)$atts['truncate']);
  1301. $replace_with = $part_one .'<a href="#" onclick="jQuery(this).next().css(\'display\', \'inline\');jQuery(this).css(\'display\', \'none\');return false;" class="frm_text_exposed_show"> '. $more_link_text .'</a><span style="display:none;">'. $part_two .'</span>';
  1302. }
  1303. }
  1304. if(isset($atts['clickable']))
  1305. $replace_with = make_clickable($replace_with);
  1306. if (!isset($replace_with))
  1307. $replace_with = '';
  1308. $content = str_replace($shortcodes[0][$short_key], $replace_with, $content);
  1309. }
  1310. unset($replace_with);
  1311. if(isset($field))
  1312. unset($field);
  1313. }
  1314. unset($atts);
  1315. unset($conditional);
  1316. }
  1317. return $content;
  1318. }
  1319. function conditional_replace_with_value($replace_with, $atts, $field, $tag){
  1320. if($field and isset($atts['show']) and $field->type == 'data'){
  1321. $old_replace_with = $replace_with;
  1322. $replace_with = FrmProFieldsHelper::get_display_value($replace_with, $field, $atts);
  1323. if($old_replace_with == $replace_with)
  1324. $replace_with = '';
  1325. }
  1326. if(isset($atts['equals']) and ($replace_with != $atts['equals'])){
  1327. if($field and $field->type == 'data'){
  1328. $replace_with = FrmProFieldsHelper::get_display_value($replace_with, $field, $atts);
  1329. if($replace_with != $atts['equals'])
  1330. $replace_with = '';
  1331. }else if($field->field_options['post_field'] == 'post_category'){
  1332. $cats = explode(', ', $replace_with);
  1333. $replace_with = '';
  1334. foreach($cats as $cat){
  1335. if($replace_with == true)
  1336. continue;
  1337. if($atts['equals'] == strip_tags($cat))
  1338. $replace_with = true;
  1339. }
  1340. }else{
  1341. $replace_with = '';
  1342. }
  1343. }else if(isset($atts['equals']) and (($atts['equals'] == '' and $replace_with == '') or ($atts['equals'] == '0' and $replace_with == '0'))){
  1344. $replace_with = true; //if the field is blank, give it a value
  1345. }
  1346. if(isset($atts['not_equal'])){
  1347. if($replace_with == $atts['not_equal']){
  1348. $replace_with = '';
  1349. }else if(!empty($replace_with) and $field->field_options['post_field'] == 'post_category'){
  1350. $cats = explode(', ', $replace_with);
  1351. foreach($cats as $cat){
  1352. if(empty($replace_with))
  1353. continue;
  1354. if($atts['not_equal'] == strip_tags($cat))
  1355. $replace_with = '';
  1356. }
  1357. }
  1358. }
  1359. if(isset($atts['like'])){
  1360. if(strpos($replace_with, $atts['like']) === false)
  1361. $replace_with = '';
  1362. }
  1363. if(isset($atts['not_like'])){
  1364. if(strpos($replace_with, $atts['not_like']) !== false)
  1365. $replace_with = '';
  1366. }
  1367. if(isset($atts['less_than'])){
  1368. if($field and $field->type == 'date' and !preg_match('/^\d{4}-\d{2}-\d{2}$/', trim($atts['less_than'])))
  1369. $atts['less_than'] = date_i18n('Y-m-d', strtotime($atts['less_than']));
  1370. if($atts['less_than'] < $replace_with)
  1371. $replace_with = '';
  1372. }
  1373. if(isset($atts['greater_than'])){
  1374. if($field and $field->type == 'date' and !preg_match('/^\d{4}-\d{2}-\d{2}$/', trim($atts['greater_than'])))
  1375. $atts['greater_than'] = date_i18n('Y-m-d', strtotime($atts['greater_than']));
  1376. if($atts['greater_than'] > $replace_with)
  1377. $replace_with = '';
  1378. }
  1379. return $replace_with;
  1380. }
  1381. function get_media_from_id($ids, $size='thumbnail', $html=false){
  1382. $replace_with = array();
  1383. if($size == 'label'){
  1384. foreach((array)$ids as $id){
  1385. if(!is_numeric($id))
  1386. continue;
  1387. $attachment = get_post($id);
  1388. if($attachment)
  1389. $replace_with[] = basename($attachment->guid);
  1390. }
  1391. }else{
  1392. foreach((array)$ids as $id){
  1393. if(!is_numeric($id))
  1394. continue;
  1395. $image = wp_get_attachment_image_src($id, $size); //Returns an array (url, width, height) or false
  1396. if($image)
  1397. $img = $image[0];
  1398. else
  1399. $img = wp_get_attachment_url($id);
  1400. if($html)
  1401. $img = '<img src="'. $img .'" />';
  1402. $replace_with[] = $img;
  1403. unset($img);
  1404. unset($id);
  1405. }
  1406. }
  1407. if(count($replace_with) == 1)
  1408. $replace_with = reset($replace_with);
  1409. return $replace_with;
  1410. }
  1411. function get_display_value($replace_with, $field, $atts=array()){
  1412. $sep = (isset($atts['sep'])) ? $atts['sep'] : ', ';
  1413. if ($field->type == 'user_id'){
  1414. $user_info = (isset($atts['show'])) ? $atts['show'] : 'display_name';
  1415. $replace_with = FrmProFieldsHelper::get_display_name($replace_with, $user_info, $atts);
  1416. if(is_array($replace_with)){
  1417. $new_val = '';
  1418. foreach($replace_with as $key => $val){
  1419. if(!empty($new_val))
  1420. $new_val .= ', ';
  1421. $new_val .= $key .'. '. $val;
  1422. }
  1423. $replace_with = $new_val;
  1424. }
  1425. }else if ($field->type == 'date'){
  1426. if(isset($atts['time_ago']))
  1427. $atts['format'] = 'Y-m-d H:i:s';
  1428. if(!isset($atts['format']))
  1429. $atts['format'] = false;
  1430. $replace_with = FrmProFieldsHelper::get_date($replace_with, $atts['format']);
  1431. if(isset($atts['time_ago']))
  1432. $replace_with = FrmProAppHelper::human_time_diff( strtotime($replace_with), strtotime(date_i18n('Y-m-d')) );
  1433. }else if ((is_numeric($replace_with) or is_array($replace_with)) and $field->type == 'file'){
  1434. //size options are thumbnail, medium, large, or full
  1435. $size = (isset($atts['size'])) ? $atts['size'] : (isset($atts['show']) ? $atts['show'] : 'thumbnail');
  1436. $inc_html = (isset($atts['html']) and $atts['html']) ? true : false;
  1437. $sep = (isset($atts['sep'])) ? $atts['sep'] : ' ';
  1438. if($size != 'id')
  1439. $replace_with = FrmProFieldsHelper::get_media_from_id($replace_with, $size, $inc_html);
  1440. if(is_array($replace_with))
  1441. $replace_with = implode($sep, $replace_with);
  1442. }else if ($field->type == 'data'){ //and (is_numeric($replace_with) or is_array($replace_with))
  1443. $field->field_options = maybe_unserialize($field->field_options);
  1444. if(isset($field->field_options['form_select']) and $field->field_options['form_select'] == 'taxonomy')
  1445. return $replace_with;
  1446. $replace_with = explode($sep, $replace_with);
  1447. if (isset($atts['show'])){
  1448. if (in_array($atts['show'], array('key', 'created-at', 'created_at', 'updated-at', 'updated_at', 'post_id'))){
  1449. global $frm_entry;
  1450. if(is_array($replace_with)){
  1451. $linked_ids = $replace_with;
  1452. $replace_with = '';
  1453. foreach($linked_ids as $linked_id){
  1454. $linked_entry = FrmEntry::getOne($linked_id);
  1455. if(!empty($replace_with))
  1456. $replace_with .= $sep;
  1457. if($atts['show'] == 'created-at')
  1458. $replace_with .= $linked_entry->created_at;
  1459. else if($atts['show'] == 'updated-at')
  1460. $replace_with .= $linked_entry->updated_at;
  1461. else if($atts['show'] == 'key')
  1462. $replace_with .= $linked_entry->item_key;
  1463. else
  1464. $replace_with .= (isset($linked_entry->{$atts['show']})) ? $linked_entry->{$atts['show']} : $linked_entry->item_key;
  1465. }
  1466. }else{
  1467. $linked_entry = FrmEntry::getOne($replace_with);
  1468. if($atts['show'] == 'created-at')
  1469. $replace_with = $linked_entry->created_at;
  1470. else if($atts['show'] == 'updated-at')
  1471. $replace_with = $linked_entry->updated_at;
  1472. else if($atts['show'] == 'key')
  1473. $replace_with = $linked_entry->item_key;
  1474. else
  1475. $replace_with = (isset($linked_entry->{$atts['show']})) ? $linked_entry->{$atts['show']} : $linked_entry->item_key;
  1476. }
  1477. }else if ($atts['show'] == 'id'){
  1478. if(is_array($replace_with))
  1479. $replace_with = implode($sep, $replace_with);
  1480. //just keep the value since it's already the id
  1481. }else{
  1482. if(is_array($replace_with)){
  1483. $linked_ids = $replace_with;
  1484. $replace_with = array();
  1485. foreach($linked_ids as $linked_id){
  1486. $new_val = FrmProFieldsHelper::get_data_value($linked_id, $field, $atts);
  1487. if($linked_id != $new_val){
  1488. if(is_array($new_val))
  1489. $new_val = implode($sep, $new_val);
  1490. $replace_with[] = $new_val;
  1491. }
  1492. unset($new_val);
  1493. }
  1494. $replace_with = implode($sep, $replace_with);
  1495. }else
  1496. $replace_with = FrmProFieldsHelper::get_data_value($replace_with, $field, $atts);
  1497. }
  1498. }else{
  1499. if(is_array($replace_with)){
  1500. $linked_ids = $replace_with;
  1501. $replace_with = array();
  1502. foreach($linked_ids as $linked_id){
  1503. $new_val = FrmProFieldsHelper::get_data_value($linked_id, $field, $atts);
  1504. if($linked_id != $new_val)
  1505. $replace_with[] = $new_val;
  1506. unset($new_val);
  1507. }
  1508. $replace_with = implode($sep, $replace_with);
  1509. }else
  1510. $replace_with = FrmProFieldsHelper::get_data_value($replace_with, $field, $atts);
  1511. }
  1512. }else if($field->type == 'textarea'){
  1513. $autop = isset($atts['wpautop']) ? $atts['wpautop'] : true;
  1514. if(apply_filters('frm_use_wpautop', $autop))
  1515. $replace_with = wpautop($replace_with);
  1516. unset($autop);
  1517. }else if($field->type == 'number'){
  1518. if(!isset($atts['decimal'])){
  1519. $num = explode('.', $replace_with);
  1520. $atts['decimal'] = (isset($num[1])) ? strlen($num[1]) : 0;
  1521. }
  1522. if(!isset($atts['dec_point']))
  1523. $atts['dec_point'] = '.';
  1524. if(!isset($atts['thousands_sep']))
  1525. $atts['thousands_sep'] = '';
  1526. $replace_with = number_format($replace_with, $atts['decimal'], $atts['dec_point'], $atts['thousands_sep']);
  1527. }
  1528. $replace_with = stripslashes_deep($replace_with);
  1529. return $replace_with;
  1530. }
  1531. function get_table_options($field_options){
  1532. $columns = array();
  1533. $rows = array();
  1534. if (is_array($field_options)){
  1535. foreach ($field_options as $opt_key => $opt){
  1536. switch(substr($opt_key,0,3)){
  1537. case 'col':
  1538. $columns[$opt_key] = $opt;
  1539. break;
  1540. case 'row':
  1541. $rows[$opt_key] = $opt;
  1542. break;
  1543. }
  1544. }
  1545. }
  1546. return array($columns,$rows);
  1547. }
  1548. function set_table_options($field_options, $columns, $rows){
  1549. if (is_array($field_options)){
  1550. foreach ($field_options as $opt_key => $opt){
  1551. if (substr($opt_key, 0, 3) == 'col' or substr($opt_key, 0, 3) == 'row')
  1552. unset($field_options[$opt_key]);
  1553. }
  1554. }else
  1555. $field_options = array();
  1556. foreach ($columns as $opt_key => $opt)
  1557. $field_options[$opt_key] = $opt;
  1558. foreach ($rows as $opt_key => $opt)
  1559. $field_options[$opt_key] = $opt;
  1560. return $field_options;
  1561. }
  1562. function mobile_check(){
  1563. global $frm_mobile;
  1564. $frm_mobile = array();
  1565. $frm_mobile['browsers'] = '2.0 MMP, 240x320, 400X240, AvantGo, BlackBerry, Blazer, Cellphone, Danger, DoCoMo, Elaine/3.0, EudoraWeb, Googlebot-Mobile, hiptop, IEMobile, KYOCERA/WX310K, LG/U990, MIDP-2., MMEF20, MOT-V, NetFront, Newt, Nintendo Wii, Nitro, Nokia, Opera Mini, Palm, PlayStation Portable, portalmmm, Proxinet, ProxiNet, SHARP-TQ-GX10, SHG-i900, Small, SonyEricsson, Symbian OS, SymbianOS, TS21i-10, UP.Browser, UP.Link, webOS, Windows CE, WinWAP, YahooSeeker/M1A1-R2D2';
  1566. $frm_mobile['touch'] = 'iPhone, iPad, iPod, Android, BlackBerry9530, LG-TU915 Obigo, LGE VX, webOS, Nokia5800';
  1567. //$frm_mobile = get_option('frm_mobile');
  1568. $browsers = explode(',', trim($frm_mobile['browsers']));
  1569. $touch = explode(',', trim($frm_mobile['touch']));
  1570. $mobiles = array_merge($browsers, $touch);
  1571. if (!isset($_SERVER["HTTP_USER_AGENT"]) || (isset($_COOKIE['frm_mobile']) && $_COOKIE['frm_mobile'] == 'false'))
  1572. $ismobile = false;
  1573. else if (isset($_COOKIE['frm_mobile']) && $_COOKIE['frm_mobile'] == 'true')
  1574. $ismobile = true;
  1575. if (!isset($ismobile) && count($mobiles)){
  1576. foreach ($mobiles as $mobile){
  1577. if (!empty($mobile) && strpos($_SERVER["HTTP_USER_AGENT"], trim($mobile)) != false){
  1578. $ismobile = true;
  1579. //setcookie('frm_mobile', 'true', time() + 300000, '/', false);
  1580. }
  1581. }
  1582. }
  1583. if (!isset($ismobile))
  1584. $ismobile = false;
  1585. $frm_mobile = $ismobile;
  1586. return $frm_mobile;
  1587. }
  1588. }
  1589. ?>