'fieldset', '#title' => t('Gallery settings') ); $form['gallery']['image_images_per_page'] = array( '#type' => 'textfield', '#title' => t('Images per page'), '#default_value' => variable_get('image_images_per_page', 6), '#size' => 3, '#maxlength' => 2, '#description' => t('Sets the number of images to be displayed in a gallery page.') ); $form['block'] = array( '#type' => 'fieldset', '#title' => t('Block settings') ); $form['block']['shazamgallery_amount_thumbs'] = array( '#type' => 'textfield', '#title' => t('Number of thumbnails in the blocks'), '#default_value' => variable_get('shazamgallery_amount_thumbs', 5), '#size' => 3, '#maxlength' => 2, '#description' => t('The maximum number of thumbnails to show in a block.') ); return $form; } /** * Implementation of hook_menu */ function shazamgallery_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'gallery/view', 'title' => t('galleries'), 'access' => user_access('access content'), 'type' => MENU_SUGGESTED_ITEM, 'callback' => 'shazamgallery_fetch'); } return $items; } /** * Implementation of hook_node_info */ function shazamgallery_node_info() { return array('gallery' => array('name' => t('gallery'), 'base' => 'shazamgallery')); } /** * Implementation of hook_form */ function shazamgallery_form(&$node, &$param) { $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title ); if (function_exists('taxonomy_node_form')) { $form['gallery'] = implode('', taxonomy_node_form('gallery', $node)); } $form['body'] = array( '#type' => 'textarea', '#title' => t('Description'), '#default_value' => $node->body, '#rows' => 20, '#required' => FALSE ); $form['format'] = filter_form($node->format); $form['log'] = array( '#type' => 'textarea', '#title' => t('Log message'), '#default_value' => $node->log, '#weight' => 5, '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.') ); return $form; } /** * Implementation of hook_insert */ function shazamgallery_insert($node) { db_query("INSERT INTO {shazamgallery} (nid) VALUES (%d)", $node->nid); } /** * Implementation of hook_update */ function shazamgallery_update($node) { db_query("UPDATE {relations} SET weight = %d WHERE left_id = %d", $node->weight, $node->nid); } /** * Implementation of hook_delete */ function shazamgallery_delete($node) { shazamgallery_delete_relation($node->nid, NULL); } /** * Implementation of hook_load */ function shazamgallery_load($node) { $gallery = db_fetch_object(db_query('SELECT * FROM {shazamgallery} WHERE nid = %d', $node->nid)); $gallery->images = shazamgallery_get_children($node->nid); $gallery->thumbnail = _shazamgallery_get_thumbnail_node($node->nid); return $gallery; } /** * Implementation of hook_view */ function shazamgallery_view(&$node, $teaser = FALSE, $page = FALSE) { if($teaser) { $node->teaser = theme('shazamgallery_gallery_node_teaser', $node); } elseif ($page) { _shazamgallery_set_gallery_breadcrumb($node); $node->body = theme('shazamgallery_gallery_node_body', $node); } } /** * Implementation of hook_form_alter(). */ function shazamgallery_form_alter($form_id, &$form) { if (isset($form['type'])) { // Extend only image node form if ('image_node_form' == $form_id) { $node = $form['#node']; //Todo: make the default per-parent-gallery $parents = shazamgallery_get_parents($node->nid); $options = array('<'.t('none').'>'); foreach (shazamgallery_get_galleries() as $gallery) { $options[$gallery->nid] = $gallery->title; } $option_count = count($options); if ($option_count > 1) { $form['gallery'] = array( '#type' => 'fieldset', '#title' => t('Gallery settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => -2 ); $form['gallery']['shazam_parents'] = array( '#type' => 'select', '#title' => t('Gallery'), '#default_value' => $parents, '#options' => $options, '#multiple' => TRUE, '#size' => $option_count > 5 ? 6 : $option_count, '#description' => t('Select a gallery to add this image to.') ); } } } } function _shazamgallery_image_insert($node) { //save the parents if ($node->shazam_parents) { foreach ($node->shazam_parents as $parent) { shazamgallery_save_relation($parent, $node->nid); } } } function _shazamgallery_image_update($node) { //save the parents if ($node->shazam_parents) { shazamgallery_delete_relation(NULL, $node->nid); foreach ($node->shazam_parents as $parent) { shazamgallery_save_relation($parent, $node->nid); } } } function _shazamgallery_image_delete($node) { $parents = shazamgallery_get_parents($node->nid); shazamgallery_delete_relation(NULL,$node->nid); } /** * Implementation of hook_link. */ function shazamgallery_link($type, $node, $main = 0) { $links = array(); $types = variable_get('shazamgallery_nodes', array('image')); if ($type == "page" && user_access('access content')) { $links[] = l(t("image galleries"), "gallery"); } return $links; } /** * Implementation of hook_block. * * Offers 2 blocks: latest image and random image */ function shazamgallery_block($op, $delta = 0) { switch ($op) { case 'list': $block[0]['info'] = t('Latest images'); $block[2]['info'] = t('Latest galleries'); return $block; case 'view': if (user_access('access content')) { switch($delta) { case 0: $block['subject'] = t('Latest images'); $block['content'] = shazamgallery_block_image_list(); break; case 2: $block['subject'] = t('Latest galleries'); $block['content'] = shazamgallery_block_gallery_list(); break; } } return $block; } } /** * Fetches an image file, allows "shorthand" image urls such of the form: * image/view/$nid/$label * Differs from image_fetch in that it adds permission checks * (e.g. image/view/25/thumbnail or image/view/14) */ function shazamgallery_fetch($nid = 0, $size = 'preview') { if ($nid && user_access("view $size image")) { $node = node_load(array('nid' => $nid)); if ($node->images[$size]) { $file = $node->images[$size]; $headers = image_file_download($file); file_transfer($file, $headers); } } } /** * Theme a gallery page/node * @param $node, the gallery node. */ function theme_shazamgallery_gallery_node_body($node) { theme_add_style(drupal_get_path('module', 'shazamgallery').'/shazamgallery.css'); $content = $node->body; if (count($node->images)) { foreach ($node->images as $image_id) { $items[] = theme('shazamgallery_image', node_load($image_id)); } $content .= '
'; //waiting for theme_wrapper to get into core $content .= theme('item_list',$items); $content .= "
\n"; $content .= '
'; $content .= theme('pager', NULL, variable_get('image_images_per_page', 6), 1); } return $content; } /** * Theme a gallery teaser view * @param $node, the gallery node. */ function theme_shazamgallery_gallery_node_teaser($node) { // add the default image $title = t('view the gallery "%name"', array('%name'=>$node->thumbnail->title)); $thumb = image_display($node->thumbnail, 'thumbnail'); $output = l($thumb, 'node/'.$node->nid, array('title'=>$title), NULL, NULL, FALSE, TRUE); if ($node->teaser) { $output .= '

'.$node->teaser.'

'; } $image_count = count($node->images); $output .= '

'.format_plural($image_count, 'There is 1 image in this gallery.', 'There are %count images in this gallery.').'

'; return $output; } /** * Theme the "galleries" view */ function theme_views_view_galleries($view, $type, $nodes) { if ($view->header) { $header = check_markup($view->header, $view->header_format, FALSE); $output .= '
'.$header."
\n"; } switch ($type) { case 'page': // Teaser View drupal_set_title(views_get_title($view)); foreach ($nodes as $node) { //We are going to view a normal node. //If you want to theme the body of the teasers then look at // theme_shazamgallery_gallery_node_teaser //If you want to theme the full node, then refer to the phptemplate (or other theme engine) documentation. $output .= node_view(node_load($node->nid), TRUE, FALSE, FALSE); } break; case 'block': // List View $output .= views_view_list($view, $nodes); $output .= theme('views_more', $view->name); break; } return '
'.$output."
\n"; } /** * Theme a gallery teaser image entry. */ function theme_shazamgallery_image($node) { $thumb = image_display($node, 'thumbnail'); $output = l($thumb, 'node/'.$node->nid, array('title'=>$node->title), NULL, NULL, FALSE, TRUE); $output .= '

'. $node->teaser .'

'; return $output; } /** * Get all available galleries * @param moderate nodes in moderation queue. 1 is in queue, 0 is out of queue, unspecified is all. */ function shazamgallery_get_galleries($moderate = 'all') { if ($moderate == 0 || $moderate == 1) { $res = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type = 'gallery' AND n.status = 1 AND n.moderate = %d ORDER BY n.created DESC"), $moderate); } else { $res = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type = 'gallery' AND n.status = 1 ORDER BY n.created DESC")); } while ($row = db_fetch_object($res)) { $galleries[] = node_load($row->nid); } return $galleries; } /** * Set the breadcrumb for a gallery node */ function _shazamgallery_set_gallery_breadcrumb($node) { $parents = shazamgallery_get_parents($node->nid); foreach ($parents as $parent) { $breadcrumb[] = array('path' => 'gallery/'.$parent->tid, 'title' => $parent->name); } $breadcrumb[] = array('path' => 'gallery', 'title' => t('Image galleries')); $breadcrumb = array_reverse($breadcrumb); drupal_set_title($gallery['gallery']->name); $breadcrumb[] = array('path' => $_GET['q']); menu_set_location($breadcrumb); } /** * Fetch the latest N galleries. */ function shazamgallery_block_gallery_list() { $galleries = shazamgallery_get_galleries(0); $output = ''; $max = variable_get('shazamgallery_amount_thumbs',5); $count = 0; foreach ($galleries as $gallery) { if ($gallery->thumbnail && $count++ < $max) { $output .= l(image_display($gallery->thumbnail, 'thumbnail'), 'node/'.$gallery->nid, array(), NULL, NULL, FALSE, TRUE); } } return $output; } /** * Fetch the latest N image(s). */ function shazamgallery_block_image_list() { $images = image_get_latest(variable_get('shazamgallery_amount_thumbs',5), 0); $output = ''; foreach ($images as $image) { $output .= l(image_display($image, 'thumbnail'), 'node/'.$image->nid, array(), NULL, NULL, FALSE, TRUE); } return $output; } /** * returns the node object of the gallery thumbnail which is any sticky image or the latest image. Give the nid of the gallery. */ function _shazamgallery_get_thumbnail_node($nid) { $res = db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {relations} r ON n.nid = r.right_id WHERE r.left_id = %d AND relationship='gallery-image' ORDER BY n.sticky DESC, n.changed DESC LIMIT 0, 1", $nid)); if($res && $node = node_load($res)) { return $node; } } /** * Relation APIs */ function shazamgallery_save_relation($left, $right, $weight = 0) { db_query("INSERT INTO {relations} (left_id, right_id, relationship, weight) VALUES (%d, %d, 'gallery-image', %d)", $left, $right, $weight); } function shazamgallery_delete_relation($left, $right) { if ($left && !$right) { db_query("DELETE FROM {relations} WHERE left_id = %d AND relationship = 'gallery-image'", $left); } elseif ($right && !$left) { db_query("DELETE FROM {relations} WHERE right_id = %d AND relationship = 'gallery-image'", $right); } elseif ($right && $left) { db_query("DELETE FROM {relations} WHERE right_id = %d OR left_id = %d AND relationship = 'gallery-image'", $right, $left); } } function shazamgallery_get_parents($nid) { $output = array(); //right to left child-parent relations $res = db_query("SELECT left_id FROM {relations} WHERE right_id = %d AND relationship = 'gallery-image'", $nid); while ($row = db_fetch_object($res)) { $output[] = $row->left_id; } return $output; } function shazamgallery_get_children($nid) { $output = array(); //left to right child-parent relations $res = pager_query("SELECT right_id FROM {relations} WHERE left_id = %d AND relationship = 'gallery-image'", variable_get('image_images_per_page', 6), 1, NULL, $nid); while ($row = db_fetch_object($res)) { $output[] = $row->right_id; } return $output; } /** * @ingroup views * Function defines the various views * Implementation of hook_views_default_views */ function shazamgallery_views_default_views() { //Gallery page. lists all the available galleries $view = new stdClass(); $view->name = t('galleries'); $view->description = t('galleries'); $view->page = TRUE; $view->url = 'gallery'; $view->page_title = 'galleries'; $view->page_type = 'teaser'; $view->use_pager = TRUE; $view->nodes_per_page = variable_get('default_nodes_main', 10); $view->menu = TRUE; $view->menu_title = t('galleries'); $view->block = FALSE; $view->sort = array ( array ( 'tablename' => 'node', 'field' => 'sticky', 'sortorder' => 'DESC', 'options' => '', ), array ( 'tablename' => 'node', 'field' => 'created', 'sortorder' => 'DESC', 'options' => '', ), ); $view->argument = array ( ); $view->field = array ( ); $view->filter = array ( array ( 'tablename' => 'node', 'field' => 'type', 'operator' => '=', 'options' => '', 'value' => 'gallery', ), array ( 'tablename' => 'node', 'field' => 'status', 'operator' => '=', 'options' => '', 'value' => '1', ), ); $views[$view->name] = $view; //Latest Image. Single node. $view = new stdClass(); $view->name = t('latest image'); $view->description = t('The last image added to the site.'); $view->page = TRUE; $view->url = 'gallery/node/latest'; $view->page_title = t('latest image in %1'); $view->page_type = 'node'; $view->use_pager = FALSE; $view->nodes_per_page = 1; $view->block = FALSE; $view->menu = FALSE; $view->breadcrumb_no_home = FALSE; $view->sort = array ( array ( 'tablename' => 'node', 'field' => 'sticky', 'sortorder' => 'DESC', 'options' => '', ), array ( 'tablename' => 'node', 'field' => 'created', 'sortorder' => 'DESC', 'options' => '', ), ); $view->argument = array ( array ( 'type' => 'nid', 'argdefault' => '3', 'title' => t('latest image'), ), ); $view->field = array ( ); $view->filter = array ( array ( 'tablename' => 'node', 'field' => 'promote', 'operator' => '=', 'options' => '', 'value' => '1', ), array ( 'tablename' => 'node', 'field' => 'status', 'operator' => '=', 'options' => '', 'value' => '1', ), ); $views[$view->name] = $view; //gallery per user $view = new stdClass(); $view->name = t('user gallery'); $view->description = t('A gallery per user.'); $view->page = TRUE; $view->url = 'gallery/user'; $view->page_title = 'gallery for %1'; $view->page_type = 'teaser'; $view->use_pager = TRUE; $view->nodes_per_page = variable_get('default_nodes_main', 10); $view->menu = FALSE; $view->menu_title = 'user'; $view->block = FALSE; $view->sort = array ( array ( 'tablename' => 'node', 'field' => 'sticky', 'sortorder' => 'DESC', 'options' => '', ), array ( 'tablename' => 'node', 'field' => 'created', 'sortorder' => 'DESC', 'options' => '', ), ); $view->argument = array ( array ( 'type' => 'uid', 'argdefault' => '2', 'title' => t('gallery for all users'), ), ); $view->field = array ( ); $view->filter = array ( array ( 'tablename' => 'node', 'field' => 'type', 'operator' => '=', 'options' => '', 'value' => 'image', ), array ( 'tablename' => 'node', 'field' => 'status', 'operator' => '=', 'options' => '', 'value' => '1', ), ); $views[$view->name] = $view; //Dated galleries $view = new stdClass(); $view->name = t('gallery by date'); $view->description = t('browse galleries by dates'); $view->page = TRUE; $view->page_title = t('gallery for %2 %1'); $view->page_header_format = '1'; $view->page_type = 'teaser'; $view->url = 'gallery/dates'; $view->use_pager = TRUE; $view->nodes_per_page = '10'; $view->menu = TRUE; $view->menu_title = t('gallery by date'); $view->menu_tab = FALSE; $view->menu_tab_default = FALSE; $view->menu_weight = ''; $view->sort = array ( array ( 'tablename' => 'node', 'field' => 'sticky', 'sortorder' => 'DESC', 'options' => '', ), array ( 'tablename' => 'node', 'field' => 'created', 'sortorder' => 'DESC', 'options' => '', ), ); $view->argument = array ( array ( 'type' => 'year', 'argdefault' => '3', 'title' => t('year'), 'options' => '', ), array ( 'type' => 'month', 'argdefault' => '3', 'title' => t('month'), 'options' => '', ), ); $view->field = array ( ); $view->filter = array ( array ( 'tablename' => 'node', 'field' => 'status', 'operator' => '=', 'options' => '', 'value' => '1', ), array ( 'tablename' => 'node', 'field' => 'type', 'operator' => '=', 'options' => '', 'value' => 'image', ), ); $view->requires = array(node, users); $views[$view->name] = $view; return $views; } ?>