Skip to content

Commit

Permalink
Reworked all date handling and formatting throughout the system. form…
Browse files Browse the repository at this point in the history
…at_date() handles output based on a setting and everything in a date picker field must be YYYY-MM-DD with no exceptions. Fixes news date weirdness.
  • Loading branch information
Phil Sturgeon committed Jan 24, 2011
1 parent 269d216 commit f701f80
Show file tree
Hide file tree
Showing 23 changed files with 379 additions and 351 deletions.
2 changes: 1 addition & 1 deletion addons/modules/galleries/views/admin/index.php
Expand Up @@ -25,7 +25,7 @@
<td><?php echo form_checkbox('action_to[]', $gallery->id); ?></td>
<td><?php echo $gallery->title; ?></td>
<td><?php echo $gallery->photo_count; ?></td>
<td><?php echo date('M d, Y',$gallery->updated_on); ?></td>
<td><?php echo format_date($gallery->updated_on); ?></td>
<td>
<?php echo
anchor('galleries/' . $gallery->slug, lang('galleries.view_label'), 'target="_blank"') . ' | ' .
Expand Down
2 changes: 1 addition & 1 deletion system/pyrocms/assets/js/admin/functions.js
Expand Up @@ -11,7 +11,7 @@ jQuery(function($) {
* This initializes all JS goodness
*/
pyro.init = function() {
$("#datepicker").datepicker({dateFormat: 'dd/mm/yy'});
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
$("#main-nav li ul").hide();
$("#main-nav li a.current").parent().find("ul").toggle();
$("#main-nav li a.current:not(.no-submenu)").addClass("bottom-border");
Expand Down
2 changes: 1 addition & 1 deletion system/pyrocms/config/migrations.php
Expand Up @@ -22,7 +22,7 @@
| be upgraded / downgraded to.
|
*/
$config['migrations_version'] = 3;
$config['migrations_version'] = 4;


/*
Expand Down
22 changes: 22 additions & 0 deletions system/pyrocms/helpers/MY_date_helper.php
@@ -0,0 +1,22 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
* CodeIgniter Date Helpers
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author Philip Sturgeon
*/

// ------------------------------------------------------------------------

function format_date($unix)
{
if ($unix == '' || ! is_numeric($unix))
{
$unix = strtotime($unix);
}

return date(get_instance()->settings->date_format, $unix);
}
28 changes: 28 additions & 0 deletions system/pyrocms/migrations/004_AddedDateFormat.php
@@ -0,0 +1,28 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_AddedDateFormat extends Migration {

function up()
{
$this->migrations->verbose AND print "Added setting - date_format.";

//can you set default to null with dbforge?
$this->db->insert('settings', array(
'slug' => 'date_format',
'title' => 'Date Format',
'description' => 'How should dates be displayed accross the website and control panel? Using PHP date format.',
'type' => 'text',
'`default`' => 'Y-m-d',
'`value`' => '',
'`options`' => '',
'module' => '',
'is_required' => 1,
'is_gui' => 1
));
}

function down()
{
$this->db->delete('settings', array('slug' => 'date_format'));
}
}
2 changes: 1 addition & 1 deletion system/pyrocms/modules/comments/views/admin/index.php
Expand Up @@ -51,7 +51,7 @@
<?php endif; ?>
</td>
<td><?php echo mailto($comment->email);?></td>
<td><?php echo date('M d, Y', $comment->created_on);?></td>
<td><?php echo format_date($comment->created_on);?></td>
<td>
<?php echo anchor('admin/comments/preview/'. $comment->id, lang('comments.preview_label'), 'rel="modal" target="_blank"'); ?> |

Expand Down
2 changes: 1 addition & 1 deletion system/pyrocms/modules/comments/views/admin/preview.php
@@ -1,6 +1,6 @@
<div id="comment-preview">
<p class="width-two-thirds float-left">
<strong><?php echo lang('comments.posted_label');?>:</strong> <?php echo date('M d, Y', $comment->created_on); ?><br/>
<strong><?php echo lang('comments.posted_label');?>:</strong> <?php echo format_date($comment->created_on); ?><br/>
<strong><?php echo lang('comments.from_label');?>:</strong> <?php echo $comment->name;?>
</p>

Expand Down
2 changes: 1 addition & 1 deletion system/pyrocms/modules/comments/views/comments.php
@@ -1 +1 @@
<div id="comments_container"> <h2><?php echo lang('comments.title');?></h2> <?php $comments = $this->comments_m->get_by_module_item($module, $module_id); if(!empty($comments)): ?> <ul id="comment_list"> <?php foreach( $comments as $comment): ?> <li class="comment"> <?php echo gravatar($comment->email, 40); ?> <p class="comment_heading"><strong><?php echo anchor($comment->website, $comment->name); ?></strong></p> <p class="comment_date"><?php echo date('M d, Y', $comment->created_on); ?></p> <p class="comment_body"><?php echo nl2br(stripslashes($comment->comment));?></p> </li> <?php endforeach; ?> </ul> <?php else: ?> <p><?php echo lang('comments.no_comments');?></p> <?php endif; ?></div><div id="comments_form_container"> <h2><?php echo lang('comments.your_comment');?></h2> <?php echo $this->load->view('comments/form', array('module'=>$module, 'id' => $module_id)); ?></div>
<div id="comments_container"> <h2><?php echo lang('comments.title');?></h2> <?php $comments = $this->comments_m->get_by_module_item($module, $module_id); if(!empty($comments)): ?> <ul id="comment_list"> <?php foreach( $comments as $comment): ?> <li class="comment"> <?php echo gravatar($comment->email, 40); ?> <p class="comment_heading"><strong><?php echo anchor($comment->website, $comment->name); ?></strong></p> <p class="comment_date"><?php echo format_date($comment->created_on); ?></p> <p class="comment_body"><?php echo nl2br(stripslashes($comment->comment));?></p> </li> <?php endforeach; ?> </ul> <?php else: ?> <p><?php echo lang('comments.no_comments');?></p> <?php endif; ?></div><div id="comments_form_container"> <h2><?php echo lang('comments.your_comment');?></h2> <?php echo $this->load->view('comments/form', array('module'=>$module, 'id' => $module_id)); ?></div>
Expand Down
Expand Up @@ -51,7 +51,7 @@
$file->name,
lang('files.'.$file->type),
$file->filename,
date("d/m/Y \a\\t g.i a", $file->date_added),
format_date($file->date_added),
$download .' | '. $edit .' | '. $delete
);
}
Expand Down
2 changes: 1 addition & 1 deletion system/pyrocms/modules/files/views/admin/folders/index.php
Expand Up @@ -22,7 +22,7 @@
$this->table->add_row(
form_checkbox('action_to[]', $folder['id']),
$indent.$folder['name'],
date("d/m/Y \a\\t g.i a", $folder['date_added']),
format_date($folder['date_added']),
$edit .' | '. $delete
);
}
Expand Down
74 changes: 41 additions & 33 deletions system/pyrocms/modules/news/controllers/admin.php
Expand Up @@ -51,7 +51,7 @@ class Admin extends Admin_Controller
'rules' => 'trim|alpha'
),
array(
'field' => 'date',
'field' => 'created_on',
'label' => 'lang:news_date_label',
'rules' => 'trim|required'
),
Expand All @@ -64,11 +64,6 @@ class Admin extends Admin_Controller
'field' => 'created_on_minute',
'label' => 'lang:news_created_minute',
'rules' => 'trim|numeric|required'
),
array(
'field' => 'created_on_day',
'label' => 'lang:news_created_day',
'rules' => 'trim|numeric'
)
);

Expand Down Expand Up @@ -138,24 +133,27 @@ public function create()
$this->load->library('form_validation');

$this->form_validation->set_rules($this->validation_rules);
if ($this->form_validation->run())

if ($this->input->post('created_on'))
{
$date = $this->input->post('date');
$date = explode('/', $date);
$created_on = strtotime(sprintf('%s %s:%s', $this->input->post('created_on'), $this->input->post('created_on_hour'), $this->input->post('created_on_minute')));
}

else
{
$created_on = now();
}

if ($this->form_validation->run())
{
$id = $this->news_m->insert(array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('slug'),
'category_id' => $this->input->post('category_id'),
'intro' => $this->input->post('intro'),
'body' => $this->input->post('body'),
'status' => $this->input->post('status'),
'created_on_hour' => $this->input->post('created_on_hour'),
'created_on_minute' => $this->input->post('created_on_minute'),
'created_on_day' => $date[0],
'created_on_month' => $date[1],
'created_on_year' => $date[2],
'created_on' => $created_on
));

if($id)
Expand All @@ -179,6 +177,7 @@ public function create()
{
$article->$field['field'] = set_value($field['field']);
}
$article->created_on = $created_on;
}

$this->template
Expand All @@ -197,16 +196,24 @@ public function create()
*/
public function edit($id = 0)
{
$date = $this->input->post('date');
$date = explode('/', $date);

$id OR redirect('admin/news');

$this->load->library('form_validation');
$this->form_validation->set_rules($this->validation_rules);

$article = $this->news_m->get($id);

// If we have a useful date, use it
if ($this->input->post('created_on'))
{
$created_on = strtotime(sprintf('%s %s:%s', $this->input->post('created_on'), $this->input->post('created_on_hour'), $this->input->post('created_on_minute')));
}

else
{
$created_on = $article->created_on;
}

$this->id = $article->id;

if ($this->form_validation->run())
Expand All @@ -218,27 +225,23 @@ public function edit($id = 0)
'intro' => $this->input->post('intro'),
'body' => $this->input->post('body'),
'status' => $this->input->post('status'),
'created_on_hour' => $this->input->post('created_on_hour'),
'created_on_minute' => $this->input->post('created_on_minute'),
'created_on_day' => $date[0],
'created_on_month' => $date[1],
'created_on_year' => $date[2],
'created_on' => $created_on
));

if ($result)
{
$this->session->set_flashdata(array('success'=> sprintf($this->lang->line('news_edit_success'), $this->input->post('title'))));

// The twitter module is here, and enabled!
if ($this->settings->item('twitter_news') == 1 && ($article->status != 'live' && $this->input->post('status') == 'live'))
{
$url = shorten_url('news/'.$date[2].'/'.str_pad($date[1], 2, '0', STR_PAD_LEFT).'/'.url_title($this->input->post('title')));
$this->load->model('twitter/twitter_m');
if ( ! $this->twitter_m->update(sprintf($this->lang->line('news_twitter_posted'), $this->input->post('title'), $url)))
{
$this->session->set_flashdata('error', lang('news_twitter_error') . ": " . $this->twitter->last_error['error']);
}
}
// if ($this->settings->item('twitter_news') == 1 && ($article->status != 'live' && $this->input->post('status') == 'live'))
// {
// $url = shorten_url('news/'.$date[2].'/'.str_pad($date[1], 2, '0', STR_PAD_LEFT).'/'.url_title($this->input->post('title')));
// $this->load->model('twitter/twitter_m');
// if ( ! $this->twitter_m->update(sprintf($this->lang->line('news_twitter_posted'), $this->input->post('title'), $url)))
// {
// $this->session->set_flashdata('error', lang('news_twitter_error') . ": " . $this->twitter->last_error['error']);
// }
// }
// End twitter code
}

Expand All @@ -256,8 +259,13 @@ public function edit($id = 0)
// Go through all the known fields and get the post values
foreach(array_keys($this->validation_rules) as $field)
{
if (isset($_POST[$field])) $article->$field = $this->form_validation->$field;
if (isset($_POST[$field]))
{
$article->$field = $this->form_validation->$field;
}
}

$article->created_on = $created_on;

// Load WYSIWYG editor
$this->template
Expand Down

2 comments on commit f701f80

@marcoscoelho
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philsturgeon
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. It was installing clean then trying to run through migrations, which is not good! I have patched this for now and I will code it up to automatically install to the newest migration in the future.

Please sign in to comment.