Blog turned into Calendar listing dates help
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
$datetime = new DateTime('tomorrow'); $where['post_date <='] = $datetime->format('Y-m-d H:i:s'); $this->CI->blog_posts_model->db()->where($where);
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?
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?> <?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
<?php $posts = fuel_model('calendar_posts', array('find' => 'all', 'order' => 'date asc', 'module' => 'calendar')) ?>
The remap that it's calling in the calendar.php controller is at the top of the page, rather then around line 73...
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; }
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'.
$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.
<?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'; }
// 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);
// 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);
$this->calendar_posts_model->debug_query();
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?