From 15445790c8f9603ca7da507b9861c4113ac0098d Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Thu, 1 Feb 2018 15:26:41 -0500 Subject: [PATCH] Almost done with File Library standard features. Still problem with Update. --- js/fileLibraryUpload/fileLibraryUpload.js | 23 +- models/admin/ajax/fileLibraryDelete.php | 103 ++++++++ models/admin/ajax/fileLibraryDownload.php | 97 ++++++++ models/admin/ajax/fileLibraryUpdate.php | 112 +++++++++ models/admin/ajax/fileLibraryUpload.php | 46 +--- models/admin/fileLibrary/index.php | 20 +- setup/validActions.php | 47 ++-- views/admin/fileLibrary/index.html | 288 +++++++++++++++++++--- 8 files changed, 614 insertions(+), 122 deletions(-) create mode 100644 models/admin/ajax/fileLibraryDelete.php create mode 100644 models/admin/ajax/fileLibraryDownload.php create mode 100644 models/admin/ajax/fileLibraryUpdate.php diff --git a/js/fileLibraryUpload/fileLibraryUpload.js b/js/fileLibraryUpload/fileLibraryUpload.js index bec4ed6d..1436e1bd 100644 --- a/js/fileLibraryUpload/fileLibraryUpload.js +++ b/js/fileLibraryUpload/fileLibraryUpload.js @@ -314,16 +314,18 @@ jQuery(document).ready(function($) { // Add files(s) to files display for ( var i = 0; i < fileData.files.length; i++ ) { // Using a copy of the supplied template, add file information - fData = $('#glm-fileLibraryDataTemplate').html(); + fData = '' + $('#glm-fileLibraryDataTemplate').html() + ''; fData = fData.replace(/{ id }/g, fileData.files[i].id); - fData = fData.replace(/\{ filename \}/g, fileData.files[i].newFileName); + fData = fData.replace(/\{ filename \}/g, fileData.files[i].name); fData = fData.replace(/\{ fileurl \}/g, fileData.files[i].newFileUrl); fData = fData.replace(/\{ newfilename \}/g, fileData.files[i].newFileName); - $('#glm-newFileContainer').prepend(fData); + $('#AfterNewFiles').before(fData); + +// $('#fileContainer_' + fileData.files[i].id).removeClass('glm-fileLibraryItemHidden'); $('.glm-NewFileHidden').removeClass('glm-fileLibraryItemHidden'); // Enable the fields that were just added - $("#" + fileData.files[i].id + " input, #" + fileData.files[i].id + " textarea").removeAttr('disabled'); +// $("#" + fileData.files[i].id + " input, #" + fileData.files[i].id + " textarea").removeAttr('disabled'); // Prepend file ID to position order input field $("#filePositionOrder").val(fileData.files[i].id + ',' + $("#filePositionOrder").val()); @@ -348,18 +350,5 @@ jQuery(document).ready(function($) { } - // Copy an element to the clipboard - function copyToClipboard(text) { - var $temp = $(""); - $("body").append($temp); - $temp.val(text).select(); - document.execCommand("copy"); - $temp.remove(); - } - - $('body').on('click', '.glm-file-library-copy', function(e) { - copyToClipboard($(this).attr('data-link')); - }); - }); diff --git a/models/admin/ajax/fileLibraryDelete.php b/models/admin/ajax/fileLibraryDelete.php new file mode 100644 index 00000000..8a7e3f6a --- /dev/null +++ b/models/admin/ajax/fileLibraryDelete.php @@ -0,0 +1,103 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/* + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_fileLibraryDelete +{ + + /** + * 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; + + } + + /* + * Perform Model Action + * + * This modelAction takes an AJAX image upload and stores the image in the + * media/images directory of the plugin. + * + * This model action does not return, it simply does it's work then calls die(); + * + * @param $_REQUEST['id'] File Library ID + */ + public function modelAction ($actionData = false) + { + + // Make sure the file ID is an integer + $fileId = filter_input(INPUT_GET, 'fileId', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE ); + if (!$fileId || $fileId < 0) { + die('Invalid File ID'); + } + + // Make sure there's a file + $file = $this->wpdb->get_col( "SELECT file_name FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."file_library WHERE id = $fileId" ); + if (!$file || !file_exists(GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file[0])) { + die('Not Found'); + } + + $filePath = GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file[0]; + $fileSize = filesize($filePath); + + // Delete the file and the file data + unlink($filePath); + + $this->wpdb->query( + $this->wpdb->prepare(" + DELETE FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."file_library + WHERE id = %d + ", + $fileId + ) + ); + + die('File Deleted'); + + } + +} diff --git a/models/admin/ajax/fileLibraryDownload.php b/models/admin/ajax/fileLibraryDownload.php new file mode 100644 index 00000000..c8f5e76b --- /dev/null +++ b/models/admin/ajax/fileLibraryDownload.php @@ -0,0 +1,97 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/* + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_fileLibraryDownload +{ + + /** + * 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; + + } + + /* + * Perform Model Action + * + * This modelAction takes an AJAX image upload and stores the image in the + * media/images directory of the plugin. + * + * This model action does not return, it simply does it's work then calls die(); + * + * @param $_REQUEST['id'] File Library ID + */ + public function modelAction ($actionData = false) + { + + // Make sure the file ID is an integer + $fileId = filter_input(INPUT_GET, 'fileId', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE ); + if (!$fileId || $fileId < 0) { + die(); + } + + // Get the current file name + $file = $this->wpdb->get_row( "SELECT name, file_name FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."file_library WHERE id = $fileId", ARRAY_A ); + if (!$file || !file_exists(GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file['file_name'])) { + die(); + } + + $filePath = GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file['file_name']; + $fileSize = filesize($filePath); + + // Send to browser + header('Content-Disposition: attachment; filename="'.$file['name'].'"'); + header("Content-Length: $fileSize"); + header("Content-Type: application/octet-stream;"); + readfile($filePath); + + die(); + + } + +} diff --git a/models/admin/ajax/fileLibraryUpdate.php b/models/admin/ajax/fileLibraryUpdate.php new file mode 100644 index 00000000..4b28ee73 --- /dev/null +++ b/models/admin/ajax/fileLibraryUpdate.php @@ -0,0 +1,112 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/* + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_fileLibraryUpdate +{ + + /** + * 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; + + } + + /* + * Perform Model Action + * + * This modelAction takes an AJAX image upload and stores the image in the + * media/images directory of the plugin. + * + * This model action does not return, it simply does it's work then calls die(); + * + * @param $_REQUEST['id'] File Library ID + */ + public function modelAction ($actionData = false) + { + + // Make sure the file ID is an integer + $fileId = filter_input(INPUT_POST, 'fileId', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE ); + if (!$fileId || $fileId < 0) { + die('Invalid File ID'); + } + + // Get input fields + $fileTitle = filter_input(INPUT_POST, 'title'); + $fileDescr = filter_input(INPUT_POST, 'descr'); + + // Make sure there's a file + $file = $this->wpdb->get_col( "SELECT file_name FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."file_library WHERE id = $fileId" ); + if (!$file || !file_exists(GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file[0])) { + die('Not Found'); + } + + $filePath = GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH.'/'.$file[0]; + $fileSize = filesize($filePath); + + // Delete the file and the file data + + $this->wpdb->query( + $this->wpdb->prepare(" + UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX."file_library + SET descr = %s, title = %s, last_access_time = %s + WHERE id = %d + ", + array ( + $fileDescr, + $fileTitle, + date('Y-m-d H:i:s'), + $fileId, + ) + ) + ); + + die('File updated'); + + } + +} diff --git a/models/admin/ajax/fileLibraryUpload.php b/models/admin/ajax/fileLibraryUpload.php index 0281dde9..52729773 100644 --- a/models/admin/ajax/fileLibraryUpload.php +++ b/models/admin/ajax/fileLibraryUpload.php @@ -83,17 +83,6 @@ class GlmMembersAdmin_ajax_fileLibraryUpload extends GlmDataFileLibrary 'message' => '' ); -/* - // Check for valid refType - See validActions in Admin Controller - if (!isset($_REQUEST['glm_refType']) || !isset($this->config['ref_type_table'][$_REQUEST['glm_refType']])) { - $return['message'] = 'Invalid target table does not exists!'; - echo json_encode($return); - die(); - } - $refType = $_REQUEST['glm_refType']; - $refTable = $this->config['ref_type_table'][$_REQUEST['glm_refType']]; -*/ - // Check for uploaded files if (!isset($_FILES) || count($_FILES) == 0) { $return['message'] = 'No image file provided!'; @@ -101,26 +90,6 @@ class GlmMembersAdmin_ajax_fileLibraryUpload extends GlmDataFileLibrary die(); } -/* - // Check for valid target record ID - if (!isset($_REQUEST['glm_refDest']) || ($_REQUEST['glm_refDest']-0) <= 0) { - $return['message'] = 'Invalid target ID!'; - echo json_encode($return); - die(); - } - $refDest = ($_REQUEST['glm_refDest']-0); - - - // Make sure the record actually exists - $sql = "SELECT id FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."$refTable WHERE id = $refDest;"; - $record = $this->wpdb->get_row($sql, ARRAY_A); - if (count($record['id']) != 1) { - $return['message'] = "Specified target record (".$record['id'].") does not exist in table (".$refTable.")!\n\n".$sql; - echo json_encode($return); - die(); - } -*/ - // For each submitted file (usually only one) foreach( $_FILES as $file ) { @@ -165,12 +134,12 @@ class GlmMembersAdmin_ajax_fileLibraryUpload extends GlmDataFileLibrary */ public function storeFile ($file) { - + // Check for fileLibrary media directory if (!file_exists(GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH)) { mkdir(GLM_MEMBERS_PLUGIN_FILE_LIBRARY_PATH); } - + // If $file is just a URL to an image, then simulate the file array from a form submission if (!is_array($file)) { @@ -217,6 +186,9 @@ class GlmMembersAdmin_ajax_fileLibraryUpload extends GlmDataFileLibrary $file['newFileName'] = $newFilename; $file['newFileUrl'] = GLM_MEMBERS_WORDPRESS_FILE_LIBRARY_URL.$newFilename; + + $lastAccessTime = date('Y-m-d H:i:s'); + // Store image name in images table $sql = " INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX ."file_library @@ -224,16 +196,18 @@ class GlmMembersAdmin_ajax_fileLibraryUpload extends GlmDataFileLibrary name, file_name, descr, - title + title, + last_access_time ) VALUES ( '".$file['name']."', '".$file['newFileName']."', '', - '' + '', + '$lastAccessTime' ); - "; + "; $this->wpdb->query($sql); $queryError = $this->wpdb->last_error; diff --git a/models/admin/fileLibrary/index.php b/models/admin/fileLibrary/index.php index c6f85db8..611b9da8 100644 --- a/models/admin/fileLibrary/index.php +++ b/models/admin/fileLibrary/index.php @@ -63,16 +63,16 @@ class GlmMembersAdmin_fileLibrary_index extends GlmDataFileLibrary // Save plugin configuration object $this->config = $config; - + // Run constructor for members data class parent::__construct(false, false); } public function modelAction($actionData = false) { - + $paging = false; - + // File Library Upload scripts and css wp_register_script( 'glm-members-admin-filelibrary-upload', @@ -90,33 +90,33 @@ class GlmMembersAdmin_fileLibrary_index extends GlmDataFileLibrary GLM_MEMBERS_PLUGIN_VERSION ); wp_enqueue_style('glm-members-admin-filelibrary-upload-css'); - + $haveFiles = false; - $files = $this->getList(); + $files = $this->getList(true, 'name'); if ($files && count($files) > 0) { $haveFiles = true; } -/* +/* // Update image gallery titles, descriptions, and image positions then return current image gallery $this->imageGallery = $Images->galleryImageDataUpdate($this->config['ref_type_numb']['MemberInfo'], $this->memberInfoID, 'galleryPositionOrder'); $this->haveImageGallery = ($this->imageGallery != false); - + // Update file captions, descriptions, and file positions then // return current files data. $this->files = $Files->filesDataUpdate( $this->config['ref_type_numb']['MemberInfo'], $this->memberInfoID, 'filePositionOrder' ); $this->haveFiles = ($this->files != false); */ - + // echo "
".print_r($files,1)."
"; - + // Compile template data $templateData = array( 'haveFiles' => $haveFiles, 'files' => $files, 'paging' => $paging ); - + // Return status, any suggested view, and any data to controller return array( 'status' => true, diff --git a/setup/validActions.php b/setup/validActions.php index dbd11575..28f84bc3 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -33,36 +33,39 @@ $glmMembersValidActions = array( 'adminActions' => array( 'ajax' => array( - 'imageUpload' => 'glm-member-db', - 'fileUpload' => 'glm-member-db', - 'fileLibraryUpload' => 'glm-member-db', - 'newOldMemberIdsCsv' => 'glm-member-db', - 'membersListExport' => 'glm-member-db', - 'memberClickThrough' => 'glm-member-db', - 'memberDetailClick' => 'glm-member-db', - 'memberGraphs' => 'glm-member-db', + 'imageUpload' => 'glm-member-db', + 'fileUpload' => 'glm-member-db', + 'newOldMemberIdsCsv' => 'glm-member-db', + 'membersListExport' => 'glm-member-db', + 'memberClickThrough' => 'glm-member-db', + 'memberDetailClick' => 'glm-member-db', + 'memberGraphs' => 'glm-member-db', + 'fileLibraryUpload' => 'glm-member-db', + 'fileLibraryDownload' => 'glm-member-db', + 'fileLibraryDelete' => 'glm-member-db', + 'fileLibraryUpdate' => 'glm-member-db' ), 'dashboard' => array( 'index' => 'glm-member-db', - 'members' => 'glm-member-db', + 'members' => 'glm-member-db' ), 'dashboardWidget' => array( - 'index' => 'glm-member-db', + 'index' => 'glm-member-db' ), 'fileLibrary' => array( - 'index' => 'glm-member-db', + 'index' => 'glm-member-db' ), 'members' => array( 'index' => 'glm-member-db', // member list 'list' => 'glm-member-db', 'reports' => 'glm-member-db', - 'other' => 'glm-member-db', + 'other' => 'glm-member-db' ), 'member' => array( 'index' => 'glm-member-db', // Member Dashboard 'memberInfo' => 'glm-member-db', 'memberEdit' => 'glm-member-db', - 'locations' => 'glm-member-db', + 'locations' => 'glm-member-db' ), 'settings' => array( 'index' => 'glm-member-db', // Member Types @@ -70,7 +73,7 @@ $glmMembersValidActions = array( 'cities' => 'glm-member-db', 'regions' => 'glm-member-db', 'counties' => 'glm-member-db', - 'amenities' => 'glm-member-db', + 'amenities' => 'glm-member-db' ), 'management' => array( 'index' => 'glm-member-db', // General Options @@ -80,21 +83,21 @@ $glmMembersValidActions = array( 'theme' => 'glm-member-db', 'import' => 'glm-member-db', 'addons' => 'glm-member-db', - 'hooks' => 'glm-member-db', + 'hooks' => 'glm-member-db' ), 'shortcodes' => array( - 'index' => 'glm-member-db', + 'index' => 'glm-member-db' ), 'error' => array( 'index' => 'glm-member-db', - 'badAction' => 'glm-member-db', + 'badAction' => 'glm-member-db' ), 'pages' => array( - 'shortcode' => 'glm-member-db', + 'shortcode' => 'glm-member-db' ), 'import' => array( - 'index' => 'glm-member-db', - ), + 'index' => 'glm-member-db' + ) ), 'frontActions' => array( 'members' => array( @@ -104,7 +107,7 @@ $glmMembersValidActions = array( ), 'error' => array( 'index' => 'glm-member-db', - 'badAction' => 'glm-member-db', - ), + 'badAction' => 'glm-member-db' + ) ), ); diff --git a/views/admin/fileLibrary/index.html b/views/admin/fileLibrary/index.html index 56a0c544..7268dfa0 100644 --- a/views/admin/fileLibrary/index.html +++ b/views/admin/fileLibrary/index.html @@ -1,5 +1,15 @@
+ + +

Files Library

@@ -49,25 +59,58 @@ - - - - - - - + + + + + + + + + + + +
-
Copy Link URL
- { newfilename } -
- { filename } - -   - -   - -
Edit
-
Delete
-
+ { filename } + + { newfilename } + +   + +   +
@@ -90,47 +133,76 @@ - + - - - - + - + + {if $haveFiles} {assign var="i" value="0"} {foreach $files as $f} - {if $i++ is odd by 1} - - {else} - - {/if} - + + + - + + + + + + {/foreach} {else} @@ -145,5 +217,147 @@ + + + + {include file='admin/footer.html'} -- 2.17.1
File Original NameStored File Name Title Last Access 
    New Uploads
 
    Previous Uploads
    Previous Uploads
-
Copy Link URL
- {$f.file_name} -
{$f.name} + {$f.file_name} + {$f.title} {$f.last_access_time.datetime} -
Edit
-
Delete
+
(no files listed)