function import()
 {
     global $frm_field;
     if (!current_user_can('frm_create_entries')) {
         wp_die($frm_settings->admin_permission);
     }
     $step = FrmAppHelper::get_param('step', 'One');
     $csv_del = FrmAppHelper::get_param('csv_del', ',');
     $form_id = FrmAppHelper::get_param('form_id');
     if ($step != 'One') {
         if ($step == 'Two') {
             //validate
             if (empty($_POST['form_id']) or empty($_POST['csv']) and (!isset($_FILES) or !isset($_FILES['csv']) or empty($_FILES['csv']['name']) or (int) $_FILES['csv']['size'] <= 0)) {
                 $errors = array(__('All Fields are required', 'formidable'));
                 $step = 'One';
             } else {
                 //upload
                 $media_id = ($_POST['csv'] and is_numeric($_POST['csv'])) ? $_POST['csv'] : FrmProAppHelper::upload_file('csv');
                 if ($media_id and !is_wp_error($media_id)) {
                     $current_path = get_attached_file($media_id);
                     $row = 1;
                     $headers = $example = '';
                     if (($f = fopen($current_path, "r")) !== FALSE) {
                         $row = 0;
                         while (($data = fgetcsv($f, 100000, $csv_del)) !== FALSE) {
                             $row++;
                             if ($row == 1) {
                                 $headers = $data;
                             } else {
                                 if ($row == 2) {
                                     $example = $data;
                                 } else {
                                     continue;
                                 }
                             }
                         }
                         fclose($f);
                     }
                     $fields = $frm_field->getAll("fi.type not in ('break','divider','captcha','html') and fi.form_id=" . (int) $form_id, 'fi.field_order');
                 } else {
                     if (is_wp_error($media_id)) {
                         echo $media_id->get_error_message();
                         $step = 'One';
                     }
                 }
             }
         } else {
             if ($step == 'import') {
                 global $frm_ajax_url;
                 //IMPORT NOW
                 $media_id = FrmAppHelper::get_param('csv');
                 $current_path = get_attached_file($media_id);
                 $row = FrmAppHelper::get_param('row');
                 $opts = get_option('frm_import_options');
                 $left = ($opts and isset($opts[$media_id])) ? (int) $row - (int) $opts[$media_id]['imported'] - 1 : $row - 1;
                 $mapping = FrmAppHelper::get_param('data_array');
                 $url_vars = "&csv_del=" . urlencode($csv_del) . "&form_id={$form_id}&csv={$media_id}&row={$row}";
                 foreach ($mapping as $mkey => $map) {
                     $url_vars .= "&data_array[{$mkey}]={$map}";
                 }
             }
         }
     }
     $next_step = $step == 'One' ? __('Step Two', 'formidable') : __('Import', 'formidable');
     if ($step == 'One') {
         $csvs = get_posts(array('post_type' => 'attachment', 'post_mime_type' => 'text/csv'));
     }
     include FRMPRO_VIEWS_PATH . '/frmpro-entries/import.php';
 }
示例#2
0
 function create($entry)
 {
     global $frm_entry, $frm_field, $frm_entry_meta, $wpdb, $frm_vars;
     if (!isset($_FILES) || !is_numeric($entry)) {
         return;
     }
     $entry = $frm_entry->getOne($entry);
     if (!$entry) {
         return;
     }
     $fields = $frm_field->getAll("fi.form_id='" . (int) $entry->form_id . "' and (fi.type='file' or fi.type='tag')");
     foreach ($fields as $field) {
         if ($field->type == 'file' && isset($_POST['item_meta'])) {
             // keep existing files attached to post
             $frm_vars['media_id'][$field->id] = $_POST['item_meta'][$field->id];
         }
         if (isset($_FILES['file' . $field->id]) && !empty($_FILES['file' . $field->id]['name']) && (int) $_FILES['file' . $field->id]['size'] > 0) {
             if (!isset($frm_vars['loading']) or !$frm_vars['loading']) {
                 $frm_vars['loading'] = true;
             }
             $media_ids = FrmProAppHelper::upload_file('file' . $field->id);
             $mids = array();
             foreach ((array) $media_ids as $media_id) {
                 if (is_numeric($media_id)) {
                     $mids[] = $media_id;
                 } else {
                     foreach ($media_id->errors as $error) {
                         if (!is_array($error[0])) {
                             echo $error[0];
                         }
                         unset($error);
                     }
                 }
                 unset($media_id);
             }
             if (!empty($mids)) {
                 $frm_entry_meta->delete_entry_meta($entry->id, $field->id);
                 //TODO: delete media?
                 if (isset($field->field_options['multiple']) and $field->field_options['multiple']) {
                     if (isset($_POST['item_meta'][$field->id])) {
                         $mids = array_merge((array) $_POST['item_meta'][$field->id], $mids);
                     }
                     $frm_entry_meta->add_entry_meta($entry->id, $field->id, null, $mids);
                 } else {
                     $mids = reset($mids);
                     $frm_entry_meta->add_entry_meta($entry->id, $field->id, null, $mids);
                     if (isset($_POST['item_meta'][$field->id]) and count($_POST['item_meta'][$field->id]) == 1 and $_POST['item_meta'][$field->id] != $mids) {
                         $frm_vars['detached_media'][] = $_POST['item_meta'][$field->id];
                     }
                 }
                 if (!isset($frm_vars['media_id'])) {
                     $frm_vars['media_id'] = array();
                 }
                 if (is_array($mids)) {
                     $mids = array_filter($mids);
                 }
                 $_POST['item_meta'][$field->id] = $frm_vars['media_id'][$field->id] = $mids;
                 if (isset($_POST['frm_wp_post']) and isset($field->field_options['post_field']) and $field->field_options['post_field']) {
                     $_POST['frm_wp_post_custom'][$field->id . '=' . $field->field_options['custom_field']] = $mids;
                 }
             }
         }
         if (!isset($_POST['item_meta']) || !isset($_POST['item_meta'][$field->id])) {
             continue;
         }
         if ($field->type == 'tag') {
             $tax_type = (isset($field->field_options['taxonomy']) and !empty($field->field_options['taxonomy'])) ? $field->field_options['taxonomy'] : 'frm_tag';
             $tags = explode(',', stripslashes($_POST['item_meta'][$field->id]));
             $terms = array();
             if (isset($_POST['frm_wp_post'])) {
                 $_POST['frm_wp_post'][$field->id . '=tags_input'] = $tags;
             }
             if ($tax_type == 'frm_tag') {
                 foreach ($tags as $tag) {
                     $slug = sanitize_title($tag);
                     if (!isset($_POST['frm_wp_post'])) {
                         if (!term_exists($slug, $tax_type)) {
                             wp_insert_term(trim($tag), $tax_type, array('slug' => $slug));
                         }
                     }
                     $terms[] = $slug;
                 }
                 wp_set_object_terms($entry->id, $terms, $tax_type);
                 unset($terms);
             }
         }
     }
 }
 public static function map_csv_fields()
 {
     $name = 'frm_import_file';
     if (!isset($_FILES) || !isset($_FILES[$name]) || empty($_FILES[$name]['name']) || (int) $_FILES[$name]['size'] < 1) {
         return;
     }
     $file = $_FILES[$name]['tmp_name'];
     // check if file was uploaded
     if (!is_uploaded_file($file)) {
         return;
     }
     if (empty($_POST['form_id'])) {
         $errors = array(__('All Fields are required', 'formidable'));
         FrmXMLController::form($errors);
         return;
     }
     //upload
     $media_id = isset($_POST[$name]) && !empty($_POST[$name]) && is_numeric($_POST[$name]) ? $_POST[$name] : FrmProAppHelper::upload_file($name);
     if ($media_id && !is_wp_error($media_id)) {
         $filename = get_attached_file($media_id);
     }
     if (empty($filename)) {
         $errors = array(__('That CSV was not uploaded. Are CSV files allowed on your site?', 'formidable'));
         FrmXMLController::form($errors);
         return;
     }
     $row = 1;
     $headers = $example = '';
     $csv_del = FrmAppHelper::get_param('csv_del', ',');
     $form_id = FrmAppHelper::get_param('form_id');
     setlocale(LC_ALL, get_locale());
     if (($f = fopen($filename, "r")) !== FALSE) {
         $row = 0;
         while (($data = fgetcsv($f, 100000, $csv_del)) !== FALSE) {
             //while (($raw_data = fgets($f, 100000))){
             $row++;
             if ($row == 1) {
                 $headers = $data;
             } else {
                 if ($row == 2) {
                     $example = $data;
                 } else {
                     continue;
                 }
             }
         }
         fclose($f);
     } else {
         $errors = array(__('CSV cannot be opened.', 'formidable'));
         FrmXMLController::form($errors);
         return;
     }
     $frm_field = new FrmField();
     $fields = $frm_field->getAll(array('fi.form_id' => (int) $form_id), 'field_order');
     include FrmAppHelper::plugin_path() . '/pro/classes/views/xml/map_csv_fields.php';
 }
示例#4
0
 /**
  * Get media ID(s) to be saved to database and set global media ID values
  *
  * @since 2.0
  * @param array $values (posted values), pass by reference
  * @param integer $field_id
  * @param integer $entry_id
  *
  */
 private static function prepare_file_upload_meta(&$values, $field, $entry_id)
 {
     // If there are no files to be uploaded, exit now
     if (!isset($_FILES)) {
         return;
     }
     // Assume this field is not repeating
     $repeating = $key_pointer = $parent_field = $file_name = false;
     // Get file name
     self::get_file_name($field->id, $file_name, $parent_field, $key_pointer, $repeating);
     // If there isn't a file uploaded in this field, exit now
     if (!isset($_FILES[$file_name]) || empty($_FILES[$file_name]['name']) || (int) $_FILES[$file_name]['size'] == 0) {
         return;
     }
     // Upload the file now
     $media_ids = FrmProAppHelper::upload_file($file_name);
     // Get filtered media IDs
     $mids = self::get_final_media_ids($media_ids, $field, $values);
     // If no media IDs to upload, end now
     if (empty($mids)) {
         return;
     }
     // Get global frm_vars variable
     global $frm_vars;
     // Set up progress bar to display on form submission
     if (!isset($frm_vars['loading']) || !$frm_vars['loading']) {
         $frm_vars['loading'] = true;
     }
     // Set up global media_id vars. This will be used for post fields.
     if (!isset($frm_vars['media_id'])) {
         $frm_vars['media_id'] = array();
     }
     // If not inside of a repeating section, set the media IDs for this field
     if (!$repeating) {
         $frm_vars['media_id'][$field->id] = $mids;
     }
     // Set new posted values
     self::set_file_posted_vals($field->id, $values[$field->id], array('repeating' => $repeating, 'parent_field' => $parent_field, 'key_pointer' => $key_pointer));
     // If this is a post field
     if (isset($_POST['frm_wp_post']) && FrmField::is_option_true($field, 'post_field')) {
         $_POST['frm_wp_post_custom'][$field->id . '=' . $field->field_options['custom_field']] = $mids;
     }
 }
 function create($entry)
 {
     global $frm_entry, $frm_field, $frm_entry_meta, $wpdb, $frm_loading, $frm_detached_media;
     if (!isset($_FILES) || !is_numeric($entry)) {
         return;
     }
     $entry = $frm_entry->getOne($entry);
     $fields = $frm_field->getAll("fi.form_id='" . (int) $entry->form_id . "' and (fi.type='file' or fi.type='tag')");
     foreach ($fields as $field) {
         $field->field_options = maybe_unserialize($field->field_options);
         if (isset($_FILES['file' . $field->id]) and !empty($_FILES['file' . $field->id]['name']) and (int) $_FILES['file' . $field->id]['size'] > 0) {
             if (!$frm_loading) {
                 $frm_loading = true;
             }
             $media_id = FrmProAppHelper::upload_file('file' . $field->id);
             if (is_numeric($media_id)) {
                 $frm_entry_meta->delete_entry_meta($entry->id, $field->id);
                 //TODO: delete media?
                 $frm_entry_meta->update_entry_meta($entry->id, $field->id, $field->field_key, $media_id);
                 if ($_POST['item_meta'][$field->id] != $media_id) {
                     $frm_detached_media[] = $_POST['item_meta'][$field->id];
                 }
                 $_POST['item_meta'][$field->id] = $media_id;
                 if (isset($_POST['frm_wp_post']) and isset($field->field_options['post_field']) and $field->field_options['post_field']) {
                     global $frm_media_id;
                     $frm_media_id[$field->id] = $media_id;
                     $_POST['frm_wp_post_custom'][$field->id . '=' . $field->field_options['custom_field']] = $media_id;
                 }
             } else {
                 foreach ($media_id->errors as $error) {
                     echo $error[0];
                 }
             }
         }
         if ($field->type == 'tag') {
             $tax_type = (isset($field->field_options['taxonomy']) and !empty($field->field_options['taxonomy'])) ? $field->field_options['taxonomy'] : 'frm_tag';
             $tags = explode(',', $_POST['item_meta'][$field->id]);
             $terms = array();
             if (isset($_POST['frm_wp_post'])) {
                 $_POST['frm_wp_post'][$field->id . '=tags_input'] = $tags;
             }
             if ($tax_type == 'frm_tag') {
                 foreach ($tags as $tag) {
                     $slug = sanitize_title(stripslashes($tag));
                     if (!isset($_POST['frm_wp_post'])) {
                         if (function_exists('term_exists')) {
                             $exists = term_exists($slug, $tax_type);
                         } else {
                             $exists = is_term($slug, $tax_type);
                         }
                         if (!$exists) {
                             wp_insert_term(trim($tag), $tax_type, array('slug' => $slug));
                         }
                     }
                     $terms[] = $slug;
                 }
                 wp_set_object_terms($entry->id, $terms, $tax_type);
                 unset($terms);
             }
         }
     }
     if ($frm_loading) {
         include_once FRMPRO_VIEWS_PATH . '/frmpro-entries/hide_loading.php';
     }
 }