vendor/uvdesk/core-framework/Services/UVDeskService.php line 44

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Utils\TokenGenerator;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Webkul\UVDesk\SupportCenterBundle\Entity\KnowledgebaseWebsite;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  11. class UVDeskService
  12. {
  13.     protected $container;
  14.     protected $requestStack;
  15.     protected $entityManager;
  16.     private $avoidArray = [
  17.         '!''@''#''$''%''^''&''*''('')''_''+''-''=''/''\\'':''{''}''['']''<''>''.''?'';''"''\''',''|',
  18.         '1''2''3''4''5''6''7''8''9''0',
  19.         ' true '' false ',
  20.         ' do '' did ',
  21.         ' is '' are '' am '' was '' were ',
  22.         ' has '' have '' had ',
  23.         ' will '' would '' shall '' should '' must '' can '' could ',
  24.         ' not '' never ',
  25.         ' neither '' either ',
  26.         ' the '' a '' an '' this '' that ',
  27.         ' here '' there ',
  28.         ' then '' when '' since ',
  29.         ' he '' him '' himself '' she '' her '' herself '' i '' me '' myself '' mine '' you '' your ' ,' yourself '' ur '' we '' ourself '' it '' its ',
  30.         ' for '' from '' on '' and '' in '' be '' to '' or '' of '' with ',
  31.         ' what '' why '' where '' who '' whom '' which ',
  32.         ' a '' b '' c '' d '' e ' ' f ' ' g ' ' h ' ' i ' ' j ' ' k ' ' l ' ' m ' ' n ' ' o ' ' p ' ' q ' ' r ' ' s ' ' t ' ' u ' ' v ' ' w ' ' x ' ' y ' ' z ' ,
  33.         '  ',
  34.     ];
  35.     public function __construct(ContainerInterface $containerRequestStack $requestStackEntityManagerInterface $entityManagerUserService $userService)
  36.     {
  37.         $this->container $container;
  38.         $this->requestStack $requestStack;
  39.         $this->entityManager $entityManager;
  40.         $this->userService $userService;
  41.     }
  42.     public function updatesLocales($locales)
  43.     {  
  44.         $fileTranslation $this->container->get('kernel')->getProjectDir() . '/config/packages/translation.yaml';
  45.         $fileServices $this->container->get('kernel')->getProjectDir() . '/config/services.yaml';
  46.         // get file content and index
  47.         $fileTrans file($fileTranslation);
  48.         $fileServs file($fileServices);
  49.         foreach ($fileTrans as $index => $content) {
  50.             if (false !== strpos($content'default_locale')) {
  51.                 list($helpdesk_panel_locales$helpdesk_panel_text) = array($index$content);
  52.             }
  53.             if (false !== strpos($content'- ')) {
  54.                 list($helpdesk_panel_locales_fallback$helpdesk_panel_text_fallback) = array($index$content);
  55.             }
  56.         }
  57.         foreach ($fileServs as $indexs => $contents) {
  58.             if (false !== strpos($contents'locale')) {
  59.                 list($helpdesk_services_locales$helpdesk_services_text) = array($indexs$contents);
  60.             }
  61.         }
  62.         // save updated data in a variable ($updatedFileContent)
  63.         $updatedFileContent $fileTrans;
  64.         $updatedServicesFileContent $fileServs;
  65.         $updatedlocales = (null !== $helpdesk_panel_locales) ? substr($helpdesk_panel_text0strpos($helpdesk_panel_text'default_locale') + strlen('default_locale: ')) . $locales PHP_EOL'';
  66.         $updatedlocales_fallback = (null !== $helpdesk_panel_locales_fallback) ? substr($helpdesk_panel_text_fallback0strpos($helpdesk_panel_text_fallback'- ') + strlen('- ')) . $locales PHP_EOL'';
  67.         $updatedServiceslocales = (null !== $helpdesk_services_locales) ? substr($helpdesk_services_text0strpos($helpdesk_services_text'locale') + strlen('locale: ')) . $locales PHP_EOL'';
  68.         $updatedFileContent[$helpdesk_panel_locales] = $updatedlocales;
  69.         $updatedFileContent[$helpdesk_panel_locales_fallback] = $updatedlocales_fallback;
  70.         $updatedServicesFileContent[$helpdesk_services_locales] = $updatedServiceslocales;
  71.         // flush updated content in file
  72.         $status file_put_contents($fileTranslation$updatedFileContent);
  73.         $status1 file_put_contents($fileServices$updatedServicesFileContent);
  74.         return true;
  75.     }
  76.     public function getLocalesList() 
  77.     {
  78.         $translator $this->container->get('translator');
  79.         return  [
  80.             'en' => $translator->trans("English"),
  81.             'fr' => $translator->trans("French"),
  82.             'it' => $translator->trans("Italian"),
  83.             'ar' => $translator->trans("Arabic"),
  84.             'de' => $translator->trans("German"),
  85.             'es' => $translator->trans("Spanish"),
  86.             'tr' => $translator->trans("Turkish"),
  87.             'da' => $translator->trans("Danish"),
  88.             'zh' => $translator->trans("Chinese"),
  89.             'pl' => $translator->trans("Polish"),
  90.         ];
  91.     }
  92.     public function getActiveLocales() 
  93.     {
  94.         $localesList $this->getLocalesList();
  95.         $activeLocales $this->container->getParameter("app_locales");
  96.         $explodeActiveLocales explode("|",$activeLocales);
  97.         return $explodeActiveLocales;
  98.     }
  99.     public function getLocales()
  100.     {
  101.         $localesList $this->getLocalesList();
  102.         $explodeActiveLocales $this->getActiveLocales();
  103.         $listingActiveLocales = array();
  104.         foreach ($explodeActiveLocales as $key => $value) {
  105.             $listingActiveLocales[$value] = $localesList[$value];
  106.         }
  107.         return $listingActiveLocales;
  108.     }
  109.     public function getDefaultLangauge() 
  110.     {
  111.         return $this->container->getParameter("kernel.default_locale");  
  112.     }
  113.     
  114.     public function getTimezones()
  115.     {
  116.         return \DateTimeZone::listIdentifiers();
  117.     }
  118.     public function getPrivileges() {
  119.         $agentPrivilegeCollection = [];
  120.         // $agentPrivilegeCollection = $this->entityManager->getRepository('UserBundle:AgentPrivilege')->findAll();
  121.         return $agentPrivilegeCollection;
  122.     }
  123.     public function getLocaleUrl($locale)
  124.     {
  125.         $request $this->requestStack->getCurrentRequest();
  126.         return str_replace('/' $request->getLocale() . '/''/' $locale '/'$request->getRequestUri());
  127.     }
  128.     
  129.     public function buildPaginationQuery(array $query = [])
  130.     {
  131.         $params = array();
  132.         $query['page'] = "replacePage";
  133.         if (isset($query['domain'])) unset($query['domain']);
  134.         if (isset($query['_locale'])) unset($query['_locale']);
  135.         
  136.         foreach ($query as $key => $value) {
  137.             $params[] = !isset($value) ? $key $key '/' str_replace('%2F''/'rawurlencode($value));
  138.         }
  139.         $http_query implode('/'$params);
  140.         
  141.         if (isset($query['new'])) {
  142.             $http_query str_replace('new/1''new'$http_query);
  143.         } else if (isset($query['unassigned'])) {
  144.             $http_query str_replace('unassigned/1''unassigned'$http_query);
  145.         } else if (isset($query['notreplied'])) {
  146.             $http_query str_replace('notreplied/1''notreplied'$http_query);
  147.         } else if (isset($query['mine'])) {
  148.             $http_query str_replace('mine/1''mine'$http_query);
  149.         } else if (isset($query['starred'])) {
  150.             $http_query str_replace('starred/1''starred'$http_query);
  151.         } else if (isset($query['trashed'])) {
  152.             $http_query str_replace('trashed/1''trashed'$http_query);
  153.         }
  154.         
  155.         return $http_query;
  156.     }
  157.     public function getEntityManagerResult($entity$callFunction$args false$extraPrams false)
  158.     {
  159.         if($extraPrams)
  160.             return $this->entityManager->getRepository($entity)
  161.                         ->$callFunction($args$extraPrams);
  162.         else
  163.             return $this->entityManager->getRepository($entity)
  164.                         ->$callFunction($args);
  165.     }
  166.     public function getValidBroadcastMessage($msg$format 'Y-m-d H:i:s')
  167.     {
  168.         $broadcastMessage = !empty($msg) ? json_decode($msgtrue) : false;
  169.         if(!empty($broadcastMessage) && isset($broadcastMessage['isActive']) && $broadcastMessage['isActive']) {
  170.             $timezone = new \DateTimeZone('Asia/Kolkata');
  171.             $nowTimestamp date('U');
  172.             if(array_key_exists('from'$broadcastMessage) && ($fromDateTime \DateTime::createFromFormat($format$broadcastMessage['from'], $timezone))) {
  173.                 $fromTimeStamp $fromDateTime->format('U');
  174.                 if($nowTimestamp $fromTimeStamp) {
  175.                     return false;
  176.                 }
  177.             }
  178.             if(array_key_exists('to'$broadcastMessage) && ($toDateTime \DateTime::createFromFormat($format$broadcastMessage['to'], $timezone))) {
  179.                 $toTimeStamp $toDateTime->format('U');;
  180.                 if($nowTimestamp $toTimeStamp) {
  181.                     return false;
  182.                 }
  183.             }
  184.         } else {
  185.             return false;
  186.         }
  187.         // return valid broadcast message Array
  188.         return $broadcastMessage;
  189.     }
  190.     public function getConfigParameter($param)
  191.     {
  192.         if($param && $this->container->hasParameter($param)) {
  193.             return $this->container->getParameter($param);
  194.         } else {
  195.             return false;
  196.         }
  197.     }
  198.     
  199.     public function isDarkSkin($brandColor) {
  200.         $brandColor str_replace('#'''$brandColor);
  201.         if(strlen($brandColor) == 3)
  202.             $brandColor .= $brandColor;
  203.         $chars str_split($brandColor);
  204.         $a2fCount 0;
  205.         foreach ($chars as $key => $char) {
  206.             if(in_array($key, [024]) && in_array(strtoupper($char), ['A''B''C''D''E''F'])) {
  207.                 $a2fCount++;
  208.             }
  209.         }
  210.         if($a2fCount >= 2)
  211.             return true;
  212.         else
  213.             return false;
  214.     }
  215.     public function getActiveConfiguration($websiteId)
  216.     {
  217.         $configurationRepo $this->entityManager->getRepository(KnowledgebaseWebsite::class);
  218.         $configuration $configurationRepo->findOneBy(['website' => $websiteId'isActive' => 1]);
  219.         return $configuration;
  220.     }
  221.     public function getSupportPrivelegesResources()
  222.     {
  223.         $translator $this->container->get('translator');
  224.         return [
  225.             'ticket' => [
  226.                 'ROLE_AGENT_CREATE_TICKET' => $translator->trans('Can create ticket'),
  227.                 'ROLE_AGENT_EDIT_TICKET' => $translator->trans('Can edit ticket'),
  228.                 'ROLE_AGENT_DELETE_TICKET' => $translator->trans('Can delete ticket'),
  229.                 'ROLE_AGENT_RESTORE_TICKET' => $translator->trans('Can restore trashed ticket'),
  230.                 'ROLE_AGENT_ASSIGN_TICKET' => $translator->trans('Can assign ticket'),
  231.                 'ROLE_AGENT_ASSIGN_TICKET_GROUP' => $translator->trans('Can assign ticket group'),
  232.                 'ROLE_AGENT_UPDATE_TICKET_STATUS' => $translator->trans('Can update ticket status'),
  233.                 'ROLE_AGENT_UPDATE_TICKET_PRIORITY' => $translator->trans('Can update ticket priority'),
  234.                 'ROLE_AGENT_UPDATE_TICKET_TYPE' => $translator->trans('Can update ticket type'),
  235.                 'ROLE_AGENT_ADD_NOTE' => $translator->trans('Can add internal notes to ticket'),
  236.                 'ROLE_AGENT_EDIT_THREAD_NOTE' => $translator->trans('Can edit thread/notes'),
  237.                 'ROLE_AGENT_MANAGE_LOCK_AND_UNLOCK_THREAD' => $translator->trans('Can lock/unlock thread'),
  238.                 'ROLE_AGENT_ADD_COLLABORATOR_TO_TICKET' => $translator->trans('Can add collaborator to ticket'),
  239.                 'ROLE_AGENT_DELETE_COLLABORATOR_FROM_TICKET' => $translator->trans('Can delete collaborator from ticket'),
  240.                 'ROLE_AGENT_DELETE_THREAD_NOTE' => $translator->trans('Can delete thread/notes'),
  241.                 'ROLE_AGENT_APPLY_WORKFLOW' => $translator->trans('Can apply prepared response on ticket'),
  242.                 'ROLE_AGENT_ADD_TAG' => $translator->trans('Can add ticket tags'),
  243.                 'ROLE_AGENT_DELETE_TAG' => $translator->trans('Can delete ticket tags')
  244.             ],
  245.             'advanced' => [
  246.                 'ROLE_AGENT_MANAGE_EMAIL_TEMPLATE' => $translator->trans('Can manage email templates'),
  247.                 'ROLE_AGENT_MANAGE_GROUP' => $translator->trans('Can manage groups'),
  248.                 'ROLE_AGENT_MANAGE_SUB_GROUP' => $translator->trans('Can manage Sub-Groups/ Teams'),
  249.                 'ROLE_AGENT_MANAGE_AGENT' => $translator->trans('Can manage agents'),
  250.                 'ROLE_AGENT_MANAGE_AGENT_PRIVILEGE' => $translator->trans('Can manage agent privileges'),
  251.                 'ROLE_AGENT_MANAGE_TICKET_TYPE' => $translator->trans('Can manage ticket types'),
  252.                 'ROLE_AGENT_MANAGE_CUSTOMER' => $translator->trans('Can manage customers'),
  253.                 'ROLE_AGENT_MANAGE_WORKFLOW_MANUAL' => $translator->trans('Can manage Prepared Responses'),
  254.                 'ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC' => $translator->trans('Can manage Automatic workflow'),
  255.                 'ROLE_AGENT_MANAGE_TAG' => $translator->trans('Can manage tags'),
  256.                 'ROLE_AGENT_MANAGE_KNOWLEDGEBASE' => $translator->trans('Can manage knowledgebase'),
  257.                 'ROLE_AGENT_MANAGE_AGENT_ACTIVITY'  => $translator->trans("Can manage agent activity"),
  258.                 'ROLE_AGENT_MANAGE_MARKETING_ANNOUNCEMENT' => $translator->trans("Can manage marketing announcement"),
  259.             ]
  260.         ];
  261.     }
  262.     public function generateCsrfToken($intention)
  263.     {
  264.         $csrf $this->container->get('security.csrf.token_manager');
  265.         return $csrf->getToken($intention)->getValue();
  266.     }
  267.     /**
  268.      * This function will create content text from recived text, which we can use in meta content and as well in searching save like elastic
  269.      * @param  string $text String text
  270.      * @param  no. $lenght max return lenght string (which will convert to array)
  271.      * @param  boolean $returnArray what return type required
  272.      * @return string/ array comma seperated/ []
  273.      */
  274.     public function createConentToKeywords($text$lenght 255$returnArray false)
  275.     {
  276.         //to remove all tags from text, if any tags are in encoded form
  277.         $newText preg_replace('/[\s]+/'' 'str_replace($this->avoidArray' 'strtolower(strip_tags(html_entity_decode(strip_tags($text))))));
  278.         if($lenght)
  279.             $newText substr($newText0$lenght);
  280.         return ($returnArray explode(' '$newText) : str_replace(' '','$newText));
  281.     }
  282.     public function requestHeadersSent()
  283.     {
  284.         return headers_sent() ? true false;
  285.     }
  286.     /**
  287.      * get current prefixes of member panel and knowledgebase
  288.      */
  289.     public function getCurrentWebsitePrefixes()
  290.     {
  291.         $filePath $this->container->get('kernel')->getProjectDir() . '/config/packages/uvdesk.yaml';
  292.         
  293.         // get file content and index
  294.         $file file($filePath);
  295.         foreach ($file as $index => $content) {
  296.             if (false !== strpos($content'uvdesk_site_path.member_prefix')) {
  297.                 list($member_panel_line$member_panel_text) = array($index$content);
  298.             }
  299.             if (false !== strpos($content'uvdesk_site_path.knowledgebase_customer_prefix')) {
  300.                 list($customer_panel_line$customer_panel_text) = array($index$content);
  301.             }
  302.         }
  303.         $memberPrefix substr($member_panel_textstrpos($member_panel_text'uvdesk_site_path.member_prefix') + strlen('uvdesk_site_path.member_prefix: '));
  304.         $knowledgebasePrefix substr($customer_panel_textstrpos($customer_panel_text'uvdesk_site_path.knowledgebase_customer_prefix') + strlen('uvdesk_site_path.knowledgebase_customer_prefix: '));
  305.         return [
  306.             'memberPrefix' => trim(preg_replace('/\s\s+/'' '$memberPrefix)),
  307.             'knowledgebasePrefix' => trim(preg_replace('/\s\s+/'' '$knowledgebasePrefix)),
  308.         ];
  309.     }
  310.     /**
  311.      * update your website prefixes
  312.      */
  313.     public function updateWebsitePrefixes($member_panel_prefix$knowledgebase_prefix)
  314.     {
  315.         $filePath $this->container->get('kernel')->getProjectDir() . '/config/packages/uvdesk.yaml';
  316.         $website_prefixes = [
  317.             'member_prefix' => $member_panel_prefix,
  318.             'customer_prefix' => $knowledgebase_prefix,
  319.         ];
  320.         
  321.         // get file content and index
  322.         $file file($filePath);
  323.         foreach ($file as $index => $content) {
  324.             if (false !== strpos($content'uvdesk_site_path.member_prefix')) {
  325.                 list($member_panel_line$member_panel_text) = array($index$content);
  326.             }
  327.             if (false !== strpos($content'uvdesk_site_path.knowledgebase_customer_prefix')) {
  328.                 list($customer_panel_line$customer_panel_text) = array($index$content);
  329.             }
  330.         }
  331.         // save updated data in a variable ($updatedFileContent)
  332.         $updatedFileContent $file;
  333.         // get old member-prefix
  334.         $oldMemberPrefix substr($member_panel_textstrpos($member_panel_text'uvdesk_site_path.member_prefix') + strlen('uvdesk_site_path.member_prefix: '));
  335.         $oldMemberPrefix preg_replace('/([\r\n\t])/',''$oldMemberPrefix);
  336.         $updatedPrefixForMember = (null !== $member_panel_line) ? substr($member_panel_text0strpos($member_panel_text'uvdesk_site_path.member_prefix') + strlen('uvdesk_site_path.member_prefix: ')) . $website_prefixes['member_prefix'] . PHP_EOL'';
  337.         $updatedPrefixForCustomer = (null !== $customer_panel_line) ? substr($customer_panel_text0strpos($customer_panel_text'uvdesk_site_path.knowledgebase_customer_prefix') + strlen('uvdesk_site_path.knowledgebase_customer_prefix: ')) . $website_prefixes['customer_prefix'] . PHP_EOL '';
  338.         $updatedFileContent[$member_panel_line] = $updatedPrefixForMember;
  339.         $updatedFileContent[$customer_panel_line] = $updatedPrefixForCustomer;
  340.         // flush updated content in file
  341.         file_put_contents($filePath$updatedFileContent);
  342.         $router $this->container->get('router');
  343.         $knowledgebaseURL $router->generate('helpdesk_knowledgebase');
  344.         $memberLoginURL $router->generate('helpdesk_member_handle_login');
  345.         $memberLoginURL str_replace($oldMemberPrefix$website_prefixes['member_prefix'], $memberLoginURL);
  346.         return $collectionURL = [
  347.             'memberLogin' => $memberLoginURL,
  348.             'knowledgebase' => $knowledgebaseURL,
  349.         ];
  350.     }
  351.     public static function getTimeFormats()
  352.     {
  353.         return array(
  354.             'm-d-y G:i' => 'm-d-y G:i (01-15-1991 13:00)',
  355.             'm-d-y h:ia' => 'm-d-y h:ia (01-15-1991 01:00pm)',
  356.             'd-m-y G:i' => 'd-m-y G:i (15-01-1991 13:00)',
  357.             'd-m-y h:ia' => 'd-m-y h:ia (15-01-1991 01:00pm)',
  358.             'd-m G:i' => 'd-m G:i (15-01 13:00)',
  359.             'd-m h:ia' => 'd-m h:ia (15-01 01:00pm)',
  360.             'd-M G:i' => 'd-M G:i (15-Jan 13:00)',
  361.             'd-M h:ia' => 'd-M h:ia (15-Jan 01:00pm)',
  362.             'D-m G:i' => 'D-m G:i (Mon-01 13:00)',
  363.             'D-m h:ia' => 'D-m h:ia (Mon-01 01:00pm)',
  364.             'Y-m-d H:i:sa' => 'Y-m-d H:i:s (1991-01-15 01:00:30pm)',
  365.         );
  366.     }
  367.     public function generateCompleteLocalResourcePathUri($resource)
  368.     {
  369.         $resourceUriComponent parse_url($resource);
  370.         if (!empty($resourceUriComponent['scheme'])) {
  371.             return $resource;
  372.         }
  373.         if (empty($this->completeLocalResourcePathUri)) {
  374.             $router $this->container->get('router');
  375.     
  376.             $scheme $router->getContext()->getScheme();
  377.             $siteurl $this->container->getParameter('uvdesk.site_url');
  378.     
  379.             $baseurl "$scheme://$siteurl";
  380.             $urlComponents parse_url($baseurl);
  381.             $completeLocalResourcePathUri "{$urlComponents['scheme']}://{$urlComponents['host']}";
  382.             if (!empty($urlComponents['path'])) {
  383.                 $completeLocalResourcePathUri .= $urlComponents['path'];
  384.             }
  385.             if (substr($completeLocalResourcePathUri, -1) == '/') {
  386.                 $completeLocalResourcePathUri substr($completeLocalResourcePathUri0, -1);
  387.             }
  388.     
  389.             $this->completeLocalResourcePathUri $completeLocalResourcePathUri;
  390.         }
  391.         if ($resource[0] != '/') {
  392.             $resource "/$resource";
  393.         }
  394.         return $this->completeLocalResourcePathUri $resource;
  395.     }
  396.     public function getAvailableUserAccessScopes($user$userInstance)
  397.     {
  398.         $supportRole $userInstance->getSupportRole();
  399.         $isAdminAccessGranted in_array($supportRole->getId(), [12]) ? true false;
  400.         $availableSupportPrivileges $this->getSupportPrivelegesResources();
  401.         $resolvedAvailableSupportPrivileges = [];
  402.         foreach ($availableSupportPrivileges as $index => $collection) {
  403.             foreach ($collection as $privilegeId => $privilegeDescription) {
  404.                 $resolvedAvailableSupportPrivileges[] = $privilegeId;
  405.             }
  406.         }
  407.         if (false == $isAdminAccessGranted) {
  408.             $assignedUserSupportPrivileges $this->userService->getAssignedUserSupportPrivilegeDetails($user$userInstance);
  409.             $resolvedAssignedUserSupportPrivileges = [];
  410.             foreach ($assignedUserSupportPrivileges as $assignedSupportPrivilege) {
  411.                 foreach ($assignedSupportPrivilege['privileges'] as $privilegeId) {
  412.                     $resolvedAssignedUserSupportPrivileges[] = $privilegeId;
  413.                 }
  414.             }
  415.             return array_map(function ($supportPrivilege) {
  416.                 return strtolower(str_replace('ROLE_AGENT_'''$supportPrivilege));
  417.             }, $resolvedAssignedUserSupportPrivileges);
  418.         }
  419.         
  420.         return array_map(function ($supportPrivilege) {
  421.             return strtolower(str_replace('ROLE_AGENT_'''$supportPrivilege));
  422.         }, $resolvedAvailableSupportPrivileges);
  423.     }
  424. }