From: Steve Sutton Date: Fri, 12 Feb 2016 14:16:13 +0000 (-0500) Subject: Adding old applications to the employment db X-Git-Tag: v2.1.0^2~1 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=2cbacff430c61685c6cea5f12acada312d52dbee;p=WP-Plugins%2Fglm-employment.git Adding old applications to the employment db Import feature added --- diff --git a/controllers/admin.php b/controllers/admin.php index a33915b..4fe47c9 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -37,14 +37,22 @@ class glm_employment_admin */ public function add_job_menus() { - //add_submenu_page( - //'edit.php?post_type=' . GLM_EMP_POST_TYPE, - //'applications', - //'Applications', - //'edit_posts', - //'applications', - //array($this, 'get_applications') - //); + add_submenu_page( + 'edit.php?post_type=' . GLM_EMP_POST_TYPE, + 'applications', + 'Applications', + 'edit_posts', + 'applications', + array($this, 'get_applications') + ); + add_submenu_page( + 'edit.php?post_type=' . GLM_EMP_POST_TYPE, + 'import', + 'Import', + 'edit_posts', + 'import', + array($this, 'import') + ); add_submenu_page( 'edit.php?post_type=' . GLM_EMP_POST_TYPE, 'settings', @@ -267,25 +275,174 @@ class glm_employment_admin case 'view': $this->view_application(); break; + case 'print': + echo '
' . print_r( $_REQUEST, true ) . '
'; + if ( $id = filter_var( $_REQUEST['application'], FILTER_VALIDATE_INT ) ) { + echo '
' . print_r( $this->get_application_by_id($id), true ) . '
'; + } + break; default: $this->show_applications(); break; } } + public function get_application_by_id($id) + { + global $wpdb; + return $wpdb->get_row("SELECT * FROM {$wpdb->prefix}" . GLM_EMP_APPLICATION_TABLE . " WHERE id = $id"); + } + + /** + * import + * + * Includes a form then takes the uploaded csv file and imports the applications + * Will try to get the position id correct by referencing the title + * + * How to export out the applications to csv file from psql + * + * COPY (SELECT a.id,t.title_name AS position,a.resume_file,a.fname,a.lname,a.mname,a.create_date,a.archived + * FROM application a + * LEFT OUTER JOIN employment e ON (e.employment_id = a.employment_id) + * LEFT OUTER JOIN title t ON (t.title_id = e.title_id)) to '/tmp/applications.csv' delimiter ',' CSV HEADER; + */ + public function import() + { + $action = (isset($_REQUEST['action'])) + ? filter_var($_REQUEST['action'], FILTER_SANITIZE_STRING) + : null; + switch ($action) { + case 'upload': + //$this->upload_file(); + echo '
' . print_r( $_POST, true ) . '
'; + echo '
' . print_r( $_FILES, true ) . '
'; + if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { + return new WP_Error('upload_error', $uploads['error']); + } + $importUploadFile = $uploads['basedir'] . '/applicationImportFile.csv'; + echo '
' . print_r( $uploads, true ) . '
'; + echo '
' . print_r( $importUploadFile, true ) . '
'; + if ( isset( $_FILES['glm_emp_import'] ) + && !$_FILES['glm_emp_import']['error'] + && $_FILES['glm_emp_import']['size'] > 0 + && move_uploaded_file( $_FILES['glm_emp_import']['tmp_name'], $importUploadFile ) ) { + $contents = $this->readCsvFile($importUploadFile); + //echo '
' . print_r( $contents, true ) . '
'; + foreach ( $contents as $row ) { + $this->addApplicationRecord($row); + } + } + break; + default: + $this->show_import_form(); + break; + } + } + + public function addApplicationRecord($data) + { + echo '
' . print_r( $data, true ) . '
'; + global $wpdb; + // if data has a position then check for a matching job record + if ( $data['position'] ) { + $sql = " + SELECT ID + FROM {$wpdb->prefix}posts + WHERE post_type = '" . GLM_EMP_POST_TYPE . "' + AND post_title = '" . $test . "'"; + $preSql = "SELECT ID + FROM {$wpdb->prefix}posts + WHERE post_type = %s + AND post_title = %s"; + $sql = $wpdb->prepare($preSql, GLM_EMP_POST_TYPE, $data['position']); + $position = $wpdb->get_var( $sql ); + } else { + $position = 0; + } + if ( $data['archived'] == 't' ) { + $archived = 1; + } else { + $archived = 0; + } + $import_data = array( + 'id' => $data['id'], + 'create_time' => $data['create_date'], + 'fname' => $data['fname'], + 'lname' => $data['lname'], + 'mname' => $data['mname'], + 'position' => $position, + 'resume' => $data['resume_file'], + 'archived' => $archived + ); + echo '
' . print_r( $import_data, true ) . '
'; + $return = $wpdb->insert( + $this->wpdb->prefix . GLM_EMP_APPLICATION_TABLE, + $import_data, + array( + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%d' + ) + ); + if (!$return) { + echo '

failed to insert: ' . $wpdb->last_error . '

'; + } + echo '

Record Insert: ' . ( ($return) ? 'Success' : 'Failed!' ) . '

'; + } + + public function readCsvFile($importUploadFile) + { + $fp = fopen($importUploadFile, 'r'); + if (!$fp) { + return false; + } + $data = array(); + $index = 0; + while ( ( $line = fgetcsv( $fp ) ) !== false ) { + if ( $index == 0 ) { + $headers = $line; + } else { + $data[] = array_combine( $headers, $line ); + } + //echo '
' . print_r( $line, true ) . '
'; + ++$index; + } + fclose($fp); + return $data; + } + + /** + * show_import_form + * + * Pulls in the form for the upload + * + * @access public + * @return void + */ + public function show_import_form() + { + $viewPath = $this->pluginDirName . 'views/admin/'; + include $viewPath . 'import-form.php'; + } + /** * View the application for one id */ public function view_application() { - wp_enqueue_script( - 'foundation', - get_template_directory_uri() . '/js/app.js', - 'jquery', - '1.0', - true - ); - wp_enqueue_style('foundation', get_template_directory_uri() . '/css/app.js'); + //wp_enqueue_script( + //'foundation', + //get_template_directory_uri() . '/js/app.js', + //'jquery', + //'1.0', + //true + //); + //wp_enqueue_style('foundation', get_template_directory_uri() . '/css/app.js'); $id = filter_var($_REQUEST['application'], FILTER_VALIDATE_INT); $sql = " SELECT * diff --git a/models/database.php b/models/database.php index 985c1cd..8399cd8 100644 --- a/models/database.php +++ b/models/database.php @@ -64,7 +64,7 @@ class glm_employment_models_database fname TEXT NOT NULL, lname TEXT NOT NULL, mname TEXT DEFAULT '' NOT NULL, - position BIGINT(20) DEFAULT 0 NOT NULL, + position BIGINT(20) DEFAULT 0, resume TEXT DEFAULT '' NOT NULL, archived BOOLEAN DEFAULT false NOT NULL, UNIQUE KEY id (id) diff --git a/models/list-applications.php b/models/list-applications.php index 0443a67..c976c32 100644 --- a/models/list-applications.php +++ b/models/list-applications.php @@ -34,10 +34,10 @@ class List_Applications extends GLM_List_Table } else { $where[] = "archived <> true"; } - $where[] = "(SELECT count(*) FROM " - . $this->wpdb->prefix . GLM_EMP_FORM_TABLE . " glm_ft WHERE glm_ft.form_part = 7 AND glm_ft.application = glm_at.id)"; + //$where[] = "(SELECT count(*) FROM " + //. $this->wpdb->prefix . GLM_EMP_FORM_TABLE . " glm_ft WHERE glm_ft.form_part = 7 AND glm_ft.application = glm_at.id)"; $sql = " - SELECT id as ID,fname,lname,create_time,archived,position,resume + SELECT id as ID,fname,lname,DATE(create_time) as create_time,archived,position,resume FROM " . $this->wpdb->prefix . GLM_EMP_APPLICATION_TABLE . " glm_at "; if (!empty($where)) { @@ -127,14 +127,18 @@ class List_Applications extends GLM_List_Table ), ); } else { + $pdf_file_name = GLM_EMP_UPLOAD_DIR . $item['create_time'] . '-' . $item['ID'] . '.pdf' ; + $pdf_file_name_url = GLM_EMP_UPLOAD_URL . $item['create_time'] . '-' . $item['ID'] . '.pdf' ; + if ( is_file($pdf_file_name ) ) { + $printAction = sprintf( + 'Print', + $pdf_file_name_url + ); + } else { + $printAction = ''; + } $actions = array( - 'view' => sprintf( - 'View', - GLM_EMP_POST_TYPE, - $_REQUEST['page'], - 'view', - $item['ID'] - ), + 'print' => $printAction, 'archive' => sprintf( 'Archive', GLM_EMP_POST_TYPE, @@ -173,6 +177,8 @@ class List_Applications extends GLM_List_Table GLM_EMP_UPLOAD_URL, $item['resume'] ); + } else if ( $item['resume'] ) { + return $item['resume'] . ' not found in ' . GLM_EMP_UPLOAD_DIR; } else { return ''; } @@ -373,7 +379,7 @@ class List_Applications extends GLM_List_Table /** * First, lets decide how many records per page to show */ - $per_page = 5; + $per_page = 20; /** diff --git a/views/admin/import-form.php b/views/admin/import-form.php new file mode 100644 index 0000000..7cd846f --- /dev/null +++ b/views/admin/import-form.php @@ -0,0 +1,27 @@ + +
+ +

File Importer

+
+ + +
+
+ +
+