-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatternentity.module
More file actions
384 lines (342 loc) · 12.7 KB
/
patternentity.module
File metadata and controls
384 lines (342 loc) · 12.7 KB
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
<?php
module_load_include('inc', 'patternentity', 'includes/patternentity.utility');
module_load_include('inc', 'patternentity', 'includes/patternentity.search');
module_load_include('inc', 'patternentity', 'includes/patternentity.theme');
module_load_include('inc', 'patternentity', 'includes/patternentity.blocks');
module_load_include('inc', 'patternentity', 'includes/patternentity.core');
module_load_include('inc', 'patternentity', 'includes/patternentity.io');
module_load_include('module', 'votingapi', 'votingapi');
/**
* Implements hook_entity_info().
*/
function patternentity_entity_info() {
$return['patternentity'] = array(
'label' => t('Pattern Entity'),
'base table' => 'patternentity',
'entity keys' => array(
'id' => 'pid',
'label' => 'title',
),
'entity class' => 'PatternEntity',
'uri callback' => 'entity_class_uri',
'controller class' => 'PatternEntityController',
'admin ui' => array(
'path' => 'admin/structure/patternentity',
'controller class' => 'PatternEntityUIController',
'file' => 'includes/patternentity.admin.inc',
),
'module' => 'patternentity',
'access callback' => 'patternentity_admin_access',
'fieldable' => TRUE,
'bundles' => array(
'patternentity' => array(
'label' => t('pattern entity bundle'),
'admin' => array(
'path' => 'admin/structure/patternentity',
'access arguments' => array('administer patternentity'),
),
),
),
'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 patternentity_admin_access($op, $profile = NULL, $account = NULL) {
if (user_access('administer patternentity', $account)) {
return true;
}
else if ($op == 'create' && user_access('upload patternentity', $account)) {
return true;
}
return false;
}
/**
* Implements hook_permission().
*/
function patternentity_permission() {
return array(
'administer patternentity' => array(
'title' => t('Administer patternentity'),
),
'view patternentity' => array(
'title' => t('View patternentity list and page'),
),
'upload patternentity' => array(
'title' => t('Upload pattern file to patternentity'),
),
);
}
/**
* Implements hook_block_info().
*/
function patternentity_block_info() {
$blocks = array();
$blocks['patternentity_prolific_author'] = array(
'info' => t('Most Prolific authors in patternentity'),
);
$blocks['patternentity_hottest_tags'] = array(
'info' => t('Hottest tags in patternentity'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function patternentity_block_view($delta='') {
$block = array();
switch ($delta) {
case 'patternentity_prolific_author':
$block['subject'] = t('Most Prolific Authors:');
$block['content'] = _pattern_entity_block_most_prolific_author();
break;
case 'patternentity_hottest_tags':
$block['subject'] = t('Most Popular Tags:');
$block['content'] = _pattern_entity_block_hottest_tags();
break;
}
return $block;
}
/**
* Implements hook_menu().
*/
function patternentity_menu() {
$items = array();
//main page.
//use the $property pid of _pattern_entity_list().
$items['all_pattern_entity_list'] = array(
'title' => 'all patternentity list ',
'description' => 'all patternentity list',
'page callback' => '_pattern_entity_all_list_page',
'page arguments' => array(),
'access arguments' => array('view patternentity'),
);
//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' => '_pattern_entity_view_page',
'page arguments' => array(1),
'access arguments' => array('view patternentity'),
);
//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' => '_pattern_entity_download_pattern',
'page arguments' => array(1),
'access arguments' => array('view patternentity'),
);
$items['pattern/%/voting'] = array(
'title' => 'pattern voting link',
'description' => 'pattern voting link',
'page callback' => '_pattern_entity_voting_pattern',
'page arguments' => array(1),
'access arguments' => array('view patternentity'),
);
//four basic list table page. they all use the core function: _pattern_entity_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' => '_pattern_entity_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patternentity'),
);
$items['pattern/category/%'] = array(
'title' => 'all pattern in % category',
'description' => 'all pattern files in % category',
'page callback' => '_pattern_entity_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patternentity'),
);
$items['pattern/author/%'] = array(
'title' => 'all pattern belong to author %',
'description' => 'all pattern belong to author %',
'page callback' => '_pattern_entity_list_basicpage',
'page arguments' => array(1, 2),
'access arguments' => array('view patternentity'),
);
$items['pattern/tags/%/%'] = array(
'title' => 'all pattern with the tag %',
'description' => 'all pattern with the tag %',
'page callback' => '_pattern_entity_list_basicpage',
'page arguments' => array(2, 3),
'access arguments' => array('view patternentity'),
);
//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 patternentity',
'description' => 'search patternentity with value/category, and so on',
'page callback' => '_pattern_entity_search',
'page arguments' => array(2,3),
'access arguments' => array('view patternentity'),
);
//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' => '_pattern_entity_list_tags_all',
'page arguments' => array(2),
'access arguments' => array('view patternentity'),
);
return $items;
}
/**
* callback hook_menu() all_pattern_entity_list
* this is the main page, so I custom it. without using the _pattern_entity_list().
* this is considered with the efficient.
* the page contain three part: search, most recent, most liked.
*/
function _pattern_entity_all_list_page() {
drupal_add_js(drupal_get_path('module', 'patternentity') . '/styles/js/patternentity.search.js');
$searchform = drupal_get_form('patternentity_search');
$list = drupal_render($searchform);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'patternentity')
->propertyOrderBy('created', 'DESC')
->execute();
$list .= "<div id='pattern-entity-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 .= _pattern_entity_list_page($query, $fieldset_title);
//Most Liked Patterns.
foreach ($ordered_results_backup as $key => $value) {
$vscore[$key] = _pattern_entity_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 .= _pattern_entity_list_page($query, $fieldset_title);
}
else {
drupal_add_js(drupal_get_path('module', 'patternentity') . '/styles/js/patternentity.firstpage.js');
$list .= "<div class='pattern-entity-list-table-wrap'>";
$list .= "There is no pattern file in database.</br>Do you want to upload ";
$list .= _pattern_entity_upload_button();
$list .= " ?";
$list .= '<div id="patternentity-upload-form-js" class="hero-unit"></div>';
$list .= '</div>';
}
$list .= '</div>';
return $list;
}
/**
* callback hook_menu() for /pattern/pid
*/
function _pattern_entity_view_page($pid) {
$pe = entity_object_load($pid, 'patternentity');
if (!$pe) {
drupal_set_message("The pattern file doesn't exist.", "error");
//global $base_url;
//drupal_goto($base_url);
drupal_goto('all_pattern_entity_list');
}
drupal_set_title($pe->title);
$fieldset_title = $pe->title;
drupal_add_css(drupal_get_path('module', 'patternentity') . '/styles/css/patternentity.viewpage.css');
drupal_add_js(drupal_get_path('module', 'patternentity') . '/styles/js/patternentity.viewpage.js');
$backtoserver_voting_link = _pattern_entity_back_like_button($pid);
$content = "<div id='one_pattern_div'><fieldset><legend>$fieldset_title$backtoserver_voting_link</legend><div class='fieldset-wrapper'>";
$content .= _pattern_entity_view_pattern_description($pe);
$content .= '<div id="patternentity-page-pattern-field"><ul>';
$content .= _pattern_entity_parser_field_pattern($pe->pattern);
$content .= '</ul></div>';
$content .= '</div></fieldset></div>';
$field_view = entity_view('patternentity', array($pid => $pe));
$content .= drupal_render($field_view);
return $content;
}
/**
* callback hook_menu() pattern/pid/download
*/
function _pattern_entity_download_pattern($pid) {
$ety_obj = entity_object_load($pid, 'patternentity');
if(!$ety_obj) {
drupal_set_message("The pattern file doesn't exist.", "error");
drupal_goto('all_pattern_entity_list');
}
$public_dir_path = variable_get('file_public_path', conf_path() . '/files');
$file_path = $ety_obj->file_path;
//$file_path = substr_replace($ety_obj->file_path, $public_dir_path, 0, 8);
//dsm($file_path);
$file = file_get_contents($file_path);
if (!$file) {
return;
}
$downloads = $ety_obj->file_downloads + 1;
db_update('patternentity')
->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 _pattern_entity_list_basicpage($property, $arg) {
drupal_set_title($property . ': ' . $arg);
drupal_add_js(drupal_get_path('module', 'patternentity') . '/styles/js/patternentity.search.js');
$searchform = drupal_get_form('patternentity_search');
$searchform = drupal_render($searchform);
$list = _pattern_entity_list($property, $arg);
if ($list === false) {
drupal_set_message("The pattern list doesn't exist.", "error");
drupal_goto('all_pattern_entity_list');
}
else {
return $searchform . "<div id='pattern-entity-all-table-wrap'>" . $list . "</div>";
}
}
/**
* callback hook_menu() alltags list
*/
function _pattern_entity_list_tags_all($tid) {
$tag_name = _pattern_entity_tagname_by_tagid($tid);
drupal_set_title("Patterns with tags: " . $tag_name['name']);
drupal_add_js(drupal_get_path('module', 'patternentity') . '/styles/js/patternentity.search.js');
$searchform = drupal_get_form('patternentity_search');
$list = drupal_render($searchform);
$tags_fields_name = _pattern_entity_get_all_taxonomyfields_name('patternentity');
//dsm($tags_fields_name);
if ($tags_fields_name === false) {
drupal_set_message("The pattern list doesn't exist.", "error");
drupal_goto('all_pattern_entity_list');
}
$list .= "<div id='pattern-entity-all-table-wrap'>";
foreach ($tags_fields_name as $field_name) {
$list .= _pattern_entity_list($field_name, $tid);
}
$list .= "</div>";
return $list;
}