Blog turned into Calendar listing dates help

edited March 2014 in Modules
I used the Blog module and created a Calendar module from it, which worked out very nice. I've been noodling this for a moment, but can't seem to figure out how to do my next modification...

For the Calendar module, I would like to have the event disappear the day after it's happened (not the day of, so users can still log on the site and see the event for the day). I can't seem to figure out where to put the script, or what script to use.

The calendar module is pretty much identical to the blog module, but I've gone in and changed blog to calendar in all the files/names - time consuming, yes! Results, awesome!

Any help on the matter would be much appreciated, as it's one of the only bugs I've discovered in my conversion thus far. Once I get this part figured out, I'd be happy to share the module, so it can be perfected by the Fuel savants.

Comments

  • edited 7:57AM
    The Fuel_blog (so Fuel_calendar) class has some methods in it used to render the list of blog posts (calendar events). In particular, there is a "get_posts_by_date" method. Note that there are several database where parameters in addition the order_by (which you may need to use). I would add an additional where parameter in there that specifies you want to list only events from tomorrow and older. So perhaps something like this:
    $datetime = new DateTime('tomorrow'); $where['post_date <='] = $datetime->format('Y-m-d H:i:s'); $this->CI->blog_posts_model->db()->where($where);
  • edited 7:57AM
    Thank you for quickly responding.

    I'm still stumped where to use that script. Currently I can change the order of the posts in the themes/default/_blocks/posts.php. Order desc vs asc. "Date" reflects the day of the event in my database.

    <?php $posts = fuel_model('calendar_posts', array('find' => 'all', 'order' => 'date asc', 'module' => 'calendar')) ?>

    Should I drop the code in posts.php file?
  • edited 7:57AM
    For the blog, the fuel/modules/blog/controllers/blog.php controller provides an array of $posts using the Fuel_blog::get_posts_by_date() method (you'll see it in the main _remap method around line 73). That array gets passed on to the posts.php file. So if you are doing a secondary call in your view file, it might be an extra call to the database that you don't necessarily need if you are able to modify that "get_posts_by_date" method.
  • edited 7:57AM
    I checked the blog controller versus my calendar controller and they seem identical with exception of using calendar vs blog. Not sure if I'm making an extra call... While I'm comfortable with this framework, I'm still getting a grip on php...

    Here's my posts.php page:

    <hr size="1" noshade="noshade" /> <div class="posts left"> <?php $posts = fuel_model('calendar_posts', array('find' => 'all', 'order' => 'date asc', 'module' => 'calendar')) ?> <?=fuel_edit('create', 'Create Post', 'calendar/posts')?> <?php if (!empty($posts)) : ?> <?php foreach($posts as $post) : ?> <div class="post"> <?=fuel_edit($post)?> <?=calendar_block('post_unpublished', array('post' => $post, 'order'=>'date asc'))?><br> <h2><a href="<?=$post->url?>"><?=$post->date_formatted('F d, Y')?></a></h2><br> <h3><a href="<?=$post->url?>"><?=$post->title?></a></h3> <div class="post_content"> <?=$post->excerpt_formatted?> </div><br> <hr> </div> <div class="clear"></div> <?php endforeach; ?> <div class="view_archives"> <?php if (!empty($pagination)) : ?><?=$pagination?> &nbsp;<?php endif; ?> </div> <?php else: ?> <div class="no_posts"> <p>There are no posts available.</p> </div> <?php endif; ?> </div>

    And this is my controllers/calendar.php file line 60 - 78:

    else if ($view_by == 'date') { $page_title_arr = array(); $posts_date = mktime(0, 0, 0, $month, $day, $year); if (!empty($day)) $page_title_arr[] = $day; if (!empty($month)) $page_title_arr[] = date('M', strtotime($posts_date)); if (!empty($year)) $page_title_arr[] = $year; // run before_posts_by_date hook $hook_params = array('year' => $year, 'month' => $month, 'day' => $day, 'slug' => $slug, 'limit' => $limit); $this->fuel->calendar->run_hook('before_posts_by_date', $hook_params); $vars = array_merge($vars, $hook_params); $vars['page_title'] = $page_title_arr; $vars['posts'] = $this->fuel->calendar->get_posts_by_date($year, (int) $month, $day, $slug); $vars['pagination'] = ''; } else
  • edited 7:57AM
    This line shouldn't be necessary in the posts.php file since the controller is already making the database query with "get_posts_by_date" before sending the returned array of posts to the view (posts.php):
    <?php $posts = fuel_model('calendar_posts', array('find' => 'all', 'order' => 'date asc', 'module' => 'calendar')) ?>
  • edited 7:57AM
    Cool. I dumped that line of code in my posts.php file. I'm still a little confused on updating the other page to modify how the posts are suppose to load in-terms of not showing after the date has happened.

    The remap that it's calling in the calendar.php controller is at the top of the page, rather then around line 73...
  • edited 7:57AM
    Below is what I would recommend doing to the Fuel_calendar::get_posts_by_date() method. Note the commented area and that I substituted the model name to "calendar_posts_model" where I thought it necessary:
    function get_posts_by_date($year = NULL, $month = NULL, $day = NULL, $slug = NULL, $limit = NULL, $offset = NULL, $order_by = 'sticky, post_date desc', $return_method = NULL, $assoc_key = NULL) { $this->CI->load->module_model(CALENDAR_FOLDER, 'calendar_posts_model'); $this->CI->calendar_posts_model->readonly = TRUE; $tables = $this->CI->config->item('tables'); $where = $this->_publish_status('blog_posts'); // ADDED TO WHERE CONDITION OF QUERY $datetime = new DateTime('tomorrow'); $where['post_date <='] = $datetime->format('Y-m-d H:i:s'); $this->CI->calendar_posts_model->db()->where($where); if (!empty($year)) $this->CI->calendar_posts_model->db()->where('YEAR('.$tables['calendar_posts'].'.post_date) = '.$year); if (!empty($month)) $this->CI->calendar_posts_model->db()->where('MONTH('.$tables['calendar_posts'].'.post_date) = '.$month); if (!empty($day)) $this->CI->calendar_posts_model->db()->where('DAY('.$tables['calendar_posts'].'.post_date) = '.$day); if (!empty($slug)) $this->CI->calendar_posts_model->db()->where($tables['calendar_posts'].'.slug = "'.$slug.'"'); $return_arr = (!empty($slug)) ? FALSE : TRUE; if (!empty($limit)) { $this->CI->calendar_posts_model->db()->limit($limit); } $this->CI->calendar_posts_model->db()->offset($offset); $this->CI->calendar_posts_model->db()->order_by($order_by); $posts = $this->CI->calendar_posts_model->get($return_arr)->result(); return $posts; }
  • edited April 2014
    I updated it to $where=$this->_publish_status ('calendar_posts'); and it's still not displaying the data the way I need... I wonder if it's because I added a row for 'date' in the database?

    function get_posts_by_date($year = NULL, $month = NULL, $day = NULL, $slug = NULL, $limit = NULL, $offset = NULL, $order_by = 'sticky, post_date desc', $return_method = NULL, $assoc_key = NULL) { $this->CI->load->module_model(calendar_FOLDER, 'calendar_posts_model'); $this->CI->calendar_posts_model->readonly = TRUE; $tables = $this->CI->config->item('tables'); $where = $this->_publish_status('calendar_posts'); // ADDED TO WHERE CONDITION OF QUERY $datetime = new DateTime('tomorrow'); $where['date <='] = $datetime->format('Y-m-d H:i:s'); $this->CI->calendar_posts_model->db()->where($where); if (!empty($year)) $this->CI->calendar_posts_model->db()->where('YEAR('.$tables['calendar_posts'].'.date) = '.$year); if (!empty($month)) $this->CI->calendar_posts_model->db()->where('MONTH('.$tables['calendar_posts'].'.date) = '.$month); if (!empty($day)) $this->CI->calendar_posts_model->db()->where('DAY('.$tables['calendar_posts'].'.date) = '.$day); if (!empty($slug)) $this->CI->calendar_posts_model->db()->where($tables['calendar_posts'].'.slug = "'.$slug.'"'); $return_arr = (!empty($slug)) ? FALSE : TRUE; if (!empty($limit)) { $this->CI->calendar_posts_model->db()->limit($limit); } $this->CI->calendar_posts_model->db()->offset($offset); $this->CI->calendar_posts_model->db()->order_by($order_by); $posts = $this->CI->calendar_posts_model->get($return_arr)->result(); return $posts; }


    where it read post_date, I updated to 'date'.
  • edited 7:57AM
    Right before "return $post;", add:
    $this->CI->calendar_posts_model->debug_query();
    This will output the query that's being displayed and may make it easier to debug. If you don't see any output, then it may be that it's not hitting this method.
  • edited 7:57AM
    Doesn't seem to be hitting this method... i added that as well.
  • edited 7:57AM
    OK. So in the blog/calendar controller what is populating the $vars['post'] parameter in the _remap method?
  • edited 7:57AM
    <?php require_once(MODULES_PATH.'/calendar/libraries/Calendar_base_controller.php'); class calendar extends Calendar_base_controller { function __construct() { parent::__construct(); } function _remap() { $year = ($this->uri->rsegment(2) != 'index') ? (int) $this->uri->rsegment(2) : NULL; $month = (int) $this->uri->rsegment(3); $day = (int) $this->uri->rsegment(4); $slug = $this->uri->rsegment(5); $limit = (int) $this->fuel->calendar->config('per_page'); $view_by = 'page'; ; // we empty out year variable if it is page because we won't be querying on year' if (preg_match('#\d{4}#', $year) && !empty($year) && empty($slug)) { $view_by = 'date'; } // if the first segment is id then treat the second segment as the id else if ($this->uri->rsegment(2) === 'id' && $this->uri->rsegment(3)) { $view_by = 'slug'; $slug = (int) $this->uri->rsegment(3); $post = $this->fuel->calendar->get_post($slug); if (isset($post->id)) { redirect($post->url); } } else if (!empty($slug)) { $view_by = 'slug'; }
  • edited 7:57AM
    What does the rest of the method look like and what URI are you testing (basically wondering what the $view_by variable gets set to.
  • edited 7:57AM
    Here's the rest of that code:

    // set this to false so that we can use segments for the limit $cache_id = fuel_cache_id(); $cache = $this->fuel->calendar->get_cache($cache_id); if (!empty($cache)) { $output =& $cache; } else { $vars = $this->_common_vars(); if ($view_by == 'slug') { return $this->post($slug); } else if ($view_by == 'date') { $page_title_arr = array(); $posts_date = mktime(0, 0, 0, $month, $day, $year); if (!empty($day)) $page_title_arr[] = $day; if (!empty($month)) $page_title_arr[] = date('M', strtotime($posts_date)); if (!empty($year)) $page_title_arr[] = $year;

    Looks like this line could be the one:
    else if ($view_by=='date') ... $posts_date = mktime(0, 0, 0, $month,$day, $year);
  • edited 7:57AM
    What does it display below that?... I imagine it makes some sort of $this->fuel->calendar->....() call to get data correct?
  • edited 7:57AM
    yeah... I think so.

    // run before_posts_by_date hook $hook_params = array('year' => $year, 'month' => $month, 'day' => $day, 'slug' => $slug, 'limit' => $limit); $this->fuel->calendar->run_hook('before_posts_by_date', $hook_params); $vars = array_merge($vars, $hook_params); $vars['page_title'] = $page_title_arr; $vars['posts'] = $this->fuel->calendar->get_posts_by_date($year, (int) $month, $day, $slug); $vars['pagination'] = ''; } else { $limit = $this->fuel->calendar->config('per_page'); $this->load->library('pagination'); $config['uri_segment'] = 3; $offset = $this->uri->segment($config['uri_segment']); $this->config->set_item('enable_query_strings', FALSE); $config = $this->fuel->calendar->config('pagination'); $config['base_url'] = $this->fuel->calendar->url('page/'); //$config['total_rows'] = $this->fuel->calendar->get_posts_count(); $config['page_query_string'] = FALSE; $config['per_page'] = $limit; $config['num_links'] = 2; //$this->pagination->initialize($config); if (!empty($offset)) { $vars['page_title'] = lang('calendar_page_num_title', $offset, $offset + $limit); } else { $vars['page_title'] = ''; }


    $vars['posts'] = $this->fuel->calendar->get_posts_by_date($year, (int) $month, $day, $slug);
  • edited 7:57AM
    Is there a SQL query that gets outputted if you place this after that line (assuming calendar_posts_model is the name of your model):
    $this->calendar_posts_model->debug_query();
  • edited 7:57AM
    Yeah. calendar_posts_model is the name of my model... when I insert that line after $vars['posts'] it does not output anything different.

    I made modification to the calendar_posts_model file, so it does not display unpublished posts, so I know the model works... Can we update the calendar_model to show only the dates that haven't happen?

    I wonder if it would be a better solution to have it set to unpublished after the date happens? Hmmm?
  • edited 7:57AM
    I think it best to just display the upcoming events using the date instead of setting it to unpublished.
Sign In or Register to comment.