Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
352 ariadna 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 Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Paginator\Paginator;
12
 
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
 
15
use LeadersLinked\Model\HabitSkill;
16
use LeadersLinked\Mapper\Common\MapperCommon;
17
 
18
 
19
class HabitReportMapper extends MapperCommon
20
{
21
    const _TABLE_A = 'tbl_habits_skills';
22
    const _TABLE_B = 'tbl_habits_user_logs_categories';
23
    const _TABLE_C = 'tbl_habits_user_logs_content';
24
 
25
 
26
    /**
27
     *
28
     * @var HabitReportMapper
29
     */
30
    private static $_instance;
31
 
32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     */
36
    private function __construct($adapter)
37
    {
38
        parent::__construct($adapter);
39
    }
40
 
41
    /**
42
     *
43
     * @param AdapterInterface $adapter
44
     * @return HabitReportMapper
45
     */
46
    public static function getInstance($adapter)
47
    {
48
        if (self::$_instance == null) {
49
            self::$_instance = new HabitReportMapper($adapter);
50
        }
51
        return self::$_instance;
52
    }
53
 
54
    /**
55
     *
56
     * @param int $id
358 ariadna 57
     * @return array
352 ariadna 58
     */
59
    public function fetchFiveteen($id)
60
    {
358 ariadna 61
        // Crear el objeto de selección
352 ariadna 62
        $select = $this->sql->select(self::_TABLE_C);
357 ariadna 63
        $select->where->equalTo('user_id', $id);
352 ariadna 64
        $select->order('created_at DESC');
65
        $select->limit(15);
66
 
358 ariadna 67
        // Ejecutar la consulta
68
        $statement = $this->sql->prepareStatementForSqlObject($select);
69
        $results = $statement->execute();
70
 
71
        // Convertir los resultados en un array
72
        $records = [];
73
        foreach ($results as $row) {
74
            $records[] = $row;
75
        }
76
 
77
        return $records;
352 ariadna 78
    }
79
 
80
    /**
81
     *
82
     * @param string $uuid
83
     */
84
    public function fetchFiveteenByUuid($uuid)
85
    {
86
        $select = $this->sql->select(self::_TABLE_C);
87
        $select->where->equalTo('uuid', $uuid);
88
        $select->order('created_at DESC');
358 ariadna 89
        $select->limit(15);
352 ariadna 90
 
91
        return $select;
92
    }
93
 
94
    /**
95
     *
96
     * @param string $uuid
97
     * @param string $network_id
98
     * @return HabitSkill
99
     */
100
    public function fetchOneByUuidAndNetworkId($uuid, $network_id)
101
    {
102
        $select = $this->sql->select(self::_TABLE);
103
        $select->where->equalTo('uuid', $uuid);
104
        $select->where->equalTo('network_id', $network_id);
105
        $select->limit(1);
106
 
107
 
108
 
109
        $prototype = new HabitSkill();
110
        return $this->executeFetchOneObject($select, $prototype);
111
    }
112
 
113
    /**
114
     *
115
     * @param int $user_id
116
     * @return HabitSkill[]
117
     */
118
    public function fetchAllByUserId($user_id)
119
    {
120
 
121
        $prototype = new HabitSkill();
122
 
123
 
124
        $select = $this->sql->select(self::_TABLE);
125
        $select->where->equalTo('user_id', $user_id);
126
        $select->order('name');
127
 
128
        return $this->executeFetchAllObject($select, $prototype);
129
    }
130
 
131
    /**
132
     *
133
     * @param int[] $company_ids
134
     * @param string $search
135
     * @return HabitSkill[]
136
     */
137
    public function searchAllTemplateByCompayIds($company_ids, $search)
138
    {
139
        $prototype = new HabitSkill();
140
 
141
 
142
        $select = $this->sql->select(self::_TABLE);
143
        $select->where->in('company_id', $company_ids);
144
        $select->where->like('name', '%' . $search . '%');
145
        $select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
146
 
147
        return $this->executeFetchAllObject($select, $prototype);
148
    }
149
 
150
 
151
 
152
    /**
153
     *
154
     * @param int[] $company_ids
155
     * @return HabitSkill[]
156
     */
157
    public function fetchAllTemplateByCompayIds($company_ids)
158
    {
159
        $prototype = new HabitSkill();
160
 
161
 
162
        $select = $this->sql->select(self::_TABLE);
163
        $select->where->in('company_id', $company_ids);
164
        $select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
165
 
166
        return $this->executeFetchAllObject($select, $prototype);
167
    }
168
 
169
    /**
170
     *
171
     * @param int $company_id
172
     * @param string $search
173
     * @param int $page
174
     * @param int $records_per_page
175
     * @param string $order_field
176
     * @param string $order_direction
177
     * @return Paginator
178
     */
179
    public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
180
    {
181
        $prototype = new HabitSkill();
182
        $select = $this->sql->select(self::_TABLE);
183
 
184
        if ($search) {
185
            $select->where->like('name', '%' . $search . '%');
186
        }
187
        $select->where->equalTo('company_id', $company_id);
188
        $select->order($order_field . ' ' . $order_direction);
189
 
190
 
191
 
192
        // echo $select->getSqlString($this->adapter->platform); exit;
193
 
194
        $hydrator   = new ObjectPropertyHydrator();
195
        $resultset  = new HydratingResultSet($hydrator, $prototype);
196
 
197
        $adapter = new DbSelect($select, $this->sql, $resultset);
198
        $paginator = new Paginator($adapter);
199
        $paginator->setItemCountPerPage($records_per_page);
200
        $paginator->setCurrentPageNumber($page);
201
 
202
 
203
        return $paginator;
204
    }
205
 
206
 
207
 
208
    /**
209
     *
210
     * @param int $user_id
211
     * @param string $search
212
     * @param int $page
213
     * @param int $records_per_page
214
     * @param string $order_field
215
     * @param string $order_direction
216
     * @return Paginator
217
     */
218
    public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
219
    {
220
        $prototype = new HabitSkill();
221
        $select = $this->sql->select(self::_TABLE);
222
 
223
        if ($search) {
224
            $select->where->like('name', '%' . $search . '%');
225
        }
226
        $select->where->equalTo('user_id', $user_id);
227
        $select->order($order_field . ' ' . $order_direction);
228
 
229
        // echo $select->getSqlString($this->adapter->platform); exit;
230
 
231
        $hydrator   = new ObjectPropertyHydrator();
232
        $resultset  = new HydratingResultSet($hydrator, $prototype);
233
 
234
        $adapter = new DbSelect($select, $this->sql, $resultset);
235
        $paginator = new Paginator($adapter);
236
        $paginator->setItemCountPerPage($records_per_page);
237
        $paginator->setCurrentPageNumber($page);
238
 
239
 
240
        return $paginator;
241
    }
242
}