| 1 |
<?php
|
| 2 |
// $Id: rotor.module,v 1.1 2008/04/01 18:14:43 nestormata Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* A rotor banner consists in a set of images that will be changing.
|
| 7 |
* This module is made using jquery.
|
| 8 |
*
|
| 9 |
* @author Nestor Mata Cuthbert, nestor@achieveinternet.com. http://www.achieveinternet.com
|
| 10 |
*/
|
| 11 |
|
| 12 |
// CONSTANTS
|
| 13 |
define('ROTOR_GROUP_TABS', 0);
|
| 14 |
define('ROTOR_DONT_GROUP_TABS', 1);
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Node Info Hook
|
| 18 |
*/
|
| 19 |
function rotor_node_info() {
|
| 20 |
return array(
|
| 21 |
'rotor_item' => array(
|
| 22 |
'name' => t('Rotor Item'),
|
| 23 |
'module' => 'rotor',
|
| 24 |
'description' => t("A node to be presented in the rotor block."),
|
| 25 |
),
|
| 26 |
);
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Perm hook
|
| 31 |
*/
|
| 32 |
function rotor_perm() {
|
| 33 |
return array('administer rotor');
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Access hook
|
| 38 |
*/
|
| 39 |
function rotor_access($op, $node) {
|
| 40 |
return user_access('administer rotor');
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Help hook
|
| 45 |
*/
|
| 46 |
function rotor_help($section) {
|
| 47 |
switch($section) {
|
| 48 |
case 'node/add/rotor-item':
|
| 49 |
$text = '<p>'
|
| 50 |
. t('A rotor item is a banner that will appear in the rotor block for advertising'
|
| 51 |
. ' or display important information or images.<br />'
|
| 52 |
. 'The rotor item will have a tab text that can be configured to be shown or not'
|
| 53 |
. ' in the administration page for the rotor.<br />'
|
| 54 |
. 'The item will show the image if this one is chosen otherwise will show the'
|
| 55 |
. ' content.') . '</p>';
|
| 56 |
if(user_access('administer rotor')) {
|
| 57 |
$text .= t('You can go to the rotor administration page <a href="@link">here</a>'
|
| 58 |
, array('@link' => url('admin/settings/rotor')));
|
| 59 |
}
|
| 60 |
return $text;
|
| 61 |
case 'admin/settings/rotor':
|
| 62 |
return t('');
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Menu Hook
|
| 68 |
*/
|
| 69 |
function rotor_menu($may_cache) {
|
| 70 |
if ($may_cache){
|
| 71 |
$items[] = array(
|
| 72 |
'path' => 'admin/settings/rotor',
|
| 73 |
'title' => t('Administer the Rotor Banner'),
|
| 74 |
'callback' => 'drupal_get_form',
|
| 75 |
'callback arguments' => array('rotor_admin_form'),
|
| 76 |
'access' => user_access('administer rotor'),
|
| 77 |
);
|
| 78 |
}
|
| 79 |
return $items;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Block hook
|
| 84 |
*/
|
| 85 |
function rotor_block($op = 'list', $delta = 0, $edit = array()) {
|
| 86 |
switch($op) {
|
| 87 |
case 'list':
|
| 88 |
$blocks[0] = array(
|
| 89 |
'info' => t('The rotor banner block'),
|
| 90 |
'weight' => 0,
|
| 91 |
'enabled' => 0,
|
| 92 |
'region' => 'content',
|
| 93 |
);
|
| 94 |
return $blocks;
|
| 95 |
case 'view':
|
| 96 |
// Only one delta so far so we are not checking the delta
|
| 97 |
$block = array(
|
| 98 |
'subject' => t('Information'),
|
| 99 |
'content' => rotor_block_content(),
|
| 100 |
);
|
| 101 |
return $block;
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Admin settings form page.
|
| 107 |
*/
|
| 108 |
function rotor_admin_form() {
|
| 109 |
$form['max_items'] = array(
|
| 110 |
'#type' => 'textfield',
|
| 111 |
'#title' => t('Max nodes'),
|
| 112 |
'#default_value' => variable_get('rotor_max_items', 3),
|
| 113 |
'#rows' => 1,
|
| 114 |
'#size' => 2,
|
| 115 |
'#description' => t('Define the maximun number of nodes to present in the rotor block.'),
|
| 116 |
);
|
| 117 |
$form['time'] = array(
|
| 118 |
'#type' => 'textfield',
|
| 119 |
'#title' => t('Time'),
|
| 120 |
'#default_value' => variable_get('rotor_seconds', 10),
|
| 121 |
'#rows' => 1,
|
| 122 |
'#size' => 2,
|
| 123 |
'#description' => t('The time in seconds that will be shown every rotor item before change to the next one.'),
|
| 124 |
);
|
| 125 |
$form['show_tab'] = array(
|
| 126 |
'#type' => 'checkbox',
|
| 127 |
'#title' => t('Enable tabs'),
|
| 128 |
'#default_value' => variable_get('rotor_show_tabs', TRUE),
|
| 129 |
'#description' => t('Shows/Hide the item tabs in the block'),
|
| 130 |
);
|
| 131 |
$form['group_tabs'] = array(
|
| 132 |
'#type' => 'radios',
|
| 133 |
'#title' => t('Group tabs'),
|
| 134 |
'#default_value' => variable_get('rotor_group_tabs', ROTOR_GROUP_TABS),
|
| 135 |
'#options' => array(ROTOR_GROUP_TABS => t('Group tabs'), ROTOR_DONT_GROUP_TABS => t('Each tab with item')),
|
| 136 |
'#description' => t('If tabs are grups this will be presented all the tabs toguether otherwise each tab will be with each item.'),
|
| 137 |
);
|
| 138 |
$form['#suffix'] = theme('rotor_admin_list', rotor_get_all_items());
|
| 139 |
return system_settings_form($form);
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Admin settings form submit function
|
| 144 |
*/
|
| 145 |
function rotor_admin_form_submit($formid, $values, $extra = null) {
|
| 146 |
if ($formid == 'rotor_admin_form') {
|
| 147 |
if ($values['op'] == $values['submit']) {
|
| 148 |
variable_set('rotor_max_items', $values['max_items']);
|
| 149 |
variable_set('rotor_seconds', $values['time']);
|
| 150 |
variable_set('rotor_show_tabs', $values['show_tab']);
|
| 151 |
variable_set('rotor_group_tabs', $values['group_tabs']);
|
| 152 |
drupal_set_message(t('Settings saved'));
|
| 153 |
}
|
| 154 |
else {
|
| 155 |
variable_set('rotor_max_items', 3);
|
| 156 |
variable_set('rotor_seconds', 10);
|
| 157 |
variable_set('rotor_show_tabs', TRUE);
|
| 158 |
variable_set('rotor_group_tabs', ROTOR_GROUP_TABS);
|
| 159 |
drupal_set_message(t('Settings back to defaults'));
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Node form hook
|
| 166 |
*/
|
| 167 |
function rotor_form($node) {
|
| 168 |
// Get metadata
|
| 169 |
$type = node_get_types('type', $node);
|
| 170 |
|
| 171 |
$form['title'] = array(
|
| 172 |
'#type' => 'textfield',
|
| 173 |
'#title' => t('Tab Text'),
|
| 174 |
'#required' => TRUE,
|
| 175 |
'#default_value' => $node->title,
|
| 176 |
'#weight' => -5,
|
| 177 |
'#description' => t('The text that will be shown in the tab for this item.'),
|
| 178 |
);
|
| 179 |
$image = $node->rotor_image
|
| 180 |
? theme('image', file_create_url($node->rotor_image), '', '', NULL, FALSE)
|
| 181 |
: '';
|
| 182 |
$form['rotor_image'] = array(
|
| 183 |
'#type' => 'file',
|
| 184 |
'#title' => t('Attach an image'),
|
| 185 |
'#default_value' => $node->rotor_image,
|
| 186 |
'#weight' => -4,
|
| 187 |
'#prefix' => $image,
|
| 188 |
'#description' => t('The image that will be shown in the rotor content.'.
|
| 189 |
' If an image is uploaded only the image will be shown, otherwise only the content.'),
|
| 190 |
);
|
| 191 |
$form['body_filter']['body'] = array(
|
| 192 |
'#type' => 'textarea',
|
| 193 |
'#title' => t('Content'),
|
| 194 |
'#default_value' => $node->body,
|
| 195 |
'#rows' => 10,
|
| 196 |
'#weight' => -3,
|
| 197 |
'#description' => t('The content that will be shown in case no image is uploaded.'),
|
| 198 |
);
|
| 199 |
$form['body_filter']['filter'] = filter_form($node->format);
|
| 200 |
// Change the enctype of the form to handle the upload file.
|
| 201 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 202 |
return $form;
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Node form submit function.
|
| 207 |
* We handle the image submition.
|
| 208 |
*/
|
| 209 |
function rotor_submit(&$node) {
|
| 210 |
if(!$file = file_save_upload('rotor_image', file_directory_path(), FILE_EXISTS_RENAME)){
|
| 211 |
watchdog('rotor', 'Imaged not saved');
|
| 212 |
form_set_error('rotor_image', t('File upload failed.'));
|
| 213 |
}
|
| 214 |
else {
|
| 215 |
watchdog('rotor', 'Imaged saved:' . $file->file_path);
|
| 216 |
$node->rotor_image = $file->filepath;
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Hook Insert
|
| 222 |
*/
|
| 223 |
function rotor_insert($node) {
|
| 224 |
db_query(
|
| 225 |
"INSERT INTO {rotor_item} (nid, file_path) VALUES (%d, '%s')"
|
| 226 |
, $node->nid, $node->rotor_image
|
| 227 |
);
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Hook Update
|
| 232 |
*/
|
| 233 |
function rotor_update($node) {
|
| 234 |
db_query(
|
| 235 |
"UPDATE {rotor_item} SET file_path = '%s' WHERE nid = %d"
|
| 236 |
, $node->rotor_image, $node->nid
|
| 237 |
);
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Hook Delete
|
| 242 |
*/
|
| 243 |
function rotor_delete($node) {
|
| 244 |
db_query(
|
| 245 |
"DELETE FROM {rotor_item} WHERE nid = %d"
|
| 246 |
, $node->nid
|
| 247 |
);
|
| 248 |
}
|
| 249 |
|
| 250 |
/**
|
| 251 |
* Node Load hook
|
| 252 |
*/
|
| 253 |
function rotor_load($node) {
|
| 254 |
$additions = db_fetch_object(db_query(
|
| 255 |
'SELECT file_path AS rotor_image FROM {rotor_item} WHERE nid = %d',
|
| 256 |
$node->nid));
|
| 257 |
return $additions;
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
* Node view hook
|
| 262 |
*/
|
| 263 |
function rotor_view($node, $teaser = FALSE, $page = FALSE) {
|
| 264 |
$node = node_prepare($node, $teaser);
|
| 265 |
if($node->rotor_image){
|
| 266 |
$node->content['body']['#value']
|
| 267 |
= theme('image', file_create_url($node->rotor_image), '', '', NULL, FALSE );
|
| 268 |
}
|
| 269 |
return $node;
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
* Returns the block content.
|
| 274 |
*/
|
| 275 |
function rotor_block_content() {
|
| 276 |
$limit = variable_get('rotor_max_items', 3);
|
| 277 |
$items = array();
|
| 278 |
$result = db_query("SELECT nid FROM {node} WHERE type = 'rotor_item' LIMIT %d", $limit);
|
| 279 |
if ($result) {
|
| 280 |
while ($node = db_fetch_array($result)) {
|
| 281 |
$items[] = node_load($node);
|
| 282 |
}
|
| 283 |
}
|
| 284 |
return theme('rotor_block', $items);
|
| 285 |
}
|
| 286 |
|
| 287 |
/**
|
| 288 |
* Return a list of nodes of all the rotor_item nodes.
|
| 289 |
*/
|
| 290 |
function rotor_get_all_items() {
|
| 291 |
$items = array();
|
| 292 |
$result = db_query("SELECT nid FROM {node} WHERE type = 'rotor_item'");
|
| 293 |
if ($result) {
|
| 294 |
while ($node = db_fetch_array($result)) {
|
| 295 |
$items[] = node_load($node);
|
| 296 |
}
|
| 297 |
}
|
| 298 |
return $items;
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Theme function for the block
|
| 303 |
*
|
| 304 |
* @param array $items The list of nodes to present in the block.
|
| 305 |
*/
|
| 306 |
function theme_rotor_block($items = array()) {
|
| 307 |
if(count($items) == 0){
|
| 308 |
return '';
|
| 309 |
}
|
| 310 |
|
| 311 |
// Prints the script variables and includes the rotor javascript file.
|
| 312 |
$time = variable_get('rotor_seconds', 10);
|
| 313 |
drupal_add_js(drupal_get_path('module', 'rotor') . '/rotor.js');
|
| 314 |
drupal_add_js('var rotor_enabled = true;', 'inline');
|
| 315 |
drupal_add_js('var rotor_time = ' . $time . ';', 'inline');
|
| 316 |
|
| 317 |
// Print the rotor items.
|
| 318 |
$output = '<div id="rotor">';
|
| 319 |
if(variable_get('rotor_group_tabs', ROTOR_GROUP_TABS) == ROTOR_GROUP_TABS) {
|
| 320 |
$output .= theme('rotor_tabs', $items);
|
| 321 |
$output .= theme('rotor_items', $items);
|
| 322 |
}
|
| 323 |
else {
|
| 324 |
$output .= theme('rotor_items', $items);
|
| 325 |
}
|
| 326 |
$output .= '</div>';
|
| 327 |
return $output;
|
| 328 |
}
|
| 329 |
|
| 330 |
/**
|
| 331 |
* Theme for the rotor tabs.
|
| 332 |
*
|
| 333 |
* @param array $items The array of items from where to get the tabs.
|
| 334 |
*/
|
| 335 |
function theme_rotor_tabs($items = array()) {
|
| 336 |
$show_tabs = variable_get('rotor_show_tabs', TRUE);
|
| 337 |
$group_tabs = variable_get('rotor_group_tabs', ROTOR_GROUP_TABS);
|
| 338 |
$output = '';
|
| 339 |
if($show_tabs && $group_tabs == ROTOR_GROUP_TABS) {
|
| 340 |
$output = '<div class="rotor_tabs">';
|
| 341 |
foreach ($items as $item) {
|
| 342 |
$output .= '<div class="rotor_tab">' . $item->title . '</div>';
|
| 343 |
}
|
| 344 |
$output .= '</div>';
|
| 345 |
}
|
| 346 |
return $output;
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Theme for the rotor items list.
|
| 351 |
*
|
| 352 |
* @param array $items The array of items.
|
| 353 |
*/
|
| 354 |
function theme_rotor_items($items = array()) {
|
| 355 |
$output = '';
|
| 356 |
foreach ($items as $item) {
|
| 357 |
$output .= theme('rotor_item', $item);
|
| 358 |
}
|
| 359 |
return $output;
|
| 360 |
}
|
| 361 |
|
| 362 |
/**
|
| 363 |
* Theme for each rotor item.
|
| 364 |
*
|
| 365 |
* @param node $item The rotor_item node to theme.
|
| 366 |
*/
|
| 367 |
function theme_rotor_item($item) {
|
| 368 |
$show_tabs = variable_get('rotor_show_tabs', TRUE);
|
| 369 |
$group_tabs = variable_get('rotor_group_tabs', ROTOR_GROUP_TABS);
|
| 370 |
$output = '<div class="rotor_content">';
|
| 371 |
if($show_tabs && $group_tabs == ROTOR_DONT_GROUP_TABS) {
|
| 372 |
$output .= '<div class="rotor_tab">' . $item->title . '</div>';
|
| 373 |
}
|
| 374 |
$output .= '<div class="rotor_content">'
|
| 375 |
. ($item->rotor_image
|
| 376 |
? theme('image', file_create_url($item->rotor_image), '', '', NULL, FALSE)
|
| 377 |
: $item->body)
|
| 378 |
. '</div>';
|
| 379 |
$output .= '</div>';
|
| 380 |
return $output;
|
| 381 |
}
|
| 382 |
|
| 383 |
/**
|
| 384 |
* Theme the admin list to include in the rotor administration page.
|
| 385 |
*
|
| 386 |
* @param array $list The list of rotor_item nodes to display in the list.
|
| 387 |
*/
|
| 388 |
function theme_rotor_admin_list($list = array()) {
|
| 389 |
$headers = array(t('Tab'), t('Content'), t('Actions'));
|
| 390 |
$rows = array();
|
| 391 |
$count = 0;
|
| 392 |
foreach($list as $item) {
|
| 393 |
if($item->rotor_image) {
|
| 394 |
}
|
| 395 |
$rows[] = array(
|
| 396 |
l($item->title, 'node/' . $item->nid . '/edit'),
|
| 397 |
($item->rotor_image
|
| 398 |
? theme('image', file_create_url($item->rotor_image), '', '', NULL, FALSE)
|
| 399 |
: $item->body),
|
| 400 |
l(t('Edit'), 'node/' . $item->nid . '/edit')
|
| 401 |
. ' | '
|
| 402 |
. l(t('Remove'), 'node/' . $item->nid . '/delete')
|
| 403 |
);
|
| 404 |
}
|
| 405 |
$output = l(t('Add new item'), 'node/add/rotor-item');
|
| 406 |
$output .= theme('table', $headers, $rows, array('class' => 'rotor_admin_list'));
|
| 407 |
return $output;
|
| 408 |
}
|