Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Paginator\Paginator;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use LeadersLinked\Model\Post;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Db\ResultSet\HydratingResultSet;
12
use Laminas\Paginator\Adapter\DbSelect;
13
use Laminas\Db\Sql\Expression;
14
 
15
class PostMapper extends MapperCommon {
16
 
17
    const _TABLE = 'tbl_posts';
18
 
19
 
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
    /**
49
     *
50
     * @return Post[]
51
     */
52
    public function fetchAll()
53
    {
54
        $prototype = new Post();
55
        $select = $this->sql->select(self::_TABLE);
56
 
57
        return $this->executeFetchAllObject($select, $prototype);
58
    }
59
 
60
    /**
61
     *
62
     * @return Post[]
63
     */
64
    public function fetchAllActive()
65
    {
66
        $prototype = new Post();
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
 
77
 
78
 
79
    /**
80
     *
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);
88
        $select->where->equalTo('network_id', $network_id);
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
 
97
    }
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
        }
111
 
112
 
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
        }
126
 
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
 
141
 
142
 
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);
157
        }
158
 
159
 
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
 
283 www 171
            if(empty($values['publish_on'])) {
172
                $values['publish_on'] = new Expression('CURRENT_DATE()');
173
            }
174
 
1 efrain 175
            $insert = $this->sql->insert(self::_TABLE);
176
            $insert->values($values);
177
 
178
            $result = $this->executeInsert($insert);
179
            if($result) {
180
                $post->id = $this->lastInsertId;
181
            }
182
 
183
            return $result;
184
 
185
        }
186
 
187
        /**
188
         *
189
         * @param Post $post
190
         * @return boolean
191
         */
192
        public function update($post)
193
        {
194
            $hydrator = new ObjectPropertyHydrator();
195
            $values = $hydrator->extract($post);
196
            $values = $this->removeEmpty($values);
197
 
283 www 198
            if(empty($values['publish_on'])) {
199
                $values['publish_on'] = new Expression('CURRENT_DATE()');
200
            }
201
 
1 efrain 202
            $update = $this->sql->update(self::_TABLE);
203
            $update->set($values);
204
            $update->where->equalTo('id', $post->id);
205
 
206
            return $this->executeUpdate($update);
207
        }
208
 
209
        /**
210
         *
211
         * @param Post $post
212
         * @return boolean
213
         */
214
        public function delete($post)
215
        {
216
            $delete = $this->sql->delete(self::_TABLE);
217
            $delete->where->equalTo('id', $post->id);
218
 
219
            return $this->executeDelete($delete);
220
 
221
        }
222
 
223
        /**
224
         *
225
         * @param string $search
226
         * @param int $network_id
227
         * @param int $page
228
         * @param int $records_per_page
229
         * @param string $order_field
230
         * @param string $order_direction
231
         * @return Paginator
232
         */
233
        public function fetchAllDataTable($search, $network_id, $page = 1, $records_per_page = 10, $order_field= 'title', $order_direction = 'ASC')
234
        {
235
            $prototype = new Post();
236
            $select = $this->sql->select(self::_TABLE);
237
            $select->where->notEqualTo('status', Post::STATUS_DELETE);
238
            $select->where->equalTo('network_id', $network_id);
239
 
240
            if($search) {
241
                $select->like('title', '%' . $search . '%');
242
            }
243
            $select->order($order_field . ' ' . $order_direction);
244
 
245
 
246
 
247
            $hydrator   = new ObjectPropertyHydrator();
248
            $resultset  = new HydratingResultSet($hydrator, $prototype);
249
 
250
            $adapter = new DbSelect($select, $this->sql, $resultset);
251
            $paginator = new Paginator($adapter);
252
            $paginator->setItemCountPerPage($records_per_page);
253
            $paginator->setCurrentPageNumber($page);
254
 
255
 
256
            return $paginator;
257
        }
258
 
259
        /**
260
         *
261
         * @param int $feed_id
262
         * @return boolean
263
         */
264
        public function incTotalExternalShared($id)
265
        {
266
            $update = $this->sql->update(self::_TABLE);
267
            $update->set(['total_external_shared' => new Expression('total_external_shared + 1')]);
268
            $update->where->equalTo('id', $id);
269
 
270
            return $this->executeUpdate($update);
271
        }
272
 
273
        /**
274
         *
275
         * @param int $id
276
         * @return int
277
         */
278
        public function fetchTotalExternalShared($id)
279
        {
280
            $select = $this->sql->select(self::_TABLE);
281
            $select->columns(['total_external_shared']);
282
            $select->where->equalTo('id', $id);
283
 
284
            $record = $this->executeFetchOneArray($select);
285
            return $record['total_external_shared'];
286
        }
287
 
288
 
289
 
290
}