connections.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  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 Samples
  7. * @{
  8. */
  9. /**
  10. * @page samples
  11. * @section connections Connections
  12. */
  13. /**
  14. * This sample uses profile friends as sample connections.
  15. *
  16. * @section usage $_GET params:
  17. *
  18. * Display connections:
  19. * - id: profile ID to display connectiond for
  20. * - method = array: display connections using array method, just plain list of connections IDs
  21. * - method = sql: display connections using SQL method, custom SQL query where connection data is inserted as part of SQL query
  22. * - method = search-results: display connections using SearchResults class, SearchResults class must support connections
  23. *
  24. * Generate connections:
  25. * - action = gen: to generate sample connections for all profiles, by default around ~2 friends for each profile, and around ~1 friend request
  26. */
  27. require_once('./../inc/header.inc.php');
  28. require_once(BX_DIRECTORY_PATH_INC . "params.inc.php");
  29. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  30. bx_import('BxDolLanguages');
  31. bx_import('BxDolTemplate');
  32. $oTemplate = BxDolTemplate::getInstance();
  33. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  34. $oTemplate->setPageHeader ("Connections");
  35. $oTemplate->setPageContent ('page_main_code', PageCompMainCode());
  36. $oTemplate->getPageCode();
  37. /**
  38. * page code function
  39. */
  40. function PageCompMainCode() {
  41. $oDb = BxDolDb::getInstance();
  42. // config: for data generation
  43. $iMutual = 2; // around 2 mutual connections for each profile
  44. $iOneWay = 1; // around 1 one-way connections for each profile
  45. $sTable = "`sys_profiles_conn_connections`"; // to clean table before inserting genrated data
  46. // config: for displaying and data generation
  47. $sType = 'bx_persons'; // profiles type
  48. $sObject = 'sys_profiles_friends'; // connections object
  49. if ('gen' == bx_get('action') || (isset($_SERVER['argv'][1]) && 'gen' == $_SERVER['argv'][1])) {
  50. echo "\nConnections Generation: <br />\n";
  51. GenerateData($iMutual, $iOneWay, $sTable, $sType, $sObject);
  52. echo "\nData has been generated";
  53. exit;
  54. }
  55. ob_start();
  56. $sMethod = bx_get('method');
  57. $iProfileId = bx_get('id');
  58. if (!$iProfileId) {
  59. $sQueryOrig = "SELECT `id` FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' ORDER BY RAND() LIMIT 1";
  60. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
  61. $iProfileId = $oDb->getOne($sQueryPrepared);
  62. }
  63. bx_import('BxDolConnection');
  64. $oConnection = BxDolConnection::getObjectInstance($sObject);
  65. if (!$oConnection)
  66. die ("'$sObject' object is not defined.");
  67. echo "<h1>Profile: $iProfileId</h1>";
  68. echo '<hr class="bx-def-hr" />';
  69. switch($sMethod) {
  70. default:
  71. case 'array':
  72. echo "<h2>Mutual Content (like Friends)</h2>";
  73. echoDbg($oConnection->getConnectedContent($iProfileId, 1));
  74. echo "<h2>Connected Content</h2>";
  75. echoDbg($oConnection->getConnectedContent($iProfileId));
  76. echo "<h2>Connected Initiators</h2>";
  77. echoDbg($oConnection->getConnectedInitiators($iProfileId));
  78. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  79. echoDbg($oConnection->getConnectedContent($iProfileId, 0));
  80. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  81. echoDbg($oConnection->getConnectedInitiators($iProfileId, 0));
  82. break;
  83. case 'sql':
  84. $f = function ($aSQLParts) use ($oDb, $sType) {
  85. $sQueryOrig = "
  86. SELECT `p`.`id`, `d`.`fullname`
  87. FROM `bx_persons_data` AS `d`
  88. INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `d`.`id` AND `p`.`type` = ?)
  89. {$aSQLParts['join']}
  90. ";
  91. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
  92. $a = $oDb->getAll($sQueryPrepared);
  93. foreach ($a as $r)
  94. echo "{$r['id']} - {$r['fullname']} <br />\n";
  95. };
  96. echo "<h2>Mutual Content (like Friends)</h2>";
  97. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 1));
  98. echo "<h2>Connected Content</h2>";
  99. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId));
  100. echo "<h2>Connected Initiators</h2>";
  101. $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId));
  102. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  103. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 0));
  104. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  105. $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId, 0));
  106. break;
  107. case 'search-results':
  108. echo "<h2>Mutual Content (like Friends)</h2>";
  109. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 1, BX_DB_CONTENT_ONLY));
  110. echo "<h2>Connected Content</h2>";
  111. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', false, BX_DB_CONTENT_ONLY));
  112. echo "<h2>Connected Initiators</h2>";
  113. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', false, BX_DB_CONTENT_ONLY));
  114. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  115. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 0, BX_DB_CONTENT_ONLY));
  116. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  117. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', 0, BX_DB_CONTENT_ONLY));
  118. break;
  119. }
  120. $s = ob_get_clean();
  121. return DesignBoxContent("Connections", $s, BX_DB_PADDING_DEF);
  122. }
  123. function GenerateData($iMutual = 3, $iOneWay = 1, $sTable, $sType, $sObject) {
  124. $oDb = BxDolDb::getInstance();
  125. bx_import('BxDolConnection');
  126. $oConnection = BxDolConnection::getObjectInstance($sObject);
  127. if (!$oConnection)
  128. die ("'$sObject' object is not defined.");
  129. $oDb->query("TRUNCATE TABLE $sTable");
  130. // get all profiles
  131. $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active'";
  132. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
  133. if (!($aAll = $oDb->getAll($sQueryPrepared)))
  134. die($oDb->getErrorMessage());
  135. foreach ($aAll as $aProfile) {
  136. // get random profiles to add as connections
  137. $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' AND `id` != ? ORDER BY RAND() LIMIT ?";
  138. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType, $aProfile['id'], $iMutual + $iOneWay);
  139. if (!($a = $oDb->getAll($sQueryPrepared)))
  140. die($oDb->getErrorMessage());
  141. $i = 0;
  142. foreach ($a as $r) {
  143. echo "{$aProfile['id']} " . ($i < $iMutual ? '&lt;=&gt;' : '=&gt;') . " {$r['id']} <br />\n";
  144. $aQueries = array();
  145. if ($i < $iMutual) {
  146. // mutual
  147. if (!$oConnection->isConnected($aProfile['id'], $r['id'], true)) {
  148. $oConnection->addConnection($aProfile['id'], $r['id']);
  149. $oConnection->addConnection($r['id'], $aProfile['id']);
  150. }
  151. } elseif ($i >= $iMutual) {
  152. // one-way
  153. if (!$oConnection->isConnected($aProfile['id'], $r['id']))
  154. $oConnection->addConnection($aProfile['id'], $r['id']);
  155. }
  156. $i++;
  157. }
  158. }
  159. }
  160. /** @} */