From: Steve Sutton Date: Tue, 18 Sep 2018 20:40:14 +0000 (-0400) Subject: Updates for the admin. Import csv file. X-Git-Tag: v1.0.0^2~5 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=d304a064601ad3a6201cbe4259541b8df97c5375;p=WP-Plugins%2Fglm-member-db-staff.git Updates for the admin. Import csv file. Import the csv file. Importing will update based on the email address field. Must be a valid csv file. --- diff --git a/classes/data/dataStaff.php b/classes/data/dataStaff.php index 5f26ad6..524f2a0 100644 --- a/classes/data/dataStaff.php +++ b/classes/data/dataStaff.php @@ -149,7 +149,7 @@ class GlmDataStaff extends GlmDataAbstract 'extension' => array ( 'field' => 'extension', 'type' => 'text', - 'required' => true, + 'required' => false, 'use' => 'a' ), diff --git a/models/admin/staff/import.php b/models/admin/staff/import.php new file mode 100644 index 0000000..e8020a1 --- /dev/null +++ b/models/admin/staff/import.php @@ -0,0 +1,380 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +// Load Members data abstract +require_once GLM_MEMBERS_STAFF_PLUGIN_CLASS_PATH.'/data/dataStaff.php'; +require_once GLM_MEMBERS_STAFF_PLUGIN_CLASS_PATH.'/data/dataBuildings.php'; +require_once GLM_MEMBERS_STAFF_PLUGIN_CLASS_PATH.'/data/dataDepartments.php'; + +/* + * This class performs the work for the default action of the "Members" menu + * option, which is to display the members dashboard. + * + */ +class GlmMembersAdmin_staff_import extends GlmDataStaff +{ + + const CSV_CHARS_PER_LINE = 100000; + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /* + * Constructor + * + * This contructor 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; + + // Run constructor for members data class + parent::__construct(false, false); + + } + + /* + * 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 successfull 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 contoller 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) + { + $success = true; + $message = ''; + $view = 'import'; + $file_name = 'staff-import.csv'; + $imported = 0; + $updated = 0; + $import_good = false; + $processed = false; + $valid_headers = array( + 'firstname', + 'lastname', + 'email', + 'extension', + 'building', + 'department' + ); + + // Get any provided option + if ( isset( $_REQUEST['option'] ) ) { + $option = $_REQUEST['option']; + } + + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + + // Do selected option + switch ( $option ) { + + case 'upload': + // Set variable for the upload directory + $wpUploadDir = wp_get_upload_dir(); + + // Set the $upload_path for import files + $upload_path = $wpUploadDir['basedir'] . '/' . 'glm-member-staff'; + + // If the folder for the upload import files doesn't exists create one. + if ( !is_dir( $upload_path ) ) { + // Get old umask + $oldMask = umask(0); + // Set folder permission + mkdir( $upload_path, 0770 ); + // reset old umask + umask( $oldMask ); + } + + // echo '
$_FILES: ' . print_r( $_FILES, true ) . '
'; + + if ( isset( $_FILES['csv_file'] ) && !$_FILES['csv_file']['error'] ) { + move_uploaded_file( $_FILES['csv_file']['tmp_name'], $upload_path . '/'. $file_name ); + } + + $full_file_name = $upload_path . '/' . $file_name; + + if ( is_file( $upload_path . '/' . $file_name ) ) { + $processed = true; + $headers = $this->readCSVFileHeaders( $full_file_name ); + // echo '
$headers: ' . print_r( $headers, true ) . '
'; + if ( $valid_headers == $headers ) { + $file_data = $this->readCSVFile( $full_file_name ); + // echo '
$file_data: ' . print_r( $file_data, true ) . '
'; + if ( $file_data ) { + foreach ( $file_data as $row ) { + if ( $row['email'] ) { + // Get Department. + $department_id = $this->getDepartment( $row['department'] ); + $building_id = $this->getBuilding( $row['building'] ); + $staff_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . "staff + WHERE email = %s", + $row['email'] + ) + ); + if ( $staff_id ) { + $this->wpdb->update( + GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . 'staff', + array( + 'fname' => $row['firstname'], + 'lname' => $row['lastname'], + 'extension' => $row['extension'], + 'building' => $building_id, + 'department' => $department_id, + ), + array( 'email' => $row['email'] ), + array( + '%s', // fname + '%s', // lname + '%s', // extension + '%d', // building + '%d', // department + ), + array( '%s' ) + ); + $updated++; + } else { + $this->wpdb->insert( + GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . 'staff', + array( + 'fname' => $row['firstname'], + 'lname' => $row['lastname'], + 'email' => $row['email'], + 'extension' => $row['extension'], + 'building' => $building_id, + 'department' => $department_id, + ), + array( + '%s', // fname + '%s', // lname + '%s', // email + '%s', // extension + '%d', // building + '%d', // department + ) + ); + $imported++; + } + } + } + } + $import_good = true; + } else { + $import_good = false; + $message = 'File was not in correct format'; + } + } + + break; + + default: + break; + + } + + + // Compile template data + $templateData = array( + 'imported' => $imported, + 'updated' => $updated, + 'import_good' => $import_good, + 'processed' => $processed, + 'message' => $message, + ); + + // Return status, suggested view, and data to controller + return array( + 'status' => $success, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => 'admin/staff/' . $view . '.html', + 'data' => $templateData + ); + + } + + /** + * getBuilding + * + * Return the id for given building if found. + * Else add the new one and return the id. + * + * @param string $name + * + * @return int Id of building. + */ + public function getBuilding( $name ) + { + // If the building already exists then return the id. + $building_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . "buildings + WHERE name = %s", + $name + ) + ); + if ( $building_id ) { + return $building_id; + } + // Else insert it and return new id. + $this->wpdb->insert( + GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . 'buildings', + array( 'name' => $name ), + array( '%s' ) + ); + // Return insert id. + return $this->wpdb->insert_id; + } + + + /** + * getDepartment + * + * Return the id for given department if found. + * Else add the new one and return the id. + * + * @param string $name + * + * @return int Id of department. + */ + public function getDepartment( $name ) + { + // If the department already exists then return the id. + $department_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . "departments + WHERE name = %s", + $name + ) + ); + if ( $department_id ) { + return $department_id; + } + // Else insert it and return new id. + $this->wpdb->insert( + GLM_MEMBERS_STAFF_PLUGIN_DB_PREFIX . 'departments', + array( 'name' => $name ), + array( '%s' ) + ); + // Return insert id. + return $this->wpdb->insert_id; + } + + /** + * readCSVFile + * + * Read the entire csv file. First line is used for headers of the returned + * array. + * + * @param mixed $fileName + * @access public + * @return void + */ + public function readCSVFile( $fileName ) + { + $fileData = array(); + $fileHeaders = array(); + if ( ( $fp = fopen( $fileName, 'r' ) ) !== false ) { + // get first line to use as headers + $rowNumber = 0; + while ( ( $data = fgetcsv( $fp, SELF::CSV_CHARS_PER_LINE, ',', '"', '"' ) ) !== false ) { + if ( $rowNumber == 0 ) { + $fileHeaders = $data; + } else { + for ( $index = 0; $index < count( $data ); ++$index ) { + $fileData[$rowNumber][$fileHeaders[$index]] = $data[$index]; + } + } + $rowNumber++; + } + fclose( $fp ); + } + return $fileData; + } + + /** + * readCSVFileHeaders + * + * Read the cvs file. Just the first line is read. + * + * @param mixed $fileName Name of the file (path) + + * @access public + * @return void + */ + public function readCSVFileHeaders( $fileName ) + { + $fileHeaders = array(); + if ( ( $fp = fopen( $fileName, 'r' ) ) !== false ) { + // get first line to use as headers + $fileHeaders = fgetcsv( $fp, SELF::CSV_CHARS_PER_LINE, ',' ); + fclose( $fp ); + } + + return $fileHeaders; + } +} diff --git a/models/admin/staff/index.php b/models/admin/staff/index.php index 03507ac..b5af602 100644 --- a/models/admin/staff/index.php +++ b/models/admin/staff/index.php @@ -119,6 +119,10 @@ class GlmMembersAdmin_staff_index extends GlmDataStaff $alphaList = false; $alphaWhere = ''; $alphaSelected = false; + $numbDisplayed = false; + $lastDisplayed = false; + $departments = false; + $buildings = false; // Get any provided option if (isset($_REQUEST['option'])) { diff --git a/setup/validActions.php b/setup/validActions.php index 4efd318..af511f3 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -65,6 +65,7 @@ $glmMembersStaffAddOnValidActions = array( 'staff' => array( 'index' => GLM_MEMBERS_STAFF_PLUGIN_SLUG, 'settings' => GLM_MEMBERS_STAFF_PLUGIN_SLUG, + 'import' => GLM_MEMBERS_STAFF_PLUGIN_SLUG, ), ), 'frontActions' => array( diff --git a/views/admin/staff/header.html b/views/admin/staff/header.html index ff762b9..04bfde8 100644 --- a/views/admin/staff/header.html +++ b/views/admin/staff/header.html @@ -4,6 +4,7 @@ diff --git a/views/admin/staff/import.html b/views/admin/staff/import.html new file mode 100644 index 0000000..62a66fb --- /dev/null +++ b/views/admin/staff/import.html @@ -0,0 +1,31 @@ +{include file='admin/staff/header.html'} + +

Staff Importer

+ +
+ + + + + + + + +
+ +{if $processed} + {if $import_good} +
+ Import Success:
+ Updated: {$updated}
+ Added: {$imported}
+
+ {else} +
+ Import Failed:
+ {$message} +
+ {/if} +{/if} + +{include file='admin/staff/footer.html'} diff --git a/views/admin/staff/index.html b/views/admin/staff/index.html index 4e11c68..b6c59d5 100644 --- a/views/admin/staff/index.html +++ b/views/admin/staff/index.html @@ -5,10 +5,10 @@
- - - - + + + +
diff --git a/views/admin/staff/settings.html b/views/admin/staff/settings.html index 6593a78..217608b 100644 --- a/views/admin/staff/settings.html +++ b/views/admin/staff/settings.html @@ -45,7 +45,7 @@ {* Edit Add Building Dialog *} -
+
@@ -66,7 +66,7 @@ {* End Building Dialog *} {* Edit Add Department Dialog *} -
+
@@ -113,6 +113,7 @@ $('#building_id').val(building_id); $('#building_name').val(building_name); $('#building_option').val('updateBuilding'); + $('#building_dialog').dialog({ title: 'Edit Building'}); // Open dialog $('#building_dialog').dialog('open'); @@ -131,6 +132,7 @@ $('#building_id').val(''); $('#building_name').val(''); $('#building_option').val('addBuilding'); + $('#building_dialog').dialog({ title: 'Add Building'}); // Open dialog $('#building_dialog').dialog('open'); @@ -146,6 +148,7 @@ $('#department_id').val(department_id); $('#department_name').val(department_name); $('#department_option').val('updateDepartment'); + $('#department_dialog').dialog({ title: 'Edit Department'}); // Open dialog $('#department_dialog').dialog('open'); @@ -164,6 +167,7 @@ $('#department_id').val(''); $('#department_name').val(''); $('#department_option').val('addDepartment'); + $('#department_dialog').dialog({ title: 'Add Department'}); // Open dialog $('#department_dialog').dialog('open');