BxDolConnection.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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 list in Counter.
  11. */
  12. define('BX_CONNECTIONS_LIST_COUNTER', 5);
  13. /**
  14. * Default limit for connections lists
  15. */
  16. define('BX_CONNECTIONS_LIST_LIMIT', 1000);
  17. /**
  18. * No limit for connections lists.
  19. * Is needed for Total Number calculation.
  20. */
  21. define('BX_CONNECTIONS_LIST_NO_LIMIT', -1);
  22. /**
  23. * Connections order: no order
  24. */
  25. define('BX_CONNECTIONS_ORDER_NONE', 0);
  26. /**
  27. * Connections order: by addded time, asceding
  28. */
  29. define('BX_CONNECTIONS_ORDER_ADDED_ASC', 1);
  30. /**
  31. * Connections order: by addded time, desceding
  32. */
  33. define('BX_CONNECTIONS_ORDER_ADDED_DESC', 2);
  34. /**
  35. * Connection type: one-way
  36. */
  37. define('BX_CONNECTIONS_TYPE_ONE_WAY', 'one-way');
  38. /**
  39. * Connection type: mutual
  40. */
  41. define('BX_CONNECTIONS_TYPE_MUTUAL', 'mutual');
  42. /**
  43. * Connections content type: content
  44. */
  45. define('BX_CONNECTIONS_CONTENT_TYPE_CONTENT', 'content');
  46. /**
  47. * Connections content type: initiators
  48. */
  49. define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  50. /**
  51. * Connections content type: common
  52. */
  53. define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
  54. /**
  55. * Connection is usefull when you need to organize some sorts of connections between different content,
  56. * for example: friends, contacts, favorites, block lists, subscriptions, etc.
  57. *
  58. * Two types of connections are supported one way connections (block list, favourites) and mutual (friends).
  59. *
  60. * For automatic handling of connections (like, add/remove connection in frontend) refer to JS function: @see bx_conn_action()
  61. *
  62. * @section connection_create Creating the Connection object:
  63. *
  64. * Step 1:
  65. * Add record to 'sys_objects_connection' table:
  66. * - object: name of the connection object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  67. * for example: bx_blogs_favorites - favorite blogs for users in blogs module.
  68. * - table: table name with connections, see step 2
  69. * - type: 'one-way' or 'mutual'
  70. * - override_class_name: user defined class name which is derived from one of base classes.
  71. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  72. *
  73. * Step 2:
  74. * Create table for connections:
  75. * @code
  76. * CREATE TABLE `my_sample_connections` (
  77. * `id` int(11) NOT NULL AUTO_INCREMENT,
  78. * `initiator` int(11) NOT NULL,
  79. * `content` int(11) NOT NULL,
  80. * `mutual` tinyint(4) NOT NULL, -- can re removed for one-way connections
  81. * `added` int(10) unsigned NOT NULL,
  82. * PRIMARY KEY (`id`),
  83. * UNIQUE KEY `initiator` (`initiator`,`content`),
  84. * KEY `content` (`content`)
  85. * )
  86. * @endcode
  87. *
  88. *
  89. * @section example Example of usage
  90. *
  91. * Check if two profiles are friends:
  92. * @code
  93. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  94. * if ($oConnectionFriends) // check if connections is available for using
  95. * 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
  96. * @endcode
  97. *
  98. * Get mutual content IDs (friends IDs)
  99. * @code
  100. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  101. * if ($oConnectionFriends) // check if connections is available for using
  102. * print_r($oConnection->getConnectedContent(100, 1)); // print array of friends IDs of 100's profile
  103. * @endcode
  104. *
  105. */
  106. class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
  107. {
  108. protected $_sObject;
  109. protected $_aObject;
  110. protected $_oQuery;
  111. protected $_sType;
  112. /**
  113. * Constructor
  114. * @param $aObject array of connection options
  115. */
  116. protected function __construct($aObject)
  117. {
  118. parent::__construct();
  119. $this->_sObject = $aObject['object'];
  120. $this->_aObject = $aObject;
  121. $this->_sType = $aObject['type'];
  122. $this->_oQuery = new BxDolConnectionQuery($aObject);
  123. }
  124. /**
  125. * Get connection object instance by object name
  126. * @param $sObject object name
  127. * @return object instance or false on error
  128. */
  129. static public function getObjectInstance($sObject)
  130. {
  131. if (!$sObject)
  132. return false;
  133. if (isset($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject]))
  134. return $GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject];
  135. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  136. if (!$aObject || !is_array($aObject))
  137. return false;
  138. $sClass = empty($aObject['override_class_name']) ? 'BxTemplConnection' : $aObject['override_class_name'];
  139. if (!empty($aObject['override_class_file']))
  140. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  141. $o = new $sClass($aObject);
  142. return ($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject] = $o);
  143. }
  144. /**
  145. * Get connection type.
  146. * return BX_CONNECTIONS_TYPE_ONE_WAY or BX_CONNECTIONS_TYPE_MUTUAL
  147. */
  148. public function getType()
  149. {
  150. return $this->_sType;
  151. }
  152. /**
  153. * Check whether connection between Initiator and Content can be established.
  154. */
  155. public function checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false)
  156. {
  157. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  158. return _t('_sys_txt_access_denied');
  159. $oInitiator = BxDolProfile::getInstance($iInitiator);
  160. $oContent = BxDolProfile::getInstance($iContent);
  161. if(!$oInitiator || !$oContent)
  162. return _t('_sys_txt_access_denied');
  163. // check ACL
  164. $aCheck = checkActionModule($iInitiator, 'connect', 'system', $isPerformAction);
  165. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  166. return $aCheck[CHECK_ACTION_MESSAGE];
  167. if($isSwap)
  168. $isConnected = $this->isConnected($iContent, $iInitiator, $isMutual);
  169. else
  170. $isConnected = $this->isConnected($iInitiator, $iContent, $isMutual);
  171. if($isInvertResult)
  172. $isConnected = !$isConnected;
  173. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  174. }
  175. /**
  176. * Add new connection.
  177. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  178. * @return array
  179. */
  180. public function actionAdd ($iContent = 0, $iInitiator = false)
  181. {
  182. if (!$iContent)
  183. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  184. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  185. }
  186. /**
  187. * Remove connection. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  188. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  189. * @return array
  190. */
  191. public function actionRemove ($iContent = 0, $iInitiator = false)
  192. {
  193. if (!$iContent)
  194. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  195. if ($iContent != bx_get_logged_profile_id() && BX_CONNECTIONS_TYPE_MUTUAL == $this->_aObject['type']) {
  196. $a = $this->actionReject($iContent);
  197. if (false == $a['err'])
  198. return $a;
  199. }
  200. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  201. }
  202. /**
  203. * Reject connection request. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  204. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  205. * @return array
  206. */
  207. public function actionReject ($iContent = 0, $iInitiator = false)
  208. {
  209. if (!$iContent)
  210. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  211. return $this->_action ($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  212. }
  213. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false)
  214. {
  215. bx_import('BxDolLanguages');
  216. if (!$iContent || !$iInitiator)
  217. return array ('err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined'));
  218. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  219. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  220. return array ('err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation'));
  221. return array ('err' => true, 'msg' => _t($sErrorKey));
  222. }
  223. return array ('err' => false, 'msg' => _t('_sys_conn_msg_success'));
  224. }
  225. public function outputActionResult ($mixed, $sFormat = 'json')
  226. {
  227. switch ($sFormat) {
  228. case 'html':
  229. echo $mixed;
  230. break;
  231. case 'json':
  232. default:
  233. header('Content-Type: application/json; charset=utf-8');
  234. echo json_encode($mixed);
  235. }
  236. exit;
  237. }
  238. /**
  239. * Add new connection.
  240. * @param $iInitiator initiator of the connection, in most cases some profile id
  241. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  242. * @return true - if connection was added, false - if connection already exists or error occured
  243. */
  244. public function addConnection ($iInitiator, $iContent, $aParams = array())
  245. {
  246. $iMutual = 0;
  247. $iInitiator = (int)$iInitiator;
  248. $iContent = (int)$iContent;
  249. $aAlertExtras = array(
  250. 'initiator' => &$iInitiator,
  251. 'content' => &$iContent,
  252. 'mutual' => &$iMutual,
  253. 'object' => $this,
  254. );
  255. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  256. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  257. bx_alert($this->_sObject, 'connection_before_add', 0, bx_get_logged_profile_id(), $aAlertExtras);
  258. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  259. return false;
  260. bx_alert($this->_sObject, 'connection_added', 0, bx_get_logged_profile_id(), $aAlertExtras);
  261. $this->checkAllowedConnect ($iInitiator, $iContent, true, $iMutual, false);
  262. return true;
  263. }
  264. /**
  265. * Remove connection.
  266. * @param $iInitiator initiator of the connection
  267. * @param $iContent connected content or other profile id in case of friends
  268. * @return true - if connection was removed, false - if connection isn't exist or error occured
  269. */
  270. public function removeConnection ($iInitiator, $iContent)
  271. {
  272. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  273. return false;
  274. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  275. return false;
  276. bx_alert($this->_sObject, 'connection_removed', 0, bx_get_logged_profile_id(), array(
  277. 'initiator' => (int)$iInitiator,
  278. 'content' => (int)$iContent,
  279. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  280. 'object' => $this,
  281. ));
  282. return true;
  283. }
  284. /**
  285. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  286. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  287. * @param $iId1 one content or initiator
  288. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  289. * @param $isMutual get mutual connections only
  290. * @return array of available connections
  291. */
  292. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  293. {
  294. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  295. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  296. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  297. $sMethod = 'getConnectedInitiators';
  298. else
  299. $sMethod = 'getConnectedContent';
  300. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  301. }
  302. /**
  303. * Get common content IDs between two initiators
  304. * @param $iInitiator1 one initiator
  305. * @param $iInitiator2 second initiator
  306. * @param $isMutual get mutual connections only
  307. * @return array of available connections
  308. */
  309. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  310. {
  311. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  312. }
  313. /**
  314. * Get connected content count
  315. * @param $iInitiator initiator of the connection
  316. * @param $isMutual get mutual connections only
  317. * @return number of connections
  318. */
  319. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  320. {
  321. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual);
  322. }
  323. /**
  324. * Get connected initiators count
  325. * @param $iContent content of the connection
  326. * @param $isMutual get mutual connections only
  327. * @return number of connections
  328. */
  329. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  330. {
  331. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  332. }
  333. /**
  334. * Get connected content IDs
  335. * @param $iInitiator initiator of the connection
  336. * @param $isMutual get mutual connections only
  337. * @return array of available connections
  338. */
  339. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  340. {
  341. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  342. }
  343. /**
  344. * Get connected content IDs for specified type
  345. * @param $iInitiator initiator of the connection
  346. * @param $mixedType type of content or an array of types
  347. * @param $isMutual get mutual connections only
  348. * @return array of available connections
  349. */
  350. public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  351. {
  352. return $this->_oQuery->getConnectedContentByType($iInitiator, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  353. }
  354. /**
  355. * Get connected initiators IDs
  356. * @param $iContent content of the connection
  357. * @param $isMutual get mutual connections only
  358. * @return array of available connections
  359. */
  360. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  361. {
  362. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  363. }
  364. /**
  365. * Get connected initiators IDs
  366. * @param $iContent content of the connection
  367. * @param $mixedType type of content or an array of types
  368. * @param $isMutual get mutual connections only
  369. * @return array of available connections
  370. */
  371. public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  372. {
  373. return $this->_oQuery->getConnectedInitiatorsByType($iContent, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  374. }
  375. /**
  376. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  377. * @see getConnectionsAsArray
  378. */
  379. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  380. {
  381. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  382. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  383. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  384. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  385. else
  386. $sMethod = 'getConnectedContentAsSQLParts';
  387. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  388. }
  389. /**
  390. * Get necessary parts of SQL query to use connections in other queries
  391. * @param $sContentTable content table or alias
  392. * @param $sContentField content table field or field alias
  393. * @param $iInitiator initiator of the connection
  394. * @param $isMutual get mutual connections only
  395. * @return array of SQL string parts, for now 'join' part only is returned
  396. */
  397. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  398. {
  399. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  400. }
  401. /**
  402. * Get necessary parts of SQL query to use connections in other queries
  403. * @param $sContentTable content table or alias
  404. * @param $sContentField content table field or field alias
  405. * @param $iInitiator initiator of the connection
  406. * @param $isMutual get mutual connections only
  407. * @return array of SQL string parts, for now 'join' part only is returned
  408. */
  409. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  410. {
  411. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  412. }
  413. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  414. {
  415. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  416. }
  417. /**
  418. * Get necessary parts of SQL query to use connections in other queries
  419. * @param $sContentTable content table or alias
  420. * @param $sContentField content table field or field alias
  421. * @param $sInitiatorTable initiator table or alias
  422. * @param $sInitiatorField initiator table field or field alias
  423. * @param $iInitiator initiator of the connection
  424. * @param $isMutual get mutual connections only
  425. * @return array of SQL string parts, for now 'join' part only is returned
  426. */
  427. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  428. {
  429. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  430. }
  431. /**
  432. * Get necessary parts of SQL query to use connections in other queries
  433. * @param $sInitiatorTable initiator table or alias
  434. * @param $sInitiatorField initiator table field or field alias
  435. * @param $iContent content of the connection
  436. * @param $isMutual get mutual connections only
  437. * @return array of SQL string parts, for now 'join' part only is returned
  438. */
  439. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  440. {
  441. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  442. }
  443. /**
  444. * Get necessary parts of SQL query to use connections in other queries
  445. * @param $sInitiatorTable initiator table or alias
  446. * @param $sInitiatorField initiator table field or field alias
  447. * @param $sContentTable content table or alias
  448. * @param $sContentField content table field or field alias
  449. * @param $isMutual get mutual connections only
  450. * @return array of SQL string parts, for now 'join' part only is returned
  451. */
  452. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  453. {
  454. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  455. }
  456. /**
  457. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  458. * @see getConnectionsAsArray
  459. */
  460. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  461. {
  462. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  463. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  464. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  465. $sMethod = 'getConnectedInitiatorsAsCondition';
  466. else
  467. $sMethod = 'getConnectedContentAsCondition';
  468. return $this->$sMethod($sContentField, $iId1, $isMutual);
  469. }
  470. /**
  471. * Get necessary condition array to use connections in search classes
  472. * @param $sContentField content table field name
  473. * @param $iInitiator initiator of the connection
  474. * @param $iMutual get mutual connections only
  475. * @return array of conditions, for now with 'restriction' and 'join' parts
  476. */
  477. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  478. {
  479. return array(
  480. 'restriction' => array (
  481. 'connections_' . $this->_sObject => array(
  482. 'value' => $iInitiator1,
  483. 'field' => 'initiator',
  484. 'operator' => '=',
  485. 'table' => 'c',
  486. ),
  487. 'connections_mutual_' . $this->_sObject => array(
  488. 'value' => $iMutual,
  489. 'field' => 'mutual',
  490. 'operator' => '=',
  491. 'table' => 'c',
  492. ),
  493. 'connections2_' . $this->_sObject => array(
  494. 'value' => $iInitiator2,
  495. 'field' => 'initiator',
  496. 'operator' => '=',
  497. 'table' => 'c2',
  498. ),
  499. 'connections2_mutual_' . $this->_sObject => array(
  500. 'value' => $iMutual,
  501. 'field' => 'mutual',
  502. 'operator' => '=',
  503. 'table' => 'c2',
  504. ),
  505. ),
  506. 'join' => array (
  507. 'connections_' . $this->_sObject => array(
  508. 'type' => 'INNER',
  509. 'table' => $this->_aObject['table'],
  510. 'table_alias' => 'c',
  511. 'mainField' => $sContentField,
  512. 'onField' => 'content',
  513. 'joinFields' => array(),
  514. ),
  515. 'connections2_' . $this->_sObject => array(
  516. 'type' => 'INNER',
  517. 'table' => $this->_aObject['table'],
  518. 'table_alias' => 'c2',
  519. 'mainTable' => 'c',
  520. 'mainField' => 'content',
  521. 'onField' => 'content',
  522. 'joinFields' => array(),
  523. ),
  524. ),
  525. );
  526. }
  527. /**
  528. * Get necessary condition array to use connections in search classes
  529. * @param $sContentField content table field name
  530. * @param $iInitiator initiator of the connection
  531. * @param $iMutual get mutual connections only
  532. * @return array of conditions, for now with 'restriction' and 'join' parts
  533. */
  534. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  535. {
  536. return array(
  537. 'restriction' => array (
  538. 'connections_' . $this->_sObject => array(
  539. 'value' => $iInitiator,
  540. 'field' => 'initiator',
  541. 'operator' => '=',
  542. 'table' => $this->_aObject['table'],
  543. ),
  544. 'connections_mutual_' . $this->_sObject => array(
  545. 'value' => $iMutual,
  546. 'field' => 'mutual',
  547. 'operator' => '=',
  548. 'table' => $this->_aObject['table'],
  549. ),
  550. ),
  551. 'join' => array (
  552. 'connections_' . $this->_sObject => array(
  553. 'type' => 'INNER',
  554. 'table' => $this->_aObject['table'],
  555. 'mainField' => $sContentField,
  556. 'onField' => 'content',
  557. 'joinFields' => array(),//'initiator'),
  558. ),
  559. ),
  560. );
  561. }
  562. /**
  563. * Get necessary condition array to use connections in search classes
  564. * @param $sContentField content table field name
  565. * @param $iInitiator initiator of the connection
  566. * @param $iMutual get mutual connections only
  567. * @return array of conditions, for now with 'restriction' and 'join' parts
  568. */
  569. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  570. {
  571. return array(
  572. 'restriction' => array (
  573. 'connections_' . $this->_sObject => array(
  574. 'value' => $iContent,
  575. 'field' => 'content',
  576. 'operator' => '=',
  577. 'table' => $this->_aObject['table'],
  578. ),
  579. 'connections_mutual_' . $this->_sObject => array(
  580. 'value' => $iMutual,
  581. 'field' => 'mutual',
  582. 'operator' => '=',
  583. 'table' => $this->_aObject['table'],
  584. ),
  585. ),
  586. 'join' => array (
  587. 'connections_' . $this->_sObject => array(
  588. 'type' => 'INNER',
  589. 'table' => $this->_aObject['table'],
  590. 'mainField' => $sContentField,
  591. 'onField' => 'initiator',
  592. 'joinFields' => array(),//'initiator'),
  593. ),
  594. ),
  595. );
  596. }
  597. /**
  598. * Check if initiator and content are connected.
  599. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  600. * @param $iInitiator initiator of the connection
  601. * @param $iContent connected content or other profile id in case of friends
  602. * @return true - if content and initiator are connected or false - in all other cases
  603. */
  604. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  605. {
  606. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  607. if (!$oConnection)
  608. return false;
  609. return false === $isMutual ? true : (isset($oConnection['mutual']) ? $oConnection['mutual'] : false);
  610. }
  611. /**
  612. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  613. * This method makes sense only when type of connection is mutual.
  614. * @param $iInitiator initiator of the connection
  615. * @param $iContent connected content or other profile id in case of friends
  616. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  617. */
  618. public function isConnectedNotMutual ($iInitiator, $iContent)
  619. {
  620. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  621. if (!$oConnection)
  622. return false;
  623. return $oConnection['mutual'] ? false : true;
  624. }
  625. /**
  626. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  627. * @param $iId which can be as conetnt ot initiator
  628. * @return true if some connections were deleted
  629. */
  630. public function onDeleteInitiatorAndContent ($iId)
  631. {
  632. $b = $this->onDeleteInitiator ($iId);
  633. $b = $this->onDeleteContent ($iId) || $b;
  634. return $b;
  635. }
  636. /**
  637. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  638. * @param $iIdInitiator initiator id
  639. * @return true if some connections were deleted
  640. */
  641. public function onDeleteInitiator ($iIdInitiator)
  642. {
  643. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  644. return false;
  645. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  646. 'initiator' => (int)$iIdInitiator,
  647. 'object' => $this,
  648. ));
  649. return true;
  650. }
  651. /**
  652. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  653. * @param $iIdInitiator initiator id
  654. * @return true if some connections were deleted
  655. */
  656. public function onDeleteContent ($iIdContent)
  657. {
  658. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  659. return false;
  660. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  661. 'content' => (int)$iIdContent,
  662. 'object' => $this,
  663. ));
  664. return true;
  665. }
  666. /**
  667. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  668. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  669. * @param $sTable table name with data which have assiciations with the connection
  670. * @param $sFieldId id field name which is associated with the connection
  671. * @return number of deleted connections
  672. */
  673. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  674. {
  675. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  676. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  677. return $iAffected;
  678. }
  679. /**
  680. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  681. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  682. * @param $sTable table name with data which have assiciations with the connection
  683. * @param $sFieldId id field name which is associated with the connection
  684. * @return number of deleted connections
  685. */
  686. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  687. {
  688. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  689. }
  690. /**
  691. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  692. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  693. * @param $sTable table name with data which have assiciations with the connection
  694. * @param $sFieldId id field name which is associated with the connection
  695. * @return number of deleted connections
  696. */
  697. public function onModuleDeleteContent ($sTable, $sFieldId)
  698. {
  699. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  700. }
  701. /**
  702. * 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.
  703. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  704. * @param $sModuleName module name to delete connections for
  705. * @return number of deleted connections
  706. */
  707. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  708. {
  709. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  710. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  711. return $iAffected;
  712. }
  713. /**
  714. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  715. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  716. * @param $sModuleName module name to delete connections for
  717. * @return number of deleted connections
  718. */
  719. public function onModuleProfileDeleteInitiator ($sModuleName)
  720. {
  721. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  722. }
  723. /**
  724. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  725. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  726. * @param $sModuleName module name to delete connections for
  727. * @return number of deleted connections
  728. */
  729. public function onModuleProfileDeleteContent ($sModuleName)
  730. {
  731. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  732. }
  733. }
  734. /** @} */