| 1 |
<?php
|
| 2 |
// $Id: annotate_ed.module,v 1.2 2008/08/08 09:55:54 clemenstolboom Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Let users attach an editorial note to a node
|
| 6 |
*/
|
| 7 |
|
| 8 |
define( ANNOTATE_ED_EDIT, 'edit editorial note');
|
| 9 |
define( ANNOTATE_ED_LOCK, 'lock editorial note');
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu().
|
| 13 |
*/
|
| 14 |
function annotate_ed_menu() {
|
| 15 |
$items = array();
|
| 16 |
|
| 17 |
$items['admin/settings/annotate_ed'] = array(
|
| 18 |
'title' => 'Editorial annotation settings',
|
| 19 |
'description' => 'Change how editorial note behave.',
|
| 20 |
'page callback' => 'drupal_get_form',
|
| 21 |
'page arguments' => array('annotate_ed_admin_settings'),
|
| 22 |
'access arguments' => array('administer site configuration')
|
| 23 |
);
|
| 24 |
|
| 25 |
return $items;
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_perm()
|
| 30 |
*/
|
| 31 |
function annotate_ed_perm() {
|
| 32 |
return array( ANNOTATE_ED_EDIT, ANNOTATE_ED_LOCK);
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Define the settings form.
|
| 37 |
*/
|
| 38 |
function annotate_ed_admin_settings() {
|
| 39 |
$form['annotate_ed_show_expanded'] = array(
|
| 40 |
'#type' => 'radios',
|
| 41 |
'#title' => t('Show the annotation form expanded'),
|
| 42 |
'#options' => array( '1' => t('Yes'), '0' => t('No')),
|
| 43 |
'#default_value' => variable_get('annotate_ed_show_expanded', '0'),
|
| 44 |
'#description' => t('The annotation is not collapsed by default'),
|
| 45 |
);
|
| 46 |
|
| 47 |
$form['annotate_ed_nodetypes'] = array(
|
| 48 |
'#type' => 'checkboxes',
|
| 49 |
'#title' => t('Users may attach an editorial note to these node types'),
|
| 50 |
'#options' => node_get_types('names'),
|
| 51 |
'#default_value' => variable_get('annotate_ed_nodetypes', array('story')),
|
| 52 |
'#description' => t('A text field will be available on these node types to make editorial notes.')
|
| 53 |
);
|
| 54 |
|
| 55 |
$form['array_filter'] = array(
|
| 56 |
'#type' => 'hidden'
|
| 57 |
);
|
| 58 |
|
| 59 |
$formats = filter_formats();
|
| 60 |
$options = array();
|
| 61 |
foreach ($formats as $format) {
|
| 62 |
$options[$format->format]= $format->name;
|
| 63 |
}
|
| 64 |
|
| 65 |
$form['annotate_ed_format'] = array(
|
| 66 |
'#type' => 'radios',
|
| 67 |
'#title' => t('Users may use these formats for editorial notes'),
|
| 68 |
'#options' => $options,
|
| 69 |
'#default_value' => variable_get('annotate_ed_format', 1),
|
| 70 |
'#description' => t('These filters are allowed to make an editorial note.')
|
| 71 |
);
|
| 72 |
|
| 73 |
return system_settings_form($form);
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Implementation of hook_nodeapi().
|
| 78 |
*/
|
| 79 |
function annotate_ed_nodeapi(&$node, $op, $teaser, $page) {
|
| 80 |
switch ($op) {
|
| 81 |
case 'view':
|
| 82 |
global $user;
|
| 83 |
// If only the node summary is being displayed
|
| 84 |
if ($teaser) {
|
| 85 |
break;
|
| 86 |
}
|
| 87 |
|
| 88 |
// Check whether editorial notes are allowed for node
|
| 89 |
$types_to_ed_note = variable_get('annotate_ed_nodetypes', array('story'));
|
| 90 |
if (!in_array($node->type, $types_to_ed_note)) {
|
| 91 |
break;
|
| 92 |
}
|
| 93 |
|
| 94 |
// Get previously saved note, if any.
|
| 95 |
$result = db_query("SELECT note, locked FROM {annotate_ed} WHERE nid = %d", $node->nid);
|
| 96 |
$fields = db_fetch_array($result);
|
| 97 |
|
| 98 |
$node->annotate_ed_note = $fields['note'];
|
| 99 |
$node->annotate_ed_locked= isset( $fields['locked']) ? $fields['locked'] : 0;
|
| 100 |
|
| 101 |
if (( user_access( ANNOTATE_ED_EDIT, $user) && !$node->annotate_ed_locked)
|
| 102 |
|| user_access( ANNOTATE_ED_LOCK, $user)) {
|
| 103 |
// Add our form as a content item.
|
| 104 |
$node->content['annotate_ed_form'] = array(
|
| 105 |
'#value' => drupal_get_form('annotate_ed_entry_form', $node),
|
| 106 |
'#weight' => 10
|
| 107 |
);
|
| 108 |
}
|
| 109 |
else if (! empty( $node->annotate_ed_note) && $node->annotate_ed_locked) {
|
| 110 |
$node->content['annotate_ed'] = array(
|
| 111 |
'#value' => theme('annotate_ed_note', $node),
|
| 112 |
'#weight' => -10,
|
| 113 |
);
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
unset($node->annotate_ed_note);
|
| 117 |
unset($node->annotate_ed_locked);
|
| 118 |
}
|
| 119 |
break;
|
| 120 |
|
| 121 |
case 'delete':
|
| 122 |
_annotate_ed_node_delete($node);
|
| 123 |
break;
|
| 124 |
|
| 125 |
default:
|
| 126 |
break;
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
function annotate_ed_theme($existing, $type, $theme, $path) {
|
| 131 |
return array(
|
| 132 |
'annotate_ed_note' => array(
|
| 133 |
'node' => NULL,
|
| 134 |
),
|
| 135 |
);
|
| 136 |
}
|
| 137 |
|
| 138 |
function theme_annotate_ed_note( $node) {
|
| 139 |
drupal_add_css( drupal_get_path('module', 'annotate_ed') .'/css/annotate_ed.css');
|
| 140 |
return '<div class="annotate-editorial">'. check_markup($node->annotate_ed_note) .'</div>';
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Define the form for entering an editorial note.
|
| 145 |
*/
|
| 146 |
function annotate_ed_entry_form($form_state, $node) {
|
| 147 |
global $user;
|
| 148 |
|
| 149 |
$form['annotate_ed'] = array(
|
| 150 |
'#type' => 'fieldset',
|
| 151 |
'#title' => t('Editorial note'),
|
| 152 |
'#collapsible' => TRUE,
|
| 153 |
'#collapsed' => !$node->annotate_ed_note && !variable_get('annotate_ed_show_expanded', '0'),
|
| 154 |
);
|
| 155 |
|
| 156 |
$form['annotate_ed']['nid'] = array(
|
| 157 |
'#type' => 'value',
|
| 158 |
'#value' => $node->nid,
|
| 159 |
);
|
| 160 |
|
| 161 |
$locked= $node->annotate_ed_locked;
|
| 162 |
|
| 163 |
$form['annotate_ed']['note']['ed_note'] = array(
|
| 164 |
'#type' => 'textarea',
|
| 165 |
'#title' => t('Note'),
|
| 166 |
'#default_value' => $node->annotate_ed_note,
|
| 167 |
'#disabled' => $locked,
|
| 168 |
'#description' => t('Edit the editorial note about this content here.')
|
| 169 |
);
|
| 170 |
|
| 171 |
$form['annotate_ed']['note']['filter']= filter_form(variable_get('annotate_ed_format', 1));
|
| 172 |
|
| 173 |
if (user_access( ANNOTATE_ED_LOCK, $user)) {
|
| 174 |
$options = array(
|
| 175 |
0 => t('Unlocked'),
|
| 176 |
1 => t('Locked')
|
| 177 |
);
|
| 178 |
|
| 179 |
$form['annotate_ed']['locked'] = array(
|
| 180 |
'#title' => t('Lock note'),
|
| 181 |
'#type' => 'radios',
|
| 182 |
'#description' => t('Check this to lock the editorial note and show it to all users'),
|
| 183 |
'#options' => $options,
|
| 184 |
'#default_value' => $node->annotate_ed_locked
|
| 185 |
);
|
| 186 |
}
|
| 187 |
$form['annotate_ed']['submit'] = array(
|
| 188 |
'#type' => 'submit',
|
| 189 |
'#value' => t('Update')
|
| 190 |
);
|
| 191 |
return $form;
|
| 192 |
}
|
| 193 |
|
| 194 |
/*
|
| 195 |
* Save the editorial note to the database.
|
| 196 |
*/
|
| 197 |
function annotate_ed_entry_form_submit($form, &$form_state) {
|
| 198 |
global $user;
|
| 199 |
|
| 200 |
$nid = $form_state['values']['nid'];
|
| 201 |
$note = $form_state['values']['ed_note'];
|
| 202 |
$locked = isset($form_state['values']['locked']) ? $form_state['values']['locked'] : 0;
|
| 203 |
|
| 204 |
db_query("DELETE FROM {annotate_ed} WHERE nid = %d", $nid);
|
| 205 |
|
| 206 |
db_query("INSERT INTO {annotate_ed} (nid, note, locked, timestamp)
|
| 207 |
VALUES (%d, '%s', %d, %d)", $nid, $note, $locked, time());
|
| 208 |
drupal_set_message(t('Your editorial note was saved. locked='. $locked ));
|
| 209 |
}
|
| 210 |
|
| 211 |
function _annotate_ed_node_delete(&$node) {
|
| 212 |
if (_annotate_ed_exists_by_node( $nid)) {
|
| 213 |
db_query("DELETE FROM {annotate_ed} WHERE nid = %d", $node->nid);
|
| 214 |
drupal_set_message(t('Editorial annotation deleted for node %nid.', array( '%nid' => $node->nid)));
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
function _annotate_ed_exists_by_node( $nid) {
|
| 219 |
$result= db_query( ( "SELECT COUNT(*) AS num FROM {annotate_ed} WHERE nid=%d"), $nid);
|
| 220 |
$row = db_fetch_array( $result);
|
| 221 |
return $row['num'] > 0;
|
| 222 |
}
|