| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCoreSamples Samples
- * @{
- */
- /**
- * @page samples
- * @section connections Connections
- */
- /**
- * This sample uses profile friends as sample connections.
- *
- * @section usage $_GET params:
- *
- * Display connections:
- * - id: profile ID to display connectiond for
- * - method = array: display connections using array method, just plain list of connections IDs
- * - method = sql: display connections using SQL method, custom SQL query where connection data is inserted as part of SQL query
- * - method = search-results: display connections using SearchResults class, SearchResults class must support connections
- *
- * Generate connections:
- * - action = gen: to generate sample connections for all profiles, by default around ~2 friends for each profile, and around ~1 friend request
- */
- require_once('./../inc/header.inc.php');
- require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
- $oTemplate = BxDolTemplate::getInstance();
- $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
- $oTemplate->setPageHeader ("Connections");
- $oTemplate->setPageContent ('page_main_code', PageCompMainCode());
- $oTemplate->getPageCode();
- /**
- * page code function
- */
- function PageCompMainCode()
- {
- $oDb = BxDolDb::getInstance();
- // config: for data generation
- $iMutual = 2; // around 2 mutual connections for each profile
- $iOneWay = 1; // around 1 one-way connections for each profile
- $sTable = "`sys_profiles_conn_connections`"; // to clean table before inserting genrated data
- // config: for displaying and data generation
- $sType = 'bx_persons'; // profiles type
- $sObject = 'sys_profiles_friends'; // connections object
- if ('gen' == bx_get('action') || (isset($_SERVER['argv'][1]) && 'gen' == $_SERVER['argv'][1])) {
- echo "\nConnections Generation: <br />\n";
- GenerateData($iMutual, $iOneWay, $sTable, $sType, $sObject);
- echo "\nData has been generated";
- exit;
- }
- ob_start();
- $sMethod = bx_get('method');
- $iProfileId = bx_get('id');
- if (!$iProfileId) {
- $sQueryOrig = "SELECT `id` FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' ORDER BY RAND() LIMIT 1";
- $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
- $iProfileId = $oDb->getOne($sQueryPrepared);
- }
- $iProfileId2 = bx_get('id2');
- if (!$iProfileId2) {
- $sQueryOrig = "SELECT `id` FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' AND `id` != ? ORDER BY RAND() LIMIT 1";
- $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType, $iProfileId);
- $iProfileId2 = $oDb->getOne($sQueryPrepared);
- }
- $oConnection = BxDolConnection::getObjectInstance($sObject);
- if (!$oConnection)
- die ("'$sObject' object is not defined.");
- echo "<h1>Profile: $iProfileId / another one: $iProfileId2 </h1>";
- echo '<hr class="bx-def-hr" />';
- switch($sMethod) {
- default:
- case 'array':
- echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
- echoDbg($oConnection->getCommonContent($iProfileId, $iProfileId2, 1));
- echo "<h2>Mutual Content (like Friends)</h2>";
- echoDbg($oConnection->getConnectedContent($iProfileId, 1));
- echo "<h2>Connected Content</h2>";
- echoDbg($oConnection->getConnectedContent($iProfileId));
- echo "<h2>Connected Initiators</h2>";
- echoDbg($oConnection->getConnectedInitiators($iProfileId));
- echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
- echoDbg($oConnection->getConnectedContent($iProfileId, 0));
- echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
- echoDbg($oConnection->getConnectedInitiators($iProfileId, 0));
- break;
- case 'sql':
- $f = function ($aSQLParts) use ($oDb, $sType) {
- $sQueryOrig = "
- SELECT `p`.`id`, `d`.`fullname`
- FROM `bx_persons_data` AS `d`
- INNER JOIN `sys_profiles` AS `p` ON (`p`.`content_id` = `d`.`id` AND `p`.`type` = ?)
- {$aSQLParts['join']}
- ";
- $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
- $a = $oDb->getAll($sQueryPrepared);
- foreach ($a as $r)
- echo "{$r['id']} - {$r['fullname']} <br />\n";
- };
- echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
- $f($oConnection->getCommonContentAsSQLParts('p', 'id', $iProfileId, $iProfileId2, 1));
- echo "<h2>Mutual Content (like Friends)</h2>";
- $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 1));
- echo "<h2>Connected Content</h2>";
- $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId));
- echo "<h2>Connected Initiators</h2>";
- $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId));
- echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
- $f($oConnection->getConnectedContentAsSQLParts('p', 'id', $iProfileId, 0));
- echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
- $f($oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $iProfileId, 0));
- break;
- case 'search-results':
- echo "<h2>Common Content (like mutual Friends between two initiators)</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'common', 1, BX_DB_CONTENT_ONLY, $iProfileId2));
- echo "<h2>Mutual Content (like Friends)</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 1, BX_DB_CONTENT_ONLY));
- echo "<h2>Connected Content</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', false, BX_DB_CONTENT_ONLY));
- echo "<h2>Connected Initiators</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', false, BX_DB_CONTENT_ONLY));
- echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'content', 0, BX_DB_CONTENT_ONLY));
- echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
- echo BxDolService::call('bx_persons', 'browse_connections', array($iProfileId, 'sys_profiles_friends', 'initiators', 0, BX_DB_CONTENT_ONLY));
- break;
- }
- $s = ob_get_clean();
- return DesignBoxContent("Connections", $s, BX_DB_PADDING_DEF);
- }
- function GenerateData($iMutual = 3, $iOneWay = 1, $sTable, $sType, $sObject)
- {
- $oDb = BxDolDb::getInstance();
- $oConnection = BxDolConnection::getObjectInstance($sObject);
- if (!$oConnection)
- die ("'$sObject' object is not defined.");
- $oDb->query("TRUNCATE TABLE $sTable");
- // get all profiles
- $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active'";
- $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType);
- if (!($aAll = $oDb->getAll($sQueryPrepared)))
- die($oDb->getErrorMessage());
- foreach ($aAll as $aProfile) {
- // get random profiles to add as connections
- $sQueryOrig = "SELECT * FROM `sys_profiles` WHERE `type` = ? AND `status` = 'active' AND `id` != ? ORDER BY RAND() LIMIT ?";
- $sQueryPrepared = $oDb->prepare($sQueryOrig, $sType, $aProfile['id'], $iMutual + $iOneWay);
- if (!($a = $oDb->getAll($sQueryPrepared)))
- die($oDb->getErrorMessage());
- $i = 0;
- foreach ($a as $r) {
- echo "{$aProfile['id']} " . ($i < $iMutual ? '<=>' : '=>') . " {$r['id']} <br />\n";
- if ($i < $iMutual) {
- // mutual
- if (!$oConnection->isConnected($aProfile['id'], $r['id'], true)) {
- $oConnection->addConnection($aProfile['id'], $r['id']);
- $oConnection->addConnection($r['id'], $aProfile['id']);
- }
- } elseif ($i >= $iMutual) {
- // one-way
- if (!$oConnection->isConnected($aProfile['id'], $r['id']))
- $oConnection->addConnection($aProfile['id'], $r['id']);
- }
- $i++;
- }
- }
- }
- /** @} */
|