Fixed problem with script name missing from base url in some situations.
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Jan 2015 16:46:20 +0000 (11:46 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Jan 2015 16:46:20 +0000 (11:46 -0500)
controllers/admin.php
controllers/front.php
css/front.css [new file with mode: 0644]
index.php
js/front.js [new file with mode: 0644]
lib/smartyTemplateSupport.php
misc/smarty/templates_c/50e78f1400d6db96c588c737669426ab57397d1d.file.footer.html.php
views/admin/footer.html

index 5683326..293a282 100644 (file)
@@ -136,7 +136,6 @@ class glmMembersAdmin extends GlmPluginSupport
      * @access public
      */
     public $wpdb;
-
     /**
      * Plugin Configuration Data
      *
@@ -169,8 +168,8 @@ class glmMembersAdmin extends GlmPluginSupport
 
         /*
          * Check if there's a request to bypass the WordPress Dashboard and
-         * display
-         * directly to the browser using a specified menuItem/action.
+         * display directly to the browser using a specified
+         * menuItem/action.
          *
          */
         if (isset($_REQUEST['glm_display_direct']) &&
@@ -361,6 +360,16 @@ class glmMembersAdmin extends GlmPluginSupport
         $errorMsg = '';
 
         if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
+
+            // Get all defiend constants then if not VERBOSE select out only user contstants
+            $consts = get_defined_constants(true);
+            if (!GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) {
+                $consts = $consts['user'];
+            } else {
+                $this->addNotice("<pre>".print_r($_SERVER,1)."</pre>", "Server Defines");
+            }
+
+            $this->addNotice("<pre>".print_r($consts,1)."</pre>", "Defined Parameters");
             $this->addNotice("<pre>".print_r($_REQUEST,1)."</pre>", "Request Data");
         }
 
index 73e6305..9dc68e7 100644 (file)
  * @package  glmMembersDatabase
  * @author   Chuck Scott <cscott@gaslightmedia.com>
  * @license  http://www.gaslightmedia.com Gaslightmedia
- * @release  admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
+ * @release  front.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
  * @link     http://dev.gaslightmedia.com/
  */
 
+/**
+ * Array of valid menu items and actions.
+ *
+ *
+ * The higher level elements are valid menu items. These correlate to
+ * actual menu or sub menu items that are hooks back to this controller
+ * class.
+ *
+ * The lower level items below each menu item are actions that may be specified
+ * by a "glmMembersAction" form field.
+ */
+$GLOBALS['glmMembersFrontValidActions'] = array(
+
+        'members' => array(
+                'index', // member list
+                'list',
+                'reports',
+                'other'
+        )
+);
+
+// Load glmPluginSupport class
+require_once (GLM_MEMBERS_PLUGIN_DIR . '/classes/glmPluginSupport.php');
+
 /*
  * This class controls which models are use for front-end functionality
  * of this plugin.
  */
-class glmMembersFront
+class glmMembersFront extends GlmPluginSupport
 {
 
     /**
@@ -29,16 +53,335 @@ class glmMembersFront
      */
     public $wpdb;
 
-    public function __construct ($wpdb)
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    public function __construct ($wpdb, $config)
     {
 
         // Save WordPress Database object
         $this->wpdb = $wpdb;
 
+        // Save plugin configuration object
+        $this->config = $config;
+
+        // Add front-end scripts and css
+        add_action('front_enqueue_scripts',
+                array(
+                        $this,
+                        'glmMembersFrontScripts'
+                ));
+
+        add_shortcode('glm-members',
+                array(
+                        $this,
+                        'controller'
+                ));
+    }
+
+    /**
+     * Setup inclusion of front-end scripts and css
+     *
+     * This method is called by an add_action() hook setup in the contructor.
+     *
+     * (no prameters)
+     *
+     * @return void
+     * @access public
+     */
+    public function glmMembersFrontScripts ()
+    {
+
         /*
-         * This plugin does not currently have front-end functionality
+         *
+         * wp_enqueue_script('jquery');
+         * wp_enqueue_script('jquery-style');
+         * wp_enqueue_script('jquery-ui-core');
+         * wp_enqueue_script('jquery-ui-dialog');
+         *
+         * wp_register_script('glm-members-front-js',
+         * GLM_MEMBERS_PLUGIN_URL . 'js/front.js',
+         * array(
+         * 'jquery'
+         * ));
+         * wp_enqueue_script('glm-members-front-js');
          */
+
+        // A simple set of styles for things I haven't found as a WordPress
+        // default yet
+        wp_register_style('glmMembersFrontStyle',
+                GLM_MEMBERS_PLUGIN_URL . 'css/front.css');
+        wp_enqueue_style('glmMembersFrontStyle');
     }
-}
 
+    /**
+     * Front-End controller
+     *
+     * This method is called by a plugin menu method. It is responsible for
+     * executing the approriate model, combining model data with a view, and
+     * outputing the result. It is therefore the core of the controller.
+     *
+     * This controller is supplied a menu item name and then determines if
+     * there is an additional action related to that menu item that needs to be
+     * executed rather than the default menu action.
+     *
+     * All models should return an array containing the following.
+     *
+     * '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.
+     *
+     * For a better explanation of how this all works, see the description for
+     * this class.
+     *
+     * Controller parameters
+     *
+     * @param object $atts
+     *            Shortcode attributes
+     * @param string $content
+     *            Content included by an Enclosing format shortcode
+     *
+     * @return void
+     * @access public
+     */
+    public function controller ($atts, $content = null)
+    {
+        // Shortcode Attributes defaults
+        $request = shortcode_atts(
+                array(
+                        'action' => 'list',
+                        'option' => false
+                ), $atts);
+
+
+
+
+
+
+
+        $errorMsg = '';
+
+        if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
+
+            // Get all defiend constants then if not VERBOSE select out only user contstants
+            $consts = get_defined_constants(true);
+            if (!GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE) {
+                $consts = $consts['user'];
+            }
+
+            $this->addNotice("<pre>".print_r($consts,1)."</pre>", "Defined Parameters");
+            $this->addNotice("<pre>".print_r($_REQUEST,1)."</pre>", "Request Data");
+        }
+
+        /*
+         * Determine model to execute
+         */
+
+        // Default action is "index"
+        $action = 'index';
+
+        // Get any requested "action" from a form submission modify path/name
+        // accordingly
+        if (isset($_REQUEST['glm_action']) && $_REQUEST['glm_action'] != '') {
+            $a = sanitize_text_field($_REQUEST['glm_action']);
+            if ($a != '') {
+                $action = $a;
+            }
+        }
+
+        // Loop till we have a final action
+        $loopCheck = 0;
+        $redirectData = false;
+        do {
+
+            if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
+                $this->addNotice(
+                        "<b>Requested Action:</b> Menu item = $menuItem, Action = $action");
+            }
+
+            $modelRedirect = false;
+
+            // Verify that we have the requested menu item in the valid actions
+            if (! isset($GLOBALS['glmMembersFrontValidActions'][$menuItem])) {
+
+                $modelRedirect = true;
+                $menuItem = 'error';
+                $action = 'index';
+                $errorMsg .= "<b>Model doesn't exist:</b> " . $modelName;
+            }
+
+            // Verify Menu item and action using array at top of this file
+            if (! isset($GLOBALS['glmMembersFrontValidActions'][$menuItem]) ||
+                     ! in_array($action,
+                            $GLOBALS['glmMembersFrontValidActions'][$menuItem])) {
+                $menuItem = 'error';
+                $action = 'badAction';
+            }
+
+            /*
+             * Execute the selected model
+             */
+
+            // Build model and path and class names
+            $modelName = GLM_MEMBERS_PLUGIN_DIR . '/models/front/' . $menuItem .
+                     '/' . $action . '.php';
+            $className = 'GlmMembersFront_' . $menuItem . '_' . $action;
+
+            // If model file doesn't exist - we have an error
+            if (! file_exists($modelName)) {
+
+                $modelRedirect = true;
+                $menuItem = 'error';
+                $action = 'index';
+                $errorMsg .= "<b>Model doesn't exist:</b> " . $modelName;
+
+                // Otherwise, load and run the model
+            } else {
+
+                // Load the model file
+                require_once ($modelName);
+
+                // check for an invalid model class name
+                if (! class_exists($className)) {
+
+                    $modelRedirect = true;
+                    $menuItem = 'error';
+                    $action = 'index';
+                    $errorMsg .= "<b>Model class doesn't exist:</b> " .
+                             $className;
+                } else {
+
+                    // Check if this is a re-direct with parameters
+                    $args = false;
+
+                    // Instantiate the model and ask it to perform the work
+                    $model = new $className($this->wpdb, $this->config);
+                    $results = $model->modelAction($redirectData);
+
+                    // Check if there's been a model redirect request
+                    if ($results['modelRedirect']) {
+
+                        // Set the new model action
+                        $action = $results['modelRedirect'];
+
+                        // Check if there's also a menu item change
+                        if ($results['menuItemRedirect']) {
+                            $menuItem = $results['menuItemRedirect'];
+                        }
+
+                        // Check if there's data to pass to the new model
+                        if (isset($results['data']) &&
+                                 count($results['data']) > 0) {
+                            $redirectData = $results['data'];
+                        }
+
+                        $modelRedirect = true;
+                    }
+
+                    // Get the specified view file
+                    $view = false;
+                    if (isset($results['view'])) {
+                        $view = $results['view'];
+                    }
+
+                    // Check for invalid or missing view file
+                    if (! $view || ! is_file(
+                            GLM_MEMBERS_PLUGIN_DIR . '/views/' . $view)) {
+                        $modelRedirect = true;
+                        $menuItem = 'error';
+                        $action = 'index';
+                        $errorMsg .= "<b>Bad or missing view file:</b> " . $view;
+                    }
+                } // model class exists
+            }
+
+            // This is just a sanity check on this loop to keep it from getting
+            // out of control
+            if (++ $loopCheck > 10) {
+                die(
+                        '<h1>Serious failure looping on model load in "controllers/front.php".</h1>');
+            }
+
+            if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG && $modelRedirect) {
+                $this->addNotice('<b>Redirecting...</b>');
+            }
+
+            // Loop again if there's a model redirect
+        } while ($modelRedirect);
+
+        /*
+         * Check model results
+         */
+
+        // Get suggested view
+        $view = $results['view'];
+
+        // If there's a general model failure use the error view
+        if (! $results['status']) {
+            $view = 'front/error/index.html';
+        }
+
+        /*
+         * Merge data returned from the model with the selected view
+         */
+
+        // Load Smarty Template support
+        require (GLM_MEMBERS_PLUGIN_DIR . '/lib/smartyTemplateSupport.php');
+        $smarty = new smartyTemplateSupport();
+
+        // Add data from model to Smarty template
+        if (is_array($results['data']) && count($results['data']) > 0) {
+            foreach ($results['data'] as $k => $d) {
+                $smarty->templateAssign($k, $d);
+            }
+        }
+
+        $smarty->templateAssign('thisAction', $action);
+
+        // If there's an error message, add that also
+        if ($errorMsg != '') {
+            $smarty->templateAssign('errorMsg', $errorMsg);
+        }
+
+        // If view debug has been requested
+        if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
+
+            glmMembersFront::addNotice("<b>Template Data</b><br> $view");
+
+            $x = $smarty->template->getTemplateVars();
+            $templateVars = '<pre>' . print_r($x, 1) . '</pre>';
+            glmMembersFront::addNotice($templateVars, 'Template Parameters');
+        }
+
+        // Generate output from model data and view
+        $smarty->template->display($view);
+    }
+}
 ?>
\ No newline at end of file
diff --git a/css/front.css b/css/front.css
new file mode 100644 (file)
index 0000000..7766c46
--- /dev/null
@@ -0,0 +1,5 @@
+/*
+
+    Gaslight Media Members Database Front-End Styles
+
+*/
index 4bfcf0e..90cf4af 100644 (file)
--- a/index.php
+++ b/index.php
 // Debug Options
 define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG', true);
 define('GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE', true);
+define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG', true);
+define('GLM_MEMBERS_PLUGIN_FRONT_DEBUG_VERBOSE', true);
 
 // Plugin Versions
 define('GLM_MEMBERS_PLUGIN_VERSION', 0.1);
 define('GLM_MEMBERS_PLUGIN_DB_VERSION', 0.1);
 
-// Other standard plugin defines
-define('GLM_MEMBERS_PLUGIN_NAME', 'Gaslight Media Members Database');
+// URLs
 define('GLM_MEMBERS_PLUGIN_URL', plugin_dir_url(__FILE__));
+$pageProtocol = 'http';
+if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') {
+    $pageProtocol = 'https';
+}
+define('GLM_MEMBERS_PLUGIN_CURRENT_BASE_URL', $pageProtocol.'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']);
+
+// Directories
+define('GLM_MEMBERS_PLUGIN_NAME', 'Gaslight Media Members Database');
 define('GLM_MEMBERS_PLUGIN_DIR', dirname(__FILE__));
 define('GLM_MEMBERS_PLUGIN_DB_SCRIPTS', dirname(__FILE__).'/misc/databaseScripts');
 define('GLM_MEMBERS_PLUGIN_CLASS_DIR', GLM_MEMBERS_PLUGIN_DIR.'/classes');
diff --git a/js/front.js b/js/front.js
new file mode 100644 (file)
index 0000000..fe7e3d2
--- /dev/null
@@ -0,0 +1,2 @@
+
+/* Nothing here yet */
\ No newline at end of file
index a5dd0f5..c50da95 100644 (file)
@@ -57,19 +57,9 @@ class smartyTemplateSupport {
 
                $this->template = new Smarty ();
 
-               // Get current script URL
-               $urlParts = explode('?', $_SERVER['HTTP_REFERER']);
-               $scriptURL = $urlParts[0];
-
-               // Check for default action
-               $action = 'index';
-               if (isset($_REQUEST['glm_action']) && trim($_REQUEST['glm_action']) != '') {
-                   $action = $_REQUEST['glm_action'];
-               }
-
                // Add standard parameters
            $this->templateAssign ( 'adminDebug', GLM_MEMBERS_PLUGIN_ADMIN_DEBUG);
-               $this->templateAssign ( 'thisURL', $scriptURL );
+               $this->templateAssign ( 'thisURL', GLM_MEMBERS_PLUGIN_CURRENT_BASE_URL );
                $this->templateAssign ( 'thisPage', $_REQUEST['page']);
                $this->templateAssign ( 'glmPluginName', GLM_MEMBERS_PLUGIN_NAME );
                $this->templateAssign ( 'thisYear', date ( 'Y' ) );
index c174039..c82ad60 100644 (file)
@@ -1,4 +1,4 @@
-<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-01-15 01:57:45
+<?php /* Smarty version Smarty-3.1.21-dev, created on 2015-01-18 22:53:18
          compiled from "/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/footer.html" */ ?>
 <?php /*%%SmartyHeaderCode:146319615854b4cb224487c6-63517443%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
 $_valid = $_smarty_tpl->decodeProperties(array (
@@ -7,7 +7,7 @@ $_valid = $_smarty_tpl->decodeProperties(array (
     '50e78f1400d6db96c588c737669426ab57397d1d' => 
     array (
       0 => '/var/www/server/wordpress/wp-content/plugins/glm-member-db/views/admin/footer.html',
-      1 => 1421287062,
+      1 => 1421621593,
       2 => 'file',
     ),
   ),
@@ -37,7 +37,7 @@ $_valid = $_smarty_tpl->decodeProperties(array (
         Phone: 231-487-0692 - E-Mail: info@gaslightmedia.com<br>
         <a href="http://www.gaslightmedia.com">http://www.gaslightmedia.com</a>
     </div>
-    
+     
   <?php if ($_smarty_tpl->tpl_vars['adminDebug']->value) {?>
     <?php echo '<script'; ?>
 >
index 5b06a43..d34194c 100644 (file)
@@ -6,7 +6,7 @@
         Phone: 231-487-0692 - E-Mail: info@gaslightmedia.com<br>
         <a href="http://www.gaslightmedia.com">http://www.gaslightmedia.com</a>
     </div>
-    
+     
   {if $adminDebug}
     <script>
         window.open('{$thisURL}?page={$thisPage}&glmDebugWindow=true','GLM_Plugin_Debug','width=800,height=800,left=50,top=50,resizable=yes,scrollbars=yes');