Thursday, January 22, 2015

Generic dao class for wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!--php
 
defined('ABSPATH') or die("Your are not welcome");
 
class GenericDao{
    /**
     * $metadefintion is an array that contains pairs
     * key:colName val: data type (s,d,f)
     * Note 1.data Type must be accepted by wordpress
     *      2.Firdst item is Id and it is autoincrement
     * @var type
     */
    private $metaDefinition;
     
    /**
     * Name of the table
     * @var type
     */
    private $tableName;
     
    function __construct ($metaDefinition, $tableName ){
        $this--->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 :
1
2
3
4
5
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--php
 
defined('ABSPATH') or die("Your are not welcome");
 
require_once(dirname(__FILE__).'/generic-dao.php');
 
class CategTypePosteDao extends GenericDao{
         
    function __construct(){
        global $wpdb;
        parent::__construct(array('ctgtp_id'=-->'%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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

    1
    2
    3
    4
    5
    6
    7
    <!-- Primefaces mobile themes navigation handler -->
      <navigation-handler>
        org.primefaces.mobile.application.MobileNavigationHandler
      </navigation-handler>
     
    <!-- Instead of declaring render kit for each page, render kit is application scoped -->
    <default-render-kit-id>PRIMEFACES_MOBILE</default-render-kit-id>
    "
  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:
    "
pm taglib offers many possibilities such as view pagination like :
1
2
3
4
<pm:page id="page1">
 </pm:page>
<pm:page id="page2">
 </pm:page>

to define inside the same view many pages