| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- /**
- * System services for connections.
- */
- class BxBaseServiceConnections extends BxDol
- {
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * @page service Service Calls
- * @section bx_system_general System Services
- * @subsection bx_system_general-connections Connections
- * @subsubsection bx_system_general-perform perform
- *
- * @code bx_srv('system', 'perform', [[...]], 'TemplVoteServices'); @endcode
- * @code {{~system:perform:TemplVoteServices[[...]]~}} @endcode
- *
- * Performs Perform an action (add, remove, etc) with connection object.
- * @param $aParams an array with necessary parameters
- *
- * @see BxBaseServiceConnections::servicePerform
- */
- /**
- * @ref bx_system_general-perform "perform"
- * @api @ref bx_system_general-perform "perform"
- */
- public function servicePerform($aParams)
- {
- if(is_string($aParams))
- $aParams = json_decode($aParams, true);
- if(!$aParams['o'] || !$aParams['a'] || !$aParams['iid'] || !$aParams['cid'])
- return ['code' => 1];
- $oConnection = BxDolConnection::getObjectInstance($aParams['o']);
- if(!$oConnection)
- return ['code' => 2];
- $sMethodHas = 'hasQuestionnaire';
- $sMethodIsAnswered = 'isQuestionnaireAnswered';
- if($aParams['a'] == 'add' && method_exists($oConnection, $sMethodHas) && $oConnection->$sMethodHas($aParams['cid']) && method_exists($oConnection, $sMethodIsAnswered) && !$oConnection->$sMethodIsAnswered($aParams['cid'], $aParams['iid'])) {
- $sModule = $oConnection->getModule();
- return [
- 'a' => 'questionnaire',
- 'data' => [
- bx_api_get_block('form', $oConnection->getQuestionnaireForm('add', $aParams['cid'])->getCodeAPI(), ['ext' => [
- 'name' => $sModule,
- 'request' => ['url' => '/api.php?r=' . $sModule . '/get_questionnaire¶ms[]=main¶ms[]=' . $aParams['o'] . '¶ms[]=add¶ms[]=' . $aParams['cid'], 'immutable' => true]
- ]])
- ]
- ];
- }
- $sMethod = 'action' . bx_gen_method_name($aParams['a']);
- if(!method_exists($oConnection, $sMethod))
- return ['code' => 2];
- $aResult = $oConnection->$sMethod($aParams['cid'], $aParams['iid']);
- if($aResult['err'] !== false)
- return ['code' => 3, 'message' => $aResult['msg']];
- $aFlip = ['add' => 'remove', 'remove' => 'add'];
- return [
- 'a' => $aFlip[$aParams['a']],
- 'title' => $this->serviceGetActionTitle($aParams['o'], $aParams['a'], $aParams['iid'], $aParams['cid'], true),
- ];
- }
- public function serviceGetActionTitle($sObject, $sAction, $iInitiatorId, $iContentId, $bFlip = false)
- {
- if(empty($sAction))
- return '';
- $oConnection = BxDolConnection::getObjectInstance($sObject);
- if(!$oConnection || !$oConnection->isProfileContent())
- return '';
- $sMethod = 'get_menu_item_title_by_connection';
- $aContentProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iContentId);
- if(empty($aContentProfileInfo) || !is_array($aContentProfileInfo) || !BxDolRequest::serviceExists($aContentProfileInfo['type'], $sMethod))
- return '';
- $aTitles = bx_srv($aContentProfileInfo['type'], $sMethod, [$sObject, '', $iContentId, $iInitiatorId]);
- if(empty($aTitles) || !is_array($aTitles))
- return '';
- $aFlip = ['add' => 'remove', 'remove' => 'add'];
- if($bFlip)
- $sAction = $aFlip[$sAction];
- return !empty($aTitles[$sAction]) ? _t($aTitles[$sAction]) : '';
- }
- /**
- * get grid with friends connections
- */
- public function serviceConnectionsTable ($iProfileId = 0)
- {
- if(!$iProfileId && bx_get('profile_id') !== false)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- if(!$iProfileId)
- return false;
- if(bx_is_api())
- return bx_srv('system', 'browse_friends', [
- 'profile_id' => $iProfileId
- ], 'TemplServiceProfiles');
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections');
- if(!$oGrid)
- return false;
- $oGrid->setProfile($iProfileId);
- return $oGrid->getCode();
- }
- /**
- * get grid with friends connection requests
- */
- public function serviceConnectionsRequestTable ()
- {
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections_requests');
- if (!$oGrid)
- return false;
- return $oGrid->getCode();
- }
- /**
- * get number of received unconfirmed connections (friend requests)
- * @param $sConnectionsObject connections object to get unconfirmed connections from
- * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
- * @return number
- */
- public function serviceGetUnconfirmedConnectionsNum ($sConnectionsObject, $mixedId = 0)
- {
- $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
- if (!$oConnection)
- return 0;
- if (!$mixedId)
- $mixedId = bx_get_logged_profile_id();
- $i = 0;
- $a = $oConnection->getConnectedInitiators($mixedId, 0); // get received friend requests
- foreach ($a as $iId)
- if (BxDolProfile::getInstance($iId))
- ++$i;
- return $i;
- }
- public function serviceGetLiveUpdatesUnconfirmedConnections($sModule, $sConnectionsObject, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
- {
- $iProfile = bx_get_logged_profile_id();
- $oProfile = BxDolProfile::getInstance($iProfile);
- if(!$oProfile || $oProfile->getModule() != $sModule)
- return false;
- $iCountNew = (int)$this->serviceGetUnconfirmedConnectionsNum($sConnectionsObject, $iProfile);
- if($iCountNew == (int)$iCount)
- return false;
- return array(
- 'count' => $iCountNew, // required
- 'method' => 'bx_menu_show_live_update(oData)', // required
- 'data' => array(
- 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
- 'content' => '{count}'
- )),
- 'mi_parent' => $aMenuItemParent,
- 'mi_child' => $aMenuItemChild
- ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
- );
- }
- /**
- * get number of initiated connections (like "my subscriptions")
- * @param $sConnectionsObject connections object to get connections from
- * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
- * @return number
- */
- public function serviceGetConnectedContentNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
- {
- $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
- if (!$oConnection)
- return 0;
- if (!$mixedId)
- $mixedId = bx_get_logged_profile_id();
- $i = 0;
- $a = $oConnection->getConnectedContent($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
- foreach ($a as $iId)
- if (BxDolProfile::getInstance($iId))
- ++$i;
- return $i;
- }
- /**
- * get number of received connections (like "subscribed me")
- * @param $sConnectionsObject connections object to get connections from
- * @param $mixedId id to get connections for, if omitted then logged-in profile id is used
- * @return number
- */
- public function serviceGetConnectedInitiatorsNum ($sConnectionsObject, $mixedId = 0, $isMutual = false)
- {
- $oConnection = BxDolConnection::getObjectInstance($sConnectionsObject);
- if (!$oConnection)
- return 0;
- if (!$mixedId)
- $mixedId = bx_get_logged_profile_id();
- $i = 0;
- $a = $oConnection->getConnectedInitiators($mixedId, $isMutual, 0, BX_CONNECTIONS_LIST_NO_LIMIT); // get received friend requests
- foreach ($a as $iId)
- if (BxDolProfile::getInstance($iId))
- ++$i;
- return $i;
- }
- /**
- * get grid with subscriptions connections
- */
- public function serviceSubscriptionsTable ($iProfileId = 0)
- {
- if(!$iProfileId && bx_get('profile_id') !== false)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- $oProfile = false;
- if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
- return false;
- $aProfile = $oProfile->getInfo();
- if(empty($aProfile) || !is_array($aProfile))
- return false;
- if(bx_is_api())
- return bx_srv('system', 'browse_subscriptions', [
- 'profile_id' => $iProfileId,
- ], 'TemplServiceProfiles');
- $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
- if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
- return false;
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscriptions');
- if (!$oGrid)
- return false;
- $oGrid->setProfileId($iProfileId);
- $sContent = $oGrid->getCode();
- if(empty($sContent))
- return false;
- return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
- 'name' => 'subscriptions',
- 'content' => $sContent
- ));
- }
- /**
- * get grid with subscribed me connections
- */
- public function serviceSubscribedMeTable ($iProfileId = 0)
- {
- if(!$iProfileId && bx_get('profile_id') !== false)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- $oProfile = false;
- if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
- return false;
- $aProfile = $oProfile->getInfo();
- if(empty($aProfile) || !is_array($aProfile))
- return false;
- if(bx_is_api())
- return bx_srv('system', 'browse_subscribed_me', [
- 'profile_id' => $iProfileId,
- ], 'TemplServiceProfiles');
- $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
- if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
- return false;
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_subscribed_me');
- if(!$oGrid)
- return false;
- $oGrid->setProfileId($iProfileId);
- $sContent = $oGrid->getCode();
- if(empty($sContent))
- return false;
- return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
- 'name' => 'subscribers',
- 'content' => $sContent
- ));
- }
- /**
- * get grid with 'relations' connections
- */
- public function serviceRelationsTable ($iProfileId = 0)
- {
- if(!BxDolRelation::isEnabled())
- return false;
- if(!$iProfileId && bx_get('profile_id') !== false)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- $oProfile = false;
- if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
- return false;
- $aProfile = $oProfile->getInfo();
- if(empty($aProfile) || !is_array($aProfile))
- return false;
- if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableFromProfile($aProfile['type']))
- return false;
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_relations');
- if (!$oGrid)
- return false;
- $oGrid->setProfileId($iProfileId);
- $sContent = $oGrid->getCode();
- if(empty($sContent))
- return false;
- return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
- 'name' => 'relations',
- 'content' => $sContent
- ));
- }
- /**
- * get grid with 'related me' connections
- */
- public function serviceRelatedMeTable ($iProfileId = 0)
- {
- if(!BxDolRelation::isEnabled())
- return false;
- if(!$iProfileId && bx_get('profile_id') !== false)
- $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
- $oProfile = false;
- if(!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
- return false;
- $aProfile = $oProfile->getInfo();
- if(empty($aProfile) || !is_array($aProfile))
- return false;
- if(!BxDolConnection::getObjectInstance('sys_profiles_relations')->isRelationAvailableWithProfile($aProfile['type']))
- return false;
- $oGrid = BxDolGrid::getObjectInstance('sys_grid_related_me');
- if(!$oGrid)
- return false;
- $oGrid->setProfileId($iProfileId);
- $sContent = $oGrid->getCode();
- if(empty($sContent))
- return false;
- return BxDolTemplate::getInstance()->parseHtmlByName('connections_list.html', array(
- 'name' => 'related-me',
- 'content' => $sContent
- ));
- }
- /*
- * Get notification data for Notifications module - action Subscribe.
- */
- public function serviceGetNotificationsPost($aEvent)
- {
- $iOwner = (int)$aEvent['owner_id'];
- $oOwner = BxDolProfile::getInstance($iOwner);
- if(!$oOwner)
- return array();
- $iProfile = (int)$aEvent['object_id'];
- $oProfile = BxDolProfile::getInstance($iProfile);
- if(!$oProfile)
- return array();
- return array(
- 'entry_sample' => '_sys_profile_sample_single',
- 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
- 'entry_caption' => $oProfile->getDisplayName(),
- 'entry_author' => $oProfile->id(),
- 'lang_key' => '_sys_profile_subscription_added',
- );
- }
- /*
- * Get notification data for Notifications module - action Friend.
- */
- public function serviceGetNotificationsPostFriendship($aEvent)
- {
- $iOwner = (int)$aEvent['owner_id'];
- $oOwner = BxDolProfile::getInstance($iOwner);
- if(!$oOwner)
- return array();
- $iProfile = (int)$aEvent['object_id'];
- $oProfile = BxDolProfile::getInstance($iProfile);
- if(!$oProfile)
- return array();
- $sLangKey = '_sys_profile_friendship_added';
- if(isset($aEvent['content']['request']) && (int)$aEvent['content']['request'] == 1)
- $sLangKey = '_sys_profile_friend_request_added';
- return array(
- 'entry_sample' => '_sys_profile_sample_single',
- 'entry_url' => str_replace(BX_DOL_URL_ROOT, '{bx_url_root}', $oOwner->getUrl()),
- 'entry_caption' => $oProfile->getDisplayName(),
- 'entry_author' => $oProfile->id(),
- 'lang_key' => $sLangKey,
- );
- }
- public function serviceAlertResponseConnections($oAlert)
- {
- $sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
- if(!method_exists($this, $sMethod))
- return;
-
- $this->$sMethod($oAlert);
- }
- protected function processSysProfilesFriendsConnectionAdded(&$oAlert)
- {
- if((int)$oAlert->aExtras['mutual'] != 1)
- return;
- $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
- $oConnection->addConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content'], array('alert_extras' => array('silent_mode' => 1)));
- $oConnection->addConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator'], array('alert_extras' => array('silent_mode' => 1)));
- }
- protected function processSysProfilesFriendsConnectionRemoved(&$oAlert)
- {
- if((int)$oAlert->aExtras['mutual'] != 1)
- return;
- $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
- $oConnection->removeConnection((int)$oAlert->aExtras['initiator'], (int)$oAlert->aExtras['content']);
- $oConnection->removeConnection((int)$oAlert->aExtras['content'], (int)$oAlert->aExtras['initiator']);
- }
- }
- /** @} */
|