You are here

node_gallery.module in Node Gallery 6

Same filename and directory in other branches
  1. 6.3 node_gallery.module
  2. 6.2 node_gallery.module
  3. 7 node_gallery.module

Node gallery module file.

File

node_gallery.module
View source
<?php

/**
 * @file
 * Node gallery module file. 
 */
define("NODE_GALLERY_PERM_ADMIN_GALLERY", 'administer node gallery');
define("NODE_GALLERY_PERM_VIEW_GALLERY", 'view node gallery');
if (module_exists('token')) {
  module_load_include('inc', 'node_gallery', 'node_gallery.token');
}
module_load_include('inc', 'node_gallery', 'node_gallery.model');

/**
 * Implementation of hook_init().
 * Add our CSS file.
 */
function node_gallery_init() {
  drupal_add_css(drupal_get_path('module', 'node_gallery') . "/node_gallery.css");
}

/**
 * Implementation of hook_perm().
 */
function node_gallery_perm() {
  return array(
    NODE_GALLERY_PERM_ADMIN_GALLERY,
    NODE_GALLERY_PERM_VIEW_GALLERY,
  );
}

/**
 * Implementation of hook_menu()
 */
function node_gallery_menu() {
  $items = array();
  $items['admin/build/node_gallery'] = array(
    'title' => 'Node Gallery',
    'description' => t('Create and manage your Node Gallery relationships.'),
    'page callback' => 'node_gallery_config_list',
    'access arguments' => array(
      NODE_GALLERY_PERM_ADMIN_GALLERY,
    ),
    'file' => 'node_gallery.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/build/node_gallery/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/build/node_gallery/add'] = array(
    'title' => 'Add a Gallery Relationship',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_gallery_config_form',
    ),
    'access arguments' => array(
      NODE_GALLERY_PERM_ADMIN_GALLERY,
    ),
    'file' => 'node_gallery.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/build/node_gallery/edit/%node_gallery_config'] = array(
    'title' => 'Node Gallery Config Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_gallery_config_form',
      4,
    ),
    'access arguments' => array(
      NODE_GALLERY_PERM_ADMIN_GALLERY,
    ),
    'file' => 'node_gallery.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/node_gallery/delete/%node_gallery_config'] = array(
    'title' => 'Node Gallery Config Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_gallery_config_delete_form',
      4,
    ),
    'access arguments' => array(
      NODE_GALLERY_PERM_ADMIN_GALLERY,
    ),
    'file' => 'node_gallery.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['galleries'] = array(
    'title' => 'Galleries List',
    'page callback' => 'node_gallery_list',
    'access arguments' => array(
      NODE_GALLERY_PERM_VIEW_GALLERY,
    ),
    'file' => 'node_gallery.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['galleries/%user'] = array(
    'title' => 'My Galleries',
    'title callback' => 'galleries_title',
    'title arguments' => array(
      1,
    ),
    'page callback' => 'node_gallery_list',
    'page arguments' => array(
      1,
    ),
    'access arguments' => array(
      NODE_GALLERY_PERM_VIEW_GALLERY,
    ),
    'file' => 'node_gallery.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['node/%node_gallery_gallery/upload'] = array(
    'title' => 'Upload Images',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_gallery_upload_form',
      1,
    ),
    'access callback' => 'node_gallery_user_access',
    'access arguments' => array(
      'upload',
      1,
    ),
    'file' => 'node_gallery.pages.inc',
  );
  $items['node/%node_gallery_gallery/edit/images'] = array(
    'title' => 'Edit Images',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_gallery_edit_images_form',
      1,
    ),
    'access callback' => 'node_gallery_user_access',
    'access arguments' => array(
      'edit image',
      1,
    ),
    'file' => 'node_gallery.pages.inc',
  );
  $items['node_gallery/upload/js'] = array(
    'title' => 'Gallery Image Upload',
    'page callback' => 'node_gallery_upload_js',
    'access callback' => TRUE,
    'file' => 'node_gallery.pages.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_menu_alter()
 */
function node_gallery_menu_alter(&$items) {

  // Disable all Node Gallery image types from showing up on node/add
  $types = gallery_config_gateway::get_types();

  //Only do the foreach if there is a type
  if ($types) {
    foreach ($types as $type) {
      $items['node/add/' . $type->image_type]['access callback'] = FALSE;
      $items['node/add/' . str_replace('_', '-', $type->image_type)]['access callback'] = FALSE;
    }
  }
}

//node_gallery_menu_alter()

/**
 * Implementation of hook_theme()
 */
function node_gallery_theme() {
  return array(
    'image_view' => array(
      'arguments' => array(
        'imagecache' => NULL,
        'image' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_cover_view' => array(
      'template' => 'gallery_cover_view',
      'arguments' => array(
        'gallery' => NULL,
        'config' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_list' => array(
      'arguments' => array(
        'items' => NULL,
        'account' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_edit_images_form' => array(
      'file' => 'node_gallery.themes.inc',
      'arguments' => array(
        'form' => NULL,
      ),
    ),
    'gallery_images_list' => array(
      'arguments' => array(
        'gallery' => NULL,
        'config' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_image_navigator' => array(
      'arguments' => array(
        'navigator' => NULL,
        'image' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_image_thumbnail' => array(
      'arguments' => array(
        'image' => NULL,
        'config' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_image_lightbox2' => array(
      'arguments' => array(
        'image' => NULL,
        'config' => NULL,
        'gallery' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_teaser' => array(
      'arguments' => array(
        'gallery' => NULL,
        'config' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'gallery_meta' => array(
      'arguments' => array(
        'gallery' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'node_gallery_image' => array(
      'arguments' => array(
        'config' => NULL,
        'node' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
    'node_gallery_teaser' => array(
      'arguments' => array(
        'config' => NULL,
        'node' => NULL,
      ),
      'file' => 'node_gallery.themes.inc',
    ),
  );
}

/**
 * Implementation of hook_nodeapi().
 */
function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  $function = '_node_gallery_' . $op;
  if (function_exists($function)) {
    call_user_func($function, $node, $a3, $a4);
  }
}

/**
 * Implementation of hook_nodeapi(op='load').
 */
function _node_gallery_load(&$node) {
  if (gallery_config_gateway::get_types('gallery', $node->type)) {
    $node->images = gallery_gateway::get_images($node->nid);
    $node->image_count = count($node->images);
  }
  if (gallery_config_gateway::get_types('image', $node->type)) {
    $image = ImageGateway::get_file($node->nid);
    if (!empty($image)) {
      foreach (get_object_vars($image) as $k => $v) {
        $node->{$k} = $v;
      }
    }
  }
}

/**
 * Implementation of hook_nodeapi(op='view').
 */
function _node_gallery_view(&$node, $teaser, $page) {
  if (gallery_config_gateway::get_types('image', $node->type)) {
    $config = gallery_config_gateway::get_by($node->gid, 'id');
    $gallery = new Gallery(array(
      'nid' => $node->gid,
    ));
    if ($config->content_display == 'gallery') {
      $node->content['body']['#value'] = $gallery
        ->get_content($teaser);
      $tmp = $node->content['body'];
      unset($node->content);
      $node->content['body'] = $tmp;
    }
  }
}

/**
 * Implementation of hook_nodeapi(op='alter').
 */
function _node_gallery_alter(&$node) {
  if (gallery_config_gateway::get_types('image', $node->type)) {
    $config = gallery_config_gateway::get_by($node->gid, 'id');
    if ($config->content_display == 'gallery') {

      //we'll display gallery's comments, not image's;
      $node->old_comment = $node->comment;
      $node->comment = 0;
    }
  }
}

/**
 * Implementation of hook_nodeapi(op='delete').
 */
function _node_gallery_delete(&$node) {
  global $user;
  if (gallery_config_gateway::get_types('gallery', $node->type)) {
    gallery_gateway::delete($node->nid);
    if (!empty($node->images)) {
      foreach ($node->images as $image) {
        $operations[] = array(
          'images_delete_process',
          array(
            $image,
          ),
        );
      }
      $batch = array(
        'operations' => $operations,
        'finished' => 'images_process_finished',
        'title' => t('Processing Gallery Delete.'),
        'init_message' => t('Gallery Delete is starting.'),
        'progress_message' => t('Processed @current out of @total.'),
        'error_message' => t('Gallery Delete has encountered an error.'),
      );
      batch_set($batch);
      batch_process('galleries/' . $user->uid);
    }
  }
  if (gallery_config_gateway::get_types('image', $node->type)) {
    ImageGateway::delete($node);
  }
}
function node_gallery_config_load($type) {
  return gallery_config_gateway::get_by($type);
}
function node_gallery_gallery_load($nid) {
  static $galleries;
  if (empty($galleries[$nid])) {
    if (gallery_config_gateway::get_by($nid, 'id')) {
      $galleries[$nid] = new Gallery(node_load($nid));
    }
  }
  return $galleries[$nid];
}
function galleries_title($user) {
  return t('!user\'s Galleries', array(
    '!user' => $user->name,
  ));
}
function node_gallery_user_access($op, $gallery = NULL) {
  global $user;
  $gallery_config = gallery_config_gateway::get_by($gallery->type);
  switch ($op) {
    case 'view':
      return user_access(NODE_GALLERY_PERM_VIEW_GALLERY);
      break;
    case 'upload':
      if ($user->uid == $gallery->uid || $user->uid == 1) {
        return user_access('create ' . $gallery_config->image_type . ' content');
      }
      else {
        return user_access('edit any ' . $gallery->type . ' content');
      }
      break;
    case 'edit':
      if ($user->uid == $gallery->uid) {
        return user_access('edit own ' . $gallery->type . ' content') || user_access('edit any ' . $gallery->type . ' content');
      }
      else {
        return user_access('edit any ' . $gallery->type . ' content');
      }
      break;
    case 'edit image':
      if ($user->uid == $gallery->uid) {
        return user_access('edit own ' . $gallery_config->image_type . ' content') || user_access('edit any ' . $gallery_config->image_type . ' content');
      }
      else {
        return user_access('edit any ' . $gallery_config->image_type . ' content');
      }
      break;
    case 'delete':
      if ($user->uid == $gallery->uid) {
        return user_access('delete own ' . $gallery_config->image_type . ' content') || user_access('delete any ' . $gallery_config->image_type . ' content');
      }
      else {
        return user_access('delete any ' . $gallery_config->image_type . ' content');
      }
      break;
    case 'create':
      return user_access('create ' . $gallery->type . ' content');
      break;
  }
}
function node_gallery_image_user_access($op, $image = NULL) {
  global $user;
  switch ($op) {
    case 'edit':
      if ($user->uid == $image->uid) {
        return user_access("edit own {$image->type} content") || user_access("edit any {$image->type} content");
      }
      if ($user->uid != $image->uid) {
        return user_access("edit any {$image->type} content");
      }
      break;
  }
}
function node_gallery_operations($type, $a2 = NULL) {
  global $user;
  $items = array();
  switch ($type) {
    case 'list':
      $account = $a2;

      //all gallery list or current user's gallery list;
      if ($account->uid == $user->uid) {
        $node_types = node_get_types();
        $types = gallery_config_gateway::get_types();
        foreach (array_keys($types) as $type) {
          if (user_access("create {$type} content", $user)) {
            $items[] = array(
              'title' => t('Create !type', array(
                '!type' => $node_types[$type]->name,
              )),
              'href' => 'node/add/' . $type,
            );
          }
        }
      }
      break;
    case 'cover':
      $gallery = $a2;
      if (node_gallery_user_access('edit', $gallery)) {
        $items[] = array(
          'title' => t('Edit gallery'),
          'href' => "node/{$gallery->nid}/edit",
          'query' => array(
            'destination' => "galleries/{$gallery->uid}",
          ),
        );
      }
      if (node_gallery_user_access('upload', $gallery)) {
        $items[] = array(
          'title' => t('Upload images'),
          'href' => "node/{$gallery->nid}/upload",
        );
      }
      if (node_gallery_user_access('delete', $gallery)) {
        $items[] = array(
          'title' => t('Delete gallery'),
          'href' => "node/{$gallery->nid}/delete",
          'query' => array(
            'destination' => "galleries/{$gallery->uid}",
          ),
        );
      }
      break;
    case 'gallery':
      $gallery = $a2;
      if (node_gallery_user_access('edit', $gallery)) {
        $items[] = array(
          'title' => t('Edit gallery'),
          'href' => "node/{$gallery->nid}/edit",
          'query' => array(
            'destination' => "node/{$gallery->nid}",
          ),
        );
      }
      if (node_gallery_user_access('upload', $gallery)) {
        $items[] = array(
          'title' => t('Upload images'),
          'href' => "node/{$gallery->nid}/upload",
          'query' => array(
            'destination' => "node/{$gallery->nid}",
          ),
        );
      }
      if (node_gallery_user_access('edit image', $gallery) && $gallery->image_count) {
        $items[] = array(
          'title' => t('Edit images'),
          'href' => "node/{$gallery->nid}/edit/images",
          'query' => array(
            'destination' => "node/{$gallery->nid}",
          ),
        );
      }
      if (node_gallery_user_access('delete', $gallery)) {
        $items[] = array(
          'title' => t('Delete gallery'),
          'href' => "node/{$gallery->nid}/delete",
          'query' => array(
            'destination' => "galleries/{$gallery->uid}",
          ),
        );
      }
      $items[] = array(
        'title' => t('Back to !name\'s gallery list', array(
          '!name' => $gallery->name,
        )),
        'href' => 'galleries/' . $gallery->uid,
      );
      break;
    case 'image':
      $image = $a2;
      if (node_gallery_image_user_access('edit', $image)) {
        $items[] = array(
          'title' => t('Edit image'),
          'href' => 'node/' . $image->nid . "/edit",
        );
      }
      $items[] = array(
        'title' => t('Back to gallery'),
        'href' => 'node/' . $image->gid,
      );
      break;
  }
  return theme('links', $items, array(
    'class' => 'gallery-operations',
  ));
}

/**
 * We can't put this function to node_gallery.pages.inc
 * because the batch was started after reboot.
 *
 * @param unknown_type $file
 * @param unknown_type $fid
 * @param unknown_type $num
 * @param unknown_type $context
 */
function images_upload_process($form, $image, &$context) {
  $image
    ->save($form);
  $context['results'][] = $image_form_state['values']['title'];
  $context['message'] = t('Now uploading %node', array(
    '%node' => $image_form_state['values']['title'],
  ));
}
function images_delete_process($image, &$context) {
  $image
    ->delete();
  $context['results'][] = $image->title;
  $context['message'] = t('Now deleting %node', array(
    '%node' => $image->title,
  ));
}
function images_process_finished($success, $results, $operations) {
  if ($success) {

    // Here we do something meaningful with the results.

    //$message .= theme('item_list', $results);
  }
  else {

    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  }
  drupal_set_message($message);
}

/**
 * Implementation of hook_link_alter().
 */
function node_gallery_link_alter(&$links, $node) {
  if (gallery_config_gateway::get_types('image', $node->type)) {
    $conf = gallery_config_gateway::get_by($node->gid, 'id');
    if ($conf->content_display == 'gallery') {
      foreach ($links as $k => $link) {
        if (strpos($k, 'comment') !== FALSE && is_array($link)) {
          foreach ($link as $k2 => $v) {
            if ($k2 == 'href') {
              $links[$k][$k2] = preg_replace('/(\\w+)\\/(\\d+)/i', "\$1/" . $node->gid, $v);
            }
            if ($k2 == 'query') {
              $links[$k][$k2] = is_array($v) ? array_merge($v, array(
                'destination' => 'node/' . $node->nid,
              )) : $v . "&destinatioin={$node->nid}";
            }
          }
        }
      }
    }
  }
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function node_gallery_theme_registry_alter(&$theme_registry) {
  $theme_registry['node']['theme paths'][] = drupal_get_path('module', 'node_gallery');
}
function node_gallery_preprocess_node(&$vars) {
  $node = $vars['node'];
  $page = $vars['page'];
  if (gallery_config_gateway::get_types('gallery', $node->type)) {
    $config = gallery_config_gateway::get_by($node->type);
    if ($page) {
      $vars['gallery_operations'] = node_gallery_operations('gallery', $node);
      $vars['gallery'] = theme('gallery_images_list', $node, $config);
    }
    else {
      $vars['gallery'] = theme('gallery_teaser', $node, $config);
    }
    array_pop($vars['template_files']);
    $vars['template_files'][] = "node-gallery-default";
    $vars['template_files'][] = "node-" . $node->type;
  }
  elseif (gallery_config_gateway::get_types('image', $node->type)) {
    $config = gallery_config_gateway::get_by($node->gid, 'id');
    $gallery = new Gallery(array(
      'nid' => $node->gid,
    ));
    if ($page) {
      $vars['image_navigator'] = theme('gallery_image_navigator', $gallery
        ->get_image_navigator($node->nid), $node);
      $vars['image'] = theme('node_gallery_image', $config, $node);
      if ($config->content_display == 'gallery' && function_exists('comment_render')) {
        $gallery_node = node_load($node->gid);
        if ($gallery_node->comment) {
          $vars['comments'] = comment_render($gallery_node);
        }
      }
      elseif ($config->content_display == 'image' && function_exists('comment_render') && $node->old_comment) {
        $vars['comments'] = comment_render($node);
      }
    }
    else {
      $vars['image'] = theme('node_gallery_image', $config, $node);
    }
    array_pop($vars['template_files']);
    $vars['template_files'][] = "node-image-default";
    $vars['template_files'][] = "node-" . $node->type;
  }
}

Functions

Constants

Namesort descending Description
NODE_GALLERY_PERM_ADMIN_GALLERY @file Node gallery module file.
NODE_GALLERY_PERM_VIEW_GALLERY