From 71646ec84d591ad0d6d390bbcf854e587c3e0a48 Mon Sep 17 00:00:00 2001 From: lolandese Date: Sun, 15 Dec 2013 23:00:24 +0200 Subject: lolandese: Generated with Drush iq --- field/flickrfield.module | 84 +++++++++++++++++++++++++++++++++------------- flickr.api.inc | 6 ++-- flickr.inc | 60 ++++++++++++++++++++++++++++++++- 3 files changed, 123 insertions(+), 27 deletions(-) diff --git a/field/flickrfield.module b/field/flickrfield.module index 1ef2dd8..ca1f1e3 100644 --- a/field/flickrfield.module +++ b/field/flickrfield.module @@ -112,30 +112,68 @@ function flickrfield_widget(&$form, &$form_state, $field, $items, $delta = 0) { } function flickrfield_process($element, $edit, $form_state, $form) { - $options = array(); - $options['photo_id'] = t("Photo"); - $options['set_id'] = t("Photoset"); - $element['type'] = array( - '#type' => 'select', - '#title' => t('Item Type'), - '#default_value' => !empty($element['#value']['type']) ? $element['#value']['type'] : '', - '#options' => $options, - ); - $element['id'] = array( + // Build the flickr URL from the flickr values in the database. + if (!empty($element['#value']['nsid']) && !empty($element['#value']['id'])) { + // We have a photo id. + if ($element['#value']['type'] == 'photo_id') { + $default_url = flickr_photo_page_url($element['#value']['nsid'], $element['#value']['id']); + } else { // .We have a set id. + $default_url = flickr_photoset_page_url($element['#value']['nsid'], $element['#value']['id']); + } + } else { // We have no id (thean an empty flickrfield). + $default_url = ''; + } + $element['url'] = array( '#type' => 'textfield', - '#title' => t('Id'), - '#default_value' => !empty($element['#value']['id']) ? $element['#value']['id'] : '', - ); - $element['nsid'] = array( - '#type' => 'textfield', - '#title' => t('User Id'), - '#default_value' => !empty($element['#value']['nsid']) ? $element['#value']['nsid'] : variable_get('flickr_default_userid', ''), - '#required' => $element['#required'], - '#description' => t("The user id of the Flickr user who owns the photos. If this is left blank, the sites's default user will be used. Current default id is @id.", array('@id' => variable_get('flickr_default_userid', ''))), + '#title' => 'Image or Photoset URL', + '#default_value' => $default_url, + '#description' => t('Enter the URL to the Flickr image or image set.'), ); return $element; } +function flickrfield_field($op, &$node, $field, &$items, $arg1, $arg2) { + switch ($op) { + case 'validate': + $form = $arg1; + if (!is_array($items)) { + return $items; + } + foreach ($items as $i => $item) { + if (empty($item['url'])) { + continue; + } + $info = flickr_get_info_by_url($item['url']); + if (!is_array($info)) { + form_set_error($field['field_name'], $info); + continue; + } + } + break; + + case 'presave': + foreach ($items as $i => $item) { + $info = flickr_get_info_by_url($item['url']); + if (!is_array($info)) { + // Input was validated above. If we got here, it was simply an empty + // field. + continue; + } + + $items[$i]['id'] = $info['id']; + if ($info['media'] == 'photo') { + $items[$i]['type'] = 'photo_id'; + $items[$i]['nsid'] = $info['owner']['nsid']; + } + else { + $items[$i]['type'] = 'set_id'; + $items[$i]['nsid'] = $info['owner']; + } + } + break; + } +} + function flickrfield_flickrid_process($element, $edit, $form_state, $form) { $element['flickrid'] = array( '#type' => 'textfield', @@ -156,7 +194,7 @@ function flickrfield_flickrid_process($element, $edit, $form_state, $form) { */ function flickrfield_content_is_empty($item, $field) { if ($field['type'] == 'flickrfield') { - return empty($item['id']); + return empty($item['id']) && empty($item['url']); } else if ($field['type'] == 'flickrfield_photoset') { return empty($item['flickrid']); } @@ -354,7 +392,7 @@ function theme_flickrfield_photoset($img, $photo_url, $formatter, $photo_data, $ if (module_exists('flickr_sets')) { $photos = flickr_set_load($photo_data['id']); - + foreach ((array) $photos['photoset']['photo'] as $photo) { //insert owner into $photo because theme_flickr_photo needs it $photo['owner'] = $photos['photoset']['owner']; @@ -370,7 +408,7 @@ function theme_flickrfield_photoset($img, $photo_url, $formatter, $photo_data, $ } } else { $title = is_array($photo_data['title']) ? $photo_data['title']['_content'] : $photo_data['title']; - + if (arg(0) == 'node' && arg(1) == $node->nid) { $output .= '
'. $img .'
'; } else { @@ -409,7 +447,7 @@ function theme_flickrfield_photoset_primaryphoto($img, $link, $set_url, $size, $ /** * Theme for the form element. - * + * * The form is already rendered by the child elements by the time it comes back here, * just group each delta grouping into its own fieldset. */ diff --git a/flickr.api.inc b/flickr.api.inc index 9056c36..7af4b3a 100644 --- a/flickr.api.inc +++ b/flickr.api.inc @@ -1,6 +1,6 @@ $v){ $encoded_params[] = urlencode($k) . '=' . urlencode($v); } - $url = FLICKR_REST_ENDPOINT .'?'. implode('&', $encoded_params); + $url = FLICKR_REST_ENDPOINT .'?'. implode('&', $encoded_params); // If it's a cachable request, try to load a cached value. if ($cacheable) { @@ -266,6 +266,64 @@ function flickr_photo_page_url($owner, $id = NULL) { } /** + * Given the URL to a flickr asset (set or photo), return info about that asset. + * + * @param string $url + * The URL to the flickr asset, e.g. as returned by flickr_photoset_page_url() + * or flickr_photo_page_url(). + * + * @return mixed + * string - If $url was empty or invalid, or Flickr API could not locate asset + * info, return an error string. + * array - Otherwise, return the info array from flickr_photoset_get_info() or + * flickr_photo_get_info() as applicable. + */ +function flickr_get_info_by_url($url, $reset = FALSE) { + static $urls; + // Don't bother checking the same URL twice in a single page request. + if ($urls[$url] && !$reset) { + return $urls[$url]; + } + if (empty($url)) { + $urls[$url] = t('Please enter a valid Flickr URL.'); + return $urls[$url]; + } + if (!valid_url($url)) { + $urls[$url] = t('Please enter a valid Flickr URL.'); + return $urls[$url]; + } + // Prepend the protocol if it's not there, so we can use parse_url(). + if (strpos($url, 'http') !== 0) { + $url = 'http://' . $url; + } + $parts = parse_url($url); + if (strpos($parts['host'], 'flickr') === FALSE) { + $urls[$url] = t('Please enter a valid Flickr URL.'); + return $urls[$url]; + } + // Now, we only care about the path. Sanity-check the number of path elements. + $parts = explode('/', trim($parts['path'], '/')); + if (count($parts) < 3 || ($parts[2] == 'sets' && count($parts) < 4)) { + $urls[$url] = t('Unable to retrieve Flickr image or set. Please check the URL.'); + return $urls[$url]; + } + // Now that we're reasonably sure the URL is the correct format, see if the + // API can tell us about the asset. + if ($parts[2] == 'sets') { + $set_id = $parts[3]; + $info = flickr_photoset_get_info($set_id); + $urls[$url] = $info ? $info : t('Unable to retrieve Flickr image or set. Please check the URL.'); + return $urls[$url]; + } + else { + $img_id = $parts[2]; + $info = flickr_photo_get_info($img_id); + $urls[$url] = $info ? $info : t('Unable to retrieve Flickr image or set. Please check the URL.'); + return $urls[$url]; + } +} + +/** * @param $owner * owner of the photoset * @param $id -- 1.7.9.5