| 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 |
*/
|
| 381 |
ariadna |
52 |
function validateAndAdjustDates($filter, $initialDate, $finalDate)
|
| 364 |
ariadna |
53 |
{
|
|
|
54 |
$currentDate = date('Y-m-d'); // Fecha actual en formato YYYY-MM-DD
|
| 370 |
ariadna |
55 |
$yesterdayDate = date('Y-m-d', strtotime('-1 day')); // Fecha del día anterior
|
| 364 |
ariadna |
56 |
|
| 370 |
ariadna |
57 |
// Asignar la fecha del día anterior si initialDate es nula
|
| 364 |
ariadna |
58 |
if (empty($initialDate)) {
|
| 370 |
ariadna |
59 |
$initialDate = $yesterdayDate;
|
| 364 |
ariadna |
60 |
}
|
| 370 |
ariadna |
61 |
|
|
|
62 |
// Asignar la fecha actual si finalDate es nula
|
| 364 |
ariadna |
63 |
if (empty($finalDate)) {
|
|
|
64 |
$finalDate = $currentDate;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// Convertir las fechas a objetos DateTime para compararlas
|
|
|
68 |
$initialDateObj = new DateTime($initialDate);
|
|
|
69 |
$finalDateObj = new DateTime($finalDate);
|
|
|
70 |
|
|
|
71 |
// Si la fecha inicial es mayor a la final, intercambiarlas
|
|
|
72 |
if ($initialDateObj > $finalDateObj) {
|
|
|
73 |
list($initialDate, $finalDate) = [$finalDate, $initialDate];
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return [$initialDate, $finalDate];
|
|
|
77 |
}
|
|
|
78 |
|
| 381 |
ariadna |
79 |
/**
|
|
|
80 |
* Generates a list of dates within a given range and marks whether they exist in a provided list.
|
|
|
81 |
*
|
|
|
82 |
* @param array $datesList An array of dates to check against.
|
|
|
83 |
* @param string $startDate The start date of the range (YYYY-MM-DD).
|
|
|
84 |
* @param string $endDate The end date of the range (YYYY-MM-DD).
|
|
|
85 |
* @return array A list of dictionaries with each date and a flag indicating its presence in the given list.
|
|
|
86 |
*/
|
|
|
87 |
function generateDateList($datesList, $startDate, $endDate)
|
|
|
88 |
{
|
|
|
89 |
$result = [];
|
|
|
90 |
$start = new DateTime($startDate);
|
|
|
91 |
$end = new DateTime($endDate);
|
|
|
92 |
$datesLookup = array_flip($datesList); // Convert list to keys for quick lookup
|
| 364 |
ariadna |
93 |
|
| 381 |
ariadna |
94 |
while ($start <= $end) {
|
|
|
95 |
$dateStr = $start->format('Y-m-d');
|
|
|
96 |
$result[] = [
|
|
|
97 |
'date' => $dateStr,
|
|
|
98 |
'flag' => isset($datesLookup[$dateStr])
|
|
|
99 |
];
|
|
|
100 |
$start->modify('+1 day');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return $result;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
|
| 364 |
ariadna |
107 |
/**
|
|
|
108 |
*
|
| 352 |
ariadna |
109 |
* @param AdapterInterface $adapter
|
|
|
110 |
* @return HabitReportMapper
|
|
|
111 |
*/
|
|
|
112 |
public static function getInstance($adapter)
|
|
|
113 |
{
|
|
|
114 |
if (self::$_instance == null) {
|
|
|
115 |
self::$_instance = new HabitReportMapper($adapter);
|
|
|
116 |
}
|
|
|
117 |
return self::$_instance;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
*
|
|
|
122 |
* @param int $id
|
| 358 |
ariadna |
123 |
* @return array
|
| 352 |
ariadna |
124 |
*/
|
| 363 |
ariadna |
125 |
public function fetchDaysIntervalsRegisterList($id, $dateInitial, $dateFinal)
|
| 352 |
ariadna |
126 |
{
|
| 358 |
ariadna |
127 |
// Crear el objeto de selección
|
| 367 |
ariadna |
128 |
$select = $this->sql->select(self::_TABLE_B);
|
| 363 |
ariadna |
129 |
|
| 374 |
ariadna |
130 |
// Usar DISTINCT con Expression y convertir added_on a solo fecha
|
| 373 |
ariadna |
131 |
$select->columns([
|
| 379 |
ariadna |
132 |
'date' => new Expression('DISTINCT DATE(date)')
|
| 373 |
ariadna |
133 |
]);
|
|
|
134 |
|
| 374 |
ariadna |
135 |
// Agregar las condiciones de filtrado con solo la parte de la fecha
|
| 372 |
ariadna |
136 |
$select->where
|
|
|
137 |
->equalTo('user_id', $id)
|
| 379 |
ariadna |
138 |
->greaterThanOrEqualTo(new Expression('DATE(date)'), $dateInitial)
|
|
|
139 |
->lessThanOrEqualTo(new Expression('DATE(date)'), $dateFinal);
|
| 363 |
ariadna |
140 |
|
|
|
141 |
// Ordenar por fecha de forma descendente
|
| 379 |
ariadna |
142 |
$select->order('date DESC');
|
| 352 |
ariadna |
143 |
|
| 358 |
ariadna |
144 |
// Ejecutar la consulta
|
|
|
145 |
$statement = $this->sql->prepareStatementForSqlObject($select);
|
|
|
146 |
$results = $statement->execute();
|
|
|
147 |
|
|
|
148 |
// Convertir los resultados en un array
|
|
|
149 |
$records = [];
|
|
|
150 |
foreach ($results as $row) {
|
|
|
151 |
$records[] = $row;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return $records;
|
| 352 |
ariadna |
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
*
|
|
|
159 |
* @param string $uuid
|
|
|
160 |
*/
|
|
|
161 |
public function fetchFiveteenByUuid($uuid)
|
|
|
162 |
{
|
|
|
163 |
$select = $this->sql->select(self::_TABLE_C);
|
|
|
164 |
$select->where->equalTo('uuid', $uuid);
|
|
|
165 |
$select->order('created_at DESC');
|
| 358 |
ariadna |
166 |
$select->limit(15);
|
| 352 |
ariadna |
167 |
|
|
|
168 |
return $select;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
*
|
|
|
173 |
* @param string $uuid
|
|
|
174 |
* @param string $network_id
|
|
|
175 |
* @return HabitSkill
|
|
|
176 |
*/
|
|
|
177 |
public function fetchOneByUuidAndNetworkId($uuid, $network_id)
|
|
|
178 |
{
|
|
|
179 |
$select = $this->sql->select(self::_TABLE);
|
|
|
180 |
$select->where->equalTo('uuid', $uuid);
|
|
|
181 |
$select->where->equalTo('network_id', $network_id);
|
|
|
182 |
$select->limit(1);
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
$prototype = new HabitSkill();
|
|
|
187 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
*
|
|
|
192 |
* @param int $user_id
|
|
|
193 |
* @return HabitSkill[]
|
|
|
194 |
*/
|
|
|
195 |
public function fetchAllByUserId($user_id)
|
|
|
196 |
{
|
|
|
197 |
|
|
|
198 |
$prototype = new HabitSkill();
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
$select = $this->sql->select(self::_TABLE);
|
|
|
202 |
$select->where->equalTo('user_id', $user_id);
|
|
|
203 |
$select->order('name');
|
|
|
204 |
|
|
|
205 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
*
|
|
|
210 |
* @param int[] $company_ids
|
|
|
211 |
* @param string $search
|
|
|
212 |
* @return HabitSkill[]
|
|
|
213 |
*/
|
|
|
214 |
public function searchAllTemplateByCompayIds($company_ids, $search)
|
|
|
215 |
{
|
|
|
216 |
$prototype = new HabitSkill();
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
$select = $this->sql->select(self::_TABLE);
|
|
|
220 |
$select->where->in('company_id', $company_ids);
|
|
|
221 |
$select->where->like('name', '%' . $search . '%');
|
|
|
222 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
223 |
|
|
|
224 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
*
|
|
|
231 |
* @param int[] $company_ids
|
|
|
232 |
* @return HabitSkill[]
|
|
|
233 |
*/
|
|
|
234 |
public function fetchAllTemplateByCompayIds($company_ids)
|
|
|
235 |
{
|
|
|
236 |
$prototype = new HabitSkill();
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
$select = $this->sql->select(self::_TABLE);
|
|
|
240 |
$select->where->in('company_id', $company_ids);
|
|
|
241 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
242 |
|
|
|
243 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
*
|
|
|
248 |
* @param int $company_id
|
|
|
249 |
* @param string $search
|
|
|
250 |
* @param int $page
|
|
|
251 |
* @param int $records_per_page
|
|
|
252 |
* @param string $order_field
|
|
|
253 |
* @param string $order_direction
|
|
|
254 |
* @return Paginator
|
|
|
255 |
*/
|
|
|
256 |
public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
257 |
{
|
|
|
258 |
$prototype = new HabitSkill();
|
|
|
259 |
$select = $this->sql->select(self::_TABLE);
|
|
|
260 |
|
|
|
261 |
if ($search) {
|
|
|
262 |
$select->where->like('name', '%' . $search . '%');
|
|
|
263 |
}
|
|
|
264 |
$select->where->equalTo('company_id', $company_id);
|
|
|
265 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
270 |
|
|
|
271 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
272 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
273 |
|
|
|
274 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
275 |
$paginator = new Paginator($adapter);
|
|
|
276 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
277 |
$paginator->setCurrentPageNumber($page);
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
return $paginator;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
*
|
|
|
287 |
* @param int $user_id
|
|
|
288 |
* @param string $search
|
|
|
289 |
* @param int $page
|
|
|
290 |
* @param int $records_per_page
|
|
|
291 |
* @param string $order_field
|
|
|
292 |
* @param string $order_direction
|
|
|
293 |
* @return Paginator
|
|
|
294 |
*/
|
|
|
295 |
public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
296 |
{
|
|
|
297 |
$prototype = new HabitSkill();
|
|
|
298 |
$select = $this->sql->select(self::_TABLE);
|
|
|
299 |
|
|
|
300 |
if ($search) {
|
|
|
301 |
$select->where->like('name', '%' . $search . '%');
|
|
|
302 |
}
|
|
|
303 |
$select->where->equalTo('user_id', $user_id);
|
|
|
304 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
305 |
|
|
|
306 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
307 |
|
|
|
308 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
309 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
310 |
|
|
|
311 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
312 |
$paginator = new Paginator($adapter);
|
|
|
313 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
314 |
$paginator->setCurrentPageNumber($page);
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
return $paginator;
|
|
|
318 |
}
|
|
|
319 |
}
|