-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns_server.module
421 lines (375 loc) · 14.7 KB
/
patterns_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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* @file
* Patterns Server. Manages and visualizes Patterns entities.
*/
module_load_include('inc', 'patterns_server', 'includes/utility');
module_load_include('inc', 'patterns_server', 'includes/search');
module_load_include('inc', 'patterns_server', 'includes/theme');
module_load_include('inc', 'patterns_server', 'includes/blocks');
module_load_include('inc', 'patterns_server', 'includes/core');
module_load_include('inc', 'patterns_server', 'includes/io');
module_load_include('inc', 'patterns_server', 'includes/friendship');
module_load_include('module', 'votingapi', 'votingapi');
/**
* Implements hook_entity_info().
*/
function patterns_server_entity_info() {
$return['patterns_server'] = array(
'label' => t('Patterns Server'),
'base table' => 'patterns_server',
'entity keys' => array(
'id' => 'pid',
'label' => 'title',
),
'entity class' => 'PatternsServerEntity',
'uri callback' => 'entity_class_uri',
'controller class' => 'PatternsServerEntityController',
'admin ui' => array(
'path' => 'admin/patterns_server',
'controller class' => 'PatternsServerEntityUIController',
'file' => 'includes/patterns_server.admin.inc',
),
'module' => 'patterns_server',
'access callback' => 'patterns_server_admin_access',
'fieldable' => TRUE,
'bundles' => array(
'patterns_server' => array(
'label' => t('patterns server bundle'),
'admin' => array(
'path' => 'admin/patterns_server',
'access arguments' => array('administer patterns server'),
),
),
),
'view modes' => array(
'full pattern' => array(
'label' => t('full for pattern'),
'custom settings' => FALSE,
),
'teaser pattern' => array(
'label' => t('teaser for pattern'),
'custom settings' => FALSE,
),
),
);
return $return;
}
/**
* accesss callback function for hook_entity_info().
*/
function patterns_server_admin_access($op, $profile = NULL, $account = NULL) {
if (user_access('administer patterns server', $account)) {
return TRUE;
}
elseif ($op == 'create' && user_access('upload patterns server', $account)) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_permission().
*/
function patterns_server_permission() {
return array(
'administer patterns server' => array(
'title' => t('Administer Patterns Server'),
),
'view patterns server' => array(
'title' => t('View Patterns Server list and page'),
),
'upload patterns server' => array(
'title' => t('Upload pattern file to Patterns Server'),
),
);
}
/**
* Implements hook_block_info().
*/
function patterns_server_block_info() {
$blocks = array();
$blocks['patterns_server_prolific_author'] = array(
'info' => t('Most Prolific authors in Patterns Server'),
);
$blocks['patterns_server_hottest_tags'] = array(
'info' => t('Hottest tags in Patterns Server'),
);
$blocks['patterns_server_description'] = array(
'info' => t('What are Patterns?'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function patterns_server_block_view($delta='') {
$block = array();
switch ($delta) {
case 'patterns_server_prolific_author':
$block['subject'] = t('Most Prolific Authors:');
$block['content'] = _patterns_server_block_most_prolific_author();
break;
case 'patterns_server_hottest_tags':
$block['subject'] = t('Most Popular Tags:');
$block['content'] = _patterns_server_block_hottest_tags();
break;
case 'patterns_server_description':
$block['subject'] = t('What are Patterns?');
$block['content'] = _patterns_server_block_description();
break;
}
return $block;
}
/**
* Implements hook_menu().
*/
function patterns_server_menu() {
$items = array();
//main page.
//use the $property pid of _patterns_server_list().
$items['patterns_server_homepage'] = array(
'title' => 'List of all patterns',
'description' => 'the list of all patterns',
'page callback' => '_patterns_server_all_list_page',
'page arguments' => array(),
'access arguments' => array('view patterns server'),
);
//pattern's view page. "pattern/$pid"
//if success, go to pattern/$pid page.
//if not(the $pid is wrong), go to the main page.
$items['pattern/%'] = array(
'title' => 'pattern view',
'description' => 'pattern view page',
'page callback' => '_patterns_server_view_page',
'page arguments' => array(1),
'access arguments' => array('view patterns server'),
);
//these two are pattern's download&voting function.
//download: if success, download a pattern file.
// if not, go back to main page.
//voting: if success, go to pattern/$pid page.
// if not, go back to main page.(wrong pattern/$pid will redirect to the main page.)
$items['pattern/%/download'] = array(
'title' => 'pattern download link',
'description' => 'pattern download menu link',
'page callback' => '_patterns_server_download_pattern',
'page arguments' => array(1),
'access arguments' => array('view patterns server'),
);
$items['pattern/%/voting'] = array(
'title' => 'pattern voting link',
'description' => 'pattern voting link',
'page callback' => '_patterns_server_voting_pattern',
'page arguments' => array(1),
'access arguments' => array('view patterns server'),
);
$items['pattern/%/undovote'] = array(
'title' => 'pattern undovote link',
'description' => 'pattern undovote link',
'page callback' => '_patterns_server_undovote_pattern',
'page arguments' => array(1),
'access arguments' => array('view patterns server'),
);
//four basic list table page. they all use the core function "_patterns_server_list()";
//these four:
// if success, return page which contains two parts: search form & a pattern table.
// if not, go to the main page and give an error message.
$items['pattern/uploader/%'] = array(
'title' => 'uploader \'s all pattern',
'description' => 'all pattern files uploaded by user %',
'page callback' => '_patterns_server_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patterns server'),
);
$items['pattern/category/%'] = array(
'title' => 'all pattern in % category',
'description' => 'all pattern files in % category',
'page callback' => '_patterns_server_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patterns server'),
);
$items['pattern/author/%'] = array(
'title' => 'all pattern belong to author %',
'description' => 'all pattern belong to author %',
'page callback' => '_patterns_server_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patterns server'),
);
$items['pattern/tags/%/%'] = array(
'title' => 'all pattern with the tag %',
'description' => 'all pattern with the tag %',
'page callback' => '_patterns_server_list_basicpage',
'page arguments' => array(2, 3),
'access arguments' => array('view patterns server'),
);
//this is search function. search text & search type.
//actually its result is composed by the above four basic kinds of table.
$items['pattern/search/%/%'] = array(
'title' => 'search pattern',
'description' => 'search pattern with value/category, and so on',
'page callback' => '_patterns_server_search',
'page arguments' => array(2, 3),
'access arguments' => array('view patterns server'),
);
//not quite sure.
$items['pattern/alltags/%'] = array(
'title' => 'all pattern with the tag % in all fields',
'description' => 'all pattern with the tag % in all fields',
'page callback' => '_patterns_server_list_tags_all',
'page arguments' => array(2),
'access arguments' => array('view patterns server'),
);
//for relations among pattern files.
$items['patterns_server/get_random_history_tree'] = array(
'title' => 'Patterns Server Get a random histroy tree',
'description' => 'just fetch a random history tree from the index table',
'page callback' => '_patterns_server_get_random_history_tree_ajax',
'access arguments' => array('view patterns server'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* callback hook_menu() patterns_server_homepage
* this is the main page, so I custom it. without using the _patterns_server_list().
* this is considered with the efficient.
* the page contain three part: search, most recent, most liked.
*/
function _patterns_server_all_list_page() {
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.tree.js');
//_patterns_server_get_random_history_tree_ajax();
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.search.js');
drupal_add_css(drupal_get_path('module', 'patterns_server') .'/visualization/css/tree.css');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/visualization/js/jquery-ui.min.js');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/visualization/js/jquery.mousewheel.js');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/visualization/js/d3.js');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/visualization/js/d3-layout.js');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/visualization/js/tree.js');
$searchform = drupal_get_form('patterns_server_search');
$list = '<div id="patterns_server-patterns-tree-visualization" style="width:100%; height:300px;"></div>';
$list .= drupal_render($searchform);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'patterns_server')
->propertyOrderBy('created', 'DESC')
->execute();
$list .= "<div id='patterns-server-all-table-wrap'>";
$vscore = array();
if (isset($query->ordered_results) && count($query->ordered_results)) {
$ordered_results_backup = $query->ordered_results;
//Most Recent Patterns.
$query->ordered_results = array_slice($ordered_results_backup, 0, 10, TRUE);
$fieldset_title = 'Most Recent Patterns:';
$list .= _patterns_server_list_page($query, $fieldset_title);
//Most Liked Patterns.
foreach ($ordered_results_backup as $key => $value) {
$vscore[$key] = _patterns_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 Patterns:';
$list .= _patterns_server_list_page($query, $fieldset_title);
}
else {
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.firstpage.js');
$list .= "<div class='patterns-server-list-table-wrap'>";
$list .= "There is no pattern file in database.</br>Do you want to upload ";
$list .= _patterns_server_upload_button();
$list .= " ?";
$list .= '<div id="patterns-server-upload-form-js" class="hero-unit"></div>';
$list .= '</div>';
}
$list .= '</div>';
return $list;
}
/**
* callback hook_menu() for /pattern/uuuid
*/
function _patterns_server_view_page($uuuid) {
$pid = patterns_server_get_pid_by_uuuid($uuuid);
if ($pid === FALSE) {
drupal_set_message(t("The pattern file doesn't exist."), "error");
drupal_goto('patterns_server_homepage');
}
$pe = entity_object_load($pid, 'patterns_server');
drupal_set_title($pe->title);
$fieldset_title = $pe->title;
drupal_add_css(drupal_get_path('module', 'patterns_server') . '/styles/css/patterns_server.viewpage.css');
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.viewpage.js');
$backtoserver_voting_link = _patterns_server_back_like_button($pid, $uuuid);
$content = "<div id='one_pattern_div'><fieldset><legend>$fieldset_title$backtoserver_voting_link</legend><div class='fieldset-wrapper'>";
$content .= _patterns_server_view_pattern_description($pe);
$content .= '<div id="patterns-server-page-pattern-field"><ul>';
$content .= _patterns_server_parser_field_pattern($pe->pattern);
$content .= '</ul></div>';
$content .= '</div></fieldset></div>';
$field_view = entity_view('patterns_server', array($pid => $pe), 'full', NULL, TRUE);
$content .= drupal_render($field_view);
return $content;
}
/**
* callback hook_menu() pattern/pid/download
*/
function _patterns_server_download_pattern($uuuid) {
$pid = patterns_server_get_pid_by_uuuid($uuuid);
if ($pid === FALSE) {
drupal_set_message(t("The pattern file doesn't exist."), "error");
drupal_goto('patterns_server_homepage');
}
$ety_obj = entity_object_load($pid, 'patterns_server');
$public_dir_path = variable_get('file_public_path', conf_path() . '/files');
$file_path = $ety_obj->file_path;
$file = file_get_contents($file_path);
if (!$file) {
return;
}
$downloads = $ety_obj->file_downloads + 1;
db_update('patterns_server')
->condition('pid', $ety_obj->pid)
->fields(array('file_downloads' => $downloads))
->execute();
drupal_add_http_header("Content-type", " text/plain; charset=utf-8");
drupal_add_http_header("Content-Disposition", "attachment;filename=" . $ety_obj->file_name);
print $file;
}
/**
* callback hook_menu() four basic page.
*/
function _patterns_server_list_basicpage($property, $arg) {
drupal_set_title($property . ': ' . $arg);
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.search.js');
$searchform = drupal_get_form('patterns_server_search');
$searchform = drupal_render($searchform);
$list = _patterns_server_list($property, $arg);
if ($list === FALSE) {
drupal_set_message(t("The pattern list doesn't exist."), "error");
drupal_goto('patterns_server_homepage');
}
else {
return $searchform . "<div id='patterns-server-all-table-wrap'>" . $list . "</div>";
}
}
/**
* callback hook_menu() alltags list
*/
function _patterns_server_list_tags_all($tid) {
$tag_name = _patterns_server_tagname_by_tagid($tid);
drupal_set_title(t("Patterns with tags: " . $tag_name['name']));
drupal_add_js(drupal_get_path('module', 'patterns_server') . '/styles/js/patterns_server.search.js');
$searchform = drupal_get_form('patterns_server_search');
$list = drupal_render($searchform);
$tags_fields_name = _patterns_server_get_all_taxonomyfields_name('patterns_server');
if ($tags_fields_name === FALSE) {
drupal_set_message(t("The pattern list doesn't exist."), "error");
drupal_goto('patterns_server_homepage');
}
$list .= "<div id='patterns-server-all-table-wrap'>";
foreach ($tags_fields_name as $field_name) {
$list .= _patterns_server_list($field_name, $tid);
}
$list .= "</div>";
return $list;
}