src/Services/Config/ModuleTools.php line 18

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Config/ModuleTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Config;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Entity\Access;
  8. use App\Entity\SocietyGroup;
  9. use App\Entity\Config\Config;
  10. use App\Entity\Config\Module;
  11. use App\Entity\Config\ModuleConfig;
  12. use App\Services\LogTools;
  13. class ModuleTools
  14. {
  15.     public function __construct(ManagerRegistry $doctrine, LogTools $logTools)
  16.     {
  17.         $this->em = $doctrine->getManager();
  18.         $this->logTools = $logTools;
  19.     }
  20.     // Plan.io Task #3309
  21.     // Returns TRUE if the Guard is restricted according to active modules
  22.     // Returns FALSE otherwise
  23.     public function restrictGuardian(SocietyGroup $societyGroup, $modules)
  24.     {
  25.         // Test FREE_GUARD config
  26.         if ($this->applyGuardRestrictions())
  27.         {
  28.             // Test Module
  29.             if ($this->areInactiveByCode($societyGroup, $modules))
  30.             {
  31.                 // All Modules are inactive
  32.                 return true;
  33.             }
  34.         }
  35.         return false;
  36.     }
  37.     // Plan.io Task #3309
  38.     // Config :: $const FREE_GUARD
  39.     // FREE_GUARD = 1
  40.     //     - No restrictions are applied when creating a society group, or when executing the guard
  41.     //     - Default value in `matrix_global.yml`
  42.     // FREE_GUARD = 0
  43.     //     - Module related restrictions are applied when creating a society group, and when executing the guard
  44.     public function applyGuardRestrictions()
  45.     {
  46.         $config = $this->em->getRepository(Config::class)->findOneByName(Config::FREE_GUARD);
  47.         if ($config === null)
  48.         {
  49.             return false;
  50.         }
  51.         if ($config->getValue())
  52.         {
  53.             return false;
  54.         }
  55.         return true;
  56.     }
  57.     // Returns TRUE if all the Codes are INACTIVE
  58.     // Returns FALSE if at least one Code is ACTIVE
  59.     // We want to test if all the codes are inactive
  60.     // code1.inactive && code2.inactive && code3.inactive
  61.     // Therefore a single active code breaks the chain
  62.     public function areInactiveByCode(SocietyGroup $societyGroup, $codes)
  63.     {
  64.         $allInactive = true;
  65.         foreach ($codes as $code)
  66.         {
  67.             if ($this->isActiveByCode($societyGroup, $code))
  68.             {
  69.                 $allInactive = false;
  70.                 break;
  71.             }
  72.         }
  73.         return $allInactive;
  74.     }
  75.     public function isActiveByCode(SocietyGroup $societyGroup, $code)
  76.     {
  77.         $module = $this->em->getRepository(Module::class)->findOneByCode($code);
  78.         if ($module === null)
  79.         {
  80.             return false;
  81.         }
  82.         return $this->isActive($societyGroup, $module);
  83.     }
  84.     public function isInactiveByCode(SocietyGroup $societyGroup, $code)
  85.     {
  86.         return !$this->isActiveByCode($societyGroup, $code);
  87.     }
  88.     public function isActive(SocietyGroup $societyGroup, Module $module)
  89.     {
  90.         // Fetch them from the SocietyGroup instead of the database
  91.         // This way we can test the module even if the SocietyGroup
  92.         // has not been persisted to the database yet
  93.         return $societyGroup->hasModule($module);
  94.     }
  95.     public function getConfig(SocietyGroup $societyGroup, Module $module)
  96.     {
  97.         $config = $this->em->getRepository(ModuleConfig::class)
  98.             ->findOneBy(array(
  99.                 'societyGroup'        =>    $societyGroup,
  100.                 'module'            =>    $module,
  101.             ));
  102.         if ($config !== null)
  103.         {
  104.             return $config;
  105.         }
  106.         return null;
  107.     }
  108.     public function isActiveTrustpilot(SocietyGroup $societyGroup)
  109.     {
  110.         return $this->isActiveByCode($societyGroup, Module::MODULE_TRUSTPILOT);
  111.     }
  112.     public function isActiveClientAccount(SocietyGroup $societyGroup)
  113.     {
  114.         return $this->isActiveByCode($societyGroup, Module::MODULE_CLIENT_ACCOUNT);
  115.     }
  116.     public function isActiveRHForm(SocietyGroup $societyGroup)
  117.     {
  118.         return $this->isActiveByCode($societyGroup, Module::MODULE_RHFORM);
  119.     }
  120.     public function isActiveInvoice(SocietyGroup $societyGroup)
  121.     {
  122.         return $this->isActiveByCode($societyGroup, Module::MODULE_INVOICE);
  123.     }
  124.     public function isActiveEInvoicing(SocietyGroup $societyGroup)
  125.     {
  126.         return $this->isActiveByCode($societyGroup, Module::MODULE_EINVOICING);
  127.     }
  128.     public function isActiveIkeaServiceOrder(SocietyGroup $societyGroup)
  129.     {
  130.         return $this->isActiveByCode($societyGroup, Module::MODULE_IKEA_SERVICE_ORDER);
  131.     }
  132.     public function isActiveIkeaKitchenPlanner(SocietyGroup $societyGroup)
  133.     {
  134.         return $this->isActiveByCode($societyGroup, Module::MODULE_IKEA_KITCHEN_PLANNER);
  135.     }
  136.     public function isActiveDevis(SocietyGroup $societyGroup)
  137.     {
  138.         return $this->isActiveByCode($societyGroup, Module::MODULE_DEVIS);
  139.     }
  140.     public function isActiveDocument(SocietyGroup $societyGroup)
  141.     {
  142.         return $this->isActiveByCode($societyGroup, Module::MODULE_DOCUMENT);
  143.     }
  144.     public function isActiveProspect(SocietyGroup $societyGroup)
  145.     {
  146.         return $this->isActiveByCode($societyGroup, Module::MODULE_PROSPECT);
  147.     }
  148.     public function isActivePunchOut(SocietyGroup $societyGroup)
  149.     {
  150.         return $this->isActiveByCode($societyGroup, Module::MODULE_PUNCHOUT);
  151.     }
  152.     public function isActiveDemand(SocietyGroup $societyGroup)
  153.     {
  154.         return $this->isActiveByCode($societyGroup, Module::MODULE_DEMAND);
  155.     }
  156.     public function isActiveApplication(SocietyGroup $societyGroup)
  157.     {
  158.         $codes = array(
  159.             Module::MODULE_APPLICATION,
  160.         );
  161.         // One single positve answer is enough
  162.         foreach ($codes as $code)
  163.         {
  164.             if ($this->isActiveByCode($societyGroup, $code))
  165.             {
  166.                 return true;
  167.             }
  168.         }
  169.         // If we are here all hope is lost
  170.         return false;
  171.     }
  172.     public function isActiveMission(SocietyGroup $societyGroup)
  173.     {
  174.         $codes = array(
  175.             Module::MODULE_MISSION,
  176.             Module::MODULE_MISSION_LIGHT,
  177.             Module::MODULE_MISSION_PLUS,
  178.         );
  179.         // One single positve answer is enough
  180.         foreach ($codes as $code)
  181.         {
  182.             if ($this->isActiveByCode($societyGroup, $code))
  183.             {
  184.                 return true;
  185.             }
  186.         }
  187.         // If we are here all hope is lost
  188.         return false;
  189.     }
  190.     public function isActiveClientOrMission(SocietyGroup $societyGroup)
  191.     {
  192.         $codes = array(
  193.             Module::MODULE_CLIENT,
  194.             Module::MODULE_CLIENT_LIGHT,
  195.             Module::MODULE_MISSION,
  196.             Module::MODULE_MISSION_LIGHT,
  197.             Module::MODULE_MISSION_PLUS,
  198.         );
  199.         // One single positve answer is enough
  200.         foreach ($codes as $code)
  201.         {
  202.             if ($this->isActiveByCode($societyGroup, $code))
  203.             {
  204.                 return true;
  205.             }
  206.         }
  207.         // If we are here all hope is lost
  208.         return false;
  209.     }
  210.     public function isActiveMissionAndDevis(SocietyGroup $societyGroup)
  211.     {
  212.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_DEVIS))
  213.         {
  214.             // No need to go any further
  215.             return false;
  216.         }
  217.         // If we are here we know that the Devis module is active
  218.         // Only one mission option is required
  219.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
  220.         {
  221.             return true;
  222.         }
  223.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
  224.         {
  225.             return true;
  226.         }
  227.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
  228.         {
  229.             return true;
  230.         }
  231.         // If we are here all hope is lost
  232.         return false;
  233.     }
  234.     public function isActiveMissionAndInvoice(SocietyGroup $societyGroup)
  235.     {
  236.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_INVOICE))
  237.         {
  238.             // No need to go any further
  239.             return false;
  240.         }
  241.         // If we are here we know that the Invoice module is active
  242.         // Only one mission option is required
  243.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
  244.         {
  245.             return true;
  246.         }
  247.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
  248.         {
  249.             return true;
  250.         }
  251.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
  252.         {
  253.             return true;
  254.         }
  255.         // If we are here all hope is lost
  256.         return false;
  257.     }
  258.     public function isActiveMissionAndPlanning(SocietyGroup $societyGroup)
  259.     {
  260.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_PLANNING))
  261.         {
  262.             // No need to go any further
  263.             return false;
  264.         }
  265.         // If we are here we know that the Planning module is active
  266.         // Only one mission option is required
  267.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
  268.         {
  269.             return true;
  270.         }
  271.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
  272.         {
  273.             return true;
  274.         }
  275.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
  276.         {
  277.             return true;
  278.         }
  279.         // If we are here all hope is lost
  280.         return false;
  281.     }
  282.     public function isActiveMissionAndDocument(SocietyGroup $societyGroup)
  283.     {
  284.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_DOCUMENT))
  285.         {
  286.             // No need to go any further
  287.             return false;
  288.         }
  289.         // If we are here we know that the Document module is active
  290.         // Only one mission option is required
  291.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
  292.         {
  293.             return true;
  294.         }
  295.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
  296.         {
  297.             return true;
  298.         }
  299.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
  300.         {
  301.             return true;
  302.         }
  303.         // If we are here all hope is lost
  304.         return false;
  305.     }
  306.     public function isActiveMissionAndInvoiceAndDocument(SocietyGroup $societyGroup)
  307.     {
  308.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_INVOICE))
  309.         {
  310.             // No need to go any further
  311.             return false;
  312.         }
  313.         if ($this->isInactiveByCode($societyGroup, Module::MODULE_DOCUMENT))
  314.         {
  315.             // No need to go any further
  316.             return false;
  317.         }
  318.         // If we are here we know that the Invoice and Document modules are active
  319.         // Only one mission option is required
  320.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
  321.         {
  322.             return true;
  323.         }
  324.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
  325.         {
  326.             return true;
  327.         }
  328.         if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
  329.         {
  330.             return true;
  331.         }
  332.         // If we are here all hope is lost
  333.         return false;
  334.     }
  335.     // Plan.io Task #3631
  336.     public function isActiveSingleSociety(SocietyGroup $societyGroup)
  337.     {
  338.         return !$this->isActiveMultipleSocieties($societyGroup);
  339.     }
  340.     // Plan.io Task #3631
  341.     public function isActiveMultipleSocieties(SocietyGroup $societyGroup)
  342.     {
  343.         // If the society group has more than one society,
  344.         // we mult display them all
  345.         // without taking the config into account
  346.         if ($societyGroup->getSocieties()->count() > 1)
  347.         {
  348.             return true;
  349.         }
  350.         // If a single society in the group, check to see if the society module is activated
  351.         $societyModuleActivated = false;
  352.         if ($this->isActiveByCode($societyGroup, Module::MODULE_SOCIETY))
  353.         {
  354.             $societyModuleActivated = true;
  355.         }
  356.         if ($societyModuleActivated === true)
  357.         {
  358.             // Display multiple societies
  359.             return true;
  360.         }
  361.         else
  362.         {
  363.             // Don't display the society notion
  364.             return false;
  365.         }
  366.         return false;
  367.     }
  368. }