| 1 |
<?php
|
| 2 |
// $Id: quicktags.module,v 1.22.2.2 2008/02/12 21:07:20 tcblack Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help().
|
| 6 |
*/
|
| 7 |
function quicktags_help($section) {
|
| 8 |
switch($section) {
|
| 9 |
case 'admin/modules#description':
|
| 10 |
return t('Allows Drupal to use quicktags around the textarea for easier user input');
|
| 11 |
}
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_perm().
|
| 16 |
*/
|
| 17 |
function quicktags_perm() {
|
| 18 |
return array('use quicktags');
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_settings().
|
| 23 |
*/
|
| 24 |
function quicktags_settings() {
|
| 25 |
|
| 26 |
$form['quicktags_options'] = array(
|
| 27 |
'#type' => 'fieldset',
|
| 28 |
'#title' => t('Quicktags options'),
|
| 29 |
);
|
| 30 |
$form['quicktags_options']['quicktags_show'] = array(
|
| 31 |
'#type' => 'radios',
|
| 32 |
'#title' => t('Show quicktags'),
|
| 33 |
'#default_value' => variable_get('quicktags_show', 0),
|
| 34 |
'#options' => array(0 => t('Everywhere'), 1 => t('Only nodes'), 2 => t('Nodes and comments')),
|
| 35 |
'#description' => t('Where to show the quicktags toolbar.'),
|
| 36 |
);
|
| 37 |
|
| 38 |
return system_settings_form($form);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_menu().
|
| 43 |
*/
|
| 44 |
function quicktags_menu($may_cache) {
|
| 45 |
$items = array();
|
| 46 |
|
| 47 |
if ($may_cache) {
|
| 48 |
$items[] = array(
|
| 49 |
'path' => 'quicktags',
|
| 50 |
'callback' => 'quicktags_javascript',
|
| 51 |
'access' => TRUE,
|
| 52 |
'type' => MENU_CALLBACK
|
| 53 |
);
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'admin/settings/quicktags',
|
| 56 |
'title' => t('Quicktags'),
|
| 57 |
'description' => t('Configure Quicktags options.'),
|
| 58 |
'callback' => 'drupal_get_form',
|
| 59 |
'callback arguments' => 'quicktags_settings',
|
| 60 |
'access' => user_access('administer site configuration'),
|
| 61 |
);
|
| 62 |
}
|
| 63 |
|
| 64 |
return $items;
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Implementation of hook_elements().
|
| 69 |
*/
|
| 70 |
function quicktags_elements() {
|
| 71 |
$type = array();
|
| 72 |
|
| 73 |
// Check to see if quicktags should show or not.
|
| 74 |
if (quicktags_show_quicktags()) {
|
| 75 |
// Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
|
| 76 |
$type['textarea'] = array(
|
| 77 |
// '#resizable' => FALSE,
|
| 78 |
'#theme' => 'quicktags_textfield',
|
| 79 |
);
|
| 80 |
}
|
| 81 |
|
| 82 |
return $type;
|
| 83 |
}
|
| 84 |
|
| 85 |
/**
|
| 86 |
* Function to evalutate if quicktags should show or not.
|
| 87 |
* Returns TRUE or FALSE.
|
| 88 |
*/
|
| 89 |
function quicktags_show_quicktags() {
|
| 90 |
if (user_access('use quicktags')) {
|
| 91 |
switch (variable_get('quicktags_show', 0)) {
|
| 92 |
case 0:
|
| 93 |
return TRUE;
|
| 94 |
break;
|
| 95 |
case 1:
|
| 96 |
if (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit')) {
|
| 97 |
return TRUE;
|
| 98 |
}
|
| 99 |
break;
|
| 100 |
case 2:
|
| 101 |
if (arg(0) == 'node' || arg(0) == 'comment') {
|
| 102 |
return TRUE;
|
| 103 |
}
|
| 104 |
break;
|
| 105 |
default:
|
| 106 |
return FALSE;
|
| 107 |
}
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
return FALSE;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Attach quicktags to a textarea
|
| 116 |
*/
|
| 117 |
function theme_quicktags_textfield($element) {
|
| 118 |
static $ta_count = 0;
|
| 119 |
|
| 120 |
$path = drupal_get_path('module','quicktags');
|
| 121 |
|
| 122 |
//Add the stylesheet
|
| 123 |
$stylesheet = $path .'/quicktags.css';
|
| 124 |
drupal_add_css($stylesheet);
|
| 125 |
|
| 126 |
//Add the static file
|
| 127 |
$javascript = $path .'/quicktags.js';
|
| 128 |
drupal_add_js($javascript);
|
| 129 |
|
| 130 |
//Add the dynamically generated file
|
| 131 |
drupal_add_js('?q=quicktags');
|
| 132 |
//using cache = FALSE might seem a good option, but this breaks on the menu.
|
| 133 |
|
| 134 |
$cols = ($element['#cols'] ? ' cols="'. $element['#cols'] .'"' : '');
|
| 135 |
$class[] = ($element['#attributes']['class'] ? $element['#attributes']['class'] : 'form-textarea');
|
| 136 |
if ($element['#resizable'] !== false) {
|
| 137 |
drupal_add_js('misc/textarea.js');
|
| 138 |
$class[] = 'resizable';
|
| 139 |
}
|
| 140 |
|
| 141 |
_form_set_class($element, $class);
|
| 142 |
|
| 143 |
$html = '<script language="JavaScript" type="text/javascript">edToolbar('. $ta_count .');</script>';
|
| 144 |
$html .= '<textarea'. $cols .' rows="'. $element['#rows'] .'" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. drupal_attributes($element['#attributes']) .'>';
|
| 145 |
$html .= check_plain($element['#value']);
|
| 146 |
$html .= '</textarea>';
|
| 147 |
$html .= '<script language="JavaScript" type="text/javascript">
|
| 148 |
<!--
|
| 149 |
edCanvas[' . $ta_count . '] = document.getElementById(\''.$element['#id'] .'\');
|
| 150 |
//-->
|
| 151 |
</script>';
|
| 152 |
$ta_count++;
|
| 153 |
|
| 154 |
return theme('form_element', $element, $html);
|
| 155 |
}
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
/* array(id, display, tagStart, tagEnd, access, open, location)
|
| 160 |
* id; // used to name the toolbar button
|
| 161 |
* display; // label on button
|
| 162 |
* tagStart; // open tag
|
| 163 |
* tagEnd; // close tag
|
| 164 |
* access; // access key
|
| 165 |
* open; // set to -1 if tag does not need to be closed
|
| 166 |
* location; // optional: onclick action omit the ().
|
| 167 |
*/
|
| 168 |
function quicktags_quicktags_insert() {
|
| 169 |
$path = base_path() . drupal_get_path('module','quicktags') .'/';
|
| 170 |
|
| 171 |
$items = array(
|
| 172 |
'ed_italic' => array(
|
| 173 |
'name' => 'italic',
|
| 174 |
'prefix' => '<em>',
|
| 175 |
'suffix' => '</em>',
|
| 176 |
'accesskey' => 'i',
|
| 177 |
'weight' => 10,
|
| 178 |
'icon' => $path .'ed_italic.png',
|
| 179 |
),
|
| 180 |
'ed_bold' => array(
|
| 181 |
'name' => 'bold',
|
| 182 |
'prefix' => '<strong>',
|
| 183 |
'suffix' => '</strong>',
|
| 184 |
'accesskey' => 'b',
|
| 185 |
'weight' => 20,
|
| 186 |
'icon' => $path .'ed_bold.png',
|
| 187 |
),
|
| 188 |
'ed_code' => array(
|
| 189 |
'name' => 'code',
|
| 190 |
'prefix' => '<code>',
|
| 191 |
'suffix' => '</code>',
|
| 192 |
'accesskey' => 'c',
|
| 193 |
'weight' => 30,
|
| 194 |
'icon' => $path .'ed_code.png',
|
| 195 |
),
|
| 196 |
'ed_block' => array(
|
| 197 |
'name' => 'blockquote',
|
| 198 |
'prefix' =>'<blockquote>',
|
| 199 |
'suffix' => '</blockquote>',
|
| 200 |
'accesskey' => 'q',
|
| 201 |
'weight' => 40,
|
| 202 |
'icon' => $path .'ed_block.png',
|
| 203 |
),
|
| 204 |
'ed_link' => array(
|
| 205 |
'name' => 'link',
|
| 206 |
'prefix' =>'<a href="http://">',
|
| 207 |
'suffix' => '</a>',
|
| 208 |
'accesskey' => 'l',
|
| 209 |
'weight' => 50,
|
| 210 |
'icon' => $path .'ed_link.png',
|
| 211 |
),
|
| 212 |
'ed_break' => array(
|
| 213 |
'name' => 'teaser break',
|
| 214 |
'prefix' =>'<!--break-->',
|
| 215 |
'accesskey' => 't',
|
| 216 |
'weight' => 60,
|
| 217 |
'close' => FALSE,
|
| 218 |
'icon' => $path .'ed_break.png',
|
| 219 |
),
|
| 220 |
);
|
| 221 |
|
| 222 |
return $items;
|
| 223 |
}
|
| 224 |
|
| 225 |
function quicktags_javascript() {
|
| 226 |
$output = '';
|
| 227 |
$items = array();
|
| 228 |
$items = module_invoke_all('quicktags_insert');
|
| 229 |
$items = array_merge($items, module_invoke_all('quicktags_alter', $items));
|
| 230 |
uasort($items, '_quicktags_sort');
|
| 231 |
|
| 232 |
$js = quicktags_javascript_prepare($items);
|
| 233 |
|
| 234 |
foreach ($items as $id => $item) {
|
| 235 |
//build the arglist to pass to JS
|
| 236 |
// id, display, tagStart, tagEnd, access, open, location, icon
|
| 237 |
$args = '\''. $id .'\',\''. $item['name'] .'\',\''. $item['prefix'] .'\',\''. $item['suffix'] .'\',\''. $item['accesskey'] .'\',\''. ($item['close'] ? 0 : -1) .'\',\''. $item['location'] .'\',\''.$item['icon'] .'\'';
|
| 238 |
|
| 239 |
$output .= "edButtons[edButtons.length] = new edButton(". $args .");\n" ;
|
| 240 |
}
|
| 241 |
|
| 242 |
if($output) {
|
| 243 |
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
|
| 244 |
print $js ."\n". $output;
|
| 245 |
}
|
| 246 |
}
|
| 247 |
|
| 248 |
function quicktags_javascript_prepare() {
|
| 249 |
$path = base_path() . drupal_get_path('module', 'quicktags');
|
| 250 |
$output = <<<EOD
|
| 251 |
function edToolbar(id) {
|
| 252 |
document.write('<div class="ed_toolbar" id="ed_toolbar' + id + '">');
|
| 253 |
for (i = 0; i < edButtons.length; i++) {
|
| 254 |
edShowButton(edButtons[i], i, id);
|
| 255 |
}
|
| 256 |
document.write('<a href="javascript:edCloseAllTags(' + id + ');"><img src="$path/ed_close.png" id="ed_close" class="ed_button" title="Close all open tags" alt="Close Tags"/></a>');
|
| 257 |
document.write('</div>');
|
| 258 |
}
|
| 259 |
EOD;
|
| 260 |
|
| 261 |
return $output;
|
| 262 |
}
|
| 263 |
|
| 264 |
function _quicktags_sort($a, $b) {
|
| 265 |
return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : ($a['name'] < $b['name'] ? -1 : 1));
|
| 266 |
}
|
| 267 |
|