| 1 |
<?php |
<?php |
| 2 |
// $Id: weight.module,v 1.23.2.7 2009/04/25 15:03:23 nancyw Exp $ |
// $Id: weight.module,v 1.23.2.8 2009/04/25 15:11:36 nancyw Exp $ |
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* This module uses the sticky column of the node table |
* This module uses the sticky column of the node table |
| 60 |
return $items; |
return $items; |
| 61 |
} |
} |
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* Implementation of hook_theme_registry_alter(). |
| 65 |
|
* |
| 66 |
|
* Swap in our own replacement for theme_node_admin_nodes(), allowing us to |
| 67 |
|
* access the node list early on. |
| 68 |
|
*/ |
| 69 |
|
function weight_theme_registry_alter(&$theme_registry) { |
| 70 |
|
$path = drupal_get_path('module', 'weight'); |
| 71 |
|
$theme_registry['node_admin_nodes']['function'] = 'theme_weight_node_admin_nodes'; |
| 72 |
|
$theme_registry['node_admin_nodes']['file'] = $path .'/weight.module'; |
| 73 |
|
$theme_registry['node_admin_nodes']['theme path'] = $path; |
| 74 |
|
$theme_registry['node_admin_nodes']['theme paths'][0] = $path; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
function weight_nodeapi(&$node, $op) { |
function weight_nodeapi(&$node, $op) { |
| 78 |
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names'))); |
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names'))); |
| 79 |
if (in_array($node->type, $weight_node_types)) { |
if (in_array($node->type, $weight_node_types)) { |
| 80 |
switch ($op) { |
switch ($op) { |
| 81 |
case 'presave': |
case 'presave': |
| 82 |
// Non-weighted nodes have a weight of zero. |
// Non-weighted nodes have a weight of zero. |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
/** |
/** |
| 104 |
* Implementation of hook_form_alter(). |
* Theme node administration overview. |
| 105 |
|
* Mostly copied from node.admin.inc. |
| 106 |
* |
* |
| 107 |
* This is where we tweak the admin/content/node to include our weight |
* @ingroup themeable |
|
* selector; hide the 'sticky' filter (it won't work when using weight module), |
|
|
* and add some help text to the form. |
|
| 108 |
*/ |
*/ |
| 109 |
function weight_form_alter(&$form, $form_state, $form_id) { |
function theme_weight_node_admin_nodes($form) { |
| 110 |
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names'))); |
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names'))); |
| 111 |
|
|
| 112 |
$weight_node_type_names = array(); |
$weight_node_type_names = array(); |
| 114 |
$weight_node_type_names[] = node_get_types('name', $type); |
$weight_node_type_names[] = node_get_types('name', $type); |
| 115 |
} |
} |
| 116 |
|
|
| 117 |
if ($form_id == 'node_admin_content') { |
// If there are rows in this form, then $form['title'] contains a list of |
| 118 |
// The admin node page does not use the nodeapi for getting lists of nodes, so |
// the title form elements. |
| 119 |
// I never have a chance to convert the sticky flag to 0/1. and trying to |
$has_posts = isset($form['title']) && is_array($form['title']); |
| 120 |
// filter on that field will not work. Therefore, I am going to hide the |
$select_header = $has_posts ? theme('table_select_header_cell') : ''; |
| 121 |
// 'filter on sticky status' altogether when weight_module is enabled |
$header = array($select_header, t('Title'), t('Type'), t('Author'), t('Status')); |
| 122 |
unset($form['filters']['status']['status']['#options']['sticky-1']); |
if (isset($form['language'])) { |
| 123 |
unset($form['filters']['status']['status']['#options']['sticky-0']); |
$header[] = t('Language'); |
| 124 |
|
} |
| 125 |
// I can't add a table header for weight, so instead I'm going to explain |
$header[] = t('Operations'); |
| 126 |
// the weight dropdown to the user. Also, to position my help text |
$header[] = t('Weight'); |
| 127 |
// appropriately, I'm using this '#suffix' hack rather than adding |
$output = ''; |
| 128 |
// a form property as i'd like to do. |
|
| 129 |
$form['admin']['options']['#suffix'] .= t('<strong>Weight:</strong> To change the weight of a node, select a value from the corresponding dropdown box under <i>Operations</i>. Node weights are submitted immediately. Selectors are only available for node types configured on the <a href="@weight_admin">weight admin page</a>.', |
$output .= drupal_render($form['options']); |
| 130 |
array('@weight_admin' => url('admin/settings/weight')) |
if ($has_posts) { |
| 131 |
); |
foreach (element_children($form['title']) as $key) { |
| 132 |
|
$one_of_ours = in_array($form['name'][$key]['#value'], $weight_node_type_names); |
| 133 |
// Add weight selector under the operations section of the admin node |
|
| 134 |
// overview page (admin/content/node). |
// Add a weight element by copying and modifying the title element. |
| 135 |
if (!empty($form['admin']['operations'])) { |
$s = weight_node_selector($key); |
| 136 |
foreach ($form['admin']['operations'] as $nid => $title) { |
$form['weight'][$key] = $form['title'][$key]; |
| 137 |
// only add weight selector if weight is enabled for this node type |
$form['weight'][$key]['#value'] = $one_of_ours ? $s['selector'] : ''; |
| 138 |
if (in_array($form['admin']['name'][$nid]['#value'], $weight_node_type_names) ) { |
$form['status'][$key]['#value'] = $s['status']; |
| 139 |
$selector = weight_node_selector($nid); |
|
| 140 |
$form['admin']['operations'][$nid]['#value'] .= ' '. $selector['selector']; |
$row = array(); |
| 141 |
$form['admin']['status'][$nid]['#value'] .= $selector['status']; |
$row[] = drupal_render($form['nodes'][$key]); |
| 142 |
} |
$row[] = drupal_render($form['title'][$key]); |
| 143 |
|
$row[] = drupal_render($form['name'][$key]); |
| 144 |
|
$row[] = drupal_render($form['username'][$key]); |
| 145 |
|
$row[] = drupal_render($form['status'][$key]); |
| 146 |
|
if (isset($form['language'])) { |
| 147 |
|
$row[] = drupal_render($form['language'][$key]); |
| 148 |
} |
} |
| 149 |
|
$row[] = drupal_render($form['operations'][$key]); |
| 150 |
|
$row[] = drupal_render($form['weight'][$key]); |
| 151 |
|
$rows[] = $row; |
| 152 |
} |
} |
| 153 |
} |
} |
| 154 |
|
else { |
| 155 |
|
$rows[] = array(array('data' => t('No posts available.'), 'colspan' => '6')); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
// Node edit page weight selector. |
$output .= theme('table', $header, $rows); |
| 159 |
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) { |
if ($form['pager']['#value']) { |
| 160 |
if (user_access('assign node weight') || user_access('administer nodes')) { |
$output .= drupal_render($form['pager']); |
|
$node = $form['#node']; |
|
|
if (in_array($node->type, $weight_node_types)) { |
|
|
$range = variable_get('weight_range', 20); |
|
|
$position = variable_get('weight_position', 0); |
|
|
$where = 'weight_form'; |
|
|
|
|
|
if ($position == 10 && user_access('administer nodes')) { |
|
|
// We will add it to the Workflow fieldset. |
|
|
$where = 'options'; |
|
|
$form['options']['#collapsed'] = ($node->node_weight == 0); |
|
|
} |
|
|
else { |
|
|
// Add the node weight selector fieldset. |
|
|
$form['weight_form'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Node Weight'), |
|
|
'#collapsible' => TRUE, |
|
|
'#collapsed' => ($node->node_weight == 0), |
|
|
'#weight' => $position, |
|
|
); |
|
|
} |
|
|
|
|
|
$form[$where]['node_weight'] = array( |
|
|
'#type' => 'weight', |
|
|
'#title' => t('Weight'), |
|
|
'#default_value' => (int)$node->node_weight, |
|
|
'#delta' => $range, |
|
|
'#description' => t('In a node list context (such as the front page or term pages), list items (e.g. "teasers") will be ordered by "stickiness" then by "node weight" then by "authored on" datestamp. Items with a lower (lighter) node weight value will appear above those with a higher (heavier) value.'), |
|
|
); |
|
|
|
|
|
if (variable_get('weight_use_menu', FALSE)) { |
|
|
$form['weight_form']['node_weight']['#description'] .= '<br /> '. t('<strong>Note</strong>: If this node is used in a menu, then this weight will be ignored.'); |
|
|
} |
|
|
} |
|
|
} |
|
| 161 |
} |
} |
| 162 |
|
|
| 163 |
|
$output .= drupal_render($form); |
| 164 |
|
return $output; |
| 165 |
} |
} |
| 166 |
|
|
| 167 |
/** |
/** |
| 177 |
drupal_add_css($path .'/weight.css'); |
drupal_add_css($path .'/weight.css'); |
| 178 |
} |
} |
| 179 |
|
|
| 180 |
$selector_template = "\n"."<select style=\"margin: 0;\" |
$selector_template = "<select style=\"margin: 0;\" |
| 181 |
onchange='httpRequest(\"GET\", \"" . base_path() . |
onchange='httpRequest(\"GET\", \"" . base_path() . |
| 182 |
"?q=admin/node/weight/_weight_change/\" + [NID] + \"/\" + |
"?q=admin/node/weight/_weight_change/\" + [NID] + \"/\" + |
| 183 |
this.options[this.selectedIndex].value,true)' >"; |
this.options[this.selectedIndex].value,true)' >"; |
| 184 |
|
|
| 185 |
$node = node_load($nid); |
// Get more stuff about the node. |
| 186 |
|
$node = db_fetch_object(db_query("SELECT nid, `status`, sticky, promote, translate, moderate FROM {node} WHERE nid = %d", $nid)); |
| 187 |
|
|
| 188 |
// Convert to our weight range. |
// Convert to our weight range. |
| 189 |
|
_weight_decode($node); |
| 190 |
$weight = $node->node_weight; |
$weight = $node->node_weight; |
| 191 |
|
|
| 192 |
// ugly bit of javascript we use for each dropdown to submit weight changes |
// Ugly bit of javascript we use for each dropdown to submit weight changes |
| 193 |
// in the background. Relies on even uglier httpRequest.js file that comes |
// in the background. Relies on even uglier httpRequest.js file that comes |
| 194 |
// with this module. Ironically, Ajax makes me feel dirty. |
// with this module. Ironically, Ajax makes me feel dirty. |
| 195 |
|
|
| 196 |
$weight_range = variable_get('weight_range', 20); |
$weight_range = variable_get('weight_range', 20); |
| 197 |
for ($i = 0 - $weight_range; $i <= $weight_range; ++$i) { |
for ($i = - $weight_range; $i <= $weight_range; ++$i) { |
| 198 |
$selector_template .= "<option value='$i'>$i</option>"; |
$selector_template .= "<option value='$i'". ($i == $node->node_weight ? " selected='selected'" : '') .">$i</option>"; |
| 199 |
} |
} |
| 200 |
$selector_template .= '</select>'; |
$selector_template .= '</select>'; |
| 201 |
|
$weight_selector = str_replace('[NID]', $nid, $selector_template); |
| 202 |
|
|
| 203 |
$weight_selector = preg_replace("/(value='$weight')/", "$1 selected='selected'", $selector_template); |
$status = $node->status ? t('published') : NULL; |
|
$weight_selector = preg_replace("/\[NID\]/", $nid, $weight_selector); |
|
|
|
|
|
$status = NULL; |
|
| 204 |
$status .= $node->sticky ? '<br />'. t('sticky') : NULL; |
$status .= $node->sticky ? '<br />'. t('sticky') : NULL; |
| 205 |
$status .= $node->promote ? '<br />'. t('promoted') : NULL; |
$status .= $node->promote ? '<br />'. t('promoted') : NULL; |
| 206 |
$status .= $node->translate ? '<br />'. t('translate') : NULL; |
$status .= $node->translate ? '<br />'. t('translate') : NULL; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
/** |
/** |
| 216 |
|
* Implementation of hook_form_alter(). |
| 217 |
|
* |
| 218 |
|
* This is where we tweak the admin/content/node to include our weight |
| 219 |
|
* selector; hide the 'sticky' filter (it won't work when using weight module), |
| 220 |
|
* and add some help text to the form. |
| 221 |
|
*/ |
| 222 |
|
function weight_form_alter(&$form, $form_state, $form_id) { |
| 223 |
|
$weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names'))); |
| 224 |
|
|
| 225 |
|
// Node edit page weight selector. |
| 226 |
|
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) { |
| 227 |
|
$node = $form['#node']; |
| 228 |
|
if (user_access('assign node weight') || user_access('administer nodes')) { |
| 229 |
|
if (in_array($node->type, $weight_node_types)) { |
| 230 |
|
$range = variable_get('weight_range', 20); |
| 231 |
|
$position = variable_get('weight_position', 0); |
| 232 |
|
$where = 'weight_form'; |
| 233 |
|
|
| 234 |
|
if ($position == 10 && user_access('administer nodes')) { |
| 235 |
|
// We will add it to the Workflow fieldset. |
| 236 |
|
$where = 'options'; |
| 237 |
|
$form['options']['#collapsed'] = ($node->node_weight == 0); |
| 238 |
|
} |
| 239 |
|
else { |
| 240 |
|
// Add the node weight selector fieldset. |
| 241 |
|
$form['weight_form'] = array( |
| 242 |
|
'#type' => 'fieldset', |
| 243 |
|
'#title' => variable_get('weight_label', t('Node Weight')), |
| 244 |
|
'#collapsible' => TRUE, |
| 245 |
|
'#collapsed' => ($node->node_weight == 0), |
| 246 |
|
'#weight' => $position, |
| 247 |
|
); |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
$form[$where]['node_weight'] = array( |
| 251 |
|
'#type' => 'weight', |
| 252 |
|
'#title' => t('Weight'), |
| 253 |
|
'#default_value' => (int)$node->node_weight, |
| 254 |
|
'#delta' => $range, |
| 255 |
|
'#description' => t('In a node list context (such as the front page or term pages), list items (e.g. "teasers") will be ordered by "stickiness" then by "node weight" then by "authored on" datestamp. Items with a lower (lighter) node weight value will appear above those with a higher (heavier) value.'), |
| 256 |
|
); |
| 257 |
|
|
| 258 |
|
if (variable_get('weight_use_menu', FALSE)) { |
| 259 |
|
$form['weight_form']['node_weight']['#description'] .= '<br /> '. t('<strong>Note</strong>: If this node is used in a menu, then this weight will be ignored.'); |
| 260 |
|
} |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
else { |
| 264 |
|
$form['node_weight'] = array( |
| 265 |
|
'#type' => 'value', |
| 266 |
|
'#value' => (int)$node->node_weight, |
| 267 |
|
); |
| 268 |
|
} |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
/** |
| 273 |
* Ajax callback for weight manager page. |
* Ajax callback for weight manager page. |
| 274 |
*/ |
*/ |
| 275 |
function _weight_change($nid, $weight) { |
function _weight_change($nid, $weight) { |
| 287 |
* unweighted sticky nodes will have a value of 100. |
* unweighted sticky nodes will have a value of 100. |
| 288 |
*/ |
*/ |
| 289 |
function _weight_encode(&$node) { |
function _weight_encode(&$node) { |
| 290 |
if ($node->sticky) { |
if ($node->sticky == 1) { |
| 291 |
$node->sticky = 100 - $node->node_weight; |
$node->sticky = 100 - $node->node_weight; |
| 292 |
} |
} |
| 293 |
// Unweighted non-sticky nodes will have a value of -100. |
// Unweighted non-sticky nodes will have a value of -100. |
| 294 |
else { |
else { |
| 295 |
$node->sticky = -($node->node_weight + 100); |
if ($node->sticky == 0) { |
| 296 |
|
$node->sticky = -($node->node_weight + 100); |
| 297 |
|
} |
| 298 |
} |
} |
| 299 |
} |
} |
| 300 |
|
|
| 325 |
} |
} |
| 326 |
|
|
| 327 |
/** |
/** |
| 328 |
* Implementation of hook_theme() |
* Implementation of hook_theme(). |
| 329 |
*/ |
*/ |
| 330 |
function weight_theme() { |
function weight_theme() { |
| 331 |
return array( |
return array( |
| 333 |
'arguments' => array('form' => NULL), |
'arguments' => array('form' => NULL), |
| 334 |
'template' => 'weight-view-weight-form', |
'template' => 'weight-view-weight-form', |
| 335 |
), |
), |
| 336 |
|
'weight_node_admin_nodes' => array( |
| 337 |
|
'arguments' => array('form' => NULL), |
| 338 |
|
), |
| 339 |
); |
); |
| 340 |
} |
} |
| 341 |
|
|
| 350 |
foreach ($vars['form']['rows'] as $count => $item) { |
foreach ($vars['form']['rows'] as $count => $item) { |
| 351 |
if (is_numeric($count)) { |
if (is_numeric($count)) { |
| 352 |
foreach ($item as $field => $value) { |
foreach ($item as $field => $value) { |
| 353 |
if (substr($field, 0, 1) != '#') { |
if (drupal_substr($field, 0, 1) != '#') { |
| 354 |
if (substr($field, 0, 6) == 'weight') { |
if (drupal_substr($field, 0, 6) == 'weight') { |
| 355 |
$value['#attributes']['class'] = 'weight_dragger'; |
$value['#attributes']['class'] = 'weight_dragger'; |
| 356 |
} |
} |
| 357 |
$vars['rows'][$count][$field] = drupal_render($value); |
$vars['rows'][$count][$field] = drupal_render($value); |
| 361 |
} |
} |
| 362 |
unset($vars['form']['rows']); |
unset($vars['form']['rows']); |
| 363 |
$vars['submit'] = drupal_render($vars['form']); |
$vars['submit'] = drupal_render($vars['form']); |
| 364 |
} |
} |