It's quite difficult to copy-paste JavaScript for same thing again and again. That's why I've come up with Thickbox helper for CakePHP – as a result of a project which involved lot of thickboxes implementations. For those who don't know what it is be sure to check Thickbox jQuery Plugin.
To use it, just include this helper in your controller and Its implementation is very simple:
1. For inline content:
PHP:
-
<?
-
$thickbox->setProperties(array('id'=>'domId', 'height'=>'300', 'width'=>'334')); // set height, width and DOM ID
-
$thickbox->setPreviewContent('click me'); // the link which will trigger thickbox on click
-
$thickbox->setMainContent('<div>see it??</div>'); // the content which will be shown in thickbox
-
?>
2. For AJAX:
PHP:
Here's the helper:
PHP:
-
<?php
-
class ThickboxHelper extends AppHelper {
-
-
-
/**
-
* Set properties - DOM ID, Height and Width, Type of thickbox window - inline or ajax
-
*
-
* @param array $options
-
*/
-
{
-
{
-
$options['type'] = 'inline';
-
}
-
$this->options = $options;
-
}
-
-
function setPreviewContent($content)
-
{
-
$this->options['previewContent'] = $content;
-
}
-
-
function setMainContent($content)
-
{
-
$this->options['mainContent'] = $content;
-
}
-
-
{
-
}
-
-
function output()
-
{
-
if($type=='inline')
-
{
-
$href = '#TB_inline?';
-
$href .= '&inlineId='.$id;
-
}
-
elseif($type=='ajax')
-
{
-
$ajaxUrl = $this->Html->url($ajaxUrl);
-
$href = $ajaxUrl.'?';
-
}
-
-
{
-
$href .= '&height='.$height;
-
}
-
{
-
$href .= '&width='.$width;
-
}
-
-
-
$output = '<a class="thickbox" href="'.$href.'">'.$previewContent.'</a>';
-
-
if($type=='inline')
-
{
-
$output .= '<div id="'.$id.'" style="display:none;">'.$mainContent.'</div>';
-
}
-
-
-
return $output;
-
}
-
-
function beforeRender()
-
{
-
$out = $this->Html->css('/effects/css/thickbox.css').'<script src="'.$this->Html->url('/effects/js/thickbox-compressed.js').'"></script>';
-
$view =& ClassRegistry::getObject('view');
-
$view->addScript($out);
-
}
-
-
}
-
?>
You'll need to copy the thickbox files to /app/webroot/effects. You can keep it in any folder for that matter, but as our team is following plugins – it's made that way.
- Abhimanyu Grover
2 Responses to “Thickbox Helper for CakePHP”
Leave A Reply
on Sat, 14 March, 2009 at 9:56 pm
Great Helper Guys…one small problem i was hoping you guys could give me some insight on…after i use an ajax call to display a form with your helper, when the form is submitted and the thickbox closes, it redirects the original page to a different url. Any idea how i can keep it on the same page after the thickbox closes? Thanks in advance.
Regards,
Robert McWay
on Mon, 16 March, 2009 at 5:34 am
Robert, All you would want to do is prevent default action using JS/jQuery, it has nothing much to do with this helper.