1
0

BxDolConnection.php 49 KB

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