*/ if ( !defined( 'WPINC' ) ) { die; } /** * Echoes page for selecting import */ function activities_import_page() { if ( !current_user_can( ACTIVITIES_ADMINISTER_ACTIVITIES ) ) { wp_die( esc_html__( 'Access Denied', 'activities' ) ); } if ( isset( $_GET['type'] ) ) { $type = sanitize_key( $_GET['type'] ); if ( $type === 'activities' || $type == 'members' ) { activities_import_page_selected( $type ); return; } } echo '

' . esc_html__( 'Select Importer', 'activities' ) . '

'; echo Activities_Admin::get_messages(); $current_url = ( isset( $_SERVER['HTTPS'] ) ? 'https' : 'http' ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . esc_html__( 'Activities', 'activities' ) . '
'; echo '' . esc_html__( 'Run Importer', 'activities' ) . '
' . esc_html__( 'Import activities from a CSV file.', 'activities' ) . '
' . esc_html__( 'Participants', 'activities' ) . '
'; echo '' . esc_html__( 'Run Importer', 'activities' ) . '
' . esc_html__( 'Import activities and participants from a CSV file.', 'activities' ) . '
'; } /** * Builds page for uploading import file, mapping data and importing * * @param string $type Type of import selected * @return string Page for doing import */ function activities_import_page_selected( $type = 'activities' ) { if ( $type == 'activities') { echo '

' . esc_html__( 'Activities Import', 'activities' ) . '

'; } elseif ( $type == 'members' ) { echo '

' . esc_html__( 'Participants Import', 'activities' ) . '

'; } else { echo '

' . esc_html__( 'No Importer Selected', 'activities' ) . '

'; } if ( ( isset( $_POST['map_activity_data'] ) || isset( $_POST['map_member_data'] )) && isset( $_FILES['import_file'] ) ) { if ( $_FILES['import_file']['error'] != UPLOAD_ERR_OK ) { Activities_Admin::add_error_message( activities_file_upload_error_message( $_FILES['import_file']['error'] )); } else { if ( validate_file( $_FILES['import_file']['name'] ) === 0 ) { $filename = sanitize_file_name( $_FILES['import_file']['name'] ); move_uploaded_file( $_FILES['import_file']['tmp_name'], acts_get_file_path( $filename ) ); if ( isset( $_POST['map_activity_data'] ) ) { if ( activities_import_activity_mapping( $filename ) ) { return; } } elseif ( isset( $_POST['map_member_data'] ) ) { if ( activities_import_member_mapping( $filename ) ) { return; } } } else { Activities_Admin::add_error_message( esc_html__( 'Could not validate file.', 'activities' ) ); } } } else if ( ( isset( $_POST['import_activity_data'] ) || isset( $_POST['import_member_data'] ) ) && isset( $_POST['map'] ) && isset( $_POST['filename'] ) ) { $selected_columns = array(); $filename = sanitize_file_name( $_POST['filename'] ); if ( validate_file( $filename ) !== 0 || !is_array( $_POST['map'] ) ) { Activities_Admin::add_error_message( __( 'An error occured.', 'activities') ); } else { $mapping = $_POST['map']; foreach ($_POST['map'] as $column => $header ) { if ( isset( $selected_columns[$column] ) ) { Activities_Admin::add_error_message( esc_html__( 'Two headers cannot be mapped to the same column.', 'activities' ) ); if ( isset( $_POST['import_activity_data'] ) ) { activities_import_activity_mapping( $filename, $_POST['map'] ); } elseif ( isset( $_POST['import_member_data'] ) ) { activities_import_member_mapping( $filename, $_POST['map'] ); } return; } if ( $header === 'null' ) { unset( $mapping[$column] ); } else { $selected_columns[$column] = 1; } } if ( isset( $_POST['import_activity_data'] ) ) { activities_import_acts( $filename, $mapping, isset( $_POST['archive_activities'] ), isset( $_POST['update_activities'] ) ); } elseif ( isset( $_POST['import_member_data'] ) ) { activities_import_members( $filename, $mapping, isset( $_POST['create_activities'] ), isset( $_POST['archive_activities'] ) ); } return; } } switch ($type) { case 'activities': $text = sprintf( esc_html__( 'To import activities the file has to have one header called %s or %s.', 'activities' ), 'name', 'activity_name' ) . '
' . esc_html__( 'Other headers can be called whatever you want, you get to map them in the next step.', 'activities' ) . '
' . sprintf( esc_html__( 'The data is expected to be delimited by a semicolon %s. Order does not matter.', 'activities' ), '(;)' ) . '
' . esc_html__( 'You can archive imported activities by selecting the option for it during the next step.', 'activities' ) . '
' . esc_html__( 'Example CSV data', 'activities' ) . ':
' . "\nname; short; long; loction; responsible; start; end name of activity; short description; long description; location name; responsible@email.com; start date; end date name of another activity; short; ; another location; responsible2@email.com; ; "; $button = get_submit_button( esc_html__( 'Next', 'activities' ), 'button-primary', 'map_activity_data' ); break; case 'members': $text = esc_html__( 'To import participants and activities the file must have exactly two headers.', 'activities' ) . '
' . esc_html__( 'Headers can be called whatever you want, you get to map them in the next step.', 'activities' ) . '
' . sprintf( esc_html__( 'The data is expected to be delimited by a semicolon %s. Order does not matter.', 'activities' ), '(;)' ) . '
' . esc_html__( 'You can have multiple activities and participants in the two columns.', 'activities' ) . '
' . esc_html__( 'You can create and archive imported activities by selecting the option for it during the next step.', 'activities' ) . '
' . esc_html__( 'Example CSV data', 'activities' ) . ':
' . "\nusers; activities user@email.com; activity1, activity2, activity3 user2@email.com, user3@email.com; activity3, activity5 user3@email.com, user@email.com; activity2 "; $button = get_submit_button( esc_html__( 'Next', 'activities' ), 'button-primary', 'map_member_data' ); break; default; $text = esc_html__( 'No importer selected.', 'activities' ); $button = ''; break; } echo Activities_Admin::get_messages(); echo '
'; echo '

'; echo $text; echo '

'; echo '
'; echo ''; echo '

'; echo $button; echo '

'; echo '
'; } /** * Echoes page for activity import mapping * * @param string $filename Name of the import file * @param array $map Values for mapping fields */ function activities_import_activity_mapping( $filename, $map = array() ) { echo Activities_Admin::get_messages(); $importer = new Activities_CSV_Importer( acts_get_file_path( $filename ), ';' ); $headers = $importer->get_header(); $name_header = ''; foreach ($headers as $header) { $c_header = preg_replace( '/ ^ [\pZ\p{Cc}\x{feff}]+ | [\pZ\p{Cc}\x{feff}]+$ /ux', '', $header ); if ( $c_header == 'name' || $c_header == 'activity_name' ) { $name_header = $header; break; } } if ( $name_header != '' ) { echo '
'; echo '

' . esc_html__( 'Select fields mapping', 'activities' ) . '

'; echo ''; echo ''; echo ''; foreach (Activities_Activity::get_columns() as $col) { if ( $col === 'name' || $col == 'archive' ) { continue; } echo ''; echo ''; echo ''; } echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . stripslashes( wp_filter_nohtml_kses( $name_header ) ); echo '
' . esc_html__( 'Archived activities cannot be changed.', 'activities') . '
'; echo ''; echo '

'; echo get_submit_button( esc_html__( 'Import', 'activities' ), 'button-primary', 'import_activity_data', false ) . ' '; echo get_submit_button( esc_html__( 'Return', 'activities' ), 'button', 'return', false ); echo '

'; echo '
'; return true; } else { Activities_Admin::add_error_message( sprintf( esc_html__( 'No header called %s or %s found.', 'activities' ), 'name', 'activity_name' ) ); return false; } } /** * Echoes page for participants import mapping * * @param string $filename Name of the import file * @param array $map Values for mapping fields */ function activities_import_member_mapping( $filename, $map = array() ) { $importer = new Activities_CSV_Importer( acts_get_file_path( $filename ), ';' ); $headers = $importer->get_header(); if ( count( $headers ) != 2) { Activities_Admin::add_error_message( esc_html__( 'Expected exactly two headers.', 'activities' ) ); return false; } echo Activities_Admin::get_messages(); echo '
'; echo '

' . esc_html__( 'Select fields mapping', 'activities' ) . '

'; echo ''; echo ''; $options = array( 'users' => esc_html__( 'Users', 'activities' ), 'activities' => esc_html__( 'Activities', 'activities' ) ); foreach ($options as $col => $display) { echo ''; echo ''; echo ''; } echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . esc_html__( 'Archived activities cannot be changed.', 'activities') . '
'; echo ''; echo '

'; echo get_submit_button( esc_html__( 'Import', 'activities' ), 'button-primary', 'import_member_data', false ) . ' '; echo get_submit_button( esc_html__( 'Return', 'activities' ), 'button', 'return', false ); echo '

'; echo '
'; return true; } /** * Handles import for activities and echoes resutls. * Can update and/or archive activities. * Already archived activities wont be updated. * * @param string $filename Name of the import file * @param array $mapping Mapping of file headers to activity data * @param bool $archive True to archive activities, false to not * @param bool $update True to update activities, false to not. */ function activities_import_acts( $filename, $mapping, $archive, $update ) { $importer = new Activities_CSV_Importer( acts_get_file_path( $filename ), ';' ); $lines = $importer->get_rows( 10 ); $added = 0; $updated = 0; $line_count = 0; wp_ob_end_flush_all(); echo '

' . esc_html__( 'Importing activities', 'activities' ) . '

'; Activities_Admin_Utility::echo_scroll_script(); echo '
'; echo str_repeat( ' ', 1024*64); flush(); while ( count( $lines ) > 0 ) { foreach ($lines as $line) { echo ''; echo str_repeat( ' ', 1024*64 ); flush(); $line_count++; } $lines = $importer->get_rows( 10 ); } echo '
'; echo sprintf( esc_html__( 'Created %d activities!', 'activities' ), $added ) . '
'; if ( $update ) { echo sprintf( esc_html__( 'Updated %d activities!', 'activities' ), $updated ) . '
'; } echo get_submit_button( esc_html__( 'Return', 'activities' ), 'button', 'return' ); echo '
'; } /** * Handles import for activities and echoes resutls. * Can create and archive activities. * Will change participants for archived activities * * @param string $filename Name of the import file * @param array $mapping Mapping of file headers to user_activity data * @param bool $create True to create activities that does not exist, false to not * @param bool $archive True to archive activities, false to not */ function activities_import_members( $filename, $mapping, $create, $archive ) { $importer = new Activities_CSV_Importer( acts_get_file_path( $filename ), ';' ); $lines = $importer->get_rows( 10 ); $archived = 0; $line_count = 0; wp_ob_end_flush_all(); echo '

' . esc_html__( 'Importing participants', 'activities' ) . '

'; Activities_Admin_Utility::echo_scroll_script(); echo '
'; echo str_repeat( ' ', 1024*64); flush(); if ( $archive ) { $all_acts = array(); } while ( count( $lines ) > 0 ) { foreach ($lines as $line) { $added = 0; $created = 0; $acts = array(); $users = array(); foreach ($mapping as $column => $header) { $san_line = sanitize_text_field( $line[$header] ); switch ($column) { case 'activities': $acts = explode( ',', $san_line ); break; case 'users': $users = explode( ',', $san_line ); break; } } $a_ids = array(); foreach ($acts as $name) { $name = sanitize_text_field( $name ); $act = Activities_Activity::load_by_name( $name ); if ( $act ) { $a_ids[$act->ID] = $name; } elseif ( $create ) { Activities_Activity::insert( array( 'name' => $name ) ); $act = Activities_Activity::load_by_name( $name ); if ( $act ) { $a_ids[$act->ID] = $name; $created++; } } } $u_ids = array(); foreach ($users as $email) { $email = sanitize_email( $email ); if ( is_email( $email ) ) { $user = get_user_by( 'email', $email ); if ( $user ) { $u_ids[$user->ID] = $user->ID; } } } if ( count( $a_ids ) > 0 && count( $u_ids ) > 0 ) { foreach ($a_ids as $a_id => $a_name) { if ( $archive && !isset( $all_acts[$a_id] ) ) { $all_acts[$a_id] = $a_name; } foreach ($u_ids as $u_id) { if ( Activities_User_Activity::insert( $u_id, $a_id, true ) ) { $added++; } } } } $line_count++; echo ''; echo str_repeat( ' ', 1024*64 ); flush(); } $lines = $importer->get_rows( 10 ); } if ( $archive ) { foreach ($all_acts as $id => $name) { if ( Activities_Activity::archive( $id ) ) { $archived++; } } } echo '
'; if ( $archive ) { echo sprintf( esc_html__( 'Archived %d activities!', 'activities' ), $archived ) . '
'; } echo get_submit_button( esc_html__( 'Return', 'activities' ), 'button', 'return' ); echo '
'; } /** * Gets the upload filepath * * @param string $filename Name of the file * @return string Filepath */ function acts_get_file_path( $filename ) { return wp_upload_dir()['path'] . '/' . $filename; } /** * Gets file upload error message * * @param string $code Error code * @return string Error message */ function activities_file_upload_error_message($code){ switch ($code) { case UPLOAD_ERR_INI_SIZE: $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; break; case UPLOAD_ERR_FORM_SIZE: $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break; case UPLOAD_ERR_PARTIAL: $message = 'The uploaded file was only partially uploaded'; break; case UPLOAD_ERR_NO_FILE: $message = 'No file was uploaded'; break; case UPLOAD_ERR_NO_TMP_DIR: $message = 'Missing a temporary folder'; break; case UPLOAD_ERR_CANT_WRITE: $message = 'Failed to write file to disk'; break; case UPLOAD_ERR_EXTENSION: $message = 'File upload stopped by extension'; break; default: $message = 'Unknown upload error'; break; } return $message; }