src/Controller/SugarCrmController.php line 219

  1. <?php
  2. namespace App\Controller;
  3. use App\BussinesLogic\sugarBussinessLogic;
  4. use App\BussinesLogic\AzureTokenManager;
  5. use App\Trait\ControllerSugarTrait;
  6. use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
  7. use Google\Auth\HttpHandler\HttpClientCache;
  8. use App\BussinesLogic\GoogleBL;
  9. use Google\Cloud\Functions\V1\CloudFunctionsServiceClient;
  10. use App\Entity\Report;
  11. use App\Entity\ReportField;
  12. use App\Entity\ReportPeriod;
  13. use App\Entity\User;
  14. use App\Trait\ControllerTrait;
  15. use GuzzleHttp\Client;
  16. use GuzzleHttp\Exception\RequestException;
  17. use OpenApi\Annotations as OA;
  18. use Nelmio\ApiDocBundle\Annotation\Model;
  19. use Nelmio\ApiDocBundle\Annotation\Security;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Google\Auth\Credentials\ServiceAccountCredentials;
  27. use Google\Auth\Middleware\AuthTokenMiddleware;
  28. use Google\Cloud;
  29. use Google\Api;
  30. use Google\Api\Authentication;
  31. use Google\ApiCore\ClientStream;
  32. use GuzzleHttp\HandlerStack;
  33. /**
  34.  * Controller for CRUD operations on User entities
  35.  */
  36. #[AsController]
  37. #[Route('/crm'name'crm_search')]
  38. class SugarCrmController extends AbstractController {
  39.     use ControllerTrait;
  40.     use ControllerSugarTrait;
  41.     #[Route("/azure_auth"name:"azure_auth"methods:["GET"])]
  42.     public function azureAuth(){
  43.         $tenantId 'b004d87e-aee6-4103-b252-fecb81f32160';
  44.         $clientID 'd6f8dc47-7b5c-4c76-9f49-77b4f1b8f601';
  45.         $apiLoginMs 'https://login.microsoftonline.com/';
  46.         $tokenEndPoint '/oauth2/v2.0/token';
  47.         $tokenScope 'https://graph.microsoft.com/.default';
  48.         $privateKeyPath $this->getParameter('azure_key_path');
  49.         $base64EncodedThumbprint 'nUS/8UKloit7g09Jho230koiavk';
  50.         // initialize the class
  51.         $tokenManager = new AzureTokenManager($tenantId$clientID$apiLoginMs$tokenEndPoint$tokenScope$privateKeyPath$base64EncodedThumbprint);
  52. //$tokenManager = new TokenManager($_ENV["TENANT_ID"], $_ENV["CLIENT_ID"], $_ENV["API_LOGIN_MS"], $_ENV["TOKENENDPOINT"], $_ENV["TOKENSCOPE"], $_ENV["PRIVATEKEYPATH"], $_ENV["BASE64THUMBPRINT"]);
  53. //just call getToken and it will return the json
  54.         $token $tokenManager->getToken();
  55.         return $this->getJsonResponse($token);
  56.     }
  57.     #[Route("/account/{piva}"name:"account_filtered_by_piva"methods:["GET"])]
  58.     public function getAccountByPiva(){
  59.         if($this->token == null )
  60.             $this->token $this->Connect2CRM();
  61.         
  62.         if( $this->sugarCrm->isConnected() !== false ){
  63.             $this->sugarCrm->setObjUrl("Accounts");
  64.             $data $this->sugarCrm->filter("Accounts",["partitaiva_c"=>$this->request->get("piva"), "deleted"=> false],1,0);
  65.             if($data){
  66.                 if( sizeof($data->records) > )
  67.                     return $this->getJsonResponse($data->records);
  68.                 else
  69.                     return $this->getJsonResponse($data);
  70.             }
  71.             return $this->getJsonResponse([]);
  72.         }
  73.     }
  74. /*
  75.  * export report
  76.  /Reports/:id/[json,pdf,csv,base64]
  77.  *
  78.  /Reports/:id/record_count?group_filters[0][industry]=Engineering
  79.  *
  80.  /Reports/:id/chart
  81.  *
  82.  /Reports/:id/records?group_filters[0][industry]=Engineering&use_saved_filters=true&offset=0&max_num=20
  83.  */
  84.     #[Route("/chart/{id}"name'crm_report_chart'methods: ['GET'])]
  85.     public function getChart($id){
  86.        return $this->getReport($idnull'chart');
  87.     }
  88.     #[Route("/report/{id}/export/{export}"name'crm_report_export'methods: ['GET'])]
  89.     public function exportReport($id$export){
  90.         return $this->getReport($idnullnull$export);
  91.     }
  92.     #[Route("/report/{id}/count/{filters}"name'crm_report_total'methods: ['GET'])]
  93.     public function getTotalReport($id$filters){
  94.         return $this->getReport($id$filters);
  95.     }
  96.     public function getReport($id$filters=null$type=null$export=null ){
  97.         if($this->token == null )
  98.             $this->token $this->Connect2CRM();
  99.         if( $this->sugarCrm->isConnected() !== false ){
  100.             $entity "Reports";
  101.             $this->sugarCrm->setObjUrl($entity);
  102.             if($export){
  103.                 $id $id "/$export";
  104.             }
  105.             if($type){
  106.                 $id $id "/$type";
  107.             }
  108.             $afilters null;
  109.             if(!$type and !$export and $filters){
  110.                 $id $id."/record_count";
  111.                 $afilters = [];
  112.                 if($filters != null ) {
  113.                     $afilters explode(","$filters);
  114.                     for ($i 0$i sizeof($afilters); $i++) {
  115.                         $itm explode("="$afilters[$i]);
  116.                         $afilters[$i] = [$itm[0] => $itm[1]];
  117.                     }
  118.                 }
  119.             }
  120.             $data $this->sugarCrm->report($entity."/$id"$afilters);
  121.             if($export !=null or $filters != null or ($type != null and ($type !="pdf" or $type != "html") ) ){
  122.                 if($data)
  123.                     return $this->getJsonResponse($data);
  124.             } else
  125.                 return new Response($data);
  126.             return $this->getJsonResponse([]);
  127.         }
  128.     }
  129. /*public function postModule(){
  130. $body = [
  131.     "name" => "",
  132.     "fiscal_code_c"=>"",
  133.     "description" => "",
  134.     "first_name"=> "",
  135.     "last_name"=> "",
  136.     "full_name"=> "",
  137.     "phone_mobile"=> "",
  138.     "phone_work"=> "",
  139.     "refered_by"=> "",
  140.     "lead_source"=> "bc_origination",
  141.     "contatto_di_orig_c"=> "",
  142.     "status"=> "New",
  143. ];*/
  144. /* Get Leads related records by link
  145. -  /Leads/193bb028-1874-11ef-9c3e-061a8a1a6f52/link/leads_dsreq_products_1
  146. }
  147. */
  148.     #[Route("/{module}/update/{id}"name:"module_update"methods:["PUT"])]
  149.     public function putModule($module,$id){
  150.         $body $this->request->toArray();
  151.         $result null;
  152.         if($this->token == null )
  153.             $this->token $this->Connect2CRM();
  154.         if( $this->sugarCrm->isConnected() !== false ){
  155.             if( !strpos($module,'_') )
  156.                 $entity ucfirst(strtolower($module));
  157.             else $entity $module;
  158.             $result $this->sugarCrm->put$entity$body$id);
  159.         }
  160.         return $this->getJsonResponse($result);
  161.     }
  162.     #[Route("/{module}/add"name:"module_add"methods:["POST"])]
  163.     public function postModule($module){
  164.         $body $this->request->toArray();
  165.         $result null;
  166.         if($this->token == null )
  167.             $this->token $this->Connect2CRM();
  168.         if( $this->sugarCrm->isConnected() !== false ){
  169.             if( !strpos($module,'_') )
  170.                 $entity ucfirst(strtolower($module));
  171.             else $entity $module;
  172.             $result $this->sugarCrm->post$entity$body);
  173.         }
  174.         return $this->getJsonResponse($result);
  175.     }
  176.     #[Route("/{module}/link/{id}/{children_field}"name:"childrens_no_filter_by_id"methods:["GET"])]
  177.     public function getChildrensNoFilter($module,$id$children_field){
  178.         return $this->getChildrensModuleById($module,$id$children_fieldnullnull,50,0);
  179.     }
  180.     #[Route("/{module}/link/{id}/{children_field}/{filters}/{fields}/{max}/{offset}"name:"childrens_with_filter_by_id"methods:["GET"])]
  181.     public function getChildrensModuleById($module,$id$children_field,$filters=null$fields=null$max=1$offset=0){
  182.         if($this->token == null )
  183.             $this->token $this->Connect2CRM();
  184.         if( $this->sugarCrm->isConnected() !== false ){
  185.             if( !strpos($module,'_') )
  186.                 $entity ucfirst(strtolower($module));
  187.             else $entity $module;
  188.             $data =null;
  189.             if($filters != null){
  190.                 $this->sugarCrm->setObjUrl("$entity/$id/link/$children_field");
  191.                 $where = [];
  192.                 if($filters != null ) {
  193.                     $afilters explode(","$filters);
  194.                     foreach ($afilters as $afilterItem ){
  195.                         if (str_contains($afilterItem'||')) {
  196.                             $afieldsFiltered explode("||"$afilterItem);
  197.                             foreach ($afieldsFiltered as $afieldItem){
  198.                                 $aItem explode("="$afieldItem);
  199.                                 if (str_contains($aItem[1], '$')) {
  200.                                     $aSubItem explode(":"$aItem[1]);
  201.                                     $where['$or'][] = [ $aItem[0] =>[ $aSubItem[0]=> $aSubItem[1] ] ];
  202.                                 }else{
  203.                                     $where['$or'][] = [ $aItem[0] => $aItem[1] ];
  204.                                 }
  205.                             }
  206.                         }else{
  207.                             $aItem explode("="$afilterItem);
  208.                             if (str_contains($aItem[1], '$')) {
  209.                                 $aSubItem explode(":"$aItem[1]);
  210.                                 $where[$aItem[0]][$aSubItem[0]] = $aSubItem[1];
  211.                             }else{
  212.                                 $where[$aItem[0]] = $aItem[1];
  213.                             }
  214.                         }
  215.                     }
  216. /*
  217.                     for ($i = 0; $i < sizeof($afilters); $i++) {
  218.                         $itm = explode("=", $afilters[$i]);
  219.                         if($itm[0] != "q"){
  220.                             if(str_contains($itm[1],'$')){
  221.                                 $itmm = explode(":", $itm[1]);
  222.                                 $where[$itm[0]][$itmm[0]] = $itmm[1];
  223.                             }else {
  224.                                 $where[$itm[0]] = $itm[1];
  225.                             }
  226.                         }
  227.                     } */
  228.                 }
  229.                 $where["deleted"] = false;
  230.                 $fields $this->getRelatedField($fields);
  231.                 $data $this->sugarCrm->filter("$entity/$id/link/$children_field",$where,$max,$offset,$fields"date_modified:DESC");
  232.             }else{
  233.                 $this->sugarCrm->setObjUrl($entity);
  234.                 $data $this->sugarCrm->get($entity,$id."/link/$children_field");
  235.             }
  236.             if($data) {
  237.                 if (sizeof($data->records) > 0)
  238.                     return $this->getJsonResponse($data->records);
  239.                 else
  240.                     return $this->getJsonResponse($data);
  241.             }
  242.             return $this->getJsonResponse([]);
  243.         }
  244.     }
  245.     #[Route("/{module}/id/{id}/{fields}"name:"module_by_id"methods:["GET"])]
  246.     public function getModuleById($module,$id,$fields){
  247.         if($this->token == null )
  248.             $this->token $this->Connect2CRM();
  249.         if( $this->sugarCrm->isConnected() !== false ){
  250.             if( !strpos($module,'_') )
  251.                 $entity ucfirst(strtolower($module));
  252.             else $entity strtolower($module);
  253.             $this->sugarCrm->setObjUrl($entity);
  254.             $where["deleted"] = false;
  255.             $where["id"] = $id;
  256.             $data $this->sugarCrm->filter($entity,$where,1,0,$fields);
  257.             //$data = $this->sugarCrm->get($entity,$id);
  258.             if($data) {
  259.                 if (sizeof($data->records) > 0)
  260.                     return $this->getJsonResponse($data->records);
  261.                 else
  262.                     return $this->getJsonResponse($data);
  263.             }
  264.             return $this->getJsonResponse([]);
  265.         }
  266.     }
  267.     public function getRelatedField($fields){
  268.         $fieldsArray explode(","$fields);
  269.         foreach ($fieldsArray as $index => $item) {
  270.             if( is_numeric(substr($item, -1)) && substr($item, -2) == "_".substr($item, -1)  ){
  271.                 $fieldsArray[$index] = '{"name" : "'.$item.'", "fields" : ["id","name"]}';
  272.             }
  273.         }
  274.         return implode(",",$fieldsArray);
  275.     }
  276. #[Route("/{module}/{filters}/{max}/{offset}/{fields}"name:"module_filtered_by_arg"methods:["GET"])]
  277. public function getModule($module,$filters$max=1$offset=0$fields=null){
  278.     if($this->token == null )
  279.         $this->token $this->Connect2CRM();
  280.     if( $this->sugarCrm->isConnected() !== false ){
  281.         if( !strpos($module,'_') )
  282.             $entity ucfirst(strtolower($module));
  283.         else $entity $module;
  284.         $this->sugarCrm->setObjUrl($entity);
  285.         $where = [];
  286.         $afilters = [];
  287.         $q null;
  288.         if($filters != null ) {
  289.             $afilters explode(","$filters);
  290.             for ($i 0$i sizeof($afilters); $i++) {
  291.                 $itm explode("="$afilters[$i]);
  292.                 if($itm[0] == "q"){
  293.                     $q $itm[1];
  294.                 }else{
  295.                     if(str_contains($itm[1],'$')){
  296.                         $itmm explode(":"$itm[1]);
  297.                         $where[$itm[0]][$itmm[0]] = $itmm[1];
  298.                     }else {
  299.                         $where[$itm[0]] = $itm[1];
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.         $fields $this->getRelatedField($fields);
  305.         $where["deleted"] = false;
  306.         if($q != null $where null;
  307.         $data $this->sugarCrm->filter($entity,$where,$max,$offset,$fields,null,null,null,$q);
  308.         if($data){
  309.             if( sizeof($data->records) > )
  310.                 return $this->getJsonResponse($data->records);
  311.             else
  312.                 return $this->getJsonResponse($data);
  313.         }
  314.         return $this->getJsonResponse([]);
  315.     }
  316. }
  317. }