View source
<?php
define('ML_FLICKR_DIR', 'images/flickr');
function ml_flickr_ml_image_source() {
return array(
'flickr' => array(
'label' => t('Flickr'),
'module' => 'ml_flickr',
'settings' => 'ml_flickr_settings',
),
);
}
function ml_flickr_flickr_source_form($form_state) {
$form = array();
$form['#description'] = t('Type in desired filter for showing results');
$metadata = ml_image_get_metadata();
foreach ($metadata as $field => $data) {
switch ($data['type']) {
case 'term':
$vocabulary = $data['vocabulary'];
if ($vocabulary->tags) {
$form[$field] = array(
'#type' => 'textfield',
'#title' => $data['label'],
'#description' => $data['description'],
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
'#maxlength' => 1024,
);
}
else {
$form[$field] = taxonomy_form($vocabulary->vid, array(), $data['description']);
$form[$field]['#title'] = $data['title'];
}
break;
}
}
return $form;
}
function ml_flickr_flickr_source_form_validate($form, &$form_state) {
$values = $form_state['values'];
}
function ml_flickr_flickr_source_form_submit($form, &$form_state) {
}
function ml_flickr_flickr_options_form($form_state) {
$form_values = $form_state['values']['flickr'];
$result = ml_flickr_search();
$form = media_library_modal_browse_form($form_state, $result['total']);
$images = array();
foreach ($result['photo'] as $image) {
if ($url = ml_flickr_build_url($image, 'thumbnail')) {
$image = ml_flickr_image_info($image['id']);
$img_tag = theme('image', $url, $image['title'], $image['title'], NULL, FALSE);
$form[] = array(
'#tree' => FALSE,
'choice' => array(
'#type' => 'radio',
'#title' => $image['title'],
'#return_value' => $image['id'],
'#prefix' => '<div class="ml-image-list-item">',
'#suffix' => $img_tag . '</div>',
),
);
$form['images'][$image['id']] = array(
'#type' => 'value',
'#value' => $image,
);
}
}
return $form;
}
function ml_flickr_flickr_options_form_validate($form, &$form_state) {
$image_id = current(array_filter($form_state['values']['choice']));
$image = $form_state['values']['images'][$image_id];
$sizes = ml_flickr_get_sizes($image['id']);
foreach ($sizes as $size) {
if ($size['width'] > 1000 || $size['height'] > 800) {
break;
}
$url = $size['source'];
}
if (!$url) {
form_set_error('choice', t('Could not find usable source image for option'));
}
}
function ml_flickr_flickr_options_form_submit($form, &$form_state) {
$image_id = current(array_filter($form_state['values']['choice']));
$image = $form_state['values']['images'][$image_id];
$sizes = ml_flickr_get_sizes($image['id']);
foreach ($sizes as $size) {
if ($size['width'] > 1000 || $size['height'] > 800) {
break;
}
$url = $size['source'];
}
if ($file = ml_flickr_save_image($url)) {
$form_state['media_obj']->filepath = $file->filepath;
$form_state['media_obj']->fid = $file->fid;
$form_state['media_obj']->filename = $file->filename;
$form_state['media_obj']->metatags = array(
'title' => $image['title'],
'author' => empty($image['owner']['realname']) ? $image['owner']['username'] : $image['owner']['realname'],
'copyright' => $image['license']['name'],
);
$metatags = (object) $form_state['media_obj']->metatags;
$metatags->fid = $file->fid;
$metatags->source = 'flickr';
ml_image_save_metatags($metatags);
}
}
function ml_flickr_settings($form_state) {
$form = array();
$form['ml_flickr_destination'] = array(
'#type' => 'textfield',
'#title' => t('Flickr Images Destination'),
'#description' => t('Directory to save images, relative to files dir. Defaults to "' . ML_FLICKR_DIR . '".'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ml_flickr_destination', ML_FLICKR_DIR),
);
return $form;
}
function ml_flickr_build_url($image, $size) {
$size = strtolower($size);
if (ml_flickr_build_url_validate($image)) {
$flickr_obj = ml_flickr_get_obj();
return $flickr_obj
->buildPhotoURL($image, $size);
}
}
function ml_flickr_build_url_validate($image) {
if (!empty($image['id']) && !empty($image['secret']) && !empty($image['server']) && !empty($image['farm'])) {
return TRUE;
}
return FALSE;
}
function ml_flickr_get_obj() {
static $flickr_obj;
if (!is_object($flickr_obj)) {
$flickr_obj = flickrapi_phpFlickr();
}
return $flickr_obj;
}
function ml_flickr_search() {
$limit = variable_get('media_library_limit', MEDIA_LIBRARY_LIMIT);
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$flickr_obj = ml_flickr_get_obj();
return $flickr_obj
->photos_search(array(
'tags' => 'cow',
'tag_mode' => 'all',
'media' => 'photos',
'per_page' => $limit,
'page' => $page + 1,
));
}
function ml_flickr_image_info($image_id) {
$flickr_obj = ml_flickr_get_obj();
$image = $flickr_obj
->photos_getInfo($image_id);
$license_info = $flickr_obj
->photos_licenses_getInfo();
$image['license'] = $license_info[$image['license']];
return $image;
}
function ml_flickr_get_sizes($image_id) {
$flickr_obj = ml_flickr_get_obj();
$sizes = $flickr_obj
->photos_getSizes($image_id);
return $sizes;
}
function ml_flickr_save_image($url) {
global $user;
$fd = fopen($url, 'rb');
if (!$fd) {
return;
}
$dir = variable_get('ml_flickr_destination', ML_FLICKR_DIR);
$dest = file_create_path($dir);
if (!file_check_directory($dest, TRUE)) {
return;
}
$filename = basename($url);
$file = new stdClass();
$file->filename = $filename;
$file->filemime = file_get_mimetype($file->filename);
$file->destination = file_destination(file_create_path($dest . '/' . $file->filename), FILE_EXISTS_REPLACE);
$file->filepath = $file->destination;
if ($fd_dest = fopen($file->filepath, 'w+')) {
stream_copy_to_stream($fd, $fd_dest);
fclose($fd);
fclose($fd_dest);
}
$file->uid = $user->uid;
$file->status = 1;
$file->timestamp = time();
$file->filesize = filesize($file->filepath);
drupal_write_record('files', $file);
return $file;
}