<?php
namespace App\Controller;
use App\Entity\User;
use App\Repository\AdminRepository;
use App\Repository\EnterpriseRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class UserDataTableController extends AbstractController
{
private $em;
private $id;
private $rpe;
private $rpu;
private $rpa;
/**
* @param $em
* @param $rps
* @param $rpe
* @param $rpu
* @param $conn
*/
public function __construct(EntityManagerInterface $em, EnterpriseRepository $rpe, UserRepository $rpu, AdminRepository $rpa)
{
$this->em = $em;
$this->rpe = $rpe;
$this->rpu = $rpu;
$this->rpa = $rpa;
}
#[Route('/user/history/table', name: 'user_history_table', methods: ['GET'])]
public function table(): JSonResponse
{
extract($_GET);
$data = [
'form' => '',
'status' => 0,
'pagenum' => $pagenum
];
$page = 1;
if ($pagenum > 1) {
$start = (($pagenum - 1) * $limit);
$page = $pagenum;
} else {
$start = 0;
}
// $user = $this->getUser();
// dd($user);
// $user = $id != null;
$download = isset($download);
// dd($download);
// Debut: Partie qui permet de compter a chaque fois le nombre de resultat en fonction de oui ou non il y'a une recherche (queryparam)
$total_data = $this->rpa->countAllMessages($id, $queryparam, $date, $statusmsg, true);
$allMessages = $this->rpa->getAllMessages($id, $queryparam, $date, $limit, $start, $statusmsg);
// dd($allMessages);
// Fin: Partie qui permet de compter a chaque fois le nombre de resultat
$limit = intval($limit);
// dd($user);
$output = '';
$selectAllDiv = '<span class="icheck-primary d-inline">
<input type="checkbox" name="selectAll" id="selectAll" value="0">
<label for="selectAll" class="mb-0"><span class="archive-text">' . (($statusmsg == '002') ? 'Restaurer' : (($statusmsg == '001') ? 'Renvoyer' : 'Archiver')) . '</span> Tout</label>
</span>';
// dd($role);
$msgcolumn = 'MSG';
$statuscolumn = '';
$receivercolumn = 'RECEIVER';
$statuscolumn = ($statusmsg == '000' || $statusmsg == '002') ? 'DATESEND' : 'DATVAL';
$output = '';
// dd(($user && !$download));
if ($view == '0') {
$archiveAllTh = '<th style="width: 15%">' . $selectAllDiv . '</th>';
$output .= '<div class="card card-primary card-outline">';
$output .= '<div class="card-body">';
$output .= '
<label id="totalrecords">Nbre total de messages : ' . $total_data . '</label>';
$output .= '
<table class="table table-hover table-bordered" id="table">
<thead class="thead-light">
<tr>
<th style="width: 45%">Message</th>
<th>Emetteur</th>
<th>Recepteur</th>
<th>Date d\'envoi</th>
' . ((!$download && $role != 'admin') ? $archiveAllTh : '') . '
</tr>
</thead>
<tbody>
';
if ($total_data > 0) {
foreach ($allMessages as $row) {
$sendertd ='<td>' . $row['SENDER'] . '</td>';
$archivetd = (!$download) && $role != 'admin' ? '<td>
<div class="icheck-primary d-inline">
<input type="checkbox" name="archives[]" id="' . $row['ID'] . '" value = ' . $row['ID'] . '>
<label for="' . $row['ID'] . '" class="mb-0"></label>
</div>
</td>' : '';
$output .= '
<tr>
<td><div>' . $row[$msgcolumn] . '</div></td>
' . $sendertd . '
<td>' . $row[$receivercolumn] . '</td>
<td>' . $row[$statuscolumn] . '</td>
' . $archivetd . '
</tr>
';
}
} else {
$output .= '
<tr>
<td colspan="5" align="center">' . 'Aucun message ne correspond aux criteres selectionnes' . '</td>
</tr>
';
}
$output .= '
</tbody>
</table>
<br />';
$total_data = $this->rpa->countAllMessages($id, $queryparam, $date, $statusmsg, false);
$output = $this->paginate($output, $total_data, $limit, $page);
$output .= '</div>';
$output .= '</div>';
} else {
$i = 0;
if ($total_data > 0) {
$output .= '
<label id="totalrecords">Total Records - ' . $total_data . '</label>';
foreach ($allMessages as $row) {
switch ($i % 3) {
case 0:
$output .= '<div class="row">';
$output = $this->col_sm_4($output, $download, $role, $row, $msgcolumn, $statuscolumn);
break;
case 1:
$output = $this->col_sm_4($output, $download, $role, $row, $msgcolumn, $statuscolumn);
break;
case 2:
$output = $this->col_sm_4($output, $download, $role, $row, $msgcolumn, $statuscolumn);
$output .= '</div>';
break;
}
$i++;
};
} else {
$output .= '<div class="alert-danger alert text-center">';
$output .= 'Aucune ligne ne correspond aux criteres recherches';
$output .= '</div>';
}
$total_data = $this->rpa->countAllMessages($id, $queryparam, $date, $statusmsg, false);
$output = $this->paginate($output, $total_data, $limit, $page);
}
// echo $output;
$data['output'] = $output;
return new JsonResponse($data);
}
/**
* @param string $output
* @param int $total_data
* @param int $limit
* @param mixed $page
* @return string
*/
public function paginate(string $output, int $total_data, int $limit, mixed $page): string
{
$output .= '
<ul class="pagination">
';
if ($total_data <= $limit) {
$total_links = ceil($limit / $limit);
} else {
$total_links = ceil($total_data / $limit);
}
$previous_link = '';
$next_link = '';
$page_link = '';
//Cette partie permet de charger tous notre tableau meme si la limite du premier
//Chargement etait a 5 ou meme 10.
//$total = $saleInvoiceRepository->findBy(['is_current' => false, 'is_archive' => true, 'state' => 'sale'], ['id' => 'DESC']);
//$totals = count($total);
//$total_links = ceil($totals / $limit);
if ($total_links > 4) {
if ($page < 5) {
for ($count = 1; $count <= 5; $count++) {
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
} else {
$end_limit = $total_links - 5;
if ($page > $end_limit) {
$page_array[] = 1;
$page_array[] = '...';
for ($count = $end_limit; $count <= $total_links; $count++) {
$page_array[] = $count;
}
} else {
$page_array[] = 1;
$page_array[] = '...';
for ($count = $page - 1; $count <= $page + 1; $count++) {
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
}
}
} else {
for ($count = 1; $count <= $total_links; $count++) {
$page_array[] = $count;
}
}
for ($count = 0; $count < count($page_array); $count++) {
if ($page == $page_array[$count]) {
$page_link .= '
<li class="page-item active">
<a class="page-link" href="#">' . $page_array[$count] . ' <span class="sr-only">(current)</span></a>
</li>
';
$previous_id = $page_array[$count] - 1;
if ($previous_id > 0) {
$previous_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' . $previous_id . '">Previous</a></li>';
} else {
$previous_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Previous</a>
</li>
';
}
$next_id = $page_array[$count] + 1;
if ($next_id >= $total_links) {
$next_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Next</a>
</li>
';
} else {
$next_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' . $next_id . '">Next</a></li>';
}
} else {
if ($page_array[$count] == '...') {
$page_link .= '
<li class="page-item disabled">
<a class="page-link" href="#">...</a>
</li>
';
} else {
$page_link .= '
<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' . $page_array[$count] . '">' . $page_array[$count] . '</a></li>
';
}
}
}
$output .= $previous_link . $page_link . $next_link;
$output .= '
</ul>
';
return $output;
}
/**
* @param string $output
* @param $user
* @param $row
* @param $msgcolumn
* @return string
*/
public function col_sm_4(string $output, $download, $role, $row, $msgcolumn, $statuscolumn): string
{
$output .= ' <div class="col-sm-4">';
$output .= ' <div class="card card-primary card-outline">';
$output .= ' <div class="card-header">';
$output .= ' <h5 class="card-title m-0">' . $row['SENDER'] . '</h5>';
$output .= ' <span class="float-right">' . $row[$statuscolumn] . '</span>';
$output .= ' </div>';
$output .= ' <div class="card-body">';
$output .= ' <p class="card-text">' . $row[$msgcolumn] . '</p>';
$output .= ' </div>';
$output .= ' <div class="card-footer">';
$output .= ( '<span class="float-left">' . $row['RECEIVER'] . '</span>');
$output .= (!$download && $role != 'admin') ? '<div class="icheck-primary d-inline float-right">
<input type="checkbox" name="archives[]" value="' . $row['ID'] . '" id="' . $row['ID'] . '"/>
<label for="' . $row['ID'] . '" class="mb-0"></label></div>' : '';
$output .= ' </div>';
$output .= ' </div>';
$output .= ' </div>';
return $output;
}
}