Index: avatar_selection.module =================================================================== --- avatar_selection.module (revision 1) +++ avatar_selection.module (working copy) @@ -92,6 +92,16 @@ 'access' => $access, 'type' => MENU_LOCAL_TASK, ); + $items[] = array( + 'path' => 'avatar_selection/select', + 'title' => t('Select an avatar'), + 'description' => t('Please select an avatar then click on submit.'), + 'callback' => 'avatar_selection_select_page', + 'callback arguments' => NULL, + 'access' => user_access('access avatars'), + 'type' => MENU_CALLBACK, + + ); if (arg(0) == 'admin' && arg(1) == 'settings' && arg(2) == 'avatar_selection' && arg(3) == 'edit') { if ((arg(4) == 'role' || arg(4) == 'og') && is_numeric(arg(5))) { @@ -105,12 +115,33 @@ ); } } + + } return $items; } +function avatar_selection_select_page($selected_avatar = '') { + if(!$selected_avatar) { + //die(print_r(_avatar_selection_image_list())); + $avatars = _avatar_selection_image_list(); + $avs = ''; + foreach($avatars['avatars_indexed'] as $avatar) + { + $avs .= l(theme_image('files/avatar_selection/'.$avatar, '', '', null, false), 'avatar_selection/select/'.$avatar, array(), NULL, NULL, FALSE, TRUE); + } + //die($avs); + return $avs; + }else{ + _avatar_selection_update_user_picture($selected_avatar); + drupal_set_message('Your have successfully changed your avatar image.'); + $output = 'Click here to close this window'; + return $output; + } +} + /** * Implementation of hook_form_alter(). * @@ -154,8 +185,10 @@ // Show selection options. $selects = _avatar_selection_image_list($user); + $enable_popup = variable_get('avatar_selection_popup', FALSE); if (count($selects['avatars'])) { $user_form = drupal_retrieve_form($form_id); + if(!$enable_popup) { $form['picture']['select_avatar'] = array( '#type' => 'radios', '#title' => $disable_upload ? t('Select an avatar') : t('Or simply select an icon'), @@ -164,6 +197,18 @@ '#required' => $force_choose ? TRUE : FALSE, '#attributes' => array('class' => 'user_avatar_select'), ); + }else{ + $images_per_page = variable_get('avatar_selection_avatar_per_page', 30); + $form['picture']['select_avatar'] = array( + '#type' => 'radios', + '#title' => $disable_upload ? t('Select an avatar') : t('Or simply select an icon'), + '#options' => array_slice($selects['avatars'], 0, $images_per_page), + '#default_value' => $user_form['_account']['#value']->picture, + '#required' => $force_choose ? TRUE : FALSE, + '#attributes' => array('class' => 'user_avatar_select'), + '#description' => 'To view all avatars '.l('click here', 'avatar_selection/select', array('target' => 'blank')).' and select one.', + ); + } } // Don't allow user to delete a selected avatar. @@ -174,9 +219,14 @@ } $images_per_page = variable_get('avatar_selection_avatar_per_page', 30); + if(!$enable_popup) { + $num_images = $selects['total']; + }else{ + $num_images = $images_per_page; + } $js_settings = array( 'num_images_per_page' => ($images_per_page ? $images_per_page : 1), - 'num_images' => $selects['total'], + 'num_images' => $num_images, 'base_path' => base_path(), 'image_path' => file_create_path('avatar_selection'), 'images' => $selects['avatars_indexed'], @@ -497,6 +547,12 @@ '#description' => t('Only allow users to pick an avatar that isn\'t already in use by another user. If there are no available avatars left, the default avatar image will be used.'), '#default_value' => variable_get('avatar_selection_distinctive_avatars', FALSE), ); + $form['update']['popup'] = array( + '#type' => 'checkbox', + '#title' => t('Enable popup window to display avatars.'), + '#description' => t('Check this option if you have huge number of avatars which may cause performance issues on the user edit form.'), + '#default_value' => variable_get('avatar_selection_popup', FALSE), + ); $form['update']['submit'] = array( @@ -914,6 +970,7 @@ variable_set('avatar_selection_avatar_per_page', $form_values['avatar_per_page']); variable_set('avatar_selection_set_random_default', $form_values['set_random_default']); variable_set('avatar_selection_distinctive_avatars', $form_values['distinctive_avatars']); + variable_set('avatar_selection_popup', $form_values['popup']); drupal_set_message(t('Configuration has been updated.')); } } @@ -1084,3 +1141,13 @@ } } + +function _avatar_selection_update_user_picture($picture) { + global $user; + switch ($GLOBALS['db_type']) { + case 'mysqli': + case 'mysql': + $query = "UPDATE users SET picture = 'files/avatar_selection/".$picture."' WHERE uid = ".$user->uid; + db_query($query); + } +}