| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* Display help and module information
|
| 5 |
* @param section which section of the site we're displaying help
|
| 6 |
* @return help text for section
|
| 7 |
*/
|
| 8 |
function avatar_selection_help($section='') {
|
| 9 |
|
| 10 |
$output = '';
|
| 11 |
|
| 12 |
switch ($section) {
|
| 13 |
case "admin/help#avatar_selection":
|
| 14 |
$output .= '<p>'. t("Allows the user to pick an avatar from a list."). '</p>';
|
| 15 |
return $output;
|
| 16 |
case "admin/modules#description":
|
| 17 |
return t("Allows the user to pick an avatar from a list.");
|
| 18 |
case "admin/settings/avatar_selection/images":
|
| 19 |
return t("Upload images to display as a user avatar. These images can be anything you like, but it is recommended that you maintain a uniform icon size for all of your avatars. Maximum dimensions are 85x85 and the maximum size is 30 kB");
|
| 20 |
}
|
| 21 |
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_perm().
|
| 26 |
* Define the permissions this module uses
|
| 27 |
*/
|
| 28 |
function avatar_selection_perm() {
|
| 29 |
return array('administer avatar selection');
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_access().
|
| 35 |
*/
|
| 36 |
function avatar_selection_access($op, $node) {
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implementation of hook_menu().
|
| 41 |
*/
|
| 42 |
function avatar_selection_menu($may_cache) {
|
| 43 |
$access = user_access('administer avatar selection');
|
| 44 |
$items = array();
|
| 45 |
|
| 46 |
if ($may_cache) {
|
| 47 |
$items[] = array('path' => 'admin/settings/avatar_selection',
|
| 48 |
'title' => t('Avatar Selection'),
|
| 49 |
'callback' => 'avatar_selection_settings_page',
|
| 50 |
'access' => $access,
|
| 51 |
'description' => t('Allows the user to pick an avatar from a list.'),
|
| 52 |
);
|
| 53 |
}
|
| 54 |
else {
|
| 55 |
$items[] = array(
|
| 56 |
'path' => 'admin/settings/avatar_selection',
|
| 57 |
'title' => t('Avatar Selection'),
|
| 58 |
'description' => t('Allows the user to pick an avatar from a list.'),
|
| 59 |
'callback' => 'drupal_get_form',
|
| 60 |
'callback arguments' => array('avatar_selection_images_form'),
|
| 61 |
'access' => $access,
|
| 62 |
);
|
| 63 |
|
| 64 |
$items[] = array(
|
| 65 |
'path' => 'admin/settings/avatar_selection/images',
|
| 66 |
'title' => t('Images'),
|
| 67 |
'callback' => 'drupal_get_form',
|
| 68 |
'callback arguments' => array('avatar_selection_images_form'),
|
| 69 |
'access' => $access,
|
| 70 |
'type' => MENU_LOCAL_TASK,
|
| 71 |
);
|
| 72 |
|
| 73 |
}
|
| 74 |
|
| 75 |
return $items;
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_form_alter().
|
| 80 |
*/
|
| 81 |
function avatar_selection_form_alter($form_id, &$form) {
|
| 82 |
if ('user_edit' == $form_id && is_array($form['picture'])) {
|
| 83 |
drupal_add_css(drupal_get_path('module', 'avatar_selection') .'/avatar_selection.css');
|
| 84 |
|
| 85 |
if (variable_get('user_pictures', 0)) {
|
| 86 |
$selects = _avatar_selection_image_list();
|
| 87 |
if (count($selects)) {
|
| 88 |
$user_form = drupal_retrieve_form($form_id);
|
| 89 |
$form['picture']['select_avatar'] = array(
|
| 90 |
'#type' => 'radios',
|
| 91 |
'#title' => t('Or simply select an icon'),
|
| 92 |
'#options' => $selects,
|
| 93 |
'#default_value' => $user_form['_account']['#value']->picture,
|
| 94 |
);
|
| 95 |
$form['picture']['select_avatar']['#attributes']['class'] = "user_avatar_select";
|
| 96 |
}
|
| 97 |
}
|
| 98 |
$path = '/'.addcslashes(file_create_path('avatar_selection'), '/').'/';
|
| 99 |
if ($form['picture']['current_picture']['#value']
|
| 100 |
&& preg_match($path, $form['picture']['current_picture']['#value'])) {
|
| 101 |
unset($form['picture']['picture_delete']);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
return $form;
|
| 105 |
}
|
| 106 |
|
| 107 |
/*
|
| 108 |
* Implementation of hook_user().
|
| 109 |
*/
|
| 110 |
function avatar_selection_user($op, &$edit, &$user, $category = 'account') {
|
| 111 |
global $form_values;
|
| 112 |
switch ($op) {
|
| 113 |
case 'validate':
|
| 114 |
$file = file_check_upload('picture_upload');
|
| 115 |
if (!$file && $edit['select_avatar']) {
|
| 116 |
$form_values['picture'] = $edit['select_avatar'];
|
| 117 |
}
|
| 118 |
break;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
function _avatar_selection_image_list() {
|
| 125 |
$selects = array();
|
| 126 |
|
| 127 |
$dir = file_create_path('avatar_selection').'/';
|
| 128 |
$listings = file_scan_directory($dir, '.*\.(gif|jpg|jpeg|png)', array('.', '..', 'CVS'), 0, FALSE);
|
| 129 |
foreach ($listings as $listing) {
|
| 130 |
$selects[$dir.$listing->basename] = theme('image', $dir.$listing->basename, $listing->basename, $listing->basename);
|
| 131 |
}
|
| 132 |
|
| 133 |
return $selects;
|
| 134 |
}
|
| 135 |
|
| 136 |
function avatar_selection_settings_page($op = NULL, $aid = NULL) {
|
| 137 |
|
| 138 |
switch ($op) {
|
| 139 |
case 'edit':
|
| 140 |
if (is_numeric($aid)) {
|
| 141 |
$output = drupal_get_form('avatar_selection_images_form', $aid);
|
| 142 |
}
|
| 143 |
break;
|
| 144 |
case 'delete' :
|
| 145 |
if (is_numeric($aid)) {
|
| 146 |
$output = avatar_selection_delete($aid);
|
| 147 |
}
|
| 148 |
break;
|
| 149 |
default:
|
| 150 |
$form[] = array(
|
| 151 |
'#type' => 'fieldset',
|
| 152 |
'#title' => t('Add another'),
|
| 153 |
);
|
| 154 |
$output .= drupal_get_form('avatar_selection_images_form');
|
| 155 |
break;
|
| 156 |
}
|
| 157 |
return $output;
|
| 158 |
}
|
| 159 |
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Define a form to upload the avatar images.
|
| 163 |
*/
|
| 164 |
function avatar_selection_images_form() {
|
| 165 |
$form['new']['picture_upload'] = array('#type' => 'file',
|
| 166 |
'#title' => t('Upload image'),
|
| 167 |
'#size' => 48,
|
| 168 |
'#description' => t('A new avatar image. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''),
|
| 169 |
);
|
| 170 |
$form['new']['attach'] = array(
|
| 171 |
'#type' => 'submit',
|
| 172 |
'#value' => t('Upload'),
|
| 173 |
);
|
| 174 |
|
| 175 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 176 |
|
| 177 |
$selects = _avatar_selection_image_list();
|
| 178 |
if (count($selects)) {
|
| 179 |
$form['images'] = array('#tree' => TRUE);
|
| 180 |
foreach ($selects as $imagepath => $imageimg) {
|
| 181 |
$form['images'][$imagepath] = array(
|
| 182 |
'#type' => 'checkbox',
|
| 183 |
'#title' => $imageimg,
|
| 184 |
'#return_value' => 1,
|
| 185 |
'#default_value' => FALSE,
|
| 186 |
'#description' => check_plain($imagepath),
|
| 187 |
);
|
| 188 |
}
|
| 189 |
$form['delete_image'] = array(
|
| 190 |
'#type' => 'submit',
|
| 191 |
'#value' => t('Delete'),
|
| 192 |
);
|
| 193 |
}
|
| 194 |
return $form;
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Validate the submission.
|
| 199 |
*
|
| 200 |
* Check whether:
|
| 201 |
* Delete has been chosen AND a checkbox has been selected
|
| 202 |
* OR
|
| 203 |
* Upload has been chosen AND the file upload form is not empty.
|
| 204 |
*/
|
| 205 |
function avatar_selection_images_form_validate($form_id, $form_values) {
|
| 206 |
if ($form_values['op'] == t('Upload')) {
|
| 207 |
if (!$file = file_check_upload('picture_upload')) {
|
| 208 |
form_set_error('picture_upload', t('Please enter the filename of an image to upload.'));
|
| 209 |
}
|
| 210 |
else {
|
| 211 |
avatar_selection_validate_picture($file);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
else if ($form_values['op'] == t('Delete')) {
|
| 215 |
if (count(array_filter($form_values['images'])) == 0) {
|
| 216 |
form_set_error('images', t('Please select images to delete.'));
|
| 217 |
}
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
function avatar_selection_validate_picture($file) {
|
| 222 |
global $form_values;
|
| 223 |
|
| 224 |
// Check that uploaded file is an image, with a maximum file size
|
| 225 |
// and maximum height/width.
|
| 226 |
$info = image_get_info($file->filepath);
|
| 227 |
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
|
| 228 |
if (!$info || !$info['extension']) {
|
| 229 |
form_set_error('picture_upload', t('The uploaded file was not an image.'));
|
| 230 |
}
|
| 231 |
else if (image_get_toolkit()) {
|
| 232 |
image_scale($file->filepath, $file->filepath, $maxwidth, $maxheight);
|
| 233 |
}
|
| 234 |
else if (filesize($file->filepath) > (variable_get('user_picture_file_size', '30') * 1000)) {
|
| 235 |
form_set_error('picture_upload', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30'))));
|
| 236 |
}
|
| 237 |
else if ($info['width'] > $maxwidth || $info['height'] > $maxheight) {
|
| 238 |
form_set_error('picture_upload', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))));
|
| 239 |
}
|
| 240 |
|
| 241 |
|
| 242 |
}
|
| 243 |
|
| 244 |
function avatar_selection_images_form_submit($form_id, $form_values) {
|
| 245 |
$op = $form_values['op'];
|
| 246 |
|
| 247 |
// Save uploaded files
|
| 248 |
if ($op == t('Upload')) {
|
| 249 |
$dir = file_create_path('avatar_selection');
|
| 250 |
$is_writable = file_check_directory($dir, 1);
|
| 251 |
|
| 252 |
if ($is_writable) {
|
| 253 |
if ($source = file_check_upload('picture_upload')) {
|
| 254 |
if ($file = file_save_upload($source, $dir)) {
|
| 255 |
if (image_get_info($file->filepath)) {
|
| 256 |
drupal_set_message(t('New image saved.'));
|
| 257 |
}
|
| 258 |
else {
|
| 259 |
file_delete($file->filepath);
|
| 260 |
drupal_set_message('Uploaded file does not appear to be a valid image file. Please try again.');
|
| 261 |
}
|
| 262 |
}
|
| 263 |
}
|
| 264 |
}
|
| 265 |
else {
|
| 266 |
form_set_error('picture_upload', t('Directory not writable: '. $dir));
|
| 267 |
}
|
| 268 |
}
|
| 269 |
else if ($op == t('Delete')) {
|
| 270 |
foreach ($form_values['images'] as $path => $is_removed) {
|
| 271 |
if ($is_removed) {
|
| 272 |
$to_delete[] = $path;
|
| 273 |
}
|
| 274 |
}
|
| 275 |
if (is_array($to_delete)) {
|
| 276 |
avatar_selection_image_delete($to_delete);
|
| 277 |
}
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
function avatar_selection_image_delete($to_delete) {
|
| 282 |
foreach ($to_delete as $path) {
|
| 283 |
if (file_check_location($path, file_create_path('avatar_selection'))) {
|
| 284 |
file_delete($path);
|
| 285 |
if (!image_get_info($path)) {
|
| 286 |
drupal_set_message(t('Image deleted.'));
|
| 287 |
}
|
| 288 |
}
|
| 289 |
}
|
| 290 |
}
|
| 291 |
|