/[drupal]/contributions/modules/imagepicker/imagepicker.module
ViewVC logotype

Contents of /contributions/modules/imagepicker/imagepicker.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download) (as text)
Tue May 19 12:00:16 2009 UTC (6 months, 1 week ago) by hutch
Branch: MAIN
CVS Tags: DRUPAL-6--1-5, HEAD
Changes since 1.21: +4 -3 lines
File MIME type: text/x-php
fixed bug in imagepicker_form_alter
1 <?php
2 // $Id: imagepicker.module,v 1.21 2009/03/07 11:02:47 hutch Exp $
3 // $Name: $
4 // for D6
5 /**
6 * @file
7 * Enables permitted roles to upload images for insertion into configured nodes.
8 */
9
10 /**
11 * Implementation of hook_help().
12 */
13 function imagepicker_help($path, $arg) {
14 switch ($path) {
15 case 'admin/help#imagepicker':
16 $output = '<p>'. t('Adds an advanced image upload form under the node body part.') .'</p>';
17 return $output;
18 }
19 }
20
21 /**
22 * Implementation of hook_perm().
23 */
24 function imagepicker_perm() {
25 return array('administer imagepicker', 'use imagepicker', 'access own imagepicker');
26 }
27
28 /**
29 * Implementation of hook_menu().
30 */
31 function imagepicker_menu() {
32 $items = array();
33 $items['imagepicker'] = array(
34 'title' => 'Image picker',
35 'page callback' => 'imagepicker_upload',
36 'access arguments' => array('use imagepicker'),
37 'type' => MENU_CALLBACK,
38 'file' => 'imagepicker.upload.inc',
39 );
40 $items['imagepicker/upload'] = array(
41 'title' => 'Upload',
42 'access arguments' => array('use imagepicker'),
43 'type' => MENU_DEFAULT_LOCAL_TASK,
44 'weight' => 1
45 );
46 $items['imagepicker/browse'] = array(
47 'title' => 'Browse',
48 'page callback' => 'imagepicker_browse',
49 'access arguments' => array('use imagepicker'),
50 'type' => MENU_LOCAL_TASK,
51 'weight' => 2
52 );
53 $items['imagepicker/browse/%imagepicker_id'] = array(
54 'title' => 'Browse',
55 'page callback' => 'imagepicker_browse',
56 'page arguments' => array(2),
57 'access arguments' => array('use imagepicker'),
58 'type' => MENU_LOCAL_TASK,
59 'weight' => 2
60 );
61 $items['imagepicker/edit/%imagepicker_id'] = array(
62 'title' => 'Edit image',
63 'page callback' => 'imagepicker_image_edit',
64 'page arguments' => array(2),
65 'access arguments' => array('use imagepicker'),
66 'type' => MENU_CALLBACK,
67 'file' => 'imagepicker.edit.inc',
68 );
69 $items['imagepicker/image/%imagepicker_id'] = array(
70 'title' => 'Imagepicker',
71 'page callback' => 'imagepicker_image_page',
72 'page arguments' => array(2),
73 'access arguments' => array('access content'),
74 'type' => MENU_CALLBACK,
75 );
76 // admin
77 $items['admin/settings/imagepicker'] = array(
78 'title' => 'Imagepicker',
79 'description' => 'Imagepicker settings.',
80 'page callback' => 'drupal_get_form',
81 'page arguments' => array('imagepicker_admin_settings'),
82 'access arguments' => array('administer imagepicker'),
83 'type' => MENU_NORMAL_ITEM,
84 'file' => 'imagepicker.admin.inc',
85 );
86 // account
87 if (variable_get('imagepicker_account_enabled', 1)) {
88 $items['user/%imagepicker_uid/imagepicker'] = array(
89 'title' => 'My imagepicker',
90 'description' => 'Manage your imagepicker files.',
91 'page callback' => 'imagepicker_user_page',
92 'page arguments' => array(1),
93 'access arguments' => array('access own imagepicker'),
94 'type' => MENU_LOCAL_TASK,
95 'file' => 'imagepicker.upload.inc',
96 );
97 // user/x/imagepicker/images
98 // user/x/imagepicker/images/browse
99 // user/x/imagepicker/images/edit/n
100 // user/x/imagepicker/images/delete/n
101
102 // user/x/imagepicker/groups
103 // user/x/imagepicker/groups/browse
104 // user/x/imagepicker/groups/edit/n
105 // user/x/imagepicker/groups/delete/n
106 // user/x/imagepicker/groups/otherfunc/n
107
108 $items['user/%imagepicker_uid/imagepicker/%imagepicker_path'] = array(
109 'title' => 'My imagepicker',
110 'description' => 'Manage your imagepicker files.',
111 'page callback' => 'imagepicker_user_page',
112 'page arguments' => array(1, 3),
113 'access arguments' => array('access own imagepicker'),
114 'type' => MENU_LOCAL_TASK,
115 );
116 $items['user/%imagepicker_uid/imagepicker/%imagepicker_path/%imagepicker_func/%imagepicker_id'] = array(
117 'title' => 'My imagepicker',
118 'description' => 'Manage your imagepicker files.',
119 'page callback' => 'imagepicker_user_page',
120 'page arguments' => array(1, 3, 4, 5),
121 'access arguments' => array('access own imagepicker'),
122 'type' => MENU_LOCAL_TASK,
123 );
124 }
125
126 return $items;
127 }
128
129 /**
130 * menu placeholder functions
131 */
132 function imagepicker_id_load($arg) {
133 return (is_numeric($arg) ? $arg : FALSE);
134 }
135
136 function imagepicker_uid_load($arg) {
137 global $user;
138 return ((is_numeric($arg) && $user->uid == $arg) ? $arg : FALSE);
139 }
140
141 function imagepicker_path_load($arg) {
142 $allowed = array('upload', 'images', 'groups');
143 return ( in_array($arg, $allowed) ? $arg : FALSE);
144 return $arg;
145 }
146
147 function imagepicker_func_load($arg) {
148 $allowed = array('browse', 'browseadmin', 'edit', 'delete');
149 return ( in_array($arg, $allowed) ? $arg : FALSE);
150 return $arg;
151 }
152
153 /**
154 * Implementation of hook_form_alter().
155 */
156 function imagepicker_form_alter(&$form, &$form_state, $form_id) {
157 $node = $form['#node'];
158 $node_types = node_get_types('names');
159 $node_type = in_array($node->type, variable_get('imagepicker_node_types_enabled', $node_types), TRUE);
160 // comment
161 $comment = FALSE;
162 $weight = 1;
163 if (module_exists('comment') && variable_get('imagepicker_comment_enabled', 0) && preg_match('/comment_form$/i', $form_id) ) {
164 $comment = TRUE;
165 $weight = 1;
166 }
167 if (user_access('use imagepicker')) {
168 if (($node_type && preg_match('/node_form$/i', $form_id) ) || $comment ) {
169 $form['body_field']['body_filter']['file_upload'] = array(
170 '#type' => 'fieldset',
171 '#title' => t('Image picker'),
172 '#collapsible' => 1,
173 '#collapsed' => variable_get('imagepicker_advanced_collapsed', 0),
174 '#weight' => $weight,
175 );
176 $form['body_field']['body_filter']['#weight'] = 2;
177 $form['body_field']['body_filter']['file_upload']['mpframe'] = array(
178 '#type' => 'markup',
179 '#value' => '<div><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></div>',
180 );
181 $form['body_field']['#prefix'] = '<a name="body_hash"></a>'. $form['body_field']['#prefix'];
182 }
183 }
184 }
185
186 /**
187 * theme registry
188 */
189 function imagepicker_theme() {
190 return array(
191 'imagepicker' => array(
192 'arguments' => array(
193 'content' => NULL),
194 ),
195 );
196 }
197
198 /**
199 * Menu local task; presents the browse and select pages for imagepicker
200 */
201 function imagepicker_browse($img_id=0) {
202
203 if ( $img_id ) {
204 imagepicker_image_select($img_id);
205 exit();
206 }
207
208 $content = _imagepicker_browse();
209 theme('imagepicker', $content);
210 }
211
212 function imagepicker_image_select($img_id) {
213 global $user;
214
215 $result = db_query_range("SELECT * FROM {imagepicker} WHERE uid = '%d' AND img_id = '%d'", $user->uid, $img_id, 0, 1);
216 $img = db_fetch_array($result);
217 if ( $img) {
218 drupal_add_js(imagepicker_js($img), 'inline');
219 $imgpath = imagepicker_get_image_path($img, 'browser');
220 $content = '<div class="help">'. t('Choose the settings you want, place the cursor in the Body box above and Insert image.') .'</div>';
221 if ($img['img_title']) {
222 $content .= '<div id="imgp_img_holder_title">'. $img['img_title'] .'</div>';
223 }
224 if ($img['img_description']) {
225 $content .= '<div id="imgp_img_holder_description">'. $img['img_description'] .'</div>';
226 }
227 $content .= '<div id="imgp_img_holder">';
228 $content .= '<img id="imgp_img" src="'. $imgpath .'" alt="'. $img['img_title'] .'" title="'. $img['img_title'] .'" />';
229 $content .= '</div>';
230 $content .= drupal_get_form('imagepicker_image_form', $img['img_id']);
231 }
232 else {
233 drupal_set_message(t('Image not found in select.'), 'error');
234 $content = '';
235 }
236
237 theme('imagepicker', $content);
238 }
239
240 function imagepicker_image_form(&$form_state, $img_id) {
241 $showoptions = array('full' => t('Full size'), 'thumb' => t('Thumbnail'), 'title' => t('Title'));
242 $linkoptions = array('none' => t('None'), 'file' => t('File'), 'page' => t('Page') );
243 if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1) ) {
244 $linkoptions['lightbox'] = t('Lightbox');
245 }
246 if (variable_get('imagepicker_default_align_show', 1)) {
247 $alignoptions = array('none' => t('None'), 'fleft' => t('Float Left'), 'fright' => t('Float right'));
248 $form['align'] = array(
249 '#type' => 'radios',
250 '#title' => t('Align'),
251 '#default_value' => variable_get('imagepicker_insert_defaults_align', 'none'),
252 '#options' => $alignoptions,
253 '#description' => '',
254 '#prefix' => '<div class="imgp_img_options">',
255 '#suffix' => '</div>'
256 );
257 }
258 $form['show'] = array(
259 '#type' => 'radios',
260 '#id' => 'show',
261 '#title' => t('Show'),
262 '#default_value' => variable_get('imagepicker_insert_defaults_show', 'full'),
263 '#options' => $showoptions,
264 '#description' => '',
265 '#prefix' => '<div class="imgp_img_options">',
266 '#suffix' => '</div>'
267 );
268 $form['link'] = array(
269 '#type' => 'radios',
270 '#title' => t('Link'),
271 '#default_value' => variable_get('imagepicker_insert_defaults_link', 'none'),
272 '#options' => $linkoptions,
273 '#description' => '',
274 '#prefix' => '<div class="imgp_img_options">',
275 '#suffix' => '</div>'
276 );
277 $form['insert'] = array(
278 '#type' => 'button',
279 '#value' => t('Insert image'),
280 '#prefix' => '<div id="imgp_controls">',
281 '#attributes' => array('onclick' => 'imagepickerInsert(this); return false;')
282 );
283 $form['edit'] = array(
284 '#type' => 'submit',
285 '#value' => t('Edit image'),
286 '#submit' => array('imagepicker_image_form_edit'),
287 );
288 $form['delete'] = array(
289 '#type' => 'submit',
290 '#value' => t('Delete image'),
291 '#submit' => array('imagepicker_image_form_delete'),
292 '#suffix' => '</div>'
293 );
294 $form['img_id'] = array(
295 '#type' => 'hidden',
296 '#value' => $img_id,
297 );
298
299 return $form;
300 }
301
302 /**
303 * Submit form functions
304 */
305 function imagepicker_image_form_delete($form, &$form_state) {
306 imagepicker_image_delete($form_state['values']['img_id']);
307 }
308
309 function imagepicker_image_form_edit($form, &$form_state) {
310 drupal_goto('imagepicker/edit/'. $form_state['values']['img_id']);
311 }
312
313 function imagepicker_image_delete($img_id, $account=FALSE) {
314 global $user;
315
316 $result = db_query_range("SELECT uid, img_name FROM {imagepicker} WHERE img_id = '%d'", $img_id, 0, 1);
317 $img = db_fetch_array($result);
318 if ($img) {
319 if ($img['uid'] != $user->uid) {
320 drupal_set_message(t('This image does not belong to you.'), 'error');
321 watchdog('imagepicker', 'User uid !a attempted to delete image belonging to user uid !b', array( '!a' => $user->uid, '!b' => $img['uid']), WATCHDOG_WARNING);
322 }
323 else {
324 $destination = imagepicker_get_path(FALSE, TRUE);
325 $thumbsdir = $destination .'thumbs'. DIRECTORY_SEPARATOR;
326 $browserdir = $destination .'browser'. DIRECTORY_SEPARATOR;
327 file_delete($destination . $img['img_name']);
328 file_delete($thumbsdir . $img['img_name']);
329 file_delete($browserdir . $img['img_name']);
330 if (db_query("DELETE FROM {imagepicker} WHERE uid = '%d' AND img_id = '%d'", array($user->uid, $img_id))) {
331 drupal_set_message(t('Image was successfully deleted'));
332 }
333 else {
334 drupal_set_message(t('Error while trying to delete your image from database.'));
335 }
336 }
337 }
338 else {
339 drupal_set_message(t('Image not found in delete.'), 'error');
340 }
341 if ($account) {
342 drupal_goto('user/'. $user->uid .'/imagepicker/images/browse');
343 }
344 else {
345 drupal_goto('imagepicker/browse');
346 }
347 }
348
349 /**
350 * Menu callback; presents the image page for imagepicker
351 */
352 function imagepicker_image_page($img_id) {
353 global $base_url;
354
355 $result = db_query_range("SELECT i.*, u.name FROM {imagepicker} AS i JOIN {users} AS u USING (uid) WHERE img_id = '%d'", $img_id, 0, 1);
356 $img = db_fetch_array($result);
357 if ($img && is_array($img) && count($img)) {
358 $path = drupal_get_path('module', 'imagepicker');
359 drupal_add_css($path .'/imagepicker.css');
360 drupal_set_title($img['img_title']);
361 $imgsrc = imagepicker_get_path(TRUE, $img) . $img['img_name'];
362 $content = '<div id="imgp_page"><div id="imgp_page_img">';
363 $content .= '<a href="'. $imgsrc .'" alt="'. $img['img_title'] .'" target="_blank">';
364 $content .= '<img src="'. $imgsrc .'" alt="'. $img['img_title'] .'" /></a></div>';
365 $content .= '<div>'. nl2br($img['img_description']) .'</div>';
366 if (variable_get('imagepicker_default_pagelink', 1)) {
367 $content .= ' <div><a href="#" onclick="history.back()">'. t('Return to page') .'</a></div>
368 ';
369 }
370 $content .= ' </div>';
371
372 }
373 else {
374 drupal_set_message(t('Image not found in page.'), 'error');
375 $content = '';
376 }
377
378 return $content;
379 }
380
381 /**
382 * theming
383 */
384 function theme_imagepicker(&$content) {
385 $head_title = (drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_name', 'Drupal'));
386 $path = drupal_get_path('module', 'imagepicker');
387 drupal_add_css($path .'/imagepicker.css');
388 $styles = drupal_get_css();
389 $scripts = drupal_get_js();
390 $tabs = theme('menu_local_tasks');
391 $messages = imagepicker_strip_messages(theme('status_messages'));
392 $template = 'imagepicker.tpl.php';
393 $defaulttemplate = variable_get('theme_default', '');
394 if ($defaulttemplate) {
395 $templatepath = drupal_get_path('theme', $defaulttemplate);
396 if (file_exists($templatepath . '/' . $template)) {
397 $template = $templatepath .'/'. $template;
398 }
399 }
400 include($template);
401 drupal_page_footer();
402 exit;
403 }
404
405 /**
406 * some common utilities
407 */
408 function imagepicker_get_path($url = FALSE, $userdir = FALSE) {
409 global $user, $base_url;
410
411 $dirsep = !$url ? DIRECTORY_SEPARATOR : '/';
412
413 if (!$url) {
414 $path = str_replace('/', DIRECTORY_SEPARATOR, getcwd());
415 }
416 else {
417 if (variable_get('imagepicker_use_full_url', 1)) {
418 $path = $base_url;
419 }
420 else {
421 $path = base_path();
422 $path = preg_replace("/\/$/", "", $path);
423 }
424 }
425
426 $path .= $dirsep . file_directory_path() . $dirsep .'imagepicker'. $dirsep;
427
428 if ($userdir) {
429 $username = !is_array($userdir) ? $user->name : $userdir['name'];
430 $firstletter = drupal_strtolower(drupal_substr($username, 0, 1));
431 $firstletter = preg_match('/^[a-z]$/', $firstletter) ? $firstletter : 'others';
432 $path .= $firstletter . $dirsep . $username . $dirsep;
433 }
434
435 return $path;
436 }
437
438 function imagepicker_get_image_path($img, $type = 'browser') {
439 $imgbasedir = imagepicker_get_path(FALSE, TRUE);
440
441 switch ($type) {
442 case 'browser':
443 if (file_exists($imgbasedir .'browser'. DIRECTORY_SEPARATOR . $img['img_name'])) {
444 $imgpath = imagepicker_get_path(TRUE, TRUE) .'browser/'. $img['img_name'];
445 }
446 elseif (file_exists($imgbasedir .'thumbs'. DIRECTORY_SEPARATOR . $img['img_name'])) {
447 $imgpath = imagepicker_get_path(TRUE, TRUE) .'thumbs/'. $img['img_name'];
448 }
449 break;
450
451 case 'full':
452 if (file_exists($imgbasedir . $img['img_name'])) {
453 $imgpath = imagepicker_get_path(TRUE, TRUE) . $img['img_name'];
454 }
455 break;
456
457 case 'thumb':
458 default:
459 if (file_exists($imgbasedir .'thumbs'. DIRECTORY_SEPARATOR . $img['img_name'])) {
460 $imgpath = imagepicker_get_path(TRUE, TRUE) .'thumbs/'. $img['img_name'];
461 }
462 elseif (file_exists($imgbasedir .'browser'. DIRECTORY_SEPARATOR . $img['img_name'])) {
463 $imgpath = imagepicker_get_path(TRUE, TRUE) .'browser/'. $img['img_name'];
464 }
465 break;
466 }
467
468 return $imgpath ? $imgpath : '';
469 }
470
471 // There is not need to inform users, that directory structure has been created
472 // and show them all paths... So lets strip these messages if there are any.
473 function imagepicker_strip_messages($msg) {
474 if ($msg) {
475 $dirsep = (DIRECTORY_SEPARATOR == '\\') ? '\\\\' : '\/';
476 $pattern = '/<li>.*'. $dirsep .'imagepicker'. $dirsep .'.*<\/li>/i';
477 $msg = preg_replace($pattern, '', $msg);
478 }
479 return $msg;
480 }
481
482 function imagepicker_js($img) {
483
484 $ret = "
485 function imagepickerInsert(button) {
486 // Get the form element
487 var imgpForm = document.getElementById('imagepicker-image-form');
488 if (imgpForm) {
489 var imgpShow = 'thumb';
490 var imgpLink = 'file';
491 var imgpAlign = 'none';
492 var imgpImagePath;
493 var imgpImageTitle = '". ($img['img_title'] ? addslashes($img['img_title']) : t('Image')) ."';
494 var imgpFileLink = '". imagepicker_get_image_path($img, 'full') ."';
495 var imgpThumbLink = '". imagepicker_get_image_path($img, 'thumb') ."';
496 var imgpPageLink = '". url('imagepicker/image/'. $img['img_id']) ."';
497 var imgpImageElement;
498 var imgpLinkElement;
499 var imgpImageStyle;
500 var imgpInsertion;
501 var i;
502
503 // Get show value
504 for (i = 0; i < imgpForm.show.length; i++) {
505 if(imgpForm.show[i].checked) {
506 var imgpShow = imgpForm.show[i].value
507 }
508 }
509 // Get link value
510 for (i = 0; i < imgpForm.link.length; i++) {
511 if(imgpForm.link[i].checked) {
512 var imgpLink = imgpForm.link[i].value
513 }
514 }
515 ";
516 if (variable_get('imagepicker_default_align_show', 1)) {
517 $ret .= "
518 // Get align value
519 for (i = 0; i < imgpForm.align.length; i++) {
520 if(imgpForm.align[i].checked) {
521 var imgpAlign = imgpForm.align[i].value
522 }
523 }
524
525 // Create a style for image holder
526 switch (imgpAlign) {
527 case 'fleft':
528 imgpImageStyle = '". variable_get('imagepicker_default_fleft', 'style="float: left"') ."';
529 break;
530
531 case 'fright':
532 imgpImageStyle = '". variable_get('imagepicker_default_fright', 'style="float: right"') ."';
533 break;
534
535 case 'none':
536 default:
537 imgpImageStyle = '';
538 break;
539 }
540 ";
541 }
542 else {
543 $ret .= "
544 imgpImageStyle = '';
545 ";
546 }
547 $ret .= "
548 switch (imgpShow) {
549 case 'full': imgpImagePath = imgpFileLink; break;
550 case 'title': imgpImagePath = ''; break;
551 case 'thumb':
552 default: imgpImagePath = imgpThumbLink; break;
553 }
554
555 // Create an image or span (containing title) HTML string
556 if (imgpImagePath) {
557 imgpImageElement = '<img src=\"'+imgpImagePath+'\" alt=\"'+imgpImageTitle+'\" ' + imgpImageStyle + ' \/>';
558 }
559 else {
560 imgpImageElement = '<span>'+imgpImageTitle+'<\/span>'
561 }
562
563 // Create a link HTML string
564 switch (imgpLink) {
565 case 'none': imgpLinkElement = '%imgpImageElement%'; break;
566 case 'page': imgpLinkElement = '<a href=\"'+imgpPageLink+'\" title=\"'+imgpImageTitle+'\" >%imgpImageElement%<\/a>'; break;
567 case 'file': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\/a>'; break;
568 ";
569 if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
570 $ret .= "
571 case 'lightbox': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" rel=\"". variable_get('imagepicker_lightbox2_insert', 'lightbox') ."\" >%imgpImageElement%<\/a>'; break;
572 ";
573 }
574 $ret .= "
575 default: imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\/a>'; break;
576 }
577
578 // Create a HTML string which should be inserted in the node body
579 imgpInsertion = imgpLinkElement.replace('%imgpImageElement%', imgpImageElement);
580
581 // Get the parent window of imagepicker iframe
582 var win = window.opener ? window.opener : window.dialogArguments;
583 if (!win) {
584 win = top;
585 }
586 //var isTinyMCE = win.document.getElementById('mce_editor_0'); // buggy
587 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.
588 if (isTinyMCE) {
589 win.tinyMCE.execCommand('mceInsertContent', false, imgpInsertion);
590 }
591 else {
592 var nodeBody = win.document.getElementById('edit-body');
593 var commentBody = win.document.getElementById('edit-comment');
594 if (nodeBody) {
595 insertAtCursor(nodeBody, imgpInsertion);
596 }
597 if (commentBody) {
598 insertAtCursor(commentBody, imgpInsertion);
599 }
600 }
601 win.location.hash='body_hash';
602 }
603 }
604
605 // Copy pasted from internet...
606 function insertAtCursor(myField, myValue) {
607 //IE support
608 if (document.selection) {
609 myField.focus();
610
611 //in effect we are creating a text range with zero
612 //length at the cursor location and replacing it
613 //with myValue
614 sel = document.selection.createRange();
615 sel.text = myValue;
616 }
617
618 //Mozilla/Firefox/Netscape 7+ support
619 else if (myField.selectionStart || myField.selectionStart == '0') {
620
621 //Here we get the start and end points of the
622 //selection. Then we create substrings up to the
623 //start of the selection and from the end point
624 //of the selection to the end of the field value.
625 //Then we concatenate the first substring, myValue,
626 //and the second substring to get the new value.
627 var startPos = myField.selectionStart;
628 var endPos = myField.selectionEnd;
629 myField.value = myField.value.substring(0, startPos)+ myValue + myField.value.substring(endPos, myField.value.length);
630
631 }
632 else {
633 myField.value += myValue;
634 }
635 }
636 ";
637 return $ret;
638 }
639
640 /**
641 * my imagepicker in my account
642 */
643 function imagepicker_user_page($uid, $path="", $func="", $id=0) {
644
645 drupal_add_css(drupal_get_path('module', 'imagepicker') .'/imagepicker.css');
646
647 $content = theme_imagepicker_user_menu($uid);
648
649 // path/func/id
650 if ($path == 'images') {
651 if ( ($func == 'browse' || $func == 'browseadmin') && is_numeric($id) && $id > 0) {
652 $content .= imagepicker_user_view($id);
653 }
654 elseif ($func == 'edit' && is_numeric($id) && $id > 0) {
655 include_once('imagepicker.edit.inc');
656 $content .= imagepicker_user_image_edit($id);
657 }
658 elseif ($func == 'delete' && is_numeric($id) && $id > 0) {
659 $content .= imagepicker_image_delete($id, TRUE);
660 }
661 elseif ($func == 'browse') {
662 $content .= imagepicker_user_browse();
663 }
664 elseif ($func == 'browseadmin') {
665 $content .= imagepicker_user_browse_admin();
666 }
667 }
668 elseif ($path == 'groups') {
669 include_once('imagepicker.group.inc');
670 if ($func == 'edit' && is_numeric($id) && $id > 0) {
671 $content .= imagepicker_user_groups($func, $id);
672 }
673 elseif ($func == 'browse') {
674 $content .= imagepicker_user_groups();
675 }
676 elseif ($func == 'delete') {
677 $content .= drupal_get_form('imagepicker_group_delete_form', $id);
678 }
679 }
680 else {
681 include_once('imagepicker.upload.inc');
682 $content .= imagepicker_user_upload();
683 }
684 return $content;
685 }
686
687 /**
688 * main thumbnails page in my imagepicker
689 */
690 function imagepicker_user_browse() {
691 global $user;
692 $content = '<p>'. l( t('Admin mode'), 'user/'. $user->uid .'/imagepicker/images/browseadmin') .'</p>';
693 $content .= _imagepicker_browse("account");
694 return $content;
695 }
696
697 function imagepicker_user_browse_admin() {
698 global $user;
699 $content = '<p>'. l( t('Browse mode'), 'user/'. $user->uid .'/imagepicker/images/browse') .'</p>';
700 $content .= _imagepicker_browse_admin("account");
701 return $content;
702 }
703
704 function imagepicker_user_view($img_id) {
705
706 $img = _imagepicker_get_img($img_id);
707 if ($img) {
708 $imgpath = imagepicker_get_image_path($img, 'full');
709 $content .= '<div id="imgp_img_view">';
710 if ($img['img_title']) {
711 $content .= '<div id="imgp_img_view_title">'. $img['img_title'] .'</div>';
712 }
713 $content .= '<img id="imgp_img_view_img" src="'. $imgpath .'" alt="'. $img['img_title'] .'" title="'. $img['img_name'] .'" />';
714 if ($img['img_description']) {
715 $content .= '<div id="imgp_img_view_desc">'. nl2br($img['img_description']) .'</div>';
716 }
717 $imgbasedir = imagepicker_get_path(FALSE, TRUE);
718 $file = $imgbasedir . $img['img_name'];
719 $info = image_get_info($file);
720 $content .= "<div>";
721 $content .= t('Width') .": ". $info['width'] ."&nbsp;&nbsp;&nbsp;";
722 $content .= t('Height') .": ". $info['height'] ."&nbsp;&nbsp;&nbsp;";
723 $content .= t('Type') .": ". $info['extension'] ."&nbsp;&nbsp;&nbsp;";
724 $content .= t('Size') .": ". $info['file_size'];
725 $content .= "</div>";
726 $content .= '</div>';
727 $content .= drupal_get_form('imagepicker_user_image_form', $img_id);
728
729 // groups
730 if (imagepicker_has_groups()) {
731 $content .= drupal_get_form('imagepicker_group_images_form', $img_id);
732 }
733 }
734 else {
735 drupal_set_message(t('Image not found in view.'), 'error');
736 $content = '';
737 }
738 return $content;
739 }
740
741 function imagepicker_user_image_form(&$form_state, $img_id) {
742 $form['edit'] = array(
743 '#type' => 'submit',
744 '#value' => t('Edit image'),
745 '#submit' => array('imagepicker_user_image_form_edit'),
746 );
747 $form['delete'] = array(
748 '#type' => 'submit',
749 '#value' => t('Delete image'),
750 '#submit' => array('imagepicker_user_image_form_delete'),
751 );
752 $form['img_id'] = array(
753 '#type' => 'hidden',
754 '#value' => $img_id,
755 );
756 return $form;
757 }
758
759 // submit functions
760 function imagepicker_user_image_form_delete($form, &$form_state) {
761
762 imagepicker_image_delete($form_state['values']['img_id'] , TRUE);
763 }
764
765 function imagepicker_user_image_form_edit($form, &$form_state) {
766 global $user;
767 drupal_goto('user/'. $user->uid .'/imagepicker/images/edit/'. $form_state['values']['img_id']);
768 }
769
770 function theme_imagepicker_user_menu($uid) {
771 #global $user;
772 $path = "user/$uid/imagepicker";
773 $items = array(
774 l(t('Upload'), $path),
775 l(t('Browse'), "$path/images/browse"),
776 l(t('Groups'), "$path/groups/browse" ),
777 );
778 $content .= theme_item_list($items, NULL, 'ul', $attributes = array('class' => 'tabs secondary'));
779 return $content;
780 }
781
782 function _imagepicker_browse($src="iframe") {
783 global $user;
784 $content = "";
785 // paths
786 if ($src == 'iframe') {
787 $imgurl = 'imagepicker/browse/';
788 }
789 else {
790 $imgurl = 'user/'. $user->uid .'/imagepicker/images/browse/';
791 }
792
793 if (variable_get('imagepicker_show_browse_order_form', 1)) {
794 $content .= drupal_get_form('imagepicker_browse_order_form');
795 }
796
797 $gid = 0;
798 if (imagepicker_has_groups() && imagepicker_has_grouplist()) {
799 // add groups select here
800 $content .= drupal_get_form('imagepicker_browse_groups_form');
801 $gids = imagepicker_get_user_group_state();
802 $gid = $gids[0];
803 }
804
805 if ($src == "account") {
806 $content .= '<div class="imgp_help">'. t('Hold the mouse over an image to view Name, Title and Description, Click on it to view.') .'</div>';
807 }
808 else {
809 $content .= '<div class="imgp_help">'. t('Hold the mouse over an image to view Name, Title and Description, Click on it to use.') .'</div>';
810 }
811 $content .= '<div class="clear-block">';
812
813 $browsercols = variable_get('imagepicker_advanced_browser_columns', 0);
814 $how_many = variable_get('imagepicker_advanced_browser_page', 25);
815 $default_order = variable_get('imagepicker_default_browser_order', 'img_id DESC');
816 $order = variable_get('imagepicker_browser_order', $default_order);
817 // filter by selected group
818 if ($gid) {
819 $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date
820 FROM {imagepicker} i, {imagepicker_group_images} g
821 WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d
822 ORDER BY i.$order";
823 $result = pager_query($sql, $how_many, 0, NULL, array($user->uid, $gid));
824 }
825 else {
826 $sql = "SELECT * FROM {imagepicker} WHERE uid=%d ORDER BY $order";
827 $result = pager_query($sql, $how_many, 0, NULL, array($user->uid) );
828 }
829
830 $ct = 0;
831 $imgct = 0;
832 while ($img = db_fetch_array($result)) {
833 // img_id img_name img_title img_description
834 $imgpath = imagepicker_get_image_path($img, 'browser');
835 if ($imgpath) {
836 $tooltip = $img['img_name'] .': '. $img['img_title'] .' '. $img['img_description'];
837 $imglink = ('<img src="'. $imgpath .'" alt="'. $img['img_title'] .'" title="'. $tooltip .'" />');
838 $content .= '<div class="imgp_holder">';
839 $content .= l($imglink, $imgurl . $img['img_id'], array('html' => TRUE));
840 $content .= '</div>';
841 $ct++;
842 if ($browsercols > 0 && $ct >= $browsercols) {
843 $content .= '</div><div class="clear-block">';
844 $ct = 0;
845 }
846 }
847 $imgct++;
848 }
849 $content .= '</div>';
850 $content .= theme('pager', NULL, $how_many);
851
852 if (! $imgct ) {
853 $content = '<div class="messages">'. t('You do not have any uploaded images') .'</div>';
854 }
855 return $content;
856 }
857
858 function _imagepicker_browse_admin($src="iframe") {
859 global $user;
860 $content = "";
861 // paths
862 if ($src == 'iframe') {
863 $editpath = "imagepicker/edit/";
864 $deletepath = "imagepicker/delete/";
865 $imgpath = 'imagepicker/browse/';
866 }
867 else {
868 $editpath = 'user/'. $user->uid .'/imagepicker/images/edit/';
869 $deletepath = 'user/'. $user->uid .'/imagepicker/images/delete/';
870 $imgpath = 'user/'. $user->uid .'/imagepicker/images/browse/';
871 }
872 // if there are groups
873 $gid = 0;
874 if (imagepicker_has_groups() && imagepicker_has_grouplist() ) {
875 // add groups select here
876 $content .= drupal_get_form('imagepicker_browse_groups_form');
877 $gids = imagepicker_get_user_group_state();
878 $gid = $gids[0];
879 }
880 $how_many = variable_get('imagepicker_advanced_browser_page', 25);
881 // filter by selected group
882 if ($gid) {
883 $sql = "SELECT i.img_id, i.uid, i.img_name, i.img_title, i.img_description, i.img_date
884 FROM {imagepicker} i, {imagepicker_group_images} g
885 WHERE i.uid = %d AND i.img_id = g.img_id AND g.gid = %d
886 ORDER BY i.img_id";
887 $result = pager_query($sql, $how_many, 0, NULL, array($user->uid, $gid));
888 }
889 else {
890 $sql = "SELECT * FROM {imagepicker} WHERE uid=%d ORDER BY img_id";
891 $result = pager_query($sql, $how_many, 0, NULL, array($user->uid) );
892 }
893 $rows = array();
894 while ($row = db_fetch_array($result)) {
895 // img_id img_name img_title img_description
896 $img_name = check_plain($row['img_name']);
897 $description = check_plain($row['img_description']);
898 if ( drupal_strlen($description) > 30 ) {
899 $description = drupal_substr($description, 0, 30) .'...';
900 }
901 $row_data = array(
902 l($img_name, $imgpath . $row['img_id']),
903 check_plain($row['img_title']),
904 $description,
905 check_plain($row['img_date']),
906 l(t('Edit'), $editpath . $row['img_id']),
907 l(t('Delete'), $deletepath . $row['img_id']),
908 );
909 $rows[] = $row_data;
910 }
911
912 if (count($rows)) {
913 $header = array(
914 t('Name'),
915 t('Title'),
916 t('Description'),
917 t('Date'),
918 array('data' => t('Actions'), 'colspan' => 2),
919 );
920 $content .= '<div class="imgp_imgs_list">';
921 $content .= theme('table', $header, $rows) . theme('pager', NULL, $how_many);
922 $content .= '</div>';
923 }
924 else {
925 $content = '<div class="messages">'. t('You do not have any uploaded images') .'</div>';
926 }
927 return $content;
928 }
929
930 function _imagepicker_get_img($img_id, $checkuser=TRUE) {
931 global $user;
932
933 $result = db_query_range("SELECT * FROM {imagepicker} WHERE img_id = '%d'", $img_id, 0, 1);
934 $img = db_fetch_array($result);
935 if ($img) {
936 if (($img['uid'] != $user->uid) && $checkuser) {
937 drupal_set_message(t('This image does not belong to you.'), 'error');
938 watchdog('imagepicker', "User uid !a attempted to edit image belonging to user uid !b", array('!a' => $user->uid, '!b' => $img['uid']), WATCHDOG_WARNING);
939 return FALSE;
940 }
941 return $img;
942 }
943 return FALSE;
944 }
945
946 function _imagepicker_user_has_img() {
947 global $user;
948 $result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d'", array($user->uid));
949 $ct = 0;
950 while ($row = db_fetch_array($result)) {
951 $ct++;
952 }
953 return $ct;
954 }
955
956 // groups
957 // get a count of all the groups for the current user;
958 function imagepicker_has_groups($account=FALSE) {
959 if ($account) {
960 $user = $account;
961 }
962 else {
963 global $user;
964 }
965 $result = db_query("SELECT count(gid) as gidct FROM {imagepicker_user_groups} WHERE uid = %d", array($user->uid));
966 $row = db_fetch_array($result);
967 return $row['gidct'];
968 }
969
970 // get all the groups for the current user;
971 function imagepicker_get_groups($account=FALSE) {
972 if ($account) {
973 $user = $account;
974 }
975 else {
976 global $user;
977 }
978 $data = array();
979 $result = db_query("SELECT * FROM {imagepicker_user_groups} WHERE uid = %d", array($user->uid));
980 while ($row = db_fetch_array($result)) {
981 $data[$row['gid']] = $row['group_name'];
982 }
983 return $data;
984 }
985 // get get the gid of the selected group
986 function imagepicker_get_user_group_state($state=1) {
987 global $user;
988 $result = db_query("SELECT gid FROM {imagepicker_user_groups} WHERE state=%d AND uid=%d", array($state, $user->uid));
989 $ct = 0;
990 while ($row = db_fetch_array($result)) {
991 $data[] = $row['gid'];
992 $ct++;
993 }
994 if ($ct) {
995 return $data;
996 }
997 return FALSE;
998 }
999 // for dropdown
1000 function imagepicker_get_grouplist() {
1001 global $user;
1002 $grouplist = array('' => 'All');
1003 $ct = 0;
1004 $result = db_query("
1005 SELECT DISTINCT g.gid, g.group_name
1006 FROM {imagepicker_user_groups} g, {imagepicker_group_images} i
1007 WHERE g.uid=%d AND g.gid = i.gid", array($user->uid));
1008 while ($row = db_fetch_array($result)) {
1009 $grouplist[$row['gid']] = $row['group_name'];
1010 $ct++;
1011 }
1012 if ($ct) {
1013 return $grouplist;
1014 }
1015 return FALSE;
1016 }
1017 // for dropdown
1018 function imagepicker_has_grouplist() {
1019 global $user;
1020 $ct = 0;
1021 $result = db_query("
1022 SELECT DISTINCT g.gid
1023 FROM {imagepicker_user_groups} g, {imagepicker_group_images} i
1024 WHERE g.uid=%d AND g.gid = i.gid", array($user->uid));
1025 while ($row = db_fetch_array($result)) {
1026 $ct++;
1027 }
1028 return $ct;
1029 }
1030
1031 // get enabled groups that have images. usually just one
1032 function imagepicker_get_enabled_group() {
1033 global $user;
1034 $result = db_query("
1035 SELECT DISTINCT g.gid, g.group_name
1036 FROM {imagepicker_user_groups} g, {imagepicker_group_images} i
1037 WHERE g.uid=%d AND g.gid = i.gid AND g.state=1", array($user->uid));
1038 $ct = 0;
1039 while ($row = db_fetch_array($result)) {
1040 $data[] = $row['gid'];
1041 $ct++;
1042 }
1043 if ($ct) {
1044 return $data;
1045 }
1046 return FALSE;
1047 }
1048
1049 function imagepicker_browse_groups_form(&$form_state) {
1050 global $user;
1051
1052 // all the groups for the current user which have images attached
1053 $grouplist = imagepicker_get_grouplist();
1054 $enabledlist = imagepicker_get_enabled_group();
1055
1056 $form['gid'] = array(
1057 '#type' => 'select',
1058 '#default_value' => $enabledlist,
1059 '#options' => $grouplist,
1060 '#title' => t('Group'),
1061 '#prefix' => '<div id="imgp_groups_form" class="container-inline">',
1062 );
1063 $form['submit'] = array(
1064 '#type' => 'submit',
1065 '#value' => t('Go'),
1066 '#suffix' => '</div>',
1067 );
1068
1069 return $form;
1070 }
1071
1072 function imagepicker_browse_groups_form_submit($form, &$form_state) {
1073 // need to get the users gids
1074 $gids = imagepicker_get_groups();
1075 $gids = array_keys($gids);
1076 foreach ($gids AS $gid) {
1077 $state = 0;
1078 if ( $gid == $form_state['values']['gid'] ) {
1079 $state = 1;
1080 }
1081 db_query("UPDATE {imagepicker_user_groups} SET state=%d WHERE gid=%d", array($state, $gid));
1082 }
1083
1084 }
1085
1086 // insert a form into the edit image page to allow the image to be associated with a group
1087 function imagepicker_group_images_form(&$form_state, $img_id) {
1088
1089 // list of all the groups for this user
1090 // also need to know which ones are enabled for this image
1091 $grouplist = imagepicker_get_groups();
1092 $enabledlist = imagepicker_get_image_groups($img_id);
1093
1094 $form['group_images'] = array(
1095 '#type' => 'fieldset',
1096 '#title' => t('Groups'),
1097 '#collapsible' => TRUE,
1098 '#collapsed' => FALSE,
1099 );
1100 $form['group_images']['grouplist'] = array(
1101 '#type' => 'checkboxes',
1102 '#default_value' => $enabledlist,
1103 '#options' => $grouplist,
1104 '#title' => t('Your Groups'),
1105 );
1106 $form['group_images']['submit'] = array(
1107 '#type' => 'submit',
1108 '#value' => t('Save group settings'),
1109 );
1110 $form['img_id'] = array(
1111 '#type' => 'hidden',
1112 '#value' => $img_id,
1113 );
1114 return $form;
1115 }
1116
1117 function imagepicker_group_images_form_submit($form, &$form_state) {
1118 // have to delete all the entries for this image and rebuild with the new ones;
1119 $img_id = $form_state['values']['img_id'];
1120 imagepicker_delete_group_image($img_id);
1121 $grouplist = $form_state['values']['grouplist'];
1122 $inserted = FALSE;
1123 foreach ($grouplist AS $gid) {
1124 if ($gid > 0) {
1125 $record = array('gid' => $gid, 'img_id' => $img_id);
1126 imagepicker_insert_group_image($record);
1127 $inserted = TRUE;
1128 }
1129 }
1130 if (! $inserted) {
1131 // need to check if this user has a state set for a group and
1132 // if this gid has images in imagepicker_group_images
1133