-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathatrium_folders.module
411 lines (366 loc) · 12.5 KB
/
atrium_folders.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
<?php
include_once('atrium_folders.features.inc');
include_once('includes/atrium_folders.toolbox.inc');
include_once('atrium_folders.pages.inc');
include_once('atrium_folders.theme.inc');
define('DOCUMENT_FOLDER', 'folder');
define('DOCUMENT_FILE', 'file');
/**
* Implementation of hook_init().
*/
function atrium_folders_init() {
drupal_add_css(drupal_get_path('module', 'atrium_folders') .'/atrium_folders.css');
}
/**
* Implementation of hook_perm().
*/
function atrium_folders_perm() {
return array('import folder from filesystem');
}
/**
* Implementation of hook_help().
*/
function atrium_folders_help($path, $arg) {
if (($path == 'help#atrium_folders') || context_isset('context', 'spaces-feature-folders')) {
switch ($path) {
case 'node/add/folder':
case 'node/%/edit':
$help = '';
$help .= '<h3>'. t('Folders') .'</h3>';
$help .= '<p>'. t('Provide help here') .'</p>';
return $help;
case 'help#atrium_folders':
default:
$help = '';
$help .= '<h3>'. t('Folders') .'</h3>';
$help .= '<p>'. t('Provide help here') .'</p>';
return $help;
}
}
}
/**
* Implementation of hook_context_links_alter();
*/
function atrium_folders_context_links_alter(&$links) {
if (context_get('context', 'folders_listing') && isset($links['folder']) && module_exists('spaces') && $space = spaces_get_space()) {
$item = menu_get_item('node/add/folder');
$node = menu_get_object();
if ($item['access'] && !empty($node->book['mlid'])) {
$links['folder']['query'] = "parent={$node->book['mlid']}";
}
}
}
/**
* Implementation of hook_menu().
*/
function atrium_folders_menu() {
$items = array();
$items['folders'] = array(
'title' => 'Folders',
'page callback' => 'atrium_folders_overview',
'page arguments' => array(),
'access callback' => 'spaces_access_feature',
'access arguments' => array('view', 'atrium_folders'),
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'features',
'file' => 'atrium_folders.pages.inc',
);
$items['folders/toolbox/%/%/%'] = array(
'title' => 'Folders',
'page callback' => 'atrium_folders_toolbox',
'page arguments' => array(2, 3, 4),
'access callback' => 'spaces_access_feature',
'access arguments' => array('view', 'atrium_folders'),
'type' => MENU_CALLBACK,
'file' => 'atrium_folders.pages.inc',
);
$items['node/%node/tree'] = array(
'title' => 'Reorder',
'page callback' => 'atrium_folders_tree',
'page arguments' => array(1),
'access callback' => 'atrium_folders_tree_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
return $items;
}
/**
* Menu access callback.
* @see: atrium_folders_menu()
*/
function atrium_folders_tree($node) {
module_load_include('inc', 'book', 'book.admin');
$book = node_load($node->book['bid']);
return drupal_get_form('book_admin_edit', $book);
}
/**
* Menu access callback.
* @see: atrium_folders_menu()
*/
function atrium_folders_tree_access($node) {
if ($node->type == 'folder') {
module_load_include('inc', 'book', 'book.admin');
$book = node_load($node->book['bid']);
return _book_outline_access($book);
}
return FALSE;
}
/**
* Implementation of hook_form_alter().
*/
function atrium_folders_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'folder_node_form') {
// Change book strings
$form['book']['#title'] = t('Folder tree');
$form['book']['plid']['#prefix'] = t('<div id="edit-book-plid-wrapper"><em>No folder selected.</em>');
$form['book']['weight']['#description'] = t('Folders at a given level are ordered first by weight and then by name.');
$form['book']['bid']['#title'] = t('Folders');
$form['book']['bid']['#options']['new'] = '<'. t('Create a new folder') .'>';
$form['book']['bid']['#description'] = t('Your folder will be a part of the selected folder tree.');
$form['book']['pick-book']['#value'] = t('Change folder (update list of parents)');
$form['body_field']['body']['#rows'] = 2; // Reduce body height
$form['attachments']['#title'] = t('File upload'); // Change attachments strings
$form['attachments']['#collapsible'] = FALSE;
$form['attachments']['#collapsed'] = FALSE;
$form['attachments']['#description'] = t('Changes made to files are not permanent until you save this folder.');
$form['notifications']['#weight'] = 100; // Move notifications at the bottom
$form['subfolders'] = array(
'#title' => t('Subfolders'),
'#description' => t('Create multiple subfolders in one shot, one per line.'),
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#tree' => FALSE,
);
$form['subfolders']['folders'] = array(
'#type' => 'textarea',
'#title' => t('Subfolder names, one per line'),
'#description' => t('Subfolders you want to create, one per line. Empty lines will be ignored.'),
);
// If form is shown in a node view page then apply toolbar theme.
$menu_link = menu_get_item();
if ($menu_link['path'] == 'node/%') {
$form['basic'] = array(
'#title' => t('Basic information'),
'#description' => t('Basic information about the folder.'),
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#tree' => FALSE,
'#weight' => -50,
);
$form['basic']['title'] = $form['title'];
$form['basic']['body_field'] = $form['body_field'];
if (module_exists('dragndrop_uploads')) {
$form['attachments']['#description'] .= '<div class="drop-zone">Drop Zone <p>Drag and Drop your files here for a quick upload</p></div>';
}
if (function_exists('zip_open')) {
$form['zip_upload'] = array(
'#title' => t('Upload multiple files'),
'#description' => t('Upload a ZIP archive possibily containing multiple files and folders. The archive will be expanded and its contents will be placed in the current directory.'),
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#tree' => FALSE,
'#weight' => 150,
'#theme' => 'folder_toolbar_fieldset',
);
$form['zip_upload']['zip'] = array(
'#type' => 'file',
'#title' => t('ZIP archive'),
);
$form['#attributes'] = array('enctype' => 'multipart/form-data');
$form['#validate'] = array_merge(array('atrium_folders_import_validate'), $form['#validate']);
$form['#submit'] = array_merge(array('atrium_folders_import_submit'), $form['#submit']);
}
unset($form['title']);
unset($form['body_field']);
unset($form['book']);
unset($form['attachments']['wrapper']['files']);
$form['basic']['#theme'] =
$form['notifications']['#theme'] =
$form['subfolders']['#theme'] =
$form['attachments']['#theme'] = 'folder_toolbar_fieldset';
$form['#theme'] = array('folder_toolbar_form');
}
// Add #validate callbacks
$form['#validate'] = array_merge(array('atrium_folders_node_form_submit_validate'), $form['#validate']);
}
// Redirect to parent folder.
if ($form_id == 'node_delete_confirm') {
$node = node_load($form['nid']['#value']);
if ($node->type == 'folder') {
$menu_link = menu_link_load($node->book['plid']);
$form['#redirect'] = $menu_link['link_path'];
}
}
}
/**
* Implementation of #validate callback
*/
function atrium_folders_node_form_submit_validate(&$form, &$form_state) {
// Fix time conflict due to AJAX calls.
$form_state['values']['changed'] = time();
}
/**
* Implemenation of hook_nodeapi().
*/
function atrium_folders_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if ($op == 'insert' || $op == 'update') {
if($node->type == 'folder' && trim($node->folders)) {
foreach(explode("\n", $node->folders) as $folder) {
// Skip empty lines and sanitize names.
if ($folder = trim($folder)) {
$node_folder = new stdClass();
$node_folder->type = 'folder';
$node_folder->title = $folder;
$node_book = node_load($node->nid);
$_GET['parent'] = $node_book->book['mlid'];
node_object_prepare($node_folder);
node_save($node_folder);
}
}
}
}
}
/**
* Implementation of hook_theme()
*/
function atrium_folders_theme() {
return array(
'folder_toolbar_fieldset' => array(
'arguments' => array('element' => NULL),
),
'folder_toolbar_form' => array(
'arguments' => array('form' => NULL),
),
'folders_navigator' => array(
'template' => 'navigator',
'arguments' => array('node' => NULL, 'form' => NULL),
'path' => drupal_get_path('module', 'atrium_folders') .'/themes',
),
'toolbox_link' => array(
'template' => 'toolbox-link',
'arguments' => array('text' => NULL, 'dest' => NULL, 'dom_id' => NULL, 'class' => NULL),
'path' => drupal_get_path('module', 'atrium_folders') .'/themes',
),
'textfields' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Implementation of hook_preprocess_views_view_table()
*/
function atrium_folders_preprocess_views_view_table(&$vars) {
if ($vars['view']->name == 'folders_files') {
foreach ($vars['view']->result as $key => $row) {
$vars['row_classes'][$key][] = 'file-'. $row->fid;
}
}
}
/**
* Implementation of hook_preprocess_views_view_unformatted()
*/
function atrium_folders_preprocess_views_view_unformatted(&$vars) {
if ($vars['view']->name == 'folders_navigation') {
$trail = array();
$node = menu_get_object();
foreach (book_build_active_trail($vars['node']->book) as $item) {
$trail[] = $item['href'];
}
foreach ($vars['rows'] as $id => $row) {
if (in_array('node/'. $vars['view']->result[$id]->nid, $trail)) { // Append trail class
$vars['classes'][$id] .= ' trail';
$vars['classes_array'][$id][] = 'trail';
}
if ($_GET['q'] == 'node/'. $vars['view']->result[$id]->nid) { // Append active class
$vars['classes'][$id] .= ' active';
$vars['classes_array'][$id][] = 'active';
}
if (isset($vars['view']->result[$id]->book_menu_links_depth)) { // Append depth class
$depth = ' depth-'. $vars['view']->result[$id]->book_menu_links_depth;
$vars['classes'][$id] .= $depth;
$vars['classes_array'][$id][] = $depth;
}
}
}
}
/**
* Implementation of hook_preprocess_node()
*/
function atrium_folders_preprocess_node(&$vars) {
if ($vars['type'] == 'folder') {
$form = '';
$node = node_load($vars['node']->nid);
if (node_access('update', $node)) {
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form($node->type .'_node_form', $node);
}
$vars['content'] = theme('folders_navigator', $node, $form);
$vars['links'] = $vars['node']->title = $vars['title'] = '';
$vars['submitted'] = '';
}
}
/**
* Implementation of hook_folder_toolbox()
*/
function atrium_folders_folder_toolbox() {
$items = array();
$items[] = array(
'op' => 'move',
'type' => 'file',
'title' => t('Move'),
'description' => t('Move the file to another location.'),
'form callback' => 'atrium_folders_move_file',
'ajax callback' => 'atrium_folders_move_file_ajax_callback',
);
$items[] = array(
'op' => 'rename',
'type' => 'file',
'title' => t('Rename'),
'description' => t('Rename the file.'),
'form callback' => 'atrium_folders_rename_file',
'ajax callback' => 'atrium_folders_rename_file_ajax_callback',
);
$items[] = array(
'op' => 'delete',
'type' => 'file',
'title' => t('Delete'),
'description' => t('Delete the file.'),
'form callback' => 'atrium_folders_delete_file',
'ajax callback' => 'atrium_folders_delete_file_ajax_callback',
);
return $items;
}
/**
* Helper: get toolbox info.
*/
function _atrium_folders_get_toolbox_info($op, $type) {
foreach (module_invoke_all('folder_toolbox') as $toolbox) {
if ($toolbox['op'] == $op && $toolbox['type'] == $type) {
return $toolbox;
}
}
return array();
}
/**
* Helper: get node object from NID or FID
*/
function _atrium_folders_get_node($op, $type, $id) {
if ($type == 'folder') {
return node_load($id);
}
elseif ($type == 'file' && $result = db_query("SELECT nid FROM {upload} WHERE fid = %d ", $id)) {
$node = db_fetch_object($result);
return node_load($node->nid);
}
else {
return NULL;
}
}
/**
* Helper: return DOM id string.
*/
function _atrium_folders_dom_id() {
$args = func_get_args();
return implode('-', $args);
}