connections.php 8.8 KB

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