BxDolConnection.php 29 KB

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