imagepicker.module in Image Picker 5
Same filename and directory in other branches
Enables permitted roles to upload images for insertion into configured nodes.
File
imagepicker.moduleView source
<?php
// $Name$
/**
* @file
* Enables permitted roles to upload images for insertion into configured nodes.
*/
// If someone is using PHP version < 5.xx, this will solve the problem, where
// function htmlspecialchars_decode() doesn't exist
if (!function_exists('htmlspecialchars_decode')) {
function htmlspecialchars_decode($text) {
return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
}
/**
* Implementation of hook_help().
*/
function imagepicker_help($section) {
switch ($section) {
case 'admin/help#imagepicker':
$content = '<p>' . t('Adds an advanced image upload form under the node body part.') . '</p>';
return $content;
}
}
/**
* Implementation of hook_perm().
*/
function imagepicker_perm() {
return array(
'administer imagepicker',
'use imagepicker',
'access own imagepicker',
);
}
/**
* Implementation of hook_menu().
*/
function imagepicker_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'imagepicker',
'title' => t('Image picker'),
'callback' => 'imagepicker_upload',
'access' => user_access('use imagepicker'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'imagepicker/upload',
'title' => t('Upload'),
'access' => user_access('use imagepicker'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$items[] = array(
'path' => 'imagepicker/browse',
'title' => t('Browse'),
'callback' => 'imagepicker_browse',
'access' => user_access('use imagepicker'),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items[] = array(
'path' => 'imagepicker/edit',
'title' => t('Edit image'),
'callback' => 'imagepicker_image_edit',
'access' => user_access('use imagepicker'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'imagepicker/image',
'title' => t('Imagepicker'),
'callback' => 'imagepicker_image_page',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
// admin settings
$items[] = array(
'path' => 'admin/settings/imagepicker',
'title' => t('Imagepicker'),
'description' => t('Imagepicker settings.'),
'access' => user_access('administer imagepicker'),
'callback' => 'drupal_get_form',
'callback arguments' => 'imagepicker_settings',
'type' => MENU_NORMAL_ITEM,
);
}
// in my account
if (variable_get('imagepicker_account_enabled', 1)) {
$items[] = array(
'path' => 'user/' . $user->uid . '/imagepicker',
'title' => t('My imagepicker'),
'description' => t('Manage your imagepicker files.'),
'callback' => 'imagepicker_user_page',
'access' => user_access('access own imagepicker'),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function imagepicker_form_alter($form_id, &$form) {
$node = $form['#node'];
$node_types = node_get_types('names');
$node_type = in_array($node->type, variable_get('imagepicker_node_types_enabled', $node_types), TRUE);
// comment
$comment = FALSE;
$weight = 1;
if (module_exists('comment') && variable_get('imagepicker_comment_enabled', 0) && preg_match('/comment_form$/i', $form_id)) {
$comment = TRUE;
$weight = 1;
}
if (user_access('use imagepicker')) {
if ($node_type && preg_match('/node_form$/i', $form_id) || $comment) {
$form['body_filter']['file_upload'] = array(
'#type' => 'fieldset',
'#title' => t('Image picker'),
'#collapsible' => 1,
'#collapsed' => variable_get('imagepicker_advanced_collapsed', 0),
'#weight' => $weight,
'#validate' => array(),
'#theme' => 'imagepicker_iframe',
1 => array(),
);
$form['body_filter']['#prefix'] = '<a name="body_hash"></a>' . $form['body_filter']['#prefix'];
}
}
}
function theme_imagepicker_iframe() {
return '<iframe id="imagepicker" style="width: ' . variable_get('imagepicker_advanced_iframe_width', "100%") . '; height: ' . variable_get('imagepicker_advanced_iframe_height', 500) . 'px; border: ' . variable_get('imagepicker_advanced_iframe_border', "0") . ';" src="' . url('imagepicker') . '">Imagepicker requires iframe support.</iframe>';
}
/**
* Menu callback; presents the upload form for imagepicker
*/
function imagepicker_upload() {
variable_del('imagepicker_advanced_browser_pagestart');
$content = drupal_get_form('imagepicker_upload_form');
theme('imagepicker', $content);
}
function imagepicker_upload_form($account = FALSE) {
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['file_upload'] = array(
'#type' => 'file',
'#title' => t('Image file'),
'#description' => t('Browse your computer for image file'),
'#required' => TRUE,
'#value' => 1,
);
$form['thumb'] = array(
'#type' => 'textfield',
'#title' => t('Thumbnail size'),
'#size' => 10,
'#default_value' => variable_get('imagepicker_default_thumbnail_size', 100),
'#description' => t('Size in pixels of thumbnail\'s bigger side'),
'#required' => TRUE,
);
$form['scale'] = array(
'#type' => 'textfield',
'#title' => t('Scale image'),
'#size' => 10,
'#default_value' => variable_get('imagepicker_default_scale', ''),
'#description' => t('Scale image to this size in pixels if not left empty'),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Add title for your image'),
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#rows' => 2,
'#cols' => 80,
'#description' => t('Add description for your image'),
);
if ($account) {
$form['account'] = array(
'#type' => 'hidden',
'#value' => 1,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
);
return $form;
}
/**
* Validate form
*/
function imagepicker_upload_form_validate($form_id, $form_values) {
foreach ($form_values as $name => $value) {
$value = trim($value);
switch ($name) {
case 'file_upload':
if (empty($_FILES['files']['name']['file_upload'])) {
form_set_error($name, t('Image file field is required.'));
}
elseif (!isset($_FILES['files']['tmp_name']['file_upload']) || !file_exists($_FILES['files']['tmp_name']['file_upload'])) {
form_set_error($name, t('Error while uploading file.'));
}
elseif (!image_get_info($_FILES['files']['tmp_name']['file_upload'])) {
form_set_error($name, t('Uploaded file is not an image.'));
}
elseif (!imagepicker_get_uploaded_file_extension('file_upload')) {
form_set_error($name, t('Only .jpg, .gif and .png image files are accepted.'));
}
break;
case 'thumb':
if (!preg_match('/^[0-9]{1,3}$/', $value) || $value <= 0) {
form_set_error($name, t('Thumbnail size should be an integer between 1 and 999.'));
}
break;
case 'scale':
if (!preg_match('/^[0-9]{0,3}$/', $value)) {
form_set_error($name, t('Scale value should be an integer between 1 and 999 or leave it empty if you don\'t want to scale your image.'));
}
break;
}
}
}
/**
* Submit form
*/
function imagepicker_upload_form_submit($form_id, $form_values) {
if ($form_values['op'] == t('Upload')) {
global $user;
$destination = imagepicker_get_path(FALSE, TRUE);
$thumbsdir = $destination . 'thumbs';
$browserdir = $destination . 'browser';
if (file_check_directory($destination, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE)) {
// Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
$destination .= DIRECTORY_SEPARATOR;
$thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
$browserdir = $browserdir . DIRECTORY_SEPARATOR;
$maxthumbsize = $form_values['thumb'] ? $form_values['thumb'] : 100;
$scaleto = $form_values['scale'] ? $form_values['scale'] : FALSE;
if (!$scaleto) {
// Use $path instead of original $destination variable cause this
// variable's value will be changed during copying file, so we won't
// loose it.
$path = $destination;
$imagemoved = imagepicker_copy_uploaded_file($path, 'file_upload');
$file = basename($path);
}
else {
$source = $_FILES['files']['tmp_name']['file_upload'];
$file = imagepicker_get_uploaded_file_name($destination, 'file_upload');
$imagescaled = imagepicker_scale_image($source, $destination . $file, $scaleto);
}
if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {
// Source file should still be an uploaded one, as scaled image
// might have some watermarks etc. from drupal's filters/hooks.
$source = $_FILES['files']['tmp_name']['file_upload'];
if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
$nextimgid = db_next_id('{imagepicker}_img_id');
$title = htmlspecialchars($form_values['title']);
$description = htmlspecialchars($form_values['description']);
if (db_query("INSERT INTO {imagepicker} (uid, img_name, img_title, img_description) VALUES ('%d', '%s', '%s', '%s')", array(
$user->uid,
$file,
$title,
$description,
))) {
drupal_set_message(t('Image was successfully uploaded.'));
if ($form_values['account']) {
drupal_goto('user/' . $user->uid . '/imagepicker/browse');
}
else {
drupal_goto('imagepicker/browse/' . $nextimgid);
}
}
else {
file_delete($thumbsdir . $file);
file_delete($browserdir . $file);
drupal_set_message(t('Error while saving information to database for uploaded image.'), 'error');
}
}
else {
drupal_set_message(t('Error while creating a thumbnail for uploaded image.'), 'error');
}
}
else {
if (!$scaleto && !$imagemoved) {
drupal_set_message(t('Error while moving uploaded file to its destination.'), 'error');
}
else {
drupal_set_message(t('Error while scaling uploaded file.'), 'error');
}
}
file_delete($destination . $file);
}
else {
drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
}
}
if ($form_values['account']) {
drupal_goto('user/' . $user->uid . '/imagepicker');
}
else {
drupal_goto('imagepicker');
}
}
function imagepicker_get_path($url = FALSE, $userdir = FALSE) {
global $user, $base_url;
$dirsep = !$url ? DIRECTORY_SEPARATOR : '/';
if (!$url) {
$path = str_replace('/', DIRECTORY_SEPARATOR, getcwd());
}
else {
if (variable_get('imagepicker_use_full_url', 1)) {
$path = $base_url;
}
else {
$path = base_path();
$path = preg_replace("/\\/\$/", "", $path);
}
}
$path .= $dirsep . file_directory_path() . $dirsep . 'imagepicker' . $dirsep;
if ($userdir) {
$username = !is_array($userdir) ? $user->name : $userdir['name'];
$firstletter = strtolower(substr($username, 0, 1));
$firstletter = preg_match('/^[a-z]$/', $firstletter) ? $firstletter : 'others';
$path .= $firstletter . $dirsep . $username . $dirsep;
}
return $path;
}
function imagepicker_copy_uploaded_file(&$destination, $name) {
$source = $_FILES['files']['tmp_name'][$name];
if (file_copy($source, $destination, FILE_EXISTS_RENAME)) {
// Fix bug in drupal's file_copy function which uses '/' instead of
// DIRECTORY_SEPARATOR for making directories. This causes problems on
// Windows mashines.
$source = str_replace('/', DIRECTORY_SEPARATOR, $source);
$file = imagepicker_get_uploaded_file_name($destination, $name);
$destination = $destination . $file;
return @rename($source, $destination);
}
return FALSE;
}
function imagepicker_get_uploaded_file_extension($name) {
switch ($_FILES['files']['type'][$name]) {
case 'image/pjpeg':
// "What genius at microsoft decided to rename the mime type for jpgs?"
// Thats a nice phrase I have found about this mime type :) Wonder what
// am I talking about? Try to upload some type of jpg image via IE7.
// Don't know if it's the same with IE6, but IE7 might give you a mime
// type of image/pjpeg. So lets just treat this 'progressive jpg' as a
// normal jpg image.
case 'image/jpeg':
$fileext = '.jpg';
break;
case 'image/gif':
$fileext = '.gif';
break;
case 'image/png':
$fileext = '.png';
break;
default:
$fileext = '';
}
return $fileext;
}
function imagepicker_get_uploaded_file_name($destination, $name) {
$fileext = imagepicker_get_uploaded_file_extension($name);
if (FALSE !== strpos($_FILES['files']['name'][$name], '.')) {
$filename = substr($_FILES['files']['name'][$name], 0, strrpos($_FILES['files']['name'][$name], '.'));
}
else {
$filename = $_FILES['files']['name'][$name];
}
$file = $filename . $fileext;
$i = 0;
while (file_exists($destination . $file)) {
$i++;
$file = $filename . '_' . $i . $fileext;
}
return $file;
}
function imagepicker_get_image_path($img, $type = 'browser') {
$imgbasedir = imagepicker_get_path(FALSE, TRUE);
switch ($type) {
case 'browser':
if (file_exists($imgbasedir . 'browser' . DIRECTORY_SEPARATOR . $img['img_name'])) {
$imgpath = imagepicker_get_path(TRUE, TRUE) . 'browser/' . $img['img_name'];
}
elseif (file_exists($imgbasedir . 'thumbs' . DIRECTORY_SEPARATOR . $img['img_name'])) {
$imgpath = imagepicker_get_path(TRUE, TRUE) . 'thumbs/' . $img['img_name'];
}
break;
case 'full':
if (file_exists($imgbasedir . $img['img_name'])) {
$imgpath = imagepicker_get_path(TRUE, TRUE) . $img['img_name'];
}
break;
case 'thumb':
default:
if (file_exists($imgbasedir . 'thumbs' . DIRECTORY_SEPARATOR . $img['img_name'])) {
$imgpath = imagepicker_get_path(TRUE, TRUE) . 'thumbs/' . $img['img_name'];
}
elseif (file_exists($imgbasedir . 'browser' . DIRECTORY_SEPARATOR . $img['img_name'])) {
$imgpath = imagepicker_get_path(TRUE, TRUE) . 'browser/' . $img['img_name'];
}
break;
}
return $imgpath ? $imgpath : '';
}
function imagepicker_scale_image($source, $destination, $maxsize) {
$info = image_get_info($source);
$width = $maxsize >= $info['width'] ? $info['width'] : $maxsize;
$height = $maxsize >= $info['height'] ? $info['height'] : $maxsize;
$aspect = $info['height'] / $info['width'];
if ($aspect < $height / $width) {
$width = (int) min($width, $info['width']);
$height = (int) round($width * $aspect);
}
else {
$height = (int) min($height, $info['height']);
$width = (int) round($height / $aspect);
}
return image_toolkit_invoke('resize', array(
$source,
$destination,
$width,
$height,
));
}
function imagepicker_browse() {
if (arg(2)) {
imagepicker_image_select();
exit;
}
$content = _imagepicker_browse();
theme('imagepicker', $content);
}
function imagepicker_image_select() {
global $user;
$img = _imagepicker_get_img(arg(2));
if ($img) {
drupal_add_js(imagepicker_js($img), 'inline');
$imgpath = imagepicker_get_image_path($img, 'browser');
$content = '<div class="imgp_help">' . t('Choose the settings you want, place the cursor in the Body box above and Insert image.') . '</div>';
if ($img['img_title']) {
$content .= '<div id="imgp_img_holder_title">' . check_plain($img['img_title']) . '</div>';
}
if ($img['img_description']) {
$content .= '<div id="imgp_img_holder_description">' . $img['img_description'] . '</div>';
}
$content .= '<div id="imgp_img_holder">';
$content .= '<img id="imgp_img" src="' . $imgpath . '" alt="' . check_plain($img['img_title']) . '" title="' . $img['img_name'] . '" />';
$content .= '</div>';
$content .= drupal_get_form('imagepicker_image_form');
// groups
if (imagepicker_get_groups()) {
$content .= drupal_get_form('imagepicker_group_images_form', $img['img_id']);
}
}
else {
drupal_set_message(t('Image not found.'), 'error');
$content = '';
}
theme('imagepicker', $content);
}
function imagepicker_image_form() {
$showoptions = array(
'full' => t('Full size'),
'thumb' => t('Thumbnail'),
'title' => t('Title'),
);
$linkoptions = array(
'none' => t('None'),
'file' => t('File'),
'page' => t('Page'),
);
if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
$linkoptions['lightbox'] = t('Lightbox');
}
if (variable_get('imagepicker_default_align_show', 1)) {
$alignoptions = array(
'none' => t('None'),
'fleft' => t('Float Left'),
'fright' => t('Float right'),
);
$form['align'] = array(
'#type' => 'radios',
'#title' => t('Align'),
'#default_value' => variable_get('imagepicker_insert_defaults_align', 'none'),
'#options' => $alignoptions,
'#description' => '',
'#prefix' => '<div class="imgp_img_options">',
'#suffix' => '</div>',
);
}
$form['show'] = array(
'#type' => 'radios',
'#id' => 'show',
'#title' => t('Show'),
'#default_value' => variable_get('imagepicker_insert_defaults_show', 'full'),
'#options' => $showoptions,
'#description' => '',
'#prefix' => '<div class="imgp_img_options">',
'#suffix' => '</div>',
);
$form['link'] = array(
'#type' => 'radios',
'#title' => t('Link'),
'#default_value' => variable_get('imagepicker_insert_defaults_link', 'none'),
'#options' => $linkoptions,
'#description' => '',
'#prefix' => '<div class="imgp_img_options">',
'#suffix' => '</div>',
);
$form['insert'] = array(
'#type' => 'button',
'#value' => t('Insert image'),
'#prefix' => '<div id="imgp_controls">',
'#attributes' => array(
'onclick' => 'imagepickerInsert(this); return FALSE;',
),
);
$form['edit'] = array(
'#type' => 'submit',
'#value' => t('Edit image'),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete image'),
'#suffix' => '</div>',
);
return $form;
}
/**
* Submit form
*/
function imagepicker_image_form_submit($form_id, $form_values) {
if ($form_values['op'] == t('Delete image')) {
imagepicker_image_delete();
}
elseif ($form_values['op'] == t('Edit image')) {
drupal_goto('imagepicker/edit/' . arg(2));
}
}
function imagepicker_image_delete($account = FALSE) {
global $user;
if ($account) {
$img_id = arg(4);
}
else {
$img_id = arg(2);
}
$img = _imagepicker_get_img($img_id);
if ($img) {
$destination = imagepicker_get_path(FALSE, TRUE);
$thumbsdir = $destination . 'thumbs' . DIRECTORY_SEPARATOR;
$browserdir = $destination . 'browser' . DIRECTORY_SEPARATOR;
file_delete($destination . $img['img_name']);
file_delete($thumbsdir . $img['img_name']);
file_delete($browserdir . $img['img_name']);
if (db_query("DELETE FROM {imagepicker} WHERE uid = '%d' AND img_id = '%d'", array(
$user->uid,
$img_id,
))) {
// groups entries
db_query("DELETE FROM {imagepicker_group_images} WHERE img_id = '%d'", array(
$img_id,
));
drupal_set_message(t('Image was successfully deleted'));
}
else {
drupal_set_message(t('Error while trying to delete your image from database.'));
}
}
else {
drupal_set_message(t('Image not found.'), 'error');
}
if ($account) {
drupal_goto('user/' . $user->uid . '/imagepicker/browse');
}
else {
drupal_goto('imagepicker/browse');
}
}
/**
* Menu callback; fetches the image edit form for imagepicker
*/
function imagepicker_image_edit() {
$content = _imagepicker_edit_img(arg(2));
theme('imagepicker', $content);
}
function imagepicker_edit_form(&$img, $account = FALSE) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Edit title of your image'),
'#default_value' => htmlspecialchars_decode($img['img_title']),
'#prefix' => '<div id="imgp_edit_form">',
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#rows' => 2,
'#cols' => 80,
'#description' => t('Edit description of your image'),
'#default_value' => htmlspecialchars_decode($img['img_description']),
'#suffix' => '</div>',
);
if ($account) {
$form['account'] = array(
'#type' => 'hidden',
'#value' => 1,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#prefix' => '<div id="imgp_controls">',
);
$form['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#suffix' => '</div>',
);
return $form;
}
/**
* Submit form
*/
function imagepicker_edit_form_submit($form_id, $form_values) {
if ($form_values['op'] == t('Submit')) {
global $user;
if ($form_values['account']) {
$img_id = arg(4);
}
else {
$img_id = arg(2);
}
$img = _imagepicker_get_img($img_id);
if ($img) {
$title = htmlspecialchars($form_values['title']);
$description = htmlspecialchars($form_values['description']);
if (db_query("UPDATE {imagepicker} SET img_title = '%s', img_description = '%s' WHERE img_id = '%d'", array(
$title,
$description,
$img_id,
))) {
drupal_set_message(t('Image was successfully updated.'));
if ($form_values['account']) {
drupal_goto('user/' . $user->uid . '/imagepicker/browse');
}
else {
drupal_goto('imagepicker/browse/' . $img_id);
}
}
else {
drupal_set_message(t('Error while updating image.'), 'error');
}
}
else {
drupal_set_message(t('Image not found.'), 'error');
}
}
if ($form_values['account']) {
drupal_goto('user/' . $user->uid . '/imagepicker/browse');
}
drupal_goto('imagepicker/browse');
}
/**
* Menu callback; presents the image page for imagepicker
*/
function imagepicker_image_page() {
global $base_url;
$content = '';
$result = db_query_range("SELECT i.*, u.name FROM {imagepicker} AS i JOIN {users} AS u USING (uid) WHERE img_id = '%d'", arg(2), 0, 1);
$img = db_fetch_array($result);
if (!is_array($img) || !count($img)) {
drupal_set_message(t('Image not found.'), 'error');
$content = '';
}
else {
$path = drupal_get_path('module', 'imagepicker');
drupal_add_css($path . '/imagepicker.css');
drupal_set_title(check_plain($img['img_title']));
$imgsrc = imagepicker_get_path(TRUE, $img) . $img['img_name'];
$content = '<div id="imgp_page"><div id="imgp_page_img">';
$content .= '<a href="' . $imgsrc . '" alt="' . check_plain($img['img_title']) . '" target="_blank">';
$content .= '<img src="' . $imgsrc . '" alt="' . $img['img_title'] . '" />';
$content .= '</a></div><div>' . nl2br($img['img_description']) . '</div>';
if (variable_get('imagepicker_default_pagelink', 1)) {
$content .= '<div><a href="#" onclick="history.back()">' . t('Return to page') . '</a></div>';
}
$content .= '</div>';
}
return $content;
}
function theme_imagepicker(&$content) {
$head_title = drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_name', 'Drupal');
$path = drupal_get_path('module', 'imagepicker');
drupal_add_css($path . '/imagepicker.css');
$styles = drupal_get_css();
$scripts = drupal_get_js();
$tabs = theme('menu_local_tasks');
$messages = imagepicker_strip_messages(theme('status_messages'));
$template = 'imagepicker.tpl.php';
$defaulttemplate = variable_get('theme_default', '');
if ($defaulttemplate) {
$templatepath = drupal_get_path('theme', $defaulttemplate);
if (file_exists($templatepath . '/' . $template)) {
$template = $templatepath . '/' . $template;
}
}
include $template;
drupal_page_footer();
exit;
}
// There is not need to inform users, that directory structure has been created
// and show them all paths... So lets strip these messages if there are any.
function imagepicker_strip_messages($msg) {
if ($msg) {
$dirsep = DIRECTORY_SEPARATOR == '\\' ? '\\\\' : '\\/';
$pattern = '/<li>.*' . $dirsep . 'imagepicker' . $dirsep . '.*<\\/li>/i';
$msg = preg_replace($pattern, '', $msg);
}
return $msg;
}
function imagepicker_js($img) {
$ret = "\n function imagepickerInsert(button) {\n // Get the form element\n var imgpForm = document.getElementById('imagepicker-image-form');\n if (imgpForm) {\n var imgpShow = 'thumb';\n var imgpLink = 'file';\n var imgpAlign = 'none';\n var imgpImagePath;\n var imgpImageTitle = '" . ($img['img_title'] ? addslashes(check_plain($img['img_title'])) : t('Image')) . "';\n var imgpFileLink = '" . imagepicker_get_image_path($img, 'full') . "';\n var imgpThumbLink = '" . imagepicker_get_image_path($img, 'thumb') . "';\n var imgpPageLink = '" . url('imagepicker/image/' . $img['img_id']) . "';\n var imgpImageElement;\n var imgpLinkElement;\n var imgpImageStyle;\n var imgpInsertion;\n var i;\n\n // Get show value\n for (i = 0; i < imgpForm.show.length; i++) {\n if(imgpForm.show[i].checked) {\n var imgpShow = imgpForm.show[i].value\n }\n }\n // Get link value\n for (i = 0; i < imgpForm.link.length; i++) {\n if(imgpForm.link[i].checked) {\n var imgpLink = imgpForm.link[i].value\n }\n }\n";
if (variable_get('imagepicker_default_align_show', 1)) {
$ret .= "\n // Get align value\n for (i = 0; i < imgpForm.align.length; i++) {\n if(imgpForm.align[i].checked) {\n var imgpAlign = imgpForm.align[i].value\n }\n }\n\n // Create a style for image holder\n switch (imgpAlign) {\n case 'fleft':\n imgpImageStyle = '" . variable_get('imagepicker_default_fleft', 'style="float: left"') . "';\n break;\n\n case 'fright':\n imgpImageStyle = '" . variable_get('imagepicker_default_fright', 'style="float: right"') . "';\n break;\n\n case 'none':\n default:\n imgpImageStyle = '';\n break;\n }\n";
}
else {
$ret .= "\n imgpImageStyle = '';\n";
}
$ret .= "\n switch (imgpShow) {\n case 'full': imgpImagePath = imgpFileLink; break;\n case 'title': imgpImagePath = ''; break;\n case 'thumb':\n default: imgpImagePath = imgpThumbLink; break;\n }\n\n // Create an image or span (containing title) HTML string\n if (imgpImagePath) {\n imgpImageElement = '<img src=\"'+imgpImagePath+'\" alt=\"'+imgpImageTitle+'\" ' + imgpImageStyle + ' \\/>';\n }\n else {\n imgpImageElement = '<span>'+imgpImageTitle+'<\\/span>'\n }\n\n // Create a link HTML string\n switch (imgpLink) {\n case 'none': imgpLinkElement = '%imgpImageElement%'; break;\n case 'page': imgpLinkElement = '<a href=\"'+imgpPageLink+'\" title=\"'+imgpImageTitle+'\" >%imgpImageElement%<\\/a>'; break;\n case 'file': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n";
if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
$ret .= "\n case 'lightbox': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" rel=\"" . variable_get('imagepicker_lightbox2_insert', 'lightbox') . "\" >%imgpImageElement%<\\/a>'; break;\n";
}
$ret .= "\n default: imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n }\n // Create a HTML string which should be inserted in the node body\n imgpInsertion = imgpLinkElement.replace('%imgpImageElement%', imgpImageElement);\n\n // Get the parent window of imagepicker iframe\n var win = window.opener ? window.opener : window.dialogArguments;\n if (!win) {\n win = top;\n }\n //var isTinyMCE = win.document.getElementById('mce_editor_0'); // buggy\n var isTinyMCE = win.tinyMCE; // Will be undefined if tinyMCE isn't loaded. This isn't a sure-proof way of knowing if tinyMCE is loaded into a field, but it works.\n if (isTinyMCE) {\n win.tinyMCE.execCommand('mceInsertContent', false, imgpInsertion);\n }\n else {\n var nodeBody = win.document.getElementById('edit-body');\n var commentBody = win.document.getElementById('edit-comment');\n if (nodeBody) {\n insertAtCursor(nodeBody, imgpInsertion);\n }\n if (commentBody) {\n insertAtCursor(commentBody, imgpInsertion);\n }\n }\n win.location.hash='body_hash';\n }\n }\n\n // Copy pasted from internet...\n function insertAtCursor(myField, myValue) {\n //IE support\n if (document.selection) {\n myField.focus();\n\n //in effect we are creating a text range with zero\n //length at the cursor location and replacing it\n //with myValue\n sel = document.selection.createRange();\n sel.text = myValue;\n }\n\n //Mozilla/Firefox/Netscape 7+ support\n else if (myField.selectionStart || myField.selectionStart == '0') {\n\n //Here we get the start and end points of the\n //selection. Then we create substrings up to the\n //start of the selection and from the end point\n //of the selection to the end of the field value.\n //Then we concatenate the first substring, myValue,\n //and the second substring to get the new value.\n var startPos = myField.selectionStart;\n var endPos = myField.selectionEnd;\n myField.value = myField.value.substring(0, startPos)+ myValue + myField.value.substring(endPos, myField.value.length);\n\n }\n else {\n myField.value += myValue;\n }\n }\n";
return $ret;
}
/**
* Menu callback; presents the settings form for imagepicker
*/
function imagepicker_settings() {
// default settings
$form['imagepicker_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Set imagepicker settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['imagepicker_settings']['imagepicker_default_browser_thumbnail_size'] = array(
'#type' => 'textfield',
'#title' => t('Default Browser Thumbnail Size'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_default_browser_thumbnail_size', 100),
'#description' => t('Configure the default browser thumbnail size'),
);
$node_types = node_get_types('names');
$form['imagepicker_settings']['imagepicker_node_types_enabled'] = array(
'#type' => 'checkboxes',
'#title' => t('Node Types'),
'#description' => t('Set the node types you want to enable Imagepicker for.'),
'#default_value' => variable_get('imagepicker_node_types_enabled', array_keys($node_types)),
'#options' => $node_types,
);
if (module_exists('comment')) {
$form['imagepicker_settings']['imagepicker_comment_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Comments'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_comment_enabled', 0),
'#description' => t('Setting this option enables Imagepicker in comments.'),
);
}
$form['imagepicker_settings']['imagepicker_default_pagelink'] = array(
'#type' => 'checkbox',
'#title' => t('Show return link in page'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_default_pagelink', 1),
'#description' => t('Setting this option will add a link back to the thumbnail when using the page option. Uses javascript history(back).'),
);
$form['imagepicker_settings']['imagepicker_account_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Imagepicker in My Account'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_account_enabled', 1),
'#description' => t('Setting this option enables Imagepicker in My Account.'),
);
// default options
$form['imagepicker_defaults'] = array(
'#type' => 'fieldset',
'#title' => t('Set imagepicker defaults'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['imagepicker_defaults']['imagepicker_default_thumbnail_size'] = array(
'#type' => 'textfield',
'#title' => t('Default Thumbnail Size'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_default_thumbnail_size', 100),
'#description' => t('Configure the default thumbnail size'),
);
$form['imagepicker_defaults']['imagepicker_default_scale'] = array(
'#type' => 'textfield',
'#title' => t('Default Scale'),
'#size' => 10,
'#required' => FALSE,
'#default_value' => variable_get('imagepicker_default_scale', ''),
'#description' => t('Configure the default scale. leave empty for no default scale'),
);
// insert settings
$form['imagepicker_insert_defaults'] = array(
'#type' => 'fieldset',
'#title' => t('Set imagepicker insert defaults'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => '',
);
$showoptions = array(
'full' => t('Full size'),
'thumb' => t('Thumbnail'),
'title' => t('Title'),
);
$linkoptions = array(
'none' => t('None'),
'file' => t('File'),
'page' => t('Page'),
);
if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
$linkoptions['lightbox'] = t('Lightbox');
}
$alignoptions = array(
'none' => t('None'),
'fleft' => t('Float Left'),
'fright' => t('Float right'),
);
$form['imagepicker_insert_defaults']['imagepicker_insert_defaults_align'] = array(
'#type' => 'radios',
'#title' => t('Align'),
'#default_value' => 'none',
'#options' => $alignoptions,
'#description' => '',
'#default_value' => variable_get('imagepicker_insert_defaults_align', 'none'),
);
$form['imagepicker_insert_defaults']['imagepicker_insert_defaults_show'] = array(
'#type' => 'radios',
'#id' => 'show',
'#title' => t('Show'),
'#default_value' => 'full',
'#options' => $showoptions,
'#description' => '',
'#default_value' => variable_get('imagepicker_insert_defaults_show', 'full'),
);
$form['imagepicker_insert_defaults']['imagepicker_insert_defaults_link'] = array(
'#type' => 'radios',
'#title' => t('Link'),
'#default_value' => 'none',
'#options' => $linkoptions,
'#description' => '',
'#default_value' => variable_get('imagepicker_insert_defaults_link', 'none'),
);
// alignment settings
$form['imagepicker_align'] = array(
'#type' => 'fieldset',
'#title' => t('Image alignment settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['imagepicker_align']['imagepicker_default_align_show'] = array(
'#type' => 'checkbox',
'#title' => t('Show Alignment options'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_default_align_show', 1),
'#description' => t('Unsetting this option will remove the alignment options from the insert page.'),
);
$form['imagepicker_align']['imagepicker_default_fleft'] = array(
'#type' => 'textfield',
'#title' => t('Default Float left code'),
'#size' => 25,
'#maxlength' => 50,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_default_fleft', 'style="float: left"'),
'#description' => t('Configure the default code used for float left styling.'),
);
$form['imagepicker_align']['imagepicker_default_fright'] = array(
'#type' => 'textfield',
'#title' => t('Default Float right code'),
'#size' => 25,
'#maxlength' => 50,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_default_fright', 'style="float: right"'),
'#description' => t('Configure the default code used for float right styling.'),
);
if (module_exists('lightbox2')) {
// lightbox2 integration
$form['imagepicker_lightbox2'] = array(
'#type' => 'fieldset',
'#title' => t('Lightbox integration'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['imagepicker_lightbox2']['imagepicker_lightbox2_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Lightbox2 in Imagepicker'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_lightbox2_enable', 1),
'#description' => t('Unsetting this option will disable Lightbox2 in Imagepicker.'),
);
$form['imagepicker_lightbox2']['imagepicker_lightbox2_insert'] = array(
'#type' => 'textfield',
'#title' => t('Default Lightbox insert'),
'#size' => 15,
'#maxlength' => 30,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_lightbox2_insert', 'lightbox'),
'#description' => t('Configure the default code inserted into the "rel" attribute.'),
);
}
// advanced settings
$form['imagepicker_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced imagepicker settings'),
'#description' => t('You can alter the default styling of the iframe here,<br />useful if the iframe is not fitting in with your theme.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['imagepicker_advanced']['imagepicker_advanced_iframe_width'] = array(
'#type' => 'textfield',
'#title' => t('Iframe width'),
'#description' => t('Configure the iframe width. This can be a number or a percentage, eg 400 or 100%'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_advanced_iframe_width', "100%"),
);
$form['imagepicker_advanced']['imagepicker_advanced_iframe_height'] = array(
'#type' => 'textfield',
'#title' => t('Iframe height'),
'#description' => t('Configure the iframe height. This must be a number, eg 500'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_advanced_iframe_height', "500"),
);
$form['imagepicker_advanced']['imagepicker_advanced_iframe_border'] = array(
'#type' => 'textfield',
'#title' => t('Iframe border'),
'#description' => t('Configure the iframe border. This can be a number, eg 0 or 1, or a css entry such as 1px solid #808080'),
'#size' => 25,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_advanced_iframe_border', "0"),
);
$form['imagepicker_advanced']['imagepicker_advanced_browser_columns'] = array(
'#type' => 'textfield',
'#title' => t('Browser columns'),
'#description' => t('Configure the number of columns in the image browser. This must be a number, 0 for normal wraparound'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_advanced_browser_columns', 0),
);
$form['imagepicker_advanced']['imagepicker_advanced_browser_page'] = array(
'#type' => 'textfield',
'#title' => t('Images per page'),
'#description' => t('Configure the number of images shown per page in the image browser. This must be a number, 0 for no paging'),
'#size' => 10,
'#required' => TRUE,
'#default_value' => variable_get('imagepicker_advanced_browser_page', 20),
);
$form['imagepicker_advanced']['imagepicker_advanced_collapsed'] = array(
'#type' => 'checkbox',
'#title' => t('Fieldset state'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_advanced_collapsed', 0),
'#description' => t('Setting this option will collapse the fieldset the iframe is in by default.'),
);
$form['imagepicker_advanced']['imagepicker_use_full_url'] = array(
'#type' => 'checkbox',
'#title' => t('Use full url'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_use_full_url', 1),
'#description' => t('Setting this option will make imagepicker use a full url to the image being inserted, unsetting it will use an absolute path.<br />This is useful if you are developing a site with a different url than the production site will be on.'),
);
$form['imagepicker_advanced']['imagepicker_show_browse_order_form'] = array(
'#type' => 'checkbox',
'#title' => t('Show the order select box in the browser'),
'#return_value' => 1,
'#default_value' => variable_get('imagepicker_show_browse_order_form', 1),
'#description' => t(''),
);
$orderlist = array(
'img_id DESC' => t('Newest first'),
'img_id ASC' => t('Newest last'),
'img_date DESC' => t('Edited first'),
'img_date ASC' => t('Edited last'),
'img_name' => t('By name'),
);
$form['imagepicker_advanced']['imagepicker_default_browser_order'] = array(
'#type' => 'select',
'#default_value' => variable_get('imagepicker_default_browser_order', 'img_id DESC'),
'#options' => $orderlist,
'#title' => t('Default order'),
'#description' => t('The default order used to sort the browser. This will be applied wether or not the select box is visible'),
);
return system_settings_form($form);
}
/**
* Validate form
*/
function imagepicker_settings_validate($form_id, $form_values) {
if (!strlen($form_values['imagepicker_default_thumbnail_size'])) {
form_set_error('imagepicker_default_thumbnail_size', t('You must fill in the Thumbnail field'));
}
if (!is_numeric($form_values['imagepicker_default_thumbnail_size'])) {
form_set_error('imagepicker_default_thumbnail_size', t('The Thumbnail field must be a number'));
}
if (!strlen($form_values['imagepicker_default_browser_thumbnail_size'])) {
form_set_error('imagepicker_default_browser_thumbnail_size', t('You must fill in the Browser Thumbnail field'));
}
if (!is_numeric($form_values['imagepicker_default_browser_thumbnail_size'])) {
form_set_error('imagepicker_default_browser_thumbnail_size', t('The Browser Thumbnail field must be a number'));
}
if (strlen($form_values['imagepicker_default_scale']) && !is_numeric($form_values['imagepicker_default_scale'])) {
form_set_error('imagepicker_default_scale', t('The Scale field must be a number'));
}
if (!strlen($form_values['imagepicker_default_fleft'])) {
form_set_error('imagepicker_default_fleft', t('You must fill in the Left alignment field'));
}
if (!strlen($form_values['imagepicker_default_fright'])) {
form_set_error('imagepicker_default_fright', t('You must fill in the Right alignment field'));
}
if (!is_numeric($form_values['imagepicker_advanced_iframe_height'])) {
form_set_error('imagepicker_advanced_iframe_height', t('The Iframe height must be a number'));
}
if (!strlen($form_values['imagepicker_advanced_iframe_width'])) {
form_set_error('imagepicker_advanced_iframe_width', t('You must fill in the Iframe width'));
}
if (!strlen($form_values['imagepicker_advanced_iframe_border'])) {
form_set_error('imagepicker_advanced_iframe_border', t('You must fill in the Iframe border'));
}
if (!drupal_strlen($form_values['imagepicker_insert_defaults_align'])) {
form_set_error('imagepicker_insert_defaults_align', t('You must fill in the default align field'));
}
if (!drupal_strlen($form_values['imagepicker_insert_defaults_show'])) {
form_set_error('imagepicker_insert_defaults_show', t('You must fill in the default show field'));
}
if (!drupal_strlen($form_values['imagepicker_insert_defaults_link'])) {
form_set_error('imagepicker_insert_defaults_link', t('You must fill in the default link field'));
}
if ($form_values['imagepicker_show_browse_order_form'] != 1) {
variable_del('imagepicker_browser_order');
}
}
// account
function imagepicker_user_page() {
$path = drupal_get_path('module', 'imagepicker');
drupal_add_css($path . '/imagepicker.css');
$content = theme_imagepicker_user_menu();
if (arg(3) == 'browse' && !arg(4)) {
$content .= imagepicker_user_browse();
}
elseif (arg(3) == 'browse' && arg(4) == 'admin') {
$content .= imagepicker_user_browse_admin();
}
elseif (arg(3) == 'browse' && arg(4)) {
$content .= imagepicker_user_view(arg(4));
}
elseif (arg(3) == 'edit' && arg(4)) {
$content .= imagepicker_user_image_edit(arg(4));
}
elseif (arg(3) == 'delete' && arg(4)) {
#$content .= ;
imagepicker_image_delete(TRUE);
}
elseif (arg(3) == 'groups') {
$content .= imagepicker_user_groups(arg(4), arg(5));
}
else {
$content .= imagepicker_user_upload();
}
return $content;
}
function imagepicker_user_upload() {
variable_del('imagepicker_advanced_browser_pagestart');
$content .= "<div class='imgp_help'>" . t('Upload images. You can give them a title and description') . "</div>";
$content .= drupal_get_form('imagepicker_upload_form', TRUE);
return $content;
}
function imagepicker_user_browse() {
global $user;
$content = '<p>' . l(t('Admin mode'), 'user/' . $user->uid . '/imagepicker/browse/admin') . '</p>';
$content .= _imagepicker_browse("account");
return $content;
}
function imagepicker_user_browse_admin() {
global $user;
$content = '<p>' . l(t('Browse mode'), 'user/' . $user->uid . '/imagepicker/browse') . '</p>';
$content .= _imagepicker_browse_admin("account");
return $content;
}
function imagepicker_user_view($img_id) {
$img = _imagepicker_get_img($img_id);
if ($img) {
$imgpath = imagepicker_get_image_path($img, 'full');
$content .= '<div id="imgp_img_view">';
if ($img['img_title']) {
$content .= '<div id="imgp_img_view_title">' . $img['img_title'] . '</div>';
}
$content .= '<img id="imgp_img_view_img" src="' . $imgpath . '" alt="' . $img['img_title'] . '" title="' . $img['img_name'] . '" />';
if ($img['img_description']) {
$content .= '<div id="imgp_img_view_desc">' . nl2br($img['img_description']) . '</div>';
}
$imgbasedir = imagepicker_get_path(FALSE, TRUE);
$file = $imgbasedir . $img['img_name'];
$info = image_get_info($file);
$content .= "<div>";
$content .= t('Width') . ": " . $info['width'] . " ";
$content .= t('Height') . ": " . $info['height'] . " ";
$content .= t('Type') . ": " . $info['extension'] . " ";
$content .= t('Size') . ": " . $info['file_size'];
$content .= "</div>";
$content .= '</div>';
$content .= drupal_get_form('imagepicker_user_image_form', $img);
// groups
if (imagepicker_get_groups()) {
$content .= drupal_get_form('imagepicker_group_images_form', $img['img_id']);
}
}
else {
drupal_set_message(t('Image not found.'), 'error');
$content = '';
}
return $content;
}
function imagepicker_user_image_form() {
$form['edit'] = array(
'#type' => 'submit',
'#value' => t('Edit image'),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete image'),
);
return $form;
}
/**
* Submit form
*/
function imagepicker_user_image_form_submit($form_id, $form_values) {
global $user;
if ($form_values['op'] == t('Delete image')) {
imagepicker_image_delete("account");
}
elseif ($form_values['op'] == t('Edit image')) {
drupal_goto('user/' . $user->uid . '/imagepicker/edit/' . arg(4));
}
}
function imagepicker_user_image_edit($img_id) {
$content = _imagepicker_edit_img($img_id, TRUE);
return $content;
}
function theme_imagepicker_user_menu() {
global $user;
$path = "user/" . $user->uid . "/imagepicker";
$items = array(
l(t('Upload'), $path),
l(t('Browse'), "{$path}/browse"),
l(t('Groups'), "{$path}/groups"),
);
$content .= theme_item_list($items, NULL, 'ul', $attributes = array(
'class' => 'tabs secondary',
));
return $content;
}
function _imagepicker_browse($src = "iframe") {
global $user;
$content = "";
// paths
if ($src == 'iframe') {
$imgurl = 'imagepicker/browse/';
}
else {
$imgurl = 'user/' . $user->uid . '/imagepicker/browse/';
}
if (variable_get('imagepicker_show_browse_order_form', 1)) {
$content .= drupal_get_form('imagepicker_browse_order_form');
}
if (imagepicker_get_groups() && imagepicker_has_grouplist()) {
// add groups select here
$content .= drupal_get_form('imagepicker_browse_groups_form');
}
if ($src == "account") {
$content .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to view.') . '</div>';
}
else {
$content .= '<div class="imgp_help">' . t('Hold the mouse over an image to view Name, Title and Description, Click on it to use.') . '</div>';
}
$content .= '<div class="clear-block">';
// if there are groups
$gid = 0;
if (imagepicker_get_groups()) {
$gids = imagepicker_get_user_group_state();
$gid = $gids[0];
}
$how_many = variable_get('imagepicker_advanced_browser_page', 25);
$default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
$order = variable_get('imagepicker_browser_order', $default_order);
// filter by selected group
if ($gid) {
$sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date\n FROM {imagepicker} i, {imagepicker_group_images} g\n WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d\n ORDER BY i.{$order}";
$result = pager_query($sql, $how_many, 0, NULL, array(
$user->uid,
$gid,
));
}
else {
$sql = "SELECT * FROM {imagepicker} WHERE uid=%d ORDER BY {$order}";
$result = pager_query($sql, $how_many, 0, NULL, array(
$user->uid,
));
}
$browsercols = variable_get('imagepicker_advanced_browser_columns', 0);
$ct = 0;
$imgct = 0;
while ($img = db_fetch_array($result)) {
// img_id img_name img_title img_description
$imgpath = imagepicker_get_image_path($img, 'browser');
if ($imgpath) {
$tooltip = $img['img_name'] . ': ' . $img['img_title'] . ' ' . $img['img_description'];
$imglink = '<img src="' . $imgpath . '" alt="' . $img['img_title'] . '" title="' . $tooltip . '" />';
$content .= '<div class="imgp_holder">';
$content .= l($imglink, $imgurl . $img['img_id'], array(), NULL, NULL, FALSE, TRUE);
$content .= '</div>';
$ct++;
if ($browsercols > 0 && $ct >= $browsercols) {
$content .= '</div><div class="clear-block">';
$ct = 0;
}
}
$imgct++;
}
$content .= '</div>';
$content .= theme('pager', NULL, $how_many);
if (!$imgct) {
$content = '<div class="messages">' . t('You do not have any uploaded images') . '</div>';
}
return $content;
}
function _imagepicker_browse_admin($src = "iframe") {
global $user;
$content = "";
// paths
if ($src == 'iframe') {
$editpath = "imagepicker/edit/";
$deletepath = "imagepicker/delete/";
$imgpath = 'imagepicker/browse/';
}
else {
$editpath = 'user/' . $user->uid . '/imagepicker/edit/';
$deletepath = 'user/' . $user->uid . '/imagepicker/delete/';
$imgpath = 'user/' . $user->uid . '/imagepicker/browse/';
}
// if there are groups
$gid = 0;
if (imagepicker_get_groups() && imagepicker_has_grouplist()) {
// add groups select here
$content .= drupal_get_form('imagepicker_browse_groups_form');
$gids = imagepicker_get_user_group_state();
$gid = $gids[0];
}
$how_many = variable_get('imagepicker_advanced_browser_page', 25);
// filter by selected group
if ($gid) {
$sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date\n FROM {imagepicker} i, {imagepicker_group_images} g\n WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d\n ORDER BY i.img_id";
$result = pager_query($sql, $how_many, 0, NULL, array(
$user->uid,
$gid,
));
}
else {
$sql = "SELECT * FROM {imagepicker} WHERE uid=%d ORDER BY img_id";
$result = pager_query($sql, $how_many, 0, NULL, array(
$user->uid,
));
}
$rows = array();
while ($row = db_fetch_array($result)) {
// img_id img_name img_title img_description
$img_name = check_plain($row['img_name']);
$description = check_plain($row['img_description']);
if (strlen($description) > 30) {
$description = substr($description, 0, 30) . '...';
}
$row_data = array(
l($img_name, $imgpath . $row['img_id']),
check_plain($row['img_title']),
$description,
check_plain($row['img_date']),
l(t('Edit'), $editpath . $row['img_id']),
l(t('Delete'), $deletepath . $row['img_id']),
);
$rows[] = $row_data;
}
if (count($rows)) {
$header = array(
t('Name'),
t('Title'),
t('Description'),
t('Date'),
array(
'data' => t('Actions'),
'colspan' => 2,
),
);
$content .= '<div class="imgp_imgs_list">';
$content .= theme('table', $header, $rows) . theme('pager', NULL, $how_many);
$content .= '</div>';
}
else {
$content = '<div class="messages">' . t('You do not have any uploaded images') . '</div>';
}
return $content;
}
function _imagepicker_get_img($img_id, $checkuser = TRUE) {
global $user;
$result = db_query_range("SELECT * FROM {imagepicker} WHERE img_id = '%d'", $img_id, 0, 1);
$img = db_fetch_array($result);
if (count($img)) {
if ($img['uid'] != $user->uid && $checkuser) {
drupal_set_message(t('This image does not belong to you.'), 'error');
watchdog('imagepicker', t('User uid !uid1 attempted to edit image belonging to user uid !uid2', array(
'!uid1' => $user->uid,
'!uid2' => $img['uid'],
)), WATCHDOG_WARNING);
return FALSE;
}
return $img;
}
return FALSE;
}
function _imagepicker_edit_img($img_id, $account = FALSE) {
$content = '';
$img = _imagepicker_get_img($img_id);
if ($img) {
$imgsrc = imagepicker_get_image_path($img, 'browser');
$content .= "<div class='imgp_help'>" . t('Edit image details') . "</div>";
$content .= '<div id="imgp_img_holder"><img src="' . $imgsrc . '" alt="' . check_plain($img['img_title']) . '" /></div>';
$content .= drupal_get_form('imagepicker_edit_form', $img, $account);
}
else {
drupal_set_message(t('Image not found.'), 'error');
$content = '';
}
return $content;
}
function _imagepicker_user_has_img() {
global $user;
$num = 0;
$result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d'", array(
$user->uid,
));
while ($row = db_fetch_array($result)) {
$num++;
}
return $num;
}
/* groups */
function imagepicker_groups() {
// from iframe
if (!arg(2)) {
$content = imagepicker_groups_list();
$content .= drupal_get_form('imagepicker_groups_form');
}
elseif (arg(2) == 'edit') {
$record = imagepicker_get_user_group(arg(3));
$content .= drupal_get_form('imagepicker_groups_form', $record);
}
elseif (arg(2) == 'delete') {
$content .= drupal_get_form('imagepicker_group_delete_form', arg(3));
}
theme('imagepicker', $content);
}
function imagepicker_groups_form($record = 0, $account = FALSE) {
$form['groupsave'] = array(
'#type' => 'fieldset',
'#title' => $record->gid ? t('Edit group') : t('Add group'),
'#description' => t('Give your group a brief name and optionally put any additional information in the group description box'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['groupsave']['group_name'] = array(
'#type' => 'textfield',
'#title' => t('Group name'),
'#size' => 20,
'#default_value' => $record->group_name ? $record->group_name : '',
'#description' => t(''),
'#required' => TRUE,
);
$form['groupsave']['group_description'] = array(
'#type' => 'textfield',
'#title' => t('group description'),
'#size' => 50,
'#maxlength' => 50,
'#default_value' => $record->group_description ? $record->group_description : '',
'#description' => t('Maximum 50 characters'),
'#required' => FALSE,
);
if ($account) {
$form['groupsave']['account'] = array(
'#type' => 'hidden',
'#value' => 1,
);
}
if ($record->gid) {
$form['groupsave']['gid'] = array(
'#type' => 'hidden',
'#value' => $record->gid,
);
}
$form['groupsave']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save group'),
);
return $form;
}
/**
* Submit form
*/
function imagepicker_groups_form_submit($form_id, $form_values) {
global $user;
$record['group_name'] = $form_values['group_name'];
$record['group_description'] = $form_values['group_description'];
$record['uid'] = $user->uid;
if ($form_values['gid']) {
$record['gid'] = $form_values['gid'];
imagepicker_update_user_group($record);
}
else {
imagepicker_insert_user_group($record);
}
}
// imagepicker_user_groups functions
function imagepicker_insert_user_group($record) {
if (db_query("INSERT INTO {imagepicker_user_groups} (uid, group_name, group_description) VALUES (%d, '%s', '%s')", array(
$record['uid'],
$record['group_name'],
$record['group_description'],
))) {
drupal_set_message(t('Group was successfully inserted'));
}
else {
drupal_set_message(t('Error while trying to insert your group.'));
}
}
function imagepicker_update_user_group($record) {
if (db_query("UPDATE {imagepicker_user_groups} SET group_name='%s', group_description='%s' WHERE gid = %d", array(
$record['group_name'],
$record['group_description'],
$record['gid'],
))) {
drupal_set_message(t('Group was successfully updated'));
}
else {
drupal_set_message(t('Error while trying to update your group.'));
}
}
function imagepicker_delete_user_group($gid) {
if (db_query("DELETE FROM {imagepicker_user_groups} WHERE gid = %d", array(
$gid,
)) && db_query("DELETE FROM {imagepicker_group_images} WHERE gid=%d", array(
$gid,
))) {
drupal_set_message(t('Group was successfully deleted'));
}
else {
drupal_set_message(t('Error while trying to delete group.'));
}
}
function imagepicker_get_user_group($gid, $obj = TRUE) {
$result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE gid = %d", array(
$gid,
));
if ($obj) {
return db_fetch_object($result);
}
return db_fetch_array($result);
}
function imagepicker_get_user_groups_by_user($uid, $obj = TRUE) {
$result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array(
$uid,
));
if ($obj) {
return db_fetch_object($result);
}
return db_fetch_array($result);
}
// imagepicker_group_images
function imagepicker_insert_group_image($record) {
if (!db_query("INSERT INTO {imagepicker_group_images} (gid, img_id) VALUES (%d, %d)", array(
$record['gid'],
$record['img_id'],
))) {
drupal_set_message(t('Error while trying to insert your group.'), 'error');
}
}
function imagepicker_delete_group_image($img_id) {
if (!db_query("DELETE FROM {imagepicker_group_images} WHERE img_id=%d", array(
$img_id,
))) {
drupal_set_message(t('Error while trying to delete your group.'), 'error');
}
}
// build a table
function imagepicker_groups_list($myaccount = FALSE, $account = FALSE) {
// show a table of the lists belonging to the current user
if ($account) {
$user = $account;
}
else {
global $user;
}
if ($myaccount) {
$editpath = 'user/' . $user->uid . '/imagepicker/groups/edit/';
$deletepath = 'user/' . $user->uid . '/imagepicker/groups/delete/';
}
else {
$editpath = "imagepicker/groups/edit/";
$deletepath = "imagepicker/groups/delete/";
}
$output = "";
$how_many = variable_get('imagepicker_rows_per_page', 25);
$sql = "SELECT * FROM {imagepicker_user_groups} WHERE uid=" . $user->uid . " ORDER BY group_name";
$result = pager_query($sql, $how_many);
$rows = array();
$totcount = 0;
$enabledlist = imagepicker_get_enabled_group();
while ($row = db_fetch_array($result)) {
$count = imagepicker_get_group_images_count($row['gid']);
$totcount += $count;
$row_data = array(
check_plain($row['group_name']),
check_plain($row['group_description']),
$count,
$enabledlist && in_array($row['gid'], $enabledlist) ? t('selected') : '',
l(t('Edit'), $editpath . $row['gid']),
l(t('Delete'), $deletepath . $row['gid']),
);
$rows[] = $row_data;
}
if (count($rows)) {
$header = array(
t('Group name'),
t('Description'),
t('No. images'),
t('State'),
array(
'data' => t('Actions'),
'colspan' => 2,
),
);
$output .= '<div class="imgp_groups_list">' . theme('table', $header, $rows) . theme('pager', NULL, $how_many) . "</div>";
$allcount = _imagepicker_user_has_img();
$output .= '<div class="imgp_groups_info">';
$output .= t('Total number of images: %allcount', array(
'%allcount' => $allcount,
)) . '<br/>';
$output .= t('Total number of grouped images: %totcount', array(
'%totcount' => $totcount,
)) . '<br/>';
$output .= t('Total number of ungrouped images: %lcount', array(
'%lcount' => $allcount - $totcount,
)) . '<br/>';
$output .= '</div>';
return $output;
}
else {
return '<div class="messages">' . t('No groups found.') . '</div>';
}
}
/* groups */
function imagepicker_user_groups($mode = "", $gid = 0) {
if (!$mode) {
$content = imagepicker_groups_list(TRUE);
$content .= drupal_get_form('imagepicker_groups_form');
}
elseif ($mode == 'edit') {
$record = imagepicker_get_user_group($gid);
$content .= drupal_get_form('imagepicker_groups_form', $record);
}
elseif ($mode == 'delete') {
$content .= drupal_get_form('imagepicker_group_delete_form', $gid);
}
return $content;
}
function imagepicker_get_group_images_count($gid) {
$result = db_query("SELECT count(gid) as gidct FROM {imagepicker_group_images} WHERE gid = %d", array(
$gid,
));
$row = db_fetch_array($result);
return $row['gidct'];
}
function imagepicker_group_edit($gid) {
$record = imagepicker_get_user_group($gid);
$content .= drupal_get_form('imagepicker_groups_form', $record);
return $content;
}
function imagepicker_group_delete_form($gid) {
$record = imagepicker_get_user_group($gid);
if ($record) {
$form['groupname'] = array(
'#value' => '<p>' . $record->group_name . '</p>',
);
$form['mode'] = array(
'#type' => 'hidden',
'#value' => 'reallydelete',
);
$form['gid'] = array(
'#type' => 'hidden',
'#value' => $gid,
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Really Delete record'),
);
return $form;
}
else {
}
}
function imagepicker_group_delete_form_submit($form_id, $form_values) {
imagepicker_delete_user_group($form_values['gid']);
}
/**
* insert a form into the edit image page to allow the image to be associated with a group
*/
function imagepicker_group_images_form($img_id) {
$grouplist = imagepicker_get_groups();
$enabledlist = imagepicker_get_image_groups($img_id);
$form['group_images'] = array(
'#type' => 'fieldset',
'#title' => t('Groups'),
'#description' => t(''),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['group_images']['grouplist'] = array(
'#type' => 'checkboxes',
'#default_value' => $enabledlist,
'#options' => $grouplist,
'#title' => t('Your Groups'),
'#description' => t(''),
);
$form['group_images']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save group settings'),
);
$form['img_id'] = array(
'#type' => 'hidden',
'#value' => $img_id,
);
return $form;
}
/**
* Submit form
*/
function imagepicker_group_images_form_submit($form_id, $form_values) {
// have to delete all the entries for this image and rebuild with the new ones;
$img_id = $form_values['img_id'];
imagepicker_delete_group_image($img_id);
$grouplist = $form_values['grouplist'];
$inserted = FALSE;
foreach ($grouplist as $gid) {
if ($gid > 0) {
$record = array(
'gid' => $gid,
'img_id' => $img_id,
);
imagepicker_insert_group_image($record);
$inserted = TRUE;
}
}
if (!$inserted) {
$gid = imagepicker_get_user_group_state();
if ($gid) {
$ids = imagepicker_get_images_by_group($gid);
if (!$ids) {
global $user;
db_query("UPDATE {imagepicker_user_groups} SET state=0 WHERE uid=%d AND state=1", array(
$user->uid,
));
}
}
}
}
/**
* get all the groups for the current user
*/
function imagepicker_get_groups($account = FALSE) {
if ($account) {
$user = $account;
}
else {
global $user;
}
$result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array(
$user->uid,
));
while ($row = db_fetch_array($result)) {
$data[$row['gid']] = $row['group_name'];
}
return $data;
}
function imagepicker_get_image_groups($img_id) {
$result = db_query("SELECT gid FROM {imagepicker_group_images} WHERE img_id = %d", array(
$img_id,
));
while ($row = db_fetch_array($result)) {
$data[] = $row['gid'];
}
return $data;
}
function imagepicker_get_images_by_group($gid) {
$result = db_query("SELECT img_id FROM {imagepicker_group_images} WHERE gid = %d", array(
$gid,
));
while ($row = db_fetch_array($result)) {
$data[] = $row['img_id'];
}
return $data;
}
function imagepicker_get_user_group_state($state = 1) {
global $user;
$result = db_query("SELECT gid FROM {imagepicker_user_groups} WHERE state=%d AND uid=%d", array(
$state,
$user->uid,
));
while ($row = db_fetch_array($result)) {
$data[] = $row['gid'];
}
return $data;
}
function imagepicker_get_grouped_images() {
global $user;
$groupedlist = array();
// now the enabled ones
$result = db_query("\n SELECT i.img_id\n FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array(
$user->uid,
));
while ($row = db_fetch_array($result)) {
$groupedlist[] = $row['img_id'];
}
return $groupedlist;
}
function imagepicker_get_grouplist() {
global $user;
$grouplist = array(
'' => 'All',
);
$result = db_query("\n SELECT DISTINCT g.gid, g.group_name\n FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n WHERE g.uid=%d AND g.gid = i.gid", array(
$user->uid,
));
while ($row = db_fetch_array($result)) {
$grouplist[$row['gid']] = $row['group_name'];
}
return $grouplist;
}
function imagepicker_has_grouplist() {
global $user;
$result = db_query("\n SELECT DISTINCT g.gid, g.group_name\n FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n WHERE g.uid=%d AND g.gid = i.gid", array(
$user->uid,
));
$ct = 0;
while ($row = db_fetch_array($result)) {
$ct++;
}
return $ct;
}
function imagepicker_get_enabled_group() {
global $user;
$result = db_query("\n SELECT DISTINCT g.gid, g.group_name\n FROM {imagepicker_user_groups} g, {imagepicker_group_images} i\n WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array(
$user->uid,
));
while ($row = db_fetch_array($result)) {
$enabledlist[] = $row['gid'];
}
return $enabledlist;
}
function imagepicker_browse_groups_form() {
global $user;
// all the groups for the current user which have images attached
$grouplist = imagepicker_get_grouplist();
$enabledlist = imagepicker_get_enabled_group();
$form['gid'] = array(
'#type' => 'select',
'#default_value' => $enabledlist,
'#options' => $grouplist,
'#title' => t('Group'),
'#prefix' => '<div id="imgp_groups_form" class="container-inline">',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Go'),
'#suffix' => '</div>',
);
return $form;
}
/**
* Submit form
*/
function imagepicker_browse_groups_form_submit($form_id, $form_values) {
// need to get the users gids
$gids = imagepicker_get_groups();
$gids = array_keys($gids);
foreach ($gids as $gid) {
$state = 0;
if ($gid == $form_values['gid']) {
$state = 1;
}
db_query("UPDATE {imagepicker_user_groups} SET state=%d WHERE gid=%d", array(
$state,
$gid,
));
}
}
// browse order
function imagepicker_browse_order_form() {
$orderlist = array(
'img_id DESC' => t('Newest first'),
'img_id ASC' => t('Newest last'),
'img_date DESC' => t('Edited first'),
'img_date ASC' => t('Edited last'),
'img_name' => t('By name'),
);
$default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
$form['imagepicker_browser_order'] = array(
'#type' => 'select',
'#default_value' => variable_get('imagepicker_browser_order', $default_order),
'#options' => $orderlist,
'#title' => t('Order'),
'#prefix' => '<div id="imgp_order_form" class="container-inline">',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Go'),
'#suffix' => '</div>',
);
return $form;
}
/**
* Submit form
*/
function imagepicker_browse_order_form_submit($form_id, $form_values) {
variable_set('imagepicker_browser_order', $form_values['imagepicker_browser_order']);
}