All pastes #331972 Raw Edit

Stuff

public text v1 · immutable
#331972 ·published 2007-01-29 17:25 UTC
rendered paste body
<?php
function ns_gallery_menu($may_cache){
  $items = array(); 
  $items[] = array(                              
    'path' => variable_get('ns_gallery_path', 'ns_gallery'), 
    'title' => t('Gallery'),
    'callback' => 'ns_gallery_display', 
    'access' => TRUE,
    'type' => MENU_CALLBACK,   
  );
  $items[] = array(
    'path' => 'admin/settings/ns_gallery',
    'title' => t('ns_gallery'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array('ns_gallery_admin_settings'),
    'access' => user_access('administer site configuration'),
    'type' => MENU_NORMAL_ITEM
  ); 
  return $items;
}

/**
* @desc ns_gallery settings
*/
function ns_gallery_admin_settings(){
  $form['ns_gallery_path'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('Path'),
    '#default_value' => variable_get('ns_gallery_path', 'ns_gallery'),
    '#description' => t('Path to which the gallery is display on.'),
  );
  $form['ns_gallery_vocab'] = array(
    '#type' => 'select',
    '#options' => ns_gallery_get_galleries(),
    '#title' => t('Vocabulary association'),
    '#default_value' => variable_get('ns_gallery_vocab', 0),
    '#description' => t('Select the vocabulary that is currently applied to image that will display the albums.'),
  );
  $form['ns_gallery_default_album'] = array(
    '#type' => 'select',
    '#options' => ns_gallery_get_albums(),
    '#title' => t('Default Album'), 
    '#default_value' => variable_get('ns_gallery_default_album', 0),
    '#description' => t('The default album that will be displayed upon gallery entry. (only needed when associated to a vocabulary)'),
  );  
  $form['ns_gallery_captions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show image captions'),
    '#default_value' => variable_get('ns_gallery_captions', 0),
    '#description' => t('When true the title of the image uploaded will be used as the image caption.'),
  );  
  return system_settings_form($form); 
}

/**
* @desc get all vocabulary associated with image 
*/
function ns_gallery_get_galleries($vid = NULL){
  if (!is_null($vid) && is_numeric($vid)){
    $select = db_query("SELECT name, tid, weight FROM term_data WHERE vid='{$vid}'");
    while ($vocab = db_fetch_array($select)){
      $pass[$vocab['tid']] = $vocab['name'];
    }
    return $pass;  
  }else{
    $select = db_query("SELECT vnt.vid, vnt.type, v.vid, v.name FROM vocabulary_node_types vnt 
      LEFT JOIN vocabulary v ON v.vid WHERE v.vid = vnt.vid AND type='image'");
    // array formatted for the select options
    $pass[0] = '<none>';
    while($vocab = db_fetch_array($select)){
      $pass[$vocab['vid']] = $vocab['name'];     
    }
    return $pass;
  }
}

/**
* @desc select and return gallery name 
*/
function ns_gallery_get_gallery_name($vid, $default){
  $select = db_query("SELECT name FROM vocabulary WHERE vid='{$vid}'");
  $vocab = db_fetch_array($select); 
  if ($vocab){
    return $vocab['name'];  
  }else{
    return $default;
  }
}

/**
* @desc get all albums within a gallery
*/
function ns_gallery_get_albums($tname = NULL){
  $gallery = variable_get('ns_gallery_vocab', 0);
  if ($gallery > 0){
    $select = db_query("SELECT tid, name FROM term_data WHERE vid='{$gallery}'");
    while($result = db_fetch_array($select)){
      $pass[$result['tid']] = $result['name']; 
    }
    return $pass;
  }else{
    return array('no gallery associated');
  }
}

/**
* @desc display album links
*/
function ns_gallery_albums(){
  $vocabid = variable_get('ns_gallery_vocab', 0);             
  // no albums
  if ($vocabid == 0){
       
  // albums
  }else{
    $albums = ns_gallery_get_galleries($vocabid); 
     foreach($albums AS $tid => $name){
      // apply class to active or default album
      $add = ((arg(1) == $tid) OR (!arg(1)) AND ($tid == variable_get('ns_gallery_default_album', 0)))? 'class="active"' : '' ;
      $album_links .= '<a href="'.$tid.'" '.$add.'>'.$name.'</a>';  
     }
    $display .= '<div id="ns_gallery_albums">';
    $display .= $album_links;
    $display .= '</div>';    
    return $display;
  } 
}

/**
* @desc display the thickbox gallery
*/
function ns_gallery_images(){
  // include needed files
  drupal_add_js(drupal_get_path('module', 'ns_gallery').'/thickbox/thickbox.js');
  drupal_add_css(drupal_get_path('module', 'ns_gallery').'/thickbox/thickbox.css');
  drupal_add_css(drupal_get_path('module', 'ns_gallery').'/ns_gallery.css');
  // if gallery associated
  if (variable_get('ns_gallery_vocab', 0)){
    // if album is selected
    if (arg(1)){
      $tid = arg(1);
      $add = "AND tn.tid = '{$tid}'";
    }else{
      $tid = variable_get('ns_gallery_default_album', 0);
      $add = "AND tn.tid = '{$tid}'";
    }
  }
  $result = db_query("SELECT f.nid, f.filename, f.filepath, f.filemime, n.title, tn.tid
                      FROM node n LEFT JOIN files f ON f.nid = n.nid LEFT JOIN term_node tn ON tn.nid = n.nid  
                      WHERE f.filename = 'thumbnail' {$add}");   
  // output images                    
  $display .= '<div id="ns_gallery">';                    
  while ($img = db_fetch_array($result)){
    $preview = db_query("SELECT files.filepath FROM files WHERE filename = 'preview' AND nid='{$img['nid']}'");
    $preview_image = db_fetch_array($preview); 
    // if captions enabled then show
    $caption = variable_get('ns_gallery_captions', 0);
    $title = $caption ? $img['title'] : '' ;  
    // thumbnail output
    $display .= '<a href="http://visiontestbed.com/drupal5/files/'.$preview_image['filepath'].'" class="thickbox" title="'.$title.'"><img src="http://visiontestbed.com/drupal5/files/'.$img['filepath'].'" /></a>';
  }
  $display .= '</div>';
  
  return $display;
}

/**
* @desc display gallery
*/
function ns_gallery_display(){
  $display .= ns_gallery_albums(); 
  $display .= ns_gallery_images();  
  return $display;
}