This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Tuesday, February 12, 2008

Using the Coolest DHTML/Javascript Calendar in CakePHP

The helpers in CakePHP are great but some things aren't all that great. One of them is the form date helper. It is really annoying to have to click on 3 select box for every date you want to key in. So finally I tried looking around and found http://nik.chankov.net/2007/09/13/advanced-datepicker-helper-for-cakephp/. Followed the instruction on the page and it worked like a charm. Only problem is that apart from adding the helper in the controller, you also have to type in the lines to include the javascript in every view that uses it (that is at least the add and edit view). So I've changed the Helper a bit so that it would include the link automatically before it render.


<?php
/**
* Autocomplete Helper
*
* @author Nik Chankov
* @website http://nik.chankov.net
* @version 1.0.0
*
* @updated 2008-02-13
* @author Abdullah
* @website http://abdullahsolutions.com
* @changes Used helpers array. Also used beforeRender so that the javascripts and theme is automatically loaded
*/

class DatePickerHelper extends FormHelper {

var $format = '%Y-%m-%d';
var $helpers = array('Javascript','Html');

/**
*Setup the format if exist in Configure class
*/
function _setup(){
$format = Configure::read('DatePicker.format');
if($format != null){
$this->format = $format;
}
}

function beforeRender(){
echo $this->Javascript->link('jscalendar/calendar.js');
echo $this->Javascript->link('jscalendar/lang/calendar-en.js');
echo $this->Javascript->link('common.js');
echo $this->Html->css('../js/jscalendar/skins/aqua/theme');
}

/**
* The Main Function - picker
*
* @param string $field Name of the database field. Possible usage with Model.
* @param array $options Optional Array. Options are the same as in the usual text input field.
*/
function picker($fieldName, $options = array()) {
$this->_setup();
$this->setFormTag($fieldName);
$htmlAttributes = $this->domId($options);
$divOptions['class'] = 'date';
$options['type'] = 'text';
$options['div']['class'] = 'date';
$options['after'] = $this->Html->link($this->Html->image('../js/jscalendar/img.gif'), '#', array('onClick'=>"return showCalendar('".$htmlAttributes['id']."', '".$this->format."'); return false;"), null, false);

$output = $this->input($fieldName, $options);

return $output;
}

function flat($fieldName, $options = array()){
$this->_setup();
$this->setFormTag($fieldName);
$htmlAttributes = $this->domId($options);
$divOptions['class'] = 'date';
$options['type'] = 'hidden';
$options['div']['class'] = 'date';
$hoder = '
';
$output = $this->input($fieldName, $options).$hoder;

return $output;
}
}
?>

3 comments:

Anonymous said...

Under flat() the contents of $hoder are missing. Maybe it's because it contains script tags. The string can be found here:
http://nik.chankov.net/2007/09/13/advanced-datepicker-helper-for-cakephp/

ianemv said...

How can i fix the error on setFormTag on 1.1.x version?

I believe it should be working since setFormTag is working on html helper.

Anonymous said...

Vert easy, juz use: setEntity instead of setFormTag.