Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3775 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
3671 efrain 6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Paginator\Paginator;
8
use LeadersLinked\Mapper\Common\MapperCommon;
1 www 9
use LeadersLinked\Model\Post;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Db\ResultSet\HydratingResultSet;
12
use Laminas\Paginator\Adapter\DbSelect;
4778 efrain 13
use Laminas\Db\Sql\Expression;
1 www 14
 
3671 efrain 15
class PostMapper extends MapperCommon {
16
 
1 www 17
    const _TABLE = 'tbl_posts';
18
 
3671 efrain 19
 
1 www 20
    /**
21
     *
22
     * @var PostMapper
23
     */
24
    private static $_instance;
25
 
26
    /**
27
     *
28
     * @param AdapterInterface $adapter
29
     */
30
    private function __construct($adapter)
31
    {
32
        parent::__construct($adapter);
33
    }
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @return \LeadersLinked\Mapper\PostMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new PostMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
3671 efrain 49
     *
1 www 50
     * @return Post[]
51
     */
52
    public function fetchAll()
53
    {
3298 efrain 54
        $prototype = new Post();
1 www 55
        $select = $this->sql->select(self::_TABLE);
56
 
57
        return $this->executeFetchAllObject($select, $prototype);
58
    }
59
 
60
    /**
61
     *
62
     * @return Post[]
63
     */
3454 efrain 64
    public function fetchAllActive()
1 www 65
    {
3298 efrain 66
        $prototype = new Post();
1 www 67
        $select = $this->sql->select(self::_TABLE);
68
        $select->where->equalTo('status', Post::STATUS_ACTIVE);
69
        $select->order('date DESC');
70
 
71
 
72
        //echo $select->getSqlString($this->adapter->platform);
73
 
74
        return $this->executeFetchAllObject($select, $prototype);
75
    }
76
 
3639 efrain 77
 
78
 
1 www 79
    /**
80
     *
3639 efrain 81
     * @param int $network_id
82
     * @return Post[]
83
     */
84
    public function fetchAllActiveByNetworkId($network_id)
85
    {
86
        $prototype = new Post();
87
        $select = $this->sql->select(self::_TABLE);
3671 efrain 88
        $select->where->equalTo('network_id', $network_id);
3639 efrain 89
        $select->where->equalTo('status', Post::STATUS_ACTIVE);
90
        $select->order('date DESC');
91
 
92
 
93
        //echo $select->getSqlString($this->adapter->platform);
94
 
95
        return $this->executeFetchAllObject($select, $prototype);
96
 
1 www 97
    }
3671 efrain 98
        /**
99
         *
100
         * @param int $id
101
         * @return Post
102
         */
103
        public function fetchOne($id)
104
        {
105
            $prototype = new Post();
106
            $select = $this->sql->select(self::_TABLE);
107
            $select->where->equalTo('id', $id);
108
 
109
            return $this->executeFetchOneObject($select, $prototype);
110
        }
1 www 111
 
3639 efrain 112
 
3671 efrain 113
        /**
114
         *
115
         * @param string $uuid
116
         * @return Post
117
         */
118
        public function fetchOneByUuid($uuid)
119
        {
120
            $prototype = new Post();
121
            $select = $this->sql->select(self::_TABLE);
122
            $select->where->equalTo('uuid', $uuid);
123
 
124
            return $this->executeFetchOneObject($select, $prototype);
125
        }
1 www 126
 
3775 efrain 127
        /**
128
         *
129
         * @param string $uuid
130
         * @return Post
131
         */
132
        public function fetchOneByUuidAnyStatus($uuid)
133
        {
134
            $prototype = new Post();
135
            $select = $this->sql->select(self::_TABLE);
136
            $select->where->equalTo('uuid', $uuid);
137
 
138
            return $this->executeFetchOneObject($select, $prototype);
139
        }
140
 
3671 efrain 141
 
1 www 142
 
3671 efrain 143
        /**
144
         *
145
         * @param string $uuid
146
         * @param int $network_id
147
         * @return Post
148
         */
149
        public function fetchOneByUuidAndNetworkId($uuid, $network_id)
150
        {
151
            $prototype = new Post();
152
            $select = $this->sql->select(self::_TABLE);
153
            $select->where->equalTo('uuid', $uuid);
154
            $select->where->equalTo('network_id', $network_id);
155
 
156
            return $this->executeFetchOneObject($select, $prototype);
1 www 157
        }
158
 
159
 
3671 efrain 160
        /**
161
         *
162
         * @param Post $post
163
         * @return boolean
164
         */
165
        public function insert($post)
166
        {
167
            $hydrator = new ObjectPropertyHydrator();
168
            $values = $hydrator->extract($post);
169
            $values = $this->removeEmpty($values);
170
 
171
            $insert = $this->sql->insert(self::_TABLE);
172
            $insert->values($values);
173
 
174
            $result = $this->executeInsert($insert);
175
            if($result) {
176
                $post->id = $this->lastInsertId;
177
            }
178
 
179
            return $result;
180
 
181
        }
1 www 182
 
3671 efrain 183
        /**
184
         *
185
         * @param Post $post
186
         * @return boolean
187
         */
188
        public function update($post)
189
        {
190
            $hydrator = new ObjectPropertyHydrator();
191
            $values = $hydrator->extract($post);
192
            $values = $this->removeEmpty($values);
193
 
194
            $update = $this->sql->update(self::_TABLE);
195
            $update->set($values);
196
            $update->where->equalTo('id', $post->id);
197
 
198
            return $this->executeUpdate($update);
199
        }
1 www 200
 
3671 efrain 201
        /**
202
         *
203
         * @param Post $post
204
         * @return boolean
205
         */
206
        public function delete($post)
207
        {
208
            $delete = $this->sql->delete(self::_TABLE);
209
            $delete->where->equalTo('id', $post->id);
210
 
211
            return $this->executeDelete($delete);
212
 
213
        }
1 www 214
 
3671 efrain 215
        /**
216
         *
217
         * @param string $search
218
         * @param int $network_id
219
         * @param int $page
220
         * @param int $records_per_page
221
         * @param string $order_field
222
         * @param string $order_direction
223
         * @return Paginator
224
         */
225
        public function fetchAllDataTable($search, $network_id, $page = 1, $records_per_page = 10, $order_field= 'title', $order_direction = 'ASC')
226
        {
227
            $prototype = new Post();
228
            $select = $this->sql->select(self::_TABLE);
229
            $select->where->notEqualTo('status', Post::STATUS_DELETE);
230
            $select->where->equalTo('network_id', $network_id);
231
 
232
            if($search) {
233
                $select->like('title', '%' . $search . '%');
234
            }
235
            $select->order($order_field . ' ' . $order_direction);
236
 
237
 
238
 
239
            $hydrator   = new ObjectPropertyHydrator();
240
            $resultset  = new HydratingResultSet($hydrator, $prototype);
241
 
242
            $adapter = new DbSelect($select, $this->sql, $resultset);
243
            $paginator = new Paginator($adapter);
244
            $paginator->setItemCountPerPage($records_per_page);
245
            $paginator->setCurrentPageNumber($page);
246
 
247
 
248
            return $paginator;
1 www 249
        }
250
 
4778 efrain 251
        /**
252
         *
253
         * @param int $feed_id
254
         * @return boolean
255
         */
256
        public function incTotalExternalShared($id)
257
        {
258
            $update = $this->sql->update(self::_TABLE);
259
            $update->set(['total_external_shared' => new Expression('total_external_shared + 1')]);
260
            $update->where->equalTo('id', $id);
261
 
262
            return $this->executeUpdate($update);
263
        }
264
 
265
        /**
266
         *
267
         * @param int $id
268
         * @return int
269
         */
270
        public function fetchTotalExternalShared($id)
271
        {
272
            $select = $this->sql->select(self::_TABLE);
273
            $select->columns(['total_external_shared']);
274
            $select->where->equalTo('id', $id);
275
 
276
            $record = $this->executeFetchOneArray($select);
277
            return $record['total_external_shared'];
278
        }
279
 
1 www 280
 
281
 
282
}