Updating blocks
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 1 Feb 2016 20:18:54 +0000 (15:18 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 1 Feb 2016 20:18:54 +0000 (15:18 -0500)
Adding ability to add block that link to events from time.ly

controllers/admin.php
js/glm-blocks.js
models/block.php
views/admin/metaBoxes.php

index cf09a3c..9079930 100644 (file)
@@ -30,3 +30,14 @@ class glm_blocks_admin
     }
 
 }
+
+/*
+ *
+    SELECT p.ID,p.post_title,
+           DATE_FORMAT(FROM_UNIXTIME(e.start), '%Y-%m-%d') as 'starting',
+           DATE_FORMAT(FROM_UNIXTIME(e.end), '%Y-%m-%d') AS ending
+      FROM glm_18_posts p LEFT OUTER JOIN glm_18_ai1ec_events e
+            ON (e.post_id = p.ID)
+     WHERE p.post_type = 'ai1ec_event';
+ *
+ */
index 541bdf4..fc3ee41 100644 (file)
@@ -1,5 +1,4 @@
 jQuery(function($){
-
     $("#edit-slug-box").hide();
     $("#set-ngg-post-thumbnail").hide();
     $("#glm_block_page").change(function(){
@@ -23,4 +22,11 @@ jQuery(function($){
             $("input[name=glm_block_url]").filter('input[value=url]').attr('checked', false);
         }
     });
+    $("#glm_block_event").change(function(){
+        if($("#glm_block_event").val()) {
+            $("input[name=glm_block_url]").filter('input[value=event]').attr('checked', true);
+        } else {
+            $("input[name=glm_block_url]").filter('input[value=event]').attr('checked', false);
+        }
+    });
 });
index 0788cd4..5c018a1 100644 (file)
@@ -70,12 +70,12 @@ class glm_models_block
             && isset($query)
             && is_object($query)
             && isset($query->query['post_type'])) {
-            $postType = $query->query['post_type'];
-            if ($postType == GLM_BLOCK_POST_TYPE) {
-                $query->set('orderby', 'menu_order');
-                $query->set('order', 'asc');
+                $postType = $query->query['post_type'];
+                if ($postType == GLM_BLOCK_POST_TYPE) {
+                    $query->set('orderby', 'menu_order');
+                    $query->set('order', 'asc');
+                }
             }
-        }
     }
 
     /**
@@ -102,7 +102,7 @@ class glm_models_block
             'label'              => __('Blocks'),
             'labels'             => $labels,
             'description'        => 'Stores Blocks and Block data',
-            'public'             => true,
+            'public'             => false,
             'publicly_queriable' => false,
             'rewrite'            => false,
             'show_ui'            => true,
@@ -156,6 +156,9 @@ class glm_models_block
         $glm_block_post    = ($post)
             ? get_post_meta($post->ID, 'glm_block_post', true)
             : '';
+        $glm_block_event    = ($post)
+            ? get_post_meta($post->ID, 'glm_block_event', true)
+            : '';
         include $this->pluginDirName . 'views/admin/metaBoxes.php';
     }
 
@@ -171,8 +174,7 @@ class glm_models_block
         global $post;
         // verify this came from the our screen and with proper authorization,
         // because save_post can be triggered at other times
-        if (isset($post) && !wp_verify_nonce($_POST['glm_block_noncename'],
-                             'glm_block_noncename')) {
+        if (isset($post) && !wp_verify_nonce($_POST['glm_block_noncename'], 'glm_block_noncename')) {
             return $post->ID;
         }
 
@@ -186,10 +188,10 @@ class glm_models_block
         // OK, we're authenticated: we need to find and save the data
         // We'll put it into an array to make it easier to loop though.
         update_post_meta($post->ID, 'glm_block_url', $_POST['glm_block_url']);
-        update_post_meta($post->ID, 'glm_block_ext_url',
-                         $_POST['glm_block_ext_url']);
+        update_post_meta($post->ID, 'glm_block_ext_url', $_POST['glm_block_ext_url']);
         update_post_meta($post->ID, 'glm_block_page', $_POST['glm_block_page']);
         update_post_meta($post->ID, 'glm_block_post', $_POST['glm_block_post']);
+        update_post_meta($post->ID, 'glm_block_event', $_POST['glm_block_event']);
     }
 
     /**
@@ -203,7 +205,7 @@ class glm_models_block
         global $wpdb, $wp;
         $catId = filter_var($catId, FILTER_VALIDATE_INT);
         $args   = array(
-               'posts_per_page' => -1,
+            'posts_per_page' => -1,
             'post_type'      => GLM_BLOCK_POST_TYPE,
             'orderby'        => 'menu_order post_title',
             'order'          => 'asc'
@@ -216,6 +218,9 @@ class glm_models_block
             $block->externalUrl = false;
             $custom = get_post_custom($block->ID);
             switch ($custom['glm_block_url'][0]) {
+            case 'event':
+                $block->url = get_permalink($custom['glm_block_event'][0]);
+                break;
             case 'page':
                 $block->url = get_permalink($custom['glm_block_page'][0]);
                 break;
@@ -271,32 +276,25 @@ class glm_models_block
 
         $messages['glm_blocks'] = array(
             0  => '', // Unused. Messages start at index 1.
-            1  => sprintf(__('Block updated. <a href="%s">View item</a>'),
-                             esc_url(get_permalink($post_ID))),
+            1  => __('Block updated.'),
             2  => __('Custom field updated.'),
             3  => __('Custom field deleted.'),
             4  => __('Block updated.'),
             /* translators: %s: date and time of the revision */
             5  => isset($_GET['revision'])
-                ? sprintf(__('Block restored to revision from %s'),
-                             wp_post_revision_title((int) $_GET['revision'],
-                                                    false))
+            ? sprintf(__('Block restored to revision from %s'),
+                wp_post_revision_title((int) $_GET['revision'],
+                false))
                 : false,
-            6  => sprintf(__('Block published. <a href="%s">View event</a>'),
-                             esc_url(get_permalink($post_ID))),
-            7  => __('Block saved.'),
-            8  => sprintf(__('Block submitted. <a target="_blank" href="%s">Preview event</a>'),
-                             esc_url(add_query_arg('preview', 'true',
-                                                   get_permalink($post_ID)))),
-            9  => sprintf(__('Block scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview job</a>'),
+                6  => __('Block published.'),
+                7  => __('Block saved.'),
+                8  => __('Block submitted.'),
+                9  => sprintf(__('Block scheduled for: <strong>%1$s</strong>.'),
                 // translators: Publish box date format, see http://php.net/date
-                             date_i18n(__('M j, Y @ G:i'),
-                                          strtotime($post->post_date)),
-                                                    esc_url(get_permalink($post_ID))),
-            10 => sprintf(__('Block draft updated. <a target="_blank" href="%s">Preview event</a>'),
-                             esc_url(add_query_arg('preview', 'true',
-                                                   get_permalink($post_ID)))),
-        );
+                date_i18n(__('M j, Y @ G:i'),
+                strtotime($post->post_date))),
+                10 => __('Block draft updated.'),
+            );
         return $messages;
     }
     /**
@@ -308,8 +306,8 @@ class glm_models_block
      */
     function addOrderColumn($header_text_columns)
     {
-      $header_text_columns['menu_order'] = "Order";
-      return $header_text_columns;
+        $header_text_columns['menu_order'] = "Order";
+        return $header_text_columns;
     }
 
     /**
@@ -323,13 +321,13 @@ class glm_models_block
     {
         global $post;
         switch ($name) {
-          case 'menu_order':
+        case 'menu_order':
             $order = $post->menu_order;
             echo $order;
             break;
-         default:
+        default:
             break;
-         }
+        }
     }
 
     /**
@@ -341,8 +339,73 @@ class glm_models_block
      */
     function orderSortable($columns)
     {
-      $columns['menu_order'] = 'menu_order';
-      return $columns;
+        $columns['menu_order'] = 'menu_order';
+        return $columns;
+    }
+
+    /**
+     * glm_blocks_get_posts
+     *
+     * @access public
+     * @return void
+     */
+    function get_posts()
+    {
+        $query = array(
+            'post_type'              => 'post',
+            'suppress_filters'       => true,
+            'update_post_term_cache' => false,
+            'update_post_meta_cache' => false,
+            'post_status'            => 'publish',
+            'order'                  => 'DESC',
+            'orderby'                => 'date',
+            'posts_per_page'         => -1,
+        );
+        $get_posts = new WP_Query;
+        $posts = $get_posts->query( $query );
+        $results = array();
+        if ( ! $get_posts->post_count ) {
+            return $results;
+        }
+        foreach ( $posts as $post ) {
+            if ( 'post' == $post->post_type ) {
+                $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
+            } else {
+                continue;
+            }
+            $results[] = array(
+                'ID'        => $post->ID,
+                'title'     => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
+                'permalink' => get_permalink( $post->ID ),
+                'info'      => $info,
+            );
+        }
+        return $results;
     }
 
+    /**
+     * get_events
+     *
+     * @access public
+     * @return void
+     */
+    public function get_events()
+    {
+        global $wpdb;
+        $results = array();
+        if ( !defined( 'AI1EC_PATH' ) ) {
+            return $results;
+        }
+
+        $sql = "
+          SELECT p.ID,p.post_title,
+                 DATE_FORMAT(FROM_UNIXTIME(e.start), '%m/%d/%Y') as 'starting',
+                 DATE_FORMAT(FROM_UNIXTIME(e.end), '%m/%d/%Y') AS ending
+            FROM {$wpdb->prefix}posts p LEFT OUTER JOIN {$wpdb->prefix}ai1ec_events e
+                 ON (e.post_id = p.ID)
+           WHERE p.post_type = 'ai1ec_event'
+        ORDER BY p.post_title,e.start";
+
+        return $wpdb->get_results( $sql, ARRAY_A );
+    }
 }
index 8adef7b..e5f6bd1 100644 (file)
@@ -45,7 +45,7 @@ wp_dropdown_pages(
 <select id="glm_block_post" name="glm_block_post">
 <option value="">None</option>
 <?php
-$glmPosts = glm_blocks_get_posts();
+$glmPosts = $this->get_posts();
 foreach ($glmPosts as $post) {
     echo '<option value="' . $post['ID'] . '"';
     if ($post['ID'] == $glm_block_post) {
@@ -56,36 +56,23 @@ foreach ($glmPosts as $post) {
 ?>
 </select>
 <?php
-function glm_blocks_get_posts() {
-    $query = array(
-        'post_type'              => 'post',
-        'suppress_filters'       => true,
-        'update_post_term_cache' => false,
-        'update_post_meta_cache' => false,
-        'post_status'            => 'publish',
-        'order'                  => 'DESC',
-        'orderby'                => 'date',
-        'posts_per_page'         => -1,
-    );
-    $get_posts = new WP_Query;
-    $posts = $get_posts->query( $query );
-    $results = array();
-    if ( ! $get_posts->post_count ) {
-        return $results;
-    }
-    foreach ( $posts as $post ) {
-        if ( 'post' == $post->post_type ) {
-            $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
-        } else {
-            continue;
+$events = $this->get_events();
+//echo '<pre>' . print_r( $events, true ) . '</pre>';
+if ( isset( $events ) && is_array( $events ) && !empty( $events ) ) {
+    ?><br>
+    <input type="radio" name="glm_block_url" value="event"
+    <?php if ($glm_block_url == 'event') { echo 'checked'; }; ?>>
+    <?php
+    echo '<label for="my_meta_box_post_type">Internal Event: </label>';
+    echo '<select id="glm_block_event" name="glm_block_event">';
+    echo '<option value="">None</option>';
+    foreach ( $events as $event ) {
+        echo '<option value="' . $event['ID'] . '"';
+        if ($event['ID'] == $glm_block_event) {
+            echo ' selected';
         }
-        $results[] = array(
-            'ID'        => $post->ID,
-            'title'     => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
-            'permalink' => get_permalink( $post->ID ),
-            'info'      => $info,
-        );
+        echo '>' . $event['post_title'] . ' (' . $event['starting'] . ')</option>';
     }
-    return $results;
+    echo '</select>';
 }
 ?>