BxDolConnection.php 33 KB

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