PageRenderTime 54ms CodeModel.GetById 14ms 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

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

  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 $fie

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