// 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;
+
+ }
+ }
+
}
/**
require_once(GLM_PROTO_PLUGIN_DIR.'/models/admin/add.php');
return new glmProtoAdminAddModel($this->wpdb);
}
-
+
}
--- /dev/null
+.glm-proto-required {
+ color: red;
+}
+.glm-proto-error {
+ color: red;
+}
\ No newline at end of file
}
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
--- /dev/null
+
+// 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
* Load and instatiate Smarty Templates
*/
require(GLM_PROTO_PLUGIN_DIR.'/lib/Smarty-3.1.21/libs/Smarty.class.php');
-
+
$this->template = new Smarty();
/*
/*
* 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
{
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;
$templateData = array(
'glm_proto_title' => '',
'glm_proto_title_error' => '',
+ 'glm_proto_background' => '',
'glm_proto_background_error' => '',
+ 'glm_proto_prototype' => '',
'glm_proto_prototype_error' => ''
);
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;
}
}
/*
- * 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
*/
class glmProtoAdminDisplayModel extends smartyTemplateSupport
{
+
/**
* WordPress Database Object
* @var $wpdb
// 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';
*/
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;
// 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);
-
+
}
/*
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;
}
}
<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> </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>
<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>
<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>