forked from developmentseed/FeatureServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fserver.module
229 lines (208 loc) · 8.52 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
<?php
include_once('fserver.features.inc');
/**
* Implementation of hook_link().
*/
function fserver_link($type, $object, $teaser = FALSE) {
// Only allow releases to be added if packaging method is 0 => 'Manual'.
$links = array();
if ($type == 'node' && $object->type == 'fserver_project' && empty($object->field_fserver_method[0]['value'])) {
$item = menu_get_item('node/add/fserver-release');
if ($item && $item['access']) {
$links['fserver-add-release'] = array(
'title' => t('Add new @release', array('@release' => strtolower($item['title']))),
'href' => 'node/add/fserver-release',
);
}
}
return $links;
}
/**
* Implementation of hook_nodeapi().
*/
function fserver_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'view' && $node->type == 'fserver_distro' && menu_get_object()->nid === $node->nid) {
//$node->content['field_fserver_drushmake'][0]['value'] = '<code>'. $node->field_fserver_drushmake[0]['value'] .'</code>';
$node->content['field_fserver_drushmake']['field']['items'][0]['#item']['safe'] = '<fieldset>'. implode('<br />', explode("\r\n", $node->content['field_fserver_drushmake']['field']['items'][0]['#item']['safe'])) .'</fieldset>';
}
if ($op == 'view' && $node->type == 'fserver_project' && menu_get_object()->nid === $node->nid) {
$url = url("fserver", array('purl' => array('disabled' => TRUE), 'absolute' => TRUE));
$project = check_plain($node->field_fserver_name[0]['value']);
$node->content['project_info'] = array(
'#type' => 'fieldset',
'info' => array(
'#access' => node_access('update', $node),
'#title' => t("For <code>@project.info</code>", array('@project' => $project)),
'#type' => 'item',
'#value' => "<code>project status url = {$url}</code>",
),
'drush_make' => array(
'#title' => t("For <code>drush make</code> makefiles"),
'#type' => 'item',
'#value' => "<code>projects[{$project}][location] = {$url}</code>",
),
'#weight' => -10,
);
}
else if ($op == 'presave' && $node->type == 'fserver_project') {
if (empty($node->field_fserver_method[0]['value'])) {
$node->field_fserver_repository[0]['value'] = '';
}
}
else if ($op == 'presave' && $node->type == 'fserver_release') {
$node->title = fserver_generate_version($node);
// If this release is recommended, set all other ones to not recommended.
if ($node->field_fserver_recommended[0]['value'] == 1) {
$view = views_get_view('fserver_releases');
$view->set_arguments(array($node->field_fserver_project[0]['nid'], $node->field_fserver_api[0]['value']));
$view->execute('block_1');
foreach ($view->result as $data) {
if (!isset($node->nid) || $data->nid != $node->nid) {
$release = node_load($data->nid);
$release->field_fserver_recommended[0]['value'] = 0;
node_save($release);
}
}
}
}
else if ($op == 'view' && $node->type == 'fserver_release') {
drupal_add_css(drupal_get_path('module', 'fserver') . '/fserver.css');
$download_url = url($node->field_fserver_file[0]['filepath'], array('purl' => array('disabled' => TRUE), 'absolute' => TRUE));
$contents = file_exists($node->field_fserver_file[0]['filepath']) ? file_get_contents($node->field_fserver_file[0]['filepath']) : '';
$info = array();
$info[] = t('Download: !url', array('!url' => l($download_url, $download_url, array('absolute' => TRUE))));
$info[] = t('md5 hash: !md5', array('!md5' => md5($contents)));
$info[] = t('Size: !size', array('!size' => format_size($node->field_fserver_file[0]['filesize'])));
$node->content['fserver_info'] = array(
'#type' => 'fieldset',
'content' => array(
'#type' => 'markup',
'#value' => theme('item_list', $info, NULL, 'ul', array('id' => 'fserver-release-info')),
),
'#weight' => -10,
);
$project = node_load($node->field_fserver_project[0]['nid']);
drupal_set_breadcrumb(array(l(t('Home'), NULL), l($project->title, 'node/' . $project->nid)));
}
}
/**
* Option field callback for version options.
*/
function fserver_cck_options($type = 'core') {
switch ($type) {
case 'core':
$return = drupal_map_assoc(array('5.x', '6.x', '7.x'));
break;
case 'major':
$major = range(0, 50);
unset($major[0]);
$return = $major;
break;
case 'method':
$return = array(
0 => t('Manual upload'),
'git' => t('GIT repository tags'),
'svn' => t('SVN repository tags folder'),
);
break;
case 'patch':
$return = range(0, 50);
break;
case 'recommended':
$return = array(t('Not recommended'), t('Recommended'));
break;
case 'security':
$return = array(t('Not a security release'), t('Security release'));
break;
case 'type':
// We don't use t() here as the update XML requires these strings to be uniform.
$return = array(
// 'core' => 'Drupal project',
'profile' => 'Installation profiles',
'module' => 'Modules',
'theme' => 'Themes',
// 'theme engine' => 'Theme engines',
// 'translation' => 'Translations',
);
break;
}
drupal_alter('fserver_cck_options', $return, $type);
return $return;
}
/**
* Parse a package name into values: core, major, patch and extra.
*/
function fserver_parse_package_name($package_filename) {
// example: fserver-6.x-2.0-rc3.tar.gz
if (preg_match('/^(?<project>\w+)-(?<core>\d+\.x)-(?<major>\d+)\.(?<patch>\d+)-?(?<extra>\w+)?\.(tar\.gz|tgz)$/', $package_filename, $matches)) {
return $matches;
};
return array();
}
/**
* Parse a package name into values: core, major, patch and extra.
*/
function fserver_parse_tag_name($tag) {
// example: drupal-6--1-0-alpha1
if (preg_match('/^(?P<project>\w+)-(?P<core>\d+)--(?P<major>\d+)-(?P<patch>\d+)-?(?P<extra>\w+)?$/', $tag, $matches)) {
$parsed = array_intersect_key($matches, array('core' => '', 'major' => '', 'patch' => '', 'extra' => ''));
if (!empty($parsed)) {
$parsed['core'] = "{$parsed['core']}.x";
$parsed['original'] = $tag;
$parsed['version'] = "{$parsed['core']}-{$parsed['major']}.{$parsed['patch']}" . (!empty($parsed['extra']) ? "-{$parsed['extra']}" : '');
return $parsed;
}
}
return array();
}
/**
* Implementation of hook_form_alter() for fserver_project_node_form.
*/
function fserver_form_fserver_project_node_form_alter(&$form, $form_state) {
drupal_add_js(drupal_get_path('module', 'fserver') . '/fserver.js');
$form['#validate'][] = 'fserver_project_node_form_validate';
}
/**
* Validate handler for fserver_project_node_form.
*/
function fserver_project_node_form_validate($form, &$form_state) {
// Ensure repository URL is set if using a non-manual method.
if (!empty($form_state['values']['field_fserver_method'][0]['value']) && empty($form_state['values']['field_fserver_repository'][0]['value'])) {
form_set_error($form_state['values']['field_fserver_repository'][0]['_error_element'], t('Please provide a valid repository URL for this project.'));
}
}
/**
* Implementation of hook_form_alter() for fserver_release_node_form.
*/
function fserver_form_fserver_release_node_form_alter(&$form, $form_state) {
drupal_add_css(drupal_get_path('module', 'fserver') . '/fserver.css');
$form['title'] = array(
'#type' => 'value',
'#value' => 'VOID',
);
$form['#submit'][] = 'fserver_release_node_form_submit';
}
/**
* Submit handler for fserver_release_node_form.
*/
function fserver_release_node_form_submit($form, &$form_state) {
// Only do something, if the user clicked the preview button.
if (isset($form_state['clicked_button']['#submit']) && in_array('node_form_build_preview', $form_state['clicked_button']['#submit'])) {
$node = node_submit($form_state['values']);
$node->title = fserver_generate_version($node);
$form_state['values'] = (array) $node;
}
}
/**
* Generate a standard version string from a node object.
* @TODO: Merge this or make use of it in fserver_handler_field_release_tag::render().
*/
function fserver_generate_version($node, $raw = FALSE) {
$project = node_load($node->field_fserver_project[0]['nid']);
$core = $node->field_fserver_api[0]['value'];
$major = $node->field_fserver_versionmajor[0]['value'];
$patch = $node->field_fserver_versionpatch[0]['value'];
$extra = $node->field_fserver_versionextra[0]['value'];
$version = "{$core}-{$major}.{$patch}" . (!empty($extra) ? "-{$extra}" : '');
return $raw ? $version : "{$project->title} $version";
}