From: Steve Sutton Date: Tue, 27 Dec 2016 14:29:09 +0000 (-0500) Subject: WIP: Start working on CSV Importer X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=b84970856c85cbc0c6217185532df7c15cfdc7ea;p=WP-Plugins%2Fglm-member-db.git WIP: Start working on CSV Importer This is for MCDA. --- diff --git a/models/admin/management/csvimport.php b/models/admin/management/csvimport.php new file mode 100644 index 00000000..967dcf0c --- /dev/null +++ b/models/admin/management/csvimport.php @@ -0,0 +1,158 @@ + + * @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 .= '
$_FILES: ' . print_r( $_FILES, true ) . '
'; + + 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 .= '
$file_data: ' . print_r( $file_data, true ) . '
'; + 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 + ); + + } + +} diff --git a/setup/validActions.php b/setup/validActions.php index c0ab1a35..0031a540 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -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 index 00000000..9938febb --- /dev/null +++ b/views/admin/management/csvimport.html @@ -0,0 +1,16 @@ +{include file='admin/management/header.html'} + +

CSV Importer

+ +{if $option == ''} +
+ + +
+{/if} + +{if $resultMessage} + {$resultMessage} +{/if} + +{include file='admin/footer.html'} diff --git a/views/admin/management/header.html b/views/admin/management/header.html index 99b0aec0..b3167815 100644 --- a/views/admin/management/header.html +++ b/views/admin/management/header.html @@ -11,6 +11,7 @@ Add-Ons Hooks Videos + CSV Importer {foreach $addOnTabs as $a} {$a.text} {/foreach}