BxDolConnection.php 33 KB

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