src/Controller/SugarCrmController.php line 219
<?php
namespace App\Controller;
use App\BussinesLogic\sugarBussinessLogic;
use App\BussinesLogic\AzureTokenManager;
use App\Trait\ControllerSugarTrait;
use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
use Google\Auth\HttpHandler\HttpClientCache;
use App\BussinesLogic\GoogleBL;
use Google\Cloud\Functions\V1\CloudFunctionsServiceClient;
use App\Entity\Report;
use App\Entity\ReportField;
use App\Entity\ReportPeriod;
use App\Entity\User;
use App\Trait\ControllerTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use OpenApi\Annotations as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\Middleware\AuthTokenMiddleware;
use Google\Cloud;
use Google\Api;
use Google\Api\Authentication;
use Google\ApiCore\ClientStream;
use GuzzleHttp\HandlerStack;
/**
* Controller for CRUD operations on User entities
*/
#[AsController]
#[Route('/crm', name: 'crm_search')]
class SugarCrmController extends AbstractController {
use ControllerTrait;
use ControllerSugarTrait;
#[Route("/azure_auth", name:"azure_auth", methods:["GET"])]
public function azureAuth(){
$tenantId = 'b004d87e-aee6-4103-b252-fecb81f32160';
$clientID = 'd6f8dc47-7b5c-4c76-9f49-77b4f1b8f601';
$apiLoginMs = 'https://login.microsoftonline.com/';
$tokenEndPoint = '/oauth2/v2.0/token';
$tokenScope = 'https://graph.microsoft.com/.default';
$privateKeyPath = $this->getParameter('azure_key_path');
$base64EncodedThumbprint = 'nUS/8UKloit7g09Jho230koiavk';
// initialize the class
$tokenManager = new AzureTokenManager($tenantId, $clientID, $apiLoginMs, $tokenEndPoint, $tokenScope, $privateKeyPath, $base64EncodedThumbprint);
//$tokenManager = new TokenManager($_ENV["TENANT_ID"], $_ENV["CLIENT_ID"], $_ENV["API_LOGIN_MS"], $_ENV["TOKENENDPOINT"], $_ENV["TOKENSCOPE"], $_ENV["PRIVATEKEYPATH"], $_ENV["BASE64THUMBPRINT"]);
//just call getToken and it will return the json
$token = $tokenManager->getToken();
return $this->getJsonResponse($token);
}
#[Route("/account/{piva}", name:"account_filtered_by_piva", methods:["GET"])]
public function getAccountByPiva(){
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
$this->sugarCrm->setObjUrl("Accounts");
$data = $this->sugarCrm->filter("Accounts",["partitaiva_c"=>$this->request->get("piva"), "deleted"=> false],1,0);
if($data){
if( sizeof($data->records) > 0 )
return $this->getJsonResponse($data->records);
else
return $this->getJsonResponse($data);
}
return $this->getJsonResponse([]);
}
}
/*
* export report
/Reports/:id/[json,pdf,csv,base64]
*
/Reports/:id/record_count?group_filters[0][industry]=Engineering
*
/Reports/:id/chart
*
/Reports/:id/records?group_filters[0][industry]=Engineering&use_saved_filters=true&offset=0&max_num=20
*/
#[Route("/chart/{id}", name: 'crm_report_chart', methods: ['GET'])]
public function getChart($id){
return $this->getReport($id, null, 'chart');
}
#[Route("/report/{id}/export/{export}", name: 'crm_report_export', methods: ['GET'])]
public function exportReport($id, $export){
return $this->getReport($id, null, null, $export);
}
#[Route("/report/{id}/count/{filters}", name: 'crm_report_total', methods: ['GET'])]
public function getTotalReport($id, $filters){
return $this->getReport($id, $filters);
}
public function getReport($id, $filters=null, $type=null, $export=null ){
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
$entity = "Reports";
$this->sugarCrm->setObjUrl($entity);
if($export){
$id = $id . "/$export";
}
if($type){
$id = $id . "/$type";
}
$afilters = null;
if(!$type and !$export and $filters){
$id = $id."/record_count";
$afilters = [];
if($filters != null ) {
$afilters = explode(",", $filters);
for ($i = 0; $i < sizeof($afilters); $i++) {
$itm = explode("=", $afilters[$i]);
$afilters[$i] = [$itm[0] => $itm[1]];
}
}
}
$data = $this->sugarCrm->report($entity."/$id", $afilters);
if($export !=null or $filters != null or ($type != null and ($type !="pdf" or $type != "html") ) ){
if($data)
return $this->getJsonResponse($data);
} else
return new Response($data);
return $this->getJsonResponse([]);
}
}
/*public function postModule(){
$body = [
"name" => "",
"fiscal_code_c"=>"",
"description" => "",
"first_name"=> "",
"last_name"=> "",
"full_name"=> "",
"phone_mobile"=> "",
"phone_work"=> "",
"refered_by"=> "",
"lead_source"=> "bc_origination",
"contatto_di_orig_c"=> "",
"status"=> "New",
];*/
/* Get Leads related records by link
- /Leads/193bb028-1874-11ef-9c3e-061a8a1a6f52/link/leads_dsreq_products_1
}
*/
#[Route("/{module}/update/{id}", name:"module_update", methods:["PUT"])]
public function putModule($module,$id){
$body = $this->request->toArray();
$result = null;
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
if( !strpos($module,'_') )
$entity = ucfirst(strtolower($module));
else $entity = $module;
$result = $this->sugarCrm->put( $entity, $body, $id);
}
return $this->getJsonResponse($result);
}
#[Route("/{module}/add", name:"module_add", methods:["POST"])]
public function postModule($module){
$body = $this->request->toArray();
$result = null;
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
if( !strpos($module,'_') )
$entity = ucfirst(strtolower($module));
else $entity = $module;
$result = $this->sugarCrm->post( $entity, $body);
}
return $this->getJsonResponse($result);
}
#[Route("/{module}/link/{id}/{children_field}", name:"childrens_no_filter_by_id", methods:["GET"])]
public function getChildrensNoFilter($module,$id, $children_field){
return $this->getChildrensModuleById($module,$id, $children_field, null, null,50,0);
}
#[Route("/{module}/link/{id}/{children_field}/{filters}/{fields}/{max}/{offset}", name:"childrens_with_filter_by_id", methods:["GET"])]
public function getChildrensModuleById($module,$id, $children_field,$filters=null, $fields=null, $max=1, $offset=0){
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
if( !strpos($module,'_') )
$entity = ucfirst(strtolower($module));
else $entity = $module;
$data =null;
if($filters != null){
$this->sugarCrm->setObjUrl("$entity/$id/link/$children_field");
$where = [];
if($filters != null ) {
$afilters = explode(",", $filters);
foreach ($afilters as $afilterItem ){
if (str_contains($afilterItem, '||')) {
$afieldsFiltered = explode("||", $afilterItem);
foreach ($afieldsFiltered as $afieldItem){
$aItem = explode("=", $afieldItem);
if (str_contains($aItem[1], '$')) {
$aSubItem = explode(":", $aItem[1]);
$where['$or'][] = [ $aItem[0] =>[ $aSubItem[0]=> $aSubItem[1] ] ];
}else{
$where['$or'][] = [ $aItem[0] => $aItem[1] ];
}
}
}else{
$aItem = explode("=", $afilterItem);
if (str_contains($aItem[1], '$')) {
$aSubItem = explode(":", $aItem[1]);
$where[$aItem[0]][$aSubItem[0]] = $aSubItem[1];
}else{
$where[$aItem[0]] = $aItem[1];
}
}
}
/*
for ($i = 0; $i < sizeof($afilters); $i++) {
$itm = explode("=", $afilters[$i]);
if($itm[0] != "q"){
if(str_contains($itm[1],'$')){
$itmm = explode(":", $itm[1]);
$where[$itm[0]][$itmm[0]] = $itmm[1];
}else {
$where[$itm[0]] = $itm[1];
}
}
} */
}
$where["deleted"] = false;
$fields = $this->getRelatedField($fields);
$data = $this->sugarCrm->filter("$entity/$id/link/$children_field",$where,$max,$offset,$fields, "date_modified:DESC");
}else{
$this->sugarCrm->setObjUrl($entity);
$data = $this->sugarCrm->get($entity,$id."/link/$children_field");
}
if($data) {
if (sizeof($data->records) > 0)
return $this->getJsonResponse($data->records);
else
return $this->getJsonResponse($data);
}
return $this->getJsonResponse([]);
}
}
#[Route("/{module}/id/{id}/{fields}", name:"module_by_id", methods:["GET"])]
public function getModuleById($module,$id,$fields){
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
if( !strpos($module,'_') )
$entity = ucfirst(strtolower($module));
else $entity = strtolower($module);
$this->sugarCrm->setObjUrl($entity);
$where["deleted"] = false;
$where["id"] = $id;
$data = $this->sugarCrm->filter($entity,$where,1,0,$fields);
//$data = $this->sugarCrm->get($entity,$id);
if($data) {
if (sizeof($data->records) > 0)
return $this->getJsonResponse($data->records);
else
return $this->getJsonResponse($data);
}
return $this->getJsonResponse([]);
}
}
public function getRelatedField($fields){
$fieldsArray = explode(",", $fields);
foreach ($fieldsArray as $index => $item) {
if( is_numeric(substr($item, -1)) && substr($item, -2) == "_".substr($item, -1) ){
$fieldsArray[$index] = '{"name" : "'.$item.'", "fields" : ["id","name"]}';
}
}
return implode(",",$fieldsArray);
}
#[Route("/{module}/{filters}/{max}/{offset}/{fields}", name:"module_filtered_by_arg", methods:["GET"])]
public function getModule($module,$filters, $max=1, $offset=0, $fields=null){
if($this->token == null )
$this->token = $this->Connect2CRM();
if( $this->sugarCrm->isConnected() !== false ){
if( !strpos($module,'_') )
$entity = ucfirst(strtolower($module));
else $entity = $module;
$this->sugarCrm->setObjUrl($entity);
$where = [];
$afilters = [];
$q = null;
if($filters != null ) {
$afilters = explode(",", $filters);
for ($i = 0; $i < sizeof($afilters); $i++) {
$itm = explode("=", $afilters[$i]);
if($itm[0] == "q"){
$q = $itm[1];
}else{
if(str_contains($itm[1],'$')){
$itmm = explode(":", $itm[1]);
$where[$itm[0]][$itmm[0]] = $itmm[1];
}else {
$where[$itm[0]] = $itm[1];
}
}
}
}
$fields = $this->getRelatedField($fields);
$where["deleted"] = false;
if($q != null ) $where = null;
$data = $this->sugarCrm->filter($entity,$where,$max,$offset,$fields,null,null,null,$q);
if($data){
if( sizeof($data->records) > 0 )
return $this->getJsonResponse($data->records);
else
return $this->getJsonResponse($data);
}
return $this->getJsonResponse([]);
}
}
}