| 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
|
| 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 |
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
*
|
| 352 |
ariadna |
82 |
* @param AdapterInterface $adapter
|
|
|
83 |
* @return HabitReportMapper
|
|
|
84 |
*/
|
|
|
85 |
public static function getInstance($adapter)
|
|
|
86 |
{
|
|
|
87 |
if (self::$_instance == null) {
|
|
|
88 |
self::$_instance = new HabitReportMapper($adapter);
|
|
|
89 |
}
|
|
|
90 |
return self::$_instance;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
*
|
|
|
95 |
* @param int $id
|
| 358 |
ariadna |
96 |
* @return array
|
| 352 |
ariadna |
97 |
*/
|
| 363 |
ariadna |
98 |
public function fetchDaysIntervalsRegisterList($id, $dateInitial, $dateFinal)
|
| 352 |
ariadna |
99 |
{
|
| 358 |
ariadna |
100 |
// Crear el objeto de selección
|
| 367 |
ariadna |
101 |
$select = $this->sql->select(self::_TABLE_B);
|
| 363 |
ariadna |
102 |
|
| 372 |
ariadna |
103 |
$select->where
|
|
|
104 |
->equalTo('user_id', $id)
|
| 369 |
ariadna |
105 |
->greaterThanOrEqualTo('added_on', $dateInitial . ' 00:00:00')
|
| 372 |
ariadna |
106 |
->lessThanOrEqualTo('added_on', $dateFinal . ' 23:59:59');
|
| 363 |
ariadna |
107 |
|
| 372 |
ariadna |
108 |
// Seleccionar solo fechas únicas
|
|
|
109 |
$select->columns(['added_on']); // Aquí puedes seleccionar la columna 'added_on' de manera única
|
|
|
110 |
|
|
|
111 |
$select->distinct(true); // Usar DISTINCT para asegurar que las fechas no se repitan
|
|
|
112 |
|
| 363 |
ariadna |
113 |
// Ordenar por fecha de forma descendente
|
| 359 |
ariadna |
114 |
$select->order('added_on DESC');
|
| 352 |
ariadna |
115 |
|
| 358 |
ariadna |
116 |
// Ejecutar la consulta
|
|
|
117 |
$statement = $this->sql->prepareStatementForSqlObject($select);
|
|
|
118 |
$results = $statement->execute();
|
|
|
119 |
|
|
|
120 |
// Convertir los resultados en un array
|
|
|
121 |
$records = [];
|
|
|
122 |
foreach ($results as $row) {
|
|
|
123 |
$records[] = $row;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
return $records;
|
| 352 |
ariadna |
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
*
|
|
|
131 |
* @param string $uuid
|
|
|
132 |
*/
|
|
|
133 |
public function fetchFiveteenByUuid($uuid)
|
|
|
134 |
{
|
|
|
135 |
$select = $this->sql->select(self::_TABLE_C);
|
|
|
136 |
$select->where->equalTo('uuid', $uuid);
|
|
|
137 |
$select->order('created_at DESC');
|
| 358 |
ariadna |
138 |
$select->limit(15);
|
| 352 |
ariadna |
139 |
|
|
|
140 |
return $select;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
*
|
|
|
145 |
* @param string $uuid
|
|
|
146 |
* @param string $network_id
|
|
|
147 |
* @return HabitSkill
|
|
|
148 |
*/
|
|
|
149 |
public function fetchOneByUuidAndNetworkId($uuid, $network_id)
|
|
|
150 |
{
|
|
|
151 |
$select = $this->sql->select(self::_TABLE);
|
|
|
152 |
$select->where->equalTo('uuid', $uuid);
|
|
|
153 |
$select->where->equalTo('network_id', $network_id);
|
|
|
154 |
$select->limit(1);
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
$prototype = new HabitSkill();
|
|
|
159 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
*
|
|
|
164 |
* @param int $user_id
|
|
|
165 |
* @return HabitSkill[]
|
|
|
166 |
*/
|
|
|
167 |
public function fetchAllByUserId($user_id)
|
|
|
168 |
{
|
|
|
169 |
|
|
|
170 |
$prototype = new HabitSkill();
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
$select = $this->sql->select(self::_TABLE);
|
|
|
174 |
$select->where->equalTo('user_id', $user_id);
|
|
|
175 |
$select->order('name');
|
|
|
176 |
|
|
|
177 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
*
|
|
|
182 |
* @param int[] $company_ids
|
|
|
183 |
* @param string $search
|
|
|
184 |
* @return HabitSkill[]
|
|
|
185 |
*/
|
|
|
186 |
public function searchAllTemplateByCompayIds($company_ids, $search)
|
|
|
187 |
{
|
|
|
188 |
$prototype = new HabitSkill();
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
$select = $this->sql->select(self::_TABLE);
|
|
|
192 |
$select->where->in('company_id', $company_ids);
|
|
|
193 |
$select->where->like('name', '%' . $search . '%');
|
|
|
194 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
195 |
|
|
|
196 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
*
|
|
|
203 |
* @param int[] $company_ids
|
|
|
204 |
* @return HabitSkill[]
|
|
|
205 |
*/
|
|
|
206 |
public function fetchAllTemplateByCompayIds($company_ids)
|
|
|
207 |
{
|
|
|
208 |
$prototype = new HabitSkill();
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
$select = $this->sql->select(self::_TABLE);
|
|
|
212 |
$select->where->in('company_id', $company_ids);
|
|
|
213 |
$select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
|
|
|
214 |
|
|
|
215 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
*
|
|
|
220 |
* @param int $company_id
|
|
|
221 |
* @param string $search
|
|
|
222 |
* @param int $page
|
|
|
223 |
* @param int $records_per_page
|
|
|
224 |
* @param string $order_field
|
|
|
225 |
* @param string $order_direction
|
|
|
226 |
* @return Paginator
|
|
|
227 |
*/
|
|
|
228 |
public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
229 |
{
|
|
|
230 |
$prototype = new HabitSkill();
|
|
|
231 |
$select = $this->sql->select(self::_TABLE);
|
|
|
232 |
|
|
|
233 |
if ($search) {
|
|
|
234 |
$select->where->like('name', '%' . $search . '%');
|
|
|
235 |
}
|
|
|
236 |
$select->where->equalTo('company_id', $company_id);
|
|
|
237 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
242 |
|
|
|
243 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
244 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
245 |
|
|
|
246 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
247 |
$paginator = new Paginator($adapter);
|
|
|
248 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
249 |
$paginator->setCurrentPageNumber($page);
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
return $paginator;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
*
|
|
|
259 |
* @param int $user_id
|
|
|
260 |
* @param string $search
|
|
|
261 |
* @param int $page
|
|
|
262 |
* @param int $records_per_page
|
|
|
263 |
* @param string $order_field
|
|
|
264 |
* @param string $order_direction
|
|
|
265 |
* @return Paginator
|
|
|
266 |
*/
|
|
|
267 |
public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field = 'name', $order_direction = 'ASC')
|
|
|
268 |
{
|
|
|
269 |
$prototype = new HabitSkill();
|
|
|
270 |
$select = $this->sql->select(self::_TABLE);
|
|
|
271 |
|
|
|
272 |
if ($search) {
|
|
|
273 |
$select->where->like('name', '%' . $search . '%');
|
|
|
274 |
}
|
|
|
275 |
$select->where->equalTo('user_id', $user_id);
|
|
|
276 |
$select->order($order_field . ' ' . $order_direction);
|
|
|
277 |
|
|
|
278 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
279 |
|
|
|
280 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
281 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
282 |
|
|
|
283 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
284 |
$paginator = new Paginator($adapter);
|
|
|
285 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
286 |
$paginator->setCurrentPageNumber($page);
|
|
|
287 |
|
|
|
288 |
|
|
|
289 |
return $paginator;
|
|
|
290 |
}
|
|
|
291 |
}
|