Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
192 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Db\ResultSet\HydratingResultSet;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\Paginator\Paginator;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
use LeadersLinked\Model\AbuseReport;
16
use LeadersLinked\Mapper\Common\MapperCommon;
17
 
18
 
19
 
20
class AbuseReportMapper extends MapperCommon
21
{
22
    const _TABLE = 'tbl_abuse_reports';
23
 
24
    /**
25
     *
26
     * @var AbuseReportMapper
27
     */
28
    private static $_instance;
29
 
30
    /**
31
     *
32
     * @param AdapterInterface $adapter
33
     */
34
    private function __construct($adapter)
35
    {
36
        parent::__construct($adapter);
37
    }
38
 
39
    /**
40
     *
41
     * @param AdapterInterface $adapter
42
     * @return AbuseReportMapper
43
     */
44
    public static function getInstance($adapter)
45
    {
46
        if(self::$_instance == null) {
47
            self::$_instance = new AbuseReportMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
51
 
52
 
53
 
54
 
55
    /**
56
     *
57
     * @param int $id
58
     * @return AbuseReport
59
     */
60
    public function fetchOne($id)
61
    {
62
        $select = $this->sql->select(self::_TABLE);
63
        $select->where->equalTo('id', $id);
64
        $select->limit(1);
65
 
66
        $prototype = new AbuseReport();
67
        return $this->executeFetchOneObject($select, $prototype);
68
    }
69
 
70
    /**
71
     *
72
     * @param string $uuid
73
     * @return AbuseReport
74
     */
75
    public function fetchOneByUuid($uuid)
76
    {
77
        $select = $this->sql->select(self::_TABLE);
78
        $select->where->equalTo('uuid', $uuid);
79
        $select->limit(1);
80
 
81
        $prototype = new AbuseReport();
82
        return $this->executeFetchOneObject($select, $prototype);
83
    }
84
 
85
    /**
86
     *
203 efrain 87
     * @param int $user_reporting_id
192 efrain 88
     * @param int $page
89
     * @param int $records_per_page
90
     * @param string $order_field
91
     * @param string $order_direction
92
     * @return Paginator
93
     */
203 efrain 94
    public function fetchAllDataTableByUserReportingId($user_reporting_id, $page = 1, $records_per_page = 10, $order_field= 'id', $order_direction = 'DESC')
192 efrain 95
    {
96
        $prototype = new AbuseReport();
97
        $select = $this->sql->select(self::_TABLE);
203 efrain 98
        $select->where->equalTo('user_reporting_id', $user_reporting_id);
192 efrain 99
        $select->order($order_field . ' ' . $order_direction);
100
 
101
        $hydrator   = new ObjectPropertyHydrator();
102
        $resultset  = new HydratingResultSet($hydrator, $prototype);
103
 
104
        $adapter = new DbSelect($select, $this->sql, $resultset);
105
        $paginator = new Paginator($adapter);
106
        $paginator->setItemCountPerPage($records_per_page);
107
        $paginator->setCurrentPageNumber($page);
108
 
109
 
110
        return $paginator;
111
    }
112
 
113
    /**
114
     *
115
     * @param int $user_reporting_id
116
     * @param string $search
117
     * @param int $page
118
     * @param int $records_per_page
119
     * @param string $order_field
120
     * @param string $order_direction
121
     * @return Paginator
122
     */
203 efrain 123
    public function fetchAllDataTable( $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
192 efrain 124
    {
125
        $prototype = new AbuseReport();
126
        $select = $this->sql->select(self::_TABLE);
127
        $select->where->equalTo('user_reporting_id', $user_reporting_id);
128
 
129
        if($search) {
130
            //$select->where->like('name', '%' . $search . '%');
131
        }
132
        $select->order($order_field . ' ' . $order_direction);
133
 
134
        $hydrator   = new ObjectPropertyHydrator();
135
        $resultset  = new HydratingResultSet($hydrator, $prototype);
136
 
137
        $adapter = new DbSelect($select, $this->sql, $resultset);
138
        $paginator = new Paginator($adapter);
139
        $paginator->setItemCountPerPage($records_per_page);
140
        $paginator->setCurrentPageNumber($page);
141
 
142
 
143
        return $paginator;
144
    }
145
 
146
    /**
147
     *
148
     * @param int $user_reporting_id
149
     * @param string $type
150
     * @return int[]
151
     */
152
    public function fetchAllDataByUserReportingIdAndTypeReturnIds($user_reporting_id, $type)
153
    {
154
        $select = $this->sql->select(self::_TABLE);
155
        $select->columns(['related_id']);
156
        $select->where->equalTo('user_reporting_id', $user_reporting_id);
157
        $select->where->equalTo('type',  $type);
158
 
159
        //echo $select->getSqlString($this->adapter->platform);
160
 
161
        $ids = [];
162
        $records = $this->executeFetchAllArray($select);
163
 
164
        foreach($records as $record)
165
        {
166
            array_push($ids, $record['related_id']);
167
        }
168
        return $ids;
169
    }
170
 
171
    /**
172
     *
173
     * @param AbuseReport $abuseReport
174
     * @return boolean
175
     */
176
    public function insert($abuseReport)
177
    {
178
        $hydrator = new ObjectPropertyHydrator();
179
        $values = $hydrator->extract($abuseReport);
203 efrain 180
        $values = $this->removeEmpty($values);
192 efrain 181
 
182
        $insert = $this->sql->insert(self::_TABLE);
183
        $insert->values($values);
184
 
185
 
186
        $result = $this->executeInsert($insert);
187
        if($result) {
188
            $abuseReport->id = $this->lastInsertId;
189
        }
190
 
191
        return $result;
192
 
193
    }
194
 
195
    /**
196
     *
197
     * @param AbuseReport $abuseReport
198
     * @return boolean
199
     */
200
    public function update($abuseReport)
201
    {
202
        $hydrator = new ObjectPropertyHydrator();
203
        $values = $hydrator->extract($abuseReport);
203 efrain 204
        $values = $this->removeEmpty($values);
192 efrain 205
 
206
        $update = $this->sql->update(self::_TABLE);
207
        $update->set($values);
208
        $update->where->equalTo('id', $abuseReport->id);
209
 
210
        return $this->executeUpdate($update);
211
    }
212
 
213
    /**
214
     *
215
     * @param AbuseReport $abuseReport
216
     * @return boolean
217
     */
218
    public function delete($abuseReport)
219
    {
220
        $delete = $this->sql->delete(self::_TABLE);
221
        $delete->where->equalTo('id', $abuseReport->id);
222
 
223
        return $this->executeDelete($delete);
224
 
225
 
226
    }
227
 
228
 
229
}