Wednesday, May 21, 2014

Rating stars

I was looking for a simple way to make rating stars, I found this post that is a very simple way to do it, but I didn't know what to make when there was already a rating that I want to show.

So I made this script, actually it will be part of a wordpress plugin, I hope it is useful for you.

Monday, December 17, 2012

Modificating validation rules in the CakePHP Models

There are many cases in which you need to add, modify or remove some validation rules that has already been set in the model. I was looking around how to do it and I found a good way to do it using the beforeValidate() callback form the model.

In my case I needed to save mora than one type of user, in one of them I didn't need to save the email address, but in the other case I did need it. This was the validation rule:

 'email' => array(  
  'rule'  => array('email', true),  
  'message' => 'Please supply a valid email address.'  

So what I did was to set this in the model:

 public function beforeValidate($options = array()) {  
  if (($this->data['User']['group_id'] == STUDENT) && empty($this->data['User']['email'])){  

With this I got to tell what type of user was being saved and if it was a "STUDENT" type of user and the email field was empty the rule for email was removed.

I hope you can use this.

Friday, December 3, 2010

Protovis: Placing the Visualizations

If you want to place the visualization inside a div or any other container, there are two ways to do it(or at least I know two ways), but only one always work.

The first is to place the script tags where you build and render the visualization inside the div
tags, something like this:


The other way, an this always work is to specify where it will be shown, something like this:

    var vis = new pv.Panel()

So It will be shown inside the div you specified.

Hope this helps you.

Wednesday, August 4, 2010

An useful shell script to use diff in a directory

Hi I just made this script to use diff in two directories, it fetch the files in the directories and diff them, if it finds a subdirectory it goes in and diff the files inside, I hope you use it and make it better.

You have to name the file and place it in your home, sorry this is my first script and I dont know how to use paths, there are 3 parameters.

To call the script there are 3 parameters:

sh original_directory copy_directory diff_files_firecory


sh my_project my_copy different_files


for i in $(ls $1)
    if [ -f "$1/$i" ];then
 diff $1/$i $2/$i > $filename
 filesize=$(wc -c < $filename)
        if [ $filesize = 0 ];then
            rm $filename
 mkdir $3/$i
 sh $HOME/ $1/$i $2/$i $3/$i

Tuesday, June 1, 2010

Using ajax or another layout

Sometimes we want to render a view with the ajax layout, but only when it is been showed because an ajax request. I used to make two different views, and some times two different actions. But I realized that there is a component that help us tell when the call comes from an ajax request.

The component is the RequestHandler, to use it you have to set it on the controller, like this:
class ExamplesController extends AppController{
   var $components = array('RequestHandler');
and in the action we want to tell if it is an ajax call we have to set:
function myaction(){
   $this->layout = $this->RequestHandler->isAjax()?'ajax':'default';
so if it is an AJAX call the layaout that will be used is the ajax layout. By the way, this is a layout that comes with CakePHP, you don't need to create it. And you also can use any other layout you create.

Thursday, May 27, 2010

Internationalizating CakePHP Sites

Making an app easy to translate and could show it in different languages is a simple task in CakePHP, CakePHP brings a tool and a set of functions that help the developer on this job

The first that has to be done is to use the function __() when writing strings that we want to be translate. this function has the next params:

        __($stringToTransalte, $boolean);
        $stringToTranslate: is the string we want to be translated
        $boolean: indicate whether the string is only returned or also echo, false to echo the string

Thursday, May 20, 2010

Hello World

On this blog I will post all the solutions I found when dealing with CakePHP, Javascrit, PHP, Prototype and any other stuff on my daily programming.

I work making websites for different purposes, usually I use CakePHP, but some times I have to use Drupal, Moodle or pure PHP. An in a next project I'll have to use Flash and Flex, so I will deal with them soon.

I hope you find this blog useful.