<?php
//----------------------------------------------------------------------
// src/Services/Config/ModuleTools.php
//----------------------------------------------------------------------
namespace App\Services\Config;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Access;
use App\Entity\SocietyGroup;
use App\Entity\Config\Config;
use App\Entity\Config\Module;
use App\Entity\Config\ModuleConfig;
use App\Services\LogTools;
class ModuleTools
{
public function __construct(ManagerRegistry $doctrine, LogTools $logTools)
{
$this->em = $doctrine->getManager();
$this->logTools = $logTools;
}
// Plan.io Task #3309
// Returns TRUE if the Guard is restricted according to active modules
// Returns FALSE otherwise
public function restrictGuardian(SocietyGroup $societyGroup, $modules)
{
// Test FREE_GUARD config
if ($this->applyGuardRestrictions())
{
// Test Module
if ($this->areInactiveByCode($societyGroup, $modules))
{
// All Modules are inactive
return true;
}
}
return false;
}
// Plan.io Task #3309
// Config :: $const FREE_GUARD
// FREE_GUARD = 1
// - No restrictions are applied when creating a society group, or when executing the guard
// - Default value in `matrix_global.yml`
// FREE_GUARD = 0
// - Module related restrictions are applied when creating a society group, and when executing the guard
public function applyGuardRestrictions()
{
$config = $this->em->getRepository(Config::class)->findOneByName(Config::FREE_GUARD);
if ($config === null)
{
return false;
}
if ($config->getValue())
{
return false;
}
return true;
}
// Returns TRUE if all the Codes are INACTIVE
// Returns FALSE if at least one Code is ACTIVE
// We want to test if all the codes are inactive
// code1.inactive && code2.inactive && code3.inactive
// Therefore a single active code breaks the chain
public function areInactiveByCode(SocietyGroup $societyGroup, $codes)
{
$allInactive = true;
foreach ($codes as $code)
{
if ($this->isActiveByCode($societyGroup, $code))
{
$allInactive = false;
break;
}
}
return $allInactive;
}
public function isActiveByCode(SocietyGroup $societyGroup, $code)
{
$module = $this->em->getRepository(Module::class)->findOneByCode($code);
if ($module === null)
{
return false;
}
return $this->isActive($societyGroup, $module);
}
public function isInactiveByCode(SocietyGroup $societyGroup, $code)
{
return !$this->isActiveByCode($societyGroup, $code);
}
public function isActive(SocietyGroup $societyGroup, Module $module)
{
// Fetch them from the SocietyGroup instead of the database
// This way we can test the module even if the SocietyGroup
// has not been persisted to the database yet
return $societyGroup->hasModule($module);
}
public function getConfig(SocietyGroup $societyGroup, Module $module)
{
$config = $this->em->getRepository(ModuleConfig::class)
->findOneBy(array(
'societyGroup' => $societyGroup,
'module' => $module,
));
if ($config !== null)
{
return $config;
}
return null;
}
public function isActiveClientAccount(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_CLIENT_ACCOUNT);
}
public function isActiveRHForm(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_RHFORM);
}
public function isActiveInvoice(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_INVOICE);
}
public function isActiveEInvoicing(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_EINVOICING);
}
public function isActiveIkeaServiceOrder(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_IKEA_SERVICE_ORDER);
}
public function isActiveIkeaKitchenPlanner(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_IKEA_KITCHEN_PLANNER);
}
public function isActiveDevis(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_DEVIS);
}
public function isActiveDocument(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_DOCUMENT);
}
public function isActiveProspect(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_PROSPECT);
}
public function isActivePunchOut(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_PUNCHOUT);
}
public function isActiveDemand(SocietyGroup $societyGroup)
{
return $this->isActiveByCode($societyGroup, Module::MODULE_DEMAND);
}
public function isActiveApplication(SocietyGroup $societyGroup)
{
$codes = array(
Module::MODULE_APPLICATION,
);
// One single positve answer is enough
foreach ($codes as $code)
{
if ($this->isActiveByCode($societyGroup, $code))
{
return true;
}
}
// If we are here all hope is lost
return false;
}
public function isActiveMission(SocietyGroup $societyGroup)
{
$codes = array(
Module::MODULE_MISSION,
Module::MODULE_MISSION_LIGHT,
Module::MODULE_MISSION_PLUS,
);
// One single positve answer is enough
foreach ($codes as $code)
{
if ($this->isActiveByCode($societyGroup, $code))
{
return true;
}
}
// If we are here all hope is lost
return false;
}
public function isActiveClientOrMission(SocietyGroup $societyGroup)
{
$codes = array(
Module::MODULE_CLIENT,
Module::MODULE_CLIENT_LIGHT,
Module::MODULE_MISSION,
Module::MODULE_MISSION_LIGHT,
Module::MODULE_MISSION_PLUS,
);
// One single positve answer is enough
foreach ($codes as $code)
{
if ($this->isActiveByCode($societyGroup, $code))
{
return true;
}
}
// If we are here all hope is lost
return false;
}
public function isActiveMissionAndDevis(SocietyGroup $societyGroup)
{
if ($this->isInactiveByCode($societyGroup, Module::MODULE_DEVIS))
{
// No need to go any further
return false;
}
// If we are here we know that the Devis module is active
// Only one mission option is required
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
{
return true;
}
// If we are here all hope is lost
return false;
}
public function isActiveMissionAndInvoice(SocietyGroup $societyGroup)
{
if ($this->isInactiveByCode($societyGroup, Module::MODULE_INVOICE))
{
// No need to go any further
return false;
}
// If we are here we know that the Invoice module is active
// Only one mission option is required
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
{
return true;
}
// If we are here all hope is lost
return false;
}
public function isActiveMissionAndPlanning(SocietyGroup $societyGroup)
{
if ($this->isInactiveByCode($societyGroup, Module::MODULE_PLANNING))
{
// No need to go any further
return false;
}
// If we are here we know that the Planning module is active
// Only one mission option is required
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
{
return true;
}
// If we are here all hope is lost
return false;
}
public function isActiveMissionAndDocument(SocietyGroup $societyGroup)
{
if ($this->isInactiveByCode($societyGroup, Module::MODULE_DOCUMENT))
{
// No need to go any further
return false;
}
// If we are here we know that the Document module is active
// Only one mission option is required
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
{
return true;
}
// If we are here all hope is lost
return false;
}
public function isActiveMissionAndInvoiceAndDocument(SocietyGroup $societyGroup)
{
if ($this->isInactiveByCode($societyGroup, Module::MODULE_INVOICE))
{
// No need to go any further
return false;
}
if ($this->isInactiveByCode($societyGroup, Module::MODULE_DOCUMENT))
{
// No need to go any further
return false;
}
// If we are here we know that the Invoice and Document modules are active
// Only one mission option is required
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_LIGHT))
{
return true;
}
if ($this->isActiveByCode($societyGroup, Module::MODULE_MISSION_PLUS))
{
return true;
}
// If we are here all hope is lost
return false;
}
// Plan.io Task #3631
public function isActiveSingleSociety(SocietyGroup $societyGroup)
{
return !$this->isActiveMultipleSocieties($societyGroup);
}
// Plan.io Task #3631
public function isActiveMultipleSocieties(SocietyGroup $societyGroup)
{
// If the society group has more than one society,
// we mult display them all
// without taking the config into account
if ($societyGroup->getSocieties()->count() > 1)
{
return true;
}
// If a single society in the group, check to see if the society module is activated
$societyModuleActivated = false;
if ($this->isActiveByCode($societyGroup, Module::MODULE_SOCIETY))
{
$societyModuleActivated = true;
}
if ($societyModuleActivated === true)
{
// Display multiple societies
return true;
}
else
{
// Don't display the society notion
return false;
}
return false;
}
}