src/Controller/UserDataTableController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Repository\AdminRepository;
  5. use App\Repository\EnterpriseRepository;
  6. use App\Repository\UserRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class UserDataTableController extends AbstractController
  13. {
  14.     private $em;
  15.     private $id;
  16.     private $rpe;
  17.     private $rpu;
  18.     private $rpa;
  19.     /**
  20.      * @param $em
  21.      * @param $rps
  22.      * @param $rpe
  23.      * @param $rpu
  24.      * @param $conn
  25.      */
  26.     public function __construct(EntityManagerInterface $emEnterpriseRepository $rpeUserRepository $rpuAdminRepository $rpa)
  27.     {
  28.         $this->em $em;
  29.         $this->rpe $rpe;
  30.         $this->rpu $rpu;
  31.         $this->rpa $rpa;
  32.     }
  33.     #[Route('/user/history/table'name'user_history_table'methods: ['GET'])]
  34.     public function table(): JSonResponse
  35.     {
  36.         extract($_GET);
  37.         $data = [
  38.             'form' => '',
  39.             'status' => 0,
  40.             'pagenum' => $pagenum
  41.         ];
  42.         $page 1;
  43.         if ($pagenum 1) {
  44.             $start = (($pagenum 1) * $limit);
  45.             $page $pagenum;
  46.         } else {
  47.             $start 0;
  48.         }
  49. //        $user = $this->getUser();
  50. //        dd($user);
  51. //        $user = $id != null;
  52.         $download = isset($download);
  53. //        dd($download);
  54.         // 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)
  55.         $total_data $this->rpa->countAllMessages($id$queryparam$date$statusmsgtrue);
  56.         $allMessages $this->rpa->getAllMessages($id$queryparam$date$limit$start$statusmsg);
  57. //        dd($allMessages);
  58.         // Fin: Partie qui permet de compter a chaque fois le nombre de resultat
  59.         $limit intval($limit);
  60. //        dd($user);
  61.         $output '';
  62.         $selectAllDiv '<span class="icheck-primary d-inline">
  63.             <input type="checkbox" name="selectAll" id="selectAll" value="0">
  64.             <label for="selectAll" class="mb-0"><span class="archive-text">' . (($statusmsg == '002') ? 'Restaurer' : (($statusmsg == '001') ? 'Renvoyer' 'Archiver')) . '</span> Tout</label>
  65.                 
  66.             </span>';
  67. //        dd($role);
  68.         $msgcolumn 'MSG';
  69.         $statuscolumn '';
  70.         $receivercolumn 'RECEIVER';
  71.         $statuscolumn = ($statusmsg == '000' || $statusmsg == '002') ? 'DATESEND' 'DATVAL';
  72.         $output '';
  73. //        dd(($user && !$download));
  74.         if ($view == '0') {
  75.             $archiveAllTh '<th style="width: 15%">' $selectAllDiv '</th>';
  76.             $output .= '<div class="card card-primary card-outline">';
  77.             $output .= '<div class="card-body">';
  78.             $output .= '
  79.         <label id="totalrecords">Nbre total de messages : ' $total_data '</label>';
  80.             $output .= '
  81.         <table class="table table-hover table-bordered" id="table">
  82.         <thead class="thead-light">
  83.           <tr>
  84.             <th style="width: 45%">Message</th>
  85.             <th>Emetteur</th>
  86.             <th>Recepteur</th>
  87.             <th>Date d\'envoi</th>
  88.           ' . ((!$download && $role != 'admin') ? $archiveAllTh '') . '
  89.             
  90.             
  91.           </tr>
  92.           </thead>
  93.           
  94.           <tbody>
  95.         ';
  96.             if ($total_data 0) {
  97.                 foreach ($allMessages as $row) {
  98.                     $sendertd ='<td>' $row['SENDER'] . '</td>';
  99.                     $archivetd = (!$download) && $role != 'admin' '<td>
  100.               <div class="icheck-primary d-inline">
  101.               <input type="checkbox" name="archives[]" id="' $row['ID'] . '" value = ' $row['ID'] . '>
  102.                                  <label for="' $row['ID'] . '" class="mb-0"></label>
  103.                  </div>
  104.             </td>' '';
  105.                     $output .= '
  106.             <tr>
  107.               <td><div>' $row[$msgcolumn] . '</div></td>
  108.               ' $sendertd '
  109.               <td>' $row[$receivercolumn] . '</td>
  110.               <td>' $row[$statuscolumn] . '</td>
  111.               
  112.               ' $archivetd '
  113.             </tr>
  114.             
  115.             ';
  116.                 }
  117.             } else {
  118.                 $output .= '
  119.           <tr>
  120.             <td colspan="5" align="center">' 'Aucun message ne correspond aux criteres selectionnes' '</td>
  121.           </tr>
  122.           
  123.           ';
  124.             }
  125.             $output .= '
  126.           </tbody>
  127.         </table>
  128.         <br />';
  129.             $total_data $this->rpa->countAllMessages($id$queryparam$date$statusmsgfalse);
  130.             $output $this->paginate($output$total_data$limit$page);
  131.             $output .= '</div>';
  132.             $output .= '</div>';
  133.         } else {
  134.             $i 0;
  135.             if ($total_data 0) {
  136.                 $output .= '
  137.         <label id="totalrecords">Total Records - ' $total_data '</label>';
  138.                 foreach ($allMessages as $row) {
  139.                     switch ($i 3) {
  140.                         case 0:
  141.                             $output .= '<div class="row">';
  142.                             $output $this->col_sm_4($output$download$role$row$msgcolumn$statuscolumn);
  143.                             break;
  144.                         case 1:
  145.                             $output $this->col_sm_4($output$download$role$row$msgcolumn$statuscolumn);
  146.                             break;
  147.                         case 2:
  148.                             $output $this->col_sm_4($output$download$role$row$msgcolumn$statuscolumn);
  149.                             $output .= '</div>';
  150.                             break;
  151.                     }
  152.                     $i++;
  153.                 };
  154.             } else {
  155.                 $output .= '<div class="alert-danger alert text-center">';
  156.                 $output .= 'Aucune ligne ne correspond aux criteres recherches';
  157.                 $output .= '</div>';
  158.             }
  159.             $total_data $this->rpa->countAllMessages($id$queryparam$date$statusmsgfalse);
  160.             $output $this->paginate($output$total_data$limit$page);
  161.         }
  162. //        echo $output;
  163.         $data['output'] = $output;
  164.         return new JsonResponse($data);
  165.     }
  166.     /**
  167.      * @param string $output
  168.      * @param int $total_data
  169.      * @param int $limit
  170.      * @param mixed $page
  171.      * @return string
  172.      */
  173.     public function paginate(string $outputint $total_dataint $limitmixed $page): string
  174.     {
  175.         $output .= '
  176.           <ul class="pagination">
  177.         ';
  178.         if ($total_data <= $limit) {
  179.             $total_links ceil($limit $limit);
  180.         } else {
  181.             $total_links ceil($total_data $limit);
  182.         }
  183.         $previous_link '';
  184.         $next_link '';
  185.         $page_link '';
  186.         //Cette partie permet de charger tous notre tableau meme si la limite du premier
  187.         //Chargement etait a 5 ou meme 10.
  188.         //$total = $saleInvoiceRepository->findBy(['is_current' => false, 'is_archive' => true, 'state' => 'sale'], ['id' => 'DESC']);
  189.         //$totals = count($total);
  190.         //$total_links = ceil($totals / $limit);
  191.         if ($total_links 4) {
  192.             if ($page 5) {
  193.                 for ($count 1$count <= 5$count++) {
  194.                     $page_array[] = $count;
  195.                 }
  196.                 $page_array[] = '...';
  197.                 $page_array[] = $total_links;
  198.             } else {
  199.                 $end_limit $total_links 5;
  200.                 if ($page $end_limit) {
  201.                     $page_array[] = 1;
  202.                     $page_array[] = '...';
  203.                     for ($count $end_limit$count <= $total_links$count++) {
  204.                         $page_array[] = $count;
  205.                     }
  206.                 } else {
  207.                     $page_array[] = 1;
  208.                     $page_array[] = '...';
  209.                     for ($count $page 1$count <= $page 1$count++) {
  210.                         $page_array[] = $count;
  211.                     }
  212.                     $page_array[] = '...';
  213.                     $page_array[] = $total_links;
  214.                 }
  215.             }
  216.         } else {
  217.             for ($count 1$count <= $total_links$count++) {
  218.                 $page_array[] = $count;
  219.             }
  220.         }
  221.         for ($count 0$count count($page_array); $count++) {
  222.             if ($page == $page_array[$count]) {
  223.                 $page_link .= '
  224.             <li class="page-item active">
  225.               <a class="page-link" href="#">' $page_array[$count] . ' <span class="sr-only">(current)</span></a>
  226.             </li>
  227.             ';
  228.                 $previous_id $page_array[$count] - 1;
  229.                 if ($previous_id 0) {
  230.                     $previous_link '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' $previous_id '">Previous</a></li>';
  231.                 } else {
  232.                     $previous_link '
  233.               <li class="page-item disabled">
  234.                 <a class="page-link" href="#">Previous</a>
  235.               </li>
  236.               ';
  237.                 }
  238.                 $next_id $page_array[$count] + 1;
  239.                 if ($next_id >= $total_links) {
  240.                     $next_link '
  241.               <li class="page-item disabled">
  242.                 <a class="page-link" href="#">Next</a>
  243.               </li>
  244.                 ';
  245.                 } else {
  246.                     $next_link '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' $next_id '">Next</a></li>';
  247.                 }
  248.             } else {
  249.                 if ($page_array[$count] == '...') {
  250.                     $page_link .= '
  251.               <li class="page-item disabled">
  252.                   <a class="page-link" href="#">...</a>
  253.               </li>
  254.               ';
  255.                 } else {
  256.                     $page_link .= '
  257.               <li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="' $page_array[$count] . '">' $page_array[$count] . '</a></li>
  258.               ';
  259.                 }
  260.             }
  261.         }
  262.         $output .= $previous_link $page_link $next_link;
  263.         $output .= '
  264.           </ul>
  265.         ';
  266.         return $output;
  267.     }
  268.     /**
  269.      * @param string $output
  270.      * @param $user
  271.      * @param $row
  272.      * @param $msgcolumn
  273.      * @return string
  274.      */
  275.     public function col_sm_4(string $output$download$role$row$msgcolumn$statuscolumn): string
  276.     {
  277.         $output .= '  <div class="col-sm-4">';
  278.         $output .= '    <div class="card card-primary card-outline">';
  279.         $output .= '       <div class="card-header">';
  280.         $output .= '         <h5 class="card-title m-0">' .  $row['SENDER'] . '</h5>';
  281.         $output .= '         <span class="float-right">' $row[$statuscolumn] . '</span>';
  282.         $output .= '       </div>';
  283.         $output .= '       <div class="card-body">';
  284.         $output .= '         <p class="card-text">' $row[$msgcolumn] . '</p>';
  285.         $output .= '       </div>';
  286.         $output .= '       <div class="card-footer">';
  287.         $output .= ( '<span class="float-left">' $row['RECEIVER'] . '</span>');
  288.         $output .= (!$download && $role != 'admin') ? '<div class="icheck-primary d-inline float-right">
  289.                                <input type="checkbox" name="archives[]" value="' $row['ID'] . '" id="' $row['ID'] . '"/>
  290.                                <label for="' $row['ID'] . '" class="mb-0"></label></div>' '';
  291.         $output .= '       </div>';
  292.         $output .= '     </div>';
  293.         $output .= '   </div>';
  294.         return $output;
  295.     }
  296. }