--- /dev/null
+<?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
+ );
+
+ }
+
+}