From c794ca8fb701ecbb0fea1bb95aa94fee0ffe75eb Mon Sep 17 00:00:00 2001 From: Alexander Razumenko Date: Wed, 14 May 2014 18:44:22 +0300 Subject: [PATCH] COGNOS-127 - Added community activities widget activation --- .../connections/ConnectionsHelper.php | 30 +- .../IBMConnections/Api/ActivitiesAPI.php | 29 +- .../IBMConnections/Api/CommunitiesAPI.php | 502 +++++++++++------- .../Connections/ExtAPIConnections.php | 20 +- 4 files changed, 346 insertions(+), 235 deletions(-) diff --git a/SugarModules/connectors/connections/ConnectionsHelper.php b/SugarModules/connectors/connections/ConnectionsHelper.php index 95cc4e7..87e42ef 100755 --- a/SugarModules/connectors/connections/ConnectionsHelper.php +++ b/SugarModules/connectors/connections/ConnectionsHelper.php @@ -1319,7 +1319,6 @@ function getCommunityList($community, $page_number, $search_text) $beans[$key]->activities = $item['activities_list']; - //$arr['members_list'] = $this->getCommunityMemberArray($arr['id']); //$arr['files_list'] = $this->getFilesList($arr['id']); //$arr['activities_list'] = $this->getActivitiesList($arr['id']); @@ -1703,12 +1702,12 @@ public function getDiscussionModal() echo json_encode($content); } - public function getFilesList($communityId, $searchText = "",$page = 1, $limit = 5) + public function getFilesList($communityId, $searchText = "", $page = 1, $limit = 5) { $entries = $this->apiClass->getFilesList($communityId, $searchText, $page, $limit); $returnData = array( 'entries' => array(), - 'total' => $entries['total'] + 'total' => $entries['total'] ); if (!empty($entries)) { @@ -1716,7 +1715,7 @@ public function getFilesList($communityId, $searchText = "",$page = 1, $limit = $arr = array(); $arr['id'] = $entry->getId(); $arr['name'] = $entry->getTitle(); - $arr['community_id'] = $entry->getCommunityId(); + $arr['community_id'] = $entry->getCommunityId(); $author = $entry->getAuthor(); $arr['author_id'] = $author['id']; @@ -1732,9 +1731,9 @@ public function getFilesList($communityId, $searchText = "",$page = 1, $limit = $arr['downloadsCount'] = $entry->getDownloadsCount(); $arr['version'] = $entry->getVersion(); $arr['fileSize'] = $entry->getFileSize(); - $arr['content_type'] = $entry->getMimeType(); + $arr['content_type'] = $entry->getMimeType(); $arr['picture'] = $this->view->getFilePicture($arr['name']); - $arr['community_id'] = $entry->getCommunityId(); + $arr['community_id'] = $entry->getCommunityId(); $returnData['entries'][] = $arr; } @@ -1751,12 +1750,16 @@ public function getActivityCompletion() public function getActivitiesList($communityId, $searchText = '', $page = 1, $limit = 5, $fields) { - $entries = $this->apiClass->getActivitiesList($communityId, $searchText, $page, $limit); + if (!$this->apiClass->isActivitiesActivated($communityId)) { + $this->activateCommunityActivities($communityId); + } + + $entries = $this->apiClass->getActivitiesList($communityId, $searchText, $page, $limit); $returnData = array( 'entries' => array(), - 'total' => $entries['total'] + 'total' => $entries['total'] ); if (!empty($entries)) { @@ -1766,7 +1769,7 @@ public function getActivitiesList($communityId, $searchText = '', $page = 1, $li $arr = array(); $arr['id'] = $entry->getId(); $arr['name'] = $entry->getTitle(); - $arr['community_id'] = $communityId; + $arr['community_id'] = $communityId; $contributor = $entry->getContributor(); $arr['contributor_id'] = $contributor['id']; @@ -1779,7 +1782,7 @@ public function getActivitiesList($communityId, $searchText = '', $page = 1, $li $arr['duedate'] = $entry->getDueDate(); $arr['completed'] = $entry->isCompleted(); - if ( array_intersect(array('commentsCount', 'completion', 'completed_todos', 'total_todos'), $fields)){ + if (array_intersect(array('commentsCount', 'completion', 'completed_todos', 'total_todos'), $fields)) { $this_activity = $this->apiClass->getActivity($entry->getId()); $arr['commentsCount'] = $this_activity->getCommentsCount(); @@ -1864,7 +1867,7 @@ public function getCommunityMemberArray($comm_id, $search_text, $page = 1, $limi $result = array( 'entries' => $this->getMembersArray($response), - 'total' => $feed->getTotalResults(), + 'total' => $feed->getTotalResults(), ); return $result; } @@ -1895,4 +1898,9 @@ public function getCommunityMembers() ); } + public function activateCommunityActivities($id) + { + return $this->apiClass->activateCommunityWidget($id, 'Activities'); + } + } diff --git a/SugarModules/custom/include/IBMConnections/Api/ActivitiesAPI.php b/SugarModules/custom/include/IBMConnections/Api/ActivitiesAPI.php index 51eefcd..cacf5d3 100755 --- a/SugarModules/custom/include/IBMConnections/Api/ActivitiesAPI.php +++ b/SugarModules/custom/include/IBMConnections/Api/ActivitiesAPI.php @@ -39,34 +39,9 @@ class ActivitiesAPI extends AbstractConnectionsAPI * Enter description here ... */ - public function isEnabled($communityId) - { - $feed = $this->getRemoteAppFeed($communityId); - if (empty($feed)) { - return false; - } - $entries = $feed->getEntries(); - for ($i = 0; $i < sizeof($entries); $i++) { - $entry = $entries[$i]; - $retArray = $entry->getCategories("http://www.ibm.com/xmlns/prod/sn/type"); - for ($k = 0; $k < sizeof($retArray); $k++) { - if (strcmp($retArray[$k]['term'], "Activities") == 0) { - $link = $entry->getLink("http://www.ibm.com/xmlns/prod/sn/remote-application/feed"); - $path = $this->extractRelativeUrlPath($link['href']); - } - } - } - return !empty($path); - } - public function listCommunityActivities($communityId, $searchText, $page, $limit) { - if (!$this->isEnabled($communityId)) { - return array('message' => 'widget_is_not_activated'); - } - - $this->getHttpClient()->resetParameters(); $this->getHttpClient()->setParameterGet("commUuid", $communityId); $this->getHttpClient()->setParameterGet("page", $page); @@ -89,7 +64,7 @@ public function listCommunityActivities($communityId, $searchText, $page, $limit $feed = IBMAtomFeed::loadFromString($result->getBody()); $result = array( 'entries' => array(), - 'total' => $feed->getTotalResults(), + 'total' => $feed->getTotalResults(), ); $entries = $feed->getEntries(); foreach ($entries as $entry) { @@ -286,7 +261,7 @@ public function createCommunityActivity($communityId, $title, $content = '', $ta //echo "path1". $path; if (empty($path)) { $commApi = new CommunitiesAPI($this->getHttpClient()); - $commApi->activateActivities($communityId); + $commApi->activateAppWidget($communityId, 'Activities'); $feed = $this->getRemoteAppFeed($communityId); if (empty($feed)) { return; diff --git a/SugarModules/custom/include/IBMConnections/Api/CommunitiesAPI.php b/SugarModules/custom/include/IBMConnections/Api/CommunitiesAPI.php index 5386ab1..a301547 100755 --- a/SugarModules/custom/include/IBMConnections/Api/CommunitiesAPI.php +++ b/SugarModules/custom/include/IBMConnections/Api/CommunitiesAPI.php @@ -14,7 +14,6 @@ /* ***************************************************************** */ - require_once 'custom/include/IBMConnections/Api/AbstractConnectionsAPI.php'; require_once 'custom/include/IBMConnections/Api/ProfilesAPI.php'; require_once 'custom/include/IBMConnections/FeedApi/IBMAtomFeed.php'; @@ -30,7 +29,8 @@ * @author Mario Casciaro * */ -class CommunitiesAPI extends AbstractConnectionsAPI { +class CommunitiesAPI extends AbstractConnectionsAPI +{ /** * Content-Type */ @@ -48,7 +48,8 @@ class CommunitiesAPI extends AbstractConnectionsAPI { * @param unknown_type $httpClient * @param IBMAtomEntry $atomEntry */ - function __construct($httpClient) { + function __construct($httpClient) + { parent::__construct($httpClient); $this->profilesAPI = new ProfilesAPI($httpClient); // $this->LOGGER = SFALogger::getLog('connections'); @@ -59,11 +60,12 @@ function __construct($httpClient) { * @param $communityId * @return status code */ - public function deleteCommunity($communityId) { + public function deleteCommunity($communityId) + { $path = '/communities/service/atom/community/instance?communityUuid=' . $communityId; $response = $this->requestForPath('DELETE', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } if ($response->getStatus() != 200) { @@ -90,24 +92,29 @@ public function deleteCommunity($communityId) { * @param $type * @return mixed */ - public function createCommunity($title, $content, $type, $tags = array(), $logo = '') { + public function createCommunity($title, $content, $type, $tags = array(), $logo = '') + { $entry = IBMEditableAtomEntry::createEmptyEditableEntry(); - $entry - ->addCategory('community', - 'http://www.ibm.com/xmlns/prod/sn/type'); + $entry->addCategory('community', 'http://www.ibm.com/xmlns/prod/sn/type'); $entry->setTitle($title); $entry->setContent($content); // $communityTypeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', 'private' ); - $communityTypeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', $type); + $communityTypeElt = $entry->getDom()->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'communityType', + $type + ); $entry->getEntryNode()->appendChild($communityTypeElt); - foreach($tags as $tag) { + foreach ($tags as $tag) { $tag = trim($tag); - if (!empty($tag)) $entry->addTag($tag); + if (!empty($tag)) { + $entry->addTag($tag); + } } - if (!empty($logo)){ + if (!empty($logo)) { $logoElt = $entry->getDom()->createElement('link'); $logoElt->setAttribute('rel', "http://www.ibm.com/xmlns/prod/sn/logo"); $logoElt->setAttribute('href', $logo); @@ -116,11 +123,15 @@ public function createCommunity($title, $content, $type, $tags = array(), $logo $this->getHttpClient()->setRawData($entry->getDomString()); - $response = $this->requestForPath('POST', + $response = $this->requestForPath( + 'POST', '/communities/service/atom/communities/my', - array('Content-Type' => 'application/atom+xml', - 'Content-Language' => 'en-US',)); - if (empty($response) || !$this->checkResult($response)){ + array( + 'Content-Type' => 'application/atom+xml', + 'Content-Language' => 'en-US', + ) + ); + if (empty($response) || !$this->checkResult($response)) { return; } @@ -130,7 +141,8 @@ public function createCommunity($title, $content, $type, $tags = array(), $logo return $query[1]; } - public function editCommunity($communityId, $title = null, $content = null, $type = null, $tags = array()) { + public function editCommunity($communityId, $title = null, $content = null, $type = null, $tags = array()) + { $entry = IBMEditableAtomEntry::createEmptyEditableEntry(); $entry->addCategory('community', 'http://www.ibm.com/xmlns/prod/sn/type'); @@ -140,48 +152,62 @@ public function editCommunity($communityId, $title = null, $content = null, $typ // $communityTypeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', 'private' ); // $communityTypeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', $type); //$entry->getEntryNode()->appendChild($communityTypeElt); - if (!empty($type)) - { - $typeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', $type); + if (!empty($type)) { + $typeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn', 'communityType', $type); $nodelist = $entry->getDom()->getElementsByTagName('communityType'); - if($nodelist->length > 0){ + if ($nodelist->length > 0) { $oldnode = $nodelist->item(0); $newnode = $entry->getDom()->importNode($typeElt, true); $oldnode->parentNode->replaceChild($newnode, $oldnode); - } - else{ + } else { $entry->getEntryNode()->appendChild($typeElt); } } - foreach($tags as $tag) { + foreach ($tags as $tag) { $tag = trim($tag); - if (!empty($tag)) $entry->addTag($tag); + if (!empty($tag)) { + $entry->addTag($tag); + } } // $client = $this->getHttpClient(); $this->getHttpClient()->setRawData($entry->getDomString()); - $response = $this->requestForPath('PUT', - '/communities/service/atom/community/instance?communityUuid='. $communityId, - array('Content-Type' => 'application/atom+xml', - 'Content-Language' => 'en-US',)); + $response = $this->requestForPath( + 'PUT', + '/communities/service/atom/community/instance?communityUuid=' . $communityId, + array( + 'Content-Type' => 'application/atom+xml', + 'Content-Language' => 'en-US', + ) + ); } - public function createSubcommunity($communityId, $title, $content, $type, $tags = array(), $logo = '') { + + public function createSubcommunity($communityId, $title, $content, $type, $tags = array(), $logo = '') + { $entry = IBMEditableAtomEntry::createEmptyEditableEntry(); $entry - ->addCategory('community', - 'http://www.ibm.com/xmlns/prod/sn/type'); + ->addCategory( + 'community', + 'http://www.ibm.com/xmlns/prod/sn/type' + ); $entry->setTitle($title); $entry->setContent($content); - $communityTypeElt = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn','communityType', $type); + $communityTypeElt = $entry->getDom()->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'communityType', + $type + ); $entry->getEntryNode()->appendChild($communityTypeElt); - foreach($tags as $tag) { + foreach ($tags as $tag) { $tag = trim($tag); - if (!empty($tag)) $entry->addTag($tag); + if (!empty($tag)) { + $entry->addTag($tag); + } } - if (!empty($logo)){ + if (!empty($logo)) { $logoElt = $entry->getDom()->createElement('link'); $logoElt->setAttribute('rel', "http://www.ibm.com/xmlns/prod/sn/logo"); $logoElt->setAttribute('href', $logo); @@ -190,16 +216,23 @@ public function createSubcommunity($communityId, $title, $content, $type, $tags $parentElt = $entry->getDom()->createElement('link'); $parentElt->setAttribute('rel', "http://www.ibm.com/xmlns/prod/sn/parentcommunity"); - $parentElt->setAttribute('href', $this->url . '/communities/service/atom/community/instance?communityUuid='. $communityId); + $parentElt->setAttribute( + 'href', + $this->url . '/communities/service/atom/community/instance?communityUuid=' . $communityId + ); $entry->getEntryNode()->appendChild($parentElt); $this->getHttpClient()->setRawData($entry->getDomString()); - $response = $this->requestForPath('POST', + $response = $this->requestForPath( + 'POST', '/communities/service/atom/communities/my', - array('Content-Type' => 'application/atom+xml', - 'Content-Language' => 'en-US',)); - if (empty($response) || !$this->checkResult($response)){ + array( + 'Content-Type' => 'application/atom+xml', + 'Content-Language' => 'en-US', + ) + ); + if (empty($response) || !$this->checkResult($response)) { return; } @@ -207,13 +240,17 @@ public function createSubcommunity($communityId, $title, $content, $type, $tags $query = explode('communityUuid=', $loc); return $query[1]; } + /** * */ - public function getCommunity($id) { + public function getCommunity($id) + { $remoteAppFeed = $this->getRemoteAppFeed($id); - if (empty($remoteAppFeed)) return "no_connection"; + if (empty($remoteAppFeed)) { + return "no_connection"; + } $this->getHttpClient()->resetParameters(); $this->getHttpClient()->setParameterGet("communityUuid", $id); $result = $this->requestForPath("GET", "/communities/service/atom/community/instance"); @@ -223,29 +260,33 @@ public function getCommunity($id) { $entry = IBMAtomEntry::loadFromString($result->getBody()); - return new ConnectionsCommunity($entry, $remoteAppFeed); } /** * Function to return the communities the user is a member of - * @param string $filterType - filter the communities the user is a member * or an owner of, default is no filtering * @param integer $pageSize - specify the number of communities to return, * default is to return 10 * @return multitype:ConnectionsCommunity */ - public function listMyCommunities($search = null, $filterType = null, $sortBy = null, $sortOrder = null, $pageSize = null, $pageNumber = null) { + public function listMyCommunities( + $search = null, + $filterType = null, + $sortBy = null, + $sortOrder = null, + $pageSize = null, + $pageNumber = null + ) { $this->getHttpClient()->resetParameters(); if ($filterType != null) { $this->getHttpClient()->setParameterGet("filterType", $filterType); } if ($search != null) { $this->getHttpClient()->setParameterGet("search", $search); - } - else { + } else { $sortBy = "lastmod"; } @@ -254,16 +295,16 @@ public function listMyCommunities($search = null, $filterType = null, $sortBy = $this->getHttpClient()->setParameterGet("ps", $pageSize); } - if($pageNumber != null) { + if ($pageNumber != null) { $this->getHttpClient()->setParameterGet("page", $pageNumber); } - if($sortBy != null) { + if ($sortBy != null) { $this->getHttpClient()->setParameterGet("sortField", $sortBy); } - if($sortOrder != null) { - if($sortOrder == "asc") { + if ($sortOrder != null) { + if ($sortOrder == "asc") { $this->getHttpClient()->setParameterGet("asc", "true"); } else { $this->getHttpClient()->setParameterGet("asc", "false"); @@ -271,8 +312,8 @@ public function listMyCommunities($search = null, $filterType = null, $sortBy = } $result = $this->requestForPath("GET", '/communities/service/atom/communities/my'); - if (empty($result) || !$this->checkResult($result)){ - return array('communities' => array(),'metadata' => array()); + if (empty($result) || !$this->checkResult($result)) { + return array('communities' => array(), 'metadata' => array()); } $communities = array(); @@ -284,29 +325,35 @@ public function listMyCommunities($search = null, $filterType = null, $sortBy = $retrievedPageNumber = ceil($feed->getStartIndex() / $feed->getItemsPerPage()); $this->setLastResultMetadata( - new ConnectionsResultMetadata($sortBy, $sortOrder, $feed->getItemsPerPage(), $retrievedPageNumber , $feed->getTotalResults()) + new ConnectionsResultMetadata($sortBy, $sortOrder, $feed->getItemsPerPage( + ), $retrievedPageNumber, $feed->getTotalResults()) ); - return array('communities' => $communities,'metadata' => $this->getLastResultMetadata()); + return array('communities' => $communities, 'metadata' => $this->getLastResultMetadata()); } /** * Function to return all communities - * @param string $filterType - filter the communities the user is a member * or an owner of, default is no filtering * @param integer $pageSize - specify the number of communities to return, * default is to return 10 * @return multitype:ConnectionsCommunity */ - public function listAllCommunities($search = null, $filterType = null, $sortBy = null, $sortOrder = null, $pageSize = null, $pageNumber = null) { + public function listAllCommunities( + $search = null, + $filterType = null, + $sortBy = null, + $sortOrder = null, + $pageSize = null, + $pageNumber = null + ) { $this->getHttpClient()->resetParameters(); if ($filterType != null) { $this->getHttpClient()->setParameterGet("filterType", $filterType); } if ($search != null) { $this->getHttpClient()->setParameterGet("search", $search); - } - else { + } else { $sortBy = "lastmod"; } @@ -314,16 +361,16 @@ public function listAllCommunities($search = null, $filterType = null, $sortBy = $this->getHttpClient()->setParameterGet("ps", $pageSize); } - if($pageNumber != null) { + if ($pageNumber != null) { $this->getHttpClient()->setParameterGet("page", $pageNumber); } - if($sortBy != null) { + if ($sortBy != null) { $this->getHttpClient()->setParameterGet("sortField", $sortBy); } - if($sortOrder != null) { - if($sortOrder == "asc") { + if ($sortOrder != null) { + if ($sortOrder == "asc") { $this->getHttpClient()->setParameterGet("asc", "true"); } else { $this->getHttpClient()->setParameterGet("asc", "false"); @@ -331,8 +378,8 @@ public function listAllCommunities($search = null, $filterType = null, $sortBy = } $result = $this->requestForPath("GET", '/communities/service/atom/communities/all'); - if (empty($result) || !$this->checkResult($result)){ - return array('communities' => array(),'metadata' => array()); + if (empty($result) || !$this->checkResult($result)) { + return array('communities' => array(), 'metadata' => array()); } $communities = array(); @@ -344,20 +391,28 @@ public function listAllCommunities($search = null, $filterType = null, $sortBy = $retrievedPageNumber = ceil($feed->getStartIndex() / $feed->getItemsPerPage()); $this->setLastResultMetadata( - new ConnectionsResultMetadata($sortBy, $sortOrder, $feed->getItemsPerPage(), $retrievedPageNumber , $feed->getTotalResults()) + new ConnectionsResultMetadata($sortBy, $sortOrder, $feed->getItemsPerPage( + ), $retrievedPageNumber, $feed->getTotalResults()) ); - return array('communities' => $communities,'metadata' => $this->getLastResultMetadata()); + return array('communities' => $communities, 'metadata' => $this->getLastResultMetadata()); } - public function getSubCommunities($communityId,$search = null, $filterType = null, $sortBy = null, $sortOrder = null, $pageSize = null, $pageNumber = null) { + public function getSubCommunities( + $communityId, + $search = null, + $filterType = null, + $sortBy = null, + $sortOrder = null, + $pageSize = null, + $pageNumber = null + ) { $this->getHttpClient()->resetParameters(); if ($filterType != null) { $this->getHttpClient()->setParameterGet("filterType", $filterType); } if ($search != null) { $this->getHttpClient()->setParameterGet("search", $search); - } - else { + } else { $sortBy = "lastmod"; } @@ -365,25 +420,28 @@ public function getSubCommunities($communityId,$search = null, $filterType = nul $this->getHttpClient()->setParameterGet("ps", $pageSize); } - if($pageNumber != null) { + if ($pageNumber != null) { $this->getHttpClient()->setParameterGet("page", $pageNumber); } - if($sortBy != null) { + if ($sortBy != null) { $this->getHttpClient()->setParameterGet("sortField", $sortBy); } - if($sortOrder != null) { - if($sortOrder == "asc") { + if ($sortOrder != null) { + if ($sortOrder == "asc") { $this->getHttpClient()->setParameterGet("asc", "true"); } else { $this->getHttpClient()->setParameterGet("asc", "false"); } } - $result = $this->requestForPath("GET", "/communities/service/atom/community/subcommunities?communityUuid={$communityId}"); - if (empty($result) || !$this->checkResult($result)){ - return array('communities' => array(),'metadata' => array()); + $result = $this->requestForPath( + "GET", + "/communities/service/atom/community/subcommunities?communityUuid={$communityId}" + ); + if (empty($result) || !$this->checkResult($result)) { + return array('communities' => array(), 'metadata' => array()); } $communities = array(); @@ -405,26 +463,9 @@ public function getSubCommunities($communityId,$search = null, $filterType = nul * Parmeters: None * Returns: Widget activation response code. */ - public function activateActivities($communityId) { - $this->activateAppWidget($communityId, "Activities"); - } - - /** - * - * Enter description here ... - * @param unknown_type $communityId - */ - public function activateWiki($communityId) { - $this->activateAppWidget($communityId, "Wiki"); - } - - /** - * - * Enter description here ... - * @param unknown_type $communityId - */ - public function activateBlog($communityId) { - $this->activateAppWidget($communityId, "Blog"); + public function activateActivities($communityId) + { + return $this->activateAppWidget($communityId, "Activities"); } /** @@ -432,30 +473,25 @@ public function activateBlog($communityId) { * Description: * Method is responsible to activate the a Connections app widget. */ - protected function activateAppWidget($communityId, $appName) { + public function activateAppWidget($communityId, $appName) + { $client = $this->getHttpClient(true); - $activationHeaders = array('Pragma' => 'WWW-Authenticate=XHR', - 'X-Requested-With' => 'XMLHttpRequest', - 'Content-Type' => 'application/x-www-form-urlencoded'); - $client->setHeaders($activationHeaders); - - $client->setParameterGet("resourceId", $communityId); - $client->setParameterGet("uiLocation", "col2"); - $client->setParameterGet("unhide", "false"); - $client->setParameterGet("widgetDefId", $appName); - - $response = $this - ->requestForPath('POST', - '/communities/addWidget.do?resourceId=' . $communityId); - - /*if ($response->getStatus() != 200) { - throw new IBMConnectionsServiceException( - "Failed to Activate $appName App widget: " - . $response->getStatus(), - CONNECTIONS_SERVICE_GENERIC); - }*/ - return; + $this->getHttpClient()->resetParameters(); + $raw = ''; + $raw .= ''; + $raw .= '' . $appName . ''; + $raw .= ''; + $raw .= '' . $appName . ''; + $raw .= 'col2'; + $raw .= ''; + $client->setRawData($raw); + + $client->setHeaders(array('Content-Type' => 'text/plain')); + $result = $this->requestForPath('POST', '/communities/service/atom/community/widgets?communityUuid=' . $communityId); + + //return array('message' => 'widget_is_not_activated'); + return ($result->getStatus() == 201); } /** @@ -463,7 +499,8 @@ protected function activateAppWidget($communityId, $appName) { * @param $cid * @param array $members - tuple containing email address and isMembershipModifier */ - function addMembers($cid, $members = array()) { + function addMembers($cid, $members = array()) + { global $sugar_config; $path = '/communities/service/atom/community/members?communityUuid=' @@ -471,7 +508,7 @@ function addMembers($cid, $members = array()) { $client = $this->getHttpClient(); $client->resetParameters(); $response = $this->requestForPath('GET', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } @@ -485,7 +522,7 @@ function addMembers($cid, $members = array()) { // trim out Feed - Keep only Entry $entryDom = new DOMDocument('1.0', 'UTF-8'); $entryDom->appendChild($entryDom->importNode($entryElt, true)); - $dom = &$entryDom; + $dom = & $entryDom; $entryElt = $dom->getElementsByTagName('entry')->item(0); // trim out the owner.. @@ -498,7 +535,9 @@ function addMembers($cid, $members = array()) { // trim out role for next iteration $existingRole = $dom ->getElementsByTagNameNS( - 'http://www.ibm.com/xmlns/prod/sn', 'role') + 'http://www.ibm.com/xmlns/prod/sn', + 'role' + ) ->item(0); $entryElt->removeChild($existingRole); @@ -516,14 +555,16 @@ function addMembers($cid, $members = array()) { $userId = $userIdsMap[strtolower($member['email'])]; // Email doesnt map to any valid user ID in connections - if (!$userId) + if (!$userId) { continue; + } // Do not attempt to add the owner again. - if (strcasecmp($ownerEmail, $member['email']) == 0) + if (strcasecmp($ownerEmail, $member['email']) == 0) { continue; + } // $this->LOGGER->info('adding userId == ' . $userId . '. Admin: ' . $member['isMembershipModifiers']); $contributorElt = $dom->createElement('contributor'); @@ -535,19 +576,26 @@ function addMembers($cid, $members = array()) { //Add Profile API to retrieve UserID $uuidElt = $dom - ->createElementNS('http://www.ibm.com/xmlns/prod/sn', - 'snx:userid', $userId); + ->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'snx:userid', + $userId + ); $contributorElt->appendChild($uuidElt); // New role. $roleElt = $dom - ->createElementNS('http://www.ibm.com/xmlns/prod/sn', + ->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', 'snx:role', $member['isMembershipModifiers'] ? 'owner' - : 'member'); + : 'member' + ); $roleElt - ->setAttribute('component', - 'http://www.ibm.com/xmlns/prod/sn/communities'); + ->setAttribute( + 'component', + 'http://www.ibm.com/xmlns/prod/sn/communities' + ); $entryElt->appendChild($roleElt); //Send Post request everytime @@ -555,11 +603,16 @@ function addMembers($cid, $members = array()) { $client->setIgnoreResponseContentLength(); $response = $this - ->requestForPath('POST', $path, - array('X-HTTP-Method-Override' => 'PUT', + ->requestForPath( + 'POST', + $path, + array( + 'X-HTTP-Method-Override' => 'PUT', 'Content-Type' => self::CONTENT_TYPE, - 'Content-Language' => 'en-US',)); - if (empty($response) || !$this->checkResult($response)){ + 'Content-Language' => 'en-US', + ) + ); + if (empty($response) || !$this->checkResult($response)) { return; } switch ($response->getStatus()) { @@ -588,14 +641,15 @@ function addMembers($cid, $members = array()) { * @param $cid * @param array $members - tuple containing email address and isMembershipModifier */ - function addMember($cid, $member) { + function addMember($cid, $member) + { global $sugar_config; $path = '/communities/service/atom/community/members?communityUuid=' . $cid; $client = $this->getHttpClient(); $client->resetParameters(); $response = $this->requestForPath('GET', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } @@ -612,7 +666,7 @@ function addMember($cid, $member) { // trim out Feed - Keep only Entry $entryDom = new DOMDocument('1.0', 'UTF-8'); $entryDom->appendChild($entryDom->importNode($entryElt, true)); - $dom = &$entryDom; + $dom = & $entryDom; $entryElt = $dom->getElementsByTagName('entry')->item(0); // trim out the owner.. @@ -626,7 +680,7 @@ function addMember($cid, $member) { // Retrieve User Ids.. $userIdsMap = $this->profilesAPI->getUserIds(array($member['email'])); - if(!array_key_exists(strtolower($member['email']), $userIdsMap)) { + if (!array_key_exists(strtolower($member['email']), $userIdsMap)) { // $this->LOGGER->warn(__METHOD__ . ' Cannot find member profile: ' . $member['email']); /*require_once 'custom/include/IBMConnections/Exceptions/IBMConnectionsApiException.php'; throw new IBMConnectionsApiException("Email doesn't map to any valid user ID in Connections", @@ -643,8 +697,9 @@ function addMember($cid, $member) { } // Do not attempt to add the owner again. - if (strcasecmp($ownerEmail, $member['email']) == 0) + if (strcasecmp($ownerEmail, $member['email']) == 0) { return; + } //$this->LOGGER->info(__METHOD__ . 'adding userId == ' . $userId . '. Admin: ' . $member['isMembershipModifiers']); $contributorElt = $dom->createElement('contributor'); @@ -658,8 +713,11 @@ function addMember($cid, $member) { $contributorElt->appendChild($uuidElt); // New role. - $roleElt = $dom->createElementNS('http://www.ibm.com/xmlns/prod/sn', 'snx:role', - $member['isMembershipModifiers'] ? 'owner' : 'member'); + $roleElt = $dom->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'snx:role', + $member['isMembershipModifiers'] ? 'owner' : 'member' + ); $roleElt->setAttribute('component', 'http://www.ibm.com/xmlns/prod/sn/communities'); $entryElt->appendChild($roleElt); @@ -667,13 +725,16 @@ function addMember($cid, $member) { $client->setRawData($dom->saveXML()); $client->setIgnoreResponseContentLength(); - $response = $this->requestForPath('POST', $path, - array('X-HTTP-Method-Override' => 'PUT', + $response = $this->requestForPath( + 'POST', + $path, + array( + 'X-HTTP-Method-Override' => 'PUT', 'Content-Type' => self::CONTENT_TYPE, 'Content-Language' => 'en-US', ) ); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } @@ -711,17 +772,20 @@ public function AddMemberById($member_id, $community_id, $role) $elt->appendChild($uuidElt); $entry->getEntryNode()->appendChild($elt); - $roleEl = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn', 'snx:role',$role); - $roleEl->setAttribute('component','http://www.ibm.com/xmlns/prod/sn/communities'); + $roleEl = $entry->getDom()->createElementNS('http://www.ibm.com/xmlns/prod/sn', 'snx:role', $role); + $roleEl->setAttribute('component', 'http://www.ibm.com/xmlns/prod/sn/communities'); $entry->getEntryNode()->appendChild($roleEl); $this->getHttpClient()->resetParameters(); $this->getHttpClient()->setRawData($entry->getDomString()); - $result = $this->requestForPath('POST', "communities/service/atom/community/members?communityUuid={$community_id}", + $result = $this->requestForPath( + 'POST', + "communities/service/atom/community/members?communityUuid={$community_id}", array( 'Content-Type' => 'application/atom+xml', 'Content-Language' => 'en-US', - )); + ) + ); } @@ -731,8 +795,9 @@ public function AddMemberById($member_id, $community_id, $role) * @param $email * @throws IBMConnectionsApiException */ - public function removeMember($cid, $email) { - $path = '/communities/service/atom/community/members?communityUuid='. $cid . '&email=' . $email; + public function removeMember($cid, $email) + { + $path = '/communities/service/atom/community/members?communityUuid=' . $cid . '&email=' . $email; $response = $this->requestForPath('DELETE', $path); switch ($response->getStatus()) { case 200: //Member was successfully removed with wrong error code @@ -752,10 +817,11 @@ public function removeMember($cid, $email) { } } - public function removeMemberById( $id, $cid) { - $path = '/communities/service/atom/community/members?communityUuid='. $cid . '&userid=' . $id; + public function removeMemberById($id, $cid) + { + $path = '/communities/service/atom/community/members?communityUuid=' . $cid . '&userid=' . $id; $response = $this->requestForPath('DELETE', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } switch ($response->getStatus()) { @@ -785,12 +851,13 @@ public function removeMemberById( $id, $cid) { * that will be removed from the Community * @throws IBMConnectionsApiException */ - public function removeMembers($cid, $emails = array()) { + public function removeMembers($cid, $emails = array()) + { foreach ($emails as $email) { $path = '/communities/service/atom/community/members?communityUuid=' . $cid . '&email=' . $email; $response = $this->requestForPath('DELETE', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } switch ($response->getStatus()) { @@ -823,16 +890,17 @@ public function removeMembers($cid, $emails = array()) { * FALSE: File is already been shared * @throws IBMConnectionsApiException */ - public function shareMyFileWithCommunity($cid, $myFileId) { - $path = '/communities/service/atom/community/instance?communityUuid='. $cid; + public function shareMyFileWithCommunity($cid, $myFileId) + { + $path = '/communities/service/atom/community/instance?communityUuid=' . $cid; $response = $this->requestForPath('GET', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } - if($response->getStatus() == 404) { + if ($response->getStatus() == 404) { // No access to the community - $message = 'Received HTTP code 404 while attempting to share File ['. $myFileId .'] with community [' . $cid .'].' - .' The community appears to have been deleted.'; + $message = 'Received HTTP code 404 while attempting to share File [' . $myFileId . '] with community [' . $cid . '].' + . ' The community appears to have been deleted.'; /*require_once 'custom/include/IBMConnections/Exceptions/IBMConnectionsApiException.php'; $connEx = new IBMConnectionsApiException($message, ERR_COMMUNITIES_SERVICE_NO_MAPPED_COMMUNITY, $response); @@ -864,9 +932,14 @@ public function shareMyFileWithCommunity($cid, $myFileId) { $client->setRawData($dom->saveXML()); //error_log( $dom->saveXML() ); $response = $this - ->requestForPath('POST', '/files/basic/api/myuserlibrary/document/' . $myFileId . '/feed', - array('Content-Type' => self::CONTENT_TYPE, - 'Content-Language' => 'en-US')); + ->requestForPath( + 'POST', + '/files/basic/api/myuserlibrary/document/' . $myFileId . '/feed', + array( + 'Content-Type' => self::CONTENT_TYPE, + 'Content-Language' => 'en-US' + ) + ); //error_log( $response->getBody() ); //echo $response->getStatus(); @@ -905,18 +978,23 @@ public function shareMyFileWithCommunity($cid, $myFileId) { * @param $cid * @param $myFileId */ - public function unshareMyFileFromCommunity($cid, $myFileId) { + public function unshareMyFileFromCommunity($cid, $myFileId) + { $nonce = $this->requestNonce(); $client = $this->getHttpClient(); $client->setParameterPost('itemId', $myFileId); $response = $this - ->requestForPath('POST', + ->requestForPath( + 'POST', '/files/basic/api/communitycollection/' . $cid . '/feed', - array('Content-Type' => self::CONTENT_TYPE, + array( + 'Content-Type' => self::CONTENT_TYPE, 'Content-Language' => 'en-US', 'X-Method-Override' => 'DELETE', - 'X-Update-Nonce' => $nonce)); + 'X-Update-Nonce' => $nonce + ) + ); //print_r( $response->getBody() ); //echo $response->getStatus(); @@ -946,14 +1024,14 @@ public function unshareMyFileFromCommunity($cid, $myFileId) { } - /** * Method to update a member's community membership type (using email addresses) * @param $cid * @param $typeMembership ( 'owner' or 'member' ) * @param $emails */ - public function updateCommunityMembership($cid, $typeMembership, $email) { + public function updateCommunityMembership($cid, $typeMembership, $email) + { $myUserId = $this->profilesAPI->getUserId($email); $dom = new DOMDocument('1.0', 'UTF-8'); $entryElt = $dom->createElementNS(self::ATOM_NS, 'entry'); @@ -965,11 +1043,16 @@ public function updateCommunityMembership($cid, $typeMembership, $email) { $client->setParameterGet('userid', $myUserId); $roleElt = $dom - ->createElementNS('http://www.ibm.com/xmlns/prod/sn', - 'snx:role', $typeMembership); + ->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'snx:role', + $typeMembership + ); $roleElt - ->setAttribute('component', - 'http://www.ibm.com/xmlns/prod/sn/communities'); + ->setAttribute( + 'component', + 'http://www.ibm.com/xmlns/prod/sn/communities' + ); $entryElt->appendChild($roleElt); $contributorElt = $dom->createElement('contributor'); @@ -977,19 +1060,26 @@ public function updateCommunityMembership($cid, $typeMembership, $email) { //Add Profile API to retrieve UserID $uuidElt = $dom - ->createElementNS('http://www.ibm.com/xmlns/prod/sn', - 'snx:userid', $myUserId); + ->createElementNS( + 'http://www.ibm.com/xmlns/prod/sn', + 'snx:userid', + $myUserId + ); $contributorElt->appendChild($uuidElt); //Send Post request everytime $client->setRawData($dom->saveXML()); $client->setIgnoreResponseContentLength(); $response = $this - ->requestForPath('PUT', $path, - array('Content-Type' => self::CONTENT_TYPE, - - 'Content-Language' => 'en-US')); - if (empty($response) || !$this->checkResult($response)){ + ->requestForPath( + 'PUT', + $path, + array( + 'Content-Type' => self::CONTENT_TYPE, + 'Content-Language' => 'en-US' + ) + ); + if (empty($response) || !$this->checkResult($response)) { return; } switch ($response->getStatus()) { @@ -1021,21 +1111,22 @@ public function updateCommunityMembership($cid, $typeMembership, $email) { * @return array( array( 'emailowner1' => 'owner', 'emailmember2' => 'member')); * @return array( array( 'id1' => 'owner', 'id2' => 'member')); */ - public function listMembers($cid, $page = 1, $sortField = 'created', $asc = 'true') { + public function listMembers($cid, $page = 1, $sortField = 'created', $asc = 'true') + { $result = array(); // Retrieve Members Email for this community.. $path = '/communities/service/atom/community/members?communityUuid=' . $cid; $this->httpClient = $this->getHttpClient(); - if ( $sortField == 'created' || $sortField == 'name'){ - $this->httpClient->setParameterGet("sortField", $communityId); + if ($sortField == 'created' || $sortField == 'name') { + $this->httpClient->setParameterGet("sortField", $sortField); } $this->httpClient->setParameterGet("page", $page); $this->httpClient->setParameterGet("ps", 32); $this->httpClient->setParameterGet("asc", $asc); $response = $this->requestForPath('GET', $path); - if (empty($response) || !$this->checkResult($response)){ + if (empty($response) || !$this->checkResult($response)) { return; } $dom = new DOMDocument(); @@ -1076,7 +1167,7 @@ public function listMembers($cid, $page = 1, $sortField = 'created', $asc = 'tru public function getActivityCount($communityId) { - return $this->getCount("/activities/service/atom2/everything?commUuid=". $communityId .'&completed=yes'); + return $this->getCount("/activities/service/atom2/everything?commUuid=" . $communityId . '&completed=yes'); } public function getFileCount($communityId) @@ -1101,24 +1192,45 @@ public function getUpdateCount($communityId) public function getWikiCount($communityId) { - return $this->getCount("/wikis/basic/api/communitywiki/". $communityId."/feed"); + return $this->getCount("/wikis/basic/api/communitywiki/" . $communityId . "/feed"); } + //// public function getBlogCount($communityId) { - return $this->getCount("/blogs/". $communityId."/feed"); + return $this->getCount("/blogs/" . $communityId . "/feed"); } + private function getCount($path) { $this->getHttpClient()->resetParameters(); $result = $this->requestForPath("GET", $path); - try{ + try { $feed = IBMAtomFeed::loadFromString($result->getBody()); - return $feed->getTotalResults(); - } - catch (Exception $c){ + return $feed->getTotalResults(); + } catch (Exception $c) { return 0; } } + public function isWidgetActivated($communityId, $widget) + { + $feed = $this->getRemoteAppFeed($communityId); + if (empty($feed)) { + return false; + } + $entries = $feed->getEntries(); + for ($i = 0; $i < sizeof($entries); $i++) { + $entry = $entries[$i]; + $retArray = $entry->getCategories("http://www.ibm.com/xmlns/prod/sn/type"); + for ($k = 0; $k < sizeof($retArray); $k++) { + if (strcmp($retArray[$k]['term'], $widget) == 0) { + $link = $entry->getLink("http://www.ibm.com/xmlns/prod/sn/remote-application/feed"); + $path = $this->extractRelativeUrlPath($link['href']); + } + } + } + return !empty($path); + } + } diff --git a/SugarModules/custom/include/externalAPI/Connections/ExtAPIConnections.php b/SugarModules/custom/include/externalAPI/Connections/ExtAPIConnections.php index 7e2f757..d8f0e06 100755 --- a/SugarModules/custom/include/externalAPI/Connections/ExtAPIConnections.php +++ b/SugarModules/custom/include/externalAPI/Connections/ExtAPIConnections.php @@ -666,8 +666,14 @@ public function getFilesList($communityId, $searchText, $page, $limit) return $this->api->getFilesAPI()->getCommunityFiles($communityId, $searchText, $page, $limit); } - public function getMembers($communityId = "", $searchText = "", $page = 1, $pageSize = 5, $sortBy = 'name', $asc = true) - { + public function getMembers( + $communityId = "", + $searchText = "", + $page = 1, + $pageSize = 5, + $sortBy = 'name', + $asc = true + ) { $searchText = urlencode($searchText); if (!empty($searchText) && empty($communityId)) { $reply = $this->makeRequest("profiles/atom/search.do?search={$searchText}&page={$page}&ps={$pageSize}"); @@ -831,4 +837,14 @@ public function getProfile($id) return $this->api->getProfilesAPI()->getProfile($id); } + public function isActivitiesActivated($id) + { + return $this->api->getCommunitiesAPI()->isWidgetActivated($id, 'Activities'); + } + + public function activateCommunityWidget($id, $widgetType) + { + return $this->api->getCommunitiesAPI()->activateAppWidget($id, $widgetType); + } + }