BxDolConnection.php 36 KB

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