BxDolConnection.php 51 KB

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