| 1 |
<?php
|
| 2 |
// $Id: taxonomy_multi_edit.module,v 1.15 2007/04/19 00:39:29 dman Exp $
|
| 3 |
/**
|
| 4 |
* @file Adds term tagging to the node admin screen
|
| 5 |
*
|
| 6 |
* Adds an action to the node operations drop-down found in admin/content/node
|
| 7 |
* This is suitable for adding one term to many nodes.
|
| 8 |
*
|
| 9 |
* Provides a new tab alongside admin/content/node where all terms are listed
|
| 10 |
* next to their nodes. This provides more finely-grained control, but is a bit
|
| 11 |
* clunkier.
|
| 12 |
*
|
| 13 |
* Note this code includes a hack in
|
| 14 |
* taxonomy_multi_edit_operations_categorize()
|
| 15 |
* that peeks at the raw form submission (non-FAPI) because the node admin form
|
| 16 |
* does not allow for extra information. Node operations are atomic and binary,
|
| 17 |
* but I needed to add a modifier (which terms to add) to that process.
|
| 18 |
*
|
| 19 |
*
|
| 20 |
* @author Dan Morrison (dman) http://coders.co.nz/
|
| 21 |
*/
|
| 22 |
|
| 23 |
function taxonomy_multi_edit_help($section = "admin/help#taxonomy_multi_edit") {
|
| 24 |
$output = "";
|
| 25 |
switch ($section) {
|
| 26 |
case 'admin/modules#description':
|
| 27 |
$output = t("Apply multiple category assignments at once.");
|
| 28 |
break;
|
| 29 |
case 'admin/content/node/taxonomy_multi_edit':
|
| 30 |
return t('Attach categories to your posts, and then click the <b>Submit</b> button.');
|
| 31 |
}
|
| 32 |
return $output;
|
| 33 |
}
|
| 34 |
|
| 35 |
function taxonomy_multi_edit_menu() {
|
| 36 |
$items['admin/content/node/taxonomy_multi_edit'] = array(
|
| 37 |
'title' => t('Assign categories'),
|
| 38 |
'page callback' => 'drupal_get_form',
|
| 39 |
'page arguments' => array('taxonomy_multi_edit_overview'),
|
| 40 |
'access arguments' => array('administer nodes'),
|
| 41 |
'weight' => 5,
|
| 42 |
'type' => MENU_LOCAL_TASK
|
| 43 |
);
|
| 44 |
return $items;
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Forms API form def
|
| 49 |
*/
|
| 50 |
function taxonomy_multi_edit_overview() {
|
| 51 |
$form = array('nodes' => array('#tree' => TRUE));
|
| 52 |
|
| 53 |
$sql = "SELECT n.nid, n.vid, title, type FROM {node} n ORDER BY changed DESC";
|
| 54 |
$result = pager_query(db_rewrite_sql($sql), variable_get('default_nodes_main', 10));
|
| 55 |
while ($node = db_fetch_object($result)) {
|
| 56 |
|
| 57 |
$node->taxonomy = taxonomy_node_get_terms($node);
|
| 58 |
// $form_taxonomy = taxonomy_node_form($node);
|
| 59 |
$fakeform = array(
|
| 60 |
'type' => array('#value' => $node->type),
|
| 61 |
'#node' => $node,
|
| 62 |
);
|
| 63 |
|
| 64 |
taxonomy_form_alter($fakeform, $form_state, $node->type. '_node_form');
|
| 65 |
|
| 66 |
unset($fakeform['type'], $fakeform['taxonomy']['#type']); // kill the fieldset and type field
|
| 67 |
// kill description fields
|
| 68 |
foreach ((array)$fakeform['taxonomy'] as $key => $arr) {
|
| 69 |
if (is_numeric($key)) {
|
| 70 |
unset($fakeform['taxonomy'][$key]['#description']);
|
| 71 |
}
|
| 72 |
}
|
| 73 |
$form['nodes'][$node->nid] = $fakeform;
|
| 74 |
unset($fakeform);
|
| 75 |
}
|
| 76 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
|
| 77 |
$form['pager'] = array('#value' => theme('pager', NULL, variable_get('default_nodes_main', 10), 0));
|
| 78 |
#dpm($form);
|
| 79 |
return $form;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Lay the form out into an admin table
|
| 84 |
*
|
| 85 |
* TODO make it line up properly for multiple columns. Currently it just
|
| 86 |
* cascades them in.
|
| 87 |
*/
|
| 88 |
function theme_taxonomy_multi_edit_overview(&$form) {
|
| 89 |
|
| 90 |
foreach (element_children($form['nodes']) as $nid) {
|
| 91 |
$node_form = $form['nodes'][$nid];
|
| 92 |
$row = array();
|
| 93 |
$row[] = l(truncate_utf8($form['nodes'][$nid]['#node']->title, 20), "node/$nid");
|
| 94 |
foreach (element_children($form['nodes'][$nid]['taxonomy']) as $vid) {
|
| 95 |
// disperse into rows. May give odd layout results with mixed node types
|
| 96 |
$row[] = array('data' => drupal_render($form['nodes'][$nid]['taxonomy'][$vid]));
|
| 97 |
}
|
| 98 |
dpm($node_form['#node']);
|
| 99 |
// Special addition for image nodes :-)
|
| 100 |
if ($node_form['#node']->type == 'image' && function_exists('image_display')) {
|
| 101 |
$node = node_load($node_form['#node']->nid);
|
| 102 |
$row[] = image_display($node, IMAGE_THUMBNAIL);
|
| 103 |
}
|
| 104 |
|
| 105 |
$rows[] = $row;
|
| 106 |
}
|
| 107 |
$header = array(
|
| 108 |
array("data" => t('title'), ),
|
| 109 |
array("data" => t('vocabularies'))
|
| 110 |
);
|
| 111 |
|
| 112 |
$output = theme('table', $header, $rows);
|
| 113 |
$output .= drupal_render($form);
|
| 114 |
return $output;
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Forms API form callback hook
|
| 119 |
*/
|
| 120 |
function taxonomy_multi_edit_overview_submit($form, &$form_state) {
|
| 121 |
foreach ($form_state['values']['nodes'] as $nid => $node_info) {
|
| 122 |
if (!$tax = $node_info['taxonomy']) {
|
| 123 |
$tax = $node_info['tags'];
|
| 124 |
}
|
| 125 |
$node = node_load($nid);
|
| 126 |
taxonomy_node_save($node, $tax);
|
| 127 |
}
|
| 128 |
drupal_set_message(t('Categories updated.'));
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* Below here is the normal Drupal content management page integration.
|
| 133 |
*/
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Hook Implimentation
|
| 137 |
*
|
| 138 |
* Change the content admin form
|
| 139 |
*/
|
| 140 |
function taxonomy_multi_edit_form_alter(&$form, $form_state, $form_id) {
|
| 141 |
// Enhance the node overview page
|
| 142 |
if ($form_id == 'node_admin_content') {
|
| 143 |
taxonomy_multi_edit_content_form_alter($form);
|
| 144 |
}
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Add a bulk classification function to the content admin
|
| 149 |
*
|
| 150 |
* Adds a javascript addition to the operations selector which displays a list
|
| 151 |
* of all terms
|
| 152 |
*/
|
| 153 |
function taxonomy_multi_edit_content_form_alter(&$form){
|
| 154 |
$form['admin']['options']['operation']['#attributes']['onchange'] = 'changedOperation(this)';
|
| 155 |
|
| 156 |
$form['admin']['options']['terms-wrapper'] = array(
|
| 157 |
'#type' => 'fieldset',
|
| 158 |
'#attributes' => array('id'=>'edit-terms-wrapper', 'style'=>'display:none;'),
|
| 159 |
'terms' => array(
|
| 160 |
'#type' => 'select',
|
| 161 |
'#title' => t("term"),
|
| 162 |
'#default_value' => array(),
|
| 163 |
'#options' => taxonomy_form_all(),
|
| 164 |
'#multiple' => TRUE,
|
| 165 |
'#size' => 10,
|
| 166 |
'#theme' => 'taxonomy_term_select',
|
| 167 |
'#description' => t("<p>Warning, using this form may override some of the normal vocabulary restrictions.</p>"),
|
| 168 |
),
|
| 169 |
);
|
| 170 |
|
| 171 |
# D6 does not allow us to add submit handlers, because it uses a button-specific submit handler in node_admin_nodes()
|
| 172 |
# It expects us to pipe our actions though 'operations' instead
|
| 173 |
|
| 174 |
drupal_add_js(taxonomy_multi_edit_content_form_javascript(),'inline');
|
| 175 |
}
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
/**
|
| 180 |
* Implementation of hook_node_operations().
|
| 181 |
*
|
| 182 |
* Declare our ability to tag nodes
|
| 183 |
*/
|
| 184 |
function taxonomy_multi_edit_node_operations() {
|
| 185 |
$operations = array(
|
| 186 |
'categorize' => array(
|
| 187 |
'label' => t('Assign to a taxonomy term'),
|
| 188 |
'callback' => 'taxonomy_multi_edit_operations_categorize',
|
| 189 |
),
|
| 190 |
'uncategorize' => array(
|
| 191 |
'label' => t('Remove a taxonomy term'),
|
| 192 |
'callback' => 'taxonomy_multi_edit_operations_categorize',
|
| 193 |
'callback arguments' => array(FALSE),
|
| 194 |
),
|
| 195 |
);
|
| 196 |
return $operations;
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Act on the node admin submission
|
| 201 |
*
|
| 202 |
* Tag all selected nids
|
| 203 |
*
|
| 204 |
* @param $add bool whether to add the term. Default is TRUE, but can be FALSE
|
| 205 |
* to remove a term.
|
| 206 |
*/
|
| 207 |
function taxonomy_multi_edit_operations_categorize($nids, $add = TRUE) {
|
| 208 |
|
| 209 |
dpm("Updating terms ".$add);
|
| 210 |
// hook_node_operations() gives us nothing but a list of nodes. I need to know the selected terms
|
| 211 |
// CHEAT by snooping on the form submission.
|
| 212 |
// node_admin_nodes_submit() really should tell me more about the context
|
| 213 |
$terms = $_REQUEST['terms'];
|
| 214 |
foreach ($nids as $nid) {
|
| 215 |
// I could have done this direct to database,
|
| 216 |
// but we'll do it safer via the published methods instead.
|
| 217 |
// Means we can safely merge instead of overwrite
|
| 218 |
if($node = node_load($nid)) {
|
| 219 |
$existing = taxonomy_node_get_terms($nid);
|
| 220 |
foreach($terms as $t){ // cannot array_merge as it messes the indexes
|
| 221 |
if ($add) {
|
| 222 |
$existing[$t] = $t;
|
| 223 |
}
|
| 224 |
else {
|
| 225 |
unset($existing[$t]);
|
| 226 |
}
|
| 227 |
}
|
| 228 |
taxonomy_node_save($node, $existing);
|
| 229 |
drupal_set_message(t('Updated terms on node '.l($node->title?$node->title:$nid, "node/$nid")));
|
| 230 |
}
|
| 231 |
}
|
| 232 |
cache_clear_all();
|
| 233 |
drupal_set_message(t('Terms have been updated for the selected nodes.'));
|
| 234 |
}
|
| 235 |
|
| 236 |
|
| 237 |
function taxonomy_multi_edit_content_form_javascript(){
|
| 238 |
// quoted javascript:
|
| 239 |
return <<<EOT
|
| 240 |
/**
|
| 241 |
* Responds to the 'operation' selectbox on content edit screen by
|
| 242 |
* displaying or hiding extra fields
|
| 243 |
*/
|
| 244 |
function changedOperation(elem){
|
| 245 |
var term_div = $('#edit-terms-wrapper');
|
| 246 |
if(!term_div) return;
|
| 247 |
if(elem.options[elem.selectedIndex].value == 'categorize' || elem.options[elem.selectedIndex].value == 'uncategorize' ){
|
| 248 |
term_div.show();
|
| 249 |
} else {
|
| 250 |
term_div.hide();
|
| 251 |
}
|
| 252 |
}
|
| 253 |
EOT;
|
| 254 |
}
|
| 255 |
|
| 256 |
|
| 257 |
function taxonomy_multi_edit_theme() {
|
| 258 |
return array(
|
| 259 |
'taxonomy_multi_edit_overview' => array(
|
| 260 |
'arguments' => array('form' => NULL),
|
| 261 |
),
|
| 262 |
);
|
| 263 |
}
|