Tag Archives: Example

Check for existing children behaviour

In some of my projects I had a requirement to prevent deletion of the records, which had children. That’s why I create a small behaviour which checks these cases and prevent this.

Add this code in /app/models/behaviours/has_children.php

<?php
/**
 * Prevent deletion if child record found
 *
 * @author    Nik Chankov
 * @url    http://nik.chankov.net
 */

class HasChildrenBehavior extends ModelBehavior {
    /**
     * Empty Setup Function
    */

    function setup(&$model) {
        $this->model = $model;
    }
   
    /**
     * Run the check and cancel the deletion if child is found
     * @access public
     */

    function beforeDelete(){
        if(isset($this->model->hasMany)){
            foreach($this->model->hasMany as $key=>$value){
                $childRecords = $this->model->{$key}->find('count', array('conditions'=>array($value['foreignKey']=>$this->model->id)));
                if($childRecords > 0){
                    return false;
                }
            }
        }
        //Checking habtm relation as well, thanks to Zoltan
        if(isset($this->model->hasAndBelongsToMany)){
            foreach($this->model->hasAndBelongsToMany as $key=>$value){
                $childRecords = $this->model->{$key}->find('count', array('conditions'=>array($value['foreignKey']=>$this->model->id)));
                if($childRecords > 0){
                    return false;
                }
            }
        }
        return true;
    }
}
?>

Continue reading

Advanced Datepicker helper for CakePHP

This is a really old post. I’ve wrote a new one which you can find it here


I am using this part of code from long time ago, but it wasn’t in a separate helper. It was part from one not very good written Auth component/helper mash up which I already not using. I fact the default Date input of CakePHP Framework is ugly and not user friendly at all. The drop-downs are good because the format is clear and users cannot mess it, but if you thinking for the user convenience, then you should know that it’s 3 times easier just to type the date in a field or to pick it from a Calendar pop up than selecting from 3 drop-downs.
Continue reading

Extended Autocomplete Helper

When I started with CakePHP, my first project includes also Autocomplete functionality for some fields. Of course I start using default Ajax Helper provided from the library. The main problem was that in most of the cases I needed the ID of the string which I searched. The best example is the Country list which is very easy to be accessed with Autocomplete, but instead to messing up with strings as usual developer I wanted to have the ID of the specified country. I used some kind of hack by adding callback function onComplete, which checks the selection in the Autocomplete field, and by this selection set hidden ID field, but as you can imagine this is not the best choice, first of all because it’s a hack – you rely on a sting which already sound as a hack /imagine if you are searching in a list there are some duplicated entries – examples are too many/ and second if you rely on callback this mean that you need another second or two for the second responce. Anyway, by giving this examples just wanted to convince you once again that Autocomplete helper with Key and Value related are really necessary in the projects /or at least that’s what I am thinking./

Well so far the default Autocomplete helper doesn’t provide such functionality. That’s why I created this helper which solves this problem partly. Why partly? You will see the answer of this in Strict Autocomplete with Scriptaculous (Part II)
Continue reading

Strict Autocomplete with Scriptaculous (Part II)

These days when I showed my example to some friends I realized that in fact even the second example had potential bugs. In fact not a bug, but missing functionality which could mislead the user while using the component.

What I found: When the user prefer to type instead of selecting the value from the list, then it’s possible to have “nothing selected” in ID field, but in fact the user have feeling that he select the correct value. This is extremely valid when the options are short and typing 3-4 chars which in fact is more convenient to type instead of selecting.
Continue reading

Instant and Lightbox Together

These days I saw an article about Instant Javascript Library. I was surprised how easy is to build eye-candy gallery looking like real album photos. The usage of gallery is very easy – just include the script file and add class of every image and that’s it. Of course there are many customizations and extras, but I am leaving this to your imagination.
Continue reading

Autocomplete with strict key=>value pair with Scriptaculous (Part I)

I am using Scriptaculous for Autocomplete control, but in the example provided there is no way to select strict value.

Let me explain what I mean: Go to autocomplete example and try to select an entry in first group of fields. Well everything looking fine.
Then try to modify the entry, for example remove the last char from the field. and press somewhere on the web page /with the mouse/. Well as you can see this didn’t clear the value from ID field and in the real example, when the ID field is hidden, users could have wrong impression that the field is selected, they change it, but their change is not applied … and the complication occur.
Continue reading