connections.php 8.9 KB

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