From 9378aa37b598220f9718641aebe13c695638c0bc Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 4 Nov 2014 01:06:47 -0500 Subject: [PATCH] Added more model functionality and cleaned up issues with default menu actions while other output is requested. --- controllers/admin.php | 23 ++++- css/index.css | 6 ++ index.php | 16 ++++ js/index.js | 40 +++++++++ lib/smartyTemplateSupport.php | 2 +- models/admin/add.php | 125 +++++++++++++++++++++++---- models/admin/display.php | 29 +++++++ models/admin/list.php | 45 ++++++---- views/admin/prototype_add.html | 24 +++-- views/admin/prototype_list.html | 17 ++++ views/admin/prototype_submitted.html | 11 ++- 11 files changed, 295 insertions(+), 43 deletions(-) create mode 100644 css/index.css create mode 100644 js/index.js diff --git a/controllers/admin.php b/controllers/admin.php index 2a110b9..379f121 100644 --- a/controllers/admin.php +++ b/controllers/admin.php @@ -50,8 +50,25 @@ class glmProtoAdmin // Add hooks to WordPress add_action( 'admin_menu', array($this, 'configureMenus')); - // Determine current action and call the desired model - + /* + * Check for Requested Actions + */ + if (isset($_REQUEST['glm_proto_action'])) { + + switch ($_REQUEST['glm_proto_action']) { + + // Display a prototype + case 'display': + require_once(GLM_PROTO_PLUGIN_DIR.'/models/admin/display.php'); + return new glmProtoAdminDisplayModel($this->wpdb); + break; + + default: + break; + + } + } + } /** @@ -98,7 +115,7 @@ class glmProtoAdmin require_once(GLM_PROTO_PLUGIN_DIR.'/models/admin/add.php'); return new glmProtoAdminAddModel($this->wpdb); } - + } diff --git a/css/index.css b/css/index.css new file mode 100644 index 0000000..b523bb6 --- /dev/null +++ b/css/index.css @@ -0,0 +1,6 @@ +.glm-proto-required { + color: red; +} +.glm-proto-error { + color: red; +} \ No newline at end of file diff --git a/index.php b/index.php index 548b641..a629497 100644 --- a/index.php +++ b/index.php @@ -163,6 +163,22 @@ function glmProtoPluginUninstall() } register_uninstall_hook( __FILE__, 'glmProtoPluginUninstall' ); +/* + * Style Sheets + */ +wp_register_style('glmProtoIndexStyle', GLM_PROTO_PLUGIN_URL.'css/index.css'); +wp_enqueue_style( 'glmProtoIndexStyle' ); + +/* + * Scripts + */ +add_action('admin_enqueue_scripts', 'glmProtoScripts'); +function glmProtoScripts() { + wp_enqueue_media(); + wp_register_script('glm-proto-index-js', GLM_PROTO_PLUGIN_URL.'js/index.js', array('jquery')); + wp_enqueue_script('glm-proto-index-js'); +} + /* * Standard includes diff --git a/js/index.js b/js/index.js new file mode 100644 index 0000000..5c614ef --- /dev/null +++ b/js/index.js @@ -0,0 +1,40 @@ + +// Use media uploaded for all file/image uploads +jQuery(document).ready(function($){ + + var custom_uploader; + + $('.glm-protp-upload-button').click(function(e) { + + e.preventDefault(); + + var id = $(this).attr('data-id'); + + //If the uploader object has already been created, reopen the dialog + if (custom_uploader) { + custom_uploader.open(); + return; + } + + //Extend the wp.media object + var custom_uploader = wp.media.frames.file_frame = wp.media({ + title: 'Choose the desired image below or select "Upload Files" to select a new image.', + button: { + text: 'Choose Image' + }, + multiple: false + }); + + //When a file is selected, grab the URL and set it as the text field's value + custom_uploader.on('select', function() { + attachment = custom_uploader.state().get('selection').first().toJSON(); + $('#' + id).val(attachment.url); + }); + + //Open the uploader dialog + custom_uploader.open(); + + }); + + +}); \ No newline at end of file diff --git a/lib/smartyTemplateSupport.php b/lib/smartyTemplateSupport.php index 1ee2e50..9ffdda2 100644 --- a/lib/smartyTemplateSupport.php +++ b/lib/smartyTemplateSupport.php @@ -58,7 +58,7 @@ abstract class smartyTemplateSupport * Load and instatiate Smarty Templates */ require(GLM_PROTO_PLUGIN_DIR.'/lib/Smarty-3.1.21/libs/Smarty.class.php'); - + $this->template = new Smarty(); /* diff --git a/models/admin/add.php b/models/admin/add.php index c79fb80..bc9b817 100644 --- a/models/admin/add.php +++ b/models/admin/add.php @@ -15,6 +15,16 @@ /* * This class performs the work needed to add a new prototype. + * + * If a prototype is properly submitted, it is stored in the wp_posts table + * using the following fields. + * + * post_date Current date + * post_date_gmt Current date in GMT + * post_content Serialized array with 'title', 'background', and 'prototype' + * post_title Title of prototype + * post_type 'glm_proto' + * */ class glmProtoAdminAddModel extends smartyTemplateSupport { @@ -39,6 +49,11 @@ class glmProtoAdminAddModel extends smartyTemplateSupport public function __construct($wpdb) { + // If there's any other display operation requested, don't do this one. + if (isset($_REQUEST['glm_proto_action'])) { + return; + } + // Save WordPress Database object $this->wpdb = $wpdb; @@ -48,7 +63,9 @@ class glmProtoAdminAddModel extends smartyTemplateSupport $templateData = array( 'glm_proto_title' => '', 'glm_proto_title_error' => '', + 'glm_proto_background' => '', 'glm_proto_background_error' => '', + 'glm_proto_prototype' => '', 'glm_proto_prototype_error' => '' ); @@ -60,29 +77,75 @@ class glmProtoAdminAddModel extends smartyTemplateSupport if ($_REQUEST['glm_proto_action'] == 'add_submit') { // Clean up all input - $title = sanitize_text_field($_REQUEST['glm_proto_title']); - - // Check that we have all needed parameters - if ($title == '') { - $fieldError['glm_proto_title_error'] = 'Title not supplied'; + $templateData['glm_proto_title'] = sanitize_text_field($_REQUEST['glm_proto_title']); + $templateData['glm_proto_background'] = sanitize_text_field($_REQUEST['glm_proto_background']); + $templateData['glm_proto_prototype'] = sanitize_text_field($_REQUEST['glm_proto_prototype']); + + // Check title field + if ($templateData['glm_proto_title'] == '') { + $templateData['glm_proto_title_error'] = 'Required title not supplied'; $submitError = true; } - if ($_FILES['glm_proto_background']['name'] == '') { - $fieldError['glm_proto_background_error'] = 'Background image not supplied'; + + // Check background image - Not required but must exist if provided + if ($templateData['glm_proto_background'] != '' && + !$this->glmProtoIsUploaded($templateData['glm_proto_background']) ) + { + $templateData['glm_proto_background_error'] = 'Supplied background image does not exists'; $submitError = true; } - if ($_FILES['glm_proto_prototype']['name'] == '') { - $fieldError['glm_proto_prototype_error'] = 'Prototype image not supplied'; + + // Check prototype image + if ($templateData['glm_proto_prototype'] == '') { + $templateData['glm_proto_prototype_error'] = 'Required prototype image not supplied '; $submitError = true; + } else { + $exists = $this->glmProtoIsUploaded($templateData['glm_proto_prototype']); + if (!$exists) { + $templateData['glm_proto_prototype_error'] .= 'Supplied prototype image does not exists'; + $submitError = true; + } } - - if (!submitError) { + + if (!$submitError) { + + // Prepair data for storage + $date = date('Y-m-d', time()); + $timezoneBackup = date_default_timezone_get(); + date_default_timezone_set("GMT"); + $gmtDate = date('Y-m-d', time()); + date_default_timezone_set($timezoneBackup); + + $content = serialize( + array( + 'title' => $templateData['glm_proto_title'], + 'background' => $templateData['glm_proto_background'], + 'prototype' => $templateData['glm_proto_prototype'] + ) + ); + + // Store into wp_posts table + $result = $this->wpdb->insert( + 'wp_posts', + array( + 'post_date' => $date, + 'post_date_gmt' => $gmtDate, + 'post_content' => $content, + 'post_title' => $templateData['glm_proto_title'], + 'post_type' => 'glm_proto' + ) + ); + + // If there was a problem storing the prototype, pass that to the template + if (!$result) { + $templateData['glm_proto_title_error'] = 'There was an unknown problem storing this prototype.'; + } + // Select view $this->templateFile = 'admin/prototype_submitted.html'; // Add template parameters $this->templateAssign($templateData); - $this->templateAssign('prototype_title', $title); $prototypeSubmitted = true; } @@ -90,24 +153,54 @@ class glmProtoAdminAddModel extends smartyTemplateSupport } /* - * If no prototype was submitted, then just display the form. + * */ - if (!$prototypeSubmitted) { + if ($prototypeSubmitted) { + // No prototype was submitted so display the form. + } else { + // Select view $this->templateFile = 'admin/prototype_add.html'; // Add template parameters $this->templateAssign('request_uri', $_SERVER['REQUEST_URI']); - + $this->templateAssign($templateData); + } // Output our template results $this->template->display($this->templateFile); - } + /* + * Check if a file supposedly uploaded to WordPress exists + * + * This method accepts a URL to a WordPress file or image, strips the + * WordPress base file URL to get the remaining path and file name for + * the file, then adds the base path for WordPress files and checks to + * see if the file exists. + * + * @param string URL of uploaded WordPress file or image + * + * @return bool True if it exists, false if not + */ + private function glmProtoIsUploaded($url) { + + // Get current WordPress upload directory/url information + $paths = wp_upload_dir(); + $base = $paths['baseurl']; + $path = $paths['basedir']; + + // Strip base directory from supplied URL + $file = substr($url, strlen($base)); + + // Check if file exists + $exists = file_exists($path.$file); + return $exists; + + } } ?> \ No newline at end of file diff --git a/models/admin/display.php b/models/admin/display.php index 687e02c..9597c26 100644 --- a/models/admin/display.php +++ b/models/admin/display.php @@ -18,6 +18,7 @@ */ class glmProtoAdminDisplayModel extends smartyTemplateSupport { + /** * WordPress Database Object * @var $wpdb @@ -43,6 +44,34 @@ class glmProtoAdminDisplayModel extends smartyTemplateSupport // Start Smarty Templates - creates $this->template object $this->setupSmarty(); + + // Check if we recieved a prototype ID + $id = $_REQUEST['proto_id'] - 0; + if ($id > 0) { + + // Get the prototype data + $res = $this->wpdb->get_row(" + SELECT id, + DATE_FORMAT(post_date, '%Y-%m-%d') p_date, + post_title, + post_content + FROM wp_posts + WHERE post_type = 'glm_proto' + AND id = $id + "); +var_dump($res); + // If we have results + if (is_array($res) && count($res) > 0) { + + // Break out the prototype file data + $d = unserialize($res['post_content']); + + } + + + } + + // Select view $this->templateFile = 'admin/prototype_display.html'; diff --git a/models/admin/list.php b/models/admin/list.php index 0afe0fc..b39e63b 100644 --- a/models/admin/list.php +++ b/models/admin/list.php @@ -38,6 +38,12 @@ class glmProtoAdminListModel extends smartyTemplateSupport */ public function __construct($wpdb) { + + // If there's any other display operation requested, don't do this one. + if (isset($_REQUEST['glm_proto_action'])) { + return; + } + // Save WordPress Database object $this->wpdb = $wpdb; @@ -47,12 +53,18 @@ class glmProtoAdminListModel extends smartyTemplateSupport // Get a current list of prototypes $list = $this->getList(); + // Clean up + + // Add template parameters + $this->templateAssign('prototypes', $list); + $this->templateAssign('request_uri', $_SERVER['REQUEST_URI']); + // Select view $this->templateFile = 'admin/prototype_list.html'; // Output our template results $this->template->display($this->templateFile); - + } /* @@ -63,22 +75,23 @@ class glmProtoAdminListModel extends smartyTemplateSupport public function getList() { - $results = $this->wpdb->get_results( ' - SELECT * - FROM wp_postmeta - ', OBJECT ); - echo "
".print_R($results, 1)."
"; - - $results = $this->wpdb->get_results( ' - SELECT ID, - post_author, - post_date, - post_title, - post_type + // Get all prototypes + $res = $this->wpdb->get_results( " + SELECT id, + DATE_FORMAT(post_date, '%Y-%m-%d') p_date, + post_title FROM wp_posts - WHERE true - ', OBJECT ); - echo "
".print_R($results, 1)."
"; + WHERE post_type = 'glm_proto' + ORDER BY post_date, post_title + ", ARRAY_A + ); + + // If we have results + if (is_array($res) && count($res) > 0) { + return $res; + } + + return false; } } diff --git a/views/admin/prototype_add.html b/views/admin/prototype_add.html index 2335a63..7224591 100644 --- a/views/admin/prototype_add.html +++ b/views/admin/prototype_add.html @@ -7,23 +7,35 @@ - - + + - + - - + +
+ + {if $glm_proto_title_error != ''}
{$glm_proto_title_error}{/if} +
+ + + {if $glm_proto_background_error != ''}
{$glm_proto_background_error}{/if} +
+ + + {if $glm_proto_prototype_error != ''}
{$glm_proto_prototype_error}{/if} +
 
- + +

* Required

diff --git a/views/admin/prototype_list.html b/views/admin/prototype_list.html index 9877424..002ec3c 100644 --- a/views/admin/prototype_list.html +++ b/views/admin/prototype_list.html @@ -1,5 +1,22 @@

List of Prototypes

+ + + + + + + + + + {foreach $prototypes as $p} + + + + + {/foreach} + +
DatePrototype Name
{$p.p_date}{$p.post_title}
diff --git a/views/admin/prototype_submitted.html b/views/admin/prototype_submitted.html index 7c6711c..26add73 100644 --- a/views/admin/prototype_submitted.html +++ b/views/admin/prototype_submitted.html @@ -1,7 +1,16 @@
+ + {if $glm_proto_title_error != ''} + +

Error Adding Prototype

+

{$glm_proto_title_error} + + {else}

New Prototype Added

-

Prototype Title: {$prototype_title}

+

Prototype Title: {$glm_proto_title}

+ + {/if}
-- 2.17.1