-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathconnections.php
227 lines (174 loc) · 8.91 KB
/
connections.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?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>";
$a = bx_srv('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'common', 1, BX_DB_CONTENT_ONLY, $iProfileId2]);
echo (isset($a['content']) ? $a['content'] : '');
echo "<h2>Mutual Content (like Friends)</h2>";
$a = bx_srv('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'content', 1, BX_DB_CONTENT_ONLY]);
echo (isset($a['content']) ? $a['content'] : '');
echo "<h2>Connected Content</h2>";
$a = bx_srv('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'content', false, BX_DB_CONTENT_ONLY]);
echo (isset($a['content']) ? $a['content'] : '');
echo "<h2>Connected Initiators</h2>";
$a = bx_srv('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'initiators', false, BX_DB_CONTENT_ONLY]);
echo (isset($a['content']) ? $a['content'] : '');
echo "<h2>Connected Content without mutual content (like Friend Requests sent)</h2>";
$a = bx_srv('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'content', 0, BX_DB_CONTENT_ONLY]);
echo (isset($a['content']) ? $a['content'] : '');
echo "<h2>Connected Initiators without mutual content (like Friend Requests received)</h2>";
$a = BxDolService::call('bx_persons', 'browse_connections', [$iProfileId, 'sys_profiles_friends', 'initiators', 0, BX_DB_CONTENT_ONLY]);
echo (isset($a['content']) ? $a['content'] : '');
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++;
}
}
}
/** @} */