You are here

function uc_option_image_nodeapi in Ubercart Option Images 6

Implements hook_nodeapi().

File

./uc_option_image.module, line 30
Provides image upload fields for attribute options. @author Tj Holowaychuk <tj@vision-media.ca/> @link http://vision-media.ca @todo supply 'default' image field when no option images are supplied or no option image attributes are…

Code

function uc_option_image_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':

      // Load option images
      // Keep in mind the file 'filename' is a mash of nid/aid/oid
      $node->option_images = array();
      $node->option_images_cached = array();
      $node->attributes = uc_product_get_attributes($node->nid);
      if ($node->attributes) {
        $page_size = variable_get('uc_option_image_page_size', 'preview');
        $enabled_attributes = variable_get('uc_option_image_attributes', array());
        foreach ($node->attributes as $attribute) {
          if ($attribute->options && $enabled_attributes[$attribute->aid]) {
            foreach ($attribute->options as $option) {
              $file = uc_option_image_load($node->nid, $attribute->aid, $option->oid);
              if ($file && $file->filepath) {
                $node->option_images[] = $file;

                // Load imagecached option images
                if ($page_size != '_original') {
                  $node->option_images_cached[$option->oid] = imagecache_create_url($page_size, $file->filepath);
                }
                else {
                  $node->option_images_cached[$option->oid] = url($file->filepath, array(
                    'absolute' => TRUE,
                  ));
                }
              }
            }
          }
        }
      }
      break;
    case 'view':

      // @todo issue being invoked so many times?
      // @todo refactor
      if (isset($node->content['add_to_cart'])) {
        if (user_access('view option images')) {
          if ($node->option_images) {
            $attributes = $node->attributes;
            $first_attribute = array_shift($node->attributes);
            array_unshift($node->attributes, $first_attribute);
            $page_size = variable_get('uc_option_image_page_size', 'preview');
            $teaser_size = variable_get('uc_option_image_teaser_size', 'thumbnail');
            $size = $a4 ? $page_size : $teaser_size;

            // Pass attributes to uc_option_image to populate JS settings
            if ($a4) {
              uc_option_image($node, $attributes, $size);
            }

            // Determine if we have a default option using
            // the first attribute's default option
            if ($first_attribute->default_option) {
              $default_option = $first_attribute->default_option;
            }

            // Load the default image file
            $file = uc_option_image_load($node->nid, $first_attribute->aid, $default_option);

            // Display the image based on teaser/page view
            // Ensure that original file exists
            if ($file->filepath && file_exists($file->filepath)) {
              $image = theme('uc_option_image', $first_attribute->aid, $file, $size);
            }
            else {
              $image = theme('uc_option_image_no_image', $node, $size);
            }

            // Preload images
            if ($a4) {
              $preloaded_images = theme('uc_option_image_preloaded', $first_attribute->aid, $node, $size);
            }
            $node->content['option_image'] = array(
              '#value' => $image . $preloaded_images,
              '#access' => user_access('view option images'),
              '#weight' => (int) variable_get('uc_option_image_node_weight', '10'),
            );
          }
        }
      }
      break;
  }
}