Adding old applications to the employment db
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 12 Feb 2016 14:16:13 +0000 (09:16 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 12 Feb 2016 14:16:13 +0000 (09:16 -0500)
Import feature added

controllers/admin.php
models/database.php
models/list-applications.php
views/admin/import-form.php [new file with mode: 0644]

index a33915b..4fe47c9 100644 (file)
@@ -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 '<pre>' . print_r( $_REQUEST, true ) . '</pre>';
+            if ( $id = filter_var( $_REQUEST['application'], FILTER_VALIDATE_INT ) ) {
+                echo '<pre>' . print_r( $this->get_application_by_id($id), true ) . '</pre>';
+            }
+            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 '<pre>' . print_r( $_POST, true ) . '</pre>';
+            echo '<pre>' . print_r( $_FILES, true ) . '</pre>';
+            if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) {
+                return new WP_Error('upload_error', $uploads['error']);
+            }
+            $importUploadFile = $uploads['basedir']  . '/applicationImportFile.csv';
+            echo '<pre>' . print_r( $uploads, true ) . '</pre>';
+            echo '<pre>' . print_r( $importUploadFile, true ) . '</pre>';
+            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 '<pre>' . print_r( $contents, true ) . '</pre>';
+                    foreach ( $contents as $row ) {
+                        $this->addApplicationRecord($row);
+                    }
+            }
+            break;
+        default:
+            $this->show_import_form();
+            break;
+        }
+    }
+
+    public function addApplicationRecord($data)
+    {
+        echo '<pre>' . print_r( $data, true ) . '</pre>';
+        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 '<pre>' . print_r( $import_data, true ) . '</pre>';
+        $return = $wpdb->insert(
+            $this->wpdb->prefix . GLM_EMP_APPLICATION_TABLE,
+            $import_data,
+            array(
+                '%d',
+                '%s',
+                '%s',
+                '%s',
+                '%s',
+                '%s',
+                '%s',
+                '%d'
+            )
+        );
+        if (!$return) {
+             echo '<p style="color:red;">failed to insert: ' . $wpdb->last_error . '</p>';
+        }
+        echo '<p>Record Insert: ' . ( ($return) ? '<span style="color:green;">Success</span>' : '<span style="color:red;">Failed!</span>' ) . '</p>';
+    }
+
+    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 '<pre>' . print_r( $line, true ) . '</pre>';
+            ++$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 *
index 985c1cd..8399cd8 100644 (file)
@@ -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)
index 0443a67..c976c32 100644 (file)
@@ -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(
+                    '<a href="%s" target="_blank">Print</a>',
+                    $pdf_file_name_url
+                );
+            } else {
+                $printAction = '';
+            }
             $actions = array(
-                'view'    => sprintf(
-                    '<a href="?post_type=%s&page=%s&action=%s&application=%s">View</a>',
-                    GLM_EMP_POST_TYPE,
-                    $_REQUEST['page'],
-                    'view',
-                    $item['ID']
-                ),
+                'print'    => $printAction,
                 'archive' => sprintf(
                     '<a href="?post_type=%s&page=%s&action=%s&application=%s">Archive</a>',
                     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 (file)
index 0000000..7cd846f
--- /dev/null
@@ -0,0 +1,27 @@
+<script type="text/javascript">
+    function checkFile() {
+        var fileElement = document.getElementById("glm_emp_import");
+        var fileExtension = "";
+        if (fileElement.value.lastIndexOf(".") > 0) {
+            fileExtension = fileElement.value.substring(fileElement.value.lastIndexOf(".") + 1, fileElement.value.length);
+        }
+        if (fileExtension.toLowerCase() == "csv") {
+            return true;
+        }
+        else {
+            alert("You must select a csv file for upload");
+            return false;
+        }
+    }
+</script>
+<form action="" method="post" enctype="multipart/form-data" onsubmit="return checkFile();">
+    <input type="hidden" name="action" value="upload">
+    <h2>File Importer</h2>
+    <div>
+        <label>File Upload: </label>
+        <input type="file" id="glm_emp_import" name="glm_emp_import" accept=".csv">
+    </div>
+    <div>
+        <input type="submit" value="Import File">
+    </div>
+</form>