BxDolConnection.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. 'initiator_ref' => &$iInitiator,
  316. 'content_ref' => &$iContent,
  317. 'mutual_ref' => &$iMutual,
  318. 'object' => $this,
  319. 'override_result' => &$iOverrideResult,
  320. ];
  321. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  322. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  323. /**
  324. * @hooks
  325. * @hookdef hook-bx_dol_connection-connection_before_add '{object_name}', 'connection_before_add' - hook before connection was added. Connection params can be overridden
  326. * - $unit_name - connection object name
  327. * - $action - equals `connection_before_add`
  328. * - $object_id - not used
  329. * - $sender_id - logged in profile id
  330. * - $extra_params - array of additional params with the following array keys:
  331. * - `initiator` - [int] by ref, profile id who is creating the connection, can be overridden in hook processing
  332. * - `content` - [int] by ref, profile id with whom the connection is creating, can be overridden in hook processing
  333. * - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
  334. * - `object` - [object] an instance of connection object, @see BxDolConnection
  335. * - `object_name` - [string] connection object name
  336. * - `override_result` - [boolean] by ref, stop adding and return specified result
  337. * @hook @ref hook-bx_dol_connection-connection_before_add
  338. */
  339. bx_alert($this->_sObject, 'connection_before_add', 0, false, $aAlertExtras);
  340. bx_alert('system', 'connection_before_add', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  341. if (null !== $aAlertExtras['override_result'])
  342. return $aAlertExtras['override_result'];
  343. if (!$this->_oQuery->addConnection($iInitiator, $iContent, $iMutual))
  344. return false;
  345. /**
  346. * @hooks
  347. * @hookdef hook-bx_dol_connection-connection_added '{object_name}', 'connection_added' - hook after connection was added. Connection params can be overridden
  348. * It's equivalent to @ref hook-bx_dol_connection-connection_before_add
  349. * @hook @ref hook-bx_dol_connection-connection_added
  350. */
  351. bx_alert($this->_sObject, 'connection_added', 0, false, $aAlertExtras);
  352. bx_alert('system', 'connection_added', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  353. $this->onAdded($iInitiator, $iContent, $iMutual);
  354. return true;
  355. }
  356. public function onAdded($iInitiator, $iContent, $iMutual)
  357. {
  358. $this->checkAllowedConnect($iInitiator, $iContent, true, $iMutual, false);
  359. if($this->_isTriggerable($iMutual))
  360. $this->_updateTriggerValue($iInitiator, $iContent, 1);
  361. /**
  362. * Call socket.
  363. */
  364. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
  365. $aMessageInitiator = $aMessageContent = [
  366. 'object' => $this->_sObject,
  367. 'action' => 'added',
  368. 'initiator' => $iInitiator,
  369. 'content' => $iContent,
  370. ];
  371. if(bx_is_api()) {
  372. $aMessageInitiator = array_merge($aMessageInitiator, [
  373. 'user' => BxDolProfile::getDataForPage($iInitiator)
  374. ]);
  375. $aMessageContent = array_merge($aMessageContent, [
  376. 'user' => BxDolProfile::getDataForPage($iContent)
  377. ]);
  378. }
  379. $oSockets->sendEvent('sys_connections', $iInitiator , 'changed', json_encode($aMessageInitiator));
  380. $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
  381. }
  382. $bMutual = false;
  383. if($this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($bMutual = ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual))) {
  384. $oProfileQuery = BxDolProfileQuery::getInstance();
  385. /**
  386. * Update recommendations.
  387. */
  388. if($this->_aObject['profile_initiator']) {
  389. $aInitiator = $oProfileQuery->getInfoById($iInitiator);
  390. if(bx_srv($aInitiator['type'], 'act_as_profile'))
  391. BxDolRecommendation::updateData($iInitiator);
  392. }
  393. if($bMutual && $this->_aObject['profile_content']) {
  394. $aContent = $oProfileQuery->getInfoById($iContent);
  395. if(bx_srv($aContent['type'], 'act_as_profile'))
  396. BxDolRecommendation::updateData($iContent);
  397. }
  398. }
  399. }
  400. /**
  401. * Remove connection.
  402. * @param $iInitiator initiator of the connection
  403. * @param $iContent connected content or other profile id in case of friends
  404. * @return true - if connection was removed, false - if connection isn't exist or error occured
  405. */
  406. public function removeConnection ($iInitiator, $iContent)
  407. {
  408. $iInitiator = (int)$iInitiator;
  409. $iContent = (int)$iContent;
  410. if(!($aConnection = $this->_oQuery->getConnection($iInitiator, $iContent))) // connection doesn't exist
  411. return false;
  412. $iMutual = isset($aConnection['mutual']) ? $aConnection['mutual'] : 0;
  413. $aAlertExtras = [
  414. 'initiator' => &$iInitiator,
  415. 'content' => &$iContent,
  416. 'mutual' => &$iMutual,
  417. 'initiator_ref' => &$iInitiator,
  418. 'content_ref' => &$iContent,
  419. 'mutual_ref' => &$iMutual,
  420. 'object' => $this,
  421. ];
  422. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  423. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  424. /**
  425. * @hooks
  426. * @hookdef hook-bx_dol_connection-connection_before_remove '{object_name}', 'connection_before_remove' - hook before connection was removed. Connection params can be overridden
  427. * - $unit_name - connection object name
  428. * - $action - equals `connection_before_remove`
  429. * - $object_id - not used
  430. * - $sender_id - logged in profile id
  431. * - $extra_params - array of additional params with the following array keys:
  432. * - `initiator` - [int] by ref, profile id who is removing the connection, can be overridden in hook processing
  433. * - `content` - [int] by ref, profile id with whom the connection is removing, can be overridden in hook processing
  434. * - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
  435. * - `object` - [object] an instance of relation, @see BxDolConnection
  436. * @hook @ref hook-bx_dol_connection-connection_before_remove
  437. */
  438. bx_alert($this->_sObject, 'connection_before_remove', 0, false, $aAlertExtras);
  439. bx_alert('system', 'connection_before_remove', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  440. if(!$this->_oQuery->removeConnection($iInitiator, $iContent))
  441. return false;
  442. /**
  443. * @hooks
  444. * @hookdef hook-bx_dol_connection-connection_removed '{object_name}', 'connection_removed' - hook after a connection was removed.
  445. * It's equivalent to @ref hook-bx_dol_connection-connection_before_remove
  446. * @hook @ref hook-bx_dol_connection-connection_removed
  447. */
  448. bx_alert($this->_sObject, 'connection_removed', 0, false, $aAlertExtras);
  449. bx_alert('system', 'connection_removed', 0, false, array_merge($aAlertExtras, ['object_name' => $this->_sObject]));
  450. $this->onRemoved($iInitiator, $iContent, $iMutual);
  451. return true;
  452. }
  453. public function onRemoved($iInitiator, $iContent, $iMutual)
  454. {
  455. $this->_updateTriggerValue($iInitiator, $iContent, -1);
  456. /**
  457. * Call socket.
  458. */
  459. if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
  460. $aMessageInitiator = $aMessageContent = [
  461. 'object' => $this->_sObject,
  462. 'action' => 'deleted',
  463. ];
  464. if(bx_is_api()) {
  465. $aMessageInitiator = array_merge($aMessageInitiator, [
  466. 'user' => BxDolProfile::getDataForPage($iInitiator)
  467. ]);
  468. $aMessageContent = array_merge($aMessageContent, [
  469. 'user' => BxDolProfile::getDataForPage($iContent)
  470. ]);
  471. }
  472. $oSockets->sendEvent('sys_connections', $iInitiator , 'changed', json_encode($aMessageInitiator));
  473. $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
  474. }
  475. }
  476. /**
  477. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  478. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  479. * @param $iId1 one content or initiator
  480. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  481. * @param $isMutual get mutual connections only
  482. * @return array of available connections
  483. */
  484. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  485. {
  486. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  487. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  488. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  489. $sMethod = 'getConnectedInitiators';
  490. else
  491. $sMethod = 'getConnectedContent';
  492. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  493. }
  494. /**
  495. * Get common content IDs between two initiators
  496. * @param $iInitiator1 one initiator
  497. * @param $iInitiator2 second initiator
  498. * @param $isMutual get mutual connections only
  499. * @return array of available connections
  500. */
  501. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  502. {
  503. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  504. }
  505. /**
  506. * Get common content count between two initiators
  507. * @param $iInitiator1 one initiator
  508. * @param $iInitiator2 second initiator
  509. * @param $isMutual get mutual connections only
  510. * @return number of connections
  511. */
  512. public function getCommonContentCount ($iInitiator1, $iInitiator2, $isMutual = false)
  513. {
  514. return $this->_oQuery->getCommonContentCount($iInitiator1, $iInitiator2, $isMutual);
  515. }
  516. /**
  517. * Get connected content count
  518. * @param $iInitiator initiator of the connection
  519. * @param $isMutual get mutual connections only
  520. * @return number of connections
  521. */
  522. public function getConnectedContentCount ($iInitiator, $isMutual = false, $iFromDate = 0)
  523. {
  524. if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_CONTENT, $iInitiator)) !== false)
  525. return $iValue;
  526. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual, $iFromDate);
  527. }
  528. /**
  529. * Get connected content count
  530. * @param $iInitiator initiator of the connection
  531. * @param $isMutual get mutual connections only
  532. * @param $aParams additional params
  533. * @return number of connections
  534. */
  535. public function getConnectedContentCountExt ($iInitiator, $isMutual = false, $aParams = [])
  536. {
  537. return $this->_oQuery->getConnectedContentCountExt($iInitiator, $isMutual, $aParams);
  538. }
  539. /**
  540. * Get connected initiators count
  541. * @param $iContent content of the connection
  542. * @param $isMutual get mutual connections only
  543. * @return number of connections
  544. */
  545. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  546. {
  547. if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, $iContent)) !== false)
  548. return $iValue;
  549. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  550. }
  551. /**
  552. * Get connected content IDs
  553. * @param $iInitiator initiator of the connection
  554. * @param $isMutual get mutual connections only
  555. * @return array of available connections
  556. */
  557. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  558. {
  559. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  560. }
  561. /**
  562. * Get connected content IDs for specified type
  563. * @param $iInitiator initiator of the connection
  564. * @param $mixedType type of content or an array of types
  565. * @param $isMutual get mutual connections only
  566. * @return array of available connections
  567. */
  568. public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  569. {
  570. return $this->_oQuery->getConnectedContentByType($iInitiator, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  571. }
  572. /**
  573. * Get connected content IDs for specified type
  574. * @param $iInitiator initiator of the connection
  575. * @param $iDate get connections appeared since this date
  576. * @param $isMutual get mutual connections only
  577. * @return array of available connections
  578. */
  579. public function getConnectedContentSince ($iInitiator, $iDate, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  580. {
  581. return $this->_oQuery->getConnectedContentSince($iInitiator, $iDate, $isMutual, $iStart, $iLimit, $iOrder);
  582. }
  583. /**
  584. * Get connected initiators IDs
  585. * @param $iContent content of the connection
  586. * @param $isMutual get mutual connections only
  587. * @return array of available connections
  588. */
  589. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  590. {
  591. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  592. }
  593. /**
  594. * Get connected initiators IDs
  595. * @param $iContent content of the connection
  596. * @param $mixedType type of content or an array of types
  597. * @param $isMutual get mutual connections only
  598. * @return array of available connections
  599. */
  600. public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  601. {
  602. return $this->_oQuery->getConnectedInitiatorsByType($iContent, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  603. }
  604. /**
  605. * Get connected initiators IDs
  606. * @param $iContent content of the connection
  607. * @param $iDate get connections appeared since this date
  608. * @param $isMutual get mutual connections only
  609. * @return array of available connections
  610. */
  611. public function getConnectedInitiatorsSince ($iContent, $iDate, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  612. {
  613. return $this->_oQuery->getConnectedInitiatorsSince($iContent, $iDate, $isMutual, $iStart, $iLimit, $iOrder);
  614. }
  615. /**
  616. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  617. * @see getConnectionsAsArray
  618. */
  619. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  620. {
  621. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  622. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  623. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  624. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  625. else
  626. $sMethod = 'getConnectedContentAsSQLParts';
  627. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  628. }
  629. /**
  630. * Get necessary parts of SQL query to use connections in other queries
  631. * @param $sContentTable content table or alias
  632. * @param $sContentField content table field or field alias
  633. * @param $iInitiator initiator of the connection
  634. * @param $isMutual get mutual connections only
  635. * @return array of SQL string parts, for now 'join' part only is returned
  636. */
  637. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  638. {
  639. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  640. }
  641. /**
  642. * Get necessary parts of SQL query to use connections in other queries
  643. * @param $sContentTable content table or alias
  644. * @param $sContentField content table field or field alias
  645. * @param $iInitiator initiator of the connection
  646. * @param $isMutual get mutual connections only
  647. * @return array of SQL string parts, for now 'join' part only is returned
  648. */
  649. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  650. {
  651. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  652. }
  653. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  654. {
  655. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  656. }
  657. /**
  658. * Get necessary parts of SQL query to use connections in other queries
  659. * @param $sContentTable content table or alias
  660. * @param $sContentField content table field or field alias
  661. * @param $sInitiatorTable initiator table or alias
  662. * @param $sInitiatorField initiator table field or field alias
  663. * @param $iInitiator initiator of the connection
  664. * @param $isMutual get mutual connections only
  665. * @return array of SQL string parts, for now 'join' part only is returned
  666. */
  667. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  668. {
  669. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  670. }
  671. /**
  672. * Get necessary parts of SQL query to use connections in other queries
  673. * @param $sInitiatorTable initiator table or alias
  674. * @param $sInitiatorField initiator table field or field alias
  675. * @param $iContent content of the connection
  676. * @param $isMutual get mutual connections only
  677. * @return array of SQL string parts, for now 'join' part only is returned
  678. */
  679. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  680. {
  681. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  682. }
  683. /**
  684. * Get necessary parts of SQL query to use connections in other queries
  685. * @param $sInitiatorTable initiator table or alias
  686. * @param $sInitiatorField initiator table field or field alias
  687. * @param $sContentTable content table or alias
  688. * @param $sContentField content table field or field alias
  689. * @param $isMutual get mutual connections only
  690. * @return array of SQL string parts, for now 'join' part only is returned
  691. */
  692. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  693. {
  694. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  695. }
  696. /**
  697. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  698. * @see getConnectionsAsArray
  699. */
  700. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  701. {
  702. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  703. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  704. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  705. $sMethod = 'getConnectedInitiatorsAsCondition';
  706. else
  707. $sMethod = 'getConnectedContentAsCondition';
  708. return $this->$sMethod($sContentField, $iId1, $isMutual);
  709. }
  710. /**
  711. * Get necessary condition array to use connections in search classes
  712. * @param $sContentField content table field name
  713. * @param $iInitiator initiator of the connection
  714. * @param $iMutual get mutual connections only
  715. * @return array of conditions, for now with 'restriction' and 'join' parts
  716. */
  717. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  718. {
  719. return array(
  720. 'restriction' => array (
  721. 'connections_' . $this->_sObject => array(
  722. 'value' => $iInitiator1,
  723. 'field' => 'initiator',
  724. 'operator' => '=',
  725. 'table' => 'c',
  726. ),
  727. 'connections_mutual_' . $this->_sObject => array(
  728. 'value' => $iMutual,
  729. 'field' => 'mutual',
  730. 'operator' => '=',
  731. 'table' => 'c',
  732. ),
  733. 'connections2_' . $this->_sObject => array(
  734. 'value' => $iInitiator2,
  735. 'field' => 'initiator',
  736. 'operator' => '=',
  737. 'table' => 'c2',
  738. ),
  739. 'connections2_mutual_' . $this->_sObject => array(
  740. 'value' => $iMutual,
  741. 'field' => 'mutual',
  742. 'operator' => '=',
  743. 'table' => 'c2',
  744. ),
  745. ),
  746. 'join' => array (
  747. 'connections_' . $this->_sObject => array(
  748. 'type' => 'INNER',
  749. 'table' => $this->_aObject['table'],
  750. 'table_alias' => 'c',
  751. 'mainField' => $sContentField,
  752. 'onField' => 'content',
  753. 'joinFields' => array(),
  754. ),
  755. 'connections2_' . $this->_sObject => array(
  756. 'type' => 'INNER',
  757. 'table' => $this->_aObject['table'],
  758. 'table_alias' => 'c2',
  759. 'mainTable' => 'c',
  760. 'mainField' => 'content',
  761. 'onField' => 'content',
  762. 'joinFields' => array(),
  763. ),
  764. ),
  765. );
  766. }
  767. /**
  768. * Get necessary condition array to use connections in search classes
  769. * @param $sContentField content table field name
  770. * @param $iInitiator initiator of the connection
  771. * @param $iMutual get mutual connections only
  772. * @return array of conditions, for now with 'restriction' and 'join' parts
  773. */
  774. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  775. {
  776. $sOperation = '=';
  777. if(is_array($iInitiator))
  778. $sOperation = 'in';
  779. return array(
  780. 'restriction' => array (
  781. 'connections_' . $this->_sObject => array(
  782. 'value' => $iInitiator,
  783. 'field' => 'initiator',
  784. 'operator' => $sOperation,
  785. 'table' => $this->_aObject['table'],
  786. ),
  787. 'connections_mutual_' . $this->_sObject => array(
  788. 'value' => $iMutual,
  789. 'field' => 'mutual',
  790. 'operator' => '=',
  791. 'table' => $this->_aObject['table'],
  792. ),
  793. ),
  794. 'join' => array (
  795. 'connections_' . $this->_sObject => array(
  796. 'type' => 'INNER',
  797. 'table' => $this->_aObject['table'],
  798. 'mainField' => $sContentField,
  799. 'onField' => 'content',
  800. 'joinFields' => array(),//'initiator'),
  801. ),
  802. ),
  803. );
  804. }
  805. /**
  806. * Get necessary condition array to use connections in search classes
  807. * @param $sContentField content table field name
  808. * @param $iInitiator initiator of the connection
  809. * @param $iMutual get mutual connections only
  810. * @return array of conditions, for now with 'restriction' and 'join' parts
  811. */
  812. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  813. {
  814. $sOperation = '=';
  815. if(is_array($iContent))
  816. $sOperation = 'in';
  817. return array(
  818. 'restriction' => array (
  819. 'connections_' . $this->_sObject => array(
  820. 'value' => $iContent,
  821. 'field' => 'content',
  822. 'operator' => $sOperation,
  823. 'table' => $this->_aObject['table'],
  824. ),
  825. 'connections_mutual_' . $this->_sObject => array(
  826. 'value' => $iMutual,
  827. 'field' => 'mutual',
  828. 'operator' => '=',
  829. 'table' => $this->_aObject['table'],
  830. ),
  831. ),
  832. 'join' => array (
  833. 'connections_' . $this->_sObject => array(
  834. 'type' => 'INNER',
  835. 'table' => $this->_aObject['table'],
  836. 'mainField' => $sContentField,
  837. 'onField' => 'initiator',
  838. 'joinFields' => array(),//'initiator'),
  839. ),
  840. ),
  841. );
  842. }
  843. /**
  844. * Check if initiator and content are connected.
  845. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  846. * @param $iInitiator initiator of the connection
  847. * @param $iContent connected content or other profile id in case of friends
  848. * @return true - if content and initiator are connected or false - in all other cases
  849. */
  850. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  851. {
  852. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  853. if (!$oConnection)
  854. return false;
  855. return false === $isMutual ? true : (isset($oConnection['mutual']) ? $oConnection['mutual'] : false);
  856. }
  857. /**
  858. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  859. * This method makes sense only when type of connection is mutual.
  860. * @param $iInitiator initiator of the connection
  861. * @param $iContent connected content or other profile id in case of friends
  862. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  863. */
  864. public function isConnectedNotMutual ($iInitiator, $iContent)
  865. {
  866. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  867. if (!$oConnection)
  868. return false;
  869. return $oConnection['mutual'] ? false : true;
  870. }
  871. public function getConnection ($iInitiator, $iContent)
  872. {
  873. return $this->_oQuery->getConnection($iInitiator, $iContent);
  874. }
  875. public function getConnectionById ($iId)
  876. {
  877. return $this->_oQuery->getConnectionById($iId);
  878. }
  879. /**
  880. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  881. * @param $iId which can be as conetnt ot initiator
  882. * @return true if some connections were deleted
  883. */
  884. public function onDeleteInitiatorAndContent ($iId)
  885. {
  886. $b = $this->onDeleteInitiator ($iId);
  887. $b = $this->onDeleteContent ($iId) || $b;
  888. return $b;
  889. }
  890. /**
  891. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  892. * @param $iIdInitiator initiator id
  893. * @return true if some connections were deleted
  894. */
  895. public function onDeleteInitiator ($iIdInitiator)
  896. {
  897. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  898. return false;
  899. /**
  900. * @hooks
  901. * @hookdef hook-bx_dol_connection-connection_removed_all '{object_name}', 'connection_removed_all' - hook after all connections with deleted 'initiator' were removed.
  902. * - $unit_name - connection object name
  903. * - $action - equals `connection_removed_all`
  904. * - $object_id - not used
  905. * - $sender_id - logged in profile id
  906. * - $extra_params - array of additional params with the following array keys:
  907. * - `initiator` - [int] profile id who created the connection
  908. * - `object` - [object] an instance of relation, @see BxDolConnection
  909. * @hook @ref hook-bx_dol_connection-connection_removed_all
  910. */
  911. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  912. 'initiator' => (int)$iIdInitiator,
  913. 'object' => $this,
  914. ));
  915. return true;
  916. }
  917. /**
  918. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  919. * @param $iIdInitiator initiator id
  920. * @return true if some connections were deleted
  921. */
  922. public function onDeleteContent ($iIdContent)
  923. {
  924. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  925. return false;
  926. /**
  927. * @hooks
  928. * @hookdef hook-bx_dol_connection-connection_removed_all '{object_name}', 'connection_removed_all' - hook after all connections with deleted 'content' were removed.
  929. * - $unit_name - connection object name
  930. * - $action - equals `connection_removed_all`
  931. * - $object_id - not used
  932. * - $sender_id - logged in profile id
  933. * - $extra_params - array of additional params with the following array keys:
  934. * - `content` - [int] profile id with whom the connection was created
  935. * - `object` - [object] an instance of relation, @see BxDolConnection
  936. * @hook @ref hook-bx_dol_connection-connection_removed_all
  937. */
  938. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  939. 'content' => (int)$iIdContent,
  940. 'object' => $this,
  941. ));
  942. return true;
  943. }
  944. /**
  945. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  946. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  947. * @param $sTable table name with data which have assiciations with the connection
  948. * @param $sFieldId id field name which is associated with the connection
  949. * @return number of deleted connections
  950. */
  951. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  952. {
  953. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  954. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  955. return $iAffected;
  956. }
  957. /**
  958. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  959. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  960. * @param $sTable table name with data which have assiciations with the connection
  961. * @param $sFieldId id field name which is associated with the connection
  962. * @return number of deleted connections
  963. */
  964. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  965. {
  966. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  967. }
  968. /**
  969. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  970. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  971. * @param $sTable table name with data which have assiciations with the connection
  972. * @param $sFieldId id field name which is associated with the connection
  973. * @return number of deleted connections
  974. */
  975. public function onModuleDeleteContent ($sTable, $sFieldId)
  976. {
  977. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  978. }
  979. /**
  980. * 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.
  981. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  982. * @param $sModuleName module name to delete connections for
  983. * @return number of deleted connections
  984. */
  985. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  986. {
  987. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  988. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  989. return $iAffected;
  990. }
  991. /**
  992. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  993. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  994. * @param $sModuleName module name to delete connections for
  995. * @return number of deleted connections
  996. */
  997. public function onModuleProfileDeleteInitiator ($sModuleName)
  998. {
  999. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  1000. }
  1001. /**
  1002. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  1003. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  1004. * @param $sModuleName module name to delete connections for
  1005. * @return number of deleted connections
  1006. */
  1007. public function onModuleProfileDeleteContent ($sModuleName)
  1008. {
  1009. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  1010. }
  1011. protected function _checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  1012. {
  1013. $sErr = _t('_sys_txt_access_denied');
  1014. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  1015. return ['code' => 1, 'message' => $sErr];
  1016. $oInitiator = BxDolProfile::getInstance($iInitiator);
  1017. $oContent = BxDolProfile::getInstance($iContent);
  1018. if(!$oInitiator || !$oContent)
  1019. return ['code' => 2, 'message' => $sErr];
  1020. // check ACL
  1021. if(($mixedResult = $this->_checkAllowedConnectInitiator($oInitiator, $isPerformAction)) !== CHECK_ACTION_RESULT_ALLOWED)
  1022. return ['code' => 3, 'message' => $mixedResult];
  1023. $iCode = 0;
  1024. $sMessage = '';
  1025. // check content's visibility
  1026. if(!$this->isConnected($iContent, $iInitiator) && ($mixedResult = $this->_checkAllowedConnectContent($oContent)) !== CHECK_ACTION_RESULT_ALLOWED)
  1027. list($iCode, $sMessage) = [4, $mixedResult];
  1028. if(!$isCheckExists)
  1029. return ['code' => $iCode, 'message' => $sMessage != '' ? $sMessage : null];
  1030. if($isSwap)
  1031. $isConnected = $this->isConnected($iContent, $iInitiator, $isMutual);
  1032. else
  1033. $isConnected = $this->isConnected($iInitiator, $iContent, $isMutual);
  1034. if($isInvertResult)
  1035. $isConnected = !$isConnected;
  1036. if($isConnected)
  1037. list($iCode, $sMessage) = [5, $sErr];
  1038. return ['code' => $iCode, 'message' => $sMessage != '' ? $sMessage : null];
  1039. }
  1040. protected function _checkAllowedConnectByAcl ($iInitiator, $iContent, $isPerformAction = false)
  1041. {
  1042. $sErr = _t('_sys_txt_access_denied');
  1043. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  1044. return ['code' => 1, 'message' => $sErr];
  1045. $oInitiator = BxDolProfile::getInstance($iInitiator);
  1046. if(!$oInitiator || !BxDolProfile::getInstance($iContent))
  1047. return ['code' => 2, 'message' => $sErr];
  1048. // check ACL
  1049. if(($mixedResult = $this->_checkAllowedConnectInitiator($oInitiator, $isPerformAction)) !== CHECK_ACTION_RESULT_ALLOWED)
  1050. return ['code' => 3, 'message' => $mixedResult];
  1051. return ['code' => 0];
  1052. }
  1053. protected function _checkAllowedConnectInitiator ($oInitiator, $isPerformAction = false)
  1054. {
  1055. $aCheck = checkActionModule($oInitiator->id(), 'connect', 'system', $isPerformAction);
  1056. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1057. return $aCheck[CHECK_ACTION_MESSAGE];
  1058. return CHECK_ACTION_RESULT_ALLOWED;
  1059. }
  1060. protected function _checkAllowedConnectContent ($oContent)
  1061. {
  1062. return $oContent->checkAllowedProfileView();
  1063. }
  1064. protected function _isTriggerable($mixedMutual)
  1065. {
  1066. return $this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $mixedMutual);
  1067. }
  1068. protected function _updateTriggerValue($iInitiator, $iContent, $iValue)
  1069. {
  1070. foreach($this->_aTriggerTypes as $sType) {
  1071. if(empty($this->_aObject['tt_' . $sType]) || empty($this->_aObject['tf_id_' . $sType]) || empty($this->_aObject['tf_count_' . $sType]))
  1072. continue;
  1073. $iObjectId = $this->_getTriggerObject($sType, $iInitiator, $iContent);
  1074. if(!$iObjectId)
  1075. continue;
  1076. $this->_oQuery->updateTriggerValue($sType, $iObjectId, $iValue);
  1077. }
  1078. }
  1079. protected function _getTriggerValueByContentType($sContentType, $iParticipantId)
  1080. {
  1081. $aCt2Tt = [
  1082. BX_CONNECTIONS_CONTENT_TYPE_CONTENT => BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR,
  1083. BX_CONNECTIONS_CONTENT_TYPE_INITIATORS => BX_CONNECTIONS_TRIGGER_TYPE_CONTENT
  1084. ];
  1085. if(!isset($aCt2Tt[$sContentType]))
  1086. return false;
  1087. $sTriggerType = $aCt2Tt[$sContentType];
  1088. if(empty($this->_aObject['tt_' . $sTriggerType]) || empty($this->_aObject['tf_id_' . $sTriggerType]) || empty($this->_aObject['tf_count_' . $sTriggerType]))
  1089. return false;
  1090. $iObjectId = 0;
  1091. if((int)$this->_aObject['profile_' . $sTriggerType]) {
  1092. if(($oParticipant = BxDolProfile::getInstance($iParticipantId)) !== false)
  1093. $iObjectId = $oParticipant->getContentId();
  1094. }
  1095. else
  1096. $iObjectId = $iParticipantId;
  1097. return $iObjectId ? $this->_oQuery->getTriggerValue($sTriggerType, $iObjectId) : false;
  1098. }
  1099. /**
  1100. * Should be overwritten in Connection class which uses triggerable fields.
  1101. */
  1102. protected function _getTriggerObject($sType, $iInitiator, $iContent)
  1103. {
  1104. return false;
  1105. }
  1106. }
  1107. /** @} */