| 1 |
<?php
|
| 2 |
// $Id: acronyms.module,v 1.1 2008/12/05 16:32:48 dellintosh Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* @file
|
| 6 |
* Maintains acronyms for your site.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu
|
| 11 |
*/
|
| 12 |
function acronyms_menu($may_cache) {
|
| 13 |
$items = array();
|
| 14 |
$create = user_access('create acronym');
|
| 15 |
$admin = user_access('administer acronyms');
|
| 16 |
|
| 17 |
if ($may_cache) {
|
| 18 |
$items[] = array(
|
| 19 |
'path' => 'admin/settings/acronyms',
|
| 20 |
'title' => t('Acronym'),
|
| 21 |
'callback' => 'drupal_get_form',
|
| 22 |
'callback arguments' => array('acronyms_admin_settings'),
|
| 23 |
'access' => $admin,
|
| 24 |
'type' => MENU_NORMAL_ITEM
|
| 25 |
);
|
| 26 |
$items[] = array(
|
| 27 |
'path' => 'node/add/acronym',
|
| 28 |
'title' => t('Acronym'),
|
| 29 |
'access' => $create
|
| 30 |
);
|
| 31 |
if (arg(0) == 'acronyms' && is_numeric(arg(1))) {
|
| 32 |
$acronym = acronyms_get_acronym(arg(1));
|
| 33 |
if ($acronym) {
|
| 34 |
$items[] = array(
|
| 35 |
'path' => 'acronyms/' . $acronym['aid'],
|
| 36 |
'title' => $acronym['acronym'],
|
| 37 |
'callback' => 'acronyms_page_acronym',
|
| 38 |
'access' => $admin,
|
| 39 |
'type' => MENU_CALLBACK
|
| 40 |
);
|
| 41 |
$items[] = array(
|
| 42 |
'path' => 'acronyms/' . $acronym['aid'] . '/view',
|
| 43 |
'title' => t('View'),
|
| 44 |
'type' => MENU_DEFAULT_LOCAL_TASK
|
| 45 |
);
|
| 46 |
$items[] = array(
|
| 47 |
'path' => 'acronyms/' . $acronym['aid'] . '/edit',
|
| 48 |
'title' => t('Edit Acronym'),
|
| 49 |
'callback' => 'drupal_get_form',
|
| 50 |
'callback arguments' => array('acronyms_page_acronym'),
|
| 51 |
'access' => $admin,
|
| 52 |
'type' => MENU_LOCAL_TASK
|
| 53 |
);
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'acronyms/' . $acronym['aid'] . '/delete',
|
| 56 |
'title' => t('Delete'),
|
| 57 |
'callback' => 'drupal_get_form',
|
| 58 |
'callback arguments' => array('acronyms_page_acronym'),
|
| 59 |
'access' => $admin,
|
| 60 |
'type' => MENU_LOCAL_TASK
|
| 61 |
);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
}
|
| 65 |
return $items;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_node_info()
|
| 70 |
*/
|
| 71 |
function acronyms_node_info() {
|
| 72 |
return array('acronym' => array(
|
| 73 |
'name' => t('Acronym'),
|
| 74 |
'module' => 'acronyms',
|
| 75 |
'description' => t('An acronym for use in filtering.'),
|
| 76 |
'has_title' => TRUE,
|
| 77 |
'title_label' => t('Acronym'),
|
| 78 |
'has_body' => FALSE
|
| 79 |
)
|
| 80 |
);
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Implementation of hook_access()
|
| 85 |
*/
|
| 86 |
function acronyms_access($op, $node) {
|
| 87 |
global $user;
|
| 88 |
if ($op == 'create') {
|
| 89 |
return (user_access('create acronym'));
|
| 90 |
}
|
| 91 |
if ($op == 'update' || $op == 'delete' || $op == 'edit') {
|
| 92 |
if (user_access('administer acronyms') && ($user->uid == $node->uid)) {
|
| 93 |
return TRUE;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
* Implementation of hook_perm
|
| 100 |
*/
|
| 101 |
function acronyms_perm(){
|
| 102 |
return array('administer acronyms', 'create acronym');
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Implementation of hook_form()
|
| 107 |
*/
|
| 108 |
function acronyms_form($node) {
|
| 109 |
$type = node_get_types('type', $node);
|
| 110 |
$form['title'] = array(
|
| 111 |
'#type' => 'textfield',
|
| 112 |
'#title' => check_plain($type->title_label),
|
| 113 |
'#required' => TRUE,
|
| 114 |
'#default_value' => $node->title,
|
| 115 |
'#weight' => -5
|
| 116 |
);
|
| 117 |
return $form;
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Implementation of hook_validate()
|
| 122 |
*/
|
| 123 |
function acronyms_validate($node) {
|
| 124 |
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Implementation of hook_insert()
|
| 129 |
*/
|
| 130 |
function acronyms_insert($node) {
|
| 131 |
db_query("INSERT INTO {acronyms} (nid, vid, acronym) VALUES (%d, %d, '%s')", $node->nid, $node->vid, strtoupper($node->title));
|
| 132 |
}
|
| 133 |
|
| 134 |
/**
|
| 135 |
* Implementation of hook_update()
|
| 136 |
*/
|
| 137 |
function acronyms_update(&$node) {
|
| 138 |
if ($node->revision) {
|
| 139 |
acronyms_insert($node);
|
| 140 |
} else {
|
| 141 |
db_query("UPDATE {acronyms} SET acronym = '%s' WHERE vid = %d", strtoupper($node->title), $node->vid);
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implementation of hook_delete()
|
| 147 |
*/
|
| 148 |
function acronyms_delete(&$node) {
|
| 149 |
// delete the related information we are saving for this node
|
| 150 |
db_query('DELETE FROM {acronyms} WHERE nid = %d', $node->nid);
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Implementation of hook_load()
|
| 155 |
*/
|
| 156 |
function acronyms_load($node) {
|
| 157 |
return db_fetch_object(db_query('SELECT acronym FROM {acronyms} WHERE vid = %d', $node->vid));
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Save new acronym nodes
|
| 162 |
*/
|
| 163 |
function acronyms_save_acronyms($acronyms) {
|
| 164 |
foreach ($acronyms as $acronym) {
|
| 165 |
$node = new stdClass();
|
| 166 |
$node->type = 'acronym';
|
| 167 |
node_object_prepare($node);
|
| 168 |
$node->title = strtoupper($acronym);
|
| 169 |
node_save($node);
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Define the settings form
|
| 175 |
*/
|
| 176 |
function acronyms_admin_settings() {
|
| 177 |
$form['enabled'] = array(
|
| 178 |
'#type' => 'checkbox',
|
| 179 |
'#title' => t('Enable Acronym filtering'),
|
| 180 |
'#description' => t('Enable or disable filtering of Acronyms on your site.'),
|
| 181 |
'#default_value' => variable_get('acronyms_site_enable', 1)
|
| 182 |
);
|
| 183 |
$form['auto_create'] = array(
|
| 184 |
'#type' => 'checkbox',
|
| 185 |
'#title' => t('Automatically Create Acronyms'),
|
| 186 |
'#description' => t('If enabled, this module will attempt to find acronyms within your strings and automatically add them.'),
|
| 187 |
'#default_value' => variable_get('acronyms_auto_create', 0)
|
| 188 |
);
|
| 189 |
$form['current_acronyms'] = array(
|
| 190 |
'#type' => 'fieldset',
|
| 191 |
'#title' => 'Current Acronyms'
|
| 192 |
);
|
| 193 |
$form['current_acronyms']['list'] = array(
|
| 194 |
'#type' => 'item',
|
| 195 |
'#description' => acronyms_list()
|
| 196 |
);
|
| 197 |
$form['add_acronym'] = array(
|
| 198 |
'#type' => 'item',
|
| 199 |
'#description' => l(t('Add new acronym'), 'node/add/acronym')
|
| 200 |
);
|
| 201 |
return system_settings_form($form);
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Submit the admin settings form
|
| 206 |
*/
|
| 207 |
function acronyms_admin_settings_submit($form_id, &$form) {
|
| 208 |
// save the sitewide enable/disable option
|
| 209 |
variable_set('acronyms_site_enable', $form['enabled']);
|
| 210 |
variable_set('acronyms_auto_create', $form['auto_create']);
|
| 211 |
}
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Returns an array of the acronym fields
|
| 215 |
*/
|
| 216 |
function acronyms_get_acronym($id) {
|
| 217 |
if (is_numeric($id)) {
|
| 218 |
return db_fetch_array(db_query('SELECT * FROM {acronyms} WHERE aid = %d', $id));
|
| 219 |
} else {
|
| 220 |
return array('aid' => 0);
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Returns a table of current acronyms, including edit/delete links
|
| 226 |
*/
|
| 227 |
function acronyms_list() {
|
| 228 |
$rows = array();
|
| 229 |
$result = db_query('SELECT nid, acronym FROM {acronyms}');
|
| 230 |
while ($data = db_fetch_array($result)) {
|
| 231 |
$row['acronym'] = l($data['acronym'], 'node/' . $data['nid']);
|
| 232 |
$row['edit'] = l(t('Edit'), 'node/' . $data['nid'] . '/edit');
|
| 233 |
$row['delete'] = l(t('Delete'), 'node/' . $data['nid'] . '/delete');
|
| 234 |
$rows[] = $row;
|
| 235 |
}
|
| 236 |
return theme('table', array('Acronym', 'Edit', 'Delete'), $rows);
|
| 237 |
}
|
| 238 |
|
| 239 |
/***************************************************************************************************
|
| 240 |
* The following sections contain the code necessary for filtering the acronyms from given strings.
|
| 241 |
***************************************************************************************************/
|
| 242 |
/*
|
| 243 |
* This function returns an array containing all enabled acronyms
|
| 244 |
*/
|
| 245 |
function acronyms_get_all_acronyms() {
|
| 246 |
$result = db_query('SELECT acronym FROM {acronyms} WHERE enabled = %d', 1);
|
| 247 |
while ($acronym = db_fetch_array($result)) {
|
| 248 |
$acronyms[] = strtoupper($acronym['acronym']); // convert to caps, just in case it was put in otherwise.
|
| 249 |
}
|
| 250 |
return $acronyms;
|
| 251 |
}
|
| 252 |
|
| 253 |
/*
|
| 254 |
* Returns an array of lowercase words (like 'a', 'and', 'the', etc.)
|
| 255 |
* Depending on how big this list gets it might be worth maintaining in the db, but I figure this is good for now.
|
| 256 |
*/
|
| 257 |
function acronyms_lowercase_words() {
|
| 258 |
$lcwords = array(
|
| 259 |
'for', 'and', 'nor', 'but', 'or', 'yet', 'so', // FANBOYS (conjunctions)
|
| 260 |
'a', 'the', 'it', 'they', // pronouns
|
| 261 |
// prepositions
|
| 262 |
'about', 'above', 'across', 'after', 'against', 'along', 'among', 'around', 'at',
|
| 263 |
'before', 'behind', 'below', 'beneath', 'beside', 'between', 'beyond', 'but', 'by',
|
| 264 |
'despite', 'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'like', 'near',
|
| 265 |
'of', 'off', 'on', 'onto', 'out', 'outside', 'over', 'past', 'since', 'through', 'throughout',
|
| 266 |
'till', 'to', 'toward', 'under', 'underneath', 'until', 'up', 'upon', 'with', 'within', 'without'
|
| 267 |
);
|
| 268 |
return $lcwords;
|
| 269 |
}
|
| 270 |
|
| 271 |
/*
|
| 272 |
* This function will return a formatted string, making sure to keep the acronyms intact.
|
| 273 |
*
|
| 274 |
* @param $string = unformatted string
|
| 275 |
*/
|
| 276 |
function acronyms_parse_string($string) {
|
| 277 |
if (variable_get('acronyms_site_enable', 1) == 1) {
|
| 278 |
// get all current acronyms
|
| 279 |
$acronyms = acronyms_get_all_acronyms();
|
| 280 |
$lcwords = acronyms_lowercase_words();
|
| 281 |
// check $string and act accordingly
|
| 282 |
if ($string == strtoupper($string)) { // if string is ALL CAPS
|
| 283 |
$output = acronyms_format_string($string, $acronyms, $lcwords, 'uppercase');
|
| 284 |
} elseif ($string == strtolower($string)) { // if string is all lowercase
|
| 285 |
$output = $string;
|
| 286 |
} else { // string is mixed case, so we should check for acronyms in it.
|
| 287 |
$output = acronyms_format_string($string, $acronyms, $lcwords, 'mixed');
|
| 288 |
}
|
| 289 |
} else {
|
| 290 |
watchdog('acronyms', 'Sitewide filtering disabled. Please configure this module at /admin/settings/acronyms', WATCHDOG_WARNING);
|
| 291 |
$output = $string;
|
| 292 |
}
|
| 293 |
return $output;
|
| 294 |
}
|
| 295 |
|
| 296 |
/*
|
| 297 |
* This is where the magic happens, formatting the actual string.
|
| 298 |
*/
|
| 299 |
function acronyms_format_string($string, $acronyms, $lcwords, $string_type) {
|
| 300 |
$result = array(); // blank return container.
|
| 301 |
$new_acronyms = array(); // container for any new acronyms found.
|
| 302 |
// explode the string into an array for easier digestion.
|
| 303 |
$words = explode(' ', trim($string));
|
| 304 |
|
| 305 |
if (is_array($acronyms)) {
|
| 306 |
foreach ($words as $word) {
|
| 307 |
if (in_array($word, $acronyms)) {
|
| 308 |
$result[] = $word;
|
| 309 |
} else {
|
| 310 |
if ((in_array(strtolower($word), $lcwords)) && ($word !== $words[0])) {
|
| 311 |
$result[] = strtolower($word);
|
| 312 |
} else {
|
| 313 |
if ($string_type == 'uppercase') {
|
| 314 |
$result[] = ucwords(strtolower($word));
|
| 315 |
} else {
|
| 316 |
if ($word == strtoupper($word)) { // We have found a NEW Acronym! Now make sure to save it! :)
|
| 317 |
$new_acronyms[] = $word;
|
| 318 |
$result[] = $word;
|
| 319 |
} else {
|
| 320 |
$result[] = $word;
|
| 321 |
}
|
| 322 |
}
|
| 323 |
}
|
| 324 |
}
|
| 325 |
}
|
| 326 |
} else {
|
| 327 |
// if there is no acronyms array then we simply return the string and log an error.
|
| 328 |
$result = array();
|
| 329 |
$result[] = $string;
|
| 330 |
watchdog('acronyms', 'No Acronyms found. Please add new acronyms before using this module.', WATCHDOG_WARNING, l(t('Add acronyms'), 'acronyms/add'));
|
| 331 |
}
|
| 332 |
// Before we return the result, make sure to save any acronyms that were discovered during this function. :)
|
| 333 |
if ($new_acronyms && variable_get('acronyms_auto_create', 0) == 1) {
|
| 334 |
acronyms_save_acronyms($new_acronyms);
|
| 335 |
}
|
| 336 |
return implode(' ', $result);
|
| 337 |
}
|
| 338 |
|
| 339 |
|