src/Application/Internit/ContentBundle/Controller/CoreController.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\ContentBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Sonata\AdminBundle\Controller\CoreController as BaseCoreContBroller;
  7. class CoreController extends BaseCoreContBroller
  8. {
  9.     private $dashboardActionRoles  = array('ROLE_MASTER_ADMIN''ROLE_CONSTRUTORA''ROLE_IMOBILIARIA''ROLE_CORRETOR''ROLE_AGENCIA');
  10.     public function dashboardAction()
  11.     {
  12.         $user $this->get('security.token_storage')->getToken()->getUser();
  13.         // construtora pode ver todos os imóveis e espelhos de vendas cadastrados no sistema,
  14.         // independente da imobiliária a qual o imóvel percente.
  15.         // userId para construtora será null. Assim ela poderá ver tudo
  16.         $userId in_array('ROLE_CONSTRUTORA'$user->getRoles()) ? null $user->getId();
  17.         if( $this->permissionByRole(['ROLE_MASTER_ADMIN'], $user) ){
  18.             //$realtys = $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardRealty($userId);
  19.             $realtys $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->findBy(['user' =>  $userId]);
  20.             $mirrors $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardMirror($userId);
  21.         }elseif( $this->permissionByRole(['ROLE_CONSTRUTORA'], $user) ) {
  22.             $realtys $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->findBy([ 'builder'=> $user->getBuilder()->getId() ]);
  23.             $mirrors = [];
  24.         }elseif( $this->permissionByRole(['ROLE_IMOBILIARIA''ROLE_CORRETOR'], $user) ) {
  25.             //$realtys = $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardRealty($userId);
  26.             //$realtys = $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->findBy(['user' =>  $userId]);
  27.             $realtys $user->getRealEstates()[0]->getRealys();
  28.             $mirrors $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardMirror($userId);
  29.         }elseif( $this->permissionByRole(['ROLE_AGENCIA'], $user) ) {
  30.             $realtys = [];
  31.             $mirrors = [];
  32.         }
  33.         $where = array();
  34.         $proposalOrder = array('createdAt' => 'DESC');
  35.         
  36.         if (in_array('ROLE_CORRETOR'$this->get('security.token_storage')->getToken()->getUser()->getRoles()))
  37.             $where = array('user'=>$this->get('security.token_storage')->getToken()->getUser()->getid());      
  38.         // usuário com perfil imobiliária vê todas as propostas abertas para empreendimentos
  39.         // da imobiliária
  40.         if (in_array('ROLE_IMOBILIARIA'$user->getRoles())) {
  41.             $proposals $this->getDoctrine()
  42.                 ->getRepository('ApplicationInternitProposalBundle:Proposal')
  43.                 ->findByUserRealEstates($user->getId());
  44.         }
  45.         else {
  46.             // usuário com perfil corretor e perfil construtora
  47.             $proposals $this->getDoctrine()
  48.                 ->getRepository('ApplicationInternitProposalBundle:Proposal')
  49.                 ->findBy($where$proposalOrder);
  50.         }
  51.         
  52.         // mostrar opções de imóveis das imobiliárias que o corretor faz parte
  53.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  54.             // construtora: vê todos os rankings do sistema
  55.             $rankings $this->getDoctrine()
  56.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  57.                 ->dashBoardRanking();
  58.         }
  59.         elseif (in_array('ROLE_IMOBILIARIA'$user->getRoles())) {
  60.             // imobiliária: vê os rankings dos imóveis da imobiliária
  61.             $rankings $this->getDoctrine()
  62.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  63.                 ->dashBoardRankingRealEstate($userId);
  64.         }
  65.         else {
  66.             // corretor: vê os rankings dos quais participa
  67.             $rankings $this->getDoctrine()
  68.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  69.                 ->dashBoardRankingUser($userId);
  70.         }
  71.         // Adicionando uma opção vazia ao array de opções
  72.         $rankings array_merge(
  73.             [ [ 'id' => '''realty' => 'Selecione um Empreendimento' ] ], 
  74.             $rankings
  75.         );
  76.         
  77.         //SALESGOAL
  78.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  79.             $data $this->getDoctrine()->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')->findAll();
  80.         }
  81.         else {
  82.             $data $this->getDoctrine()
  83.                 ->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')
  84.                 ->findByUserRealEstate($userId);
  85.         }
  86.         //$infoSaleGoal = $this->getDoctrine()->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')->showSalesGoal();
  87.         $infoSaleGoal = [];
  88.         //Edit Pós
  89.         $goals = array();
  90.         $value 0;
  91.         
  92.         $salesGoal = array();
  93.         foreach($data as $d)
  94.         {            
  95.             foreach($infoSaleGoal as $info)
  96.             {                   
  97.                 if($info['id'] == $d->getId())
  98.                 {   
  99.                     if($info['type']=='valor')       
  100.                     {         
  101.                         $value floatval($info['total_proposal']) + $value;
  102.                         $goals[$d->getId()]['total'] = $value;                                           
  103.                         $goals[$d->getId()]['points'][$info['month_year']] = floatval($info['total_proposal']);
  104.                     }
  105.                     elseif($info['type']=='unidades')
  106.                     {                        
  107.                         $value floatval($info['amount_proposal']) + $value;
  108.                         $goals[$d->getId()]['total'] = $value;                                           
  109.                         $goals[$d->getId()]['points'][$info['month_year']] = floatval($info['amount_proposal']);
  110.                     }
  111.                 }
  112.             }
  113.             $value 0;
  114.         }
  115.         $salesGoal['goals'] = $goals;
  116.         $salesGoal['datas'] = $data;
  117.         
  118.         //SALESGOAL        
  119.         /********************  QUIZ **********************/
  120.         $userId $this->getUser()->getId();
  121.         // obtém as respostas que o usuário fez aos quizes
  122.         $userAnswers $this->getDoctrine()->getRepository('ApplicationInternitQuizBundle:Quiz')
  123.             ->viewUserAnswers($userId);
  124.         // quizes que o usuário ainda não respondeu
  125.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  126.             $quizesToAnswer $this->getDoctrine()
  127.                 ->getRepository('ApplicationInternitQuizBundle:Quiz')
  128.                 ->viewUnansweredQuiz($userId);
  129.         }
  130.         else {
  131.             $quizesToAnswer $this->getDoctrine()
  132.                 ->getRepository('ApplicationInternitQuizBundle:Quiz')
  133.                 ->viewRealtorUnansweredQuiz($userId);
  134.         }
  135.         $numQuizesToAnswer count($quizesToAnswer);
  136.         $spentPoints $this->getDoctrine()
  137.             ->getRepository('ApplicationInternitQuizBundle:Quiz')->fetchSpentPoints($userId);
  138.         // para a última resposta do usuário
  139.         $lastUserAnswer = empty($userAnswers) ? null $userAnswers[0];
  140.         $lastUserAnswerHitsPercentage 0;
  141.         // para estatísticas de pontos e porcentagem de acertos totais
  142.         $totalScore 0;
  143.         $totalHits 0;
  144.         $totalHitsPercentage 0;
  145.         $totalNumAnswers 0;
  146.         $lastHits 0;
  147.         $lastNumAnswers 0;
  148.         foreach($userAnswers as $ua) {
  149.             $totalScore += $ua->getScore();
  150.             $totalHits += $ua->getNumHits();
  151.             $totalNumAnswers += count($ua->getAnswers());
  152.             if ($ua->getCreatedAt() > $lastUserAnswer->getCreatedAt()) {
  153.                 $lastUserAnswer $ua;
  154.             }
  155.         }
  156.         $availablePoints $totalScore $spentPoints;
  157.         if ($totalNumAnswers 0) {
  158.             $totalHitsPercentage = ($totalHits $totalNumAnswers) * 100;
  159.         }
  160.         if ($lastUserAnswer) {
  161.             $lastUserAnswerHitsPercentage $lastUserAnswer->getHitsPercentage();
  162.             $lastHits $lastUserAnswer->getNumHits();
  163.             $lastNumAnswers count($lastUserAnswer->getAnswers());
  164.         }
  165.         $randomRewards $this->getDoctrine()
  166.             ->getRepository('ApplicationInternitQuizBundle:Quiz')->viewRandomReward($userId2);
  167.         
  168.         /********************* FIM QUIZ **********************/
  169.         return $this->render(
  170.             '@ApplicationInternit/ContentBundle/Resources/views/home.html.twig',
  171.             [
  172.                 'realtys' => $realtys,
  173.                 'mirrors' => $mirrors,
  174.                 'proposals' => $proposals,
  175.                 'rankings' => $rankings,
  176.                 'salesGoal' => $salesGoal,
  177.                 'availablePoints' => $availablePoints,
  178.                 'totalHitsPercentage' => $totalHitsPercentage,
  179.                 'numQuizesToAnswer' => $numQuizesToAnswer,
  180.                 'lastAnswerHits' => $lastUserAnswerHitsPercentage,
  181.                 'randomRewards' => $randomRewards,
  182.                 'totalHits' => $totalHits,
  183.                 'totalNumAnswers' => $totalNumAnswers,
  184.                 'totalHitsPercentage' => $totalHitsPercentage,
  185.                 'lastHits' => $lastHits,
  186.                 'lastNumAnswers' => $lastNumAnswers,
  187.                 'lastUserAnswerHitsPercentage' => $lastUserAnswerHitsPercentage
  188.             ]
  189.         );
  190.     }
  191.     public function permissionByRole(array $modulesName$user)
  192.     {
  193.         if(in_array('ROLE_MASTER_ADMIN'$modulesName))
  194.             if( $this->isMasterAdmin($user->getRoles()) )
  195.                 return true;
  196.         foreach ($modulesName as $module)
  197.             if( in_array($module$user->getRoles()) )
  198.                 return true;
  199.         return false;
  200.     }
  201.     public function isMasterAdmin($userRoles){
  202.         $masterAdmin = [
  203.             "ROLE_SUPER_ADMIN",
  204.             "ROLE_CORRETOR",
  205.             "ROLE_IMOBILIARIA",
  206.             "ROLE_CONSTRUTORA",
  207.             "ROLE_AGENCIA",
  208.             "ROLE_USER"
  209.         ];
  210.         $isMaster 0;
  211.         foreach ($masterAdmin as $module)
  212.             if( in_array($module$userRoles) )
  213.                 $isMaster ++;
  214.         if($isMaster == 6)
  215.             return true;
  216.         return false;
  217.     }
  218. }