Réalisation de galerie/index avec Ajax/Datatable
[zf2.biz/galerie.git] / module / Galerie / src / Galerie / Model / GalerieInfoTable.php
1 <?php
2 namespace Galerie\Model;
3
4 use Zend\Db\Adapter\Adapter;
5 use Zend\Db\ResultSet\ResultSet;
6 use Zend\Db\TableGateway\TableGatewayInterface;
7 use Zend\Db\Sql\Sql;
8 use Zend\Db\Sql\Where;
9
10 use Custom\Model\Entity;
11
12 class GalerieInfoTable implements TableGatewayInterface
13 {
14
15     protected $adapter;
16     protected $resultSetPrototype;
17     protected $sql;
18
19     public function __construct(Adapter $adapter) {
20         // Gestion de l'adaptateur
21         if (!$adapter instanceof Adapter) {
22             throw new Exception\RuntimeException('GalerieInfoTable does not have an valid Adapter parameter');
23         }
24         $this->adapter = $adapter;
25
26         // Utilisation du patron de conception Prototype
27         // pour la création des objets ResultSet
28         $this->resultSetPrototype = new ResultSet();
29         $this->resultSetPrototype->setArrayObjectPrototype(
30             new GalerieInfo()
31         );
32
33         // Initialisation de l'outil de création de requête
34         $this->sql = new Sql($this->adapter, $this->getTable());
35     }
36
37
38     public function getTable()
39     {
40         return 'gallery'; // Table centrale de la requête
41     }
42
43
44     public function select($where = null, $order = null, $limit = null, $offset = null)
45     {
46         $select = $this->sql->select()
47             ->columns(array('id', 'name', 'description'))
48             ->join('user', 'gallery.id_user = user.id', array(
49                 'username' => new \Zend\Db\Sql\Expression("user.firstname || ' ' || user.lastname")
50             ))
51             ->join('photo', 'gallery.id = photo.id_gallery', array(
52                 'nb' => new \Zend\Db\Sql\Expression('count(photo.id)')
53             ))
54             ->group(array(
55                 'user.lastname',
56                 'user.firstname',
57                 'gallery.name'
58             ))
59             ->order(array(
60                 'user.lastname',
61                 'user.firstname',
62                 'gallery.name'
63             ));
64         if ($where) {
65             $select->where($where);
66         }
67         if ($order) {
68             $select->order($order);
69         }
70         if ($limit) {
71             $select->limit($limit);
72         }
73         if ($offset) {
74             $select->offset($offset);
75         }
76
77         // prepare and execute
78         $statement = $this->sql->prepareStatementForSqlObject($select);
79         $result = $statement->execute();
80
81         // build result set
82         $resultSet = clone $this->resultSetPrototype;
83         $resultSet->initialize($result);
84
85         return $resultSet;
86     }
87
88     public function insert($set) {
89         throw new \Exception('insert is not allowed');
90     }
91
92     public function update($set, $where = null) {
93         throw new \Exception('update is not allowed');
94     }
95
96     public function delete($where) {
97         throw new \Exception('delete is not allowed');
98     }
99
100     public function all()
101     {
102         return $this->select();
103     }
104
105     public function one($id)
106     {
107         if ($id === null) {
108             $row = null;
109         } else {
110             $row = $this->select(array('gallery.id' => (int) $id))->current();
111         }
112         if (!$row) {
113             throw new \Exception("cannot get row {id: {$id}} in table 'galerie'");
114         }
115         return $row;
116     }
117
118     public function any($id)
119     {
120         if ($id === null) {
121             $row = null;
122         } else {
123             $row = $this->select(array('gallery.id' => (int) $id))->current();
124         }
125         return $row;
126     }
127
128     public function all_by_user($id_user)
129     {
130         return $this->select(array('gallery.id_user' => (int) $id_user));
131     }
132
133
134
135
136     public function count_all()
137     {
138         $select = $this->sql->select()->columns(array(
139             'nb' => new \Zend\Db\Sql\Expression('count(gallery.id)')
140         ));
141
142         // prepare and execute
143         $statement = $this->sql->prepareStatementForSqlObject($select);
144         $result = $statement->execute()->current();
145
146         return $result['nb'];
147     }
148
149     public function getPartial($start, $length, $tri, $senstri, $filtre)
150     {
151         $where = new Where;
152         $where->like('gallery.name', "%{$filtre}%");
153         $where->or;
154         $where->like('gallery.description', "%{$filtre}%"); 
155
156         return $this->select($where, "{$tri} {$senstri}", $length, $start);
157     }
158
159
160
161
162 }