Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6056 | Rev 6521 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use LeadersLinked\Model\Comment;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\Db\Sql\Expression;
12
use Laminas\Db\Sql\Insert;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Model\Feed;
15
 
16
 
17
class CommentMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_comments';
20
 
6319 anderson 21
 
1 www 22
    /**
23
     *
24
     * @var CommentMapper
25
     */
26
    private static $_instance;
6319 anderson 27
 
1 www 28
    /**
29
     *
30
     * @param AdapterInterface $adapter
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
6319 anderson 36
 
1 www 37
    /**
38
     *
39
     * @param AdapterInterface $adapter
40
     * @param LoggerInterface $logger
41
     * @param int $user_id
42
     * @return \LeadersLinked\Mapper\CommentMapper
43
     */
44
    public static function getInstance($adapter)
45
    {
6319 anderson 46
        if (self::$_instance == null) {
1 www 47
            self::$_instance = new CommentMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
6319 anderson 51
 
1 www 52
    /**
53
     *
54
     * @param int $uuid
55
     * @return Comment
56
     */
57
    public function fetchOneByUuid($uuid)
58
    {
59
        $prototype = new Comment;
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('uuid', $uuid);
6319 anderson 62
 
1 www 63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
6319 anderson 65
 
66
 
1 www 67
    /**
68
     *
69
     * @param int $feed_id
70
     * @return int
71
     */
72
    public function fetchCountCommentByFeedId($feed_id)
73
    {
74
        $select = $this->sql->select(self::_TABLE);
6319 anderson 75
        $select->columns(['total' => new Expression('COUNT(*)')]);
1 www 76
        $select->where->equalTo('feed_id', $feed_id);
4808 efrain 77
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
1 www 78
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 79
 
1 www 80
        $record = $this->executeFetchOneArray($select);
81
        return $record['total'];
82
    }
6319 anderson 83
 
4808 efrain 84
    /**
85
     *
6056 efrain 86
     * @param int $knowledge_area_id
87
     * @return int
88
     */
89
    public function fetchCountCommentByKnowledgeAreaId($knowledge_area_id)
90
    {
91
        $select = $this->sql->select(self::_TABLE);
6319 anderson 92
        $select->columns(['total' => new Expression('COUNT(*)')]);
6056 efrain 93
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
94
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
6319 anderson 95
 
6056 efrain 96
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 97
 
6056 efrain 98
        $record = $this->executeFetchOneArray($select);
99
        return $record['total'];
100
    }
6319 anderson 101
 
102
 
6056 efrain 103
    /**
104
     *
4808 efrain 105
     * @param int $post_id
106
     * @return int
107
     */
108
    public function fetchCountCommentByPostId($post_id)
109
    {
110
        $select = $this->sql->select(self::_TABLE);
6319 anderson 111
        $select->columns(['total' => new Expression('COUNT(*)')]);
4808 efrain 112
        $select->where->equalTo('post_id', $post_id);
113
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
6319 anderson 114
 
4808 efrain 115
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 116
 
4808 efrain 117
        $record = $this->executeFetchOneArray($select);
118
        return $record['total'];
119
    }
6319 anderson 120
 
121
 
1 www 122
    /**
123
     *
124
     * @param int $feed_id
125
     * @return Comment[]
126
     */
127
    public function fetchAllByFeedId($feed_id)
128
    {
129
        $prototype = new Comment();
6319 anderson 130
 
1 www 131
        $select = $this->sql->select(self::_TABLE);
132
        $select->where->equalTo('feed_id', $feed_id);
4808 efrain 133
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
6319 anderson 134
 
1 www 135
        return $this->executeFetchAllObject($select, $prototype);
136
    }
6319 anderson 137
 
1 www 138
    /**
139
     *
4808 efrain 140
     * @param int $post_id
141
     * @return Comment[]
142
     */
143
    public function fetchAllByPostId($post_id)
144
    {
145
        $prototype = new Comment();
6319 anderson 146
 
4808 efrain 147
        $select = $this->sql->select(self::_TABLE);
148
        $select->where->equalTo('post_id', $post_id);
149
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
6319 anderson 150
 
4808 efrain 151
        return $this->executeFetchAllObject($select, $prototype);
152
    }
6319 anderson 153
 
4808 efrain 154
    /**
155
     *
1 www 156
     * @param int $feed_id
157
     * @return Comment[]
158
     */
159
    public function fetchAllPublishedByFeedId($feed_id)
160
    {
161
        $prototype = new Comment();
6319 anderson 162
 
1 www 163
        $select = $this->sql->select(self::_TABLE);
164
        $select->where->equalTo('feed_id', $feed_id);
165
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
4808 efrain 166
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
1682 nelberth 167
        $select->where->IsNull('parent_id');
1 www 168
        return $this->executeFetchAllObject($select, $prototype);
169
    }
6319 anderson 170
 
171
 
4808 efrain 172
    /**
173
     *
174
     * @param int $post_id
175
     * @return Comment[]
176
     */
6056 efrain 177
    public function fetchAllPublishedByKnowledgeAreaId($knowledge_area_id)
178
    {
179
        $prototype = new Comment();
6319 anderson 180
 
6056 efrain 181
        $select = $this->sql->select(self::_TABLE);
182
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
183
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
184
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
185
        $select->where->IsNull('parent_id');
186
        return $this->executeFetchAllObject($select, $prototype);
187
    }
6319 anderson 188
 
189
 
6056 efrain 190
    /**
191
     *
192
     * @param int $post_id
193
     * @return Comment[]
194
     */
4808 efrain 195
    public function fetchAllPublishedByPostId($post_id)
196
    {
197
        $prototype = new Comment();
6319 anderson 198
 
4808 efrain 199
        $select = $this->sql->select(self::_TABLE);
200
        $select->where->equalTo('post_id', $post_id);
201
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
202
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
203
        $select->where->IsNull('parent_id');
204
        return $this->executeFetchAllObject($select, $prototype);
205
    }
6319 anderson 206
 
1684 nelberth 207
    public function fetchAllPublishedByCommentId($comment_id)
208
    {
209
        $prototype = new Comment();
6319 anderson 210
 
1684 nelberth 211
        $select = $this->sql->select(self::_TABLE);
212
        $select->where->equalTo('parent_id', $comment_id);
213
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
214
        return $this->executeFetchAllObject($select, $prototype);
215
    }
6319 anderson 216
 
1 www 217
    /**
218
     *
219
     * @param int $id
220
     * @return Comment
221
     */
222
    public function fetchOne($id)
223
    {
224
        $prototype = new Comment();
6319 anderson 225
 
1 www 226
        $select = $this->sql->select(self::_TABLE);
227
        $select->where->equalTo('id', $id);
6319 anderson 228
 
1 www 229
        return $this->executeFetchOneObject($select, $prototype);
230
    }
6319 anderson 231
 
1 www 232
    /**
233
     *
234
     * @param Comment $comment
235
     * @return boolean
236
     */
237
    public function insert($comment)
238
    {
239
        $hydrator = new ObjectPropertyHydrator();
240
        $values = $hydrator->extract($comment);
241
        $values = $this->removeEmpty($values);
6319 anderson 242
 
243
 
1 www 244
        $insert = $this->sql->insert(self::_TABLE);
245
        $insert->values($values);
6319 anderson 246
 
1 www 247
        $result = $this->executeInsert($insert);
6319 anderson 248
        if ($result) {
249
            $comment->id = $this->getLastInsertId();
1 www 250
        }
6319 anderson 251
 
1 www 252
        return $result;
253
    }
6319 anderson 254
 
255
 
1 www 256
    /**
257
     *
258
     * @param Comment $comment
259
     * @return boolean
260
     */
261
    public function update($comment)
262
    {
263
        $hydrator = new ObjectPropertyHydrator();
264
        $values = $hydrator->extract($comment);
265
        $values = $this->removeEmpty($values);
6319 anderson 266
 
267
 
1 www 268
        $update = $this->sql->update(self::_TABLE);
269
        $update->set($values);
270
        $update->where->equalTo('id', $comment->id);
271
 
6319 anderson 272
 
273
 
274
 
1 www 275
        return $this->executeUpdate($update);
276
    }
6319 anderson 277
 
278
 
1 www 279
    /**
280
     *
281
     * @param int $id
282
     * @return boolean
283
     */
284
    public function delete($id)
285
    {
6319 anderson 286
 
287
 
1 www 288
        $update = $this->sql->update(self::_TABLE);
289
        $update->set([
290
            'status' => Comment::STATUS_DELETED
291
        ]);
292
        $update->where->equalTo('id', $id);
6319 anderson 293
 
1 www 294
        return $this->executeUpdate($update);
295
    }
6319 anderson 296
 
1 www 297
    /**
298
     *
299
     * @param int $feed_id
300
     * @return boolean
301
     */
302
    public function deleteAllByFeedId($feed_id)
303
    {
6319 anderson 304
 
1 www 305
        $update = $this->sql->update(self::_TABLE);
306
        $update->set([
307
            'status' => Comment::STATUS_DELETED
308
        ]);
309
        $update->where->equalTo('feed_id', $feed_id);
6319 anderson 310
 
1 www 311
        return $this->executeUpdate($update);
312
    }
6319 anderson 313
}