BxDolConnection.php 32 KB

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