src/Service/Kernel/KernelService.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Service\Kernel;
  3. use App\Entity\Kernel\ConfigurableList;
  4. use App\Entity\Kernel\ListItem;
  5. use App\Entity\Kernel\Setting;
  6. use App\Entity\Kernel\CustomizedEntity;
  7. use App\Entity\Kernel\CustomizedEntityField;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ObjectManager;
  10. use Exception;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  15. use Symfony\Component\Security\Core\Security;
  16. class KernelService
  17. {
  18.     private $entityManager;
  19.     public function __construct(EntityManagerInterface $entityManager)
  20.     {
  21.         $this->entityManager = $entityManager;
  22.     }
  23.     public function getApplicationInfoValue($key, $organization = null)
  24.     {
  25.         try {
  26.             $setting = $this->entityManager->getRepository(Setting::class)->findOneBy(
  27.                 array('identifier' => $key, 'organization' => $organization, 'state' => 0)
  28.             );
  29.             if (!$setting) {
  30.                 $setting = $this->entityManager->getRepository(Setting::class)->findOneBy(
  31.                     array('identifier' => $key, 'state' => 0)
  32.                 );
  33.                 if (!$setting) {
  34.                     return "";
  35.                 }
  36.             }
  37.             return $setting->getValue();
  38.         } catch (Exception $e) {
  39.             return "";
  40.         }
  41.     }
  42.     public function getApplicationInfoFile($key) {
  43.         $setting = $this->entityManager->getRepository(Setting::class)->findOneByIdentifier($key);
  44.         return $setting->getFileValue();
  45.     }
  46.     public function getList($list, $organization = null) {
  47.         if ($organization == null)
  48.         {
  49.             $weblist = $this->entityManager->getRepository(ConfigurableList::class)->findOneBy(array('identifier' => $list, 'state' => 0));
  50.         }
  51.         else
  52.         {
  53.             $query = $this->entityManager->createQuery("SELECT s FROM App\Entity\Kernel\ConfigurableList s WHERE (((s.state = 0) AND (s.identifier = ?1)) AND ((s.organization = ?2) or (s.organization is null)))");
  54.         $query = $query->setParameter(1, $list);
  55.             $query = $query->setParameter(2, $organization->getId());
  56.             $weblists = $query->getResult();
  57.             if ($weblists) {
  58.                 $weblist = $weblists[0];
  59.             }
  60.             else
  61.             {
  62.                 $weblist = null;
  63.             }
  64.         }
  65.         if (!$weblist) {
  66.             return null;
  67.         } else {
  68.             return $weblist;
  69.         }
  70.     }
  71.     public function getListItem($list, $item, $organization = null) {
  72.         $weblist = $this->getList($list, $organization);
  73.         if (!$weblist) {
  74.             return null;
  75.         } else {
  76.             return $this->entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist, 'name' => $item, 'state' => 0));
  77.         }
  78.     }
  79.     
  80.     public function getListItemOrCreate($list, $item, $organization = null) {
  81.         $weblist = $this->getList($list, $organization);
  82.         if (!$weblist) {
  83.             return null;
  84.         } else {
  85.             $listitem = $this->entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist, 'name' => $item, 'state' => 0));
  86.             
  87.             if (!$listitem) {
  88.                 $listitem  = new ListItem();
  89.                 $listitem->setName($item);
  90.                 $listitem->setConfigurablelist($weblist);
  91.                 $listitem->setState(0);
  92.                 $this->entityManager->persist($listitem);
  93.                 $this->entityManager->flush();
  94.             }
  95.             return $listitem;
  96.         }
  97.     }
  98.     /**
  99.      * @param string $name
  100.      * @param string $identifier
  101.      * @param $integer1
  102.      * @param $string1
  103.      * @param $string2
  104.      * @param $authorizationtype
  105.      * @param int $state
  106.      * @return ConfigurableList|mixed
  107.      */
  108.     public function createList(
  109.         string $name,
  110.         string $identifier,
  111.         $integer1,
  112.         $string1,
  113.         $string2,
  114.         $authorizationtype,
  115.         int $state
  116.     ) {
  117.         $list = $this->entityManager->getRepository(ConfigurableList::class)->findOneByIdentifier($identifier);
  118.         if (!$list) {
  119.             $list = new ConfigurableList();
  120.             $list->setName($name);
  121.             $list->setIdentifier($identifier);
  122.             $list->setInteger1($integer1);
  123.             $list->setString1($string1);
  124.             $list->setString2($string2);
  125.             $list->setAuthorizationtype($authorizationtype);
  126.             $list->setState($state);
  127.         }
  128.         return $list;
  129.     }
  130.     /**
  131.      * @param $object
  132.      * @return void
  133.      */
  134.     public function persistAndFlush($object)
  135.     {
  136.         $this->entityManager->persist($object);
  137.         $this->entityManager->flush();
  138.     }
  139.     /**
  140.      * @param ConfigurableList $list
  141.      * @param string $name
  142.      * @return ListItem
  143.      */
  144.     public function addToConfigurableList(ConfigurableList $list, string $name)
  145.     {
  146.         $listitem = $this->entityManager->getRepository(ListItem::class)->findOneBy(
  147.             array('name' => $name, 'configurablelist' => $list)
  148.         );
  149.         if (!$listitem) {
  150.             $listitem = new ListItem();
  151.             $listitem->setConfigurablelist($list);
  152.             $listitem->setName($name);
  153.             $listitem->setState(0);
  154.         }
  155.         return $listitem;
  156.     }
  157.     /**
  158.      * @param string $name
  159.      * @param string $indentifier
  160.      * @param bool $organizationSpecific
  161.      * @param int $state
  162.      * @param string $type
  163.      * @param $authorizationtype
  164.      * @return Setting
  165.      */
  166.     public function createSetting(
  167.         string $name,
  168.         string $indentifier,
  169.         bool $organizationSpecific,
  170.         int $state,
  171.         string $type,
  172.         $authorizationtype
  173.     ) {
  174.         $setting = $this->entityManager->getRepository(Setting::class)->findOneByIdentifier($indentifier);
  175.         if (!$setting) {
  176.             $setting = new Setting();
  177.             $setting->setName($name);
  178.             $setting->setIdentifier($indentifier);
  179.             $setting->setOrganizationSpecific($organizationSpecific);
  180.             $setting->setState($state);
  181.             $setting->setType($type);
  182.             $setting->setAuthorizationtype($authorizationtype);
  183.         }
  184.         return $setting;
  185.     }
  186.     
  187.     public function getCustomizedName($entity, $organization = null, $fallback = null)
  188.     {
  189.         $item = $this->getCustomizedEntity($entity, $organization);
  190.         if (!$item) {
  191.             return $fallback;
  192.         }
  193.         else
  194.         {
  195.             if (empty($item->getCustomizedName()))
  196.             {
  197.                 return $fallback;
  198.             }
  199.             else
  200.             {
  201.                 return $item->getCustomizedName();
  202.             }
  203.         }
  204.     }
  205.     
  206.     public function getCustomizedPluralName($entity, $organization = null, $fallback = null)
  207.     {
  208.         $item = $this->getCustomizedEntity($entity, $organization);
  209.         if (!$item) {
  210.             return $fallback;
  211.         }
  212.         else
  213.         {
  214.             if (empty($item->getCustomizedPluralName()))
  215.             {
  216.                 return $fallback;
  217.             }
  218.             else
  219.             {
  220.                 return $item->getCustomizedPluralName();
  221.             }
  222.         }
  223.     }
  224.     
  225.     public function getCustomizedString1($entity, $organization = null, $fallback = null)
  226.     {
  227.         $item = $this->getCustomizedEntity($entity, $organization);
  228.         if (!$item) {
  229.             return $fallback;
  230.         }
  231.         else
  232.         {
  233.             if (empty($item->getSpecificString1()))
  234.             {
  235.                 return $fallback;
  236.             }
  237.             else
  238.             {
  239.                 return $item->getSpecificString1();
  240.             }
  241.         }
  242.     }
  243.     
  244.     public function getCustomizedString2($entity, $organization = null, $fallback = null)
  245.     {
  246.         $item = $this->getCustomizedEntity($entity, $organization);
  247.         if (!$item) {
  248.             return $fallback;
  249.         }
  250.         else
  251.         {
  252.             if (empty($item->getSpecificString2()))
  253.             {
  254.                 return $fallback;
  255.             }
  256.             else
  257.             {
  258.                 return $item->getSpecificString2();
  259.             }
  260.         }
  261.     }
  262.     
  263.     public function getCustomizedEntity($entity, $organization = null)
  264.     {
  265.         try {
  266.             $item = $this->entityManager->getRepository(CustomizedEntity::class)->findOneBy(
  267.                 array('entity' => $entity, 'organization' => $organization, 'state' => 0)
  268.             );
  269.             if (!$item) {
  270.                 $item = $this->entityManager->getRepository(CustomizedEntity::class)->findOneBy(
  271.                     array('entity' => $entity, 'state' => 0)
  272.                 );
  273.                 if (!$item) {
  274.                     return null;
  275.                 }
  276.             }
  277.             return $item;
  278.         } catch (Exception $e) {
  279.             return null;
  280.         }
  281.     }
  282. }