connections.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $iProfileId2 = bx_get('id2');
  64. if (!$iProfileId2) {
  65. $sQueryOrig = "SELECT `id` FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' AND `id` != ? ORDER BY RAND() LIMIT 1";
  66. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType, $iProfileId);
  67. $iProfileId2 = $oDb->getOne($sQueryPrepared);
  68. }
  69. bx_import('BxDolConnection');
  70. $oConnection = BxDolConnection::getObjectInstance($sObject);
  71. if (!$oConnection)
  72. die ("'$sObject' object is not defined.");
  73. echo "<h1>Profile: $iProfileId / another one: $iProfileId2 </h1>";
  74. echo '<hr class="bx-def-hr" />';
  75. switch($sMethod) {
  76. default:
  77. case 'array':
  78. echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
  79. echoDbg($oConnection->getCommonContent($iProfileId, $iProfileId2, 1));
  80. echo "<h2>Mutual Content (like Friends)</h2>";
  81. echoDbg($oConnection->getConnectedContent($iProfileId, 1));
  82. echo "<h2>Connected Content</h2>";
  83. echoDbg($oConnection->getConnectedContent($iProfileId));
  84. echo "<h2>Connected Initiators</h2>";
  85. echoDbg($oConnection->getConnectedInitiators($iProfileId));
  86. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  87. echoDbg($oConnection->getConnectedContent($iProfileId, 0));
  88. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  89. echoDbg($oConnection->getConnectedInitiators($iProfileId, 0));
  90. break;
  91. case 'sql':
  92. $f = function ($aSQLParts) use ($oDb, $sType) {
  93. $sQueryOrig = "
  94. SELECT `p`.`id`, `d`.`fullname`
  95. FROM `bx_persons_data` AS `d`
  96. INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `d`.`id` AND `p`.`type` = ?)
  97. {$aSQLParts['join']}
  98. ";
  99. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
  100. $a = $oDb->getAll($sQueryPrepared);
  101. foreach ($a as $r)
  102. echo "{$r['id']} - {$r['fullname']} <br />\n";
  103. };
  104. echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
  105. $f($oConnection->getCommonContentAsSQLParts('p', 'id', $iProfileId, $iProfileId2, 1));
  106. echo "<h2>Mutual Content (like Friends)</h2>";
  107. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 1));
  108. echo "<h2>Connected Content</h2>";
  109. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId));
  110. echo "<h2>Connected Initiators</h2>";
  111. $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId));
  112. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  113. $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 0));
  114. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  115. $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId, 0));
  116. break;
  117. case 'search-results':
  118. echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
  119. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'common', 1, BX_DB_CONTENT_ONLY, $iProfileId2));
  120. echo "<h2>Mutual Content (like Friends)</h2>";
  121. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 1, BX_DB_CONTENT_ONLY));
  122. echo "<h2>Connected Content</h2>";
  123. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', false, BX_DB_CONTENT_ONLY));
  124. echo "<h2>Connected Initiators</h2>";
  125. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', false, BX_DB_CONTENT_ONLY));
  126. echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
  127. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 0, BX_DB_CONTENT_ONLY));
  128. echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
  129. echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', 0, BX_DB_CONTENT_ONLY));
  130. break;
  131. }
  132. $s = ob_get_clean();
  133. return DesignBoxContent("Connections", $s, BX_DB_PADDING_DEF);
  134. }
  135. function GenerateData($iMutual = 3, $iOneWay = 1, $sTable, $sType, $sObject) {
  136. $oDb = BxDolDb::getInstance();
  137. bx_import('BxDolConnection');
  138. $oConnection = BxDolConnection::getObjectInstance($sObject);
  139. if (!$oConnection)
  140. die ("'$sObject' object is not defined.");
  141. $oDb->query("TRUNCATE TABLE $sTable");
  142. // get all profiles
  143. $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active'";
  144. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
  145. if (!($aAll = $oDb->getAll($sQueryPrepared)))
  146. die($oDb->getErrorMessage());
  147. foreach ($aAll as $aProfile) {
  148. // get random profiles to add as connections
  149. $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' AND `id` != ? ORDER BY RAND() LIMIT ?";
  150. $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType, $aProfile['id'], $iMutual + $iOneWay);
  151. if (!($a = $oDb->getAll($sQueryPrepared)))
  152. die($oDb->getErrorMessage());
  153. $i = 0;
  154. foreach ($a as $r) {
  155. echo "{$aProfile['id']} " . ($i < $iMutual ? '&lt;=&gt;' : '=&gt;') . " {$r['id']} <br />\n";
  156. $aQueries = array();
  157. if ($i < $iMutual) {
  158. // mutual
  159. if (!$oConnection->isConnected($aProfile['id'], $r['id'], true)) {
  160. $oConnection->addConnection($aProfile['id'], $r['id']);
  161. $oConnection->addConnection($r['id'], $aProfile['id']);
  162. }
  163. } elseif ($i >= $iMutual) {
  164. // one-way
  165. if (!$oConnection->isConnected($aProfile['id'], $r['id']))
  166. $oConnection->addConnection($aProfile['id'], $r['id']);
  167. }
  168. $i++;
  169. }
  170. }
  171. }
  172. /** @} */