WIP: Start working on CSV Importer
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 27 Dec 2016 14:29:09 +0000 (09:29 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 27 Dec 2016 14:29:09 +0000 (09:29 -0500)
This is for MCDA.

models/admin/management/csvimport.php [new file with mode: 0644]
setup/validActions.php
views/admin/management/csvimport.html [new file with mode: 0644]
views/admin/management/header.html

diff --git a/models/admin/management/csvimport.php b/models/admin/management/csvimport.php
new file mode 100644 (file)
index 0000000..967dcf0
--- /dev/null
@@ -0,0 +1,158 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * Admin Development
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+/*
+ * This class performs the work for the default action of the "Members" menu
+ * option, which is to display the members dashboard.
+ *
+ */
+class GlmMembersAdmin_management_csvimport
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /*
+     * Constructor
+     *
+     * This contractor sets up this model. At this time that only includes
+     * storing away the WordPress data object.
+     *
+     * @return object Class object
+     *
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+    }
+
+    public function addScheme($url, $scheme = 'http://')
+    {
+        return parse_url($url, PHP_URL_SCHEME) === null ?
+        $scheme . $url : $url;
+    }
+
+    /*
+     * Perform Model Action
+     *
+     * This method does the work for this model and returns any resulting data
+     *
+     * @return array Status and data array
+     *
+     * 'status'
+     *
+     * True if successful and false if there was a fatal failure.
+     *
+     * 'menuItemRedirect'
+     *
+     * If not false, provides a menu item the controller should
+     * execute after this one. Normally if this is used, there would also be a
+     * modelRedirect value supplied as well.
+     *
+     * 'modelRedirect'
+     *
+     * If not false, provides an action the controller should execute after
+     * this one.
+     *
+     * 'view'
+     *
+     * A suggested view name that the controller should use instead of the
+     * default view for this model or false to indicate that the default view
+     * should be used.
+     *
+     * 'data'
+     *
+     * Data that the model is returning for use in merging with the view to
+     * produce output.
+     *
+     */
+    public function modelAction ($actionData = false)
+    {
+
+        $resultMessage = '';
+        $success       = false;
+        $haveMembers   = false;
+        $import        = false;
+        $importNotice  = '';
+        $option        = '';
+
+        if (isset($_REQUEST['option']) && $_REQUEST['option'] != '') {
+            $option = $_REQUEST['option'];
+        }
+
+        switch($option) {
+        case 'upload_file':
+            // We're given a CSV file.
+            // We need to parse it out. File must have header line.
+            $file_data = $file_headers = $data = $members = array();
+            $resultMessage .= '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
+
+            if ( isset( $_FILES ) && isset( $_FILES['file_upload'] ) && ( $fh = fopen( $_FILES['file_upload']['tmp_name'], 'r' ) ) !== false ) {
+                $row = 0;
+                while( ( $data = fgetcsv( $fh, 1000, ',' ) ) !== false ) {
+                    if ( $row === 0 ) {
+                        // First row grab as headers.
+                        $file_headers = $data;
+                    } else {
+                        // All other rows are data.
+                        $file_data[] = array_combine( $file_headers, $data );
+                    }
+                    $row++;
+                }
+                fclose( $fh );
+            }
+            $resultMessage .= '<pre>$file_data: ' . print_r( $file_data, true ) . '</pre>';
+            break;
+
+        default:
+            break;
+        }
+
+        $templateData = array(
+            'success'       => $success,
+            'option'        => $option,
+            'resultMessage' => $resultMessage
+        );
+
+        // Return status, suggested view, and data to controller
+        return array(
+            'status'           => true,
+            'menuItemRedirect' => false,
+            'modelRedirect'    => false,
+            'view'             => 'admin/management/csvimport.html',
+            'data'             => $templateData
+        );
+
+    }
+
+}
index c0ab1a3..0031a54 100644 (file)
@@ -75,6 +75,7 @@ $glmMembersValidActions = array(
             'import'      => 'glm-member-db',
             'addons'      => 'glm-member-db',
             'hooks'       => 'glm-member-db',
+            'csvimport'   => 'glm-member-db',
         ),
         'shortcodes' => array(
             'index' => 'glm-member-db',
diff --git a/views/admin/management/csvimport.html b/views/admin/management/csvimport.html
new file mode 100644 (file)
index 0000000..9938feb
--- /dev/null
@@ -0,0 +1,16 @@
+{include file='admin/management/header.html'}
+
+<h2>CSV Importer</h2>
+
+{if $option == ''}
+<form action="{$thisUrl}?page={$thisPage}&glm_action=csvimport&option=upload_file" method="post" enctype="multipart/form-data">
+    <input type="file" name="file_upload">
+    <input type="submit" class="button">
+</form>
+{/if}
+
+{if $resultMessage}
+    {$resultMessage}
+{/if}
+
+{include file='admin/footer.html'}
index 99b0aec..b316781 100644 (file)
@@ -11,6 +11,7 @@
         <a href="{$thisUrl}?page={$thisPage}&glm_action=addons" class="nav-tab{if $thisAction==addons} nav-tab-active{/if}">Add-Ons</a>
         <a href="{$thisUrl}?page={$thisPage}&glm_action=hooks" class="nav-tab{if $thisAction==hooks} nav-tab-active{/if}">Hooks</a>
         <a href="{$thisUrl}?page={$thisPage}&glm_action=videos" class="nav-tab{if $thisAction==videos} nav-tab-active{/if}">Videos</a>
+        <a href="{$thisUrl}?page={$thisPage}&glm_action=csvimport" class="nav-tab{if $thisAction==csvimport} nav-tab-active{/if}">CSV Importer</a>
 {foreach $addOnTabs as $a}
         <a href="{$thisUrl}?page={$thisPage}&glm_action={$a.action}" class="nav-tab{if $thisAction==$a.action} nav-tab-active{/if}">{$a.text}</a>
 {/foreach}