Comments on my first CI application (Calendar/event) - El Forum - 07-31-2007
[eluser]tobben[/eluser]
Hi,
first I must give my respect to the guys behind this framework. I really love it so far.
Second: the topic issue. I've just puzzled together something that atleast looks like a event calendar.
It works, _but_ I want to get some feedback on the structure and inprovments for the logic, so I can go further in the learning process.
// THE CONTROLLER (kalender.php)
Code: class Kalender extends Controller {
// Constuctor
function __construct()
{
parent::Controller();
$this->load->library('calendar');
$this->load->model('kalender_model');
}
// index
function index()
{
redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location');
}
function dato()
{
// Grab some vars
$cal_year = $this->uri->segment(3);
$cal_month = $this->uri->segment(4);
$cal_day = $this->uri->segment(5);
// Check if is numeric
if(!empty($cal_year) && ctype_digit($cal_year) == FALSE)
{
redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location');
}
if(!empty($cal_month) && ctype_digit($cal_month) == FALSE)
{
redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location');
}
if(!empty($cal_day) && ctype_digit($cal_day) == FALSE)
{
redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location');
}
// Redirect if empty vars
if(!$cal_year || !$cal_month)
{
redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location');
}
// Setup calendar
$prefs = array (
'start_day' => 'monday',
'month_type' => 'long',
'day_type' => 'abr',
'show_next_prev' => TRUE,
'next_prev_url' => base_url() . 'kalender/dato/'
);
$this->calendar->initialize($prefs);
// Say hello to the models
$data['event_data'] = $this->kalender_model->get_events($cal_year, $cal_month);
// If day isset, get day info
if($cal_day)
{
$data['event_day_data'] = $this->kalender_model->get_day($cal_year, $cal_month, $cal_day);
}
// Tell it to the viewer
$this->load->view('kalender_view', $data);
}
}
// THE MODEL (kalender_model.php)
Code: class Kalender_model extends Model {
// Constuctor
function __construct() {
parent::Model();
}
function get_events($year, $month) {
$first_day = mdate('%Y-%m-%d', mktime(0,0,0,$month,1,$year));
$last_day = mdate('%Y-%m-%d', mktime(0,0,0,$month,days_in_month($month, $year),$year));
$this->db->where('event_date >', $first_day);
$this->db->where('event_date <', $last_day);
$query = $this->db->get('kalender');
if ($query->num_rows() > 0)
{
foreach($query->result() as $k => $row)
{
$key = mdate('%j', mysql_to_unix($row->event_date));
$data_array[$key] = base_url() . 'kalender/dato/' . $year . '/' . $month . '/' . str_pad($key, 2, '0', STR_PAD_LEFT);
}
return $data_array;
}
}
function get_day($year, $month, $day) {
$first_day = mdate('%Y-%m-%d', mktime(0,0,0,$month,1,$year));
$last_day = mdate('%Y-%m-%d', mktime(0,0,0,$month,days_in_month($month, $year),$year));
$current_day = mdate('%Y-%m-%d', mktime(0,0,0,$month,$day,$year));
$this->db->where('event_date =', $current_day);
$query = $this->db->get('kalender');
if ($query->num_rows() > 0)
{
foreach($query->result() as $k => $row)
{
$key = mdate('%d', mysql_to_unix($row->event_date));
$data_array[$k]['dato'] = base_url() . 'kalender/dato/' . $year . '/' . $month . '/' . $key;
$data_array[$k]['body'] = $row->body;
$data_array[$k]['title'] = $row->title;
}
return $data_array;
}
}
}
// THE VIEW (kalender_view.php)
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="index,follow" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<title>Title</title>
</head>
<body>
<div id="container">
<?php
echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4), $event_data);
if($this->uri->segment(5)){
foreach($event_day_data as $value) {
echo '<h2>' . $value['title'] . '</h2>';
echo '<p>' . $value['body'] . '</p>';
}
}
?>
</div>
</body>
</html>
Well, that's all folks, and thanks for all the fish.
Comments on my first CI application (Calendar/event) - El Forum - 07-31-2007
[eluser]kirkaracha[/eluser]
Instead of repeating this: Code: redirect('kalender/dato/'. date('Y') . '/' . date('m') . '/','location')
Would it be easier to make the redirect destination a variable? You could do something like this:Code: var $redirect_destination = 'kalender/dato/'. date('Y') . '/' . date('m') . '/';
Then:Code: redirect($redirect_destination,'location')
Comments on my first CI application (Calendar/event) - El Forum - 12-16-2007
[eluser]Déjà Vu[/eluser]
I am new to CI, and I wanna implement this app on my site.
Y have some questions for this application.
How can I setup the database for this calendar?
How can I do a page for add events?
Can I see the links rendered in a calendar?, or this app only show me the events when are called from URI.
It could be great a wiki entry for this app.
Thanks in advance
|