1
0

BxDolConnection.php 52 KB

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