vendor/uvdesk/automation-bundle/Services/AutomationService.php line 29

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\AutomationBundle\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Webkul\UVDesk\AutomationBundle\Workflow\FunctionalGroup;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Doctrine\DBAL\Connection;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  12. use UVDesk\CommunityPackages\UVDesk\CustomFields\Entity\CustomFields;
  13. use UVDesk\CommunityPackages\UVDesk\CustomFields\Entity\CustomFieldsValues;
  14. use Webkul\UVDesk\CommunityPackages\UVDesk\CustomFields\Entity;
  15. use Webkul\UVDesk\CommunityPackages\UVDesk\CustomFields\Entity as CommunityPackageEntities;
  16. use Webkul\UVDesk\AutomationBundle\Entity\Workflow;
  17. use Doctrine\Persistence\ManagerRegistry;
  18. use UVDesk\CommunityPackages\UVDesk\CustomFields\Services\CustomFieldsService;
  19. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreFrameworkBundleEntities;
  20. class AutomationService 
  21. {
  22.     private $container;
  23.     private $requestStack;
  24.     private $entityManager;
  25.     public function __construct(ContainerInterface $containerRequestStack $requestStackEntityManagerInterface $entityManagerTranslatorInterface $translator)
  26.     {
  27.         $this->container $container;
  28.         $this->requestStack $requestStack;
  29.         $this->entityManager $entityManager;
  30.         $this->translator $translator;
  31.     }
  32.     public function getWorkflowEvents()
  33.     {
  34.         return [
  35.             FunctionalGroup::USER => $this->translator->trans('User'),
  36.             FunctionalGroup::AGENT => $this->translator->trans('Agent'),
  37.             FunctionalGroup::CUSTOMER => $this->translator->trans('Customer'),
  38.             FunctionalGroup::TICKET => $this->translator->trans('Ticket'),
  39.         ];
  40.     }
  41.     public function getWorkflowEventValues()
  42.     {
  43.         $ticketEventCollection = [];
  44.         foreach ($this->container->get('uvdesk.automations.workflows')->getRegisteredWorkflowEvents() as $workflowDefinition) {
  45.             $functionalGroup $workflowDefinition->getFunctionalGroup();
  46.             if (!isset($ticketEventCollection[$functionalGroup])) {
  47.                 $ticketEventCollection[$functionalGroup] = [];
  48.             }
  49.             $ticketEventCollection[$functionalGroup][$workflowDefinition->getId()] = $this->translator->trans($workflowDefinition->getDescription());
  50.         }
  51.         return $ticketEventCollection;
  52.     }
  53.     public function getWorkflowConditions()
  54.     {
  55.         // Get Entity Manager
  56.         //$entityManager = $this->getDoctrine()->getManager();
  57.         // Fetch custom fields from the table where decider = 1
  58.         $Workcustoms $this->entityManager->getRepository(CustomFields::class)->findBy(['decider' => 1]);
  59.             
  60.         $conditions = [
  61.             'ticket' => [
  62.                 'mail' => [
  63.                     [
  64.                         'lable' => $this->translator->trans('From Email'),
  65.                         'value' => 'from_mail',
  66.                         'match' => 'email'
  67.                     ],
  68.                     [
  69.                         'lable' => $this->translator->trans('To Email'),
  70.                         'value' => 'to_mail',
  71.                         'match' => 'email'
  72.                     ],
  73.                 ],
  74.                 'ticket' => [
  75.                     [
  76.                         'lable' => $this->translator->trans('Subject'),
  77.                         'value' => 'subject',
  78.                         'match' => 'string'
  79.                     ],
  80.                     [
  81.                         'lable' => $this->translator->trans('Description'),
  82.                         'value' => 'description',
  83.                         'match' => 'string'
  84.                     ],
  85.                     [
  86.                         'lable' => $this->translator->trans('Subject or Description'),
  87.                         'value' => 'subject_or_description',
  88.                         'match' => 'string'
  89.                     ],
  90.                     [
  91.                         'lable' => $this->translator->trans('Priority'),
  92.                         'value' => 'TicketPriority',
  93.                         'match' => 'select'
  94.                     ],
  95.                     [
  96.                         'lable' => $this->translator->trans('Type'),
  97.                         'value' => 'TicketType',
  98.                         'match' => 'select'
  99.                     ],
  100.                     [
  101.                         'lable' => $this->translator->trans('Status'),
  102.                         'value' => 'TicketStatus',
  103.                         'match' => 'select'
  104.                     ],
  105.                     [
  106.                         'lable' => $this->translator->trans('Source'),
  107.                         'value' => 'source',
  108.                         'match' => 'select'
  109.                     ],
  110.                     [
  111.                         'lable' => $this->translator->trans('Created'),
  112.                         'value' => 'created',
  113.                         'match' => 'date'
  114.                     ],
  115.                     [
  116.                         'lable' => $this->translator->trans('Agent'),
  117.                         'value' => 'agent',
  118.                         'match' => 'select'
  119.                     ],
  120.                     [
  121.                         'lable' => $this->translator->trans('Group'),
  122.                         'value' => 'group',
  123.                         'match' => 'select'
  124.                     ],
  125.                     [
  126.                         'lable' => $this->translator->trans('Customfields'),
  127.                         'value' => 'customfields',
  128.                         'match' => 'select'
  129.                     ],
  130.                     [
  131.                         'lable' => $this->translator->trans('Team'),
  132.                         'value' => 'team',
  133.                         'match' => 'select'
  134.                     ],
  135.                 ],
  136.                 'customer' => [
  137.                     [
  138.                         'lable' => $this->translator->trans('Customer Name'),
  139.                         'value' => 'customer_name',
  140.                         'match' => 'string'
  141.                     ],
  142.                     [
  143.                         'lable' => $this->translator->trans('Customer Email'),
  144.                         'value' => 'customer_email',
  145.                         'match' => 'email'
  146.                     ],
  147.                 ],
  148.             ],
  149.         ];
  150.         
  151.         // -----------------------------------------------------------------------
  152.         // 1) Build the customFieldConditions array
  153.         // -----------------------------------------------------------------------
  154.         $customFieldConditions = [];
  155.         foreach ($Workcustoms as $field) {
  156.             $customFieldConditions[] = [
  157.                 'type'  => 'hidden',
  158.                 'lable' => $this->translator->trans($field->getName()),
  159.                 'value' => 'custom_field_' $field->getId(),
  160.                 'match' => 'string',
  161.             ];
  162.         }
  163.         // // -----------------------------------------------------------------------
  164.         // // 2) Merge the customFieldConditions into the ticket array
  165.         // // -----------------------------------------------------------------------
  166.         // // This will append all custom-field conditions to the existing array
  167.         // $conditions['ticket']['ticket'] = array_merge(
  168.         //     $conditions['ticket']['ticket'],
  169.         //     $customFieldConditions
  170.         // );
  171.         $conditions['ticket']['Customfields'] = $customFieldConditions;
  172.         return $conditions;
  173.     }
  174.     public function getWorkflowMatchConditions()
  175.     {
  176.         return [
  177.             'email' => [
  178.                 [
  179.                     'lable' => $this->translator->trans('Is Equal To'),
  180.                     'value' => 'is'
  181.                 ],
  182.                 [
  183.                     'lable' => $this->translator->trans('Is Not Equal To'),
  184.                     'value' => 'isNot'
  185.                 ],
  186.                 [
  187.                     'lable' => $this->translator->trans('Contains'),
  188.                     'value' => 'contains'
  189.                 ],
  190.                 [
  191.                     'lable' => $this->translator->trans('Does Not Contain'),
  192.                     'value' => 'notContains'
  193.                 ],
  194.             ],
  195.             'string' => [
  196.                 [
  197.                     'lable' => $this->translator->trans('Is Equal To'),
  198.                     'value' => 'is'
  199.                 ],
  200.                 [
  201.                     'lable' => $this->translator->trans('Is Not Equal To'),
  202.                     'value' => 'isNot'
  203.                 ],
  204.                 [
  205.                     'lable' => $this->translator->trans('Contains'),
  206.                     'value' => 'contains'
  207.                 ],
  208.                 [
  209.                     'lable' => $this->translator->trans('Does Not Contain'),
  210.                     'value' => 'notContains'
  211.                 ],
  212.                 [
  213.                     'lable' => $this->translator->trans('Starts With'),
  214.                     'value' => 'startWith'
  215.                 ],
  216.                 [
  217.                     'lable' => $this->translator->trans('Ends With'),
  218.                     'value' => 'endWith'
  219.                 ],
  220.             ],
  221.             'select' => [
  222.                 [
  223.                     'lable' => $this->translator->trans('Is Equal To'),
  224.                     'value' => 'is'
  225.                 ],
  226.                 [
  227.                     'lable' => $this->translator->trans('Is Not Equal To'),
  228.                     'value' => 'isNot'
  229.                 ],
  230.             ],
  231.             'date' => [
  232.                 [
  233.                     'lable' => $this->translator->trans('Before'),
  234.                     'value' => 'before'
  235.                 ],
  236.                 [
  237.                     'lable' => $this->translator->trans('Before On'),
  238.                     'value' => 'beforeOn'
  239.                 ],
  240.                 [
  241.                     'lable' => $this->translator->trans('After'),
  242.                     'value' => 'after'
  243.                 ],
  244.                 [
  245.                     'lable' => $this->translator->trans('After On'),
  246.                     'value' => 'afterOn'
  247.                 ],
  248.             ],
  249.             'datetime' => [
  250.                 [
  251.                     'lable' => $this->translator->trans('Before'),
  252.                     'value' => 'beforeDateTime'
  253.                 ],
  254.                 [
  255.                     'lable' => $this->translator->trans('Before On'),
  256.                     'value' => 'beforeDateTimeOn'
  257.                 ],
  258.                 [
  259.                     'lable' => $this->translator->trans('After'),
  260.                     'value' => 'afterDateTime'
  261.                 ],
  262.                 [
  263.                     'lable' => $this->translator->trans('After On'),
  264.                     'value' => 'afterDateTimeOn'
  265.                 ],
  266.             ],
  267.             'time' => [
  268.                 [
  269.                     'lable' => $this->translator->trans('Before'),
  270.                     'value' => 'beforeTime'
  271.                 ],
  272.                 [
  273.                     'lable' => $this->translator->trans('Before On'),
  274.                     'value' => 'beforeTimeOn'
  275.                 ],
  276.                 [
  277.                     'lable' => $this->translator->trans('After'),
  278.                     'value' => 'afterTime'
  279.                 ],
  280.                 [
  281.                     'lable' => $this->translator->trans('After On'),
  282.                     'value' => 'afterTimeOn'
  283.                 ],
  284.             ],
  285.             'number' => [
  286.                 [
  287.                     'lable' => $this->translator->trans('Is Equal To'),
  288.                     'value' => 'is'
  289.                 ],
  290.                 [
  291.                     'lable' => $this->translator->trans('Is Not Equal To'),
  292.                     'value' => 'isNot'
  293.                 ],
  294.                 [
  295.                     'lable' => $this->translator->trans('Contains'),
  296.                     'value' => 'contains'
  297.                 ],
  298.                 [
  299.                     'lable' => $this->translator->trans('Greater Than'),
  300.                     'value' => 'greaterThan'
  301.                 ],
  302.                 [
  303.                     'lable' => $this->translator->trans('Less Than'),
  304.                     'value' => 'lessThan'
  305.                 ],
  306.             ],
  307.         ];
  308.     }
  309.     public function getWorkflowActions($force false)
  310.     {
  311.         $workflowActions = [];
  312.         
  313.         // @TODO: Add minimum required access levels to workflow actions to restrict usage
  314.         foreach ($this->container->get('uvdesk.automations.workflows')->getRegisteredWorkflowActions() as $workflowDefinition) {
  315.             $functionalGroup $workflowDefinition->getFunctionalGroup();
  316.             if (!isset($workflowActions[$functionalGroup])) {
  317.                 $workflowActions[$functionalGroup] = [];
  318.             }
  319.             $workflowActions[$functionalGroup][$workflowDefinition->getId()] = $this->translator->trans($workflowDefinition->getDescription());
  320.         }
  321.         return $workflowActions;
  322.     }
  323.     public function getPreparedResponseActions($force false)
  324.     {
  325.         $preparedResponseActions = [];
  326.         // @TODO: Add minimum required access levels to prepared response actions to restrict usage
  327.         foreach ($this->container->get('uvdesk.automations.prepared_responses')->getRegisteredPreparedResponseActions() as $preparedResponseDefinition) {
  328.             $functionalGroup $preparedResponseDefinition->getFunctionalGroup();
  329.             if (!isset($preparedResponseActions[$functionalGroup])) {
  330.                 $preparedResponseActions[$functionalGroup] = [];
  331.             }
  332.             $preparedResponseActions[$functionalGroup][$preparedResponseDefinition->getId()] = $this->translator->trans($preparedResponseDefinition->getDescription());
  333.         }
  334.         return $preparedResponseActions;
  335.     }
  336. }