ml_image_basic.module in Media Library 6
This module aims to provide some basic sources for images to use with Media Library, such as navigating on existing files and uploading new images
File
ml_image/ml_image_basic/ml_image_basic.moduleView source
<?php
/**
* @file
* This module aims to provide some basic sources for images to use with
* Media Library, such as navigating on existing files and uploading new images
*/
/**
* Default images dir
*/
define('ML_IMAGE_BASIC_DIR', 'images');
/**
* ML Image Hooks
*/
/**
* Implementation of hook_ml_image_source()
*/
function ml_image_basic_ml_image_source() {
return array(
'existing' => array(
'label' => t('Existing images'),
'module' => 'ml_image_basic',
),
'upload' => array(
'label' => t('Upload image'),
'module' => 'ml_image_basic',
'settings' => 'ml_image_basic_settings',
),
);
}
/**
* Forms
*/
/**
* Form for checking existing images.
*/
function ml_image_basic_existing_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 {
// Extract terms belonging to the vocabulary in question.
$form[$field] = taxonomy_form($vocabulary->vid, array(), $data['description']);
$form[$field]['#title'] = $data['title'];
}
break;
}
}
$sources = ml_image_get_sources();
foreach ($sources as $source => $info) {
$sources[$source] = $info['label'];
}
unset($sources['existing']);
array_unshift($sources, t('Any'));
$form['source'] = array(
'#type' => 'select',
'#title' => t('Source'),
'#description' => t('Source of image'),
'#options' => $sources,
);
return $form;
}
/**
* Validate callback for ml_image_basic_existing_source_form()
*/
function ml_image_basic_existing_source_form_validate($form, &$form_state) {
$values = $form_state['values'];
}
/**
* Image selection stage
*/
function ml_image_basic_existing_options_form($form_state) {
$form_values = $form_state['values']['existing'];
if ($form_values['source']) {
$source = $form_values['source'];
}
$metadata = ml_image_get_metadata();
foreach ($metadata as $field => $data) {
switch ($data['type']) {
case 'term':
$vocabulary = $data['vocabulary'];
$vid = $vocabulary->vid;
// Free tagging vocabularies do not send their tids in the form,
// so we'll detect them here and process them independently.
$terms = array();
if ($vocabulary->tags) {
$typed_terms = drupal_explode_tags($form_values[$field]);
$inserted = array();
foreach ($typed_terms as $typed_term) {
// See if the term exists in the chosen vocabulary
// and return the tid; otherwise, add a new record.
$possibilities = taxonomy_get_term_by_name($typed_term);
$typed_term_tid = NULL;
// tid match, if any.
foreach ($possibilities as $possibility) {
if ($possibility->vid == $vid) {
$typed_term_tid = $possibility->tid;
}
}
if ($typed_term_tid) {
$terms[] = $typed_term_tid;
}
}
}
else {
$terms = $form_values[$field];
}
break;
}
}
$images = ml_image_basic_search($terms, $source);
// Build base form
$form = media_library_modal_browse_form($form_state);
// Build form elements
foreach ($images as $image) {
// TODO: check for imagecache thumb
$img_tag = theme('media_library_thumb', $image);
$form['choice'][] = array(
'#type' => 'radio',
'#title' => $image->title,
'#return_value' => $image->fid,
'#prefix' => '<div class="ml-image-list-item">',
'#suffix' => $img_tag . '</div>',
// TODO: use post-process to parse this
'#parents' => array(
'existing',
'choice',
),
);
$form['images'][$image->fid] = array(
'#type' => 'value',
'#value' => $image,
);
}
return $form;
}
/**
* Submit of image selection stage
*/
function ml_image_basic_existing_options_form_validate($form, &$form_state) {
if (!is_numeric($form_state['values']['choice']) || !$form_state['values']['choice']) {
form_set_error('choice', t('Please select an image!'));
}
}
/**
* Submit of image selection stage
*/
function ml_image_basic_existing_options_form_submit($form, &$form_state) {
$fid = $form_state['values']['choice'];
$image = ml_image_get_image($fid);
foreach ($image as $attr => $value) {
$form_state['media_obj']->{$attr} = $value;
}
}
/**
* Form for uploading images.
*/
function ml_image_basic_upload_source_form($form_state) {
$form = array();
$form['image_upload'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#size' => 40,
);
return $form;
}
/**
* Validate callback for ml_image_basic_upload_source_form()
*/
function ml_image_basic_upload_source_form_validate($form, &$form_state) {
$dir = variable_get('ml_image_basic_destination', ML_IMAGE_BASIC_DIR);
$dest = file_create_path($dir);
if (!file_check_directory($dest, TRUE)) {
$dest = file_directory_path() . '/' . $dir;
}
$validators = array(
'file_validate_is_image' => array(),
'ml_image_basic_upload_validate' => array(),
);
if ($file = file_save_upload('upload', $validators, $dest)) {
$form_state['media_obj']->file = $file;
}
else {
form_set_error('files[upload]', t('Please upload a file!'));
}
}
/**
* Submit callback for ml_image_basic_upload_source_form()
*/
function ml_image_basic_upload_source_form_submit($form, &$form_state) {
$file = $form_state['media_obj']->file;
$form_state['media_obj']->filepath = $file->filepath;
$form_state['media_obj']->fid = $file->fid;
$form_state['media_obj']->filemime = $file->filemime;
// TODO: extract metatags from image (yeah)
$form_state['media_obj']->metatags = array();
}
/**
* Function for upload options (second step) {
*/
function ml_image_basic_upload_options_form($form_state) {
$form = array();
$metadata = ml_image_get_metadata();
foreach ($metadata as $field => $data) {
switch ($data['type']) {
case 'textfield':
case 'textarea':
$form[$field] = array(
'#type' => $data['type'],
'#title' => $data['label'],
'#description' => $data['description'],
);
break;
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 {
// Extract terms belonging to the vocabulary in question.
$form[$field] = taxonomy_form($vocabulary->vid, array(), $data['description']);
$form[$field]['#title'] = $data['title'];
}
break;
}
}
return $form;
}
/**
* Submit callback for ml_image_basic_upload_options_form()
*/
function ml_image_basic_upload_options_form_submit($form, &$form_state) {
$metadata = ml_image_get_metadata();
if (!isset($form_state['media_obj']->metatags) || !is_array($form_state['media_obj']->metatags)) {
$form_state['media_obj']->metatags = array();
}
$metatags = new stdClass();
foreach ($metadata as $field => $data) {
switch ($data['type']) {
case 'textfield':
case 'textarea':
if (!empty($form_state['values'][$field])) {
$form_state['media_obj']->metatags[$field] = $form_state['values'][$field];
$metatags->{$field} = $form_state['values'][$field];
}
break;
case 'term':
$vocabulary = $data['vocabulary'];
$vid = $vocabulary->vid;
// Free tagging vocabularies do not send their tids in the form,
// so we'll detect them here and process them independently.
$terms = array();
if ($vocabulary->tags) {
$typed_terms = drupal_explode_tags($form_state['values'][$field]);
$inserted = array();
foreach ($typed_terms as $typed_term) {
// See if the term exists in the chosen vocabulary
// and return the tid; otherwise, add a new record.
$possibilities = taxonomy_get_term_by_name($typed_term);
$typed_term_tid = NULL;
// tid match, if any.
foreach ($possibilities as $possibility) {
if ($possibility->vid == $vid) {
$typed_term_tid = $possibility->tid;
}
}
if (!$typed_term_tid) {
$edit = array(
'vid' => $vid,
'name' => $typed_term,
);
$status = taxonomy_save_term($edit);
$typed_term_tid = $edit['tid'];
}
$terms[$typed_term_tid] = $typed_term;
}
}
else {
$terms = $form_state['values'][$field];
}
// Save original string to object, ready to be used
$form_state['media_obj']->metatags[$field] = $form_state['values'][$field];
// Save array version to object being inserted
$metatags->{$field} = $terms;
break;
}
}
$metatags->fid = $form_state['media_obj']->fid;
$metatags->source = 'upload';
// Save metatags do database
ml_image_save_metatags($metatags);
}
/**
* Settings form
*/
function ml_image_basic_settings($form_state) {
$form = array();
$form['ml_image_basic_destination'] = array(
'#type' => 'textfield',
'#title' => t('Images Destination'),
'#description' => t('Directory to save images, relative to files dir. Defaults to "' . ML_IMAGE_BASIC_DIR . '".'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => variable_get('ml_image_basic_destination', ML_IMAGE_BASIC_DIR),
);
return $form;
}
/**
* Helpers and callbacks
*/
/**
* Validates filename before saving upload
*/
function ml_image_basic_upload_validate(&$file) {
$messages = array();
// Use transliteration to clean filenames
if (module_exists('transliteration')) {
module_load_include('inc', 'transliteration');
$path_args = explode('/', $file->destination);
$filename = transliteration_clean_filename(array_pop($path_args));
array_push($path_args, $filename);
$file->destination = implode('/', $path_args);
}
return $messages;
}
/**
* Search the images
*/
function ml_image_basic_search($terms, $source) {
// Get variables
$limit = variable_get('media_library_limit', MEDIA_LIBRARY_LIMIT);
$images = ml_image_get_images($terms, $source, $limit);
return $images;
}
Functions
Constants
Name | Description |
---|---|
ML_IMAGE_BASIC_DIR | Default images dir |