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