| 1 |
<?php
|
| 2 |
// $Id: maxlength.module,v 1.14 2008/09/27 19:23:10 mariuss Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Module to enable a max length countdown on node body and title.
|
| 6 |
*/
|
| 7 |
|
| 8 |
require_once 'maxlength.inc';
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implemenation of hook_perm().
|
| 12 |
*/
|
| 13 |
function maxlength_perm() {
|
| 14 |
return array(ADMINISTER_MAXLENGTH);
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implemenation of hook_help().
|
| 19 |
*/
|
| 20 |
function maxlength_help($section) {
|
| 21 |
switch ($section) {
|
| 22 |
case 'admin/help#maxlength':
|
| 23 |
case 'admin/modules#description':
|
| 24 |
return t('Sets a maximum length for body fields and shows a counter that is updated as you type.');
|
| 25 |
break;
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implemenation of hook_menu().
|
| 31 |
*/
|
| 32 |
function maxlength_menu() {
|
| 33 |
$items['admin/settings/maxlength'] = array(
|
| 34 |
'title' => t('Maxlength'),
|
| 35 |
'description' => t('Set maximum length for body fields.'),
|
| 36 |
'page callback' => 'drupal_get_form',
|
| 37 |
'page arguments' => array('_maxlength_admin_settings'),
|
| 38 |
'access arguments' => array(ADMINISTER_MAXLENGTH),
|
| 39 |
'type' => MENU_NORMAL_ITEM,
|
| 40 |
);
|
| 41 |
|
| 42 |
return $items;
|
| 43 |
}
|
| 44 |
|
| 45 |
function _maxlength_admin_settings() {
|
| 46 |
$form = array();
|
| 47 |
|
| 48 |
foreach (node_get_types() as $type => $name) {
|
| 49 |
$code = MAXLENGTH_NODE_TYPE . $type;
|
| 50 |
|
| 51 |
$type_info = node_get_types('type', $type);
|
| 52 |
|
| 53 |
$form['nodetypes'][$type] = array(
|
| 54 |
'#type' => 'fieldset',
|
| 55 |
'#title' => $name->name,
|
| 56 |
'#collapsible' => TRUE,
|
| 57 |
'#collapsed' => strlen(variable_get($code .'_t', '') . variable_get($code .'_b', '')) == 0,
|
| 58 |
);
|
| 59 |
|
| 60 |
$form['nodetypes'][$type][$code .'_t'] = array(
|
| 61 |
'#type' => 'textfield',
|
| 62 |
'#title' => t('!label Maxlength', array('!label' => $type_info->title_label)),
|
| 63 |
'#field_suffix' => t('characters'),
|
| 64 |
'#return_value' => 1,
|
| 65 |
'#default_value' => variable_get($code .'_t', ''),
|
| 66 |
'#description' => t('Maximum number of characters allowed for the title field of this content type. Leave blank for an unlimited size.'),
|
| 67 |
);
|
| 68 |
|
| 69 |
$form['nodetypes'][$type][$code .'_b'] = array(
|
| 70 |
'#type' => 'textfield',
|
| 71 |
'#title' => t('!label Maxlength', array('!label' => $type_info->body_label)),
|
| 72 |
'#field_suffix' => t('characters'),
|
| 73 |
'#return_value' => 1,
|
| 74 |
'#default_value' => variable_get($code .'_b', ''),
|
| 75 |
'#description' => t('Maximum number of characters allowed for the body field of this content type. Leave blank for an unlimited size.'),
|
| 76 |
);
|
| 77 |
}
|
| 78 |
|
| 79 |
return system_settings_form($form);
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Implemenation of hook_elements().
|
| 84 |
*/
|
| 85 |
function maxlength_elements() {
|
| 86 |
$type = array();
|
| 87 |
|
| 88 |
if (_maxlength_limit_body()) {
|
| 89 |
$type['textarea'] = array(
|
| 90 |
'#theme' => 'maxlength_textarea',
|
| 91 |
);
|
| 92 |
}
|
| 93 |
|
| 94 |
return $type;
|
| 95 |
}
|
| 96 |
|
| 97 |
function _maxlength_limit_body() {
|
| 98 |
if (arg(0) == 'node') {
|
| 99 |
if (arg(1) == 'add') {
|
| 100 |
$type = str_replace('-', '_', arg(2));
|
| 101 |
|
| 102 |
return strlen(variable_get(MAXLENGTH_NODE_TYPE . $type .'_b', '')) > 0;
|
| 103 |
}
|
| 104 |
|
| 105 |
if (arg(2) == 'edit') {
|
| 106 |
$nid = intval(arg(1));
|
| 107 |
|
| 108 |
return strlen(variable_get(MAXLENGTH_NODE_TYPE . _maxlength_node_type_from_id($nid) .'_b', '')) > 0;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
return FALSE;
|
| 113 |
}
|
| 114 |
/**
|
| 115 |
* Implementation of hook_theme().
|
| 116 |
*/
|
| 117 |
function maxlength_theme(){
|
| 118 |
return array(
|
| 119 |
'maxlength_textarea' => array(
|
| 120 |
'arguments' => array(
|
| 121 |
'element' => array()
|
| 122 |
),
|
| 123 |
),
|
| 124 |
);
|
| 125 |
}
|
| 126 |
function theme_maxlength_textarea($element) {
|
| 127 |
$prefix = '';
|
| 128 |
|
| 129 |
if ($element['#name'] == 'body') {
|
| 130 |
$path = drupal_get_path('module', 'maxlength');
|
| 131 |
drupal_add_js($path .'/maxlength.js');
|
| 132 |
|
| 133 |
if (arg(1) == 'add') {
|
| 134 |
$type = str_replace('-', '_', arg(2));
|
| 135 |
}
|
| 136 |
else {
|
| 137 |
$type = _maxlength_node_type_from_id(intval(arg(1)));
|
| 138 |
}
|
| 139 |
|
| 140 |
$limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $type .'_b', ''));
|
| 141 |
|
| 142 |
$remaining = $limit - drupal_strlen($element['#value']);
|
| 143 |
|
| 144 |
if ($remaining < 0) {
|
| 145 |
drupal_set_message(
|
| 146 |
t('%body_field_label truncated to %limit characters!',
|
| 147 |
array(
|
| 148 |
'%body_field_label' => $element['#title'],
|
| 149 |
'%limit' => $limit)
|
| 150 |
),
|
| 151 |
'error'
|
| 152 |
);
|
| 153 |
|
| 154 |
$element['#value'] = drupal_substr($element['#value'], 0, $limit);
|
| 155 |
$remaining = 0;
|
| 156 |
}
|
| 157 |
|
| 158 |
$element['#attributes']['onchange'] = 'maxlength_limit(this, '. $limit .');';
|
| 159 |
$element['#attributes']['onkeyup'] = 'maxlength_limit(this, '. $limit .');';
|
| 160 |
|
| 161 |
$prefix = t('<div id="maxlength-counter">Content limited to !limit characters, remaining: <strong id="maxlength-counter-remaining">!remaining</strong></div>', array('!limit' => $limit, '!remaining' => $remaining));
|
| 162 |
}
|
| 163 |
|
| 164 |
return theme_textarea($element) . $prefix;
|
| 165 |
}
|
| 166 |
|
| 167 |
function _maxlength_node_type_from_id($nid) {
|
| 168 |
$node = node_load($nid);
|
| 169 |
|
| 170 |
return $node->type;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Implemenation of hook_form_alter().
|
| 175 |
*/
|
| 176 |
function maxlength_form_alter(&$form, $form_state, $form_id) {
|
| 177 |
if (isset($form['type'])) {
|
| 178 |
$type = $form['type']['#value'];
|
| 179 |
if ( $type .'_node_form' == $form_id) {
|
| 180 |
if (strlen(variable_get(MAXLENGTH_NODE_TYPE . $type .'_t', '')) > 0) {
|
| 181 |
$limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $type .'_t', ''));
|
| 182 |
$form['title']['#maxlength'] = $limit;
|
| 183 |
|
| 184 |
if (drupal_strlen($form['title']['#default_value']) > $limit) {
|
| 185 |
$form['title']['#default_value'] = drupal_substr($form['title']['#default_value'], 0, $limit);
|
| 186 |
drupal_set_message(
|
| 187 |
t('%title_field_label truncated to %limit characters!',
|
| 188 |
array(
|
| 189 |
'%title_field_label' => $form['title']['#title'],
|
| 190 |
'%limit' => $limit)
|
| 191 |
),
|
| 192 |
'error'
|
| 193 |
);
|
| 194 |
}
|
| 195 |
}
|
| 196 |
}
|
| 197 |
}
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Implemenation of hook_nodeapi().
|
| 202 |
*/
|
| 203 |
function maxlength_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 204 |
$code = MAXLENGTH_NODE_TYPE . $node->type;
|
| 205 |
|
| 206 |
if (strlen(variable_get($code .'_b', '')) > 0) {
|
| 207 |
switch ($op) {
|
| 208 |
case 'validate':
|
| 209 |
$form = $a3;
|
| 210 |
|
| 211 |
$limit = intval(variable_get($code .'_b', ''));
|
| 212 |
|
| 213 |
if (drupal_strlen($node->body) > $limit) {
|
| 214 |
form_set_error('body', t('The maximum number of characters has been exceeded!'));
|
| 215 |
}
|
| 216 |
|
| 217 |
break;
|
| 218 |
}
|
| 219 |
}
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* Implemenation of hook_node_type().
|
| 224 |
*/
|
| 225 |
function maxlength_node_type($op, $info) {
|
| 226 |
switch ($op) {
|
| 227 |
case 'delete':
|
| 228 |
$code = MAXLENGTH_NODE_TYPE . $info->type;
|
| 229 |
|
| 230 |
variable_del($code .'_t');
|
| 231 |
variable_del($code .'_b');
|
| 232 |
|
| 233 |
break;
|
| 234 |
|
| 235 |
case 'update':
|
| 236 |
if (!empty($info->old_type) && $info->old_type != $info->type) {
|
| 237 |
$code_old = MAXLENGTH_NODE_TYPE . $info->old_type;
|
| 238 |
$code_new = MAXLENGTH_NODE_TYPE . $info->type;
|
| 239 |
|
| 240 |
$max_title = variable_get($code_old .'_t', '');
|
| 241 |
$max_body = variable_get($code_old .'_b', '');
|
| 242 |
|
| 243 |
variable_set($code_new .'_t', $max_title);
|
| 244 |
variable_set($code_new .'_b', $max_body);
|
| 245 |
|
| 246 |
variable_del($code_old .'_t');
|
| 247 |
variable_del($code_old .'_b');
|
| 248 |
}
|
| 249 |
|
| 250 |
break;
|
| 251 |
}
|
| 252 |
}
|