flickr_sets.inc

  1. <?php
  2. // $Id
  3.  
  4. // author : Alex McFadyen (openlyconnectec.com) - http://drupal.org/user/195063
  5. // @todo update settings so they can be per field not just global.
  6.  
  7. define('IMAGE_NCCK_DEFAULT_FLICKR_SET_CLASS', 'flickr-set');
  8. define('IMAGE_NCCK_DEFAULT_FLICKR_SET_HEADER', '<a href="!url" target="_blank">Flickr Set : @name, showing @number of @total images</a>');
  9. define('IMAGE_NCCK_DEFAULT_FLICKR_SET_LIMIT', 10);
  10.  
  11. include_once('flickr.inc');
  12. /**
  13.  * hook image_ncck_PROVIDER_info
  14.  * this returns information relevant to a specific 3rd party video provider
  15.  * @return
  16.  *   an array of strings requested by various admin and other forms
  17.  *   'name' => the translated name of the provider
  18.  *   'url' => the url to the main page for the provider
  19.  *   'settings_description' => a description of the provider that will be posted in the admin settings form
  20.  *   'supported_features' => an array of rows describing the state of certain supported features by the provider.
  21.  *      These will be rendered in a table, with the columns being 'Feature', 'Supported', 'Notes'.
  22.  */
  23.  
  24. function image_ncck_flickr_sets_info() {
  25.         echo('CALLED');
  26.   $name = t('Flickr Sets');
  27.   return array(
  28.     'provider' => 'flickr_sets',
  29.     'name' => $name,
  30.     'url' => IMAGE_NCCK_FLICKR_MAIN_URL,
  31.     'settings_description' => t('These settings specifically affect images displayed from !flickr.', array('!flickr' => l($name, IMAGE_NCCK_FLICKR_MAIN_URL, array('target' => '_blank')), '!api' => l(t('API'), IMAGE_NCCK_FLICKR_API_INFO, array('target' => '_blank'))))
  32.   );
  33. }
  34.  
  35. /**
  36.  * hook image_ncck_PROVIDER_settings
  37.  * this should return a subform to be added to the image_ncck_settings() admin settings page.
  38.  * note that a form field will already be provided, at $form['PROVIDER'] (such as $form['flickr'])
  39.  * so if you want specific provider settings within that field, you can add the elements to that form field.
  40.  */
  41. function image_ncck_flickr_sets_settings() {
  42.   $form['flickr_set']['image_ncck_flickr_set_limit'] = array(
  43.     '#type' => 'textfield',
  44.     '#title' => t('Flickr Sets limit'),
  45.     '#default_value' => variable_get('image_ncck_flickr_set_limit', IMAGE_NCCK_DEFAULT_FLICKR_SET_LIMIT),
  46.     '#description' => t("The maxamum number of results to return from flickr for each set."),
  47.   );
  48.   $form['flickr_set']['image_ncck_flickr_set_class'] = array(
  49.     '#type' => 'textfield',
  50.     '#title' => t('Flickr Sets CSS class'),
  51.     '#default_value' => variable_get('image_ncck_flickr_set_class', IMAGE_NCCK_DEFAULT_FLICKR_SET_CLASS),
  52.     '#description' => t("The class or classes to be applied to the wrapper around the Flickr set's results."),
  53.   );
  54.   $form['flickr_set']['image_ncck_flickr_set_header'] = array(
  55.     '#type' => 'textfield',
  56.     '#title' => t('Flickr Sets header'),
  57.     '#default_value' => variable_get('image_ncck_flickr_set_header', IMAGE_NCCK_DEFAULT_FLICKR_SET_HEADER),
  58.     '#description' => t("The text placed above the selection of images, this text can use several tokens<br/>
  59.    <ul><li>!url : The url back to the set on flickr's website</li>
  60.    <li>@name : The name of the set</li>
  61.    <li>@total : The total number of images in the set</li>
  62.    <li>@number : The number of images currently being displayed from the set</li>
  63.    <li>@description : The description of the set, from flickr</li></ul>")
  64.   );
  65.   return $form;
  66. }
  67.  
  68. /**
  69.  * hook image_ncck_PROVIDER_extract($embed)
  70.  * Strips out the ID of the resource from a url or embed code.
  71.  *  @param $embed
  72.  *    the string containing the contents to check
  73.  *  @return
  74.  *    a string containing the id or a array with a regex pattern in it.
  75.  */
  76. function image_ncck_flickr_sets_extract($embed = '') {
  77.   //http://www.flickr.com/photos/9382875@N05/sets/2238059970
  78.   return array('@flickr\.com/photos/[^/]*/sets/(\d+)@i');
  79. }
  80.  
  81. /**
  82.  * hook image_ncck_PROVIDER_data($code)
  83.  * returns an array of data about the item
  84.  *  @param $field
  85.  *      a array with the form api details of the field calling
  86.  *  @param $item
  87.  *    a array containing the item to get data for
  88.  *  @return
  89.  *    a array of data about the item
  90.  */
  91. function image_ncck_flickr_sets_data($field, $item) {
  92.   $images = image_ncck_flickr_sets_getSet($item['value']);
  93.  
  94.   if (preg_match('@flickr\.com/photos/([^/]*)@i', $item['embed'], $matches)) {
  95.         $owner = $matches[1];
  96.   }
  97.  
  98.   $data = array();
  99.   foreach ($images['#set'] as $image) {
  100.     $tags = array();
  101.     if (is_array($image['#tags'])) {
  102.       foreach ($image['#tags'] as $tag) {
  103.         $tags[] = $tag['raw'];
  104.       }
  105.     }
  106.    
  107.     $data[] = array(
  108.       'owner' => $owner,
  109.       'title' => $image['#title'],
  110.         'description' => $image['#description'],
  111.       'tags' => $tags
  112.     );
  113.   }
  114.  
  115.   return $data;
  116. }
  117.  
  118.  
  119.  
  120. /**
  121.  *  This allows flickr photosets to be collected
  122.  * @todo update the image_ncck_flickr_request/request_xml to get tags correctly
  123.  */
  124. function image_ncck_flickr_sets_getSet($id, $limit = -1, $page = 0) {
  125.         if ($limit == -1) $limit = variable_get('image_ncck_flickr_set_limit', IMAGE_NCCK_DEFAULT_FLICKR_SET_LIMIT);
  126.   $images = array();
  127.  
  128.   // http://www.flickr.com/photos/nikkiana/sets/72157601948647678/
  129.   if (is_numeric($id)) {
  130.     $page ++;  // flickr starts current page at 1
  131.     $args = array('photoset_id' => $id);
  132.     if ($limit) {
  133.       $args['per_page'] = $limit;
  134.     }
  135.     $args['page'] = $page;
  136.     //print_r($args);
  137.     $xml = image_ncck_flickr_request('flickr.photosets.getPhotos', $args);
  138.     //print_r($xml);
  139.     $images['#pages'] = $xml['photoset']['pages'];
  140.     $images['#page'] = $xml['photoset']['page'] - 1;
  141.     $images['#total'] = $xml['photoset']['total'];
  142.     $images['#per_page'] = $xml['photoset']['per_page'];
  143.     $images['#set'] = array();
  144.     foreach ($xml['photoset']['photo'] as $photo) {
  145.       $data = image_ncck_flickr_data(NULL, array('value' => $photo['id']));
  146.       $images['#set'][] = array(
  147.         '#code' => $photo['id'],
  148.         '#title' => $photo['title'],
  149.         '#link' => image_ncck_flickr_embedded_link($photo['id'], $xml['photoset']['owner']),
  150.         '#thumb' => image_ncck_flickr_image_url($photo['id'], 100, 100, NULL, NULL, NULL),
  151.         '#body' => $images['description'],
  152.         '#tags' => $images['tags'],
  153.       );
  154.     }
  155.   }
  156.   return $images;
  157. }
  158.  
  159. /**
  160.  * hook image_ncck_PROVIDER_embedded_link($code)
  161.  * returns a link to view the content at the provider's site
  162.  *  @param $code
  163.  *    the string containing the content to watch
  164.  *  @return
  165.  *    a string containing the URL to view the video at the original provider's site
  166.  */
  167. function image_ncck_flickr_sets_embedded_link($code, $data = array()) {
  168.   return image_ncck_flickr_embedded_link($code, $data);
  169. }
  170.  
  171. /**
  172.  *  implement image_ncck_PROVIDER_image_title
  173.  *
  174.  *  @param $code
  175.  *    the code of the image
  176.  *  @param $data
  177.  *    any stored data for the image, which may already have the title
  178.  *  @return
  179.  *    the title as the 3rd party provider knows it, if accessible to us. otherwise, ''
  180.  */
  181. function image_ncck_flickr_sets_image_title($code, $data) {
  182.   return image_ncck_flickr_image_title($code, $data);
  183. }
  184.  
  185.  
  186. /**
  187.  *  implement image_ncck_PROVIDER_image_url
  188.  *
  189.  *  @param $code
  190.  *    the code of the image
  191.  *  @param $data
  192.  *    any stored data for the image, which may already have the title
  193.  *  @return
  194.  *    the url directly to the image to display
  195.  */
  196. function image_ncck_flickr_sets_image_url($code, $width, $height, $formatter = NULL, $field = NULL, $item = NULL, $node = NULL) {
  197.   return image_ncck_flickr_image_url($code, $width, $height, $formatter, $field, $item, $node);
  198. }
  199.  
  200.  
  201.  
  202. /**
  203.  *  implement image_ncck_PROVIDER_image_display
  204.  *
  205.  *  @return
  206.  *    formatted image(s)
  207.  */
  208. function image_ncck_flickr_sets_image_display($field, $item, $formatter, $node, $code, $width, $height, $link) {
  209.         $output = '<div class="'.variable_get('image_ncck_flickr_set_class', IMAGE_NCCK_DEFAULT_FLICKR_SET_CLASS).'">';
  210.         $images = image_ncck_flickr_sets_getSet($item['value']);
  211.  
  212.   if (preg_match('@flickr\.com/photos/([^/]*)@i', $item['embed'], $matches)) {
  213.         $owner = $matches[1];
  214.   }
  215.  
  216.   $xml = image_ncck_flickr_request('flickr.photosets.getInfo', array('photoset_id'=>$item['value']));
  217.  
  218.   $number = count($images['#set']);
  219.  
  220.   $output .= t(variable_get('image_ncck_flickr_set_header', IMAGE_NCCK_DEFAULT_FLICKR_SET_HEADER),
  221.                                                                 array(
  222.                                                                         '!url' => $item['embed'],
  223.                                                                         '@name' => $xml['photoset']['title']['_content'],
  224.                                                                         '@total' => $xml['photoset']['photos'],
  225.                                                                         '@number' => $number,
  226.                                                                         '@description' => strip_tags($xml['photoset']['description']['_content'])
  227.                                                          ));
  228.  
  229.   foreach ($images['#set'] as $image) {
  230.         $this_link = $link;
  231.     $tags = array();
  232.     if (is_array($image['#tags'])) {
  233.       foreach ($image['#tags'] as $tag) {
  234.         $tags[] = $tag['raw'];
  235.       }
  236.     }
  237.    
  238.     if ($this_link == IMAGE_NCCK_LINK_CONTENT) {
  239.       $this_link = 'node/' . $node->nid;
  240.     }
  241.     else if ($link == IMAGE_NCCK_LINK_PROVIDER) {
  242.       $this_link = image_ncck_flickr_sets_embedded_link($image['#code'], array('owner'=>$owner));
  243.     }
  244.     else {
  245.       $this_link = NULL;
  246.     }
  247.                 $title = image_ncck_flickr_sets_image_title($image['#code'], array('title'=>$image['#title']));
  248.                 $output .= theme('image_ncck_image', $field, $item, $formatter, $node, $image['#code'], $width, $height, $title, $this_link);
  249.  
  250.   }
  251.  
  252.   return $output.'</div>';
  253.                        
  254.  
  255. }