<?php

function morelikethis_flickr_theme() {
  return array(
    'morelikethis_flickr_item' => array('arguments' => array('item' => NULL)),
    'morelikethis_flickr_block' => array('arguments' => array('items' => NULL)),
  
   );
}

function morelikethis_flickr_morelikethis() {
  return array(
    'flickr' => array(
      '#title' => 'Flickr',
      '#description' => 'Lookup related content using Flickr',
      '#class' => 'MoreLikeThisFlickr',
      '#classfile' => 'morelikethis_flickr.class.inc',
      '#settings' => 'morelikethis_flickr_settings',
    ),
  );
}

function morelikethis_flickr_settings() {
  node_types_rebuild();
  $types = node_get_types('types', NULL, TRUE);
  $form['mlt_flickr_description'] = array(
    '#prefix' => "<h4>",
    '#suffix' => "</h4>",
    '#value'=> t('More Like This Flickr allows you to incorporate photos from Flickr on a node page, by passing the applied MLT terms to the Flickr service as keywords, etc. '),
  );
  
  $form['node_type_settings'] = array( 
    '#type' => 'fieldset', 
    '#title'=> t('Enabled Node Types'),
    '#description' => t('Select the node types that can display the flickr MLT block.')
  );
  
  foreach($types as $type) {
  	$key = drupal_strtolower($type->type);
	  $form['node_type_settings']["morelikethis_flickr_content_type_$key"] = array(
	   '#type' => 'checkbox',
	   '#title' => t($type->name),
	   '#default_value' => variable_get("morelikethis_flickr_content_type_$key", NULL),
	  );
  }
	
	$form['search_params'] = array(
	 '#type' => 'fieldset',
	 '#title' => t('Search Parameters'),
	 '#description' => t('Parameters for the <a target="_blank" href="http://www.flickr.com/services/api/flickr.photos.search.html">flikr.photos.search method</a>.')
	
	);
  $form['search_params']['flickr_api_page_size'] = array(
    '#type' => 'textfield',
    '#title' => t('# of Photos'),
    '#default_value' => variable_get('flickr_api_page_size', '10'),
    '#size' => 3,
    '#description' => t('Number of photos to display'),    
  );
  
  $form['search_params']['flickr_api_page_safe_search'] = array(
    '#type' => 'select',
    '#title' => 'Safe Search',
    '#default_value' => variable_get('flickr_api_page_safe_search', '1'),
    '#options' => array('1' => 'Safe', '2' => 'Moderate', '3' => 'Restricted'),
    '#description' => t('Adjust the filters used to prevent objectionable photos from surfacing on your site.'),    
  );
  
  $form['search_params']['flickr_api_page_tag_mode'] = array(
    '#type' => 'select',
    '#title' => 'Tag Search Mode',
    '#default_value' => variable_get('flickr_api_page_tag_mode', 'any'),
    '#options' => array('any' => 'Any', 'all' => 'All'),
    '#description' => t('Use "Any" to include photos matching any combination of MLT terms applied to a node, and use "All" to only include photos matching EVERY MLT term applied.'),
  );
  
  $form['search_params']['flickr_api_thumbnail_size'] = array(
    '#type' => 'select',
    '#title' => t('Size Suffixes'),
    '#description' => t('Choose a sizing constraint for photos returned by Flickr. Also <a href="http://www.flickr.com/services/api/misc.urls.html">See Photo Source URLs documentation.</a>'),
    '#options' => array(
      's' => t('small square, 75x75'),
      't' => t('thumbnail, 100 on longest side'),
      'm' => t('small, 240 on longest side'),
      '' => t('medium, 500 on longest side'),
      'b' => t('large, 1024 on longest side'),
      'o' => t('original image'), 
    ),
    '#default_value' => variable_get('flickr_api_thumbnail_size', ''),
  );
  
  //TODO: Add min upload date.
  //TODO: locations
  //TODO: content type
  
  return  system_settings_form($form);
}

function theme_morelikethis_flickr_block($items) {
  // todo: add flag for to disable this, since if you override
  // the theme.. this becomes useless.
  drupal_add_css(drupal_get_path('module', "morelikethis") . "/contrib/morelikethis_flickr/mlt_flickr.css");
  drupal_add_js(drupal_get_path('module', "morelikethis") . "/contrib/morelikethis_flickr/mlt_flickr.js");
  
  $output = "<div class='search-with' style='display:none;'>".$items['search_tags']. "</div>";
  $output .= "<ul class='mlt-flickr'>";

 if($items) {
    foreach($items as $photo) {
      $output .= "<li>". theme('morelikethis_flickr_item', $photo);
      $output .= "<div class='tags' style='display:none'>".$photo['tags']."</div>" . "</li>";
    }
  }
  else
    $output .= "<li>No photos found</li>";
      
  $output .= "</ul>";

  return $output;  
}


function theme_morelikethis_flickr_item($photo) {
  $options['html'] = TRUE;
  $options['absolute'] = TRUE;
  
  $link = "<img src='". $photo['url'] ."'/>";
  $url = "http://www.flickr.com/photos/". $photo['owner'] ."/". $photo['id'];
  
  return l($link, $url, $options);
}

