BxDolConnection.php 31 KB

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