<?php
// $Id: gallery_assist_4cviewer.module,v 1.0 2009/11/10 11:48:40 jcmc Exp $

/**
 * @file gallery_assist_4cviewer.module
 * Allow Gallery Assist to display gallerie content over Cooliris.
 */

/**
 * Implementation of hook_menu().
 */
function gallery_assist_4cviewer_menu() {
  $items = array();
  
  $items['gallery_assist/%node/data4cooliris.rss'] = array(
    'page callback' => 'gallery_assist_4cviewer_xmler',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
function gallery_assist_4cviewer_form_alter(&$form, $form_state, $form_id) { 
  
  $node = $form['#node'];
  $cooliris_conf = unserialize($node->cooliris_conf);

  // Form to assign Cooliris to Gallery Assist or Gallery Assist assigments content types.
  if ($form_id == 'gallery_assist_settings_extras') {
    // Cooliris
    $form['gallery_assist_extras']['gallery_assist_4cviewer'] = array(
      '#type' => 'fieldset',
      '#title' => t('Cooliris'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => -48,
      '#description' => t('Assign the Cooliris functionality to Gallery Assist assigments.'),
    );

    $form['gallery_assist_extras']['gallery_assist_4cviewer'] += _gallery_assist_4cviewer_form();
    
    $form['#submit'][] = 'gallery_assist_4cviewer_settings_submit';
  }

  if (isset($node->nid) && $form_id == $node->type .'_node_form' && variable_get('gallery_assist_'. $node->type .'_cooliris', 0) == 1) {
      
      // Cooliris configuration for the node (fieldset).
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris'] = array(
        '#type' => 'fieldset',
        '#title' => t('Cooliris settings for this NODE'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      
      // Cooliris toggle for the node.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['cooliris'] = array(
        '#type' => 'checkbox',
        '#title' => t('Check if you want to config this node separatelly.'),
        '#default_value' => $node->cooliris,
      );
      // Cooliris toggle for the node.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['cooliris_changed'] = array(
        '#type' => 'value',
        '#value' => $node->cooliris,
      );
    
    if ($node->cooliris == 1) {
      
      // Cooliris configuration for the teaser view of the node.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t'] = array(
        '#type' => 'fieldset',
        '#title' => t('Cooliris settings for the teaser view'),
        '#prefix' => '<div class="gallery-assist-settings-left-half">',
        '#suffix' => '</div>',
      );
      
      // Cooliris configuration for the page view of the node.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p'] = array(
        '#type' => 'fieldset',
        '#title' => t('Cooliris settings for the page view'),
        '#prefix' => '<div class="gallery-assist-settings-left-half">',
        '#suffix' => '</div><div class="clear-block"></div>',
      );

      // Teaser conf.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_method'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris method'),
        '#default_value' => $cooliris_conf['t_cooliris_method'],
        '#options' => array('piclens' => 'Piclens', 'embed' => 'Embed'),
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_float'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris container position'),
        '#default_value' => $cooliris_conf['t_cooliris_float'],
        '#options' => array('none' => 'none', 'left' => 'left', 'right' => 'right', 'center' => 'center'),
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container with'),
        '#default_value' => $cooliris_conf['t_cooliris_width'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_height'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container height'),
        '#default_value' => $cooliris_conf['t_cooliris_height'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_backgroundColor'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container background-color'),
        '#default_value' => $cooliris_conf['t_cooliris_backgroundColor'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_showSearch'] = array(
        '#type' => 'checkbox',
        '#title' => t('Cooliris show Search'),
        '#default_value' => $cooliris_conf['t_cooliris_showSearch'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_showEmbed'] = array(
        '#type' => 'checkbox',
        '#title' => t('Cooliris show Embed'),
        '#default_value' => $cooliris_conf['t_cooliris_showEmbed'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['t']['gallery_assist_' . $node->type . '_t_cooliris_numRows'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris numRows'),
        '#default_value' => $cooliris_conf['t_cooliris_numRows'],
        '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
      );

      // Page conf.
      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_method'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris method'),
        '#default_value' => $cooliris_conf['p_cooliris_method'],
        '#options' => array('piclens' => 'Piclens', 'embed' => 'Embed'),
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_float'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris container position'),
        '#default_value' => $cooliris_conf['p_cooliris_float'],
        '#options' => array('' => 'none', 'left' => 'left', 'right' => 'right', 'center' => 'center'),
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container with'),
        '#default_value' => $cooliris_conf['p_cooliris_width'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_height'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container height'),
        '#default_value' => $cooliris_conf['p_cooliris_height'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_backgroundColor'] = array(
        '#type' => 'textfield',
        '#title' => t('Cooliris container background-color'),
        '#default_value' => $cooliris_conf['p_cooliris_backgroundColor'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_showSearch'] = array(
        '#type' => 'checkbox',
        '#title' => t('Cooliris show Search'),
        '#default_value' => $cooliris_conf['p_cooliris_showSearch'],
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_showEmbed'] = array(
        '#type' => 'checkbox',
        '#title' => t('Cooliris show Embed'),
        '#default_value' => $cooliris_conf['p_cooliris_showEmbed'],
        '#size' => 5,
      );

      $form['gallery_assist_settings']['gallery_assist_'. $node->type .'_cooliris']['p']['gallery_assist_' . $node->type . '_p_cooliris_numRows'] = array(
        '#type' => 'select',
        '#title' => t('Cooliris numRows'),
        '#default_value' => $cooliris_conf['p_cooliris_numRows'],
        '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
      );

      $form['extra_gref']['gref'] = array(
        '#type' => 'hidden',
        '#value' => $node->gref,
      );
    }
      
    $form['#submit'][] = 'gallery_assist_4cviewer_node_settings_submit';
  }
}

/**
 * Build the list of content types to assign the Cooliris functionality.
 * 
 * @see gallery_assist_4cviewer_form_alter().
 * @return An form array preformated to checkbox containing a list of content types with assigned gallery functionality.
 */
function _gallery_assist_4cviewer_form() {
  $form = array();

  $types = node_get_types();
  foreach ($types as $typ => $v) {
    $default_value = variable_get('gallery_assist_'. $v->type .'_cooliris', 0);
    if (variable_get('gallery_assist_'. $v->type, 0) == 1) {
      $form['gallery_assist_4cviewer']['gallery_assist_'. $v->type .'_cooliris'] = array(
        '#type' => 'checkbox',
        '#title' => $v->name,
        '#default_value' => $default_value,
      );

      $form['gallery_assist_4cviewer']['gallery_assist_'. $v->type .'_cooliris_changed'] = array(
        '#type' => 'hidden',
        '#value' => $default_value,

      );
    }
  }

  return $form;
}

/**
 * Assignig of Cooliris functionality and set default config variables.
 */
function gallery_assist_4cviewer_settings_submit(&$form, $form_state) {
  $types = node_get_types();
  
  foreach ($types as $key) {
    if ($form_state['values']['gallery_assist_'. $key->type .'_cooliris'] != $form_state['values']['gallery_assist_'. $key->type .'_cooliris_changed']) {

      variable_set('gallery_assist_'. $key->type .'_cooliris', $form_state['values']['gallery_assist_'. $key->type .'_cooliris']);
      
      if ($form_state['values']['gallery_assist_'. $key->type .'_cooliris'] == 1) {
        $data = variable_get('gallery_assist_4cviewer_default_data', FALSE);
        variable_set('gallery_assist_4cviewer_'. $key->type .'_data', $data);
      }
      
      if ($form_state['values']['gallery_assist_'. $key->type .'_cooliris'] == 0) {
        variable_set('gallery_assist_4cviewer_'. $key->type .'_data', NULL);
      }
  
    }
  }
}

/**
 * Save the settings data for each node with separate cooliris configuration.
 */
function gallery_assist_4cviewer_node_settings_submit(&$form, $form_state) {

  $entry_io = $form_state['values']['cooliris'];

  if ($entry_io == 1)  {

    $g = 'gallery_assist_'. $form['#node']->type .'_';

    $data = array(
      't_cooliris_method'           => $form_state['values'][$g .'t_cooliris_method'] ? $form_state['values'][$g .'t_cooliris_method'] : 'piclens',
      't_cooliris_float'            => $form_state['values'][$g .'t_cooliris_float'] ? $form_state['values'][$g .'t_cooliris_float'] : '',
      't_cooliris_width'            => $form_state['values'][$g .'t_cooliris_width'] ? $form_state['values'][$g .'t_cooliris_width'] : '300',
      't_cooliris_height'           => $form_state['values'][$g .'t_cooliris_height'] ? $form_state['values'][$g .'t_cooliris_height'] : '0',
      't_cooliris_backgroundColor'  => $form_state['values'][$g .'t_cooliris_backgroundColor'] ? $form_state['values'][$g .'t_cooliris_backgroundColor'] : '#FFFFFF',
      't_cooliris_showSearch'       => $form_state['values'][$g .'t_cooliris_showSearch'] ? $form_state['values'][$g .'t_cooliris_showSearch'] : 0,
      't_cooliris_showEmbed'        => $form_state['values'][$g .'t_cooliris_showEmbed'] ? $form_state['values'][$g .'t_cooliris_showEmbed'] : 0,
      't_cooliris_numRows'          => $form_state['values'][$g .'t_cooliris_numRows'] ? $form_state['values'][$g .'t_cooliris_numRows'] : 2,
      
      'p_cooliris_method'           => $form_state['values'][$g .'p_cooliris_method'] ? $form_state['values'][$g .'p_cooliris_method'] : 'embed',
      'p_cooliris_float'            => $form_state['values'][$g .'p_cooliris_float'] ? $form_state['values'][$g .'p_cooliris_float'] : 'none',
      'p_cooliris_width'            => $form_state['values'][$g .'p_cooliris_width'] ? $form_state['values'][$g .'p_cooliris_width'] : '100%',
      'p_cooliris_height'           => $form_state['values'][$g .'p_cooliris_height'] ? $form_state['values'][$g .'p_cooliris_height'] : '400',
      'p_cooliris_backgroundColor'  => $form_state['values'][$g .'p_cooliris_backgroundColor'] ? $form_state['values'][$g .'p_cooliris_backgroundColor'] : '#FFFFFF',
      'p_cooliris_showSearch'       => $form_state['values'][$g .'p_cooliris_showSearch'] ? $form_state['values'][$g .'p_cooliris_showSearch'] : 0,
      'p_cooliris_showEmbed'        => $form_state['values'][$g .'p_cooliris_showEmbed'] ? $form_state['values'][$g .'p_cooliris_showEmbed'] : 0,
      'p_cooliris_numRows'          => $form_state['values'][$g .'p_cooliris_numRows'] ? $form_state['values'][$g .'p_cooliris_numRows'] : 3,
    );
    
    $entry_data = serialize($data);
  }
  else {

    $entry_io = (int)0;
    $entry_data = NULL;

  }

  db_query("UPDATE {gallery_assist} SET cooliris = %d, cooliris_conf = '%s' WHERE nid = %d", $entry_io, $entry_data, $form['#node']->nid);
}

/**
 * Implementation of hook_nodeapi().   
 */
function gallery_assist_4cviewer_nodeapi(&$node, $op, $teaser, $page) {

  switch ($op) {
    case 'load':

      $coll_conf = gallery_assist_4cviewer_load($node);
      $node->cooliris = $coll_conf['cooliris'];
      $node->cooliris_conf = $coll_conf['cooliris_conf'];
      break;

    case 'view':

      if ($teaser) {
        if ($node->cooliris == 1) {
          $conf = unserialize($node->cooliris_conf);
        }
        else {
          $conf = variable_get('gallery_assist_4cviewer_'. $node->type .'_data', FALSE);
        }
        
        if (variable_get('gallery_assist_'. $node->type .'_cooliris', 0) == 1) {
          if ($conf['t_cooliris_method'] == 'piclens') {
            gallery_assist_4cviewer_piclens_rss($node);
            gallery_assist_4cviewer_piclens();
          }
          
          if ($conf['t_cooliris_method'] == 'embed') {
            if (!empty($node->gallitems)) {
              $cooliris_container = gallery_assist_4cviewer_container($node, 'teaser', $conf);
              $node->content['my_gallery_field'] = array(
                '#value' => $cooliris_container,
                '#weight' => $node->gallery_assist_weight,
              );
            }
          }
        }
      }

      if ($page) {
        if ($node->cooliris == 1) {
          $conf = unserialize($node->cooliris_conf);
        }
        else {
          $conf = variable_get('gallery_assist_4cviewer_'. $node->type .'_data', FALSE);
        }
        
        if (variable_get('gallery_assist_'. $node->type .'_cooliris', 0) == 1) {
          if ($conf['p_cooliris_method'] == 'piclens') {
            gallery_assist_4cviewer_piclens_rss($node);
            gallery_assist_4cviewer_piclens();
          }
          
          if ($conf['p_cooliris_method'] == 'embed') {
            if (!empty($node->gallitems)) {
              $cooliris_container = gallery_assist_4cviewer_container($node, 'page', $conf);
              $node->content['my_gallery_field'] = array(
                '#value' => $cooliris_container,
                '#weight' => $node->gallery_assist_weight,
              );
            }
          }
        }
      }
      break;
  }
}

/**
 * If the Browser add-on "Cooliris" installed and active appear a icon over 
 * the images linked to the server application on cooliris.com. 
 * The rss data must, will be sended.
 */
function gallery_assist_4cviewer_piclens_rss(&$node) {
  global $base_url;

  // <link id="photos" rel="alternate" href="photos.rss" type="application/rss+xml" title="PicLens RSS" />
  $rss_attr = array(
    'id'    => "photos", 
    'rel'   => "alternate", 
    'href'  => $base_url .'/gallery_assist/'. $node->nid .'/data4cooliris.rss',
    'type'  => "application/rss+xml", 
    'title' => "PicLens RSS",
  );

  drupal_add_link($rss_attr);

}

/**
 * Call and include the feed link and the needed js file.
 */
function gallery_assist_4cviewer_piclens() {

  $head_content = drupal_get_html_head();
  if (!preg_match("/piclens.js/", $head_content)) {
    $js_attr = array(
      'type' => "text/javascript", 
      'src'  => "http://lite.piclens.com/current/piclens.js",
    );
    drupal_set_html_head('<script'. drupal_attributes($js_attr) ." /></script>\n");
  }
}

/**
 * Build the Gallery Assist Container with the embed Cooliris player.
 * 
 * @param $node
 *   object containing all loaded fields of the node.
 * @param $op
 *   string wich tell the function or the output will be listed or displayed as page (teaser or page).
 * @param $conf
 *   An array with the needed settings variables for the Cooliris player.
 * 
 * @return
 *   String containing the Gallery Assist Container with the embed Cooliris player.
 */
function gallery_assist_4cviewer_container($node, $op, $conf) {
  global $base_url;

  $prefix = $op == 'teaser' ? 't_' : 'p_';
  
  $feed = $base_url .'/gallery_assist/'. $node->nid .'/data4cooliris.rss';
  
  $cooliris_conf = $conf;

  $backgroundColor = $cooliris_conf[$prefix .'cooliris_backgroundColor'] ? $cooliris_conf[$prefix .'cooliris_backgroundColor'] : '#FFFFFF';
  $showSearch = $cooliris_conf[$prefix .'cooliris_showSearch'] ? $cooliris_conf[$prefix .'cooliris_showSearch'] : 'false';
  $showEmbed = $cooliris_conf[$prefix .'cooliris_showEmbed'] ? $cooliris_conf[$prefix .'cooliris_showEmbed'] : 'false';
  $numRows = $cooliris_conf[$prefix .'cooliris_numRows'] ? $cooliris_conf[$prefix .'cooliris_numRows'] : 3;
  $container_width = $cooliris_conf[$prefix .'cooliris_width'] ? $cooliris_conf[$prefix .'cooliris_width'] : '600';
  $container_height = $cooliris_conf[$prefix .'cooliris_height'] ? $cooliris_conf[$prefix .'cooliris_height'] : '';

  $gallery_container_attr = array(
    'id' => 'gallery-assist-cooliris-container-'. $node->nid,
    'class' => $node->type .' gallery-assist-cooliris-container',
  );

  $my_attr_array = array(
    'style' => 'max-width:'. $node->gallconf[$node->type]['container_width'] .'px;',
  );
  $stl = array();
  $pstl = array();
  
  $conf = array();
  $conf = $node->gallconf[$node->type];

  switch ($op) {
    case 'teaser':
      if ($conf['layout']['ga_align'] != 'none' || $conf['layout']['t_ga_float'] != 'none') {
        
        //ALIGN TEASER
        switch ($conf['layout']['ga_align']) {
          case 'left':
            $stl['l'] = 'margin-right:auto';
            break;
  
          case 'center':
            $stl['ml'] = 'margin-left:auto';
            $stl['mr'] = 'margin-right:auto';
            break;
  
          case 'right':
            $stl['r'] = 'margin-left:auto';
            break;
        }
  
        //FLOAT TEASER
        switch ($conf['layout']['t_ga_float']) {
          case 'left':
            $stl['l'] = 'float:left';
            $stl['mr'] = 'margin-right:10px';
            unset($stl['ml']);
            break;
  
          case 'right':
            $stl['l'] = 'float:right';
            $stl['ml'] = 'margin-left:10px';
            unset($stl['mr']);
            break;
        }
      } // end (styles)
      break;

    case 'page': 

      if ($conf['layout']['gap_align'] != 'none' || $conf['layout']['p_ga_float'] != 'none') { //(styles)
  
        //ALIGN PAGE
        switch ($conf['layout']['gap_align']) {
          case 'left':
            $stl['l'] = 'margin-right:auto';
            $pstl['pl'] = 'text-align:left;';
            break;
  
          case 'center':
            $stl['ml'] = 'margin-left:auto';
            $stl['mr'] = 'margin-right:auto';
            $pstl['pl'] = 'text-align:center;padding-right:10px;';
            break;
  
          case 'right':
            $stl['r'] = 'margin-left:auto';
            $pstl['pl'] = 'text-align:right;';
            break;
        }
  
        //FLOAT PAGE
        switch ($conf['layout']['p_ga_float']) {
          case 'left':
            $stl['l'] = 'float:left';
            $stl['mr'] = 'margin-right:10px';
            unset($stl['ml']);
            break;
  
          case 'right':
            $stl['l'] = 'float:right';
            $stl['ml'] = 'margin-left:10px';
            unset($stl['mr']);
            break;
        }
      } // end (styles)
      break;
  }

  $my_attr_array['style'] .= implode(';', $stl);
  $my_attr_array['style'] .= ';width:'. $container_width .'px;';

$new = array_merge($gallery_container_attr, $my_attr_array);

  //$output .= '<div '. $attr .'>'."\n";
  $output .= '<div '.  drupal_attributes($new) .'>'."\n";

  $output .= '<object id="o" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'. $container_width .'" height="'. $container_height .'">
    <param name="movie" value="http://apps.cooliris.com/embed/cooliris.swf" />
    <param name="wmode" value="transparent" /> 
    <param name="allowFullScreen" value="true" />
    <param name="allowScriptAccess" value="always" />
    <param name="flashvars" value="showEmbed='. $showEmbed .
           '&showSearch='. $showSearch .
           '&backgroundColor='. $backgroundColor .
           '&numRows='. $numRows .
           '&feed='. $feed .'" />
    <embed type="application/x-shockwave-flash" 
           src="http://apps.cooliris.com/embed/cooliris.swf" 
           flashvars="showEmbed='. $showEmbed .
           '&showSearch='. $showSearch .
           '&backgroundColor='. $backgroundColor .
           '&numRows='. $numRows .
           '&feed='. $feed .'" 
           width="'. $container_width .'" 
           height="'. $container_height .'" 
           wmode= "transparent" 
           allowFullScreen="true" 
           allowScriptAccess="always">
    </embed></object>'."\n";
  $output .= '</div>'."\n";

  return $output;
}

/**
 * Implementation of hook_load(). 
 */
function gallery_assist_4cviewer_load(&$node) {
  $q = "SELECT cooliris, cooliris_conf FROM {gallery_assist} WHERE nid = %d";
  $additions = db_fetch_array(db_query($q, $node->nid));

  return $additions;
}

/**
 * Build the needed feed data for cooliris.
 */
function gallery_assist_4cviewer_xmler($node) {

  if (count($node->gallitems) == 0) {
    return;
  }
  
  global $base_url;

  $dom = new DOMDocument('1.0', 'utf-8');
  $dom->xmlStandalone = true;

  // create root
  $rss = $dom->createElement('rss');
  $dom->appendChild($rss);
    // Add attributes to root.
    // Version.
    $rss_attr1 = $dom->createAttribute('version');
    $rss->appendChild($rss_attr1);
    $rss_attr1_text = $dom->createTextNode('2.0');
    $rss_attr1->appendChild($rss_attr1_text);
    // XmlnsMedia.
    $rss_attr2 = $dom->createAttribute('xmlns:media');
    $rss->appendChild($rss_attr2);
    $rss_attr2_text = $dom->createTextNode('http://search.yahoo.com/mrss/');
    $rss_attr2->appendChild($rss_attr2_text);
    // XmlnsAtom.
    $rss_attr3 = $dom->createAttribute('xmlns:atom');
    $rss->appendChild($rss_attr3);
    $rss_attr3_text = $dom->createTextNode('http://www.w3.org/2005/Atom');
    $rss_attr3->appendChild($rss_attr3_text);

  // Header.
  $channel = $dom->createElement('channel');
    $rss->appendChild($channel);
    $ptitle = $dom->createElement('title', 'Test');
    $channel->appendChild($ptitle);
    $plink = $dom->createElement('link', $base_url);
    $channel->appendChild($plink);
    $pdescription = $dom->createElement('description', $node->body);
    $channel->appendChild($pdescription);
    
    foreach ($node->gallitems as $key) {
      // Content.
      $item = $dom->createElement('item');
      $channel->appendChild($item);

      $title = $dom->createElement('title', $key->ptitle);
      $item->appendChild($title);

      $description = $dom->createElement('media:description', $key->pdescription);
      $item->appendChild($description);

      $link = $dom->createElement('link', $base_url .'/node/'. $node->nid .'/'. $key->pid);
      $item->appendChild($link);

      $thumbnail = $dom->createElement('media:thumbnail');
      $item->appendChild($thumbnail);
      $thumbnail_attr1 = $dom->createAttribute('url');
      $thumbnail->appendChild($thumbnail_attr1);
      $thumbnail_attr1_text = $dom->createTextNode(file_create_url($key->tpath));
      $thumbnail_attr1->appendChild($thumbnail_attr1_text);

      $content = $dom->createElement('media:content');
      $item->appendChild($content);
      $content_attr1 = $dom->createAttribute('url');
      $content->appendChild($content_attr1);
      $content_attr1_text = $dom->createTextNode(file_create_url($key->opath));
      $content_attr1->appendChild($content_attr1_text);
  }

  $data = $dom->saveXML();

  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
  
  print $data;

}
