Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1682 | Rev 4808 | 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
 
21
 
22
    /**
23
     *
24
     * @var CommentMapper
25
     */
26
    private static $_instance;
27
 
28
    /**
29
     *
30
     * @param AdapterInterface $adapter
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
36
 
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
    {
46
        if(self::$_instance == null) {
47
            self::$_instance = new CommentMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
51
 
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);
62
 
63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
65
 
66
 
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);
75
        $select->columns(['total' => new Expression('COUNT(*)') ]);
76
        $select->where->equalTo('feed_id', $feed_id);
77
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
78
 
79
        $record = $this->executeFetchOneArray($select);
80
        return $record['total'];
81
    }
82
 
83
 
84
    /**
85
     *
86
     * @param int $feed_id
87
     * @return Comment[]
88
     */
89
    public function fetchAllByFeedId($feed_id)
90
    {
91
        $prototype = new Comment();
92
 
93
        $select = $this->sql->select(self::_TABLE);
94
        $select->where->equalTo('feed_id', $feed_id);
95
 
96
        return $this->executeFetchAllObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $feed_id
102
     * @return Comment[]
103
     */
104
    public function fetchAllPublishedByFeedId($feed_id)
105
    {
106
        $prototype = new Comment();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('feed_id', $feed_id);
110
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
1682 nelberth 111
        $select->where->IsNull('parent_id');
1 www 112
        return $this->executeFetchAllObject($select, $prototype);
113
    }
1684 nelberth 114
    public function fetchAllPublishedByCommentId($comment_id)
115
    {
116
        $prototype = new Comment();
117
 
118
        $select = $this->sql->select(self::_TABLE);
119
        $select->where->equalTo('parent_id', $comment_id);
120
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
121
        return $this->executeFetchAllObject($select, $prototype);
122
    }
1 www 123
 
124
    /**
125
     *
126
     * @param int $id
127
     * @return Comment
128
     */
129
    public function fetchOne($id)
130
    {
131
        $prototype = new Comment();
132
 
133
        $select = $this->sql->select(self::_TABLE);
134
        $select->where->equalTo('id', $id);
135
 
136
        return $this->executeFetchOneObject($select, $prototype);
137
    }
138
 
139
    /**
140
     *
141
     * @param Comment $comment
142
     * @return boolean
143
     */
144
    public function insert($comment)
145
    {
146
        $hydrator = new ObjectPropertyHydrator();
147
        $values = $hydrator->extract($comment);
148
        $values = $this->removeEmpty($values);
149
 
150
 
151
        $insert = $this->sql->insert(self::_TABLE);
152
        $insert->values($values);
153
 
154
        $result = $this->executeInsert($insert);
155
        if($result) {
156
           $comment->id = $this->getLastInsertId();
157
        }
158
 
159
        return $result;
160
 
161
    }
162
 
163
 
164
    /**
165
     *
166
     * @param Comment $comment
167
     * @return boolean
168
     */
169
    public function update($comment)
170
    {
171
        $hydrator = new ObjectPropertyHydrator();
172
        $values = $hydrator->extract($comment);
173
        $values = $this->removeEmpty($values);
174
 
175
 
176
        $update = $this->sql->update(self::_TABLE);
177
        $update->set($values);
178
        $update->where->equalTo('id', $comment->id);
179
 
180
 
181
 
182
 
183
        return $this->executeUpdate($update);
184
    }
185
 
186
 
187
    /**
188
     *
189
     * @param int $id
190
     * @return boolean
191
     */
192
    public function delete($id)
193
    {
194
 
195
 
196
        $update = $this->sql->update(self::_TABLE);
197
        $update->set([
198
            'status' => Comment::STATUS_DELETED
199
        ]);
200
        $update->where->equalTo('id', $id);
201
 
202
        return $this->executeUpdate($update);
203
    }
204
 
205
    /**
206
     *
207
     * @param int $feed_id
208
     * @return boolean
209
     */
210
    public function deleteAllByFeedId($feed_id)
211
    {
212
 
213
        $update = $this->sql->update(self::_TABLE);
214
        $update->set([
215
            'status' => Comment::STATUS_DELETED
216
        ]);
217
        $update->where->equalTo('feed_id', $feed_id);
218
 
219
        return $this->executeUpdate($update);
220
    }
221
}