Adding a third party class to CodeIgniter library

You can find many php classes at http://www.phpclasses.org/.
This time I’d like to add a new class called BBQQ calendar to library. I know that there is a calendar class for CodeIgniter, but this will show you how simple is to add a third party php class to CI.

Files

Download a zip file from http://www.phpclasses.org/browse/package/5743.html#download. You need to register and sign-in in order to see a download link. Unzip it somewhere in your desktop.
Open calendar.php and replace <?php with the following code at the top and save it as BBQQ_Calendar.php in application/libraries/ directory.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Controller

Copy, paste the following code and save it as calendar.php in controllers directory.

<?php
class calendar extends Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('BBQQ_Calendar');
    }

    function index()
    {

        $this->load->view('calendar_view');
    }
}

View

Copy, paste the following code and save it as calendar_view.php in views directory.
Please note that I added <base href=”<?=base_url();?>” /> in head section.
This allows us to link a css (and javascript if you are using) file with <link href=”css/example.css” media=”screen” rel=”stylesheet” type=”text/css” >

<!DOCTYPE html><html>
<head>
<title>BBQQ CALENDAR</title>
<base href="<?=base_url();?>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<link href="css/example.css" media="screen" rel="stylesheet" type="text/css" >
<body>
<h3>A plain calendar with all default settings</h3>
<?php
$cal = new BBQQ_Calendar();
$cal->render(0);
?>

<h3>A plain calenar with raw output, for geektool</h3>
<pre style="background:#333;width:420px;padding:12px;">
<?php
$cal->render(0, 1);
?>
</pre>

<h3>Customised Week Day Titles</h3>
<p>change sun/sat to sunday/saturday</p>
<?php
$cal->setWeekDayTitle(array(
    'sun' => 'Sunday',
    'sat' => 'Saturday'
));
$cal->render(0);
?>

<h3>Customised header</h3>
<?php
$cal->render(0, 0,
    '<tr><th colspan="7" style="background:#69c;">simple calendar</th></tr>' .
    '<tr><th>s</th><th>m</th><th>t</th><th>w</th><th>t</th><th>f</th><th>s</th></tr>'
);
?>

<h3>Hightlight Days With Customised Templates (Advanced Usage)</h3>
<?php
$days = array(
            // custom template for today
            $cal->getToday('j') => array(
                'template' => '<b>TODAY: :num</b>'
            ),
            // highlight day: 5 with custom template/scripts and wrapper tag
            5 => array(
                'template' => '<a href="event?id=event_:timestamp">:num</a>',
                'wrapper' => array(
                   'tag' => 'td',
                   'id'    => null,
                   'class' => 'cal-thu',
                   'format' => 'j F, Y',
                   'style' => 'color:red;background:yellow',
                   'onclick' => "alert('Title: ' + this.title);return false;",
                   'title' => ':title'
                )
            ),
            // highlight day: 12 with custom template and wrapper tag
            12 => array(
                'template' => '<a href="?id=event_:title">:num</a>',
                'wrapper' => array(
                   'tag' => 'td',
                   'id'    => null,
                   'class' => 'cal-thu',
                   'format' => 'j F, Y',
                   'style' => 'color:red;background:green',
                   'title' => 'some :title'
                )
            )
        );

$cal->highlightDays($days);
$cal->render(0);
?>
</body>
</html>

CSS

Create a css folder as the same level as sytem, application and img folder.
Find a file called example.css in the unzipped folder. Move this file to css folder.

Results

BBQQ-CALENDAR

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>