| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Formidable |
| 4 |
*/ |
| 5 |
|
| 6 |
if(!defined('ABSPATH')) die('You are not allowed to call this page directly.'); |
| 7 |
|
| 8 |
if(class_exists('FrmEntriesController')) |
| 9 |
return; |
| 10 |
|
| 11 |
class FrmEntriesController{ |
| 12 |
|
| 13 |
public static function load_hooks(){ |
| 14 |
add_action('admin_menu', 'FrmEntriesController::menu', 11); |
| 15 |
add_action('wp', 'FrmEntriesController::process_entry', 10, 0); |
| 16 |
add_action('frm_wp', 'FrmEntriesController::process_entry', 10, 0); |
| 17 |
add_filter('frm_redirect_url', 'FrmEntriesController::delete_entry_before_redirect', 50, 3); |
| 18 |
add_action('frm_after_entry_processed', 'FrmEntriesController::delete_entry_after_save', 100); |
| 19 |
add_filter('frm_email_value', 'FrmEntriesController::filter_email_value', 10, 3); |
| 20 |
} |
| 21 |
|
| 22 |
public static function menu(){ |
| 23 |
global $frm_vars; |
| 24 |
if(!$frm_vars['pro_is_installed']){ |
| 25 |
add_submenu_page('formidable', 'Formidable |'. __('Entries', 'formidable'), '<span style="opacity:.5;filter:alpha(opacity=50);">'. __('Entries', 'formidable') .'</span>', 'administrator', 'formidable-entries', 'FrmEntriesController::list_entries'); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public static function list_entries(){ |
| 30 |
global $frm_entry; |
| 31 |
$frm_form = new FrmForm(); |
| 32 |
$form_select = $frm_form->getAll("is_template=0 AND (status is NULL OR status = '' OR status = 'published')", ' ORDER BY name'); |
| 33 |
$form_id = FrmAppHelper::get_param('form', false); |
| 34 |
if($form_id) |
| 35 |
$form = $frm_form->getOne($form_id); |
| 36 |
else |
| 37 |
$form = (isset($form_select[0])) ? $form_select[0] : 0; |
| 38 |
|
| 39 |
if($form) |
| 40 |
$entry_count = $frm_entry->getRecordCount($form->id); |
| 41 |
|
| 42 |
include(FrmAppHelper::plugin_path() .'/classes/views/frm-entries/list.php'); |
| 43 |
} |
| 44 |
|
| 45 |
public static function show_form($id='', $key='', $title=false, $description=false){ |
| 46 |
_deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' ); |
| 47 |
return FrmFormsController::show_form($id, $key, $title, $description); |
| 48 |
} |
| 49 |
|
| 50 |
public static function get_form($filename, $form, $title, $description) { |
| 51 |
_deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form()' ); |
| 52 |
return FrmFormsController::get_form($form, $title, $description); |
| 53 |
} |
| 54 |
|
| 55 |
public static function process_entry($errors='', $ajax=false){ |
| 56 |
if((is_admin() and !defined('DOING_AJAX')) or !isset($_POST) or !isset($_POST['form_id']) or !is_numeric($_POST['form_id']) or !isset($_POST['item_key'])) |
| 57 |
return; |
| 58 |
|
| 59 |
global $frm_entry, $frm_vars; |
| 60 |
|
| 61 |
$frm_form = new FrmForm(); |
| 62 |
$form = $frm_form->getOne($_POST['form_id']); |
| 63 |
if(!$form) |
| 64 |
return; |
| 65 |
|
| 66 |
$params = FrmEntriesController::get_params($form); |
| 67 |
|
| 68 |
if(!isset($frm_vars['form_params'])) |
| 69 |
$frm_vars['form_params'] = array(); |
| 70 |
$frm_vars['form_params'][$form->id] = $params; |
| 71 |
|
| 72 |
if(isset($frm_vars['created_entries'][$_POST['form_id']])) |
| 73 |
return; |
| 74 |
|
| 75 |
if($errors == '') |
| 76 |
$errors = $frm_entry->validate($_POST); |
| 77 |
$frm_vars['created_entries'][$_POST['form_id']] = array('errors' => $errors); |
| 78 |
|
| 79 |
if( empty($errors) ){ |
| 80 |
$_POST['frm_skip_cookie'] = 1; |
| 81 |
if($params['action'] == 'create'){ |
| 82 |
if (apply_filters('frm_continue_to_create', true, $_POST['form_id']) and !isset($frm_vars['created_entries'][$_POST['form_id']]['entry_id'])) |
| 83 |
$frm_vars['created_entries'][$_POST['form_id']]['entry_id'] = $frm_entry->create( $_POST ); |
| 84 |
} |
| 85 |
|
| 86 |
do_action('frm_process_entry', $params, $errors, $form, array('ajax' => $ajax)); |
| 87 |
unset($_POST['frm_skip_cookie']); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public static function delete_entry_before_redirect($url, $form, $atts){ |
| 92 |
self::_delete_entry($atts['id'], $form); |
| 93 |
return $url; |
| 94 |
} |
| 95 |
|
| 96 |
//Delete entry if not redirected |
| 97 |
public static function delete_entry_after_save($atts){ |
| 98 |
self::_delete_entry($atts['entry_id'], $atts['form']); |
| 99 |
} |
| 100 |
|
| 101 |
private static function _delete_entry($entry_id, $form){ |
| 102 |
if(!$form) |
| 103 |
return; |
| 104 |
|
| 105 |
$form->options = maybe_unserialize($form->options); |
| 106 |
if(isset($form->options['no_save']) and $form->options['no_save']){ |
| 107 |
global $frm_entry; |
| 108 |
$frm_entry->destroy( $entry_id ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public static function show_entry_shortcode($atts){ |
| 113 |
$atts = shortcode_atts(array( |
| 114 |
'id' => false, 'entry' => false, 'fields' => false, 'plain_text' => false, |
| 115 |
'user_info' => false, 'include_blank' => false, 'default_email' => false, |
| 116 |
'form_id' => false, 'format' => 'text', 'direction' => 'ltr', |
| 117 |
'font_size' => '', 'text_color' => '', |
| 118 |
'border_width' => '', 'border_color' => '', |
| 119 |
'bg_color' => '', 'alt_bg_color' => '', |
| 120 |
), $atts); |
| 121 |
extract($atts); |
| 122 |
|
| 123 |
if ( $format != 'text' ) { |
| 124 |
//format options are text, array, or json |
| 125 |
$plain_text = true; |
| 126 |
} |
| 127 |
|
| 128 |
global $frm_entry; |
| 129 |
|
| 130 |
if ( !$entry || !is_object($entry) ) { |
| 131 |
if ( !$id && !$default_email ) { |
| 132 |
return ''; |
| 133 |
} |
| 134 |
|
| 135 |
if($id) |
| 136 |
$entry = $frm_entry->getOne($id, true); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $entry ) { |
| 140 |
$form_id = $entry->form_id; |
| 141 |
$id = $entry->id; |
| 142 |
} |
| 143 |
|
| 144 |
if ( !$fields || !is_array($fields) ) { |
| 145 |
global $frm_field; |
| 146 |
$fields = $frm_field->getAll(array('fi.form_id' => $form_id), 'field_order'); |
| 147 |
} |
| 148 |
|
| 149 |
$content = ( $format != 'text' ) ? array() : ''; |
| 150 |
$odd = true; |
| 151 |
|
| 152 |
if ( !$plain_text ) { |
| 153 |
global $frmpro_settings; |
| 154 |
|
| 155 |
$default_settings = array( |
| 156 |
'border_color' => 'dddddd', |
| 157 |
'bg_color' => 'f7f7f7', |
| 158 |
'text_color' => '444444', |
| 159 |
'font_size' => '12px', |
| 160 |
'border_width' => '1px', |
| 161 |
'alt_bg_color' => 'ffffff', |
| 162 |
); |
| 163 |
|
| 164 |
// merge defaults, global settings, and shortcode options |
| 165 |
foreach ( $default_settings as $key => $setting ) { |
| 166 |
if ( $atts[$key] != '' ) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
if ( $frmpro_settings ) { |
| 171 |
if ( 'alt_bg_color' == $key ) { |
| 172 |
$atts[$key] = $frmpro_settings->bg_color_active; |
| 173 |
} else if ( 'border_width' == $key ) { |
| 174 |
$atts[$key] = $frmpro_settings->field_border_width; |
| 175 |
} else { |
| 176 |
$atts[$key] = $frmpro_settings->{$key}; |
| 177 |
} |
| 178 |
} else { |
| 179 |
$atts[$key] = $setting; |
| 180 |
} |
| 181 |
unset($key, $setting); |
| 182 |
} |
| 183 |
|
| 184 |
unset($default_settings); |
| 185 |
|
| 186 |
$content .= "<table cellspacing='0' style='font-size:{$atts['font_size']};line-height:135%; border-bottom:{$atts['border_width']} solid #{$atts['border_color']};'><tbody>\r\n"; |
| 187 |
$bg_color = " style='background-color:#{$atts['bg_color']};'"; |
| 188 |
$bg_color_alt = " style='background-color:#{$atts['alt_bg_color']};'"; |
| 189 |
$row_style = "style='text-align:". ( $direction == 'rtl' ? 'right' : 'left' ) .";color:#{$atts['text_color']};padding:7px 9px;border-top:{$atts['border_width']} solid #{$atts['border_color']}'"; |
| 190 |
} |
| 191 |
|
| 192 |
foreach ( $fields as $f ) { |
| 193 |
if ( in_array($f->type, array('divider', 'captcha', 'break', 'html')) ) |
| 194 |
continue; |
| 195 |
|
| 196 |
if ( $entry && !isset($entry->metas[$f->id]) ) { |
| 197 |
if ( $entry->post_id && ( $f->type == 'tag' || (isset($f->field_options['post_field']) && $f->field_options['post_field'])) ) { |
| 198 |
$p_val = FrmProEntryMetaHelper::get_post_value($entry->post_id, $f->field_options['post_field'], $f->field_options['custom_field'], array( |
| 199 |
'truncate' => (($f->field_options['post_field'] == 'post_category') ? true : false), |
| 200 |
'form_id' => $entry->form_id, 'field' => $f, 'type' => $f->type, |
| 201 |
'exclude_cat' => (isset($f->field_options['exclude_cat']) ? $f->field_options['exclude_cat'] : 0) |
| 202 |
)); |
| 203 |
if ( $p_val != '' ) { |
| 204 |
$entry->metas[$f->id] = $p_val; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if ( !isset($entry->metas[$f->id]) && !$include_blank && !$default_email ) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
$entry->metas[$f->id] = $default_email ? '['. $f->id .']' : ''; |
| 213 |
} |
| 214 |
|
| 215 |
$val = ''; |
| 216 |
if ( $entry ) { |
| 217 |
$prev_val = maybe_unserialize($entry->metas[$f->id]); |
| 218 |
$meta = array('item_id' => $id, 'field_id' => $f->id, 'meta_value' => $prev_val, 'field_type' => $f->type); |
| 219 |
|
| 220 |
$val = $default_email ? $prev_val : apply_filters('frm_email_value', $prev_val, (object) $meta, $entry); |
| 221 |
} else if ( $default_email ) { |
| 222 |
$val = '['. $f->id .']'; |
| 223 |
} |
| 224 |
|
| 225 |
if ( $f->type == 'textarea' and !$plain_text ) { |
| 226 |
$val = str_replace(array("\r\n", "\r", "\n"), ' <br/>', $val); |
| 227 |
} |
| 228 |
|
| 229 |
//Remove signature from default-message shortcode |
| 230 |
if ( $f->type == 'signature' && !$default_email ) { |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
if ( is_array($val) && $format == 'text' ) { |
| 235 |
$val = implode(', ', $val); |
| 236 |
} |
| 237 |
|
| 238 |
$fname = $default_email ? '['. $f->id .' show=field_label]' : $f->name; |
| 239 |
|
| 240 |
if ( $format != 'text' ){ |
| 241 |
$content[$f->field_key] = $val; |
| 242 |
} else if ( $plain_text ) { |
| 243 |
if ( 'rtl' == $direction ) { |
| 244 |
$content .= $val . ' :' . $fname . "\r\n\r\n"; |
| 245 |
} else { |
| 246 |
$content .= $fname . ': ' . $val . "\r\n\r\n"; |
| 247 |
} |
| 248 |
} else { |
| 249 |
if (!$default_email){ |
| 250 |
$content .= '<tr'. ( $odd ? $bg_color : $bg_color_alt ) .'>'; |
| 251 |
if ( 'rtl' == $direction ) { |
| 252 |
$content .= "<td $row_style>$val</td><th $row_style>" . $fname ."</th>"; |
| 253 |
} else { |
| 254 |
$content .= "<th $row_style>" . $fname ."</th><td $row_style>$val</td>"; |
| 255 |
} |
| 256 |
$content .= '</tr>'. "\r\n"; |
| 257 |
$odd = ($odd) ? false : true; |
| 258 |
}else{ |
| 259 |
$content .= '[if '. $f->id .']<tr style="[frm-alt-color]">'; |
| 260 |
if ( 'rtl' == $direction ) { |
| 261 |
$content .= "<td $row_style>$val</td><th $row_style>" . $fname ."</th>"; |
| 262 |
} else { |
| 263 |
$content .= "<th $row_style>" . $fname ."</th><td $row_style>$val</td>"; |
| 264 |
} |
| 265 |
$content .= "</tr>\r\n[/if $f->id]"; |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
unset($fname, $f); |
| 271 |
} |
| 272 |
|
| 273 |
if ( $user_info ) { |
| 274 |
if ( isset($entry->description) ) { |
| 275 |
$data = maybe_unserialize($entry->description); |
| 276 |
} else if ( $default_email ) { |
| 277 |
$entry->ip = '[ip]'; |
| 278 |
$data = array( |
| 279 |
'browser' => '[browser]', |
| 280 |
'referrer' => '[referrer]', |
| 281 |
); |
| 282 |
} |
| 283 |
if ( $format != 'text' ) { |
| 284 |
$content['ip'] = $entry->ip; |
| 285 |
$content['browser'] = $data['browser']; |
| 286 |
$content['referrer'] = $data['referrer']; |
| 287 |
} else if ( $plain_text ) { |
| 288 |
$content .= "\r\n\r\n" . __('User Information', 'formidable') ."\r\n"; |
| 289 |
if ( 'rtl' == $direction ) { |
| 290 |
$content .= $entry->ip . ' :'. __('IP Address', 'formidable') ."\r\n"; |
| 291 |
$content .= $data['browser'] .' :'. __('User-Agent (Browser/OS)', 'formidable') ."\r\n"; |
| 292 |
$content .= $data['referrer'] .' :'. __('Referrer', 'formidable') ."\r\n"; |
| 293 |
} else { |
| 294 |
$content .= __('IP Address', 'formidable') . ': '. $entry->ip ."\r\n"; |
| 295 |
$content .= __('User-Agent (Browser/OS)', 'formidable') . ': '. $data['browser']."\r\n"; |
| 296 |
$content .= __('Referrer', 'formidable') . ': '. $data['referrer']."\r\n"; |
| 297 |
} |
| 298 |
} else { |
| 299 |
$content .= '<tr'. ($odd ? $bg_color : $bg_color_alt) .'>'; |
| 300 |
if ( 'rtl' == $direction ) { |
| 301 |
$content .= "<td $row_style>". $entry->ip ."</td><th $row_style>". __('IP Address', 'formidable') . "</th>"; |
| 302 |
} else { |
| 303 |
$content .= "<th $row_style>". __('IP Address', 'formidable') . "</th><td $row_style>". $entry->ip ."</td>"; |
| 304 |
} |
| 305 |
$content .= '</tr>'. "\r\n"; |
| 306 |
$odd = ($odd) ? false : true; |
| 307 |
|
| 308 |
if ( isset($data['browser']) ) { |
| 309 |
$content .= '<tr'. ($odd ? $bg_color : $bg_color_alt) .'>'; |
| 310 |
if ( 'rtl' == $direction ) { |
| 311 |
$content .= "<td $row_style>". $data['browser']."</td><th $row_style>". __('User-Agent (Browser/OS)', 'formidable') . "</th>"; |
| 312 |
} else { |
| 313 |
$content .= "<th $row_style>". __('User-Agent (Browser/OS)', 'formidable') . "</th><td $row_style>". $data['browser']."</td>"; |
| 314 |
} |
| 315 |
$content .= '</tr>'. "\r\n"; |
| 316 |
} |
| 317 |
$odd = ($odd) ? false : true; |
| 318 |
|
| 319 |
if ( isset($data['referrer']) ) { |
| 320 |
$content .= '<tr'. ($odd ? $bg_color : $bg_color_alt) .'>'; |
| 321 |
if ( 'rtl' == $direction ) { |
| 322 |
$content .= "<td $row_style>". str_replace("\r\n", '<br/>', $data['referrer']) ."</td><th $row_style>".__('Referrer', 'formidable') . "</th>"; |
| 323 |
} else { |
| 324 |
$content .= "<th $row_style>".__('Referrer', 'formidable') . "</th><td $row_style>". str_replace("\r\n", '<br/>', $data['referrer']) ."</td>"; |
| 325 |
} |
| 326 |
$content .= '</tr>'. "\r\n"; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if ( ! $plain_text ) { |
| 332 |
$content .= '</tbody></table>'; |
| 333 |
} |
| 334 |
|
| 335 |
if ( $format == 'json' ) { |
| 336 |
$content = json_encode($content); |
| 337 |
} |
| 338 |
|
| 339 |
return $content; |
| 340 |
} |
| 341 |
|
| 342 |
public static function &filter_email_value($value, $meta, $entry, $atts=array()){ |
| 343 |
$frm_field = new FrmField(); |
| 344 |
$field = $frm_field->getOne($meta->field_id); |
| 345 |
if(!$field) |
| 346 |
return $value; |
| 347 |
|
| 348 |
$value = self::filter_display_value($value, $field, $atts); |
| 349 |
return $value; |
| 350 |
} |
| 351 |
|
| 352 |
public static function &filter_display_value($value, $field, $atts=array()){ |
| 353 |
$field->field_options = maybe_unserialize($field->field_options); |
| 354 |
|
| 355 |
$saved_value = (isset($atts['saved_value']) and $atts['saved_value']) ? true : false; |
| 356 |
if(!in_array($field->type, array('radio', 'checkbox', 'radio', 'select')) or !isset($field->field_options['separate_value']) or !$field->field_options['separate_value'] or $saved_value) |
| 357 |
return $value; |
| 358 |
|
| 359 |
$field->options = maybe_unserialize($field->options); |
| 360 |
$f_values = array(); |
| 361 |
$f_labels = array(); |
| 362 |
foreach($field->options as $opt_key => $opt){ |
| 363 |
if(!is_array($opt)) |
| 364 |
continue; |
| 365 |
|
| 366 |
$f_labels[$opt_key] = isset($opt['label']) ? $opt['label'] : reset($opt); |
| 367 |
$f_values[$opt_key] = isset($opt['value']) ? $opt['value'] : $f_labels[$opt_key]; |
| 368 |
if($f_labels[$opt_key] == $f_values[$opt_key]){ |
| 369 |
unset($f_values[$opt_key]); |
| 370 |
unset($f_labels[$opt_key]); |
| 371 |
} |
| 372 |
unset($opt_key); |
| 373 |
unset($opt); |
| 374 |
} |
| 375 |
|
| 376 |
if(!empty($f_values)){ |
| 377 |
foreach((array)$value as $v_key => $val){ |
| 378 |
if(in_array($val, $f_values)){ |
| 379 |
$opt = array_search($val, $f_values); |
| 380 |
if(is_array($value)) |
| 381 |
$value[$v_key] = $f_labels[$opt]; |
| 382 |
else |
| 383 |
$value = $f_labels[$opt]; |
| 384 |
} |
| 385 |
unset($v_key); |
| 386 |
unset($val); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return $value; |
| 391 |
} |
| 392 |
|
| 393 |
public static function get_params($form=null){ |
| 394 |
global $frm_vars; |
| 395 |
|
| 396 |
$frm_form = new FrmForm(); |
| 397 |
if(!$form) |
| 398 |
$form = $frm_form->getAll(array(), 'name', 1); |
| 399 |
else if(!is_object($form)) |
| 400 |
$form = $frm_form->getOne($form); |
| 401 |
|
| 402 |
if(isset($frm_vars['form_params']) && is_array($frm_vars['form_params']) && isset($frm_vars['form_params'][$form->id])) |
| 403 |
return $frm_vars['form_params'][$form->id]; |
| 404 |
|
| 405 |
$action_var = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action'; |
| 406 |
$action = apply_filters('frm_show_new_entry_page', FrmAppHelper::get_param($action_var, 'new'), $form); |
| 407 |
|
| 408 |
$default_values = array( |
| 409 |
'id' => '', 'form_name' => '', 'paged' => 1, 'form' => $form->id, 'form_id' => $form->id, |
| 410 |
'field_id' => '', 'search' => '', 'sort' => '', 'sdir' => '', 'action' => $action |
| 411 |
); |
| 412 |
|
| 413 |
$values['posted_form_id'] = FrmAppHelper::get_param('form_id'); |
| 414 |
if (!is_numeric($values['posted_form_id'])) |
| 415 |
$values['posted_form_id'] = FrmAppHelper::get_param('form'); |
| 416 |
|
| 417 |
if ($form->id == $values['posted_form_id']){ //if there are two forms on the same page, make sure not to submit both |
| 418 |
foreach ($default_values as $var => $default){ |
| 419 |
if($var == 'action') |
| 420 |
$values[$var] = FrmAppHelper::get_param($action_var, $default); |
| 421 |
else |
| 422 |
$values[$var] = FrmAppHelper::get_param($var, $default); |
| 423 |
unset($var); |
| 424 |
unset($default); |
| 425 |
} |
| 426 |
}else{ |
| 427 |
foreach ($default_values as $var => $default){ |
| 428 |
$values[$var] = $default; |
| 429 |
unset($var); |
| 430 |
unset($default); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
if(in_array($values['action'], array('create', 'update')) and (!isset($_POST) or (!isset($_POST['action']) and !isset($_POST['frm_action'])))) |
| 435 |
$values['action'] = 'new'; |
| 436 |
|
| 437 |
return $values; |
| 438 |
} |
| 439 |
|
| 440 |
} |
| 441 |
|