-
Notifications
You must be signed in to change notification settings - Fork 5
/
fserver.module
555 lines (509 loc) · 17 KB
/
fserver.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
/**
* @file
* Code for the Feature Server feature.
*/
include_once 'fserver.features.inc';
include_once 'includes/fserver.entity.inc';
include_once 'includes/fserver.entity_type.inc';
define("FSERVER_VIEW_NAME_PROJECTS_ALL", 'fserver_projects');
define("FSERVER_VIEW_NAME_PROJECTS_SINGLE", 'fserver_project');
/**
* Implements hook_permission().
*/
function fserver_permission() {
$permissions = array();
$permissions['administer fserver'] = array(
'title' => t('Administer Feature Server'),
'description' => t('Perform administration tasks for Feature Server.'),
);
$permissions['create fserver projects'] = array(
'title' => t('Create projects of any type'),
'description' => t('Create any type of Feature Server project entities.'),
);
$types = project_type_load();
foreach ($types as $type) {
$permissions['view own fserver ' . $type->type . ' projects'] = array(
'title' => t('View own fserver @type project type entities', array('@type' => $type->type)),
'description' => t('View own projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['view any fserver ' . $type->type . ' projects'] = array(
'title' => t('View any fserver @type project type entities', array('@type' => $type->type)),
'description' => t('View any projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['create fserver ' . $type->type . ' projects'] = array(
'title' => t('Create fserver @type project type entities', array('@type' => $type->type)),
'description' => t('Create projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['edit own fserver ' . $type->type . ' projects'] = array(
'title' => t('Edit own fserver @type project type entities', array('@type' => $type->type)),
'description' => t('Edit own projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['edit any fserver ' . $type->type . ' projects'] = array(
'title' => t('Edit any fserver @type project type entities', array('@type' => $type->type)),
'description' => t('Edit any projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['delete own fserver ' . $type->type . ' projects'] = array(
'title' => t('Delete own fserver @type project type entities', array('@type' => $type->type)),
'description' => t('Delete own projects of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['delete any fserver ' . $type->type . ' projects'] = array(
'title' => t('Delete any fserver @type project type entities', array('@type' => $type->type)),
'description' => t('Delete any projects of type @type within Feature Server', array('@type' => $type->type)),
);
}
$types = release_type_load();
foreach ($types as $type) {
$permissions['view own fserver ' . $type->type . ' releases'] = array(
'title' => t('View own fserver @type release type entities', array('@type' => $type->type)),
'description' => t('View own releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['view any fserver ' . $type->type . ' releases'] = array(
'title' => t('View fserver @type release type entities', array('@type' => $type->type)),
'description' => t('View any releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['create fserver ' . $type->type . ' releases'] = array(
'title' => t('Create fserver @type release type entities', array('@type' => $type->type)),
'description' => t('Create releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['edit own fserver ' . $type->type . ' releases'] = array(
'title' => t('Edit own fserver @type release type entities', array('@type' => $type->type)),
'description' => t('Edit own releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['edit any fserver ' . $type->type . ' releases'] = array(
'title' => t('Edit any fserver @type release type entities', array('@type' => $type->type)),
'description' => t('Edit any releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['delete own fserver ' . $type->type . ' releases'] = array(
'title' => t('Delete own fserver @type release type entities', array('@type' => $type->type)),
'description' => t('Delete own releases of type @type within Feature Server', array('@type' => $type->type)),
);
$permissions['delete any fserver ' . $type->type . ' releases'] = array(
'title' => t('Delete any fserver @type release type entities', array('@type' => $type->type)),
'description' => t('Delete any releases of type @type within Feature Server', array('@type' => $type->type)),
);
}
return $permissions;
}
/**
* Implements hook_entity_info().
*/
function fserver_entity_info() {
$info = array();
$info['project_type'] = array(
'label' => t('Project Type'),
'controller class' => 'EntityAPIControllerExportable',
'entity class' => 'Entity',
'base table' => 'fserver_project_type',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => 'description',
'name' => 'type',
'uuid' => 'uuid'
),
'bundle of' => 'project',
'exportable' => TRUE,
'export' => array(
'default hook' => 'default_project_type',
),
'uuid' => TRUE,
'module' => 'fserver',
'access callback' => 'project_type_access',
'entity cache' => module_exists('entitycache'),
'metadata controller class' => 'FserverProjectTypeMetadataController',
'admin ui' => array(
'path' => 'admin/structure/project_types',
'file' => 'includes/fserver.admin.inc',
),
);
$info['release_type'] = array(
'label' => t('Release Type'),
'controller class' => 'EntityAPIControllerExportable',
'entity class' => 'Entity',
'base table' => 'fserver_release_type',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => 'description',
'name' => 'type',
'uuid' => 'uuid'
),
'bundle of' => 'release',
'exportable' => TRUE,
'export' => array(
'default hook' => 'default_release_type',
),
'uuid' => TRUE,
'module' => 'fserver',
'access callback' => 'release_type_access',
'entity cache' => module_exists('entitycache'),
'metadata controller class' => 'FserverReleaseTypeMetadataController',
'admin ui' => array(
'path' => 'admin/structure/release_types',
'file' => 'includes/fserver.admin.inc',
),
);
$info['project'] = array(
'label' => t('Project'),
'entity class' => 'FserverProject',
'controller class' => 'FserverProjectController',
'base table' => 'fserver_project',
'fieldable' => TRUE,
'uuid' => TRUE,
'uri callback' => 'project_uri',
'entity cache' => module_exists('entitycache'),
'entity keys' => array(
'id' => 'id',
'label' => 'title',
'bundle' => 'type',
'uuid' => 'uuid',
'name' => 'name',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'module' => 'fserver',
'access callback' => 'project_access',
'metadata controller class' => 'FserverProjectMetadataController',
'inline entity form' => array(
'controller' => 'FserverProjectEntityInlineEntityFormController'
),
'admin ui' => array(
'path' => 'admin/content/projects',
'file' => 'includes/fserver.admin.inc',
'controller class' => 'EntityBundleableUIController',
'menu wildcard' => '%project',
),
);
$info['release'] = array(
'label' => t('Release'),
'entity class' => 'FserverRelease',
'controller class' => 'FserverReleaseController',
'base table' => 'fserver_release',
'fieldable' => TRUE,
'uuid' => TRUE,
'uri callback' => 'release_uri',
'entity cache' => module_exists('entitycache'),
'entity keys' => array(
'id' => 'id',
'label' => 'name',
'bundle' => 'type',
'uuid' => 'uuid',
'name' => 'name',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'module' => 'fserver',
'access callback' => 'release_access',
'metadata controller class' => 'FserverReleaseMetadataController',
'inline entity form' => array(
'controller' => 'FserverReleaseEntityInlineEntityFormController',
),
'admin ui' => array(
'path' => 'admin/content/releases',
'file' => 'includes/fserver.admin.inc',
'controller class' => 'EntityBundleableUIController',
'menu wildcard' => '%release',
),
);
if (db_table_exists('fserver_project_type')) {
$project_bundles = db_select('fserver_project_type', 'pt')
->fields('pt')
->execute()
->fetchAllAssoc('type');
foreach ($project_bundles as $type_name => $type) {
$info['project']['bundles'][$type_name] = array(
'label' => $type->description,
'admin' => array(
'path' => 'admin/structure/project_types/manage/%project_type',
'real path' => 'admin/structure/project_types/manage/' . $type->type,
'bundle argument' => 4,
'access arguments' => array('administer fserver'),
),
);
}
}
if (db_table_exists('fserver_release_type')) {
$release_bundles = db_select('fserver_release_type', 'rt')
->fields('rt')
->execute()
->fetchAllAssoc('type');
foreach ($release_bundles as $type_name => $type) {
$info['release']['bundles'][$type_name] = array(
'label' => $type->description,
'admin' => array(
'path' => 'admin/structure/release_types/manage/%release_type',
'real path' => 'admin/structure/release_types/manage/' . $type->type,
'bundle argument' => 4,
'access arguments' => array('administer fserver'),
),
);
}
}
return $info;
}
// @todo add with something similar to https://git.dropfort.com/dropfort/dropfort_project/blob/7.x-1.x/dropfort_project.module#L622
/**
* Implements hook_menu_alter().
*/
function fserver_menu_alter(&$items) {
// Example - disable the page at node/add
$items['admin/content/projects']['type'] = MENU_LOCAL_TASK;
$items['admin/content/releases']['type'] = MENU_LOCAL_TASK;
}
/**
* Implements hook_field_formatter_info().
*/
function fserver_field_formatter_info() {
return array(
'file_size' => array(
'label' => t('File Size'),
'field types' => array('file'),
),
'file_hash_md5' => array(
'label' => t('File MD5'),
'field types' => array('file'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function fserver_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'file_size':
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => $item['filesize']);
}
break;
case 'file_hash_md5':
foreach ($items as $delta => $item) {
if (isset($item['filehash']['md5'])) {
$element[$delta] = array('#markup' => $item['filehash']['md5']);
}
else {
watchdog('fserver', 'No MD5 hash exists for fid: @fid', array('@fid' => $item['fid']), WATCHDOG_WARNING);
}
}
break;
}
return $element;
}
/**
* Generate the XML for a file field
*/
function _fserver_release_file_format($item) {
$archive_type = '';
if ('.tar.gz' == drupal_substr($item['filename'], -7)) {
$archive_type = 'tar.gz';
}
elseif ('.tar' == drupal_substr($item['filename'], -4)) {
$archive_type = 'tar';
}
elseif ('.tgz' == drupal_substr($item['filename'], -4)) {
$archive_type = 'tgz';
}
elseif ('.zip' == drupal_substr($item['filename'], -4)) {
$archive_type = 'zip';
}
$file['archive_type'] = $archive_type;
$file['url'] = file_create_url($item['uri']);
$file['md5'] = $item['filehash']['md5'];
$file['size'] = check_plain($item['filesize']);
$file['filedate'] = $item['timestamp'];
return $file;
}
/**
* Check if project exists
*
* @param string $shortname
* Project short name
*
* @return boolean
* Returns TRUE if a project by that name exists, FALSE otherwise.
*/
function fserver__project_exists($shortname) {
$query = new EntityFieldQuery();
$project = $query
->EntityCondition("entity_type", "project")
->propertyCondition("short_name", $shortname)
->execute();
if (!isset($project['project'])) {
return FALSE;
}
else {
return TRUE;
}
}
/**
* Create an fserver project
*
* $project array(
* 'title' => 'Long Project Tile',
* 'type' => 'module', // Either module, distribution, or theme
* 'short_name' => 'project_name',
* 'recommended_major' => 1, // Optional
* 'supported_majors' => '1', // Optional
* 'link' => 'project link', // Optional
* 'project_status' => 'published', // Optional
* );
*/
function fserver__project_create($project) {
$entity = entity_create('project', $project);
if ($entity === FALSE) {
return FALSE;
}
return entity_save('project', $entity);
}
/**
* Load an fserver project by shortname
*
* @param string $shortname
* Project shortname
*/
function fserver__project_load($shortname) {
$query = new EntityFieldQuery();
$project = $query
->EntityCondition("entity_type", "project")
->propertyCondition("short_name", $shortname)
->execute();
if (!isset($project['project'])) {
return FALSE;
}
$project = array_keys($project['project']);
$id = reset($project);
return project_load($id);
}
/**
* Update an fserver project
*/
function fserver__project_update($project) {
return entity_save('project', (object) $project);
}
/**
* Delete an fserver project
*/
function fserver__project_delete($shortname) {
$project = fserver__project_load($shortname);
if ($project === FALSE) {
return FALSE;
}
return entity_delete('project', $project->id);
}
/**
* Create an fserver release
*
* $release array(
* 'api' = '7.x',
* 'type' => 'release',
* 'version_major' => '1',
* 'version_patch' => '', // Optional
* 'version_extra' => 'dev', // Optional
* );
*/
function fserver__release_create($release) {
$entity = entity_create('release', $release);
if ($entity === FALSE) {
return FALSE;
}
$entity->generateReleaseName($release);
$existing_release = fserver__release_load($entity->name);
if ($existing_release) {
fserver__release_delete($entity->name);
}
// @todo - fix this: entity_save should return one of: SAVED_UPDATED or SAVED_NEW constants, but currently returns false
if (entity_save('release', $entity)) {
return $entity;
}
//return FALSE;
return $entity;
}
/**
* Load an fserver release
*/
function fserver__release_load($name) {
$query = new EntityFieldQuery();
$release = $query
->EntityCondition("entity_type", "release")
->propertyCondition("name", $name)
->execute();
if (!isset($release['release'])) {
return FALSE;
}
$release = array_keys($release['release']);
$release = entity_load('release', $release);
return array_pop($release);
}
/**
* Load all release entities for a given project
*
* Note that the dev release will also be the last item in the array.
*
* @param string $shortname
* Project short name
* @param string $api
* API version
* @return array|boolean
* Returns an array of entities, FALSE otherwise.
*/
function fserver__release_load_by_project($shortname, $api) {
$project = fserver__project_load($shortname);
if (!isset($project->id)) {
return FALSE;
}
$query = new EntityFieldQuery();
$releases = $query
->EntityCondition("entity_type", "release")
->fieldCondition("field_project_reference", "target_id", $project->id)
->propertyCondition("api", $api)
->propertyOrderBy('date', 'DESC')
->execute();
if (!isset($releases['release'])) {
return FALSE;
}
$releases = array_keys($releases['release']);
$releases = entity_load('release', $releases);
// Append dev release to the end of the array
foreach ($releases as $id => $release) {
if ($release->version_extra == 'dev') {
unset($releases[$id]);
$releases[] = $release;
break;
}
}
return $releases;
}
/**
* Update an fserver release
*/
function fserver__release_update($release) {
return entity_save('release', (object) $release);
}
/**
* Delete an fserver release
*/
function fserver__release_delete($name) {
$release = fserver__release_load($name);
if ($release === FALSE) {
return FALSE;
}
return entity_delete('release', $release->id);
}