BxDolConnection.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. * @page objects
  36. * @section connection Connection
  37. * @ref BxDolConnection
  38. */
  39. /**
  40. * Connection is usefull when you need to organize some sorts of connections between different content,
  41. * for example: friends, contacts, favorites, block lists, subscriptions, etc.
  42. *
  43. * Two types of connections are supported one way connections (block list, favourites) and mutual (friends).
  44. *
  45. * For automatic handling of connections (like, add/remove connection in frontend) refer to JS function: @see bx_conn_action()
  46. *
  47. * @section connection_create Creating the Connection object:
  48. *
  49. * Step 1:
  50. * Add record to 'sys_objects_connection' table:
  51. * - object: name of the connection object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing;
  52. * for example: bx_blogs_favorites - favorite blogs for users in blogs module.
  53. * - table: table name with connections, see step 2
  54. * - type: 'one-way' or 'mutual'
  55. * - override_class_name: user defined class name which is derived from one of base classes.
  56. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  57. *
  58. * Step 2:
  59. * Create table for connections:
  60. * @code
  61. * CREATE TABLE `my_sample_connections` (
  62. * `id` int(11) NOT NULL AUTO_INCREMENT,
  63. * `initiator` int(11) NOT NULL,
  64. * `content` int(11) NOT NULL,
  65. * `mutual` tinyint(4) NOT NULL, -- can re removed for one-way connections
  66. * `added` int(10) unsigned NOT NULL,
  67. * PRIMARY KEY (`id`),
  68. * UNIQUE KEY `initiator` (`initiator`,`content`),
  69. * KEY `content` (`content`)
  70. * )
  71. * @endcode
  72. *
  73. *
  74. * @section example Example of usage
  75. *
  76. * Check if two profiles are friends:
  77. * @code
  78. * bx_import('BxDolConnection'); // import connection class
  79. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  80. * if ($oConnectionFriends) // check if connections is available for using
  81. * 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
  82. * @endcode
  83. *
  84. * Get mutual content IDs (friends IDs)
  85. * @code
  86. * bx_import('BxDolConnection'); // import connection class
  87. * $oConnectionFriends = BxDolConnection::getObjectInstance('bx_profiles_friends'); // get friends connections object
  88. * if ($oConnectionFriends) // check if connections is available for using
  89. * print_r($oConnection->getConnectedContent(100, 1)); // print array of friends IDs of 100's profile
  90. * @endcode
  91. *
  92. */
  93. class BxDolConnection extends BxDol implements iBxDolFactoryObject {
  94. protected $_sObject;
  95. protected $_aObject;
  96. protected $_oQuery;
  97. protected $_sType;
  98. /**
  99. * Constructor
  100. * @param $aObject array of connection options
  101. */
  102. protected function __construct($aObject) {
  103. parent::__construct();
  104. $this->_sObject = $aObject['object'];
  105. $this->_aObject = $aObject;
  106. $this->_sType = $aObject['type'];
  107. $this->_oQuery = new BxDolConnectionQuery($aObject);
  108. }
  109. /**
  110. * Get connection object instance by object name
  111. * @param $sObject object name
  112. * @return object instance or false on error
  113. */
  114. static public function getObjectInstance($sObject) {
  115. if (!$sObject)
  116. return false;
  117. if (isset($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject]))
  118. return $GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject];
  119. $aObject = BxDolConnectionQuery::getConnectionObject($sObject);
  120. if (!$aObject || !is_array($aObject))
  121. return false;
  122. $sClass = empty($aObject['override_class_name']) ? 'BxDolConnection' : $aObject['override_class_name'];
  123. if (!empty($aObject['override_class_file']))
  124. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  125. else
  126. bx_import($sClass);
  127. $o = new $sClass($aObject);
  128. return ($GLOBALS['bxDolClasses']['BxDolConnection!'.$sObject] = $o);
  129. }
  130. /**
  131. * Add new connection.
  132. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  133. * @return array
  134. */
  135. public function actionAdd () {
  136. return $this->_action ('addConnection', '_sys_conn_err_connection_already_exists', false, true);
  137. }
  138. /**
  139. * Remove connection.
  140. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  141. * @return array
  142. */
  143. public function actionRemove () {
  144. $a = $this->actionReject();
  145. if (false == $a['err'])
  146. return $a;
  147. return $this->_action ('removeConnection', '_sys_conn_err_connection_does_not_exists', false);
  148. }
  149. /**
  150. * Reject connection request.
  151. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  152. * @return array
  153. */
  154. public function actionReject () {
  155. return $this->_action ('removeConnection', '_sys_conn_err_connection_does_not_exists', true);
  156. }
  157. protected function _action ($sMethod, $sErrorKey, $bSwapInitiatorAndContent, $isMutual = false) {
  158. bx_import('BxDolLanguages');
  159. $iContent = 0;
  160. $iInitiator = 0;
  161. if ($bSwapInitiatorAndContent) {
  162. $iInitiator = bx_process_input($_POST['id'], BX_DATA_INT);
  163. $iContent = bx_get_logged_profile_id();
  164. } else {
  165. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  166. $iInitiator = bx_get_logged_profile_id();
  167. }
  168. if (!$iContent || !$iInitiator)
  169. return array ('err' => true, 'msg' => _t('_sys_conn_err_input_data_is_not_defined'));
  170. if (!$this->$sMethod((int)$iInitiator, (int)$iContent)) {
  171. if ($isMutual && BX_CONNECTIONS_TYPE_MUTUAL == $this->_sType && $this->isConnected((int)$iInitiator, (int)$iContent, false) && !$this->isConnected((int)$iInitiator, (int)$iContent, true))
  172. return array ('err' => true, 'msg' => _t('_sys_conn_err_connection_is_awaiting_confirmation'));
  173. return array ('err' => true, 'msg' => _t($sErrorKey));
  174. }
  175. return array ('err' => false, 'msg' => _t('_sys_conn_msg_success'));
  176. }
  177. public function outputActionResult ($mixed, $sFormat = 'json') {
  178. switch ($sFormat) {
  179. case 'json':
  180. default:
  181. header('Content-Type: application/json; charset=utf-8');
  182. echo json_encode($mixed);
  183. }
  184. exit;
  185. }
  186. /**
  187. * Add new connection.
  188. * @param $iInitiator initiator of the connection, in most cases some profile id
  189. * @param $iContent content to make connection to, in most cases some content id, or other profile id in case of friends
  190. * @return true - if connection was added, false - if connection already exists or error occured
  191. */
  192. public function addConnection ($iInitiator, $iContent) {
  193. $iMutual = 0;
  194. if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
  195. return false;
  196. bx_alert($this->_sObject, 'connection_added', 0, getLoggedId(), array(
  197. 'initiator' => (int)$iInitiator,
  198. 'content' => (int)$iContent,
  199. 'mutual' => (int)$iMutual,
  200. 'object' => $this,
  201. ));
  202. return true;
  203. }
  204. /**
  205. * Remove connection.
  206. * @param $iInitiator initiator of the connection
  207. * @param $iContent connected content or other profile id in case of friends
  208. * @return true - if connection was removed, false - if connection isn't exist or error occured
  209. */
  210. public function removeConnection ($iInitiator, $iContent) {
  211. if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
  212. return false;
  213. if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
  214. return false;
  215. bx_alert($this->_sObject, 'connection_removed', 0, getLoggedId(), array(
  216. 'initiator' => (int)$iInitiator,
  217. 'content' => (int)$iContent,
  218. 'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
  219. 'object' => $this,
  220. ));
  221. return true;
  222. }
  223. /**
  224. * Get connected content IDs
  225. * @param $iInitiator initiator of the connection
  226. * @param $isMutual get mutual connections only
  227. * @return array of available connections
  228. */
  229. public function getConnectedContent ($iInitiator, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  230. return $this->_oQuery->getConnectedContent($iInitiator, $isMutual, $iStart, $iLimit, $iOrder);
  231. }
  232. /**
  233. * Get connected initiators IDs
  234. * @param $iContent content of the connection
  235. * @param $isMutual get mutual connections only
  236. * @return array of available connections
  237. */
  238. public function getConnectedInitiators ($iContent, $isMutual = false, $iStart = 0, $iLimit = BX_CONNECTIONS_LIST_LIMIT, $iOrder = BX_CONNECTIONS_ORDER_NONE) {
  239. return $this->_oQuery->getConnectedInitiators($iContent, $isMutual, $iStart, $iLimit, $iOrder);
  240. }
  241. /**
  242. * Get necessary parts of SQL query to use connections in other queries
  243. * @param $sContentTable content table or alias
  244. * @param $sContentField content table field or field alias
  245. * @param $iInitiator initiator of the connection
  246. * @param $isMutual get mutual connections only
  247. * @return array of SQL string parts, for now 'join' part only is returned
  248. */
  249. public function getConnectedContentAsSQLParts ($sContentTable, $sContentField, $iInitiator, $isMutual = false) {
  250. return $this->_oQuery->getConnectedContentSQLParts($sContentTable, $sContentField, $iInitiator, $isMutual);
  251. }
  252. /**
  253. * Get necessary parts of SQL query to use connections in other queries
  254. * @param $sContentTable content table or alias
  255. * @param $sContentField content table field or field alias
  256. * @param $iContent content of the connection
  257. * @param $isMutual get mutual connections only
  258. * @return array of SQL string parts, for now 'join' part only is returned
  259. */
  260. public function getConnectedInitiatorsAsSQLParts ($sContentTable, $sContentField, $iContent, $isMutual = false) {
  261. return $this->_oQuery->getConnectedInitiatorsSQLParts($sContentTable, $sContentField, $iContent, $isMutual);
  262. }
  263. /**
  264. * Get necessary condition array to use connections in search classes
  265. * @param $sContentField content table field name
  266. * @param $iInitiator initiator of the connection
  267. * @param $iMutual get mutual connections only
  268. * @return array of conditions, for now with 'restriction' and 'join' parts
  269. */
  270. public function getConnectedContentAsCondition ($sContentField, $iInitiator, $iMutual = false) {
  271. return array(
  272. 'restriction' => array (
  273. 'connections_' . $this->_sObject => array(
  274. 'value' => $iInitiator,
  275. 'field' => 'initiator',
  276. 'operator' => '=',
  277. 'table' => $this->_aObject['table'],
  278. ),
  279. 'connections_mutual_' . $this->_sObject => array(
  280. 'value' => $iMutual,
  281. 'field' => 'mutual',
  282. 'operator' => '=',
  283. 'table' => $this->_aObject['table'],
  284. ),
  285. ),
  286. 'join' => array (
  287. 'connections_' . $this->_sObject => array(
  288. 'type' => 'INNER',
  289. 'table' => $this->_aObject['table'],
  290. 'mainField' => $sContentField,
  291. 'onField' => 'content',
  292. 'joinFields' => array(),//'initiator'),
  293. ),
  294. ),
  295. );
  296. }
  297. /**
  298. * Get necessary condition array to use connections in search classes
  299. * @param $sContentField content table field name
  300. * @param $iInitiator initiator of the connection
  301. * @param $iMutual get mutual connections only
  302. * @return array of conditions, for now with 'restriction' and 'join' parts
  303. */
  304. public function getConnectedInitiatorsAsCondition ($sContentField, $iContent, $iMutual = false) {
  305. return array(
  306. 'restriction' => array (
  307. 'connections_' . $this->_sObject => array(
  308. 'value' => $iContent,
  309. 'field' => 'content',
  310. 'operator' => '=',
  311. 'table' => $this->_aObject['table'],
  312. ),
  313. 'connections_mutual_' . $this->_sObject => array(
  314. 'value' => $iMutual,
  315. 'field' => 'mutual',
  316. 'operator' => '=',
  317. 'table' => $this->_aObject['table'],
  318. ),
  319. ),
  320. 'join' => array (
  321. 'connections_' . $this->_sObject => array(
  322. 'type' => 'INNER',
  323. 'table' => $this->_aObject['table'],
  324. 'mainField' => $sContentField,
  325. 'onField' => 'initiator',
  326. 'joinFields' => array(),//'initiator'),
  327. ),
  328. ),
  329. );
  330. }
  331. /**
  332. * Check if initiator and content are connected.
  333. * In case if friends this function in conjunction with isMutual parameter can be used to check pending friend requests.
  334. * @param $iInitiator initiator of the connection
  335. * @param $iContent connected content or other profile id in case of friends
  336. * @return true - if content and initiator are connected or false - in all other cases
  337. */
  338. public function isConnected ($iInitiator, $iContent, $isMutual = false) {
  339. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  340. if (!$oConnection)
  341. return false;
  342. return false === $isMutual ? true : $oConnection['mutual'];
  343. }
  344. /**
  345. * Check if initiator and content are connected but connetion is not mutual, for checking pending connection requests.
  346. * This method makes sense only when type of connection is mutual.
  347. * @param $iInitiator initiator of the connection
  348. * @param $iContent connected content or other profile id in case of friends
  349. * @return true - if content and initiator are connected but connection is not mutual or false in all other cases
  350. */
  351. public function isConnectedNotMutual ($iInitiator, $iContent) {
  352. $oConnection = $this->_oQuery->getConnection ($iInitiator, $iContent);
  353. if (!$oConnection)
  354. return false;
  355. return $oConnection['mutual'] ? false : true;
  356. }
  357. }
  358. /** @} */