Thursday, January 22, 2015

Generic dao class for wordpress

metaDefinition = $metaDefinition;
        $this->tableName = $tableName;
    }
    /**
     * Insert a new row
     * @global type $wpdb
     * @param type $pairs
     * @return type last inserted id
     */
    function insertRow($pairs){
            global $wpdb;          
            $prototypes = array();
             foreach ($pairs as $colName => $dataType) {
                 $prototypes[] = $this->metaDefinition[$colName];
             }
            $wpdb->insert($this->tableName, $pairs,$prototypes);
            return $wpdb->insert_id;
    }
    /**
     * Select all rows
     * @global type $wpdb
     * @param type $orderingExpr
     * @param type $limit
     * @return type
     */
    function  selectAllRows($orderingExpr=" ",$limit=""){
            global $wpdb;
            $sql = "select * from $this->tableName ".$orderingExpr.$limit;
            return $wpdb->get_results($sql);
    }
    /**
     * Query table
     * @global type $wpdb
     * @param type $where
     * @param type $orderingExpr
     * @param type $limit
     * @return type
     */
    function  selectRows($where,$orderingExpr=" ",$limit=""){
            global $wpdb;
            $sql = "select * from $this->tableName where ".$where.' '.$orderingExpr.' '.$limit;
            return $wpdb->get_results($sql);
    }
    /**
     * Select rows count
     * @global type $wpdb
     * @return type
     */
    function selectRowsCount(){
            global $wpdb;
            $sql = "select count(*)as count from $this->tableName ";
            return $wpdb->get_results($sql);
    }
    /**
     * Delete row
     * @global type $wpdb
     * @param type $pairs array of paris (columns, new values)
     * @param type $where
     * @param type $whereFormat
     * @return type
     */
    function updateRow($pairs,$where,$whereFormat=array("%d")){
            global $wpdb;
             $prototypes = array();
             foreach ($pairs as $colName => $dataType) {
                 $prototypes[] = $this->metaDefinition[$colName];
             }
             return $wpdb->update( $this->tableName, $pairs, $where, $prototypes, $whereFormat );
    }
        
    function deleteRow($where,$whereFormat=array("%d")){
            global $wpdb;
            return $wpdb->delete( $this->tableName, $where,$whereFormat);
    }
    

}
?>

An implementation of this genric class is this little code:
Supposing that we have this table :
CREATE  TABLE IF NOT EXISTS `wordpress_categ_type_poste` (
  `ctgtp_id` INT NOT NULL AUTO_INCREMENT ,
  `ctgtp_lib` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`ctgtp_id`) )
ENGINE = InnoDB;
then an extension of this class can be
'%d','ctgtp_lib'=>'%s'),$wpdb->prefix . "categ_type_poste");
    }
  }
?>
once you have finished that class, it's recommended to build your service class exposing some or all functions of the dao class.

Friday, January 9, 2015

Jquery get longitude and latitude from address using google api

A little js code to retrieve long and lat from adress using google api
    function getLtLgFromAdresse(address,apiKey){
         var lng;
         var lt;
         jQuery.ajax({
                    type: "GET",
                    url: "https://maps.googleapis.com/maps/api/geocode/xml?address="+address+"&key="+apiKey,
                    dataType: "xml",
                    success: function(xml) {      
                          if(jQuery(xml).find('status').text() == 'OK'){
                              var locationNode = jQuery(xml).find('result:first'). find('geometry').find('location'); 
                              lng = locationNode.find('lng').text();
                              lt =  locationNode.find('lat').text();
                              alert("lng: "+lng+" lat:"+lt) 
                          }    else{
                                alert('Error!');          
                          }
                       
                    }                
            });
    }
apiKey is your google key, read this for more info
https://developers.google.com/maps/documentation/geocoding/

Note that here we show only the first occurence "using the :first near to the selector result.

Friday, January 2, 2015

Primefaces responsive theme

In order to render a mobile content for primefaces framework bases web application:
just follow these 2 steps:
  1. in faces config.xmln add

      
         
           
             org.primefaces.mobile.application.MobileNavigationHandler
           
         
         
         PRIMEFACES_MOBILE
    "
  2. at the top of your xhtml or jsf or ... whatever extesion choosen for files that will be processed by jsf servlet  add this taglib:
    xmlns:pm="http://primefaces.org/mobile
    "
pm taglib offers many possibilities such as view pagination like :
 
   
 
  

to define inside the same view many pages