-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2d_server.module
403 lines (348 loc) · 11.9 KB
/
d2d_server.module
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* @file
*
*/
module_load_include('inc', 'd2d_server', 'includes/db');
module_load_include('inc', 'd2d_server', 'includes/io');
module_load_include('inc', 'd2d_server', 'includes/fetch');
module_load_include('inc', 'd2d_server', 'includes/search');
module_load_include('inc', 'd2d_server', 'includes/theme');
module_load_include('inc', 'd2d_server', 'includes/votingapi');
module_load_include('inc', 'd2d_server', 'includes/cron');
module_load_include('inc', 'd2d_server', 'includes/friendship');
/**
* Implements hook_entity_info().
*/
function d2d_server_entity_info() {
$return['d2d_entity'] = array(
'label' => t('D2D Server'),
'base table' => 'd2d_entity',
'entity keys' => array(
'id' => 'did',
'label' => 'title',
),
'entity class' => 'D2DEntity',
'uri callback' => 'entity_class_uri',
'controller class' => 'D2DEntityController',
'admin ui' => array(
'path' => 'admin/d2d_server',
'controller class' => 'D2DEntityUIController',
'file' => 'includes/d2d_entity.admin.inc',
),
'module' => 'd2d_server',
'access callback' => 'd2d_server_admin_access',
'fieldable' => TRUE,
'bundles' => array(
'd2d_entity' => array(
'label' => t('d2d entity bundle'),
'admin' => array(
'path' => 'admin/d2d_server',
'access arguments' => array('administer d2d server'),
),
),
),
);
return $return;
}
/**
* accesss callback function for hook_entity_info().
*/
function d2d_server_admin_access($op, $profile = NULL, $account = NULL) {
if (user_access('administer d2d server', $account)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_permission().
*/
function d2d_server_permission() {
return array(
'administer d2d server' => array(
'title' => t('Administer D2D Server'),
),
'view d2d server' => array(
'title' => t('View D2D Server list and page'),
),
);
}
/**
* Implements hook_block_info().
*/
function d2d_server_block_info() {
$blocks = array();
$blocks['d2d_server_description'] = array(
'info' => t('What is D2D?'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function d2d_server_block_view($delta='') {
$block = array();
switch ($delta) {
case 'd2d_server_description':
$block['subject'] = t('What is D2D');
$block['content'] = _d2d_server_block();
break;
}
return $block;
}
/**
*/
function d2d_server_menu() {
$items['admin/d2d_server/fetchClientInfo'] = array(
'title' => 'Fetch data from Clients',
'description' => 'Fetch the latest data from all d2d friend instances on which d2d_client is installed.',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('d2d_fetchClientInfo_form'),
'access callback' => TRUE,
);
//main page.
$items['d2d_server_homepage'] = array(
'title' => 'List of all d2d instances',
'description' => 'the list of all d2d instances',
'page callback' => '_d2d_server_all_list_page',
'page arguments' => array(),
'access arguments' => array('view d2d server'),
);
//d2d instance's view page. "d2d/$did"
//if success, go to d2d/$did page.
//if not(the $pid is wrong), go to the main page.
$items['d2d/%'] = array(
'title' => 'd2d instance view',
'description' => 'd2d instance view page',
'page callback' => '_d2d_instance_view_page',
'page arguments' => array(1),
'access arguments' => array('view d2d server'),
);
//this one is d2d instance's voting function.
//voting: if success, go to d2d/$did page.
// if not, go back to main page.(wrong d2d/$did will redirect to the main page.)
$items['d2d/%/voting'] = array(
'title' => 'd2d instance voting link',
'description' => 'd2d instance voting link',
'page callback' => '_d2d_server_voting',
'page arguments' => array(1),
'access arguments' => array('view d2d server'),
);
$items['d2d/%/undovote'] = array(
'title' => 'd2d instance undo vote before',
'description' => 'd2d instance undo vote before',
'page callback' => '_d2d_server_undo_vote',
'page arguments' => array(1),
'access arguments' => array('view d2d server'),
);
$items['d2d/category/%'] = array(
'title' => 'all d2d instances in % category',
'description' => 'all d2d instances in % category',
'page callback' => '_d2d_server_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view d2d server'),
);
$items['d2d/tags/%/%'] = array(
'title' => 'all d2d instances with the tag %',
'description' => 'all d2d instances with the tag %',
'page callback' => '_d2d_server_list_basicpage',
'page arguments' => array(2, 3),
'access arguments' => array('view d2d server'),
);
//this is search function. search text & search type.
//actually its result is composed by the above four basic kinds of table.
$items['d2d/search/%/%'] = array(
'title' => 'search d2d instances',
'description' => 'search d2d instances with value/category, and so on',
'page callback' => '_d2d_server_search',
'page arguments' => array(2, 3),
'access arguments' => array('view d2d server'),
);
$items['d2d_server/get_actions'] = array(
'title' => 'D2D Server Get Actions',
'description' => 'Outputs information about the actions sent by clients using d2d_passport notifications.',
'page callback' => '_d2d_server_get_actions_service',
'access arguments' => array('view d2d server'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
*/
function _d2d_server_all_list_page() {
$path = drupal_get_path('module', 'd2d_server');
drupal_add_js(array(
'clean_url' => variable_get('clean_url', 0),
), 'setting');
drupal_add_js('https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false');
drupal_add_js($path . '/visualization/js/main.js');
drupal_add_css($path . '/styles/css/mainpage.css');
$list = "<div id='d2d-server-world-usage-map'></div>";
$searchform = drupal_get_form('d2d_server_search');
$list .= drupal_render($searchform);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'd2d_entity')
->propertyOrderBy('created', 'DESC')
->execute();
$list .= "<div id='d2d-server-all-table-wrap'>";
$vscore = array();
if (isset($query->ordered_results) && count($query->ordered_results)) {
$ordered_results_backup = $query->ordered_results;
//Most Recent Web Sites.
$query->ordered_results = array_slice($ordered_results_backup, 0, 10, TRUE);
$fieldset_title = 'Recently Added Web Sites:';
$list .= _d2d_server_list_page($query, $fieldset_title);
//Most Liked Web Sites.
foreach ($ordered_results_backup as $key => $value) {
$vscore[$key] = _d2d_server_get_voting_score($value->entity_id);
}
arsort($vscore);
$new_ordered_results = array();
foreach ($vscore as $key => $value) {
$new_ordered_results[] = $ordered_results_backup[$key];
}
$query->ordered_results = array_slice($new_ordered_results, 0, 10, TRUE);
$fieldset_title = 'Most Liked Web Sites:';
$list .= _d2d_server_list_page($query, $fieldset_title);
}
else {
//drupal_add_js($path . '/styles/js/patterns_server.firstpage.js');
$list .= "<div class='d2d-server-list-table-wrap'>";
$url = 'admin/d2d_server/add';
$list .= t('There is no d2d instance in the database. !br Check whether whether you can import some !friends.', array(
'!br' => '<br/>',
'!friends' => l(t('friends'), $url, array(
'attributes' => array('class' => array('add-button-link')),
)),
));
$list .= '</div>';
}
$list .= '</div>';
return $list;
}
/**
*/
function _d2d_instance_view_page($d2did) {
//$a = d2d_server_create_d2dentity($d2did);
//$a = d2d_api_own_instance_get();
//dsm($a);
$de = d2d_server_get_d2dentity_by_d2did($d2did);
if (!$de) {
drupal_set_message(t("The d2d instance doesn't exist."), "error");
drupal_goto('d2d_server_homepage');
}
$did = $de->did;
drupal_set_title($de->title);
$fieldset_title = $de->title;
drupal_add_css(drupal_get_path('module', 'd2d_server') . '/styles/css/viewpage.css');
drupal_add_js(drupal_get_path('module', 'd2d_server') . '/styles/js/viewpage.js');
$backtoserver_voting_link = _d2d_server_back_like_button($did, $d2did);
$content = "<div id='one_d2d_instance'><fieldset><legend>$fieldset_title$backtoserver_voting_link</legend><div class='fieldset-wrapper'>";
$friends = d2d_server_get_friends_from_friendship($d2did);
$content .= _d2d_instance_view_page_description($de, $friends);
if ($friends != FALSE) {
$header = array(
t('Friend D2D ID'),
t('Friend URL'),
);
$rows = array();
foreach ($friends as $friend) {
$row = array();
if ($friend['alreadyinserver'] == 1) {
$row[] = l($friend['friend_d2did'], 'd2d/' . $friend['friend_d2did']);
}
else {
$row[] = $friend['friend_d2did'];
}
$url = substr($friend['friend_url'], 0, -10);
$row[] = l($url, $url);
$rows[] = $row;
}
$content .= theme('table', array('header' => $header, 'rows' => $rows));
}
$content .= '</div></fieldset></div>';
$field_view = entity_view('d2d_entity', array($did => $de), 'full', NULL, TRUE);
$content .= drupal_render($field_view);
return $content;
}
/**
*/
function _d2d_server_list_basicpage($property, $arg) {
drupal_set_title($property . ': ' . $arg);
$searchform = drupal_get_form('d2d_server_search');
$searchform = drupal_render($searchform);
$list = _d2d_server_list($property, $arg);
if ($list === FALSE) {
drupal_set_message(t("The d2d instances list doesn't exist."), "error");
drupal_goto('d2d_server_homepage');
}
else {
return $searchform . "<div id='d2d-server-all-table-wrap'>" . $list . "</div>";
}
}
/**
* this function return a html table.
* style: <div class='d2d-server-list-table-wrap'>a table</div>
* this is core function.
* if it get a list table, it return the table. if not, it return FALSE.
* it can handle menus:
* d2d/tags/$field_tags_name/$tid ====> $property: field_tags_name, $arg: $tid.
* d2d/category/[System] ====> $property: category, $arg: [System].
* if can hanndle another pid $property and $pid $arg.
* all five kinds of property: four menus and the special pid $property.
*
* these two are the basic table, a page can contain several tables. that
* means this function can be called any times. the output is a <div>table</div>.
*
*/
function _d2d_server_list($property, $arg, $operate = '=', $showupload = TRUE) {
$pos = strpos($property, 'field');
$property_sign = $property;
if ($pos !== FALSE) {
$property_sign = 'tag';
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'd2d_entity');
switch ($property_sign) {
case 'tag':
$tax = taxonomy_term_load($arg);
$fieldset_title = "tag: " . $tax->name;
//$fieldset_title = "tag: " . $tax->name . " in $property field";
$query->fieldCondition($property, 'tid', $arg, '=');
break;
default:
$fieldset_title = $property . ': ' . $arg;
$query->propertyCondition($property, $arg, $operate);
}
$query->propertyOrderBy('created', 'DESC')
->pager(10)
->execute();
if (isset($query->ordered_results) && count($query->ordered_results)) {
$list = _d2d_server_list_page($query, $fieldset_title);
return $list;
}
else {
return FALSE;
}
}
/**
*/
function _d2d_server_block() {
$s = "<p><strong>";
$s .= "Welcome to Drupal 2 Drupal!";
$s .= "</strong></p>";
$s .= "<p>";
$s .= "Discover relevant ";
$s .= l("Drupal", "http://www.drupal.org");
$s .= " communities, by browsing the archive of registered web sites.";
$s .= "</p>";
$s .= "<p>";
$s .= "Add them as a friend, and start sharing content, updates, and much more!";
$s .= "</p>";
$s .= "<p>";
$s .= "Join the Drupal2Drupal network ";
$s .= l("now", "content/about");
$s .= ".</p>";
return $s;
}