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 $doctrineLogTools $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 $societyGroupModule $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 $societyGroupModule $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 isActiveClientAccount(SocietyGroup $societyGroup)
  109.     {
  110.         return $this->isActiveByCode($societyGroupModule::MODULE_CLIENT_ACCOUNT);
  111.     }
  112.     public function isActiveRHForm(SocietyGroup $societyGroup)
  113.     {
  114.         return $this->isActiveByCode($societyGroupModule::MODULE_RHFORM);
  115.     }
  116.     public function isActiveInvoice(SocietyGroup $societyGroup)
  117.     {
  118.         return $this->isActiveByCode($societyGroupModule::MODULE_INVOICE);
  119.     }
  120.     public function isActiveEInvoicing(SocietyGroup $societyGroup)
  121.     {
  122.         return $this->isActiveByCode($societyGroupModule::MODULE_EINVOICING);
  123.     }
  124.     public function isActiveIkeaServiceOrder(SocietyGroup $societyGroup)
  125.     {
  126.         return $this->isActiveByCode($societyGroupModule::MODULE_IKEA_SERVICE_ORDER);
  127.     }
  128.     public function isActiveIkeaKitchenPlanner(SocietyGroup $societyGroup)
  129.     {
  130.         return $this->isActiveByCode($societyGroupModule::MODULE_IKEA_KITCHEN_PLANNER);
  131.     }
  132.     public function isActiveDevis(SocietyGroup $societyGroup)
  133.     {
  134.         return $this->isActiveByCode($societyGroupModule::MODULE_DEVIS);
  135.     }
  136.     public function isActiveDocument(SocietyGroup $societyGroup)
  137.     {
  138.         return $this->isActiveByCode($societyGroupModule::MODULE_DOCUMENT);
  139.     }
  140.     public function isActiveProspect(SocietyGroup $societyGroup)
  141.     {
  142.         return $this->isActiveByCode($societyGroupModule::MODULE_PROSPECT);
  143.     }
  144.     public function isActivePunchOut(SocietyGroup $societyGroup)
  145.     {
  146.         return $this->isActiveByCode($societyGroupModule::MODULE_PUNCHOUT);
  147.     }
  148.     public function isActiveDemand(SocietyGroup $societyGroup)
  149.     {
  150.         return $this->isActiveByCode($societyGroupModule::MODULE_DEMAND);
  151.     }
  152.     public function isActiveApplication(SocietyGroup $societyGroup)
  153.     {
  154.         $codes = array(
  155.             Module::MODULE_APPLICATION,
  156.         );
  157.         // One single positve answer is enough
  158.         foreach ($codes as $code)
  159.         {
  160.             if ($this->isActiveByCode($societyGroup$code))
  161.             {
  162.                 return true;
  163.             }
  164.         }
  165.         // If we are here all hope is lost
  166.         return false;
  167.     }
  168.     public function isActiveMission(SocietyGroup $societyGroup)
  169.     {
  170.         $codes = array(
  171.             Module::MODULE_MISSION,
  172.             Module::MODULE_MISSION_LIGHT,
  173.             Module::MODULE_MISSION_PLUS,
  174.         );
  175.         // One single positve answer is enough
  176.         foreach ($codes as $code)
  177.         {
  178.             if ($this->isActiveByCode($societyGroup$code))
  179.             {
  180.                 return true;
  181.             }
  182.         }
  183.         // If we are here all hope is lost
  184.         return false;
  185.     }
  186.     public function isActiveClientOrMission(SocietyGroup $societyGroup)
  187.     {
  188.         $codes = array(
  189.             Module::MODULE_CLIENT,
  190.             Module::MODULE_CLIENT_LIGHT,
  191.             Module::MODULE_MISSION,
  192.             Module::MODULE_MISSION_LIGHT,
  193.             Module::MODULE_MISSION_PLUS,
  194.         );
  195.         // One single positve answer is enough
  196.         foreach ($codes as $code)
  197.         {
  198.             if ($this->isActiveByCode($societyGroup$code))
  199.             {
  200.                 return true;
  201.             }
  202.         }
  203.         // If we are here all hope is lost
  204.         return false;
  205.     }
  206.     public function isActiveMissionAndDevis(SocietyGroup $societyGroup)
  207.     {
  208.         if ($this->isInactiveByCode($societyGroupModule::MODULE_DEVIS))
  209.         {
  210.             // No need to go any further
  211.             return false;
  212.         }
  213.         // If we are here we know that the Devis module is active
  214.         // Only one mission option is required
  215.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION))
  216.         {
  217.             return true;
  218.         }
  219.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_LIGHT))
  220.         {
  221.             return true;
  222.         }
  223.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_PLUS))
  224.         {
  225.             return true;
  226.         }
  227.         // If we are here all hope is lost
  228.         return false;
  229.     }
  230.     public function isActiveMissionAndInvoice(SocietyGroup $societyGroup)
  231.     {
  232.         if ($this->isInactiveByCode($societyGroupModule::MODULE_INVOICE))
  233.         {
  234.             // No need to go any further
  235.             return false;
  236.         }
  237.         // If we are here we know that the Invoice module is active
  238.         // Only one mission option is required
  239.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION))
  240.         {
  241.             return true;
  242.         }
  243.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_LIGHT))
  244.         {
  245.             return true;
  246.         }
  247.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_PLUS))
  248.         {
  249.             return true;
  250.         }
  251.         // If we are here all hope is lost
  252.         return false;
  253.     }
  254.     public function isActiveMissionAndPlanning(SocietyGroup $societyGroup)
  255.     {
  256.         if ($this->isInactiveByCode($societyGroupModule::MODULE_PLANNING))
  257.         {
  258.             // No need to go any further
  259.             return false;
  260.         }
  261.         // If we are here we know that the Planning module is active
  262.         // Only one mission option is required
  263.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION))
  264.         {
  265.             return true;
  266.         }
  267.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_LIGHT))
  268.         {
  269.             return true;
  270.         }
  271.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_PLUS))
  272.         {
  273.             return true;
  274.         }
  275.         // If we are here all hope is lost
  276.         return false;
  277.     }
  278.     public function isActiveMissionAndDocument(SocietyGroup $societyGroup)
  279.     {
  280.         if ($this->isInactiveByCode($societyGroupModule::MODULE_DOCUMENT))
  281.         {
  282.             // No need to go any further
  283.             return false;
  284.         }
  285.         // If we are here we know that the Document module is active
  286.         // Only one mission option is required
  287.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION))
  288.         {
  289.             return true;
  290.         }
  291.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_LIGHT))
  292.         {
  293.             return true;
  294.         }
  295.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_PLUS))
  296.         {
  297.             return true;
  298.         }
  299.         // If we are here all hope is lost
  300.         return false;
  301.     }
  302.     public function isActiveMissionAndInvoiceAndDocument(SocietyGroup $societyGroup)
  303.     {
  304.         if ($this->isInactiveByCode($societyGroupModule::MODULE_INVOICE))
  305.         {
  306.             // No need to go any further
  307.             return false;
  308.         }
  309.         if ($this->isInactiveByCode($societyGroupModule::MODULE_DOCUMENT))
  310.         {
  311.             // No need to go any further
  312.             return false;
  313.         }
  314.         // If we are here we know that the Invoice and Document modules are active
  315.         // Only one mission option is required
  316.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION))
  317.         {
  318.             return true;
  319.         }
  320.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_LIGHT))
  321.         {
  322.             return true;
  323.         }
  324.         if ($this->isActiveByCode($societyGroupModule::MODULE_MISSION_PLUS))
  325.         {
  326.             return true;
  327.         }
  328.         // If we are here all hope is lost
  329.         return false;
  330.     }
  331.     // Plan.io Task #3631
  332.     public function isActiveSingleSociety(SocietyGroup $societyGroup)
  333.     {
  334.         return !$this->isActiveMultipleSocieties($societyGroup);
  335.     }
  336.     // Plan.io Task #3631
  337.     public function isActiveMultipleSocieties(SocietyGroup $societyGroup)
  338.     {
  339.         // If the society group has more than one society,
  340.         // we mult display them all
  341.         // without taking the config into account
  342.         if ($societyGroup->getSocieties()->count() > 1)
  343.         {
  344.             return true;
  345.         }
  346.         // If a single society in the group, check to see if the society module is activated
  347.         $societyModuleActivated false;
  348.         if ($this->isActiveByCode($societyGroupModule::MODULE_SOCIETY))
  349.         {
  350.             $societyModuleActivated true;
  351.         }
  352.         if ($societyModuleActivated === true)
  353.         {
  354.             // Display multiple societies
  355.             return true;
  356.         }
  357.         else
  358.         {
  359.             // Don't display the society notion
  360.             return false;
  361.         }
  362.         return false;
  363.     }
  364. }