src/Services/Common/CounterTools.php line 398

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Common/CounterTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Common;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Security;
  8. use App\Entity\Access;
  9. use App\Entity\SocietyGroup;
  10. use App\Services\LogTools;
  11. class CounterTools
  12. {
  13.     public function __construct(ManagerRegistry $doctrineSecurity $securityLogTools $logTools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->security $security;
  17.         $this->logTools $logTools;
  18.         $this->debug true;
  19.     }
  20.     // Plan.io Task #4619
  21.     public function countExternalMessagesSms(SocietyGroup $societyGroupAccess $access)
  22.     {
  23.         $societies $access->getSocietiesAsString();
  24.         $sqlMainQuery "SELECT COUNT(DISTINCT external_message.id) AS counter
  25.             FROM external_message
  26.             INNER JOIN society ON external_message.society_id = society.id
  27.             INNER JOIN society_group ON society.society_group_id = society_group.id
  28.             INNER JOIN external_message_type ON external_message.external_message_type_id = external_message_type.id
  29.             INNER JOIN external_message_status ON external_message.external_message_status_id = external_message_status.id
  30.             WHERE external_message_type.sms = 1
  31.             AND external_message_status.unsolved = 1
  32.             AND society_group.id = :sg_id";
  33.         // Get data
  34.         $sql "";
  35.         $sqlQueries = array();
  36.         if (!$this->security->isGranted('list_external_messages_any'))
  37.         {
  38.             return 0;
  39.         }
  40.         else
  41.         {
  42.             if ($this->security->isGranted('list_external_messages'))
  43.             {
  44.             }
  45.             else
  46.             {
  47.                 $sqlMainQuery .= " ";
  48.                 $sqlMainQuery .= " AND society.id IN ".$societies;
  49.             }
  50.             $sqlQueries[] = $sqlMainQuery;
  51.         }
  52.         if (count($sqlQueries) > 0)
  53.         {
  54.             if (count($sqlQueries) == 1)
  55.             {
  56.                 $sql $sqlQueries[0];
  57.             }
  58.             else
  59.             {
  60.                 for ($pos 0$pos count($sqlQueries) - 1$pos++)
  61.                 {
  62.                     $sql .= $sqlQueries[$pos];
  63.                     $sql .= " UNION ";
  64.                 }
  65.                 $sql .= $sqlQueries[$pos];
  66.             }
  67.         }
  68.         if ($sql != "")
  69.         {
  70.             $conn $this->em->getConnection();
  71.             $stmt $conn->prepare($sqlMainQuery);
  72.             $stmt->bindValue('sg_id'$societyGroup->getId());
  73.             $queryResult $stmt->executeQuery();
  74.             $result $queryResult->fetchAssociative();
  75.             if (!empty($result['counter']))
  76.             {
  77.                 return intval($result['counter']);
  78.             }
  79.         }
  80.         return 0;
  81.     }
  82.     public function countMissions(SocietyGroup $societyGroupAccess $access)
  83.     {
  84.         // Plan.io Task #3961 : Add Shared Missions
  85.         // SQL Explanation for Shared Missions
  86.         /*
  87.         SELECT COUNT(DISTINCT mission.id) AS counter
  88.              FROM mission
  89.              INNER JOIN society ON mission.society_owner_id = society.id
  90.                 =>    JOIN on mission.societyOwner
  91.                 =>    This is used for permission : show counter only for the current user's societies
  92.              INNER JOIN mission_status ON mission.mission_status_id = mission_status.id
  93.                 =>    The mission_status is the one of the mission
  94.                 =>    These are the statuses in the SocietyGroup that shared the mission (SocietyGroupAuthor)
  95.             WHERE mission.society_group_owner_id = :sg_id
  96.                 =>    The missions are selected in the current group, but the JOIN is done on the SocietyGroupOwner id
  97.              AND mission.society_group_owner_id <> mission.society_group_author_id
  98.                 =>    Only shared missions
  99.              AND mission_status.code IN (
  100.                  SELECT code FROM mission_status WHERE mission_status.society_group_id = mission.society_group_owner_id AND mission_status.unsolved = 1
  101.              )
  102.                 =>    And now the fun part :)
  103.                 =>    We want the statuses from the SocietyGroupOwner, but which are unsolved in the SocietyGroupAuthor
  104.                 =>    Thus we select the code of the status from the SocietyGroupAuthor
  105.                 =>    and look for it in the unsolved codes of the statuses of the SocietyGroupOwner
  106.         */
  107.         $nbOwnMissions 0;
  108.         $nbSharedMissions 0;
  109.         $societies $access->getSocietiesAsString();
  110.         $missionSqls = array(
  111.             // Own Missions
  112.             "SELECT COUNT(DISTINCT mission.id) AS counter
  113.                 FROM mission
  114.                 INNER JOIN society ON mission.society_id = society.id
  115.                 INNER JOIN society_group ON society.society_group_id = society_group.id
  116.                 INNER JOIN mission_status ON mission.mission_status_id = mission_status.id
  117.                 WHERE mission_status.unsolved = 1
  118.                 AND society_group.id = :sg_id",
  119.             // Shared Missions
  120.             "SELECT COUNT(DISTINCT mission.id) AS counter
  121.                 FROM mission
  122.                 INNER JOIN society ON mission.society_owner_id = society.id
  123.                 INNER JOIN mission_status ON mission.mission_status_id = mission_status.id
  124.                 WHERE mission.society_group_owner_id = :sg_id
  125.                 AND mission.society_group_owner_id <> mission.society_group_author_id
  126.                 AND mission_status.code IN (
  127.                     SELECT code FROM mission_status WHERE mission_status.society_group_id = mission.society_group_owner_id AND mission_status.unsolved = 1
  128.                 )",
  129.         );
  130.         foreach ($missionSqls as $key => $missionSql)
  131.         {
  132.             // Get data
  133.             $sql "";
  134.             $sqlQueries = array();
  135.             if (!$this->security->isGranted('list_missions_any'))
  136.             {
  137.                 return 0;
  138.             }
  139.             else
  140.             {
  141.                 if ($this->security->isGranted('list_missions'))
  142.                 {
  143.                 }
  144.                 else
  145.                 {
  146.                     $missionSql .= " ";
  147.                     $missionSql .= " AND society.id IN ".$societies;
  148.                 }
  149.                 $sqlQueries[] = $missionSql;
  150.             }
  151.             if (count($sqlQueries) > 0)
  152.             {
  153.                 if (count($sqlQueries) == 1)
  154.                 {
  155.                     $sql $sqlQueries[0];
  156.                 }
  157.                 else
  158.                 {
  159.                     for ($pos 0$pos count($sqlQueries) - 1$pos++)
  160.                     {
  161.                         $sql .= $sqlQueries[$pos];
  162.                         $sql .= " UNION ";
  163.                     }
  164.                     $sql .= $sqlQueries[$pos];
  165.                 }
  166.             }
  167.             if ($sql != "")
  168.             {
  169.                 $conn $this->em->getConnection();
  170.                 $stmt $conn->prepare($missionSql);
  171.                 $stmt->bindValue('sg_id'$societyGroup->getId());
  172.                 $queryResult $stmt->executeQuery();
  173.                 $result $queryResult->fetchAssociative();
  174.                 if (!empty($result['counter']))
  175.                 {
  176.                     if ($key == 0)
  177.                     {
  178.                         $nbOwnMissions intval($result['counter']);
  179.                     }
  180.                     else
  181.                     {
  182.                         $nbSharedMissions intval($result['counter']);
  183.                     }
  184.                 }
  185.             }
  186.         }
  187.         // $this->logTools->ploopLog("nbOwnMissions = $nbOwnMissions");
  188.         // $this->logTools->ploopLog("nbSharedMissions = $nbSharedMissions");
  189.         return $nbOwnMissions $nbSharedMissions;
  190.     }
  191.     public function countDemands(SocietyGroup $societyGroupAccess $access)
  192.     {
  193.         $societies $access->getSocietiesAsString();
  194.         $demandSql "SELECT COUNT(DISTINCT demand.id) AS counter
  195.             FROM demand
  196.             INNER JOIN society ON demand.society_id = society.id
  197.             INNER JOIN society_group ON society.society_group_id = society_group.id
  198.             INNER JOIN demand_status ON demand.demand_status_id = demand_status.id
  199.             WHERE demand_status.unsolved = 1
  200.             AND society_group.id = :sg_id";
  201.         // Get data
  202.         $sql "";
  203.         $sqlQueries = array();
  204.         if (!$this->security->isGranted('list_demands_any'))
  205.         {
  206.             return 0;
  207.         }
  208.         else
  209.         {
  210.             if ($this->security->isGranted('list_demands'))
  211.             {
  212.             }
  213.             else
  214.             {
  215.                 $demandSql .= " ";
  216.                 $demandSql .= " AND society.id IN ".$societies;
  217.             }
  218.             $sqlQueries[] = $demandSql;
  219.         }
  220.         if (count($sqlQueries) > 0)
  221.         {
  222.             if (count($sqlQueries) == 1)
  223.             {
  224.                 $sql $sqlQueries[0];
  225.             }
  226.             else
  227.             {
  228.                 for ($pos 0$pos count($sqlQueries) - 1$pos++)
  229.                 {
  230.                     $sql .= $sqlQueries[$pos];
  231.                     $sql .= " UNION ";
  232.                 }
  233.                 $sql .= $sqlQueries[$pos];
  234.             }
  235.         }
  236.         if ($sql != "")
  237.         {
  238.             $conn $this->em->getConnection();
  239.             $stmt $conn->prepare($demandSql);
  240.             $stmt->bindValue('sg_id'$societyGroup->getId());
  241.             $queryResult $stmt->executeQuery();
  242.             $result $queryResult->fetchAssociative();
  243.             if (!empty($result['counter']))
  244.             {
  245.                 return intval($result['counter']);
  246.             }
  247.         }
  248.         return 0;
  249.     }
  250.     public function countIkeaServiceOrders(SocietyGroup $societyGroupAccess $access)
  251.     {
  252.         $societies $access->getSocietiesAsString();
  253.         $demandSql "SELECT COUNT(DISTINCT ikea_service_order.id) AS counter
  254.             FROM ikea_service_order
  255.             INNER JOIN society ON ikea_service_order.society_id = society.id
  256.             INNER JOIN society_group ON society.society_group_id = society_group.id
  257.             INNER JOIN ikea_service_order_status ON ikea_service_order.service_order_status_id = ikea_service_order_status.id
  258.             WHERE ikea_service_order_status.unsolved = 1
  259.             AND society_group.id = :sg_id";
  260.         // Get data
  261.         $sql "";
  262.         $sqlQueries = array();
  263.         if (!$this->security->isGranted('list_ikea_service_orders_any'))
  264.         {
  265.             return 0;
  266.         }
  267.         else
  268.         {
  269.             if ($this->security->isGranted('list_ikea_service_orders'))
  270.             {
  271.             }
  272.             else
  273.             {
  274.                 $demandSql .= " ";
  275.                 $demandSql .= " AND society.id IN ".$societies;
  276.             }
  277.             $sqlQueries[] = $demandSql;
  278.         }
  279.         if (count($sqlQueries) > 0)
  280.         {
  281.             if (count($sqlQueries) == 1)
  282.             {
  283.                 $sql $sqlQueries[0];
  284.             }
  285.             else
  286.             {
  287.                 for ($pos $pos count($sqlQueries) - $pos++)
  288.                 {
  289.                     $sql .= $sqlQueries[$pos];
  290.                     $sql .= " UNION ";
  291.                 }
  292.                 $sql .= $sqlQueries[$pos];
  293.             }
  294.         }
  295.         if ($sql != "")
  296.         {
  297.             $conn $this->em->getConnection();
  298.             $stmt $conn->prepare($demandSql);
  299.             $stmt->bindValue('sg_id'$societyGroup->getId());
  300.             $queryResult $stmt->executeQuery();
  301.             $result $queryResult->fetchAssociative();
  302.             if (!empty($result['counter']))
  303.             {
  304.                 return intval($result['counter']);
  305.             }
  306.         }
  307.         return 0;
  308.     }
  309.     public function countNotifications(Access $access)
  310.     {
  311.         $accessId $access->getId();
  312.         $notificationSql =
  313.             "SELECT
  314.               COUNT(DISTINCT notification.id) AS counter
  315.             FROM
  316.               ding_notification as notification
  317.               INNER JOIN access ON notification.access_id = access.id
  318.             WHERE
  319.               access_id = :id_access
  320.               and notification.seen = 0";
  321.         $conn $this->em->getConnection();
  322.         $stmt $conn->prepare($notificationSql);
  323.         $stmt->bindValue('id_access'$accessId);
  324.         $queryResult $stmt->executeQuery();
  325.         $result $queryResult->fetchAssociative();
  326.         $counter 0;
  327.         if (!empty($result['counter']))
  328.         {
  329.             $counter intval($result['counter']);
  330.         }
  331.         return $counter;
  332.     }
  333.     // Plan.io Task #3865
  334.     public function countAdvancedNotifications(Access $access)
  335.     {
  336.         $societyGroup $access->getSocietyGroup();
  337.         $societies $access->getSocietiesAsString();
  338.         $dingSql "SELECT COUNT(ding.id) as counter
  339.             FROM ding_advanced_notification ding
  340.             INNER JOIN society ON society.id = ding.society_id
  341.             INNER JOIN society_group ON society_group.id = society.society_group_id
  342.             WHERE society_group.id = :sg_id";
  343.         // Get data
  344.         if (!$this->security->isGranted('list_advanced_notifications_any'))
  345.         {
  346.             return 0;
  347.         }
  348.         else
  349.         {
  350.             if ($this->security->isGranted('list_advanced_notifications'))
  351.             {
  352.             }
  353.             else
  354.             {
  355.                 $dingSql .= " ";
  356.                 $dingSql .= " AND society.id IN ".$societies;
  357.             }
  358.         }
  359.         $conn $this->em->getConnection();
  360.         $stmt $conn->prepare($dingSql);
  361.         $stmt->bindValue('sg_id'$societyGroup->getId());
  362.         $queryResult $stmt->executeQuery();
  363.         $result $queryResult->fetchAssociative();
  364.         if (!empty($result['counter']))
  365.         {
  366.             return intval($result['counter']);
  367.         }
  368.         return 0;
  369.     }
  370.     public function countApplications(SocietyGroup $societyGroupAccess $access)
  371.     {
  372.         $societies $access->getSocietiesAsString();
  373.         $applicationSql "SELECT COUNT(application.id) as counter
  374.             FROM application
  375.             INNER JOIN application_status as status ON status.id = application.status_id
  376.             INNER JOIN society ON society.id = application.society_id
  377.             INNER JOIN society_group ON society_group.id = society.society_group_id
  378.             LEFT JOIN human_resource ON application.human_resource_id = human_resource.id
  379.             WHERE status.archived = 0
  380.             AND human_resource.id IS NULL
  381.             AND society_group.id = :sg_id";
  382.         // Get data
  383.         $sql "";
  384.         $sqlQueries = array();
  385.         if (!$this->security->isGranted('list_applications_any'))
  386.         {
  387.             return 0;
  388.         }
  389.         else
  390.         {
  391.             if ($this->security->isGranted('list_applications'))
  392.             {
  393.             }
  394.             else
  395.             {
  396.                 $applicationSql .= " ";
  397.                 $applicationSql .= " AND society.id IN ".$societies;
  398.             }
  399.             $sqlQueries[] = $applicationSql;
  400.         }
  401.         if (count($sqlQueries) > 0)
  402.         {
  403.             if (count($sqlQueries) == 1)
  404.             {
  405.                 $sql $sqlQueries[0];
  406.             }
  407.             else
  408.             {
  409.                 for ($pos 0$pos count($sqlQueries) - 1$pos++)
  410.                 {
  411.                     $sql .= $sqlQueries[$pos];
  412.                     $sql .= " UNION ";
  413.                 }
  414.                 $sql .= $sqlQueries[$pos];
  415.             }
  416.         }
  417.         if ($sql != "")
  418.         {
  419.             $conn $this->em->getConnection();
  420.             $stmt $conn->prepare($applicationSql);
  421.             $stmt->bindValue('sg_id'$societyGroup->getId());
  422.             $queryResult $stmt->executeQuery();
  423.             $result $queryResult->fetchAssociative();
  424.             if (!empty($result['counter']))
  425.             {
  426.                 return intval($result['counter']);
  427.             }
  428.         }
  429.         return 0;
  430.     }
  431.     // Task plan.io #3847
  432.     public function countCosts(SocietyGroup $societyGroupAccess $access)
  433.     {
  434.         $societies $access->getSocietiesAsString();
  435.         $costSql "SELECT COUNT(DISTINCT cost.id) AS nb
  436.             FROM cost
  437.             INNER JOIN society ON cost.society_id = society.id
  438.             INNER JOIN society_group ON society.society_group_id = society_group.id
  439.             INNER JOIN cost_status ON cost.status_id = cost_status.id
  440.             INNER JOIN access ON cost.access_id = access.id
  441.             INNER JOIN access author ON cost.author_access_id = author.id
  442.             WHERE cost_status.unsolved = 1
  443.             AND cost.parent_id is NULL
  444.             AND society_group.id = :sg_id";
  445.         if ($this->security->isGranted('list_costs_any'))
  446.         {
  447.             if ($this->security->isGranted('list_costs'))
  448.             {
  449.             }
  450.             else
  451.             {
  452.                 if ($this->security->isGranted('list_costs_society'))
  453.                 {
  454.                     $costSql .= " ";
  455.                     $costSql .= " AND society.id IN ".$societies;
  456.                 }
  457.                 else
  458.                 {
  459.                     if ($this->security->isGranted('list_costs_own'))
  460.                     {
  461.                         $costSql .= " ";
  462.                         $costSql .= " AND (access.id = " $access->getId() . " || author.id = " $access->getId() . ")";
  463.                     }
  464.                 }
  465.             }
  466.         }
  467.         else
  468.         {
  469.             return 0;
  470.         }
  471.         if ($costSql != "")
  472.         {
  473.             $conn $this->em->getConnection();
  474.             $stmt $conn->prepare($costSql);
  475.             $stmt->bindValue('sg_id'$societyGroup->getId());
  476.             $queryResult $stmt->executeQuery();
  477.             $result $queryResult->fetchAssociative();
  478.             if (!empty($result['nb']))
  479.             {
  480.                 return intval($result['nb']);
  481.             }
  482.         }
  483.         return 0;
  484.     }
  485. }
  486. /*
  487. select mission.id, mission.ref, mission_status.value, mission_status.code
  488. from mission
  489. join mission_status on (mission_status.id = mission.mission_status_id)
  490. where mission.society_group_owner_id=263 and mission.society_group_author_id <> mission.society_group_owner_id
  491. and mission_status.code in (select code from mission_status where mission_status.society_group_id = 263 and mission_status.unsolved = 1)
  492. */