From d1fa92a0d7a5fe3aa67a121a9118500c0cb58925 Mon Sep 17 00:00:00 2001 From: Gyula Madarasz Date: Fri, 7 Apr 2017 15:28:50 +0100 Subject: [PATCH 1/3] multiple portal account support --- site/controller.php | 34 ++++++++++++++++--- site/models/SugarCasesConnection.php | 51 +++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/site/controller.php b/site/controller.php index a0f3302..5578e19 100755 --- a/site/controller.php +++ b/site/controller.php @@ -165,7 +165,11 @@ function create() { if(isset($_REQUEST['sug']) && $_REQUEST['sug'] != ''){ - $contacts = $restClient->getEntry('Contacts',$_REQUEST['sug'],array('name','email1')); + $module = $this->getRequestModule(); + + $sug = $this->getRequestSugarId(); + + $contacts = $restClient->getEntry($module, $sug,array('name','email1')); if(!empty($contacts['entry_list'])){ $contact = $contacts['entry_list'][0]['name_value_list']; @@ -214,7 +218,7 @@ function create() { $user->set('tmp_user', true); } - $restClient->setEntry('Contacts',array('id'=>$_REQUEST['sug'], 'joomla_account_id' => $user->id,'joomla_account_access' => $pass, )); + $restClient->setEntry($module, array('id'=>$sug, 'joomla_account_id' => $user->id,'joomla_account_access' => $pass, )); echo json_encode(array("success"=>true)); } }else{ @@ -231,7 +235,11 @@ function update_e(){ if(isset($_REQUEST['sug']) && $_REQUEST['sug'] != ''){ - $contacts = $restClient->getEntry('Contacts',$_REQUEST['sug'],array('name','email1','joomla_account_id')); + $module = $this->getRequestModule(); + + $sug = $this->getRequestSugarId(); + + $contacts = $restClient->getEntry($module, $sug,array('name','email1','joomla_account_id')); if(!empty($contacts['entry_list'])){ $contact = $contacts['entry_list'][0]['name_value_list']; @@ -273,7 +281,12 @@ private function setUserDisabled($disable){ include_once 'components/com_advancedopenportal/sugarRestClient.php'; $restClient = new sugarRestClient(); $restClient->login(); - $contacts = $restClient->getEntry('Contacts',$_REQUEST['sug'],array('joomla_account_id')); + + $module = $this->getRequestModule(); + + $sug = $this->getRequestSugarId(); + + $contacts = $restClient->getEntry($module, $sug,array('joomla_account_id')); if(!empty($contacts['entry_list'])){ $contact = $contacts['entry_list'][0]['name_value_list']; $userId = (int) $_REQUEST['uid']; @@ -292,5 +305,18 @@ private function setUserDisabled($disable){ JFactory::getApplication()->close(); } + private function getRequestModule() { + $module = 'Contacts'; + if(isset($_REQUEST['m'])) { + $module = $_REQUEST['m']; + } + return $module; + } + + private function getRequestSugarId() { + $split = explode('::', $_REQUEST['sug']); + $sug = $split[0]; + return $sug; + } } diff --git a/site/models/SugarCasesConnection.php b/site/models/SugarCasesConnection.php index deea130..a2e5566 100755 --- a/site/models/SugarCasesConnection.php +++ b/site/models/SugarCasesConnection.php @@ -89,6 +89,12 @@ public function getCaseStatusDisplay($status){ public function addFiles($caseId, $caseUpdateId, $contactId, $files){ $results = array(); + + $split = explode('::', $contactId); + if(count($split) > 1) { + $contactId = $split[1]; + } + //For every file, create a new note. Add an attachment and link the note to the foreach($files as $file_name => $file_location){ $note_data = array( @@ -99,6 +105,7 @@ public function addFiles($caseId, $caseUpdateId, $contactId, $files){ $new_note = $this->restClient->setEntry('Notes',$note_data); $note_id = $new_note['id']; $this->restClient->set_note_attachment($note_id, $file_name, $file_location); + $this->restClient->setRelationship("Notes",$note_id,"contact",$contactId); $results[] = array('id'=>$note_id,'file_name'=>$file_name); } @@ -107,6 +114,12 @@ public function addFiles($caseId, $caseUpdateId, $contactId, $files){ public function newCase($contact_id,$subject, $description,$type,$priority,$files){ + $split = explode('::', $contact_id); + $contact_id = $split[0]; + if(count($split) > 1) { + $contact_id = $split[1]; + } + $data = array("contact_id"=>$contact_id, "contact_created_by_id"=>$contact_id, "name" => $subject, @@ -137,6 +150,12 @@ public function newCase($contact_id,$subject, $description,$type,$priority,$file public function postUpdate($case_id,$update_text, $contact_id){ $data = array(); //TODO: Add validation that this user can update this case. + + $split = explode('::', $contact_id); + if(count($split) > 1) { + $contact_id = $split[1]; + } + $data['name'] = $update_text; $data['description'] = $update_text; $data['contact_id'] = $contact_id; @@ -147,6 +166,12 @@ public function postUpdate($case_id,$update_text, $contact_id){ } public function getUpdate($update_id){ + + $split = explode('::', $this->case_update_fields['contact_id']); + if(count($split) > 1) { + $this->case_update_fields['contact_id'] = $split[1]; + } + $sugarupdate = $this->restClient->getEntry("AOP_Case_Updates",$update_id,$this->case_update_fields, array( array('name'=>'contact', @@ -252,20 +277,32 @@ public function getCase($case_id,$contact_id){ } public function getContact($contactId){ - $sugarcontact = $this->restClient->getEntry("Contacts",$contactId,$this->contact_fields); + + $split = explode('::', $contactId); + if(count($split) > 1) { + $contactId = $split[1]; + } + + $sugarcontact = $this->restClient->getEntry('Contacts', $contactId,$this->contact_fields); $contact = new SugarUpdate($sugarcontact['entry_list'][0],$sugarcontact['relationship_list'][0]); return $contact; } public function getCases($contact_id){ - $contact = $this->getContact($contact_id); + $split = explode('::', $contact_id); + $aid = $cid = $split[0]; + if(count($split) > 1) { + $cid = $split[1]; + } + $contact = $this->getContact($aid); switch($contact->portal_user_type){ case 'Account': + $contact = $this->getContact($cid); $cases = $this->fromSugarCases($this->restClient->getRelationships('Accounts', $contact->account_id,'cases','',$this->case_fields)); break; case 'Single': default: - $cases = $this->fromSugarCases($this->restClient->getRelationships('Contacts', $contact_id,'cases','',$this->case_fields)); + $cases = $this->fromSugarCases($this->restClient->getRelationships('Contacts', $cid,'cases','',$this->case_fields)); break; } return $cases; @@ -303,8 +340,14 @@ private function getContactData($sugarId,$user){ } public function updateOrCreateContact($sugarId,$user){ + + $split = explode('::', $sugarId); + if(count($split) > 1) { + $sugarId = $split[1]; + } + $contactData = $this->getContactData($sugarId, $user); - $res = $this->restClient->setEntry('Contacts',$contactData); + $res = $this->restClient->setEntry('Contacts', $contactData); return $res; } From f5476c465362694c785dfe2776dd137bf01dd217 Mon Sep 17 00:00:00 2001 From: Gyula Madarasz Date: Fri, 7 Apr 2017 16:47:12 +0100 Subject: [PATCH 2/3] code refact --- site/models/SugarCasesConnection.php | 53 +++++++++++----------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/site/models/SugarCasesConnection.php b/site/models/SugarCasesConnection.php index a2e5566..0763735 100755 --- a/site/models/SugarCasesConnection.php +++ b/site/models/SugarCasesConnection.php @@ -90,10 +90,7 @@ public function getCaseStatusDisplay($status){ public function addFiles($caseId, $caseUpdateId, $contactId, $files){ $results = array(); - $split = explode('::', $contactId); - if(count($split) > 1) { - $contactId = $split[1]; - } + self::resolveAccountContactId($contactId); //For every file, create a new note. Add an attachment and link the note to the foreach($files as $file_name => $file_location){ @@ -114,11 +111,7 @@ public function addFiles($caseId, $caseUpdateId, $contactId, $files){ public function newCase($contact_id,$subject, $description,$type,$priority,$files){ - $split = explode('::', $contact_id); - $contact_id = $split[0]; - if(count($split) > 1) { - $contact_id = $split[1]; - } + self::resolveAccountContactId($contact_id); $data = array("contact_id"=>$contact_id, "contact_created_by_id"=>$contact_id, @@ -151,10 +144,7 @@ public function postUpdate($case_id,$update_text, $contact_id){ $data = array(); //TODO: Add validation that this user can update this case. - $split = explode('::', $contact_id); - if(count($split) > 1) { - $contact_id = $split[1]; - } + self::resolveAccountContactId($contact_id); $data['name'] = $update_text; $data['description'] = $update_text; @@ -167,10 +157,7 @@ public function postUpdate($case_id,$update_text, $contact_id){ public function getUpdate($update_id){ - $split = explode('::', $this->case_update_fields['contact_id']); - if(count($split) > 1) { - $this->case_update_fields['contact_id'] = $split[1]; - } + self::resolveAccountContactId($this->case_update_fields['contact_id']); $sugarupdate = $this->restClient->getEntry("AOP_Case_Updates",$update_id,$this->case_update_fields, array( @@ -278,10 +265,7 @@ public function getCase($case_id,$contact_id){ public function getContact($contactId){ - $split = explode('::', $contactId); - if(count($split) > 1) { - $contactId = $split[1]; - } + self::resolveAccountContactId($contactId); $sugarcontact = $this->restClient->getEntry('Contacts', $contactId,$this->contact_fields); $contact = new SugarUpdate($sugarcontact['entry_list'][0],$sugarcontact['relationship_list'][0]); @@ -289,22 +273,19 @@ public function getContact($contactId){ } public function getCases($contact_id){ - $split = explode('::', $contact_id); - $aid = $cid = $split[0]; - if(count($split) > 1) { - $cid = $split[1]; - } - $contact = $this->getContact($aid); + + $contact = $this->getContact(self::resolveAccountContactId($contact_id)); switch($contact->portal_user_type){ case 'Account': - $contact = $this->getContact($cid); + $contact = $this->getContact($contact_id); $cases = $this->fromSugarCases($this->restClient->getRelationships('Accounts', $contact->account_id,'cases','',$this->case_fields)); break; case 'Single': default: - $cases = $this->fromSugarCases($this->restClient->getRelationships('Contacts', $cid,'cases','',$this->case_fields)); + $cases = $this->fromSugarCases($this->restClient->getRelationships('Contacts', $contact_id, 'cases','',$this->case_fields)); break; } + return $cases; } @@ -341,14 +322,20 @@ private function getContactData($sugarId,$user){ public function updateOrCreateContact($sugarId,$user){ - $split = explode('::', $sugarId); - if(count($split) > 1) { - $sugarId = $split[1]; - } + self::resolveAccountContactId($sugarId); $contactData = $this->getContactData($sugarId, $user); $res = $this->restClient->setEntry('Contacts', $contactData); return $res; } + protected static function resolveAccountContactId(&$contactId) { + $split = explode('::', $contactId); + $accountId = $contactId = $split[0]; + if(count($split) > 1) { + $contactId = $split[1]; + } + return $accountId; + } + } \ No newline at end of file From a4dd06bc6660f7f67399c5a811f069aae77da7c6 Mon Sep 17 00:00:00 2001 From: Gyula Madarasz Date: Tue, 11 Apr 2017 12:03:53 +0100 Subject: [PATCH 3/3] creator portal url --- site/models/SugarCasesConnection.php | 1 + 1 file changed, 1 insertion(+) diff --git a/site/models/SugarCasesConnection.php b/site/models/SugarCasesConnection.php index 0763735..74d2e04 100755 --- a/site/models/SugarCasesConnection.php +++ b/site/models/SugarCasesConnection.php @@ -121,6 +121,7 @@ public function newCase($contact_id,$subject, $description,$type,$priority,$file "type" => $type, "priority" => $priority, 'update_date_entered' => true, + 'aop_creator_portal' => JUri::base(), ); //TODO: Check call results //Create the actual case.