BxDolConnection.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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->_aObject['per_page_default'] = 20;
  122. $this->_sType = $aObject['type'];
  123. $this->_oQuery = new BxDolConnectionQuery($aObject);
  124. }
  125. /**
  126. * Get connection object instance by object name
  127. * @param $sObject object name
  128. * @return object instance or false on error
  129. */
  130. static public function getObjectInstance($sObject)
  131. {
  132. if (!$sObject)
  133. return false;
  134. if (isset($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject]))
  135. return $GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject];
  136. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  137. if (!$aObject || !is_array($aObject))
  138. return false;
  139. $sClass = empty($aObject['override_class_name']) ? 'BxTemplConnection' : $aObject['override_class_name'];
  140. if (!empty($aObject['override_class_file']))
  141. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  142. $o = new $sClass($aObject);
  143. return ($GLOBALS['bxDolClasses']['BxTemplConnection!'.$sObject] = $o);
  144. }
  145. /**
  146. * Get connection type.
  147. * return BX_CONNECTIONS_TYPE_ONE_WAY or BX_CONNECTIONS_TYPE_MUTUAL
  148. */
  149. public function getType()
  150. {
  151. return $this->_sType;
  152. }
  153. /**
  154. * Check whether connection between Initiator and Content can be established.
  155. */
  156. public function checkAllowedConnect ($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  157. {
  158. if(!$iInitiator || !$iContent || $iInitiator == $iContent)
  159. return _t('_sys_txt_access_denied');
  160. $oInitiator = BxDolProfile::getInstance($iInitiator);
  161. $oContent = BxDolProfile::getInstance($iContent);
  162. if(!$oInitiator || !$oContent)
  163. return _t('_sys_txt_access_denied');
  164. // check ACL
  165. if(($mixedResult = $this->_checkAllowedConnectInitiator($oInitiator, $isPerformAction)) !== CHECK_ACTION_RESULT_ALLOWED)
  166. return $mixedResult;
  167. // check content's visibility
  168. if(!$this->isConnected($iContent, $iInitiator) && ($mixedResult = $this->_checkAllowedConnectContent($oContent)) !== CHECK_ACTION_RESULT_ALLOWED)
  169. return $mixedResult;
  170. if(!$isCheckExists)
  171. return CHECK_ACTION_RESULT_ALLOWED;
  172. if($isSwap)
  173. $isConnected = $this->isConnected($iContent, $iInitiator, $isMutual);
  174. else
  175. $isConnected = $this->isConnected($iInitiator, $iContent, $isMutual);
  176. if($isInvertResult)
  177. $isConnected = !$isConnected;
  178. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  179. }
  180. protected function _checkAllowedConnectInitiator ($oInitiator, $isPerformAction = false)
  181. {
  182. $aCheck = checkActionModule($oInitiator->id(), 'connect', 'system', $isPerformAction);
  183. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  184. return $aCheck[CHECK_ACTION_MESSAGE];
  185. return CHECK_ACTION_RESULT_ALLOWED;
  186. }
  187. protected function _checkAllowedConnectContent ($oContent)
  188. {
  189. return $oContent->checkAllowedProfileView();
  190. }
  191. /**
  192. * Add new connection.
  193. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  194. * @return array
  195. */
  196. public function actionAdd ($iContent = 0, $iInitiator = false)
  197. {
  198. if (!$iContent)
  199. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  200. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  201. }
  202. /**
  203. * Remove connection. 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 actionRemove ($iContent = 0, $iInitiator = false)
  208. {
  209. if (!$iContent)
  210. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  211. if ($iContent != bx_get_logged_profile_id() && BX_CONNECTIONS_TYPE_MUTUAL == $this->_aObject['type']) {
  212. $a = $this->actionReject($iContent);
  213. if (false == $a['err'])
  214. return $a;
  215. }
  216. return $this->_action ($iInitiator ? $iInitiator : bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists', false, true);
  217. }
  218. /**
  219. * Reject connection request. This method is wrapper for @see removeConnection to be called from @see conn.php upon AJAX request to this file.
  220. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  221. * @return array
  222. */
  223. public function actionReject ($iContent = 0, $iInitiator = false)
  224. {
  225. if (!$iContent)
  226. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  227. return $this->_action($iContent, $iInitiator ? $iInitiator : bx_get_logged_profile_id(), 'removeConnection', '_sys_conn_err_connection_does_not_exists', false, true);
  228. }
  229. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false, $isInvert = false)
  230. {
  231. bx_import('BxDolLanguages');
  232. if(!$iContent || !$iInitiator)
  233. return ['err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined')];
  234. $sMethodCheck = 'checkAllowed' . bx_gen_method_name($sMethod);
  235. if(($mixedResult = $this->{method_exists($this, $sMethodCheck) ? $sMethodCheck : 'checkAllowedConnect'}($iInitiator, $iContent, false, false, $isInvert)) !== CHECK_ACTION_RESULT_ALLOWED)
  236. return ['err' => true, 'msg' => $mixedResult];
  237. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  238. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  239. return ['err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation')];
  240. return ['err' => true, 'msg' => _t($sErrorKey)];
  241. }
  242. return ['err' => false, 'msg' => _t('_sys_conn_msg_success')];
  243. }
  244. public function outputActionResult ($mixed, $sFormat = 'json')
  245. {
  246. switch ($sFormat) {
  247. case 'html':
  248. echo $mixed;
  249. break;
  250. case 'json':
  251. default:
  252. header('Content-Type: application/json; charset=utf-8');
  253. echo json_encode($mixed);
  254. }
  255. exit;
  256. }
  257. /**
  258. * Add new connection.
  259. * @param $iInitiator initiator of the connection, in most cases some profile id
  260. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  261. * @return true - if connection was added, false - if connection already exists or error occured
  262. */
  263. public function addConnection ($iInitiator, $iContent, $aParams = array())
  264. {
  265. $iMutual = 0;
  266. $iInitiator = (int)$iInitiator;
  267. $iContent = (int)$iContent;
  268. $aAlertExtras = array(
  269. 'initiator' => &$iInitiator,
  270. 'content' => &$iContent,
  271. 'mutual' => &$iMutual,
  272. 'object' => $this,
  273. );
  274. if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
  275. $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
  276. bx_alert($this->_sObject, 'connection_before_add', 0, bx_get_logged_profile_id(), $aAlertExtras);
  277. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  278. return false;
  279. bx_alert($this->_sObject, 'connection_added', 0, bx_get_logged_profile_id(), $aAlertExtras);
  280. $this->onAdded($iInitiator, $iContent, $iMutual);
  281. return true;
  282. }
  283. public function onAdded($iInitiator, $iContent, $iMutual)
  284. {
  285. $this->checkAllowedConnect ($iInitiator, $iContent, true, $iMutual, false);
  286. /**
  287. * Update recommendations.
  288. */
  289. $bMutual = false;
  290. if($this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($bMutual = ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual))) {
  291. $oProfileQuery = BxDolProfileQuery::getInstance();
  292. if($this->_aObject['profile_initiator']) {
  293. $aInitiator = $oProfileQuery->getInfoById($iInitiator);
  294. if(bx_srv($aInitiator['type'], 'act_as_profile'))
  295. BxDolRecommendation::updateData($iInitiator);
  296. }
  297. if($bMutual && $this->_aObject['profile_content']) {
  298. $aContent = $oProfileQuery->getInfoById($iContent);
  299. if(bx_srv($aContent['type'], 'act_as_profile'))
  300. BxDolRecommendation::updateData($iContent);
  301. }
  302. }
  303. }
  304. /**
  305. * Remove connection.
  306. * @param $iInitiator initiator of the connection
  307. * @param $iContent connected content or other profile id in case of friends
  308. * @return true - if connection was removed, false - if connection isn't exist or error occured
  309. */
  310. public function removeConnection ($iInitiator, $iContent)
  311. {
  312. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  313. return false;
  314. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  315. return false;
  316. bx_alert($this->_sObject, 'connection_removed', 0, bx_get_logged_profile_id(), array(
  317. 'initiator' => (int)$iInitiator,
  318. 'content' => (int)$iContent,
  319. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  320. 'object' => $this,
  321. ));
  322. $this->onRemoved($iInitiator, $iContent);
  323. return true;
  324. }
  325. public function onRemoved($iInitiator, $iContent) {}
  326. /**
  327. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  328. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  329. * @param $iId1 one content or initiator
  330. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  331. * @param $isMutual get mutual connections only
  332. * @return array of available connections
  333. */
  334. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  335. {
  336. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  337. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  338. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  339. $sMethod = 'getConnectedInitiators';
  340. else
  341. $sMethod = 'getConnectedContent';
  342. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  343. }
  344. /**
  345. * Get common content IDs between two initiators
  346. * @param $iInitiator1 one initiator
  347. * @param $iInitiator2 second initiator
  348. * @param $isMutual get mutual connections only
  349. * @return array of available connections
  350. */
  351. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  352. {
  353. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  354. }
  355. /**
  356. * Get common content count between two initiators
  357. * @param $iInitiator1 one initiator
  358. * @param $iInitiator2 second initiator
  359. * @param $isMutual get mutual connections only
  360. * @return number of connections
  361. */
  362. public function getCommonContentCount ($iInitiator1, $iInitiator2, $isMutual = false)
  363. {
  364. return $this->_oQuery->getCommonContentCount($iInitiator1, $iInitiator2, $isMutual);
  365. }
  366. /**
  367. * Get connected content count
  368. * @param $iInitiator initiator of the connection
  369. * @param $isMutual get mutual connections only
  370. * @return number of connections
  371. */
  372. public function getConnectedContentCount ($iInitiator, $isMutual = false)
  373. {
  374. return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual);
  375. }
  376. /**
  377. * Get connected initiators count
  378. * @param $iContent content of the connection
  379. * @param $isMutual get mutual connections only
  380. * @return number of connections
  381. */
  382. public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
  383. {
  384. return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
  385. }
  386. /**
  387. * Get connected content IDs
  388. * @param $iInitiator initiator of the connection
  389. * @param $isMutual get mutual connections only
  390. * @return array of available connections
  391. */
  392. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  393. {
  394. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  395. }
  396. /**
  397. * Get connected content IDs for specified type
  398. * @param $iInitiator initiator of the connection
  399. * @param $mixedType type of content or an array of types
  400. * @param $isMutual get mutual connections only
  401. * @return array of available connections
  402. */
  403. public function getConnectedContentByType ($iInitiator, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  404. {
  405. return $this->_oQuery->getConnectedContentByType($iInitiator, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  406. }
  407. /**
  408. * Get connected initiators IDs
  409. * @param $iContent content of the connection
  410. * @param $isMutual get mutual connections only
  411. * @return array of available connections
  412. */
  413. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  414. {
  415. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  416. }
  417. /**
  418. * Get connected initiators IDs
  419. * @param $iContent content of the connection
  420. * @param $mixedType type of content or an array of types
  421. * @param $isMutual get mutual connections only
  422. * @return array of available connections
  423. */
  424. public function getConnectedInitiatorsByType ($iContent, $mixedType, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  425. {
  426. return $this->_oQuery->getConnectedInitiatorsByType($iContent, $mixedType, $isMutual, $iStart, $iLimit, $iOrder);
  427. }
  428. /**
  429. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  430. * @see getConnectionsAsArray
  431. */
  432. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false)
  433. {
  434. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  435. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  436. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  437. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  438. else
  439. $sMethod = 'getConnectedContentAsSQLParts';
  440. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  441. }
  442. /**
  443. * Get necessary parts of SQL query to use connections in other queries
  444. * @param $sContentTable content table or alias
  445. * @param $sContentField content table field or field alias
  446. * @param $iInitiator initiator of the connection
  447. * @param $isMutual get mutual connections only
  448. * @return array of SQL string parts, for now 'join' part only is returned
  449. */
  450. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false)
  451. {
  452. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  453. }
  454. /**
  455. * Get necessary parts of SQL query to use connections in other queries
  456. * @param $sContentTable content table or alias
  457. * @param $sContentField content table field or field alias
  458. * @param $iInitiator initiator of the connection
  459. * @param $isMutual get mutual connections only
  460. * @return array of SQL string parts, for now 'join' part only is returned
  461. */
  462. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  463. {
  464. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  465. }
  466. public function getConnectedContentAsSQLPartsExt ($sContentTable, $sContentField, $iInitiator, $isMutual = false)
  467. {
  468. return $this->_oQuery->getConnectedContentSQLPartsExt($sContentTable, $sContentField, $iInitiator, $isMutual);
  469. }
  470. /**
  471. * Get necessary parts of SQL query to use connections in other queries
  472. * @param $sContentTable content table or alias
  473. * @param $sContentField content table field or field alias
  474. * @param $sInitiatorTable initiator table or alias
  475. * @param $sInitiatorField initiator table field or field alias
  476. * @param $iInitiator initiator of the connection
  477. * @param $isMutual get mutual connections only
  478. * @return array of SQL string parts, for now 'join' part only is returned
  479. */
  480. public function getConnectedContentAsSQLPartsMultiple ($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual = false)
  481. {
  482. return $this->_oQuery->getConnectedContentSQLPartsMultiple($sContentTable, $sContentField, $sInitiatorTable, $sInitiatorField, $isMutual);
  483. }
  484. /**
  485. * Get necessary parts of SQL query to use connections in other queries
  486. * @param $sInitiatorTable initiator table or alias
  487. * @param $sInitiatorField initiator table field or field alias
  488. * @param $iContent content of the connection
  489. * @param $isMutual get mutual connections only
  490. * @return array of SQL string parts, for now 'join' part only is returned
  491. */
  492. public function getConnectedInitiatorsAsSQLParts ($sInitiatorTable, $sInitiatorField, $iContent, $isMutual = false)
  493. {
  494. return $this->_oQuery->getConnectedInitiatorsSQLParts($sInitiatorTable, $sInitiatorField, $iContent, $isMutual);
  495. }
  496. /**
  497. * Get necessary parts of SQL query to use connections in other queries
  498. * @param $sInitiatorTable initiator table or alias
  499. * @param $sInitiatorField initiator table field or field alias
  500. * @param $sContentTable content table or alias
  501. * @param $sContentField content table field or field alias
  502. * @param $isMutual get mutual connections only
  503. * @return array of SQL string parts, for now 'join' part only is returned
  504. */
  505. public function getConnectedInitiatorsAsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual = false)
  506. {
  507. return $this->_oQuery->getConnectedInitiatorsSQLPartsMultiple ($sInitiatorTable, $sInitiatorField, $sContentTable, $sContentField, $isMutual);
  508. }
  509. /**
  510. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  511. * @see getConnectionsAsArray
  512. */
  513. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  514. {
  515. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  516. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  517. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  518. $sMethod = 'getConnectedInitiatorsAsCondition';
  519. else
  520. $sMethod = 'getConnectedContentAsCondition';
  521. return $this->$sMethod($sContentField, $iId1, $isMutual);
  522. }
  523. /**
  524. * Get necessary condition array to use connections in search classes
  525. * @param $sContentField content table field name
  526. * @param $iInitiator initiator of the connection
  527. * @param $iMutual get mutual connections only
  528. * @return array of conditions, for now with 'restriction' and 'join' parts
  529. */
  530. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false)
  531. {
  532. return array(
  533. 'restriction' => array (
  534. 'connections_' . $this->_sObject => array(
  535. 'value' => $iInitiator1,
  536. 'field' => 'initiator',
  537. 'operator' => '=',
  538. 'table' => 'c',
  539. ),
  540. 'connections_mutual_' . $this->_sObject => array(
  541. 'value' => $iMutual,
  542. 'field' => 'mutual',
  543. 'operator' => '=',
  544. 'table' => 'c',
  545. ),
  546. 'connections2_' . $this->_sObject => array(
  547. 'value' => $iInitiator2,
  548. 'field' => 'initiator',
  549. 'operator' => '=',
  550. 'table' => 'c2',
  551. ),
  552. 'connections2_mutual_' . $this->_sObject => array(
  553. 'value' => $iMutual,
  554. 'field' => 'mutual',
  555. 'operator' => '=',
  556. 'table' => 'c2',
  557. ),
  558. ),
  559. 'join' => array (
  560. 'connections_' . $this->_sObject => array(
  561. 'type' => 'INNER',
  562. 'table' => $this->_aObject['table'],
  563. 'table_alias' => 'c',
  564. 'mainField' => $sContentField,
  565. 'onField' => 'content',
  566. 'joinFields' => array(),
  567. ),
  568. 'connections2_' . $this->_sObject => array(
  569. 'type' => 'INNER',
  570. 'table' => $this->_aObject['table'],
  571. 'table_alias' => 'c2',
  572. 'mainTable' => 'c',
  573. 'mainField' => 'content',
  574. 'onField' => 'content',
  575. 'joinFields' => array(),
  576. ),
  577. ),
  578. );
  579. }
  580. /**
  581. * Get necessary condition array to use connections in search classes
  582. * @param $sContentField content table field name
  583. * @param $iInitiator initiator of the connection
  584. * @param $iMutual get mutual connections only
  585. * @return array of conditions, for now with 'restriction' and 'join' parts
  586. */
  587. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false)
  588. {
  589. $sOperation = '=';
  590. if(is_array($iInitiator))
  591. $sOperation = 'in';
  592. return array(
  593. 'restriction' => array (
  594. 'connections_' . $this->_sObject => array(
  595. 'value' => $iInitiator,
  596. 'field' => 'initiator',
  597. 'operator' => $sOperation,
  598. 'table' => $this->_aObject['table'],
  599. ),
  600. 'connections_mutual_' . $this->_sObject => array(
  601. 'value' => $iMutual,
  602. 'field' => 'mutual',
  603. 'operator' => '=',
  604. 'table' => $this->_aObject['table'],
  605. ),
  606. ),
  607. 'join' => array (
  608. 'connections_' . $this->_sObject => array(
  609. 'type' => 'INNER',
  610. 'table' => $this->_aObject['table'],
  611. 'mainField' => $sContentField,
  612. 'onField' => 'content',
  613. 'joinFields' => array(),//'initiator'),
  614. ),
  615. ),
  616. );
  617. }
  618. /**
  619. * Get necessary condition array to use connections in search classes
  620. * @param $sContentField content table field name
  621. * @param $iInitiator initiator of the connection
  622. * @param $iMutual get mutual connections only
  623. * @return array of conditions, for now with 'restriction' and 'join' parts
  624. */
  625. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false)
  626. {
  627. $sOperation = '=';
  628. if(is_array($iContent))
  629. $sOperation = 'in';
  630. return array(
  631. 'restriction' => array (
  632. 'connections_' . $this->_sObject => array(
  633. 'value' => $iContent,
  634. 'field' => 'content',
  635. 'operator' => $sOperation,
  636. 'table' => $this->_aObject['table'],
  637. ),
  638. 'connections_mutual_' . $this->_sObject => array(
  639. 'value' => $iMutual,
  640. 'field' => 'mutual',
  641. 'operator' => '=',
  642. 'table' => $this->_aObject['table'],
  643. ),
  644. ),
  645. 'join' => array (
  646. 'connections_' . $this->_sObject => array(
  647. 'type' => 'INNER',
  648. 'table' => $this->_aObject['table'],
  649. 'mainField' => $sContentField,
  650. 'onField' => 'initiator',
  651. 'joinFields' => array(),//'initiator'),
  652. ),
  653. ),
  654. );
  655. }
  656. /**
  657. * Check if initiator and content are connected.
  658. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  659. * @param $iInitiator initiator of the connection
  660. * @param $iContent connected content or other profile id in case of friends
  661. * @return true - if content and initiator are connected or false - in all other cases
  662. */
  663. public function isConnected ($iInitiator, $iContent, $isMutual = false)
  664. {
  665. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  666. if (!$oConnection)
  667. return false;
  668. return false === $isMutual ? true : (isset($oConnection['mutual']) ? $oConnection['mutual'] : false);
  669. }
  670. /**
  671. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  672. * This method makes sense only when type of connection is mutual.
  673. * @param $iInitiator initiator of the connection
  674. * @param $iContent connected content or other profile id in case of friends
  675. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  676. */
  677. public function isConnectedNotMutual ($iInitiator, $iContent)
  678. {
  679. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  680. if (!$oConnection)
  681. return false;
  682. return $oConnection['mutual'] ? false : true;
  683. }
  684. public function getConnection ($iInitiator, $iContent)
  685. {
  686. return $this->_oQuery->getConnection($iInitiator, $iContent);
  687. }
  688. public function getConnectionById ($iId)
  689. {
  690. return $this->_oQuery->getConnectionById($iId);
  691. }
  692. /**
  693. * Must be called when some content is deleted which can have connections as 'content' or as 'initiator', to delete any associated data
  694. * @param $iId which can be as conetnt ot initiator
  695. * @return true if some connections were deleted
  696. */
  697. public function onDeleteInitiatorAndContent ($iId)
  698. {
  699. $b = $this->onDeleteInitiator ($iId);
  700. $b = $this->onDeleteContent ($iId) || $b;
  701. return $b;
  702. }
  703. /**
  704. * Must be called when some content is deleted which can have connections as 'initiator', to delete any associated data
  705. * @param $iIdInitiator initiator id
  706. * @return true if some connections were deleted
  707. */
  708. public function onDeleteInitiator ($iIdInitiator)
  709. {
  710. if(!$this->_oQuery->onDelete ($iIdInitiator, 'initiator'))
  711. return false;
  712. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  713. 'initiator' => (int)$iIdInitiator,
  714. 'object' => $this,
  715. ));
  716. return true;
  717. }
  718. /**
  719. * Must be called when some content is deleted which can have connections as 'content', to delete any associated data
  720. * @param $iIdInitiator initiator id
  721. * @return true if some connections were deleted
  722. */
  723. public function onDeleteContent ($iIdContent)
  724. {
  725. if(!$this->_oQuery->onDelete ($iIdContent, 'content'))
  726. return false;
  727. bx_alert($this->_sObject, 'connection_removed_all', 0, bx_get_logged_profile_id(), array(
  728. 'content' => (int)$iIdContent,
  729. 'object' => $this,
  730. ));
  731. return true;
  732. }
  733. /**
  734. * Must be called when module (which can have connections as 'content' or as 'initiator') is deleted, to delete any associated data.
  735. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  736. * @param $sTable table name with data which have assiciations with the connection
  737. * @param $sFieldId id field name which is associated with the connection
  738. * @return number of deleted connections
  739. */
  740. public function onModuleDeleteInitiatorAndContent ($sTable, $sFieldId)
  741. {
  742. $iAffected = $this->onModuleDeleteInitiator ($sTable, $sFieldId);
  743. $iAffected += $this->onModuleDeleteContent ($sTable, $sFieldId);
  744. return $iAffected;
  745. }
  746. /**
  747. * Must be called when module (which can have connections as 'initiator') is deleted, to delete any associated data.
  748. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  749. * @param $sTable table name with data which have assiciations with the connection
  750. * @param $sFieldId id field name which is associated with the connection
  751. * @return number of deleted connections
  752. */
  753. public function onModuleDeleteInitiator ($sTable, $sFieldId)
  754. {
  755. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'initiator');
  756. }
  757. /**
  758. * Must be called when module (which can have connections as 'content') is deleted, to delete any associated data.
  759. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  760. * @param $sTable table name with data which have assiciations with the connection
  761. * @param $sFieldId id field name which is associated with the connection
  762. * @return number of deleted connections
  763. */
  764. public function onModuleDeleteContent ($sTable, $sFieldId)
  765. {
  766. return $this->_oQuery->onModuleDelete ($sTable, $sFieldId, 'content');
  767. }
  768. /**
  769. * 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.
  770. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  771. * @param $sModuleName module name to delete connections for
  772. * @return number of deleted connections
  773. */
  774. public function onModuleProfileDeleteInitiatorAndContent ($sModuleName)
  775. {
  776. $iAffected = $this->onModuleProfileDeleteInitiator ($sModuleName);
  777. $iAffected += $this->onModuleProfileDeleteContent ($sModuleName);
  778. return $iAffected;
  779. }
  780. /**
  781. * Must be called when module (which can have connections as 'initiator' with 'sys_profiles' table) is deleted, to delete any associated data.
  782. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  783. * @param $sModuleName module name to delete connections for
  784. * @return number of deleted connections
  785. */
  786. public function onModuleProfileDeleteInitiator ($sModuleName)
  787. {
  788. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'initiator');
  789. }
  790. /**
  791. * Must be called when module (which can have connections as 'content' with 'sys_profiles' table) is deleted, to delete any associated data.
  792. * This method call may be automated via @see BxBaseModGeneralInstaller::_aConnections property.
  793. * @param $sModuleName module name to delete connections for
  794. * @return number of deleted connections
  795. */
  796. public function onModuleProfileDeleteContent ($sModuleName)
  797. {
  798. return $this->_oQuery->onModuleProfileDelete ($sModuleName, 'content');
  799. }
  800. }
  801. /** @} */