RSS
 

Heat in Cyprus

05 Jun

[Heat], salt lake in lady's mile

 

Ice case of sassuolo lake

03 Jun

More photos

 

Cyprus traffic jam avoidance

02 Jun

Omonia Area – we have 99% of dead-ends in Limassol. Enjoy the ride, preak!

 

Bobina @ club Nuovo, tonight

31 Mar

And just a small video

 

CakePHP, draggable/droppable in jQuery

27 Mar

Working on bunch of CRUD routines in CakePHP, I finally got tired of multiple checkboxes and decided to write a small helper for jQuery UI drag-n-drop functionality.

In response to Teknoid post, I’ve made couple of additional tweaks to dynamic select boxes, and got quite nice combination of selectboxes and dynamic lists.

The main requirement of the script was:

  1. On Selectbox value changed, return the list of related items (in #base-list div-element)
  2. Add drag/drop functionality to the base-list ul elements as well as add arrow-west/arrow-east addition/deletion of the elements
  3. After all the movements are over, to be able to save dragged values in CakePHP form to be saved.
  4. Result: here.
  5. Sample MySQL DB file: Here

The view/helpers/droppable.php looks like:

 tag
    */
    function droppableElements($outside = false) {
        $js_code = "";
 
        $js_code = '
                    var $baseDiv = $(\'#base-list\');
                    var $targetDiv = $(\'#target-list\');
 
                    $(\'li\', \'#baseUl\' ).draggable({
                        accept: \'#baseUl > li\',
                        revert: \'invalid\',
                        helper: \'clone\',
                        cursor: \'move\'
                    }); 
 
                    $(\'li\', \'#targetUl\' ).draggable({
                        accept: \'#targetUl > li\',
                        revert: \'invalid\',
                        helper: \'clone\',
                        cursor: \'move\'
                    }); 
 
                    $targetDiv.droppable({
                        accept: \'#baseUl > li\',
                        activeClass: \'ui-state-highlight\',
                        drop: function(ev, ui) {
                            moveItem(ui.draggable, $targetDiv);
                        }
                    });
 
                    $baseDiv.droppable({
                        accept: \'#targetUl > li\',
                        activeClass: \'ui-state-highlight\',
                        drop: function(ev,ui) {
                            moveItem(ui.draggable, $baseDiv);
                        }
                    });
 
                    $(\'#baseUl > li\').click(function(ev){
                        moveItemClicked($(this), $(this).parent());
                    });
 
                    $(\'#targetUl > li\').click(function(ev){
                        moveItemClicked($(this), $(this).parent());
                    });';
 
            if($outside) {
                $this->out .= $this->Javascript->codeBlock($js_code);
                return $this->out;
            } else {
                return $js_code;
            }
 
    }
 
    /*
    *   @var string $boxId - observed selectBox id element
    *   @var string $eventType - default 'change'
    *   @var string $jsonAction - method call, i.e '/departments/ajax_get_list/departments/company_id/'
    */
 
    function observeSelectBox($boxId, $eventType = 'change', $jsonAction) {
        $js_script = "";
 
        $js_script ='
          $(\'#'.$boxId.'\').live(\''.$eventType.'\', function(){
            if($(this).val().length != 0) {
            $.getJSON(\''.$jsonAction.'\',
                {searchParam: $(this).val() }, 
 
                function(items) {
                    $(\'.jq_list\').remove();
                    generateBaseList(items);
 
                '.$this->droppableElements().'
                }
            );
        }
        } );';
 
        $this->out .= $this->Javascript->codeBlock($js_script);
 
        return $this->out;
    }
 
}?>

For using so-called ajax_get_list method, I’ve written a small function in app/app_controller.php that get all the id,name list array based on couple of arguments:

In order to use this method, you should have a small layout in related views folder, that looks like:

<?php
    // somewhere in views/departments/ajax_get_list.ctp
   if(isset($items)) {
        echo $javascript->object($items);
 }?>

The view of ajax_get_list method will output serialized array of the method. For the initial view, you need to add 2 div-elements and one select box. Div elements have static id’s which are used by jQuery script to look-up (base-list, and target-list).
In our view, it should look like:

<?php  echo $droppable->observeSelectBox('company-id','change','/departments/ajax_get_list/departments/company_id/');?>
 
        <label> Load company departments: </label>
        <?php print $form->select('Company.id', $companies, null, array('id'=>'company-id'), 'Company Departments);?>
 
        <?php print $html->div('right block', $html->div('target-title', 'Container', array(), false),array('id'=>'target-list'),false);?> 
 
        <?php print $html->div(null,null, array('id'=>'base-list'));
?>

And that’s about it, once you finish dragging the li-elements to the target-list, and save the form, its values will appear in ['Container']['item_ids'] array where you can do whatever you want with it.
To make things simple, you can download a small sample of code of these files here, as well as database sample file.

 

Perl, from Excel to MySQL

10 Mar

One of the reasons I like Perl – it’s really fast on solving daily routines. The request was to get a list of shipping flags from one of the websites to Excel and paste it to the site’s database. Quick and dirty solution:

#!/usr/bin/perl
 
use strict;
use warnings;
use DBI;
use Spreadsheet::ParseExcel;
 
my $file = 'flags.xls';
 
my $xl_parser = Spreadsheet::ParseExcel->new();
 
my $workbook = $xl_parser->Parse($file);
my $worksheet = $workbook->worksheet('Sheet1');
 
my ($c_min,$c_max) = $worksheet->col_range();
my ($r_min,$r_max) = $worksheet->row_range();
 
my @flags;  # used for storing an array of flags;
 
for my $row($r_min..$r_max) {
 
    for my $col($c_min..$c_max) {
 
        my $cell = $worksheet->get_cell($row,$col);
        my $val = $cell->value;
 
        $val =~ s/^\s+(.*?)\s$//g;
 
        push @flags, $val;
        #some extras on different cells/etc/
 
    }   
}
 
my $dsn = "DBI:mysql:database=db_blah;host=host;port=blah_port";
my $dbh = DBI->connect( $dsn, 'usr_blah', 'pwd_blah') || die("Couldn't connect to database\n");
 
 
$dbh->do("INSERT INTO flags(name) VALUES(".$dbh->quote($_).")") for @flags;
 

CakePHP: remove tmp folder from svn control

01 Mar

We’d have to use propset command from SVN.

$#: cd /path/to/repository/cake_project
$#: svn delete app/tmp/*
$#: svn propset svn:ignore '*' tmp/

Now, after you commit the changes your tmp/files won’t be commited to the SVN repo.

 

service mysqld restart: no file or directory

05 Jan

rpm -qa | grep mysql (find out the version of mysql you got)Somehow managed to break mysqld file (wow! applauses).

Errors received from ‘mysql -u <username> -p <pass>’:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

On using ‘rpm -e mysql-server’ was getting:

error reading information on service mysqld: No such file or directory
error: %preun(mysql-server-3.23.58-4) scriptlet failed, exit status 1

The solution ended up in:

  • rpm -qa | grep mysql (find out the version of mysql you got)
  • Get the right version of you RPM
  • rpm -Uhv –force mysql-server-xx.x.rpm
  • service mysqld restart