Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3. /*
  4.  * voir http://chromero.blogspot.fr/search/label/jQueryTable
  5.  */
  6. try {
  7.     session_start();
  8.     if (!array_key_exists('token', $_SESSION) || $_SESSION['token'] != $_REQUEST['token']) {
  9.         die("invalid token");
  10.     }
  11.     include('db.php');
  12.  
  13.     $pdo = $pdo = new PDO(
  14.             "mysql:host=$host;dbname=$database", $user, $pass);
  15.     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.     $pdo->query("SET NAMES 'UTF8'");
  17.  
  18.     $fields = '*';
  19.     $filter = array();
  20.     if(array_key_exists('filter', $_REQUEST)) {
  21.         $temp = json_decode($_REQUEST['filter'], true);
  22.         $filter = $temp['content'];
  23.     }
  24.     $sort = array_key_exists('sort', $_REQUEST) ? $_REQUEST['sort'] : '';
  25.     $limit = '1,30';
  26.     if (array_key_exists('limit', $_REQUEST)) {
  27.         $limit = $_REQUEST['limit'];
  28.     }
  29.     $sql = new SQL($pdo, 'maps_ville');
  30.  
  31.     $sql->setFields($fields)->setFilter($filter)->setSort($sort)->setLimit($limit);
  32.     if ($_REQUEST['action'] == 'columns') {
  33.         $ret = $sql->getColumns();
  34.     } else {
  35.         $stmt = $sql->prepare($_REQUEST['action'] == 'count');
  36.         $sql->execute();
  37.         $res = $stmt->fetchAll(PDO::FETCH_NUM);
  38.         //print_r($res);
  39.         if ($_REQUEST['action'] == 'count') {
  40.             $ret = $res[0][0];
  41.         } else {
  42.             $ret = $res;    
  43.         }
  44.     }
  45.     print(json_encode($ret));
  46. } catch (Exception $e) {
  47.     print($sql->getSql());
  48.     print_r($sql->filter);
  49.     print_r($sql->params);
  50.     print '<pre>' . $e->getMessage();
  51.     print $e->getTraceAsString();
  52.     print('</pre>');
  53. }
  54.  
  55. class SQL {
  56.  
  57.     private $pdo;
  58.     private $table;
  59.     private $fields = '*';
  60.     public $filter = array();
  61.     private $sort = '';
  62.     private $limit;
  63.     public $params = array();
  64.  
  65.     function __construct($pdo, $table) {
  66.         $this->pdo = $pdo;
  67.         $this->table = $table;
  68.     }
  69.  
  70.     public function getLimit() {
  71.         return $this->limit;
  72.     }
  73.  
  74.     public function setLimit($limit) {
  75.         $this->limit = $limit;
  76.     }
  77.  
  78.     public function getFields() {
  79.         return $this->fields;
  80.     }
  81.  
  82.     public function setFields($fields) {
  83.         $this->fields = $fields;
  84.         return $this;
  85.     }
  86.  
  87.     public function getFilter() {
  88.         return $this->filter;
  89.     }
  90.  
  91.     public function setFilter($filter) {
  92.         $this->filter = $filter;
  93.         return $this;
  94.     }
  95.  
  96.     public function getSort() {
  97.         return $this->sort;
  98.     }
  99.  
  100.     public function setSort($sort) {
  101.         $this->sort = $sort;
  102.         return $this;
  103.     }
  104.  
  105.     public function getSql($count = false) {
  106.         //print("count:$count ".$_REQUEST['action']);
  107.         $fields = ($count) ? 'count(*)' : $this->fields;
  108.         $sql = "SELECT $fields FROM " . $this->table;
  109.         $cnt = count($this->filter);
  110.         // {field:'',op:'',value:''}
  111.  
  112.         $where = '';
  113.         $sep = ' WHERE ';
  114.         for ($index = 0; $index < $cnt; $index++) {
  115.             $field = $this->filter[$index]['field'];
  116.             $where .= $where . $sep . $field . ' ' . $this->filter[$index]['op'] . ' :' . $field;
  117.             $this->params[':' . $field] = $this->filter[$index]['value'];
  118.             $sep = ' AND ';
  119.         }
  120.         $sort = '';
  121.         $limit = '';
  122.  
  123.         if (!$count) {
  124.             if (!empty($this->sort)) {
  125.                 $sort=  str_replace(array("';"), array('  '), $this->sort);
  126.                 $sort = ' ORDER BY ' . $sort;
  127.             }
  128.             if (!empty($this->limit)) {
  129.                 $limites = explode(',', $this->limit);
  130.                 // par sécurité on converti les limites en numérique
  131.                 $lims = array_map(function($a){return 0+$a;}, $limites);
  132.                 $limit = ' LIMIT ' . implode(',', $lims);
  133.             }
  134.         }
  135.         return $sql . $where . $sort . $limit;
  136.     }
  137.  
  138.     public function prepare($count = false) {
  139.         $this->stmt = $this->pdo->prepare($this->getSql($count));
  140.         return $this->stmt;
  141.     }
  142.  
  143.     public function execute() {
  144.         return $this->stmt->execute($this->params);
  145.     }
  146.  
  147.     public function getColumns() {
  148.  
  149.         $this->setLimit('1');
  150.         $ret = array();
  151.         $this->prepare();
  152.         $this->execute();
  153.         //$this->stmt->fetch();
  154.         $colCount = $this->stmt->columnCount();
  155.         for ($i = 0; $i < $colCount; $i++) {
  156.             $tab = $this->stmt->getColumnMeta($i);
  157.             $ret[] = array('id' => $tab['name'], 'label' => $tab['name'], 'type' => $tab['native_type']);
  158.         }
  159.         return $ret;
  160.     }
  161.  
  162. }