vendor/uvdesk/support-center-bundle/Controller/MarketingModule.php line 175

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntities;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Services\FileUploadService;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreEntities;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. Class MarketingModule extends AbstractController
  15. {
  16.     private $translator;
  17.     private $userService;
  18.     public $container;
  19.     public $entityManager;
  20.     public $fileUplaodService;
  21.     public $uvdeskService;
  22.     public function __construct(TranslatorInterface $translatorUserService $userServiceContainerInterface $containerEntityManagerInterface $entityManagerFileUploadService $fileUplaodServiceUVDeskService $uvdeskService)
  23.     {
  24.         $this->translator $translator;
  25.         $this->userService $userService;
  26.         $this->container $container;
  27.         $this->entityManager $entityManager;
  28.         $this->fileUplaodService $fileUplaodService;
  29.         $this->uvdeskService $uvdeskService;
  30.     }
  31.     public function listModules(Request $request)
  32.     {
  33.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_MARKETING_MODULE')) {
  34.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  35.         }
  36.         return $this->render('@UVDeskSupportCenter/Staff/MarketingModule/listModules.html.twig');
  37.     }
  38.     public function listModulesXHR(Request $requestContainerInterface $container)
  39.     {
  40.         $json = array();
  41.         $repository $this->entityManager->getRepository(SupportEntities\MarketingModule::class);
  42.         $json =  $repository->getAllMarketingModules($request->query$container);
  43.         $response = new Response(json_encode($json));
  44.         $response->headers->set('Content-Type''application/json');
  45.         return $response;
  46.     }
  47.     public function updateModule(Request $request)
  48.     {
  49.         $prefix 'marketing/module/';
  50.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_MARKETING_MODULE')) {
  51.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  52.         }
  53.         if ($request->attributes->get('id')) {
  54.             $marketingModule $this->entityManager->getRepository(SupportEntities\MarketingModule::class)->findOneBy([
  55.                                     'id' => $request->attributes->get('id'),
  56.                                 ]);
  57.         } else {
  58.             $marketingModule = new SupportEntities\MarketingModule;
  59.             $marketingModule->setCreatedAt(new \DateTime('now'));
  60.         }
  61.         if ($request->getMethod() == "POST") {
  62.             $uploadImage $request->files->get('marketingModule_image');
  63.             $request $request->request->get('marketingModule_form');
  64.             $group $this->entityManager->getRepository(CoreEntities\SupportGroup::class)->find($request['group']);
  65.             $marketingModule->setTitle($request['title']);
  66.             $marketingModule->setDescription($request['description']);
  67.             $marketingModule->setIsActive($request['status']);
  68.             $marketingModule->setGroup($group);
  69.             $marketingModule->setBorderColor($this->hex2rgb($request['borderColor']));
  70.             $marketingModule->setLinkURL($request['linkURL']);
  71.             try {
  72.                 $validMimeType = ['image/jpeg''image/png''image/jpg''image/gif'];
  73.                 if ($uploadImage) {
  74.                     if (! empty($uploadImage) && ! in_array($uploadImage->getMimeType(), $validMimeType)) {
  75.                         $message $this->translator->trans('Warning! Provide valid image file. (Recommended: PNG, JPG or GIF Format).');
  76.                         $this->addFlash('warning'$message);
  77.                         throw new \Exception($message);
  78.                     }
  79.                     $uploadedFileAttributes $this->fileUplaodService->uploadFile($uploadImage$prefix);
  80.                     if ($uploadedFileAttributes) {
  81.                         $marketingModule->setImage($this->uvdeskService->generateCompleteLocalResourcePathUri($uploadedFileAttributes['path']));
  82.                     }
  83.                 }
  84.             } catch (\Exception $e) {
  85.                 return $this->render('@UVDeskSupportCenter/Staff/MarketingModule/marketingModuleForm.html.twig', [
  86.                     'marketingModule' => $marketingModule,
  87.                     'errors'          => $e->getMessage()
  88.                 ]);
  89.             }
  90.             $this->entityManager->persist($marketingModule);
  91.             $this->entityManager->flush();
  92.             $this->addFlash('success''Success! Marketing Module data saved successfully.');
  93.             return $this->redirect($this->generateUrl('helpdesk_member_knowledgebase_marketing_module'));
  94.         }
  95.         return $this->render('@UVDeskSupportCenter/Staff/MarketingModule/marketingModuleForm.html.twig', [
  96.                 'marketingModule' => $marketingModule,
  97.                 'errors'          => ''
  98.         ]);
  99.     }
  100.     public function hex2rgb($color)
  101.     {
  102.         // Check if the color is in RGB format
  103.         if (preg_match('/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/'$color$matches)) {
  104.             // If it's already in RGB format, return the color as is
  105.             return $color;
  106.         } else {
  107.             // Assume it's a hexadecimal color and convert it to RGB
  108.             $hexColor $color;
  109.             $shorthand = (strlen($hexColor) == 4);
  110.             list($r$g$b) = $shorthand sscanf($hexColor"#%1s%1s%1s") : sscanf($hexColor"#%2s%2s%2s");
  111.             return 'rgb('.hexdec($shorthand "$r$r$r).','.
  112.                         hexdec($shorthand "$g$g$g).','.
  113.                         hexdec($shorthand "$b$b$b).')';
  114.         }
  115.     }
  116.     public function removeModuleXHR(Request $request)
  117.     {
  118.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_MARKETING_MODULE')) {
  119.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  120.         }
  121.         $marketingAnnouncement $this->entityManager->getRepository(SupportEntities\MarketingModule::class)
  122.             ->findOneBy([
  123.                 'id' => $request->attributes->get('id')
  124.             ]);
  125.         if ($marketingAnnouncement) {
  126.             $this->entityManager->remove($marketingAnnouncement);
  127.             $this->entityManager->flush();
  128.             $json = [
  129.                 'alertClass'   => 'success',
  130.                 'alertMessage' => 'Marketing Module deleted successfully!',
  131.             ];
  132.             $responseCode 200;
  133.         } else {
  134.             $json = [
  135.                 'alertClass'   => 'warning',
  136.                 'alertMessage' => 'Marketing Module not found!',
  137.             ];
  138.         }
  139.         $response = new Response(json_encode($json));
  140.         $response->headers->set('Content-Type''application/json');
  141.         return $response;
  142.     }
  143.     public function marketingModuleCustomerListXHR(Request $requestContainerInterface $container )
  144.     {
  145.         $json = array();
  146.         $customer $this->userService->getCurrentUser();
  147.         $repository $this->entityManager->getRepository(SupportEntities\MarketingModule::class);
  148.         $json $repository->getAllMarketingModulesForCustomer($request->query$container$customer);
  149.         $response = new Response(json_encode($json));
  150.         $response->headers->set('Content-Type''application/json');
  151.         return $response;
  152.     }
  153. }