BxDolConnection.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * Default limit for connections lists
  11. */
  12. define('BX_CONNECTIONS_LIST_LIMIT', 1000);
  13. /**
  14. * Connections order: no order
  15. */
  16. define('BX_CONNECTIONS_ORDER_NONE', 0);
  17. /**
  18. * Connections order: by addded time, asceding
  19. */
  20. define('BX_CONNECTIONS_ORDER_ADDED_ASC', 1);
  21. /**
  22. * Connections order: by addded time, desceding
  23. */
  24. define('BX_CONNECTIONS_ORDER_ADDED_DESC', 2);
  25. /**
  26. * Connection type: one-way
  27. */
  28. define('BX_CONNECTIONS_TYPE_ONE_WAY', 'one-way');
  29. /**
  30. * Connection type: mutual
  31. */
  32. define('BX_CONNECTIONS_TYPE_MUTUAL', 'mutual');
  33. /**
  34. * Connections content type: content
  35. */
  36. define('BX_CONNECTIONS_CONTENT_TYPE_CONTENT', 'content');
  37. /**
  38. * Connections content type: initiators
  39. */
  40. define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  41. /**
  42. * Connections content type: common
  43. */
  44. define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
  45. /**
  46. * @page objects
  47. * @section connection Connection
  48. * @ref BxDolConnection
  49. */
  50. /**
  51. * Connection is usefull when you need to organize some sorts of connections between different content,
  52. * for example: friends, contacts, favorites, block lists, subscriptions, etc.
  53. *
  54. * Two types of connections are supported one way connections (block list, favourites) and mutual (friends).
  55. *
  56. * For automatic handling of connections (like, add/remove connection in frontend) refer to JS function: @see bx_conn_action()
  57. *
  58. * @section connection_create Creating the Connection object:
  59. *
  60. * Step 1:
  61. * Add record to 'sys_objects_connection' table:
  62. * - object: name of the connection object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  63. * for example: bx_blogs_favorites - favorite blogs for users in blogs module.
  64. * - table: table name with connections, see step 2
  65. * - type: 'one-way' or 'mutual'
  66. * - override_class_name: user defined class name which is derived from one of base classes.
  67. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  68. *
  69. * Step 2:
  70. * Create table for connections:
  71. * @code
  72. * CREATE TABLE `my_sample_connections` (
  73. * `id` int(11) NOT NULL AUTO_INCREMENT,
  74. * `initiator` int(11) NOT NULL,
  75. * `content` int(11) NOT NULL,
  76. * `mutual` tinyint(4) NOT NULL, -- can re removed for one-way connections
  77. * `added` int(10) unsigned NOT NULL,
  78. * PRIMARY KEY (`id`),
  79. * UNIQUE KEY `initiator` (`initiator`,`content`),
  80. * KEY `content` (`content`)
  81. * )
  82. * @endcode
  83. *
  84. *
  85. * @section example Example of usage
  86. *
  87. * Check if two profiles are friends:
  88. * @code
  89. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  90. * if ($oConnectionFriends) // check if connections is available for using
  91. * 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
  92. * @endcode
  93. *
  94. * Get mutual content IDs (friends IDs)
  95. * @code
  96. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  97. * if ($oConnectionFriends) // check if connections is available for using
  98. * print_r($oConnection->getConnectedContent(100, 1)); // print array of friends IDs of 100's profile
  99. * @endcode
  100. *
  101. */
  102. class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
  103. {
  104. protected $_sObject;
  105. protected $_aObject;
  106. protected $_oQuery;
  107. protected $_sType;
  108. /**
  109. * Constructor
  110. * @param $aObject array of connection options
  111. */
  112. protected function __construct($aObject)
  113. {
  114. parent::__construct();
  115. $this->_sObject = $aObject['object'];
  116. $this->_aObject = $aObject;
  117. $this->_sType = $aObject['type'];
  118. $this->_oQuery = new BxDolConnectionQuery($aObject);
  119. }
  120. /**
  121. * Get connection object instance by object name
  122. * @param $sObject object name
  123. * @return object instance or false on error
  124. */
  125. static public function getObjectInstance($sObject)
  126. {
  127. if (!$sObject)
  128. return false;
  129. if (isset($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject]))
  130. return $GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject];
  131. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  132. if (!$aObject || !is_array($aObject))
  133. return false;
  134. $sClass = empty($aObject['override_class_name']) ? 'BxDolConnection' : $aObject['override_class_name'];
  135. if (!empty($aObject['override_class_file']))
  136. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  137. $o = new $sClass($aObject);
  138. return ($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject] = $o);
  139. }
  140. /**
  141. * Add new connection.
  142. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  143. * @return array
  144. */
  145. public function actionAdd ($iContent = 0, $iInitiator = false)
  146. {
  147. if (!$iContent)
  148. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  149. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  150. }
  151. /**
  152. * Remove connection.
  153. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  154. * @return array
  155. */
  156. public function actionRemove ($iContent = 0, $iInitiator = false)
  157. {
  158. if (!$iContent)
  159. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  160. if ($iContent != bx_get_logged_profile_id()) {
  161. $a = $this->actionReject($iContent);
  162. if (false == $a['err'])
  163. return $a;
  164. }
  165. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  166. }
  167. /**
  168. * Reject connection request.
  169. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  170. * @return array
  171. */
  172. public function actionReject ($iContent = 0, $iInitiator = false)
  173. {
  174. if (!$iContent)
  175. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  176. return $this->_action ($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  177. }
  178. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false)
  179. {
  180. bx_import('BxDolLanguages');
  181. if (!$iContent || !$iInitiator)
  182. return array ('err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined'));
  183. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  184. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  185. return array ('err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation'));
  186. return array ('err' => true, 'msg' => _t($sErrorKey));
  187. }
  188. return array ('err' => false, 'msg' => _t('_sys_conn_msg_success'));
  189. }
  190. public function outputActionResult ($mixed, $sFormat = 'json')
  191. {
  192. switch ($sFormat) {
  193. case 'json':
  194. default:
  195. header('Content-Type: application/json; charset=utf-8');
  196. echo json_encode($mixed);
  197. }
  198. exit;
  199. }
  200. /**
  201. * Add new connection.
  202. * @param $iInitiator initiator of the connection, in most cases some profile id
  203. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  204. * @return true - if connection was added, false - if connection already exists or error occured
  205. */
  206. public function addConnection ($iInitiator, $iContent)
  207. {
  208. $iMutual = 0;
  209. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  210. return false;
  211. bx_alert($this->_sObject, 'connection_added', 0, getLoggedId(), array(
  212. 'initiator' => (int)$iInitiator,
  213. 'content' => (int)$iContent,
  214. 'mutual' => (int)$iMutual,
  215. 'object' => $this,
  216. ));
  217. return true;
  218. }
  219. /**
  220. * Remove connection.
  221. * @param $iInitiator initiator of the connection
  222. * @param $iContent connected content or other profile id in case of friends
  223. * @return true - if connection was removed, false - if connection isn't exist or error occured
  224. */
  225. public function removeConnection ($iInitiator, $iContent)
  226. {
  227. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  228. return false;
  229. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  230. return false;
  231. bx_alert($this->_sObject, 'connection_removed', 0, getLoggedId(), array(
  232. 'initiator' => (int)$iInitiator,
  233. 'content' => (int)$iContent,
  234. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  235. 'object' => $this,
  236. ));
  237. return true;
  238. }
  239. /**
  240. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  241. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  242. * @param $iId1 one content or initiator
  243. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  244. * @param $isMutual get mutual connections only
  245. * @return array of available connections
  246. */
  247. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  248. {
  249. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  250. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  251. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  252. $sMethod = 'getConnectedInitiators';
  253. else
  254. $sMethod = 'getConnectedContent';
  255. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  256. }
  257. /**
  258. * Get common content IDs between two initiators
  259. * @param $iInitiator1 one initiator
  260. * @param $iInitiator2 second initiator
  261. * @param $isMutual get mutual connections only
  262. * @return array of available connections
  263. */
  264. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  265. {
  266. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  267. }
  268. /**
  269. * Get connected content count
  270. * @param $iInitiator initiator of the connection
  271. * @param $isMutual get mutual connections only
  272. * @return number of connections
  273. */
  274. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  275. {
  276. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual);
  277. }
  278. /**
  279. * Get connected initiators count
  280. * @param $iContent content of the connection
  281. * @param $isMutual get mutual connections only
  282. * @return number of connections
  283. */
  284. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  285. {
  286. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  287. }
  288. /**
  289. * Get connected content IDs
  290. * @param $iInitiator initiator of the connection
  291. * @param $isMutual get mutual connections only
  292. * @return array of available connections
  293. */
  294. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  295. {
  296. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  297. }
  298. /**
  299. * Get connected initiators IDs
  300. * @param $iContent content of the connection
  301. * @param $isMutual get mutual connections only
  302. * @return array of available connections
  303. */
  304. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  305. {
  306. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  307. }
  308. /**
  309. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  310. * @see getConnectionsAsArray
  311. */
  312. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  313. {
  314. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  315. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  316. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  317. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  318. else
  319. $sMethod = 'getConnectedContentAsSQLParts';
  320. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  321. }
  322. /**
  323. * Get necessary parts of SQL query to use connections in other queries
  324. * @param $sContentTable content table or alias
  325. * @param $sContentField content table field or field alias
  326. * @param $iInitiator initiator of the connection
  327. * @param $isMutual get mutual connections only
  328. * @return array of SQL string parts, for now 'join' part only is returned
  329. */
  330. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  331. {
  332. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  333. }
  334. /**
  335. * Get necessary parts of SQL query to use connections in other queries
  336. * @param $sContentTable content table or alias
  337. * @param $sContentField content table field or field alias
  338. * @param $iInitiator initiator of the connection
  339. * @param $isMutual get mutual connections only
  340. * @return array of SQL string parts, for now 'join' part only is returned
  341. */
  342. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  343. {
  344. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  345. }
  346. /**
  347. * Get necessary parts of SQL query to use connections in other queries
  348. * @param $sContentTable content table or alias
  349. * @param $sContentField content table field or field alias
  350. * @param $iContent content of the connection
  351. * @param $isMutual get mutual connections only
  352. * @return array of SQL string parts, for now 'join' part only is returned
  353. */
  354. public function getConnectedInitiatorsAsSQLParts ($sContentTable, $sContentField, $iContent, $isMutual = false)
  355. {
  356. return $this->_oQuery->getConnectedInitiatorsSQLParts($sContentTable, $sContentField, $iContent, $isMutual);
  357. }
  358. /**
  359. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  360. * @see getConnectionsAsArray
  361. */
  362. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  363. {
  364. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  365. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  366. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  367. $sMethod = 'getConnectedInitiatorsAsCondition';
  368. else
  369. $sMethod = 'getConnectedContentAsCondition';
  370. return $this->$sMethod($sContentField, $iId1, $isMutual);
  371. }
  372. /**
  373. * Get necessary condition array to use connections in search classes
  374. * @param $sContentField content table field name
  375. * @param $iInitiator initiator of the connection
  376. * @param $iMutual get mutual connections only
  377. * @return array of conditions, for now with 'restriction' and 'join' parts
  378. */
  379. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  380. {
  381. return array(
  382. 'restriction' => array (
  383. 'connections_' . $this->_sObject => array(
  384. 'value' => $iInitiator1,
  385. 'field' => 'initiator',
  386. 'operator' => '=',
  387. 'table' => 'c',
  388. ),
  389. 'connections_mutual_' . $this->_sObject => array(
  390. 'value' => $iMutual,
  391. 'field' => 'mutual',
  392. 'operator' => '=',
  393. 'table' => 'c',
  394. ),
  395. 'connections2_' . $this->_sObject => array(
  396. 'value' => $iInitiator2,
  397. 'field' => 'initiator',
  398. 'operator' => '=',
  399. 'table' => 'c2',
  400. ),
  401. 'connections2_mutual_' . $this->_sObject => array(
  402. 'value' => $iMutual,
  403. 'field' => 'mutual',
  404. 'operator' => '=',
  405. 'table' => 'c2',
  406. ),
  407. ),
  408. 'join' => array (
  409. 'connections_' . $this->_sObject => array(
  410. 'type' => 'INNER',
  411. 'table' => $this->_aObject['table'],
  412. 'table_alias' => 'c',
  413. 'mainField' => $sContentField,
  414. 'onField' => 'content',
  415. 'joinFields' => array(),
  416. ),
  417. 'connections2_' . $this->_sObject => array(
  418. 'type' => 'INNER',
  419. 'table' => $this->_aObject['table'],
  420. 'table_alias' => 'c2',
  421. 'mainTable' => 'c',
  422. 'mainField' => 'content',
  423. 'onField' => 'content',
  424. 'joinFields' => array(),
  425. ),
  426. ),
  427. );
  428. }
  429. /**
  430. * Get necessary condition array to use connections in search classes
  431. * @param $sContentField content table field name
  432. * @param $iInitiator initiator of the connection
  433. * @param $iMutual get mutual connections only
  434. * @return array of conditions, for now with 'restriction' and 'join' parts
  435. */
  436. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  437. {
  438. return array(
  439. 'restriction' => array (
  440. 'connections_' . $this->_sObject => array(
  441. 'value' => $iInitiator,
  442. 'field' => 'initiator',
  443. 'operator' => '=',
  444. 'table' => $this->_aObject['table'],
  445. ),
  446. 'connections_mutual_' . $this->_sObject => array(
  447. 'value' => $iMutual,
  448. 'field' => 'mutual',
  449. 'operator' => '=',
  450. 'table' => $this->_aObject['table'],
  451. ),
  452. ),
  453. 'join' => array (
  454. 'connections_' . $this->_sObject => array(
  455. 'type' => 'INNER',
  456. 'table' => $this->_aObject['table'],
  457. 'mainField' => $sContentField,
  458. 'onField' => 'content',
  459. 'joinFields' => array(),//'initiator'),
  460. ),
  461. ),
  462. );
  463. }
  464. /**
  465. * Get necessary condition array to use connections in search classes
  466. * @param $sContentField content table field name
  467. * @param $iInitiator initiator of the connection
  468. * @param $iMutual get mutual connections only
  469. * @return array of conditions, for now with 'restriction' and 'join' parts
  470. */
  471. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  472. {
  473. return array(
  474. 'restriction' => array (
  475. 'connections_' . $this->_sObject => array(
  476. 'value' => $iContent,
  477. 'field' => 'content',
  478. 'operator' => '=',
  479. 'table' => $this->_aObject['table'],
  480. ),
  481. 'connections_mutual_' . $this->_sObject => array(
  482. 'value' => $iMutual,
  483. 'field' => 'mutual',
  484. 'operator' => '=',
  485. 'table' => $this->_aObject['table'],
  486. ),
  487. ),
  488. 'join' => array (
  489. 'connections_' . $this->_sObject => array(
  490. 'type' => 'INNER',
  491. 'table' => $this->_aObject['table'],
  492. 'mainField' => $sContentField,
  493. 'onField' => 'initiator',
  494. 'joinFields' => array(),//'initiator'),
  495. ),
  496. ),
  497. );
  498. }
  499. /**
  500. * Check if initiator and content are connected.
  501. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  502. * @param $iInitiator initiator of the connection
  503. * @param $iContent connected content or other profile id in case of friends
  504. * @return true - if content and initiator are connected or false - in all other cases
  505. */
  506. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  507. {
  508. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  509. if (!$oConnection)
  510. return false;
  511. return false === $isMutual ? true : $oConnection['mutual'];
  512. }
  513. /**
  514. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  515. * This method makes sense only when type of connection is mutual.
  516. * @param $iInitiator initiator of the connection
  517. * @param $iContent connected content or other profile id in case of friends
  518. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  519. */
  520. public function isConnectedNotMutual ($iInitiator, $iContent)
  521. {
  522. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  523. if (!$oConnection)
  524. return false;
  525. return $oConnection['mutual'] ? false : true;
  526. }
  527. /**
  528. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  529. * @param $iId which can be as conetnt ot initiator
  530. * @return true if some connections were deleted
  531. */
  532. public function onDeleteInitiatorAndContent ($iId)
  533. {
  534. $b = $this->onDeleteInitiator ($iId);
  535. $b = $this->onDeleteContent ($iId) || $b;
  536. return $b;
  537. }
  538. /**
  539. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  540. * @param $iIdInitiator initiator id
  541. * @return true if some connections were deleted
  542. */
  543. public function onDeleteInitiator ($iIdInitiator)
  544. {
  545. return $this->_oQuery->onDelete ($iIdInitiator, 'initiator');
  546. }
  547. /**
  548. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  549. * @param $iIdInitiator initiator id
  550. * @return true if some connections were deleted
  551. */
  552. public function onDeleteContent ($iIdContent)
  553. {
  554. return $this->_oQuery->onDelete ($iIdContent, 'content');
  555. }
  556. /**
  557. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  558. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  559. * @param $sTable table name with data which have assiciations with the connection
  560. * @param $sFieldId id field name which is associated with the connection
  561. * @return number of deleted connections
  562. */
  563. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  564. {
  565. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  566. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  567. return $iAffected;
  568. }
  569. /**
  570. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  571. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  572. * @param $sTable table name with data which have assiciations with the connection
  573. * @param $sFieldId id field name which is associated with the connection
  574. * @return number of deleted connections
  575. */
  576. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  577. {
  578. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  579. }
  580. /**
  581. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  582. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  583. * @param $sTable table name with data which have assiciations with the connection
  584. * @param $sFieldId id field name which is associated with the connection
  585. * @return number of deleted connections
  586. */
  587. public function onModuleDeleteContent ($sTable, $sFieldId)
  588. {
  589. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  590. }
  591. /**
  592. * 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.
  593. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  594. * @param $sModuleName module name to delete connections for
  595. * @return number of deleted connections
  596. */
  597. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  598. {
  599. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  600. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  601. return $iAffected;
  602. }
  603. /**
  604. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  605. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  606. * @param $sModuleName module name to delete connections for
  607. * @return number of deleted connections
  608. */
  609. public function onModuleProfileDeleteInitiator ($sModuleName)
  610. {
  611. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  612. }
  613. /**
  614. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  615. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  616. * @param $sModuleName module name to delete connections for
  617. * @return number of deleted connections
  618. */
  619. public function onModuleProfileDeleteContent ($sModuleName)
  620. {
  621. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  622. }
  623. }
  624. /** @} */