--- content_slider.module +++ (clipboard) @@ -1,7 +1,6 @@ 'Content Slider', - 'description' => t('Setting Content Slider.'), + 'description' => 'Setting Content Slider.', 'page callback' => 'drupal_get_form', 'page arguments' => array('content_slider_admin_settings'), - 'access arguments' => array('administer site configuration'), + 'access arguments' => array('administer content slider'), ); - $items[] = array( - 'path' => 'content_slider/list/'. arg(2), - 'title' => t('Content Slider'), - 'page callback' => 'content_slider_page_list', - 'page arguments' => array(arg(2)), - 'access arguments' => array('access content_slider'), - 'type' => MENU_CALLBACK, - ); + return $items; } function content_slider_admin_settings() { - // only administrators can access this function - // Generate the form - settings applying to all patterns first - $form['content_slider_settings'] = array( + // Defines form elements for sliders content type + $form['content_slider_source'] = array( '#type' => 'fieldset', - '#weight' => -20, - '#title' => t('Basic settings'), + '#title' => t('Slider content source (based on Drupal content-type)'), + '#description' => t('Enter the machine name of the content types you wish to use for each slider. Only one per slider, ex: story, page, image, etc.'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); - $form['content_slider_settings']['content_slider_auto'] = array( - '#type' => 'checkbox', - '#title' => t('Auto rotate contents'), - '#default_value' => variable_get('content_slider_auto', 1), - '#description' => t("Display content without mouse click."), - '#maxlength' => '1', '#size' => '1', + $form['content_slider_source']['content_slider_source_0'] = array( + '#type' => 'textfield', + '#title' => t('Content type for Slider 1'), + '#default_value' => variable_get('content_slider_source_0', 'page'), ); - $form['content_slider_settings']['content_slider_speed'] = array( + $form['content_slider_source']['content_slider_source_1'] = array( '#type' => 'textfield', - '#title' => t('Glide animation duration (in milliseconds)'), - '#default_value' => variable_get('content_slider_speed', 1000), + '#title' => t('Content type for Slider 2'), + '#default_value' => variable_get('content_slider_source_1', 'story'), ); - $form['content_slider_settings']['content_slider_direction'] = array( + $form['content_slider_source']['content_slider_source_2'] = array( '#type' => 'textfield', - '#title' => t('Set direction of glide'), - '#default_value' => variable_get('content_slider_direction', 'leftright'), - '#description' => 'Available value: "updown", "downup", "leftright", or "rightleft"', + '#title' => t('Content type for Slider 3'), + '#default_value' => variable_get('content_slider_source_2', 'featured'), ); - // set direction of glide: "updown", "downup", "leftright", or "rightleft"' - //$output_body .= 'direction: "downup", - - $form['content_slider_source'] = array( + // General settings + $form['content_slider_settings'] = array( '#type' => 'fieldset', - '#weight' => -20, - '#title' => t('Slider content source (based on Drupal content-type)'), + '#title' => t('Basic settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); - $form['content_slider_source']['content_slider_source_1'] = array( - '#type' => 'textfield', - '#title' => t('Content type for Slider 1'), - '#default_value' => variable_get('content_slider_source_1', 'page'), - ); - - $form['content_slider_source']['content_slider_source_2'] = array( - '#type' => 'textfield', - '#title' => t('Content type for Slider 2'), - '#default_value' => variable_get('content_slider_source_2', 'story'), + $form['content_slider_settings']['content_slider_auto'] = array( + '#type' => 'checkbox', + '#title' => t('Auto rotate contents'), + '#default_value' => variable_get('content_slider_auto', 1), + '#description' => t("Should the rotation be automatic?"), ); - $form['content_slider_source']['content_slider_source_3'] = array( + $form['content_slider_settings']['content_slider_speed'] = array( '#type' => 'textfield', - '#title' => t('Content type for Slider 3'), - '#default_value' => variable_get('content_slider_source_3', 'featured'), + '#size' => 4, + '#title' => t('Glide animation duration (in milliseconds)'), // TODO + '#default_value' => variable_get('content_slider_speed', 1000), ); + + // Commented since this has I don't see where it is implemented + // + // $form['content_slider_settings']['content_slider_direction'] = array( + // '#type' => 'select', + // '#title' => t('Set animation direction of glide'), + // '#default_value' => variable_get('content_slider_direction', 'leftright'), + // '#options' => array( + // 'updown' => t('From up to down'), + // 'downup' => t('From down to up'), + // 'leftright' => t('From left to right'), + // 'rightleft' => t('From right to left'), + // ), + // ); return system_settings_form($form); } @@ -119,130 +116,88 @@ * */ function content_slider_block($op = 'list', $delta = 0) { - $content_slider_auto = variable_get("content_slider_auto", 1); - $content_slider_speed = variable_get("content_slider_speed", 1000); - $content_slider_direction = variable_get("content_slider_direction", 'leftright'); - - $content_slider_source_1 = variable_get("content_slider_source_1","page"); - $content_slider_source_2 = variable_get("content_slider_source_2","story"); - $content_slider_source_3 = variable_get("content_slider_source_3","featured"); - - global $base_url; - $ajax_base_path = $base_url; - - if ($op == 'list') { - $blocks[0]['info'] = 'Content Slider 1'; - $blocks[1]['info'] = 'Content Slider 2'; - $blocks[2]['info'] = 'Content Slider 3'; - return $blocks; - } - - if ($op == 'view') { - - global $base_url; - - drupal_add_css(drupal_get_path('module', 'content_slider') . '/contentslider.css'); - drupal_add_js(drupal_get_path('module', 'content_slider'). '/contentslider.js'); - $output_body = ""; - //Text - $output_head = ''."\n"; - $output_head .= '/***********************************************'."\n"; - $output_head .= '* Featured Content Slider script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)'."\n"; - $output_head .= '* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts'."\n"; - $output_head .= '* This notice must stay intact for legal use'."\n"; - $output_head .= '***********************************************/'."\n"; - //drupal_add_js($output_head,'inline'); - - $slider_id = $delta + 1; - switch ($delta) { - case 0: - $content_type = $content_slider_source_1; - $block['subject'] = 'Content Slider 1'; - $block['content'] = outbody_of_block($content_type,$delta); - break; - - case 1: - $content_type = $content_slider_source_2; - $block['subject'] = 'Content Slider 2'; - $block['content'] = outbody_of_block($content_type,$delta); - - break; - - case 2: - $content_type = $content_slider_source_3; - $block['subject'] = 'Content Slider 3'; - $block['content'] = outbody_of_block($content_type,$delta); - - break; - } - return $block; + switch ($op) { + case 'list': + // This is an hard limit which isn't good + for ($i=0; $i < 3; $i++) { + $content_type = variable_get('content_slider_source_'. $i, ''); + if ($content_type != '') { + $blocks[$i]['info'] = t('Content Slider !delta - !content_type', array('!delta' => $i, '!content_type' => $content_type)); + } + } + return $blocks; + break; + case 'view': + $content_type = variable_get('content_slider_source_'. $delta, ''); + + if ($content_type != '') { + // We add necessary CSS and JavaScript files + drupal_add_css(drupal_get_path('module', 'content_slider') . '/contentslider.css'); + drupal_add_js(drupal_get_path('module', 'content_slider'). '/contentslider.js'); + + $nodes = content_slider_load_nodes($content_type); + + $block['subject'] = t('Content Slider !delta', array('!delta' => $delta)); + $block['content'] = theme('content_slider', $nodes, $delta); + + $t_previous = t('Previous'); + $t_next = t('Next'); + $speed = variable_get('content_slider_speed', 1000); + $auto_rotate = (bool) variable_get('content_slider_auto', 1); + + $script .= "$(document).ready(function(){\n"; + $script .= " featuredcontentslider.init({\n"; + $script .= " id: 'slider$delta',\n"; + $script .= " contentsource: ['inline', ''],\n"; + $script .= " toc: '#increment',\n"; + $script .= " nextprev: ['$t_previous', '$t_next'],\n"; + $script .= " revealtype: 'click',\n"; + $script .= " enablefade: [true, 0.2],\n"; + $script .= " autorotate: [$auto_rotate, $speed],\n"; + $script .= " });\n"; + $script .= "});\n"; + + drupal_add_js($script, 'inline', 'header'); + } + return $block; + break; } } -function outbody_of_block($content_type,$delta) -{ - $output_body = ''; - $sql = " SELECT n.nid, teaser FROM {node} n INNER JOIN {node_revisions} r ON n.nid=r.nid "." WHERE n.status=1 AND n.type='".$content_type."' ORDER BY n.changed DESC LIMIT 5"; - $results = db_query($sql); - - $output_body .= '
'."\n"; - while ($data = db_fetch_object($results)) { - $slider_nid = $data->nid; - $output_body .= '
'."\n"; - $output_body .= "". node_view(node_load($data->nid), 1) .""; - if ($slider_nid == 1) { - $output_body .= '

'; - } - $output_body .= '
'; - } - $output_body .= '
'; - $output_body .= ''; - - $output_body .= ''; - return $output_body; - - //$output_body .= ''; -} - -function content_slider_page_list($i) { - $content_slider_item = variable_get("content_slider_item", 10); - - $dse_news_total = db_query(" SELECT count(*) AS total FROM {node} n WHERE n.type='slider".$i."' AND n.status=1"); - $row_news = db_fetch_object($dse_news_total); - $news_total = $row_news->total; - $sql_counts = "SELECT $news_total"; - - $sql = " SELECT n.nid, n.title, r.teaser FROM {node} n INNER JOIN {node_revisions} r ON r.nid = n.nid WHERE n.type = 'slider$i' AND n.status=1 ORDER BY n.created DESC"; - $sql = db_rewrite_sql($sql); - - $results = pager_query($sql, $content_slider_item, 0, $sql_counts); - - $output = "
"; - while ($node = db_fetch_object($results)) { - $output .= node_view(node_load($node->nid), 1); +function content_slider_load_nodes($content_type, $delta) { + $nodes = array(); + // I'm wondering why ORDER BY n.changed? wouldn't be n.created more accurate? + $result = db_query_range("SELECT n.nid FROM {node} n WHERE n.status = 1 AND n.type = '%s' ORDER BY n.changed DESC", array($content_type), $from = 0, $count = 5); + while ($record = db_fetch_object($result)) { + $nodes[] = node_load($record->nid); } + return $nodes; +} - $output .= theme('pager', NULL, $content_slider_item, 0); - $output .= '
'; - +/** + * You can easily override this function at the theme level + */ +function theme_content_slider($nodes = array(), $delta = 0) { + $output = "
\n"; + foreach ($nodes as $key => $node) { + $output .= "
\n"; + $output .= " nid) ."'>". node_view($node, $teaser = TRUE, $page = FALSE, $links = FALSE) ."\n"; + $output .= "
\n"; + } + $output .= "
\n"; + $output .= "\n"; return $output; } +/** + * Implementation of hook_theme(). + * + */ +function content_slider_theme() { + return array( + 'content_slider' => array( + 'arguments' => array('nodes' => NULL, 'delta' => NULL), + ), + ); +} \ No newline at end of file