You are here

function node_gallery_nodeapi in Node Gallery 6.3

Same name and namespace in other branches
  1. 6 node_gallery.module \node_gallery_nodeapi()
  2. 6.2 node_gallery.module \node_gallery_nodeapi()

Implements hook_nodeapi().

File

./node_gallery.module, line 554
Node gallery module file.

Code

function node_gallery_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      if (in_array($node->type, (array) node_gallery_get_types('gallery'))) {
        $node->cover_image = node_gallery_get_cover_nid($node);
      }
      if (in_array($node->type, (array) node_gallery_get_types('image'))) {
        node_gallery_load_image($node);
      }
      break;
    case 'insert':
      if (in_array($node->type, (array) node_gallery_get_types('image'))) {
        node_gallery_set_gallery_cover_image($node);
        node_gallery_set_image_weight($node);
        node_gallery_clear_gallery_caches($node->gid);
        if (module_exists('og')) {
          node_gallery_inherit_group_settings($node);
          og_nodeapi($node, $op, $a3, $a4);
        }
        drupal_write_record('node_gallery_images', $node);
        node_gallery_update_image_counts($node);
        if ($node->pluploaded == TRUE && variable_get('node_gallery_plupload_manage_images_integration', TRUE)) {
          if (!isset($_SESSION['node_gallery_plupload_nids'][$node->gid])) {
            $_SESSION['node_gallery_plupload_nids'][$node->gid] = array();
          }
          array_unshift($_SESSION['node_gallery_plupload_nids'][$node->gid], $node->nid);
        }
      }
      elseif (in_array($node->type, (array) node_gallery_get_types('gallery'))) {
        $node->gid = $node->nid;
        drupal_write_record('node_gallery_galleries', $node);
        node_gallery_update_image_counts($node);
      }
      break;
    case 'update':
      if (in_array($node->type, (array) node_gallery_get_types('image'))) {
        node_gallery_set_gallery_cover_image($node);

        // If we've change gid's, clear both the old and the new gid caches
        if (isset($node->oldgid)) {
          node_gallery_clear_gallery_caches($node->oldgid);
          node_gallery_clear_gallery_caches($node->newgid);
        }

        // If we change an image's publish status, clear the caches
        $publish_status_changed = FALSE;
        $pos = node_gallery_get_image_position($node->gid, $node->nid);
        if ($node->status == 1) {

          // It should be in the cache, if not, clear them
          if (!isset($pos)) {
            $publish_status_changed = TRUE;
          }
        }
        else {

          // We should *not* be in the cache
          if (isset($pos)) {
            $publish_status_changed = TRUE;
          }
        }
        if ($publish_status_changed) {
          node_gallery_clear_gallery_caches($node->gid);
        }
        node_gallery_set_image_weight($node);
        if (module_exists('og')) {
          node_gallery_inherit_group_settings($node);
          og_nodeapi($node, $op, $a3, $a4);
        }
        $image_exists = db_result(db_query('SELECT nid FROM {node_gallery_images} WHERE nid = %d', $node->nid));
        if ($image_exists) {
          drupal_write_record('node_gallery_images', $node, 'nid');
        }
        else {
          drupal_write_record('node_gallery_images', $node);
        }
        node_gallery_update_image_counts($node);
      }
      elseif (in_array($node->type, (array) node_gallery_get_types('gallery'))) {
        $node->gid = $node->nid;
        $old_node = node_load($node->nid);
        if ($node->status != $old_node->status) {

          // publish status changed
          _node_gallery_set_publish($node, $node->status);
        }
        if (module_exists('og')) {
          $og_settings = array();
          if (isset($node->og_public) && $node->og_public != $old_node->og_public) {
            $og_settings['og_public'] = $node->og_public;
          }
          if (isset($node->og_groups) && $node->og_groups != $old_node->og_groups) {
            $og_settings['og_groups'] = $node->og_groups;
          }
          if (isset($node->og_groups_both) && $node->og_groups_both != $old_node->og_groups_both) {
            $og_settings['og_groups_both'] = $node->og_groups_both;
          }
          if (!empty($og_settings)) {
            _node_gallery_update_group_settings($node, $og_settings);
          }
        }
        $gallery_exists = db_result(db_query('SELECT gid FROM {node_gallery_galleries} WHERE gid = %d', $node->nid));
        if ($gallery_exists) {
          drupal_write_record('node_gallery_galleries', $node, 'gid');
        }
        else {
          drupal_write_record('node_gallery_galleries', $node);
        }
        node_gallery_update_image_counts($node);
        if (module_exists('pathauto')) {

          // Our tokens are all based off the gallery nid or title, no need for processing if those stay the same
          if ($node->title != $old_node->title || $node->gid != $old_node->gid) {
            $imagenids = node_gallery_get_image_nids($node->gid, FALSE, FALSE);
            if (count($imagenids) > 0) {
              if (function_exists('pathauto_node_update_alias_multiple')) {
                pathauto_node_update_alias_multiple($imagenids, 'bulkupdate');
              }
              else {
                pathauto_node_operations_update($imagenids);
              }
            }
          }
        }
      }
      break;
    case 'presave':

      // Add random gid links for devel_generate
      if (!empty($node->devel_generate)) {
        static $gallerynids;
        $gallerytypes = (array) node_gallery_get_types('gallery');
        if (in_array($node->type, $gallerytypes)) {
          $gallerynids = array();
        }
        $imagetypes = (array) node_gallery_get_types('image');
        if (in_array($node->type, $imagetypes)) {
          $i2g = node_gallery_get_image_to_gallery_types();
          if (!isset($gallerynids[$i2g[$node->type]])) {
            $gallerynids[$i2g[$node->type]] = node_gallery_get_gallery_gids($i2g[$node->type]);
          }
          $randindex = array_rand($gallerynids[$i2g[$node->type]], 1);
          $node->gid = $gallerynids[$i2g[$node->type]][$randindex];

          // devel generate always checks the "promote to front page" box.
          $node->promote = 0;

          // remove any extra images devel_generate adds to our image node
          $relationship = node_gallery_get_relationship(NULL, $node->type);
          $node->{$relationship['imagefield_name']} = array_slice($node->{$relationship['imagefield_name']}, 0, 1);
        }
      }
      break;
    case 'view':

      // viewing node gallery page.
      if (in_array($node->type, (array) node_gallery_get_types('gallery'))) {
        _node_gallery_gallery_view($node, $a3, $a4);
      }
      elseif (in_array($node->type, (array) node_gallery_get_types('image'))) {
        _node_gallery_image_view($node, $a3, $a4);
      }
      break;
    case 'delete':
      _node_gallery_delete($node);
      node_gallery_update_image_counts($node);
      break;
  }
}