BxDolConnection.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. return true;
  249. }
  250. /**
  251. * Remove connection.
  252. * @param $iInitiator initiator of the connection
  253. * @param $iContent connected content or other profile id in case of friends
  254. * @return true - if connection was removed, false - if connection isn't exist or error occured
  255. */
  256. public function removeConnection ($iInitiator, $iContent)
  257. {
  258. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  259. return false;
  260. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  261. return false;
  262. bx_alert($this->_sObject, 'connection_removed', 0, bx_get_logged_profile_id(), array(
  263. 'initiator' => (int)$iInitiator,
  264. 'content' => (int)$iContent,
  265. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  266. 'object' => $this,
  267. ));
  268. return true;
  269. }
  270. /**
  271. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  272. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  273. * @param $iId1 one content or initiator
  274. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  275. * @param $isMutual get mutual connections only
  276. * @return array of available connections
  277. */
  278. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  279. {
  280. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  281. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  282. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  283. $sMethod = 'getConnectedInitiators';
  284. else
  285. $sMethod = 'getConnectedContent';
  286. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  287. }
  288. /**
  289. * Get common content IDs between two initiators
  290. * @param $iInitiator1 one initiator
  291. * @param $iInitiator2 second initiator
  292. * @param $isMutual get mutual connections only
  293. * @return array of available connections
  294. */
  295. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  296. {
  297. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  298. }
  299. /**
  300. * Get connected content count
  301. * @param $iInitiator initiator of the connection
  302. * @param $isMutual get mutual connections only
  303. * @return number of connections
  304. */
  305. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  306. {
  307. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual);
  308. }
  309. /**
  310. * Get connected initiators count
  311. * @param $iContent content of the connection
  312. * @param $isMutual get mutual connections only
  313. * @return number of connections
  314. */
  315. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  316. {
  317. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  318. }
  319. /**
  320. * Get connected content IDs
  321. * @param $iInitiator initiator of the connection
  322. * @param $isMutual get mutual connections only
  323. * @return array of available connections
  324. */
  325. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  326. {
  327. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  328. }
  329. /**
  330. * Get connected initiators IDs
  331. * @param $iContent content of the connection
  332. * @param $isMutual get mutual connections only
  333. * @return array of available connections
  334. */
  335. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  336. {
  337. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  338. }
  339. /**
  340. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  341. * @see getConnectionsAsArray
  342. */
  343. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  344. {
  345. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  346. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  347. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  348. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  349. else
  350. $sMethod = 'getConnectedContentAsSQLParts';
  351. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  352. }
  353. /**
  354. * Get necessary parts of SQL query to use connections in other queries
  355. * @param $sContentTable content table or alias
  356. * @param $sContentField content table field or field alias
  357. * @param $iInitiator initiator of the connection
  358. * @param $isMutual get mutual connections only
  359. * @return array of SQL string parts, for now 'join' part only is returned
  360. */
  361. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  362. {
  363. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  364. }
  365. /**
  366. * Get necessary parts of SQL query to use connections in other queries
  367. * @param $sContentTable content table or alias
  368. * @param $sContentField content table field or field alias
  369. * @param $iInitiator initiator of the connection
  370. * @param $isMutual get mutual connections only
  371. * @return array of SQL string parts, for now 'join' part only is returned
  372. */
  373. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  374. {
  375. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  376. }
  377. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  378. {
  379. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  380. }
  381. /**
  382. * Get necessary parts of SQL query to use connections in other queries
  383. * @param $sContentTable content table or alias
  384. * @param $sContentField content table field or field alias
  385. * @param $sInitiatorTable initiator table or alias
  386. * @param $sInitiatorField initiator table field or field alias
  387. * @param $iInitiator initiator of the connection
  388. * @param $isMutual get mutual connections only
  389. * @return array of SQL string parts, for now 'join' part only is returned
  390. */
  391. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  392. {
  393. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  394. }
  395. /**
  396. * Get necessary parts of SQL query to use connections in other queries
  397. * @param $sInitiatorTable initiator table or alias
  398. * @param $sInitiatorField initiator table field or field alias
  399. * @param $iContent content of the connection
  400. * @param $isMutual get mutual connections only
  401. * @return array of SQL string parts, for now 'join' part only is returned
  402. */
  403. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  404. {
  405. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  406. }
  407. /**
  408. * Get necessary parts of SQL query to use connections in other queries
  409. * @param $sInitiatorTable initiator table or alias
  410. * @param $sInitiatorField initiator table field or field alias
  411. * @param $sContentTable content table or alias
  412. * @param $sContentField content table field or field alias
  413. * @param $isMutual get mutual connections only
  414. * @return array of SQL string parts, for now 'join' part only is returned
  415. */
  416. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  417. {
  418. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  419. }
  420. /**
  421. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  422. * @see getConnectionsAsArray
  423. */
  424. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  425. {
  426. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  427. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  428. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  429. $sMethod = 'getConnectedInitiatorsAsCondition';
  430. else
  431. $sMethod = 'getConnectedContentAsCondition';
  432. return $this->$sMethod($sContentField, $iId1, $isMutual);
  433. }
  434. /**
  435. * Get necessary condition array to use connections in search classes
  436. * @param $sContentField content table field name
  437. * @param $iInitiator initiator of the connection
  438. * @param $iMutual get mutual connections only
  439. * @return array of conditions, for now with 'restriction' and 'join' parts
  440. */
  441. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  442. {
  443. return array(
  444. 'restriction' => array (
  445. 'connections_' . $this->_sObject => array(
  446. 'value' => $iInitiator1,
  447. 'field' => 'initiator',
  448. 'operator' => '=',
  449. 'table' => 'c',
  450. ),
  451. 'connections_mutual_' . $this->_sObject => array(
  452. 'value' => $iMutual,
  453. 'field' => 'mutual',
  454. 'operator' => '=',
  455. 'table' => 'c',
  456. ),
  457. 'connections2_' . $this->_sObject => array(
  458. 'value' => $iInitiator2,
  459. 'field' => 'initiator',
  460. 'operator' => '=',
  461. 'table' => 'c2',
  462. ),
  463. 'connections2_mutual_' . $this->_sObject => array(
  464. 'value' => $iMutual,
  465. 'field' => 'mutual',
  466. 'operator' => '=',
  467. 'table' => 'c2',
  468. ),
  469. ),
  470. 'join' => array (
  471. 'connections_' . $this->_sObject => array(
  472. 'type' => 'INNER',
  473. 'table' => $this->_aObject['table'],
  474. 'table_alias' => 'c',
  475. 'mainField' => $sContentField,
  476. 'onField' => 'content',
  477. 'joinFields' => array(),
  478. ),
  479. 'connections2_' . $this->_sObject => array(
  480. 'type' => 'INNER',
  481. 'table' => $this->_aObject['table'],
  482. 'table_alias' => 'c2',
  483. 'mainTable' => 'c',
  484. 'mainField' => 'content',
  485. 'onField' => 'content',
  486. 'joinFields' => array(),
  487. ),
  488. ),
  489. );
  490. }
  491. /**
  492. * Get necessary condition array to use connections in search classes
  493. * @param $sContentField content table field name
  494. * @param $iInitiator initiator of the connection
  495. * @param $iMutual get mutual connections only
  496. * @return array of conditions, for now with 'restriction' and 'join' parts
  497. */
  498. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  499. {
  500. return array(
  501. 'restriction' => array (
  502. 'connections_' . $this->_sObject => array(
  503. 'value' => $iInitiator,
  504. 'field' => 'initiator',
  505. 'operator' => '=',
  506. 'table' => $this->_aObject['table'],
  507. ),
  508. 'connections_mutual_' . $this->_sObject => array(
  509. 'value' => $iMutual,
  510. 'field' => 'mutual',
  511. 'operator' => '=',
  512. 'table' => $this->_aObject['table'],
  513. ),
  514. ),
  515. 'join' => array (
  516. 'connections_' . $this->_sObject => array(
  517. 'type' => 'INNER',
  518. 'table' => $this->_aObject['table'],
  519. 'mainField' => $sContentField,
  520. 'onField' => 'content',
  521. 'joinFields' => array(),//'initiator'),
  522. ),
  523. ),
  524. );
  525. }
  526. /**
  527. * Get necessary condition array to use connections in search classes
  528. * @param $sContentField content table field name
  529. * @param $iInitiator initiator of the connection
  530. * @param $iMutual get mutual connections only
  531. * @return array of conditions, for now with 'restriction' and 'join' parts
  532. */
  533. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  534. {
  535. return array(
  536. 'restriction' => array (
  537. 'connections_' . $this->_sObject => array(
  538. 'value' => $iContent,
  539. 'field' => 'content',
  540. 'operator' => '=',
  541. 'table' => $this->_aObject['table'],
  542. ),
  543. 'connections_mutual_' . $this->_sObject => array(
  544. 'value' => $iMutual,
  545. 'field' => 'mutual',
  546. 'operator' => '=',
  547. 'table' => $this->_aObject['table'],
  548. ),
  549. ),
  550. 'join' => array (
  551. 'connections_' . $this->_sObject => array(
  552. 'type' => 'INNER',
  553. 'table' => $this->_aObject['table'],
  554. 'mainField' => $sContentField,
  555. 'onField' => 'initiator',
  556. 'joinFields' => array(),//'initiator'),
  557. ),
  558. ),
  559. );
  560. }
  561. /**
  562. * Check if initiator and content are connected.
  563. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  564. * @param $iInitiator initiator of the connection
  565. * @param $iContent connected content or other profile id in case of friends
  566. * @return true - if content and initiator are connected or false - in all other cases
  567. */
  568. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  569. {
  570. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  571. if (!$oConnection)
  572. return false;
  573. return false === $isMutual ? true : $oConnection['mutual'];
  574. }
  575. /**
  576. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  577. * This method makes sense only when type of connection is mutual.
  578. * @param $iInitiator initiator of the connection
  579. * @param $iContent connected content or other profile id in case of friends
  580. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  581. */
  582. public function isConnectedNotMutual ($iInitiator, $iContent)
  583. {
  584. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  585. if (!$oConnection)
  586. return false;
  587. return $oConnection['mutual'] ? false : true;
  588. }
  589. /**
  590. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  591. * @param $iId which can be as conetnt ot initiator
  592. * @return true if some connections were deleted
  593. */
  594. public function onDeleteInitiatorAndContent ($iId)
  595. {
  596. $b = $this->onDeleteInitiator ($iId);
  597. $b = $this->onDeleteContent ($iId) || $b;
  598. return $b;
  599. }
  600. /**
  601. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  602. * @param $iIdInitiator initiator id
  603. * @return true if some connections were deleted
  604. */
  605. public function onDeleteInitiator ($iIdInitiator)
  606. {
  607. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  608. return false;
  609. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  610. 'initiator' => (int)$iIdInitiator,
  611. 'object' => $this,
  612. ));
  613. return true;
  614. }
  615. /**
  616. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  617. * @param $iIdInitiator initiator id
  618. * @return true if some connections were deleted
  619. */
  620. public function onDeleteContent ($iIdContent)
  621. {
  622. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  623. return false;
  624. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  625. 'content' => (int)$iIdContent,
  626. 'object' => $this,
  627. ));
  628. return true;
  629. }
  630. /**
  631. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  632. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  633. * @param $sTable table name with data which have assiciations with the connection
  634. * @param $sFieldId id field name which is associated with the connection
  635. * @return number of deleted connections
  636. */
  637. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  638. {
  639. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  640. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  641. return $iAffected;
  642. }
  643. /**
  644. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  645. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  646. * @param $sTable table name with data which have assiciations with the connection
  647. * @param $sFieldId id field name which is associated with the connection
  648. * @return number of deleted connections
  649. */
  650. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  651. {
  652. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  653. }
  654. /**
  655. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  656. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  657. * @param $sTable table name with data which have assiciations with the connection
  658. * @param $sFieldId id field name which is associated with the connection
  659. * @return number of deleted connections
  660. */
  661. public function onModuleDeleteContent ($sTable, $sFieldId)
  662. {
  663. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  664. }
  665. /**
  666. * 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.
  667. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  668. * @param $sModuleName module name to delete connections for
  669. * @return number of deleted connections
  670. */
  671. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  672. {
  673. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  674. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  675. return $iAffected;
  676. }
  677. /**
  678. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  679. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  680. * @param $sModuleName module name to delete connections for
  681. * @return number of deleted connections
  682. */
  683. public function onModuleProfileDeleteInitiator ($sModuleName)
  684. {
  685. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  686. }
  687. /**
  688. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  689. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  690. * @param $sModuleName module name to delete connections for
  691. * @return number of deleted connections
  692. */
  693. public function onModuleProfileDeleteContent ($sModuleName)
  694. {
  695. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  696. }
  697. }
  698. /** @} */