BxDolConnection.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolConnectionQuery');
  10. /**
  11. * Default limit for connections lists
  12. */
  13. define('BX_CONNECTIONS_LIST_LIMIT', 1000);
  14. /**
  15. * Connections order: no order
  16. */
  17. define('BX_CONNECTIONS_ORDER_NONE', 0);
  18. /**
  19. * Connections order: by addded time, asceding
  20. */
  21. define('BX_CONNECTIONS_ORDER_ADDED_ASC', 1);
  22. /**
  23. * Connections order: by addded time, desceding
  24. */
  25. define('BX_CONNECTIONS_ORDER_ADDED_DESC', 2);
  26. /**
  27. * Connection type: one-way
  28. */
  29. define('BX_CONNECTIONS_TYPE_ONE_WAY', 'one-way');
  30. /**
  31. * Connection type: mutual
  32. */
  33. define('BX_CONNECTIONS_TYPE_MUTUAL', 'mutual');
  34. /**
  35. * Connections content type: content
  36. */
  37. define('BX_CONNECTIONS_CONTENT_TYPE_CONTENT', 'content');
  38. /**
  39. * Connections content type: initiators
  40. */
  41. define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  42. /**
  43. * Connections content type: common
  44. */
  45. define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
  46. /**
  47. * @page objects
  48. * @section connection Connection
  49. * @ref BxDolConnection
  50. */
  51. /**
  52. * Connection is usefull when you need to organize some sorts of connections between different content,
  53. * for example: friends, contacts, favorites, block lists, subscriptions, etc.
  54. *
  55. * Two types of connections are supported one way connections (block list, favourites) and mutual (friends).
  56. *
  57. * For automatic handling of connections (like, add/remove connection in frontend) refer to JS function: @see bx_conn_action()
  58. *
  59. * @section connection_create Creating the Connection object:
  60. *
  61. * Step 1:
  62. * Add record to 'sys_objects_connection' table:
  63. * - object: name of the connection object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  64. * for example: bx_blogs_favorites - favorite blogs for users in blogs module.
  65. * - table: table name with connections, see step 2
  66. * - type: 'one-way' or 'mutual'
  67. * - override_class_name: user defined class name which is derived from one of base classes.
  68. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  69. *
  70. * Step 2:
  71. * Create table for connections:
  72. * @code
  73. * CREATE TABLE `my_sample_connections` (
  74. * `id` int(11) NOT NULL AUTO_INCREMENT,
  75. * `initiator` int(11) NOT NULL,
  76. * `content` int(11) NOT NULL,
  77. * `mutual` tinyint(4) NOT NULL, -- can re removed for one-way connections
  78. * `added` int(10) unsigned NOT NULL,
  79. * PRIMARY KEY (`id`),
  80. * UNIQUE KEY `initiator` (`initiator`,`content`),
  81. * KEY `content` (`content`)
  82. * )
  83. * @endcode
  84. *
  85. *
  86. * @section example Example of usage
  87. *
  88. * Check if two profiles are friends:
  89. * @code
  90. * bx_import('BxDolConnection'); // import connection class
  91. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  92. * if ($oConnectionFriends) // check if connections is available for using
  93. * 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
  94. * @endcode
  95. *
  96. * Get mutual content IDs (friends IDs)
  97. * @code
  98. * bx_import('BxDolConnection'); // import connection class
  99. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  100. * if ($oConnectionFriends) // check if connections is available for using
  101. * print_r($oConnection->getConnectedContent(100, 1)); // print array of friends IDs of 100's profile
  102. * @endcode
  103. *
  104. */
  105. class BxDolConnection extends BxDol implements iBxDolFactoryObject {
  106. protected $_sObject;
  107. protected $_aObject;
  108. protected $_oQuery;
  109. protected $_sType;
  110. /**
  111. * Constructor
  112. * @param $aObject array of connection options
  113. */
  114. protected function __construct($aObject) {
  115. parent::__construct();
  116. $this->_sObject = $aObject['object'];
  117. $this->_aObject = $aObject;
  118. $this->_sType = $aObject['type'];
  119. $this->_oQuery = new BxDolConnectionQuery($aObject);
  120. }
  121. /**
  122. * Get connection object instance by object name
  123. * @param $sObject object name
  124. * @return object instance or false on error
  125. */
  126. static public function getObjectInstance($sObject) {
  127. if (!$sObject)
  128. return false;
  129. if (isset($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject]))
  130. return $GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject];
  131. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  132. if (!$aObject || !is_array($aObject))
  133. return false;
  134. $sClass = empty($aObject['override_class_name']) ? 'BxDolConnection' : $aObject['override_class_name'];
  135. if (!empty($aObject['override_class_file']))
  136. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  137. else
  138. bx_import($sClass);
  139. $o = new $sClass($aObject);
  140. return ($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject] = $o);
  141. }
  142. /**
  143. * Add new connection.
  144. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  145. * @return array
  146. */
  147. public function actionAdd ($iContent = 0) {
  148. if (!$iContent)
  149. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  150. return $this->_action (bx_get_logged_profile_id(), $iContent, 'addConnection', '_sys_conn_err_connection_already_exists', true);
  151. }
  152. /**
  153. * Remove connection.
  154. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  155. * @return array
  156. */
  157. public function actionRemove ($iContent = 0) {
  158. if (!$iContent)
  159. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  160. $a = $this->actionReject($iContent);
  161. if (false == $a['err'])
  162. return $a;
  163. return $this->_action (bx_get_logged_profile_id(), $iContent, 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  164. }
  165. /**
  166. * Reject connection request.
  167. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  168. * @return array
  169. */
  170. public function actionReject ($iContent = 0) {
  171. if (!$iContent)
  172. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  173. return $this->_action ($iContent, bx_get_logged_profile_id(), 'removeConnection', '_sys_conn_err_connection_does_not_exists');
  174. }
  175. protected function _action ($iInitiator, $iContent, $sMethod, $sErrorKey, $isMutual = false) {
  176. bx_import('BxDolLanguages');
  177. if (!$iContent || !$iInitiator)
  178. return array ('err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined'));
  179. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  180. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  181. return array ('err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation'));
  182. return array ('err' => true, 'msg' => _t($sErrorKey));
  183. }
  184. return array ('err' => false, 'msg' => _t('_sys_conn_msg_success'));
  185. }
  186. public function outputActionResult ($mixed, $sFormat = 'json') {
  187. switch ($sFormat) {
  188. case 'json':
  189. default:
  190. header('Content-Type: application/json; charset=utf-8');
  191. echo json_encode($mixed);
  192. }
  193. exit;
  194. }
  195. /**
  196. * Add new connection.
  197. * @param $iInitiator initiator of the connection, in most cases some profile id
  198. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  199. * @return true - if connection was added, false - if connection already exists or error occured
  200. */
  201. public function addConnection ($iInitiator, $iContent) {
  202. $iMutual = 0;
  203. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  204. return false;
  205. bx_alert($this->_sObject, 'connection_added', 0, getLoggedId(), array(
  206. 'initiator' => (int)$iInitiator,
  207. 'content' => (int)$iContent,
  208. 'mutual' => (int)$iMutual,
  209. 'object' => $this,
  210. ));
  211. return true;
  212. }
  213. /**
  214. * Remove connection.
  215. * @param $iInitiator initiator of the connection
  216. * @param $iContent connected content or other profile id in case of friends
  217. * @return true - if connection was removed, false - if connection isn't exist or error occured
  218. */
  219. public function removeConnection ($iInitiator, $iContent) {
  220. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  221. return false;
  222. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  223. return false;
  224. bx_alert($this->_sObject, 'connection_removed', 0, getLoggedId(), array(
  225. 'initiator' => (int)$iInitiator,
  226. 'content' => (int)$iContent,
  227. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  228. 'object' => $this,
  229. ));
  230. return true;
  231. }
  232. /**
  233. * Compound function, which calls getCommonContent, getConnectedContent or getConnectedInitiators depending on $sContentType
  234. * @param $sContentType content type to get BX_CONNECTIONS_CONTENT_TYPE_CONTENT, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS or BX_CONNECTIONS_CONTENT_TYPE_COMMON
  235. * @param $iId1 one content or initiator
  236. * @param $iId2 second content or initiator only in case of BX_CONNECTIONS_CONTENT_TYPE_COMMON content type
  237. * @param $isMutual get mutual connections only
  238. * @return array of available connections
  239. */
  240. public function getConnectionsAsArray ($sContentType, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  241. {
  242. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  243. return $this->getCommonContent($iId1, $iId2, $isMutual, $iStart, $iLimit, $iOrder);
  244. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  245. $sMethod = 'getConnectedInitiators';
  246. else
  247. $sMethod = 'getConnectedContent';
  248. return $this->$sMethod($iId1, $isMutual, $iStart, $iLimit, $iOrder);
  249. }
  250. /**
  251. * Get common content IDs between two initiators
  252. * @param $iInitiator1 one initiator
  253. * @param $iInitiator2 second initiator
  254. * @param $isMutual get mutual connections only
  255. * @return array of available connections
  256. */
  257. public function getCommonContent ($iInitiator1, $iInitiator2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  258. return $this->_oQuery->getCommonContent($iInitiator1, $iInitiator2, $isMutual, $iStart, $iLimit, $iOrder);
  259. }
  260. /**
  261. * Get connected content IDs
  262. * @param $iInitiator initiator of the connection
  263. * @param $isMutual get mutual connections only
  264. * @return array of available connections
  265. */
  266. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  267. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  268. }
  269. /**
  270. * Get connected initiators IDs
  271. * @param $iContent content of the connection
  272. * @param $isMutual get mutual connections only
  273. * @return array of available connections
  274. */
  275. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  276. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  277. }
  278. /**
  279. * Similar to getConnectionsAsArray, but for getCommonContentAsSQLParts, getConnectedContentAsSQLParts or getConnectedInitiatorsAsSQLParts methods
  280. * @see getConnectionsAsArray
  281. */
  282. public function getConnectionsAsSQLParts ($sContentType, $sContentTable, $sContentField, $iId1, $iId2, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE)
  283. {
  284. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  285. return $this->getCommonContentAsSQLParts($sContentTable, $sContentField, $iId1, $iId2, $isMutual);
  286. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  287. $sMethod = 'getConnectedInitiatorsAsSQLParts';
  288. else
  289. $sMethod = 'getConnectedContentAsSQLParts';
  290. return $this->$sMethod($sContentTable, $sContentField, $iId1, $isMutual);
  291. }
  292. /**
  293. * Get necessary parts of SQL query to use connections in other queries
  294. * @param $sContentTable content table or alias
  295. * @param $sContentField content table field or field alias
  296. * @param $iInitiator initiator of the connection
  297. * @param $isMutual get mutual connections only
  298. * @return array of SQL string parts, for now 'join' part only is returned
  299. */
  300. public function getCommonContentAsSQLParts ($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual = false) {
  301. return $this->_oQuery->getCommonContentSQLParts($sContentTable, $sContentField, $iInitiator1, $iInitiator2, $isMutual);
  302. }
  303. /**
  304. * Get necessary parts of SQL query to use connections in other queries
  305. * @param $sContentTable content table or alias
  306. * @param $sContentField content table field or field alias
  307. * @param $iInitiator initiator of the connection
  308. * @param $isMutual get mutual connections only
  309. * @return array of SQL string parts, for now 'join' part only is returned
  310. */
  311. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false) {
  312. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  313. }
  314. /**
  315. * Get necessary parts of SQL query to use connections in other queries
  316. * @param $sContentTable content table or alias
  317. * @param $sContentField content table field or field alias
  318. * @param $iContent content of the connection
  319. * @param $isMutual get mutual connections only
  320. * @return array of SQL string parts, for now 'join' part only is returned
  321. */
  322. public function getConnectedInitiatorsAsSQLParts ($sContentTable, $sContentField, $iContent, $isMutual = false) {
  323. return $this->_oQuery->getConnectedInitiatorsSQLParts($sContentTable, $sContentField, $iContent, $isMutual);
  324. }
  325. /**
  326. * Similar to getConnectionsAsArray, but for getCommonContentAsCondition, getConnectedContentAsCondition or getConnectedInitiatorsAsCondition methods
  327. * @see getConnectionsAsArray
  328. */
  329. public function getConnectionsAsCondition ($sContentType, $sContentField, $iId1, $iId2, $isMutual = false)
  330. {
  331. if (BX_CONNECTIONS_CONTENT_TYPE_COMMON == $sContentType)
  332. return $this->getCommonContentAsCondition($sContentField, $iId1, $iId2, $isMutual);
  333. if (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType)
  334. $sMethod = 'getConnectedInitiatorsAsCondition';
  335. else
  336. $sMethod = 'getConnectedContentAsCondition';
  337. return $this->$sMethod($sContentField, $iId1, $isMutual);
  338. }
  339. /**
  340. * Get necessary condition array to use connections in search classes
  341. * @param $sContentField content table field name
  342. * @param $iInitiator initiator of the connection
  343. * @param $iMutual get mutual connections only
  344. * @return array of conditions, for now with 'restriction' and 'join' parts
  345. */
  346. public function getCommonContentAsCondition ($sContentField, $iInitiator1, $iInitiator2, $iMutual = false) {
  347. return array(
  348. 'restriction' => array (
  349. 'connections_' . $this->_sObject => array(
  350. 'value' => $iInitiator1,
  351. 'field' => 'initiator',
  352. 'operator' => '=',
  353. 'table' => 'c',
  354. ),
  355. 'connections_mutual_' . $this->_sObject => array(
  356. 'value' => $iMutual,
  357. 'field' => 'mutual',
  358. 'operator' => '=',
  359. 'table' => 'c',
  360. ),
  361. 'connections2_' . $this->_sObject => array(
  362. 'value' => $iInitiator2,
  363. 'field' => 'initiator',
  364. 'operator' => '=',
  365. 'table' => 'c2',
  366. ),
  367. 'connections2_mutual_' . $this->_sObject => array(
  368. 'value' => $iMutual,
  369. 'field' => 'mutual',
  370. 'operator' => '=',
  371. 'table' => 'c2',
  372. ),
  373. ),
  374. 'join' => array (
  375. 'connections_' . $this->_sObject => array(
  376. 'type' => 'INNER',
  377. 'table' => $this->_aObject['table'],
  378. 'table_alias' => 'c',
  379. 'mainField' => $sContentField,
  380. 'onField' => 'content',
  381. 'joinFields' => array(),
  382. ),
  383. 'connections2_' . $this->_sObject => array(
  384. 'type' => 'INNER',
  385. 'table' => $this->_aObject['table'],
  386. 'table_alias' => 'c2',
  387. 'mainTable' => 'c',
  388. 'mainField' => 'content',
  389. 'onField' => 'content',
  390. 'joinFields' => array(),
  391. ),
  392. ),
  393. );
  394. }
  395. /**
  396. * Get necessary condition array to use connections in search classes
  397. * @param $sContentField content table field name
  398. * @param $iInitiator initiator of the connection
  399. * @param $iMutual get mutual connections only
  400. * @return array of conditions, for now with 'restriction' and 'join' parts
  401. */
  402. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false) {
  403. return array(
  404. 'restriction' => array (
  405. 'connections_' . $this->_sObject => array(
  406. 'value' => $iInitiator,
  407. 'field' => 'initiator',
  408. 'operator' => '=',
  409. 'table' => $this->_aObject['table'],
  410. ),
  411. 'connections_mutual_' . $this->_sObject => array(
  412. 'value' => $iMutual,
  413. 'field' => 'mutual',
  414. 'operator' => '=',
  415. 'table' => $this->_aObject['table'],
  416. ),
  417. ),
  418. 'join' => array (
  419. 'connections_' . $this->_sObject => array(
  420. 'type' => 'INNER',
  421. 'table' => $this->_aObject['table'],
  422. 'mainField' => $sContentField,
  423. 'onField' => 'content',
  424. 'joinFields' => array(),//'initiator'),
  425. ),
  426. ),
  427. );
  428. }
  429. /**
  430. * Get necessary condition array to use connections in search classes
  431. * @param $sContentField content table field name
  432. * @param $iInitiator initiator of the connection
  433. * @param $iMutual get mutual connections only
  434. * @return array of conditions, for now with 'restriction' and 'join' parts
  435. */
  436. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false) {
  437. return array(
  438. 'restriction' => array (
  439. 'connections_' . $this->_sObject => array(
  440. 'value' => $iContent,
  441. 'field' => 'content',
  442. 'operator' => '=',
  443. 'table' => $this->_aObject['table'],
  444. ),
  445. 'connections_mutual_' . $this->_sObject => array(
  446. 'value' => $iMutual,
  447. 'field' => 'mutual',
  448. 'operator' => '=',
  449. 'table' => $this->_aObject['table'],
  450. ),
  451. ),
  452. 'join' => array (
  453. 'connections_' . $this->_sObject => array(
  454. 'type' => 'INNER',
  455. 'table' => $this->_aObject['table'],
  456. 'mainField' => $sContentField,
  457. 'onField' => 'initiator',
  458. 'joinFields' => array(),//'initiator'),
  459. ),
  460. ),
  461. );
  462. }
  463. /**
  464. * Check if initiator and content are connected.
  465. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  466. * @param $iInitiator initiator of the connection
  467. * @param $iContent connected content or other profile id in case of friends
  468. * @return true - if content and initiator are connected or false - in all other cases
  469. */
  470. public function isConnected ($iInitiator, $iContent, $isMutual = false) {
  471. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  472. if (!$oConnection)
  473. return false;
  474. return false === $isMutual ? true : $oConnection['mutual'];
  475. }
  476. /**
  477. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  478. * This method makes sense only when type of connection is mutual.
  479. * @param $iInitiator initiator of the connection
  480. * @param $iContent connected content or other profile id in case of friends
  481. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  482. */
  483. public function isConnectedNotMutual ($iInitiator, $iContent) {
  484. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  485. if (!$oConnection)
  486. return false;
  487. return $oConnection['mutual'] ? false : true;
  488. }
  489. }
  490. /** @} */