forked from rutcreate/node_weight
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_weight.module
467 lines (420 loc) · 15.1 KB
/
node_weight.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
<?php
// $Id$
/**
* @file
* Node weight module's administrative pages.
*
* Contains form building functions, submit handlers, and theme functions for
* the module's overview form, add and edit forms, and the delete confirmation
* form.
*/
function node_weight_permission() {
return array('administer node weight');
}
/**
* Implementation of hook_menu().
*/
function node_weight_menu() {
$items['admin/content/node_weight'] = array(
'title' => t('Node weight'),
'description' => t('Select content type for weight manager.'),
'page callback' => 'node_weight_type_form',
'access arguments' => array('administer node weight'),
'type' => MENU_NORMAL_ITEM,
'file' => 'node_weight.admin.inc',
);
$items['admin/settings/node_weight'] = array(
'title' => t('Node Weight'),
'description' => t('Configure Node Weight options.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('node_weight_admin_settings_form'),
'access arguments' => array('administer node weight'),
'type' => MENU_NORMAL_ITEM,
'file' => 'node_weight.admin.inc',
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function node_weight_theme() {
return array(
'node_weight_overview_form' => array(
'render element' => 'form'
)
);
}
/**
* Implementation of hook_node_xx().
*/
function node_weight_node_insert($node) {
if (node_weight_in_allow_type($node)) {
$node->url = empty($node->url) ? 'node/'. $node->nid : $node->url;
db_insert('node_weight')
->fields(array(
'nid' => $node->nid,
'weight' => 0,
'url' => $node->url
))
->execute();
}
}
function node_weight_node_update($node) {
if (node_weight_in_allow_type($node)) {
$node->url = empty($node->url) ? 'node/'. $node->nid : $node->url;
db_update('node_weight')
->fields(
array('url' => $node->url)
)
->condition('nid', $node->nid)
->execute();
}
}
function node_weight_node_delete($node) {
if (node_weight_in_allow_type($node)) {
db_delete('node_weight')
->condition('nid', $node->nid)
->execute();
}
}
function node_weight_node_load($nodes, $types) {
foreach ($nodes as $node) {
$data = db_query("SELECT * FROM {node_weight} WHERE nid = :nid", array(':nid' => $node->nid));
foreach ($data as $record) {
$nodes[$node->nid]->url = $record->url;
$nodes[$node->nid]->weight = $record->weight;
}
}
}
/**
* Check node weight allowed type function.
*/
function node_weight_in_allow_type($node) {
$allow_type = variable_get('node_weight_allow_type', NULL);
if ($allow_type) {
foreach ($allow_type as $type => $value) {
if ($value) {
$allow_types[] = $type;
}
}
if (isset($allow_types) && in_array($node->type, $allow_types)) {
return true;
}
}
return false;
}
/**
* Implementation of hook_form_alter().
*/
function node_weight_form_alter(&$form, $form_state, $form_id) {
$allow_type = variable_get('node_weight_allow_type', NULL);
if ($allow_type) {
foreach ($allow_type as $type => $value) {
if ($value) {
$allow_form[] = $type.'_node_form';
}
}
if (isset($allow_form) && in_array($form_id, $allow_form)) {
$node = node_load(arg(1));
$form['url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => empty($node->url) ? '' : $node->url,
);
}
}
}
/**
* Implementation of hook_block_xx().
*/
function node_weight_block_info() {
if ($allow_type = variable_get('node_weight_allow_type', NULL)) {
foreach ($allow_type as $type => $value) {
if ($value) {
$blocks[$type]['info'] = t('Node Weight: !type', array('!type' => ucfirst($type)));
}
}
return $blocks;
}
}
function node_weight_block_configure($delta = '') {
// Load node_weight ordered table.
$form_state = array();
require_once('node_weight.admin.inc');
$form = node_weight_overview_form($form_state);
$types = array();
$display_list = array(0 => t('None'));
foreach (module_implements('weight_info') as $name) {
$function = $name .'_weight_info';
$return = $function();
if (isset($return) && is_array($return)) {
foreach ($return['form'] as $key => $_form) {
$return['form'][$key]['#default_value'] = variable_get('nw_'. $key .'_'. $delta, $return['form'][$key]['#default_value']);
}
$form['node_weight_'. $name .'_fs'] = $return['form'];
$form['node_weight_'. $name .'_fs']['#type'] = 'fieldset';
$form['node_weight_'. $name .'_fs']['#title'] = $return['name'] .' settings';
$form['node_weight_'. $name .'_fs']['#collapsible'] = TRUE;
$form['node_weight_'. $name .'_fs']['#collapsed'] = variable_get('nw_display_style_'. $delta, '') == $name ? FALSE : TRUE;
$display_list[$name] = $return['name'] .' - '. $return['description'];
$types[] = $name;
}
}
$form['display_style'] = array(
'#type' => 'select',
'#title' => t('Display style'),
'#default_value' => variable_get('nw_display_style_'. $delta, 0),
'#options' => $display_list,
'#weight' => -10,
);
$form['general_fs'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -9,
);
$form['general_fs']['number_of_item'] = array(
'#type' => 'textfield',
'#title' => t('Number of items'),
'#description' => t('Number of items to display.'),
'#default_value' => variable_get('nw_number_of_item_'. $delta, 5),
'#size' => 5,
);
/* Should create own preset instead.
if (module_exists('imagecache')) {
$presets = array('none' => t('Do not use imagecacahe'));
foreach (imagecache_presets() as $preset) {
$presets[$preset['presetname']] = $preset['presetname'];
}
$form['general_fs']['preset_image'] = array(
'#type' => 'select',
'#title' => t('Preset image'),
'#description' => t('Select imagecache preset for image.'),
'#default_value' => variable_get('nw_preset_image_'. $delta, 'none'),
'#options' => $presets,
);
}
*/
$form['general_fs']['display_image_as_link'] = array(
'#type' => 'checkbox',
'#title' => t('Display image as link'),
'#default_value' => variable_get('nw_display_image_as_link_'. $delta, TRUE),
);
$form['general_fs']['display_title'] = array(
'#type' => 'checkbox',
'#title' => t('Display title'),
'#description' => t('Display node title'),
'#default_value' => variable_get('nw_display_title_'. $delta, FALSE),
);
$form['general_fs']['display_body'] = array(
'#type' => 'checkbox',
'#title' => t('Display body'),
'#description' => t('Display node body'),
'#default_value' => variable_get('nw_display_body_'. $delta, FALSE),
);
$form['general_fs']['footer'] = array(
'#type' => 'textarea',
'#title' => t('Footer'),
'#description' => t('Display message after node body. Leave blank if do not want to display.'),
'#default_value' => variable_get('nw_footer_'. $delta, ''),
);
// Javascript
$nw_settings['node_weight'] = array(
'types' => $types,
);
drupal_add_js(drupal_get_path('module', 'node_weight') .'/node_weight.js');
drupal_add_js($nw_settings, 'setting');
return $form;
}
function node_weight_block_save($delta = '', $edit = array()) {
variable_set('nw_display_style_'. $delta, $edit['display_style']);
variable_set('nw_number_of_item_'. $delta, $edit['number_of_item']);
variable_set('nw_display_title_'. $delta, $edit['display_title']);
variable_set('nw_display_body_'. $delta, $edit['display_body']);
variable_set('nw_footer_'. $delta, $edit['footer']);
variable_set('nw_display_image_as_link_'. $delta, $edit['display_image_as_link']);
/*
if (module_exists('imagecache')) {
variable_set('nw_preset_image_'. $delta, $edit['preset_image']);
}
*/
// Save settings from implements module
foreach (module_implements('weight_info') as $name) {
$function = $name .'_weight_info';
$return = $function();
if (isset($return) && is_array($return)) {
if (!empty($return['form'])) {
foreach ($return['form'] as $key => $form) {
if (array_key_exists($key, $edit)) {
variable_set('nw_'. $key .'_'. $delta, $edit[$key]);
}
}
}
}
}
// Save node_weight arrange nodes.
$nodes = $edit['node_weight'];
foreach($nodes as $node) {
db_update("node_weight")
->fields(array(
'weight' => $node['weight']
))
->condition('nid',$node['nid'])
->execute();
}
}
function node_weight_block_view($delta = '') {
$settings = array(
'number_of_item' => variable_get('nw_number_of_item_'. $delta, 5),
'style' => variable_get('nw_display_style_'. $delta, 0),
'display_title' => variable_get('nw_display_title_'. $delta, FALSE),
'display_body' => variable_get('nw_display_body_'. $delta, FALSE),
'footer' => variable_get('nw_footer_'. $delta, ''),
'display_image_as_link' => variable_get('nw_display_image_as_link_'. $delta, TRUE),
);
/*
if (module_exists('imagecache')) {
$preset_image = variable_get('nw_preset_image_'. $delta, 'none');
if ($preset_image != 'none' && $preset_image != '') {
$settings['preset_image'] = $preset_image;
}
}
*/
$limit = $settings['number_of_item'] > 0 ? 'LIMIT 0, '. $settings['number_of_item'] : '';
// Multi-language support
if (module_exists('locale')) {
global $language;
$weight = db_query("SELECT n.nid, nw.url FROM {node} n LEFT JOIN {node_weight} nw ON n.nid = nw.nid WHERE n.type = ':delta' AND n.status = 1 AND (n.language = '' OR n.language = ':lang') ORDER BY nw.weight :limit",
array(':delta' => $delta, ':lang' => $language->language, ':limit' => $limit));
}
else {
$weight = db_query("SELECT n.nid, nw.url FROM {node} n LEFT JOIN {node_weight} nw ON n.nid = nw.nid WHERE n.type = '$delta' AND n.status = 1 ORDER BY nw.weight $limit ");
}
// List nodes
$nodes = array();
foreach ($weight as $_node) {
$style = variable_get('nw_display_style_'.$delta, FALSE);
$function = $style .'_weight_info';
if (function_exists($function)) {
$return = $function();
foreach ($return['form'] as $key => $form) {
$settings[$key] = variable_get('nw_'. $key .'_'. $delta, NULL);
}
}
$node = node_load($_node->nid);
$node->url = $_node->url;
// Multi-language URL
if (!empty($node->language) && $node->language != 'und') {
if (mb_substr($node->url, 0, 7) != 'http://') {
$node->url = $node->language .'/'. drupal_get_path_alias($node->url);
}
}
if (mb_substr($node->url, 0, 7) != 'http://') {
$node->link = l($node->title, $node->url);
}
else {
$node->link = l($node->title, $node->url, array('absolute' => TRUE, 'attributes' => array('target' => '_blank')));
}
$node->node_weight = array(
'image_path' => '',
'thumb_path' => '',
'swf_path' => '',
'embed' => ''
);
if (isset($node->node_weight_image[$node->language]))
$node->node_weight['image_path'] = $node->node_weight_image[$node->language][0]['uri'];
if (isset($node->node_weight_thumb[$node->language]))
$node->node_weight['thumb_path'] = $node->node_weight_thumb[$node->language][0]['uri'];
if (isset($node->node_weight_swf[$node->language]))
$node->node_weight['swf_path'] = $node->node_weight_swf[$node->language][0]['uri'];
if (isset($node->node_weight_embed[$node->language][0]['value']))
$node->node_weight['embed'] = $node->node_weight_embed[$node->language][0]['value'];
$nodes[] = $node;
}
if ($style = variable_get('nw_display_style_'.$delta, FALSE)) {
$function = $style .'_weight_view';
if (function_exists($function)) {
$block['content'] = $function($nodes, $delta, $settings);
}
}
if (empty($block['content'])) {
$block['content'] = 'default';
}
return $block;
}
/**
* Return settings
*/
function node_weight_get_settings($name, $type) {
return variable_get('nw_'. $name .'_'. $type, NULL);
}
/**
* Theme the node weight overview form.
*
* Arranges nodes in a table, and adds the css and js for draggable sorting.
*
* @ingroup themeable
* @ingroup forms
* @see node_weight_overview_form()
*/
function theme_node_weight_overview_form($variables) {
// Each node has a 'weight' that can be used to arrange it in relation to
// other nodes. Drupal's tabledrag.js library allows users to control these
// weights by dragging and dropping the nodes in a list -- we just need to
// add identifying CSS classes to key elements in the table.
$form = $variables['form'];
$rows = array();
//$rows[] = array('<pre>' . print_r($form,1) . '</pre>', '', '', 'test', '');
foreach (element_children($form) as $key) {
if (!is_numeric($key)) continue;
$row = array();
// Render the hidden 'node_weight id' field and the title of the node into the
// same column of the row.
$row[] = drupal_render($form[$key]['nid']) . drupal_render($form[$key]['title']);
// Add an identifying CSS class to our weight field, as it's the one
// the tabledrag.js will be controlling. This can be anything we want it to
// be, we'll just tell the tabledrag.js library what it should look for.
$form[$key]['weight']['#attributes']['class'] = array('node-weight-weight');
$row[] = drupal_render($form[$key]['weight']);
// Render the edit and delete links
$row[] = drupal_render($form[$key]['edit']);
$row[] = drupal_render($form[$key]['delete']);
// Add the new row to our collection of rows, and give it the 'draggable'
// class, indicating that it should be... well, draggable.
$rows[] = array(
'data' => $row,
'class' => array('draggable'),
);
}
// If there were no nodes found, note the fact so users don't get confused
// by a completely empty table.
if (count($rows) == 0) {
$rows[] = array(t('No nodes have been added.'), '<span class="node-weight-weight"></span>', '');
}
// Render a list of header titles, and our array of rows, into a table. Even
// we've already rendered all of our node, we always call drupal_render()
// on the form itself after we're done, so hidden security fields and other
// elements (like buttons) will appear properly at the bottom of the form.
$output = drupal_render($form['create_new']);
$header = array(t('Title'), t('Weight'), t('Edit'), t('Delete'));
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'node-weight-overview')
));
drupal_add_js(drupal_get_path('module', 'node_weight') .'/override_block.js',
array('weight' => 100)
);
// Now that we've built our output, tell Drupal to add the tabledrag.js library.
// We'll pass in the ID of the table, the behavior we want it to use, and the
// class that appears on each 'weight' form element it should be controlling.
drupal_add_tabledrag('node-weight-overview', 'order', 'self', 'node-weight-weight');
return $output;
}
function d($msg) {
drupal_set_message($msg);
}
function da($msg) {
drupal_set_message('<pre>'. print_r($msg, 1) .'</pre>');
}