-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_weight.admin.inc
164 lines (157 loc) · 4.88 KB
/
node_weight.admin.inc
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
<?php
/**
* Settings form
*/
function node_weight_admin_settings_form(){
$form['node_weight_allow_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Allow content type'),
'#options' => node_get_types('names'),
'#default_value' => variable_get('node_weight_allow_type', array()),
'#description' => t('Select content type to use node weight manager.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Settings form submit
*/
function node_weight_admin_settings_form_submit($form, $form_state){
variable_set('node_weight_allow_type', $form_state['values']['node_weight_allow_type']);
foreach ($form_state['values']['node_weight_allow_type'] as $type => $value) {
if ($value) {
db_query("INSERT INTO {node_weight} SELECT n.nid, 0, CONCAT('node/', n.nid) FROM {node} AS n WHERE n.type = '%s' ON DUPLICATE KEY UPDATE node_weight.nid = node_weight.nid", $type);
}
}
}
/**
* Build the Node weight select type overview form.
* Loads all node and builds an overview form with weight elements
*
* @ingroup forms
* @see theme_node_weight_type_form()
*/
function node_weight_type_form() {
$type = variable_get('node_weight_allow_type', NULL);
if ($type) {
foreach ($type as $key => $value) {
if ($value) {
$rows[] = array(array('data' => l($key, 'admin/content/node_weight/'.$key)));
}
}
}
if ($rows) {
$header = array(t('Content Type'));
return theme('table', $header, $rows, array('id' => 'node-weight-type'));
}
else {
return 'No content type to select, you can setting '. l('here', 'admin/settings/node_weight', array('query' => drupal_get_destination()));
}
}
/**
* Build the Node weight overview form.
* Loads all node and builds an overview form with weight elements
*
* @ingroup forms
* @see _node_weight_overview_field()
* @see node_weight_overview_form_submit()
* @see theme_node_weight_overview_form()
*/
function node_weight_overview_form(&$form_state, $type = NULL) {
drupal_set_title('Weight Manager for <em>'.$type.'</em>');
$allow_type = variable_get('node_weight_allow_type', NULL);
if ($allow_type) {
foreach ($allow_type as $allow => $value) {
if ($value) {
$allow_types[] = $allow;
}
}
if (isset($allow_types) && in_array($type, $allow_types)) {
$result = db_query("SELECT n.nid, n.title, n.type, nw.weight FROM {node} AS n LEFT JOIN {node_weight} AS nw ON n.nid = nw.nid WHERE n.type = '%s' ORDER BY nw.weight", $type);
while ($node = db_fetch_array($result)) {
$nodes[$node['nid']] = $node;
}
$form['node_weight']['#tree'] = TRUE;
$form['create_new'] = array(
'#type' => 'markup',
'#value' => l('Create new '.$type, 'node/add/'.str_replace('_', '-', $type), array('query' => drupal_get_destination()))
);
if ($nodes) {
foreach ($nodes as $nid => $node) {
$form['node_weight'][$nid] = _node_weight_overview_field($node);
}
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
'#disabled' => empty($nodes),
);
}
if (!isset($form)) {
drupal_goto('node_weight');
}
return $form;
}
else {
drupal_goto('node_weight');
}
}
else {
$form['error'] = array(
'#type' => 'markup',
'#value' => 'No content type to select, you can setting '. l('here', 'admin/settings/node_weight', array('query' => drupal_get_destination())),
);
return $form;
}
}
/**
* Build the overview form fields.
*
* This internal function should not be called outside the node_weight module,
*
* @ingroup forms
* @see node_weight_overview_form()
*/
function _node_weight_overview_field($node) {
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node['nid'],
);
$form['title'] = array(
'#type' => 'markup',
'#value' => l($node['title'], 'node/'.$node['nid']),
);
$form['taxonomy'] = array(
'#type' => 'markup',
'#value' => implode(', ', array_keys((array)taxonomy_node_get_terms(node_load($node['nid']), 'name'))),
);
$form['weight'] = array(
'#type' => 'weight',
'#default_value' => $node['weight'],
);
$form['edit'] = array(
'#type' => 'markup',
'#value' => l('edit', 'node/'.$node['nid'].'/edit', array('query' => drupal_get_destination())),
);
$form['delete'] = array(
'#type' => 'markup',
'#value' => l('delete', 'node/'.$node['nid'].'/delete', array('query' => drupal_get_destination())),
);
return $form;
}
/**
* General submit handler for node_weight's overview form.
*
* Updates the weights of all nodes on the form.
*
* @ingroup formapi
* @see node_weight_overview_form()
*/
function node_weight_overview_form_submit($form, &$form_state) {
$nodes = $form_state['values']['node_weight'];
foreach($nodes as $node) {
db_query("UPDATE {node_weight} SET weight = %d WHERE nid = %d", $node['weight'], $node['nid']);
}
}