1
0

BxDolConnection.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. * No limit for connections lists.
  15. * Is needed for Total Number calculation.
  16. */
  17. define('BX_CONNECTIONS_LIST_NO_LIMIT', -1);
  18. /**
  19. * Connections order: no order
  20. */
  21. define('BX_CONNECTIONS_ORDER_NONE', 0);
  22. /**
  23. * Connections order: by addded time, asceding
  24. */
  25. define('BX_CONNECTIONS_ORDER_ADDED_ASC', 1);
  26. /**
  27. * Connections order: by addded time, desceding
  28. */
  29. define('BX_CONNECTIONS_ORDER_ADDED_DESC', 2);
  30. /**
  31. * Connection type: one-way
  32. */
  33. define('BX_CONNECTIONS_TYPE_ONE_WAY', 'one-way');
  34. /**
  35. * Connection type: mutual
  36. */
  37. define('BX_CONNECTIONS_TYPE_MUTUAL', 'mutual');
  38. /**
  39. * Connections content type: content
  40. */
  41. define('BX_CONNECTIONS_CONTENT_TYPE_CONTENT', 'content');
  42. /**
  43. * Connections content type: initiators
  44. */
  45. define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  46. /**
  47. * Connections content type: common
  48. */
  49. define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
  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. * Get connection type.
  142. * return BX_CONNECTIONS_TYPE_ONE_WAY or BX_CONNECTIONS_TYPE_MUTUAL
  143. */
  144. public function getType()
  145. {
  146. return $this->_sType;
  147. }
  148. /**
  149. * Check whether connection between Initiator and Content can be established.
  150. */
  151. public function checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false)
  152. {
  153. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  154. return _t('_sys_txt_access_denied');
  155. $oInitiator = BxDolProfile::getInstance($iInitiator);
  156. $oContent = BxDolProfile::getInstance($iContent);
  157. if(!$oInitiator || !$oContent)
  158. return _t('_sys_txt_access_denied');
  159. // check ACL
  160. $aCheck = checkActionModule($iInitiator, 'connect', 'system', $isPerformAction);
  161. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  162. return $aCheck[CHECK_ACTION_MESSAGE];
  163. if($isSwap)
  164. $isConnected = $this->isConnected($iContent, $iInitiator, $isMutual);
  165. else
  166. $isConnected = $this->isConnected($iInitiator, $iContent, $isMutual);
  167. if($isInvertResult)
  168. $isConnected = !$isConnected;
  169. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  170. }
  171. /**
  172. * Add new connection.
  173. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  174. * @return array
  175. */
  176. public function actionAdd ($iContent = 0, $iInitiator = false)
  177. {
  178. if (!$iContent)
  179. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  180. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  181. }
  182. /**
  183. * Remove connection. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  184. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  185. * @return array
  186. */
  187. public function actionRemove ($iContent = 0, $iInitiator = false)
  188. {
  189. if (!$iContent)
  190. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  191. if ($iContent != bx_get_logged_profile_id() && BX_CONNECTIONS_TYPE_MUTUAL == $this->_aObject['type']) {
  192. $a = $this->actionReject($iContent);
  193. if (false == $a['err'])
  194. return $a;
  195. }
  196. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  197. }
  198. /**
  199. * Reject connection request. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  200. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  201. * @return array
  202. */
  203. public function actionReject ($iContent = 0, $iInitiator = false)
  204. {
  205. if (!$iContent)
  206. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  207. return $this->_action ($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  208. }
  209. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false)
  210. {
  211. bx_import('BxDolLanguages');
  212. if (!$iContent || !$iInitiator)
  213. return array ('err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined'));
  214. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  215. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  216. return array ('err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation'));
  217. return array ('err' => true, 'msg' => _t($sErrorKey));
  218. }
  219. return array ('err' => false, 'msg' => _t('_sys_conn_msg_success'));
  220. }
  221. public function outputActionResult ($mixed, $sFormat = 'json')
  222. {
  223. switch ($sFormat) {
  224. case 'json':
  225. default:
  226. header('Content-Type: application/json; charset=utf-8');
  227. echo json_encode($mixed);
  228. }
  229. exit;
  230. }
  231. /**
  232. * Add new connection.
  233. * @param $iInitiator initiator of the connection, in most cases some profile id
  234. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  235. * @return true - if connection was added, false - if connection already exists or error occured
  236. */
  237. public function addConnection ($iInitiator, $iContent)
  238. {
  239. $iMutual = 0;
  240. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  241. return false;
  242. bx_alert($this->_sObject, 'connection_added', 0, bx_get_logged_profile_id(), array(
  243. 'initiator' => (int)$iInitiator,
  244. 'content' => (int)$iContent,
  245. 'mutual' => (int)$iMutual,
  246. 'object' => $this,
  247. ));
  248. $this->checkAllowedConnect ($iInitiator, $iContent, true, $iMutual, false);
  249. return true;
  250. }
  251. /**
  252. * Remove connection.
  253. * @param $iInitiator initiator of the connection
  254. * @param $iContent connected content or other profile id in case of friends
  255. * @return true - if connection was removed, false - if connection isn't exist or error occured
  256. */
  257. public function removeConnection ($iInitiator, $iContent)
  258. {
  259. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  260. return false;
  261. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  262. return false;
  263. bx_alert($this->_sObject, 'connection_removed', 0, bx_get_logged_profile_id(), array(
  264. 'initiator' => (int)$iInitiator,
  265. 'content' => (int)$iContent,
  266. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  267. 'object' => $this,
  268. ));
  269. return true;
  270. }
  271. /**
  272. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  273. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  274. * @param $iId1 one content or initiator
  275. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  276. * @param $isMutual get mutual connections only
  277. * @return array of available connections
  278. */
  279. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  280. {
  281. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  282. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  283. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  284. $sMethod = 'getConnectedInitiators';
  285. else
  286. $sMethod = 'getConnectedContent';
  287. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  288. }
  289. /**
  290. * Get common content IDs between two initiators
  291. * @param $iInitiator1 one initiator
  292. * @param $iInitiator2 second initiator
  293. * @param $isMutual get mutual connections only
  294. * @return array of available connections
  295. */
  296. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  297. {
  298. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  299. }
  300. /**
  301. * Get connected content count
  302. * @param $iInitiator initiator of the connection
  303. * @param $isMutual get mutual connections only
  304. * @return number of connections
  305. */
  306. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  307. {
  308. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual);
  309. }
  310. /**
  311. * Get connected initiators count
  312. * @param $iContent content of the connection
  313. * @param $isMutual get mutual connections only
  314. * @return number of connections
  315. */
  316. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  317. {
  318. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  319. }
  320. /**
  321. * Get connected content IDs
  322. * @param $iInitiator initiator of the connection
  323. * @param $isMutual get mutual connections only
  324. * @return array of available connections
  325. */
  326. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  327. {
  328. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  329. }
  330. /**
  331. * Get connected initiators IDs
  332. * @param $iContent content of the connection
  333. * @param $isMutual get mutual connections only
  334. * @return array of available connections
  335. */
  336. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  337. {
  338. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  339. }
  340. /**
  341. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  342. * @see getConnectionsAsArray
  343. */
  344. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  345. {
  346. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  347. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  348. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  349. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  350. else
  351. $sMethod = 'getConnectedContentAsSQLParts';
  352. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  353. }
  354. /**
  355. * Get necessary parts of SQL query to use connections in other queries
  356. * @param $sContentTable content table or alias
  357. * @param $sContentField content table field or field alias
  358. * @param $iInitiator initiator of the connection
  359. * @param $isMutual get mutual connections only
  360. * @return array of SQL string parts, for now 'join' part only is returned
  361. */
  362. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  363. {
  364. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  365. }
  366. /**
  367. * Get necessary parts of SQL query to use connections in other queries
  368. * @param $sContentTable content table or alias
  369. * @param $sContentField content table field or field alias
  370. * @param $iInitiator initiator of the connection
  371. * @param $isMutual get mutual connections only
  372. * @return array of SQL string parts, for now 'join' part only is returned
  373. */
  374. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  375. {
  376. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  377. }
  378. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  379. {
  380. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  381. }
  382. /**
  383. * Get necessary parts of SQL query to use connections in other queries
  384. * @param $sContentTable content table or alias
  385. * @param $sContentField content table field or field alias
  386. * @param $sInitiatorTable initiator table or alias
  387. * @param $sInitiatorField initiator table field or field alias
  388. * @param $iInitiator initiator of the connection
  389. * @param $isMutual get mutual connections only
  390. * @return array of SQL string parts, for now 'join' part only is returned
  391. */
  392. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  393. {
  394. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  395. }
  396. /**
  397. * Get necessary parts of SQL query to use connections in other queries
  398. * @param $sInitiatorTable initiator table or alias
  399. * @param $sInitiatorField initiator table field or field alias
  400. * @param $iContent content of the connection
  401. * @param $isMutual get mutual connections only
  402. * @return array of SQL string parts, for now 'join' part only is returned
  403. */
  404. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  405. {
  406. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  407. }
  408. /**
  409. * Get necessary parts of SQL query to use connections in other queries
  410. * @param $sInitiatorTable initiator table or alias
  411. * @param $sInitiatorField initiator table field or field alias
  412. * @param $sContentTable content table or alias
  413. * @param $sContentField content table field or field alias
  414. * @param $isMutual get mutual connections only
  415. * @return array of SQL string parts, for now 'join' part only is returned
  416. */
  417. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  418. {
  419. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  420. }
  421. /**
  422. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  423. * @see getConnectionsAsArray
  424. */
  425. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  426. {
  427. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  428. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  429. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  430. $sMethod = 'getConnectedInitiatorsAsCondition';
  431. else
  432. $sMethod = 'getConnectedContentAsCondition';
  433. return $this->$sMethod($sContentField, $iId1, $isMutual);
  434. }
  435. /**
  436. * Get necessary condition array to use connections in search classes
  437. * @param $sContentField content table field name
  438. * @param $iInitiator initiator of the connection
  439. * @param $iMutual get mutual connections only
  440. * @return array of conditions, for now with 'restriction' and 'join' parts
  441. */
  442. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  443. {
  444. return array(
  445. 'restriction' => array (
  446. 'connections_' . $this->_sObject => array(
  447. 'value' => $iInitiator1,
  448. 'field' => 'initiator',
  449. 'operator' => '=',
  450. 'table' => 'c',
  451. ),
  452. 'connections_mutual_' . $this->_sObject => array(
  453. 'value' => $iMutual,
  454. 'field' => 'mutual',
  455. 'operator' => '=',
  456. 'table' => 'c',
  457. ),
  458. 'connections2_' . $this->_sObject => array(
  459. 'value' => $iInitiator2,
  460. 'field' => 'initiator',
  461. 'operator' => '=',
  462. 'table' => 'c2',
  463. ),
  464. 'connections2_mutual_' . $this->_sObject => array(
  465. 'value' => $iMutual,
  466. 'field' => 'mutual',
  467. 'operator' => '=',
  468. 'table' => 'c2',
  469. ),
  470. ),
  471. 'join' => array (
  472. 'connections_' . $this->_sObject => array(
  473. 'type' => 'INNER',
  474. 'table' => $this->_aObject['table'],
  475. 'table_alias' => 'c',
  476. 'mainField' => $sContentField,
  477. 'onField' => 'content',
  478. 'joinFields' => array(),
  479. ),
  480. 'connections2_' . $this->_sObject => array(
  481. 'type' => 'INNER',
  482. 'table' => $this->_aObject['table'],
  483. 'table_alias' => 'c2',
  484. 'mainTable' => 'c',
  485. 'mainField' => 'content',
  486. 'onField' => 'content',
  487. 'joinFields' => array(),
  488. ),
  489. ),
  490. );
  491. }
  492. /**
  493. * Get necessary condition array to use connections in search classes
  494. * @param $sContentField content table field name
  495. * @param $iInitiator initiator of the connection
  496. * @param $iMutual get mutual connections only
  497. * @return array of conditions, for now with 'restriction' and 'join' parts
  498. */
  499. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  500. {
  501. return array(
  502. 'restriction' => array (
  503. 'connections_' . $this->_sObject => array(
  504. 'value' => $iInitiator,
  505. 'field' => 'initiator',
  506. 'operator' => '=',
  507. 'table' => $this->_aObject['table'],
  508. ),
  509. 'connections_mutual_' . $this->_sObject => array(
  510. 'value' => $iMutual,
  511. 'field' => 'mutual',
  512. 'operator' => '=',
  513. 'table' => $this->_aObject['table'],
  514. ),
  515. ),
  516. 'join' => array (
  517. 'connections_' . $this->_sObject => array(
  518. 'type' => 'INNER',
  519. 'table' => $this->_aObject['table'],
  520. 'mainField' => $sContentField,
  521. 'onField' => 'content',
  522. 'joinFields' => array(),//'initiator'),
  523. ),
  524. ),
  525. );
  526. }
  527. /**
  528. * Get necessary condition array to use connections in search classes
  529. * @param $sContentField content table field name
  530. * @param $iInitiator initiator of the connection
  531. * @param $iMutual get mutual connections only
  532. * @return array of conditions, for now with 'restriction' and 'join' parts
  533. */
  534. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  535. {
  536. return array(
  537. 'restriction' => array (
  538. 'connections_' . $this->_sObject => array(
  539. 'value' => $iContent,
  540. 'field' => 'content',
  541. 'operator' => '=',
  542. 'table' => $this->_aObject['table'],
  543. ),
  544. 'connections_mutual_' . $this->_sObject => array(
  545. 'value' => $iMutual,
  546. 'field' => 'mutual',
  547. 'operator' => '=',
  548. 'table' => $this->_aObject['table'],
  549. ),
  550. ),
  551. 'join' => array (
  552. 'connections_' . $this->_sObject => array(
  553. 'type' => 'INNER',
  554. 'table' => $this->_aObject['table'],
  555. 'mainField' => $sContentField,
  556. 'onField' => 'initiator',
  557. 'joinFields' => array(),//'initiator'),
  558. ),
  559. ),
  560. );
  561. }
  562. /**
  563. * Check if initiator and content are connected.
  564. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  565. * @param $iInitiator initiator of the connection
  566. * @param $iContent connected content or other profile id in case of friends
  567. * @return true - if content and initiator are connected or false - in all other cases
  568. */
  569. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  570. {
  571. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  572. if (!$oConnection)
  573. return false;
  574. return false === $isMutual ? true : $oConnection['mutual'];
  575. }
  576. /**
  577. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  578. * This method makes sense only when type of connection is mutual.
  579. * @param $iInitiator initiator of the connection
  580. * @param $iContent connected content or other profile id in case of friends
  581. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  582. */
  583. public function isConnectedNotMutual ($iInitiator, $iContent)
  584. {
  585. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  586. if (!$oConnection)
  587. return false;
  588. return $oConnection['mutual'] ? false : true;
  589. }
  590. /**
  591. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  592. * @param $iId which can be as conetnt ot initiator
  593. * @return true if some connections were deleted
  594. */
  595. public function onDeleteInitiatorAndContent ($iId)
  596. {
  597. $b = $this->onDeleteInitiator ($iId);
  598. $b = $this->onDeleteContent ($iId) || $b;
  599. return $b;
  600. }
  601. /**
  602. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  603. * @param $iIdInitiator initiator id
  604. * @return true if some connections were deleted
  605. */
  606. public function onDeleteInitiator ($iIdInitiator)
  607. {
  608. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  609. return false;
  610. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  611. 'initiator' => (int)$iIdInitiator,
  612. 'object' => $this,
  613. ));
  614. return true;
  615. }
  616. /**
  617. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  618. * @param $iIdInitiator initiator id
  619. * @return true if some connections were deleted
  620. */
  621. public function onDeleteContent ($iIdContent)
  622. {
  623. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  624. return false;
  625. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  626. 'content' => (int)$iIdContent,
  627. 'object' => $this,
  628. ));
  629. return true;
  630. }
  631. /**
  632. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  633. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  634. * @param $sTable table name with data which have assiciations with the connection
  635. * @param $sFieldId id field name which is associated with the connection
  636. * @return number of deleted connections
  637. */
  638. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  639. {
  640. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  641. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  642. return $iAffected;
  643. }
  644. /**
  645. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  646. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  647. * @param $sTable table name with data which have assiciations with the connection
  648. * @param $sFieldId id field name which is associated with the connection
  649. * @return number of deleted connections
  650. */
  651. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  652. {
  653. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  654. }
  655. /**
  656. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  657. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  658. * @param $sTable table name with data which have assiciations with the connection
  659. * @param $sFieldId id field name which is associated with the connection
  660. * @return number of deleted connections
  661. */
  662. public function onModuleDeleteContent ($sTable, $sFieldId)
  663. {
  664. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  665. }
  666. /**
  667. * 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.
  668. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  669. * @param $sModuleName module name to delete connections for
  670. * @return number of deleted connections
  671. */
  672. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  673. {
  674. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  675. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  676. return $iAffected;
  677. }
  678. /**
  679. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  680. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  681. * @param $sModuleName module name to delete connections for
  682. * @return number of deleted connections
  683. */
  684. public function onModuleProfileDeleteInitiator ($sModuleName)
  685. {
  686. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  687. }
  688. /**
  689. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  690. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  691. * @param $sModuleName module name to delete connections for
  692. * @return number of deleted connections
  693. */
  694. public function onModuleProfileDeleteContent ($sModuleName)
  695. {
  696. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  697. }
  698. }
  699. /** @} */