diff --git a/custom/Extension/application/Ext/Extensions/PrvCloud.php b/custom/Extension/application/Ext/Extensions/PrvCloud.php new file mode 100755 index 00000000000..5b3faf40bc8 --- /dev/null +++ b/custom/Extension/application/Ext/Extensions/PrvCloud.php @@ -0,0 +1,6 @@ + "prv_cloud_class", + "extdir" => "PrvCloud", + "file" => 'prv_cloud.ext.php', + "module" => ""); diff --git a/custom/Extension/application/Ext/PrvCloud/prv_cloud.ext.php b/custom/Extension/application/Ext/PrvCloud/prv_cloud.ext.php new file mode 100755 index 00000000000..d0fed7b60bc --- /dev/null +++ b/custom/Extension/application/Ext/PrvCloud/prv_cloud.ext.php @@ -0,0 +1,403 @@ +host = $sugar_config['PVT_HOST']; + $this->client = new GuzzleClient(['base_uri' => $this->host, 'timeout' => 15, 'connect_timeout' => 15, 'handler' => $stack, 'debug' => false, 'http_errors' => false, ]); + + $this->authTok = $sugar_config['PVT_AUTH']; + $this->record_type_id = $sugar_config['PVT_record_type_id']; + $this->workspace_id = $sugar_config['PVT_workspace_id']; + $this->container_guid = $sugar_config['PVT_container_guid']; + + } + /********Generate Token********/ + private function makeAuth() + { + if (session_id() == "") + { + session_start(); + } + $this->token = isset($_SESSION["PVT_TOKEN"]) ? $_SESSION["PVT_TOKEN"] : ''; + + if (empty($this->token)) + { + $res = $this + ->client + ->request("POST", $this->host . "/api/refresh_token", ["headers" => ["Accept" => "application/json", "Authorization" => "Bearer " . $this->authTok, ], ]); + $response = (array)json_decode($res->getBody() + ->getContents()); + + if (isset($response["access_token"]) && !empty($response["access_token"])) + { + $_SESSION["PVT_TOKEN"] = $response["access_token"]; + $this->token = $response["access_token"]; + } + else + { + $this->RedirectBack("There some error in API authentication. Try again"); + #$response['code'] $response['message'] + + } + } + } + /*******create record*********/ + public function createRecord($data, $depth = 0) + { + $this->setClient(); + /*start httpclient*/ + $this->makeAuth(); + $data_t = ["record_type_id" => $this->record_type_id, "workspace_id" => $this->workspace_id, "container_guid" => $this->container_guid]; + $data_f = array_merge($data, $data_t); + + + if ($this->token) + { + + try + { + + $res = $this + ->client + ->post("/api/record", [ + + 'headers' => ['Content-Type' => 'application/json', 'Authorization' => "Bearer " . $this->token], 'body' => json_encode($data_f) ]); + + $response = (array)json_decode($res->getBody()->getContents()); + + /**check if authentication fail try to recreate token****/ + if (trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->createRecord($data_f, ++$depth); + } + else + { + $GLOBALS["log"]->fatal("Record create/update error:" . var_export($response, true)); + $this->RedirectBack($response['message']); + } + + } + elseif (isset($response['record']) && !empty($response['record'])) + { + #"Record added"; + $this->RedirectBack("Record added successfully guid - " . $response['guid'], 1); + + } + else + { + $GLOBALS["log"]->fatal("Record create/update error:" . var_export($response, true)); + $this->RedirectBack($response['message']); + + } + + } + catch(RequestException $e) + { + if ($e->hasResponse()) + { + $exception = (string)$e->getResponse() + ->getBody(); + $exception = json_decode($exception); + $GLOBALS["log"]->fatal("PVT - error " . var_export($exception, true)); + return new JsonResponse($exception, $e->getCode()); + } + else + { + $GLOBALS["log"]->fatal("PVT = error" . var_export($e, true)); + return new JsonResponse($e->getMessage() , 503); + } + + } + } + } + /**fetch records from container***/ + public function listContainerRecords($depth = 0) + { + $this->setClient(); + /*start httpclient*/ + $this->makeAuth(); + $res = $this + ->client + ->request('GET', '/api/container/' . $this->container_guid . '/records', ['headers' => ['accept' => 'application/json', 'Authorization' => "Bearer " . $this->token]]); + + $response = (array)json_decode($res->getBody() + ->getContents()); + + /**check if authentication fail try to recreate token****/ + if (isset($response['code']) && trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->listContainerRecords(++$depth); + } + else + { + $GLOBALS["log"]->fatal($response['message']); + } + + } + elseif (isset($response[0]) && !empty($response[0]->guid)) + { + #"Record added"; + $GLOBALS["log"]->fatal("Record loaded successfully container - " . $this->container_guid); + return $response; + + } + else + { + $GLOBALS["log"]->fatal($response['message']); + + } + + } + /**fetch records by guid***/ + public function listRecords($id,$depth = 0) + { + $this->setClient(); + /*start httpclient*/ + $this->makeAuth(); + $res = $this + ->client + ->request('GET', '/api/record/' . $id.'/decrypt', ['headers' => ['accept' => 'application/json', 'Authorization' => "Bearer " . $this->token]]); + + $response = (array)json_decode($res->getBody() + ->getContents()); + + /**check if authentication fail try to recreate token****/ + if (isset($response['code']) && trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->listRecords($id,++$depth); + } + else + { + $GLOBALS["log"]->fatal($response['message']); + } + + } + elseif (isset($response['record']) && !empty($response['record'])) + { + #"Record added"; + $GLOBALS["log"]->fatal("Record loaded successfully - " . $id); + return $response; + + } + else + { + $GLOBALS["log"]->fatal($response['message']); + + } + + } + /******list record bulk*******/ + public function listBulkRecords($rids,$depth = 0) + { + if ($this->token) + { + + try + { + + $res = $this + ->client + ->post("/api/record/bulk/decrypt", [ + + 'headers' => ['Content-Type' => 'application/json','accept' => 'application/json', 'Authorization' => "Bearer " . $this->token], 'body' => json_encode($rids) ]); + + $response = (array)json_decode($res->getBody()->getContents()); + + /**check if authentication fail try to recreate token****/ + if (isset($response['code']) && trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->listBulkRecords($rids, ++$depth); + } + else + { + $this->RedirectBack($response['message']); + } + + } + elseif (isset($response) && !empty($response)) + { + $GLOBALS["log"]->fatal("Bulk Record loaded successfully "); + return $response; + + } + else + { + + $this->RedirectBack($response['message']); + + } + + } + catch(RequestException $e) + { + if ($e->hasResponse()) + { + $exception = (string)$e->getResponse() + ->getBody(); + $exception = json_decode($exception); + $GLOBALS["log"]->fatal("PVT - error " . var_export($exception, true)); + return new JsonResponse($exception, $e->getCode()); + } + else + { + $GLOBALS["log"]->fatal("PVT = error" . var_export($e, true)); + return new JsonResponse($e->getMessage() , 503); + } + + } + } + + } + public function deleteRecord($id,$depth = 0){ + $this->setClient(); + /*start httpclient*/ + $this->makeAuth(); + $res = $this + ->client + ->request('DELETE', '/api/record/' . $id, ['headers' => ['accept' => 'application/json', 'Authorization' => "Bearer " . $this->token]]); + + + + /**check if authentication fail try to recreate token****/ + if (isset($response['code']) && trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->deleteRecord($id,++$depth); + } + else + { + $GLOBALS["log"]->fatal($response['message']); + } + + } + elseif (isset($response) && empty($response)) + { + #"Record added"; + $GLOBALS["log"]->fatal("Record Deleted successfully - " . $id); + + + } + else + { + $GLOBALS["log"]->fatal(@$response['message']); + + } + } + /*******Update record*********/ + public function updateRecord($data, $id,$depth = 0) + { + $this->setClient(); + /*start httpclient*/ + $this->makeAuth(); + $data_t = ["record_type_id" => $this->record_type_id, "workspace_id" => $this->workspace_id, "container_guid" => $this->container_guid]; + $data_f = array_merge($data, $data_t); + + if ($this->token) + { + + try + { + + $res = $this + ->client + ->PATCH("/api/record/".$id, [ + + 'headers' => ['accept' => 'application/json','Content-Type' => 'application/json', 'Authorization' => "Bearer " . $this->token], 'body' => json_encode($data_f) ]); + + $response = (array)json_decode($res->getBody()->getContents()); + + /**check if authentication fail try to recreate token****/ + if (trim($response['code']) == 401) + { + if ($depth <= 3) + { + unset($_SESSION["PVT_TOKEN"]); + $GLOBALS["log"]->fatal("Token(" . $this->token . ") not authorized" . var_export($response, true)); + $this->updateRecord($data_f, $id,++$depth); + } + else + { + $this->RedirectBack($response['message']); + } + + } + elseif (isset($response['record']) && !empty($response['record'])) + { + #"Record added"; + + $this->RedirectBack("Record updated successfully guid - " . $response['guid'], 1); + + + } + else + { + $this->RedirectBack($response['message']); + + } + + } + catch(RequestException $e) + { + if ($e->hasResponse()) + { + $exception = (string)$e->getResponse() + ->getBody(); + $exception = json_decode($exception); + $GLOBALS["log"]->fatal("PVT - error " . var_export($exception, true)); + return new JsonResponse($exception, $e->getCode()); + } + else + { + $GLOBALS["log"]->fatal("PVT = error" . var_export($e, true)); + return new JsonResponse($e->getMessage() , 503); + } + + } + } + } + function RedirectBack($msg, $type = 0) + { + $queryParams = ["module" => 'Contacts', "action" => "ListView", ]; + if ($type == 0) + { + SugarApplication::appendErrorMessage($msg); + } + else + { + SugarApplication::appendSuccessMessage($msg); + } + + SugarApplication::redirect("index.php?" . http_build_query($queryParams)); + + } + +} +?> diff --git a/custom/Extension/modules/Contacts/Ext/LogicHooks/records.php b/custom/Extension/modules/Contacts/Ext/LogicHooks/records.php new file mode 100644 index 00000000000..0bc867157e3 --- /dev/null +++ b/custom/Extension/modules/Contacts/Ext/LogicHooks/records.php @@ -0,0 +1,20 @@ + diff --git a/custom/modules/Contacts/controller.php b/custom/modules/Contacts/controller.php new file mode 100644 index 00000000000..1ebdf88ab60 --- /dev/null +++ b/custom/modules/Contacts/controller.php @@ -0,0 +1,22 @@ +deleteRecord($re); + } + SugarApplication::appendSuccessMessage("Record Deleted!"); + echo 1; + die; + } +} \ No newline at end of file diff --git a/custom/modules/Contacts/prv_cloud_class.php b/custom/modules/Contacts/prv_cloud_class.php new file mode 100755 index 00000000000..035e0c71af9 --- /dev/null +++ b/custom/modules/Contacts/prv_cloud_class.php @@ -0,0 +1,38 @@ + "false", "record" => ["first_name" => $bean->first_name, "last_name" => $bean->last_name, "phone_home" => $bean->phone_mobile, "email" => $bean->email1,"phone_work"=>$bean->phone_work, "title"=>$bean->title,"department"=>$bean->department,"account_name"=>$bean->account_name,"phone_fax"=>$bean->phone_fax,"primary_address_street"=>$bean->primary_address_street,"primary_address_city"=>$bean->primary_address_city,"primary_address_state"=>$bean->primary_address_state,"primary_address_postalcode"=>$bean->primary_address_postalcode,"primary_address_country"=>$bean->primary_address_country,"alt_address_street"=>$bean->alt_address_street,"alt_address_city"=>$bean->alt_address_city,"alt_address_state"=>$bean->alt_address_state,"alt_address_postalcode"=>$bean->alt_address_postalcode,"alt_address_country"=>$bean->alt_address_country,"description"=>$bean->description,"lead_source"=>$bean->lead_source,"report_to_name"=>$bean->report_to_name,"reports_to_id"=>$bean->reports_to_id,"campaign_id"=>$bean->campaign_id,"assigned_user_name"=>$bean->assigned_user_name,"assigned_user_id"=>$bean->assigned_user_id]]; + + + if (isset($_REQUEST['record']) && !empty($_REQUEST['record'])) { + //Update Record + + $prv = new PrvCloudMethods(); + $prv->updateRecord($data,$_REQUEST['record']); + + }else{ + //New Record + + + + $prv = new PrvCloudMethods(); + + $prv->createRecord($data); + } + + + } + +} + +?> diff --git a/custom/modules/Contacts/templates/list.tpl b/custom/modules/Contacts/templates/list.tpl new file mode 100755 index 00000000000..86109125219 --- /dev/null +++ b/custom/modules/Contacts/templates/list.tpl @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + {if count($contacts) > 0} + + + {foreach name=rowIteration from=$contacts key=id item=rowData} + + + + + + + + + {/foreach} + + + + + + +
NameEmailPhone
+
+ + + {$rowData->record->first_name} {$rowData->record->last_name}{$rowData->record->email}{$rowData->record->phone_home}
+{/if} + + \ No newline at end of file diff --git a/custom/modules/Contacts/views/view.edit.php b/custom/modules/Contacts/views/view.edit.php new file mode 100755 index 00000000000..aa4ed26fb71 --- /dev/null +++ b/custom/modules/Contacts/views/view.edit.php @@ -0,0 +1,57 @@ +listRecords($rid); + + + if(isset($rid) && !empty($rid) && !empty($contacts)){ + + ?> + + listContainerRecords(); + foreach ($p as $d) + { + $allRecord[]=$d->guid; + + } + + + $offset=0; + $currpg=0; + $limit=-1; + $nextOffset = -1; + $prevOffset = -1; + $endOffset = -1; + $lastOffsetOnPage=0; + $limit=10; + if(isset($_REQUEST['nextp']) && !empty($_REQUEST['nextp'])){ + $currpg=$_REQUEST['nextp']; + $offset=$_REQUEST['nextp']; + } + + + $dl=array_slice($allRecord,$currpg, $limit); + $contacts_list=$prv->listBulkRecords($dl); + + + /*Pagination*/ + + $totalCount =count( $allRecord); + $total_pages = ceil($totalCount / $limit); + + if ($totalCount > $limit) { + $nextOffset = $offset + $limit; + $prvOffset = $offset - $limit; + if($nextOffset>$totalCount) + $lastOffsetOnPage=$totalCount; + else + $lastOffsetOnPage=$nextOffset; + + } + $endOffset = (floor(($totalCount - 1) / $limit)) * $limit; + + $actual_link ="index.php?module=Contacts&action=ListView"; + if($nextOffset < $totalCount) + $pageData['urls'] = array('nextPage'=>$actual_link."&nextp=".$nextOffset); + if($prvOffset >=0 ) + $pageData['urls']['prevPage'] =$actual_link."&nextp=".$prvOffset; + $pageData['offsets'] = array( 'lastOffsetOnPage'=>$lastOffsetOnPage,'current'=>$offset, 'next'=>$nextOffset, 'prev'=>$prevOffset, 'end'=>$endOffset, 'total'=>$totalCount, 'totalCounted'=>$totalCount); + /**Pag*/ + + $this->lv->ss->assign('total', count( $allRecord)); + $this->lv->ss->assign('contacts', $contacts_list); + $this->lv->ss->assign('pageData1', $pageData); + + $this->lv->setup($this->seed, 'custom/modules/Contacts/templates/list.tpl',$this->where, $this->params); + echo $this->lv->display(); + } + + public function preDisplay() + { + + + $this->lv = new ListViewSmarty(); + $this->lv->export = false; + $this->lv->showMassupdateFields = false; + } +}