| 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;
|
| 367 |
ariadna |
12 |
use Laminas\Db\Sql\Expression;
|
| 352 |
ariadna |
13 |
|
|
|
14 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
15 |
|
|
|
16 |
use LeadersLinked\Model\HabitSkill;
|
|
|
17 |
use LeadersLinked\Mapper\Common\MapperCommon;
|
| 364 |
ariadna |
18 |
use DateTime;
|
| 352 |
ariadna |
19 |
|
|
|
20 |
|
|
|
21 |
class HabitReportMapper extends MapperCommon
|
|
|
22 |
{
|
|
|
23 |
const _TABLE_A = 'tbl_habits_skills';
|
| 363 |
ariadna |
24 |
const _TABLE_B = 'tbl_habits_skills_registers';
|
| 352 |
ariadna |
25 |
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
*
|
|
|
29 |
* @var HabitReportMapper
|
|
|
30 |
*/
|
|
|
31 |
private static $_instance;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
* @param AdapterInterface $adapter
|
|
|
36 |
*/
|
|
|
37 |
private function __construct($adapter)
|
|
|
38 |
{
|
|
|
39 |
parent::__construct($adapter);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
| 364 |
ariadna |
43 |
* Valida y ajusta las fechas de un rango de búsqueda.
|
| 352 |
ariadna |
44 |
*
|
| 364 |
ariadna |
45 |
* - Si alguna fecha es `null`, se asigna la fecha actual en formato `YYYY-MM-DD`.
|
|
|
46 |
* - Si la fecha inicial es mayor que la final, se intercambian.
|
|
|
47 |
*
|
|
|
48 |
* @param string|null $initialDate Fecha inicial en formato `YYYY-MM-DD` o `null`.
|
|
|
49 |
* @param string|null $finalDate Fecha final en formato `YYYY-MM-DD` o `null`.
|
|
|
50 |
* @return array Arreglo con las fechas validadas [initialDate, finalDate].
|
|
|
51 |
*/
|
|
|
52 |
function validateAndAdjustDates($initialDate, $finalDate)
|
|
|
53 |
{
|
|
|
54 |
$currentDate = date('Y-m-d'); // Fecha actual en formato YYYY-MM-DD
|
|
|
55 |
|
|
|
56 |
// Asignar la fecha actual si alguna de las fechas es nula
|
|
|
57 |
if (empty($initialDate)) {
|
|
|
58 |
$initialDate = $currentDate;
|
|
|
59 |
}
|
|
|
60 |
if (empty($finalDate)) {
|
|
|
61 |
$finalDate = $currentDate;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Convertir las fechas a objetos DateTime para compararlas
|
|
|
65 |
$initialDateObj = new DateTime($initialDate);
|
|
|
66 |
$finalDateObj = new DateTime($finalDate);
|
|
|
67 |
|
|
|
68 |
// Si la fecha inicial es mayor a la final, intercambiarlas
|
|
|
69 |
if ($initialDateObj > $finalDateObj) {
|
|
|
70 |
list($initialDate, $finalDate) = [$finalDate, $initialDate];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
return [$initialDate, $finalDate];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
*
|
| 352 |
ariadna |
79 |
* @param AdapterInterface $adapter
|
|
|
80 |
* @return HabitReportMapper
|
|
|
81 |
*/
|
|
|
82 |
public static function getInstance($adapter)
|
|
|
83 |
{
|
|
|
84 |
if (self::$_instance == null) {
|
|
|
85 |
self::$_instance = new HabitReportMapper($adapter);
|
|
|
86 |
}
|
|
|
87 |
return self::$_instance;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
*
|
|
|
92 |
* @param int $id
|
| 358 |
ariadna |
93 |
* @return array
|
| 352 |
ariadna |
94 |
*/
|
| 363 |
ariadna |
95 |
public function fetchDaysIntervalsRegisterList($id, $dateInitial, $dateFinal)
|
| 352 |
ariadna |
96 |
{
|
| 358 |
ariadna |
97 |
// Crear el objeto de selección
|
| 367 |
ariadna |
98 |
$select = $this->sql->select(self::_TABLE_B);
|
| 363 |
ariadna |
99 |
|
| 366 |
ariadna |
100 |
// Filtrar por usuario y rango de fechas (ignorando la hora)
|
| 363 |
ariadna |
101 |
$select->where
|
|
|
102 |
->equalTo('user_id', $id)
|
| 366 |
ariadna |
103 |
->greaterThanOrEqualTo(new Expression("DATE(added_on)"), $dateInitial)
|
|
|
104 |
->lessThanOrEqualTo(new Expression("DATE(added_on)"), $dateFinal);
|
| 363 |
ariadna |
105 |
|
|
|
106 |
// Ordenar por fecha de forma descendente
|
| 359 |
ariadna |
107 |
$select->order('added_on DESC');
|
| 352 |
ariadna |
108 |
|
| 358 |
ariadna |
109 |
// Ejecutar la consulta
|
|
|
110 |
$statement = $this->sql->prepareStatementForSqlObject($select);
|
|
|
111 |
$results = $statement->execute();
|
|
|
112 |
|
|
|
113 |
// Convertir los resultados en un array
|
|
|
114 |
$records = [];
|
|
|
115 |
foreach ($results as $row) {
|
|
|
116 |
$records[] = $row;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return $records;
|
| 352 |
ariadna |
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
*
|
|
|
124 |
* @param string $uuid
|
|
|
125 |
*/
|
|
|
126 |
public function fetchFiveteenByUuid($uuid)
|
|
|
127 |
{
|
|
|
128 |
$select = $this->sql->select(self::_TABLE_C);
|
|
|
129 |
$select->where->equalTo('uuid', $uuid);
|
|
|
130 |
$select->order('created_at DESC');
|
| 358 |
ariadna |
131 |
$select->limit(15);
|
| 352 |
ariadna |
132 |
|
|
|
133 |
return $select;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
*
|
|
|
138 |
* @param string $uuid
|
|
|
139 |
* @param string $network_id
|
|
|
140 |
* @return HabitSkill
|
|
|
141 |
*/
|
|
|
142 |
public function fetchOneByUuidAndNetworkId($uuid, $network_id)
|
|
|
143 |
{
|
|
|
144 |
$select = $this->sql->select(self::_TABLE);
|
|
|
145 |
$select->where->equalTo('uuid', $uuid);
|
|
|
146 |
$select->where->equalTo('network_id', $network_id);
|
|
|
147 |
$select->limit(1);
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
$prototype = new HabitSkill();
|
|
|
152 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
*
|
|
|
157 |
* @param int $user_id
|
|
|
158 |
* @return HabitSkill[]
|
|
|
159 |
*/
|
|
|
160 |
public function fetchAllByUserId($user_id)
|
|
|
161 |
{
|
|
|
162 |
|
|
|
163 |
$prototype = new HabitSkill();
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
$select = $this->sql->select(self::_TABLE);
|
|
|
167 |
$select->where->equalTo('user_id', $user_id);
|
|
|
168 |
$select->order('name');
|
|
|
169 |
|
|
|
170 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
*
|
|
|
175 |
* @param int[] $company_ids
|
|
|
176 |
* @param string $search
|
|
|
177 |
* @return HabitSkill[]
|
|
|
178 |
*/
|
|
|
179 |
public function searchAllTemplateByCompayIds($company_ids, $search)
|
|
|
180 |
{
|
|
|
181 |
$prototype = new HabitSkill();
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
$select = $this->sql->select(self::_TABLE);
|
|
|
185 |
$select->where->in('company_id', $company_ids);
|
|
|
186 |
$select->where->like('name', '%' . $search . '%');
|
|
|
187 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
188 |
|
|
|
189 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
*
|
|
|
196 |
* @param int[] $company_ids
|
|
|
197 |
* @return HabitSkill[]
|
|
|
198 |
*/
|
|
|
199 |
public function fetchAllTemplateByCompayIds($company_ids)
|
|
|
200 |
{
|
|
|
201 |
$prototype = new HabitSkill();
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
$select = $this->sql->select(self::_TABLE);
|
|
|
205 |
$select->where->in('company_id', $company_ids);
|
|
|
206 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
207 |
|
|
|
208 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
*
|
|
|
213 |
* @param int $company_id
|
|
|
214 |
* @param string $search
|
|
|
215 |
* @param int $page
|
|
|
216 |
* @param int $records_per_page
|
|
|
217 |
* @param string $order_field
|
|
|
218 |
* @param string $order_direction
|
|
|
219 |
* @return Paginator
|
|
|
220 |
*/
|
|
|
221 |
public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
222 |
{
|
|
|
223 |
$prototype = new HabitSkill();
|
|
|
224 |
$select = $this->sql->select(self::_TABLE);
|
|
|
225 |
|
|
|
226 |
if ($search) {
|
|
|
227 |
$select->where->like('name', '%' . $search . '%');
|
|
|
228 |
}
|
|
|
229 |
$select->where->equalTo('company_id', $company_id);
|
|
|
230 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
235 |
|
|
|
236 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
237 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
238 |
|
|
|
239 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
240 |
$paginator = new Paginator($adapter);
|
|
|
241 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
242 |
$paginator->setCurrentPageNumber($page);
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
return $paginator;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
*
|
|
|
252 |
* @param int $user_id
|
|
|
253 |
* @param string $search
|
|
|
254 |
* @param int $page
|
|
|
255 |
* @param int $records_per_page
|
|
|
256 |
* @param string $order_field
|
|
|
257 |
* @param string $order_direction
|
|
|
258 |
* @return Paginator
|
|
|
259 |
*/
|
|
|
260 |
public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
261 |
{
|
|
|
262 |
$prototype = new HabitSkill();
|
|
|
263 |
$select = $this->sql->select(self::_TABLE);
|
|
|
264 |
|
|
|
265 |
if ($search) {
|
|
|
266 |
$select->where->like('name', '%' . $search . '%');
|
|
|
267 |
}
|
|
|
268 |
$select->where->equalTo('user_id', $user_id);
|
|
|
269 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
270 |
|
|
|
271 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
272 |
|
|
|
273 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
274 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
275 |
|
|
|
276 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
277 |
$paginator = new Paginator($adapter);
|
|
|
278 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
279 |
$paginator->setCurrentPageNumber($page);
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
return $paginator;
|
|
|
283 |
}
|
|
|
284 |
}
|