Archive
Cakephp 1.2.x: Distinct Select
Just a reminder for myself:
$foo = $this->$model->find('all', array('fields'=>array("DISTINCT {$model}.dnid")));
CakePHP 1.1.x style used to be:
$this->$model->findAll(null, "DISTINCT {$model}.city");
Javascript: change img url and anchors title onclick
Recently, was playing around basic javascript Dom functionality elements. The main task was to trigger some functionality via onclick event of JS, and change the view of anchor and img tags inside the DOM component.
For instance we have HTML looking like this:
<a href="javascript:void(0)" onclick="enable_foo()" id="dist_link" title='Enable'><img src="ruler.png" id="ruler_img"/></a>
For this case a basic JS script would look like:
function enable_foo(){ var link_text = document.getElementById('dist_link'); var my_title = link_text.getAttribute('title'); if(my_title == 'Enable'){ link_text.firstChild.data = 'Disable'; link_text.setAttribute('title','Disable'); document.getElementById('ruler_img').src = "ruller_enabled.png"; } else if(my_title == 'Disable'){ link_text.firstChild.data = 'Enable'; link_text.setAttribute('title','Enable'); document.getElementById('ruler_img').src = "ruller.png"; }
For avoiding the changes of the image, you can always fire up the title of anchor tag by removing the call for ‘ruler_img’ (with img tag inside anchor in HTML as well) tag and using:
link_text.firstChild.data = 'Enable'; //or link_text.firstChild.data = 'Disable';
CakePHP: jQuery styling of flash messages
CakePHP session flash messages is one of the most useful things for usability, and user notification. Trying to make it more fancy, I came to jQuery framework with its HighlightFade plugin.
Adding just a couple of lines, we can get nice effects, looking the same as Wordpress messages in Posting/Editing:
var $j = jQuery.noConflict(); //I'm using Prototype as well, so we don't need conflicts
$j(document).ready(function() {
if( $j("#flashMessage") ){
$j("#flashMessage").highlightFade({color:'#24F273',speed:2000, iterator:'exponential'});
}
});
Thus, next time you will pass $this->Session->setFlash('Foo');, your flash message with default
will be used with above HighLight effect.
CakePHP: named over url parameters passing
Playing around with CakePHP parameters passing through URL, I’ve noticed that using Paginator helper for listing entities mixes up named and url params in the URL.
For instance, classical paginator URL is:
http://domain.com/controller/action/page:number
which looks like:
Array(
[named] =>Array(
[page] => number
)
)
Any parameter used in the Paginator goes inside of the URL and doesn’t affect the behaviour (in my case, URL param is used for language switching), i.e. http://domain.com/controller/action/?param=foo/page:number
No matter what your $this->params['url']['param'] contains – it won’t reflect on the logic.
Thus, if we use another variable $this->params['named']['foo'], we can always access it in our viewers and utilize it in the $paginator variable, so our URL will look like:
http://domain.com/controller/action/page:number/foo:bar
Useful links:
Additional parameters in $paginator