BxDolConnection.php 52 KB

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