Added more model functionality and cleaned up issues with default menu actions while...
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 4 Nov 2014 06:06:47 +0000 (01:06 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 4 Nov 2014 06:06:47 +0000 (01:06 -0500)
controllers/admin.php
css/index.css [new file with mode: 0644]
index.php
js/index.js [new file with mode: 0644]
lib/smartyTemplateSupport.php
models/admin/add.php
models/admin/display.php
models/admin/list.php
views/admin/prototype_add.html
views/admin/prototype_list.html
views/admin/prototype_submitted.html

index 2a110b9..379f121 100644 (file)
@@ -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 (file)
index 0000000..b523bb6
--- /dev/null
@@ -0,0 +1,6 @@
+.glm-proto-required {
+       color: red;
+}
+.glm-proto-error {
+       color: red;     
+}
\ No newline at end of file
index 548b641..a629497 100644 (file)
--- 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 (file)
index 0000000..5c614ef
--- /dev/null
@@ -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
index 1ee2e50..9ffdda2 100644 (file)
@@ -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();
                
                /*
index c79fb80..bc9b817 100644 (file)
 
 /*
  * 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
index 687e02c..9597c26 100644 (file)
@@ -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';
index 0afe0fc..b39e63b 100644 (file)
@@ -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 "<pre>".print_R($results, 1)."</pre>";
-               
-               $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 "<pre>".print_R($results, 1)."</pre>";
+                        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;
        }
        
 }
index 2335a63..7224591 100644 (file)
@@ -7,23 +7,35 @@
                <table class="form-table">
                <input type="hidden" name="glm_proto_action" value="add_submit">
                        <tr>
-                               <th scope="row"><label for="glm_proto_title">Title for this Prototype</label></th>
-                               <td><input name="glm_proto_title" type="text" id="glm_proto_title" value="" class="regular-text" /></td>
+                               <th scope="row"><label for="glm_proto_title">Title for this Prototype <span class="glm-proto-required">*</span></label></th>
+                               <td>
+                                       <input name="glm_proto_title" type="text" id="glm_proto_title" value="{$glm_proto_title}" class="regular-text" />
+                                       {if $glm_proto_title_error != ''}<br><span class="glm-proto-error">{$glm_proto_title_error}</span>{/if}
+                               </td>
                        </tr>
                        <tr>
                                <th scope="row"><label for="glm_proto_background">Prototype Background</label></th>
-                               <td><input name="glm_proto_background" type="file" id="glm_proto_background" value="" class="regular-text" /></td>
+                               <td>
+                                       <input id="glm-proto-background" class="glm-proto-media-uploader" type="text" size="36" name="glm_proto_background" value="{$glm_proto_background}" />
+                               <input class="glm-protp-upload-button button" data-id="glm-proto-background" type="button" value="Upload Image" />
+                                       {if $glm_proto_background_error != ''}<br><span class="glm-proto-error">{$glm_proto_background_error}</span>{/if}
+                               </td>
                        </tr>
                        <tr>
-                               <th scope="row"><label for="glm_proto_prototype">Prototype Image</label></th>
-                               <td><input name="glm_proto_prototype" type="file" id="glm_proto_prototype" value="" class="regular-text" /></td>
+                               <th scope="row"><label for="glm_proto_prototype">Prototype Image <span class="glm-proto-required">*</span></label></th>
+                               <td>
+                                       <input id="glm-proto-prototype" class="glm-proto-media-uploader" type="text" size="36" name="glm_proto_prototype" value="{$glm_proto_prototype}" />
+                               <input class="glm-protp-upload-button button" data-id="glm-proto-prototype" type="button" value="Upload Image" />
+                                       {if $glm_proto_prototype_error != ''}<br><span class="glm-proto-error">{$glm_proto_prototype_error}</span>{/if}
+                               </td>
                        </tr>
                        <tr>
                                <th>&nbsp;</th>
                                <td><input type="submit" name="Submit" value="Add this template" class="button-primary"></td>
                        </tr>
                </table>
-               
+                               
+               <p><span class="glm-proto-required">*</span> Required</p>
        </form>
        
 </div>
index 9877424..002ec3c 100644 (file)
@@ -1,5 +1,22 @@
 <div class="wrap">
        
        <h2>List of Prototypes</h2>
+       
+       <table class="widefat">
+               <thead>
+               <tr>
+                       <th>Date</th>
+                       <th style="width: 75%">Prototype Name</th>
+               </tr>
+               </thead>
+               <tbody>
+       {foreach $prototypes as $p}
+                       <tr>
+                       <td>{$p.p_date}</td>
+                       <td><a href="{$request_uri}&glm_proto_action=display&proto_id={$p.id}" value="add_submit"">{$p.post_title}</a></td>
+                       </tr>
+       {/foreach}
+               </tbody>
+       </table>
 
 </div>
index 7c6711c..26add73 100644 (file)
@@ -1,7 +1,16 @@
 <div class="wrap">
+
+  {if $glm_proto_title_error != ''}
+
+       <h2>Error Adding Prototype</h2>
+       <p><b class="glm-proto-error">{$glm_proto_title_error}</b>
+
+  {else}
        
        <h2>New Prototype Added</h2>
 
-       <p><b>Prototype Title:</b> {$prototype_title}</p>
+       <p><b>Prototype Title:</b> {$glm_proto_title}</p>
+  
+  {/if}
        
 </div>