BxDolConnection.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Default limit for connections list in Counter.
  11. */
  12. define('BX_CONNECTIONS_LIST_COUNTER', 5);
  13. /**
  14. * Default limit for connections lists
  15. */
  16. define('BX_CONNECTIONS_LIST_LIMIT', 1000);
  17. /**
  18. * No limit for connections lists.
  19. * Is needed for Total Number calculation.
  20. */
  21. define('BX_CONNECTIONS_LIST_NO_LIMIT', -1);
  22. /**
  23. * Connections order: no order
  24. */
  25. define('BX_CONNECTIONS_ORDER_NONE', 0);
  26. /**
  27. * Connections order: by addded time, asceding
  28. */
  29. define('BX_CONNECTIONS_ORDER_ADDED_ASC', 1);
  30. /**
  31. * Connections order: by addded time, desceding
  32. */
  33. define('BX_CONNECTIONS_ORDER_ADDED_DESC', 2);
  34. /**
  35. * Connection type: one-way
  36. */
  37. define('BX_CONNECTIONS_TYPE_ONE_WAY', 'one-way');
  38. /**
  39. * Connection type: mutual
  40. */
  41. define('BX_CONNECTIONS_TYPE_MUTUAL', 'mutual');
  42. /**
  43. * Connections content type: content
  44. */
  45. define('BX_CONNECTIONS_CONTENT_TYPE_CONTENT', 'content');
  46. /**
  47. * Connections content type: initiators
  48. */
  49. define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  50. /**
  51. * Connections content type: common
  52. */
  53. define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
  54. /**
  55. * Connections trigger type: initiator.
  56. * It updates 'Initiator' data with a counter's value of connected 'Content'.
  57. */
  58. define('BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR', 'initiator');
  59. /**
  60. * Connections trigger type: content.
  61. * It updates 'Content' data with a counter's value of connected 'Initiators'.
  62. */
  63. define('BX_CONNECTIONS_TRIGGER_TYPE_CONTENT', 'content');
  64. /**
  65. * Connection is usefull when you need to organize some sorts of connections between different content,
  66. * for example: friends, contacts, favorites, block lists, subscriptions, etc.
  67. *
  68. * Two types of connections are supported one way connections (block list, favourites) and mutual (friends).
  69. *
  70. * For automatic handling of connections (like, add/remove connection in frontend) refer to JS function: @see bx_conn_action()
  71. *
  72. * @section connection_create Creating the Connection object:
  73. *
  74. * Step 1:
  75. * Add record to 'sys_objects_connection' table:
  76. * - object: name of the connection object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  77. * for example: bx_blogs_favorites - favorite blogs for users in blogs module.
  78. * - table: table name with connections, see step 2
  79. * - type: 'one-way' or 'mutual'
  80. * - override_class_name: user defined class name which is derived from one of base classes.
  81. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  82. *
  83. * Step 2:
  84. * Create table for connections:
  85. * @code
  86. * CREATE TABLE `my_sample_connections` (
  87. * `id` int(11) NOT NULL AUTO_INCREMENT,
  88. * `initiator` int(11) NOT NULL,
  89. * `content` int(11) NOT NULL,
  90. * `mutual` tinyint(4) NOT NULL, -- can re removed for one-way connections
  91. * `added` int(10) unsigned NOT NULL,
  92. * PRIMARY KEY (`id`),
  93. * UNIQUE KEY `initiator` (`initiator`,`content`),
  94. * KEY `content` (`content`)
  95. * )
  96. * @endcode
  97. *
  98. *
  99. * @section example Example of usage
  100. *
  101. * Check if two profiles are friends:
  102. * @code
  103. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  104. * if ($oConnectionFriends) // check if connections is available for using
  105. * echo $oConnectionFriends->isConnected (100, 200, true) ? "100 and 200 are friends" : "100 and 200 aren't friends"; // check if profiles with IDs 100 and 200 have mutual connections
  106. * @endcode
  107. *
  108. * Get mutual content IDs (friends IDs)
  109. * @code
  110. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  111. * if ($oConnectionFriends) // check if connections is available for using
  112. * print_r($oConnection->getConnectedContent(100, 1)); // print array of friends IDs of 100's profile
  113. * @endcode
  114. *
  115. */
  116. class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
  117. {
  118. protected $_oQuery;
  119. protected $_sObject;
  120. protected $_aObject;
  121. protected $_iInitiator;
  122. protected $_iContent;
  123. protected $_sType;
  124. protected $_bMutual;
  125. protected $_aTriggerTypes;
  126. /**
  127. * Constructor
  128. * @param $aObject array of connection options
  129. */
  130. protected function __construct($aObject)
  131. {
  132. parent::__construct();
  133. $this->_sObject = $aObject['object'];
  134. $this->_aObject = $aObject;
  135. $this->_aObject['per_page_default'] = 20;
  136. $this->_iInitiator = bx_get_logged_profile_id();
  137. $this->_sType = $aObject['type'];
  138. $this->_bMutual = $this->_sType == BX_CONNECTIONS_TYPE_MUTUAL;
  139. $this->_aTriggerTypes = [BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR, BX_CONNECTIONS_TRIGGER_TYPE_CONTENT];
  140. $this->_oQuery = new BxDolConnectionQuery($aObject);
  141. }
  142. /**
  143. * Get connection object instance by object name
  144. * @param $sObject object name
  145. * @return object instance or false on error
  146. */
  147. static public function getObjectInstance($sObject)
  148. {
  149. if (!$sObject)
  150. return false;
  151. if (isset($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject]))
  152. return $GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject];
  153. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  154. if (!$aObject || !is_array($aObject))
  155. return false;
  156. $sClass = empty($aObject['override_class_name']) ? 'BxTemplConnection' : $aObject['override_class_name'];
  157. if (!empty($aObject['override_class_file']))
  158. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  159. $o = new $sClass($aObject);
  160. $o->init();
  161. return ($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject] = $o);
  162. }
  163. /**
  164. * Init something here if it's needed.
  165. */
  166. public function init()
  167. {
  168. }
  169. /**
  170. * Get connection type.
  171. * return BX_CONNECTIONS_TYPE_ONE_WAY or BX_CONNECTIONS_TYPE_MUTUAL
  172. */
  173. public function getType()
  174. {
  175. return $this->_sType;
  176. }
  177. /**
  178. * Get connection table.
  179. * return string with table name.
  180. */
  181. public function getTable()
  182. {
  183. return $this->_aObject['table'];
  184. }
  185. /**
  186. * Checks whether connection's Initiator is profile or not.
  187. * return boolean.
  188. */
  189. public function isProfileInitiator()
  190. {
  191. return (int)$this->_aObject['profile_initiator'] != 0;
  192. }
  193. /**
  194. * Checks whether connection's Content is profile or not.
  195. * return boolean.
  196. */
  197. public function isProfileContent()
  198. {
  199. return (int)$this->_aObject['profile_content'] != 0;
  200. }
  201. /**
  202. * Check whether connection between Initiator and Content can be established.
  203. */
  204. public function checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  205. {
  206. $aResult = $this->_checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  207. return $aResult['code'] == 0 ? CHECK_ACTION_RESULT_ALLOWED : $aResult['message'];
  208. }
  209. public function checkAllowedConnectByAcl ($iInitiator, $iContent, $isPerformAction = false)
  210. {
  211. $aResult = $this->_checkAllowedConnectByAcl($iInitiator, $iContent, $isPerformAction);
  212. return $aResult['code'] == 0 ? CHECK_ACTION_RESULT_ALLOWED : $aResult['message'];
  213. }
  214. public function checkAllowedAddConnection ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  215. {
  216. $aResult = $this->_checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  217. return $aResult['code'] == 0 || ($aResult['code'] == 4 && $this->_sObject == 'sys_profiles_friends') ? CHECK_ACTION_RESULT_ALLOWED : $aResult['message'];
  218. }
  219. public function checkAllowedRemoveConnection ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  220. {
  221. $aResult = $this->_checkAllowedConnect($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  222. return $aResult['code'] == 0 || ($aResult['code'] == 4 && $this->_sObject == 'sys_profiles_friends') ? CHECK_ACTION_RESULT_ALLOWED : $aResult['message'];
  223. }
  224. /**
  225. * Add new connection.
  226. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  227. * @return array
  228. */
  229. public function actionAdd ($iContent = 0, $iInitiator = false)
  230. {
  231. if(!$iContent && ($_iContent = bx_get('id')) !== false)
  232. $iContent = bx_process_input($_iContent, BX_DATA_INT);
  233. if($iInitiator)
  234. $this->_iInitiator = $iInitiator;
  235. $this->_iContent = $iContent;
  236. return $this->_action ($this->_iInitiator, $this->_iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  237. }
  238. /**
  239. * Remove connection. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  240. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  241. * @return array
  242. */
  243. public function actionRemove ($iContent = 0, $iInitiator = false)
  244. {
  245. if (!$iContent && ($_iContent = bx_get('id')) !== false)
  246. $iContent = bx_process_input($_iContent, BX_DATA_INT);
  247. if ($iContent != bx_get_logged_profile_id() && BX_CONNECTIONS_TYPE_MUTUAL == $this->_aObject['type']) {
  248. $a = $this->actionReject($iContent, $iInitiator);
  249. if (false == $a['err'])
  250. return $a;
  251. }
  252. if($iInitiator)
  253. $this->_iInitiator = $iInitiator;
  254. $this->_iContent = $iContent;
  255. return $this->_action ($this->_iInitiator, $this->_iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists', false, true);
  256. }
  257. /**
  258. * Reject connection request. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  259. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  260. * @return array
  261. */
  262. public function actionReject ($iContent = 0, $iInitiator = false)
  263. {
  264. if (!$iContent && ($_iContent = bx_get('id')) !== false)
  265. $iContent = bx_process_input($_iContent, BX_DATA_INT);
  266. if($iInitiator)
  267. $this->_iInitiator = $iInitiator;
  268. $this->_iContent = $iContent;
  269. return $this->_action($this->_iContent, $this->_iInitiator, 'removeConnection', '_sys_conn_err_connection_does_not_exists', false, true);
  270. }
  271. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false, $isInvert = false)
  272. {
  273. bx_import('BxDolLanguages');
  274. if(!$iContent || !$iInitiator)
  275. return ['err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined')];
  276. $sMethodCheck = 'checkAllowed' . bx_gen_method_name($sMethod);
  277. if(($mixedResult = $this->{method_exists($this, $sMethodCheck) ? $sMethodCheck : 'checkAllowedConnect'}($iInitiator, $iContent, false, false, $isInvert)) !== CHECK_ACTION_RESULT_ALLOWED)
  278. return ['err' => true, 'msg' => $mixedResult];
  279. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  280. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  281. return ['err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation')];
  282. return ['err' => true, 'msg' => _t($sErrorKey)];
  283. }
  284. return ['err' => false, 'msg' => _t('_sys_conn_msg_success')];
  285. }
  286. public function outputActionResult ($mixed, $sFormat = 'json')
  287. {
  288. switch ($sFormat) {
  289. case 'html':
  290. echo $mixed;
  291. break;
  292. case 'json':
  293. default:
  294. header('Content-Type: application/json; charset=utf-8');
  295. echo json_encode($mixed);
  296. }
  297. exit;
  298. }
  299. /**
  300. * Add new connection.
  301. * @param $iInitiator initiator of the connection, in most cases some profile id
  302. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  303. * @return true - if connection was added, false - if connection already exists or error occured
  304. */
  305. public function addConnection ($iInitiator, $iContent, $aParams = array())
  306. {
  307. $iMutual = 0;
  308. $iInitiator = (int)$iInitiator;
  309. $iContent = (int)$iContent;
  310. $iOverrideResult = null;
  311. $aAlertExtras = [
  312. 'initiator' => &$iInitiator,
  313. 'content' => &$iContent,
  314. 'mutual' => &$iMutual,
  315. 'object' => $this,
  316. 'override_result' => &$iOverrideResult,
  317. ];
  318. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  319. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  320. /**
  321. * @hooks
  322. * @hookdef hook-bx_dol_connection-connection_before_add '{object_name}', 'connection_before_add' - hook before connection was added. Connection params can be overridden
  323. * - $unit_name - connection object name
  324. * - $action - equals `connection_before_add`
  325. * - $object_id - not used
  326. * - $sender_id - logged in profile id
  327. * - $extra_params - array of additional params with the following array keys:
  328. * - `initiator` - [int] by ref, profile id who is creating the connection, can be overridden in hook processing
  329. * - `content` - [int] by ref, profile id with whom the connection is creating, can be overridden in hook processing
  330. * - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
  331. * - `object` - [object] an instance of connection object, @see BxDolConnection
  332. * - `object_name` - [string] connection object name
  333. * - `override_result` - [boolean] by ref, stop adding and return specified result
  334. * @hook @ref hook-bx_dol_connection-connection_before_add
  335. */
  336. bx_alert($this->_sObject, 'connection_before_add', 0, false, $aAlertExtras);
  337. bx_alert('system', 'connection_before_add', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  338. if (null !== $aAlertExtras['override_result'])
  339. return $aAlertExtras['override_result'];
  340. if (!$this->_oQuery->addConnection($iInitiator, $iContent, $iMutual))
  341. return false;
  342. /**
  343. * @hooks
  344. * @hookdef hook-bx_dol_connection-connection_added '{object_name}', 'connection_added' - hook after connection was added. Connection params can be overridden
  345. * It's equivalent to @ref hook-bx_dol_connection-connection_before_add
  346. * @hook @ref hook-bx_dol_connection-connection_added
  347. */
  348. bx_alert($this->_sObject, 'connection_added', 0, false, $aAlertExtras);
  349. bx_alert('system', 'connection_added', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  350. $this->onAdded($iInitiator, $iContent, $iMutual);
  351. return true;
  352. }
  353. public function onAdded($iInitiator, $iContent, $iMutual)
  354. {
  355. $this->checkAllowedConnect($iInitiator, $iContent, true, $iMutual, false);
  356. if($this->_isTriggerable($iMutual))
  357. $this->_updateTriggerValue($iInitiator, $iContent, 1);
  358. /**
  359. * Call socket.
  360. */
  361. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
  362. $aMessageInitiator = $aMessageContent = [
  363. 'object' => $this->_sObject,
  364. 'action' => 'added',
  365. 'initiator' => $iInitiator,
  366. 'content' => $iContent,
  367. ];
  368. if(bx_is_api()) {
  369. $aMessageInitiator = array_merge($aMessageInitiator, [
  370. 'user' => BxDolProfile::getDataForPage($iInitiator)
  371. ]);
  372. $aMessageContent = array_merge($aMessageContent, [
  373. 'user' => BxDolProfile::getDataForPage($iContent)
  374. ]);
  375. }
  376. $oSockets->sendEvent('sys_connections', $iInitiator , 'changed', json_encode($aMessageInitiator));
  377. $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
  378. }
  379. $bMutual = false;
  380. if($this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($bMutual = ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual))) {
  381. $oProfileQuery = BxDolProfileQuery::getInstance();
  382. /**
  383. * Update recommendations.
  384. */
  385. if($this->_aObject['profile_initiator']) {
  386. $aInitiator = $oProfileQuery->getInfoById($iInitiator);
  387. if(bx_srv($aInitiator['type'], 'act_as_profile'))
  388. BxDolRecommendation::updateData($iInitiator);
  389. }
  390. if($bMutual && $this->_aObject['profile_content']) {
  391. $aContent = $oProfileQuery->getInfoById($iContent);
  392. if(bx_srv($aContent['type'], 'act_as_profile'))
  393. BxDolRecommendation::updateData($iContent);
  394. }
  395. }
  396. }
  397. /**
  398. * Remove connection.
  399. * @param $iInitiator initiator of the connection
  400. * @param $iContent connected content or other profile id in case of friends
  401. * @return true - if connection was removed, false - if connection isn't exist or error occured
  402. */
  403. public function removeConnection ($iInitiator, $iContent)
  404. {
  405. $iInitiator = (int)$iInitiator;
  406. $iContent = (int)$iContent;
  407. if(!($aConnection = $this->_oQuery->getConnection($iInitiator, $iContent))) // connection doesn't exist
  408. return false;
  409. $iMutual = isset($aConnection['mutual']) ? $aConnection['mutual'] : 0;
  410. $aAlertExtras = [
  411. 'initiator' => &$iInitiator,
  412. 'content' => &$iContent,
  413. 'mutual' => &$iMutual,
  414. 'object' => $this,
  415. ];
  416. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  417. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  418. /**
  419. * @hooks
  420. * @hookdef hook-bx_dol_connection-connection_before_remove '{object_name}', 'connection_before_remove' - hook before connection was removed. Connection params can be overridden
  421. * - $unit_name - connection object name
  422. * - $action - equals `connection_before_remove`
  423. * - $object_id - not used
  424. * - $sender_id - logged in profile id
  425. * - $extra_params - array of additional params with the following array keys:
  426. * - `initiator` - [int] by ref, profile id who is removing the connection, can be overridden in hook processing
  427. * - `content` - [int] by ref, profile id with whom the connection is removing, can be overridden in hook processing
  428. * - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
  429. * - `object` - [object] an instance of relation, @see BxDolConnection
  430. * @hook @ref hook-bx_dol_connection-connection_before_remove
  431. */
  432. bx_alert($this->_sObject, 'connection_before_remove', 0, false, $aAlertExtras);
  433. bx_alert('system', 'connection_before_remove', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  434. if(!$this->_oQuery->removeConnection($iInitiator, $iContent))
  435. return false;
  436. /**
  437. * @hooks
  438. * @hookdef hook-bx_dol_connection-connection_removed '{object_name}', 'connection_removed' - hook after a connection was removed.
  439. * It's equivalent to @ref hook-bx_dol_connection-connection_before_remove
  440. * @hook @ref hook-bx_dol_connection-connection_removed
  441. */
  442. bx_alert($this->_sObject, 'connection_removed', 0, false, $aAlertExtras);
  443. bx_alert('system', 'connection_removed', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  444. $this->onRemoved($iInitiator, $iContent, $iMutual);
  445. return true;
  446. }
  447. public function onRemoved($iInitiator, $iContent, $iMutual)
  448. {
  449. $this->_updateTriggerValue($iInitiator, $iContent, -1);
  450. /**
  451. * Call socket.
  452. */
  453. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
  454. $aMessageInitiator = $aMessageContent = [
  455. 'object' => $this->_sObject,
  456. 'action' => 'deleted',
  457. ];
  458. if(bx_is_api()) {
  459. $aMessageInitiator = array_merge($aMessageInitiator, [
  460. 'user' => BxDolProfile::getDataForPage($iInitiator)
  461. ]);
  462. $aMessageContent = array_merge($aMessageContent, [
  463. 'user' => BxDolProfile::getDataForPage($iContent)
  464. ]);
  465. }
  466. $oSockets->sendEvent('sys_connections', $iInitiator , 'changed', json_encode($aMessageInitiator));
  467. $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
  468. }
  469. }
  470. /**
  471. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  472. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  473. * @param $iId1 one content or initiator
  474. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  475. * @param $isMutual get mutual connections only
  476. * @return array of available connections
  477. */
  478. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  479. {
  480. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  481. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  482. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  483. $sMethod = 'getConnectedInitiators';
  484. else
  485. $sMethod = 'getConnectedContent';
  486. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  487. }
  488. /**
  489. * Get common content IDs between two initiators
  490. * @param $iInitiator1 one initiator
  491. * @param $iInitiator2 second initiator
  492. * @param $isMutual get mutual connections only
  493. * @return array of available connections
  494. */
  495. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  496. {
  497. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  498. }
  499. /**
  500. * Get common content count between two initiators
  501. * @param $iInitiator1 one initiator
  502. * @param $iInitiator2 second initiator
  503. * @param $isMutual get mutual connections only
  504. * @return number of connections
  505. */
  506. public function getCommonContentCount ($iInitiator1, $iInitiator2, $isMutual = false)
  507. {
  508. return $this->_oQuery->getCommonContentCount($iInitiator1, $iInitiator2, $isMutual);
  509. }
  510. /**
  511. * Get connected content count
  512. * @param $iInitiator initiator of the connection
  513. * @param $isMutual get mutual connections only
  514. * @return number of connections
  515. */
  516. public function getConnectedContentCount ($iInitiator, $isMutual = false, $iFromDate = 0)
  517. {
  518. if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_CONTENT, $iInitiator)) !== false)
  519. return $iValue;
  520. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual, $iFromDate);
  521. }
  522. /**
  523. * Get connected content count
  524. * @param $iInitiator initiator of the connection
  525. * @param $isMutual get mutual connections only
  526. * @param $aParams additional params
  527. * @return number of connections
  528. */
  529. public function getConnectedContentCountExt ($iInitiator, $isMutual = false, $aParams = [])
  530. {
  531. return $this->_oQuery->getConnectedContentCountExt($iInitiator, $isMutual, $aParams);
  532. }
  533. /**
  534. * Get connected initiators count
  535. * @param $iContent content of the connection
  536. * @param $isMutual get mutual connections only
  537. * @return number of connections
  538. */
  539. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  540. {
  541. if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, $iContent)) !== false)
  542. return $iValue;
  543. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  544. }
  545. /**
  546. * Get connected content IDs
  547. * @param $iInitiator initiator of the connection
  548. * @param $isMutual get mutual connections only
  549. * @return array of available connections
  550. */
  551. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  552. {
  553. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  554. }
  555. /**
  556. * Get connected content IDs for specified type
  557. * @param $iInitiator initiator of the connection
  558. * @param $mixedType type of content or an array of types
  559. * @param $isMutual get mutual connections only
  560. * @return array of available connections
  561. */
  562. public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  563. {
  564. return $this->_oQuery->getConnectedContentByType($iInitiator, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  565. }
  566. /**
  567. * Get connected content IDs for specified type
  568. * @param $iInitiator initiator of the connection
  569. * @param $iDate get connections appeared since this date
  570. * @param $isMutual get mutual connections only
  571. * @return array of available connections
  572. */
  573. public function getConnectedContentSince ($iInitiator, $iDate, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  574. {
  575. return $this->_oQuery->getConnectedContentSince($iInitiator, $iDate, $isMutual, $iStart, $iLimit, $iOrder);
  576. }
  577. /**
  578. * Get connected initiators IDs
  579. * @param $iContent content of the connection
  580. * @param $isMutual get mutual connections only
  581. * @return array of available connections
  582. */
  583. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  584. {
  585. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  586. }
  587. /**
  588. * Get connected initiators IDs
  589. * @param $iContent content of the connection
  590. * @param $mixedType type of content or an array of types
  591. * @param $isMutual get mutual connections only
  592. * @return array of available connections
  593. */
  594. public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  595. {
  596. return $this->_oQuery->getConnectedInitiatorsByType($iContent, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  597. }
  598. /**
  599. * Get connected initiators IDs
  600. * @param $iContent content of the connection
  601. * @param $iDate get connections appeared since this date
  602. * @param $isMutual get mutual connections only
  603. * @return array of available connections
  604. */
  605. public function getConnectedInitiatorsSince ($iContent, $iDate, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  606. {
  607. return $this->_oQuery->getConnectedInitiatorsSince($iContent, $iDate, $isMutual, $iStart, $iLimit, $iOrder);
  608. }
  609. /**
  610. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  611. * @see getConnectionsAsArray
  612. */
  613. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  614. {
  615. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  616. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  617. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  618. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  619. else
  620. $sMethod = 'getConnectedContentAsSQLParts';
  621. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  622. }
  623. /**
  624. * Get necessary parts of SQL query to use connections in other queries
  625. * @param $sContentTable content table or alias
  626. * @param $sContentField content table field or field alias
  627. * @param $iInitiator initiator of the connection
  628. * @param $isMutual get mutual connections only
  629. * @return array of SQL string parts, for now 'join' part only is returned
  630. */
  631. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  632. {
  633. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  634. }
  635. /**
  636. * Get necessary parts of SQL query to use connections in other queries
  637. * @param $sContentTable content table or alias
  638. * @param $sContentField content table field or field alias
  639. * @param $iInitiator initiator of the connection
  640. * @param $isMutual get mutual connections only
  641. * @return array of SQL string parts, for now 'join' part only is returned
  642. */
  643. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  644. {
  645. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  646. }
  647. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  648. {
  649. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  650. }
  651. /**
  652. * Get necessary parts of SQL query to use connections in other queries
  653. * @param $sContentTable content table or alias
  654. * @param $sContentField content table field or field alias
  655. * @param $sInitiatorTable initiator table or alias
  656. * @param $sInitiatorField initiator table field or field alias
  657. * @param $iInitiator initiator of the connection
  658. * @param $isMutual get mutual connections only
  659. * @return array of SQL string parts, for now 'join' part only is returned
  660. */
  661. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  662. {
  663. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  664. }
  665. /**
  666. * Get necessary parts of SQL query to use connections in other queries
  667. * @param $sInitiatorTable initiator table or alias
  668. * @param $sInitiatorField initiator table field or field alias
  669. * @param $iContent content of the connection
  670. * @param $isMutual get mutual connections only
  671. * @return array of SQL string parts, for now 'join' part only is returned
  672. */
  673. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  674. {
  675. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  676. }
  677. /**
  678. * Get necessary parts of SQL query to use connections in other queries
  679. * @param $sInitiatorTable initiator table or alias
  680. * @param $sInitiatorField initiator table field or field alias
  681. * @param $sContentTable content table or alias
  682. * @param $sContentField content table field or field alias
  683. * @param $isMutual get mutual connections only
  684. * @return array of SQL string parts, for now 'join' part only is returned
  685. */
  686. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  687. {
  688. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  689. }
  690. /**
  691. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  692. * @see getConnectionsAsArray
  693. */
  694. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  695. {
  696. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  697. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  698. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  699. $sMethod = 'getConnectedInitiatorsAsCondition';
  700. else
  701. $sMethod = 'getConnectedContentAsCondition';
  702. return $this->$sMethod($sContentField, $iId1, $isMutual);
  703. }
  704. /**
  705. * Get necessary condition array to use connections in search classes
  706. * @param $sContentField content table field name
  707. * @param $iInitiator initiator of the connection
  708. * @param $iMutual get mutual connections only
  709. * @return array of conditions, for now with 'restriction' and 'join' parts
  710. */
  711. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  712. {
  713. return array(
  714. 'restriction' => array (
  715. 'connections_' . $this->_sObject => array(
  716. 'value' => $iInitiator1,
  717. 'field' => 'initiator',
  718. 'operator' => '=',
  719. 'table' => 'c',
  720. ),
  721. 'connections_mutual_' . $this->_sObject => array(
  722. 'value' => $iMutual,
  723. 'field' => 'mutual',
  724. 'operator' => '=',
  725. 'table' => 'c',
  726. ),
  727. 'connections2_' . $this->_sObject => array(
  728. 'value' => $iInitiator2,
  729. 'field' => 'initiator',
  730. 'operator' => '=',
  731. 'table' => 'c2',
  732. ),
  733. 'connections2_mutual_' . $this->_sObject => array(
  734. 'value' => $iMutual,
  735. 'field' => 'mutual',
  736. 'operator' => '=',
  737. 'table' => 'c2',
  738. ),
  739. ),
  740. 'join' => array (
  741. 'connections_' . $this->_sObject => array(
  742. 'type' => 'INNER',
  743. 'table' => $this->_aObject['table'],
  744. 'table_alias' => 'c',
  745. 'mainField' => $sContentField,
  746. 'onField' => 'content',
  747. 'joinFields' => array(),
  748. ),
  749. 'connections2_' . $this->_sObject => array(
  750. 'type' => 'INNER',
  751. 'table' => $this->_aObject['table'],
  752. 'table_alias' => 'c2',
  753. 'mainTable' => 'c',
  754. 'mainField' => 'content',
  755. 'onField' => 'content',
  756. 'joinFields' => array(),
  757. ),
  758. ),
  759. );
  760. }
  761. /**
  762. * Get necessary condition array to use connections in search classes
  763. * @param $sContentField content table field name
  764. * @param $iInitiator initiator of the connection
  765. * @param $iMutual get mutual connections only
  766. * @return array of conditions, for now with 'restriction' and 'join' parts
  767. */
  768. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  769. {
  770. $sOperation = '=';
  771. if(is_array($iInitiator))
  772. $sOperation = 'in';
  773. return array(
  774. 'restriction' => array (
  775. 'connections_' . $this->_sObject => array(
  776. 'value' => $iInitiator,
  777. 'field' => 'initiator',
  778. 'operator' => $sOperation,
  779. 'table' => $this->_aObject['table'],
  780. ),
  781. 'connections_mutual_' . $this->_sObject => array(
  782. 'value' => $iMutual,
  783. 'field' => 'mutual',
  784. 'operator' => '=',
  785. 'table' => $this->_aObject['table'],
  786. ),
  787. ),
  788. 'join' => array (
  789. 'connections_' . $this->_sObject => array(
  790. 'type' => 'INNER',
  791. 'table' => $this->_aObject['table'],
  792. 'mainField' => $sContentField,
  793. 'onField' => 'content',
  794. 'joinFields' => array(),//'initiator'),
  795. ),
  796. ),
  797. );
  798. }
  799. /**
  800. * Get necessary condition array to use connections in search classes
  801. * @param $sContentField content table field name
  802. * @param $iInitiator initiator of the connection
  803. * @param $iMutual get mutual connections only
  804. * @return array of conditions, for now with 'restriction' and 'join' parts
  805. */
  806. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  807. {
  808. $sOperation = '=';
  809. if(is_array($iContent))
  810. $sOperation = 'in';
  811. return array(
  812. 'restriction' => array (
  813. 'connections_' . $this->_sObject => array(
  814. 'value' => $iContent,
  815. 'field' => 'content',
  816. 'operator' => $sOperation,
  817. 'table' => $this->_aObject['table'],
  818. ),
  819. 'connections_mutual_' . $this->_sObject => array(
  820. 'value' => $iMutual,
  821. 'field' => 'mutual',
  822. 'operator' => '=',
  823. 'table' => $this->_aObject['table'],
  824. ),
  825. ),
  826. 'join' => array (
  827. 'connections_' . $this->_sObject => array(
  828. 'type' => 'INNER',
  829. 'table' => $this->_aObject['table'],
  830. 'mainField' => $sContentField,
  831. 'onField' => 'initiator',
  832. 'joinFields' => array(),//'initiator'),
  833. ),
  834. ),
  835. );
  836. }
  837. /**
  838. * Check if initiator and content are connected.
  839. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  840. * @param $iInitiator initiator of the connection
  841. * @param $iContent connected content or other profile id in case of friends
  842. * @return true - if content and initiator are connected or false - in all other cases
  843. */
  844. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  845. {
  846. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  847. if (!$oConnection)
  848. return false;
  849. return false === $isMutual ? true : (isset($oConnection['mutual']) ? $oConnection['mutual'] : false);
  850. }
  851. /**
  852. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  853. * This method makes sense only when type of connection is mutual.
  854. * @param $iInitiator initiator of the connection
  855. * @param $iContent connected content or other profile id in case of friends
  856. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  857. */
  858. public function isConnectedNotMutual ($iInitiator, $iContent)
  859. {
  860. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  861. if (!$oConnection)
  862. return false;
  863. return $oConnection['mutual'] ? false : true;
  864. }
  865. public function getConnection ($iInitiator, $iContent)
  866. {
  867. return $this->_oQuery->getConnection($iInitiator, $iContent);
  868. }
  869. public function getConnectionById ($iId)
  870. {
  871. return $this->_oQuery->getConnectionById($iId);
  872. }
  873. /**
  874. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  875. * @param $iId which can be as conetnt ot initiator
  876. * @return true if some connections were deleted
  877. */
  878. public function onDeleteInitiatorAndContent ($iId)
  879. {
  880. $b = $this->onDeleteInitiator ($iId);
  881. $b = $this->onDeleteContent ($iId) || $b;
  882. return $b;
  883. }
  884. /**
  885. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  886. * @param $iIdInitiator initiator id
  887. * @return true if some connections were deleted
  888. */
  889. public function onDeleteInitiator ($iIdInitiator)
  890. {
  891. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  892. return false;
  893. /**
  894. * @hooks
  895. * @hookdef hook-bx_dol_connection-connection_removed_all '{object_name}', 'connection_removed_all' - hook after all connections with deleted 'initiator' were removed.
  896. * - $unit_name - connection object name
  897. * - $action - equals `connection_removed_all`
  898. * - $object_id - not used
  899. * - $sender_id - logged in profile id
  900. * - $extra_params - array of additional params with the following array keys:
  901. * - `initiator` - [int] profile id who created the connection
  902. * - `object` - [object] an instance of relation, @see BxDolConnection
  903. * @hook @ref hook-bx_dol_connection-connection_removed_all
  904. */
  905. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  906. 'initiator' => (int)$iIdInitiator,
  907. 'object' => $this,
  908. ));
  909. return true;
  910. }
  911. /**
  912. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  913. * @param $iIdInitiator initiator id
  914. * @return true if some connections were deleted
  915. */
  916. public function onDeleteContent ($iIdContent)
  917. {
  918. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  919. return false;
  920. /**
  921. * @hooks
  922. * @hookdef hook-bx_dol_connection-connection_removed_all '{object_name}', 'connection_removed_all' - hook after all connections with deleted 'content' were removed.
  923. * - $unit_name - connection object name
  924. * - $action - equals `connection_removed_all`
  925. * - $object_id - not used
  926. * - $sender_id - logged in profile id
  927. * - $extra_params - array of additional params with the following array keys:
  928. * - `content` - [int] profile id with whom the connection was created
  929. * - `object` - [object] an instance of relation, @see BxDolConnection
  930. * @hook @ref hook-bx_dol_connection-connection_removed_all
  931. */
  932. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  933. 'content' => (int)$iIdContent,
  934. 'object' => $this,
  935. ));
  936. return true;
  937. }
  938. /**
  939. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  940. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  941. * @param $sTable table name with data which have assiciations with the connection
  942. * @param $sFieldId id field name which is associated with the connection
  943. * @return number of deleted connections
  944. */
  945. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  946. {
  947. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  948. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  949. return $iAffected;
  950. }
  951. /**
  952. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  953. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  954. * @param $sTable table name with data which have assiciations with the connection
  955. * @param $sFieldId id field name which is associated with the connection
  956. * @return number of deleted connections
  957. */
  958. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  959. {
  960. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  961. }
  962. /**
  963. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  964. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  965. * @param $sTable table name with data which have assiciations with the connection
  966. * @param $sFieldId id field name which is associated with the connection
  967. * @return number of deleted connections
  968. */
  969. public function onModuleDeleteContent ($sTable, $sFieldId)
  970. {
  971. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  972. }
  973. /**
  974. * Must be called when module (which can have connections as 'content' or as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  975. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  976. * @param $sModuleName module name to delete connections for
  977. * @return number of deleted connections
  978. */
  979. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  980. {
  981. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  982. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  983. return $iAffected;
  984. }
  985. /**
  986. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  987. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  988. * @param $sModuleName module name to delete connections for
  989. * @return number of deleted connections
  990. */
  991. public function onModuleProfileDeleteInitiator ($sModuleName)
  992. {
  993. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  994. }
  995. /**
  996. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  997. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  998. * @param $sModuleName module name to delete connections for
  999. * @return number of deleted connections
  1000. */
  1001. public function onModuleProfileDeleteContent ($sModuleName)
  1002. {
  1003. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  1004. }
  1005. protected function _checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  1006. {
  1007. $sErr = _t('_sys_txt_access_denied');
  1008. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  1009. return ['code' => 1, 'message' => $sErr];
  1010. $oInitiator = BxDolProfile::getInstance($iInitiator);
  1011. $oContent = BxDolProfile::getInstance($iContent);
  1012. if(!$oInitiator || !$oContent)
  1013. return ['code' => 2, 'message' => $sErr];
  1014. // check ACL
  1015. if(($mixedResult = $this->_checkAllowedConnectInitiator($oInitiator, $isPerformAction)) !== CHECK_ACTION_RESULT_ALLOWED)
  1016. return ['code' => 3, 'message' => $mixedResult];
  1017. $iCode = 0;
  1018. $sMessage = '';
  1019. // check content's visibility
  1020. if(!$this->isConnected($iContent, $iInitiator) && ($mixedResult = $this->_checkAllowedConnectContent($oContent)) !== CHECK_ACTION_RESULT_ALLOWED)
  1021. list($iCode, $sMessage) = [4, $mixedResult];
  1022. if(!$isCheckExists)
  1023. return ['code' => $iCode, 'message' => $sMessage != '' ? $sMessage : null];
  1024. if($isSwap)
  1025. $isConnected = $this->isConnected($iContent, $iInitiator, $isMutual);
  1026. else
  1027. $isConnected = $this->isConnected($iInitiator, $iContent, $isMutual);
  1028. if($isInvertResult)
  1029. $isConnected = !$isConnected;
  1030. if($isConnected)
  1031. list($iCode, $sMessage) = [5, $sErr];
  1032. return ['code' => $iCode, 'message' => $sMessage != '' ? $sMessage : null];
  1033. }
  1034. protected function _checkAllowedConnectByAcl ($iInitiator, $iContent, $isPerformAction = false)
  1035. {
  1036. $sErr = _t('_sys_txt_access_denied');
  1037. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  1038. return ['code' => 1, 'message' => $sErr];
  1039. $oInitiator = BxDolProfile::getInstance($iInitiator);
  1040. if(!$oInitiator || !BxDolProfile::getInstance($iContent))
  1041. return ['code' => 2, 'message' => $sErr];
  1042. // check ACL
  1043. if(($mixedResult = $this->_checkAllowedConnectInitiator($oInitiator, $isPerformAction)) !== CHECK_ACTION_RESULT_ALLOWED)
  1044. return ['code' => 3, 'message' => $mixedResult];
  1045. return ['code' => 0];
  1046. }
  1047. protected function _checkAllowedConnectInitiator ($oInitiator, $isPerformAction = false)
  1048. {
  1049. $aCheck = checkActionModule($oInitiator->id(), 'connect', 'system', $isPerformAction);
  1050. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1051. return $aCheck[CHECK_ACTION_MESSAGE];
  1052. return CHECK_ACTION_RESULT_ALLOWED;
  1053. }
  1054. protected function _checkAllowedConnectContent ($oContent)
  1055. {
  1056. return $oContent->checkAllowedProfileView();
  1057. }
  1058. protected function _isTriggerable($mixedMutual)
  1059. {
  1060. return $this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $mixedMutual);
  1061. }
  1062. protected function _updateTriggerValue($iInitiator, $iContent, $iValue)
  1063. {
  1064. foreach($this->_aTriggerTypes as $sType) {
  1065. if(empty($this->_aObject['tt_' . $sType]) || empty($this->_aObject['tf_id_' . $sType]) || empty($this->_aObject['tf_count_' . $sType]))
  1066. continue;
  1067. $iObjectId = $this->_getTriggerObject($sType, $iInitiator, $iContent);
  1068. if(!$iObjectId)
  1069. continue;
  1070. $this->_oQuery->updateTriggerValue($sType, $iObjectId, $iValue);
  1071. }
  1072. }
  1073. protected function _getTriggerValueByContentType($sContentType, $iParticipantId)
  1074. {
  1075. $aCt2Tt = [
  1076. BX_CONNECTIONS_CONTENT_TYPE_CONTENT => BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR,
  1077. BX_CONNECTIONS_CONTENT_TYPE_INITIATORS => BX_CONNECTIONS_TRIGGER_TYPE_CONTENT
  1078. ];
  1079. if(!isset($aCt2Tt[$sContentType]))
  1080. return false;
  1081. $sTriggerType = $aCt2Tt[$sContentType];
  1082. if(empty($this->_aObject['tt_' . $sTriggerType]) || empty($this->_aObject['tf_id_' . $sTriggerType]) || empty($this->_aObject['tf_count_' . $sTriggerType]))
  1083. return false;
  1084. $iObjectId = 0;
  1085. if((int)$this->_aObject['profile_' . $sTriggerType]) {
  1086. if(($oParticipant = BxDolProfile::getInstance($iParticipantId)) !== false)
  1087. $iObjectId = $oParticipant->getContentId();
  1088. }
  1089. else
  1090. $iObjectId = $iParticipantId;
  1091. return $iObjectId ? $this->_oQuery->getTriggerValue($sTriggerType, $iObjectId) : false;
  1092. }
  1093. /**
  1094. * Should be overwritten in Connection class which uses triggerable fields.
  1095. */
  1096. protected function _getTriggerObject($sType, $iInitiator, $iContent)
  1097. {
  1098. return false;
  1099. }
  1100. }
  1101. /** @} */