* @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
{
/**
*/
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