diff -r b4cf9c635ae6 -r dfa3e7b9f89e avatar_selection/avatar_selection.module --- a/avatar_selection/avatar_selection.module Thu Jan 03 20:21:17 2008 +0100 +++ b/avatar_selection/avatar_selection.module Thu Jan 03 21:38:31 2008 +0100 @@ -86,7 +86,15 @@ function avatar_selection_menu($may_cach 'access' => $access, 'type' => MENU_LOCAL_TASK, ); - + $items[] = array( + 'path' => 'admin/settings/avatar_selection/scan', + 'title' => t('Scan avatars directory'), + 'description' => t('Allows the avatar directory and adds database entries for any new files found.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('avatar_selection_scan_form'), + 'access' => $access, + 'type' => MENU_LOCAL_TASK, + ); } return $items; @@ -135,6 +143,11 @@ function avatar_selection_form_alter($fo '#default_value' => $user_form['_account']['#value']->picture, '#required' => $force_choose ? TRUE : FALSE, '#attributes' => array('class' => 'user_avatar_select'), + ); + $form['picture']['pager'] = array( + '#value' => variable_get('avatar_selection_pager_id_number', 0), + '#theme' => 'avatar_selection_pager_wrapper', + '#weight' => 100, ); } @@ -220,15 +233,19 @@ function avatar_selection_get_random_ima function _avatar_selection_image_list($user = "") { $avatars = array(); - + $avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30); + $pager_id_number = variable_get('avatar_selection_pager_id_number', 0); $dir = file_create_path('avatar_selection'); - $mask = '.*\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)'; - $listings = file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, FALSE); - - $result = db_query("SELECT avatar, access, og_access FROM {avatar_selection} avs"); + + $listings = array(); + + $result = pager_query( "SELECT * FROM {avatar_selection} avs", $avatars_per_page, $pager_id_number ); while ($avatar = db_fetch_object($result)) { - $avs_image = $avatar->avatar; - $avs_access = preg_split('/\s*,\s*/', $avatar->access); + $avs_image = $avatar->avatar; + $listings[$avatar->avatar]->filename = $avs_image; + $listings[$avatar->avatar]->basename = basename( $avs_image ); + + $avs_access = preg_split('/\s*,\s*/', $avatar->access); $og_access = preg_split('/\s*,\s*/', $avatar->og_access); $del_avatar = 1; @@ -288,8 +305,9 @@ function _avatar_selection_image_list($u return $selects; } -function avatar_selection_settings_page($op = NULL, $aid = NULL) { - +function avatar_selection_settings_page($op = NULL, $aid = NULL) +{ + $avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30); switch ($op) { case 'edit': if (is_numeric($aid)) { @@ -306,6 +324,10 @@ function avatar_selection_settings_page( $output = drupal_get_form('avatar_selection_edit_form'); } break; + case 'scan' : + if (is_numeric($aid)) { + $output = drupal_get_form('avatar_selection_scan_form'); + } default: $form[] = array( '#type' => 'fieldset', @@ -333,7 +355,14 @@ function avatar_selection_config_form() '#default_value' => variable_get('avatar_selection_avatar_per_page', 30), '#size' => 3, ); - + // To store how many avatars per page are displayed. + $form['update']['pager_id_number'] = array('#type' => 'textfield', + '#title' => t('Pager id number'), + '#description' => t('The id number to use for the pager, this must be unique for every page that shows avatar image listings.'), + '#default_value' => variable_get('avatar_selection_pager_id_number', 0), + '#size' => 3, + ); + $form['update']['disable_user_upload'] = array('#type' => 'checkbox', '#title' => t('Disable users uploading pictures to profile'), '#description' => t('Allow users to pick their avatar from the selection but prevent them from uploading new avatars when editing their account.'), @@ -443,6 +472,14 @@ function avatar_selection_edit_form($for $form['#multistep'] = TRUE; if ($step == "list") { + $num_images_per_page = variable_get('avatar_selection_avatar_per_page', 30); + $js_settings = array( + 'num_images_per_page' => ($num_images_per_page ? $num_images_per_page : 1), + 'num_images' => $selects['total'], + ); + drupal_add_js(array('avatar_selection' => $js_settings), 'setting'); + drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header'); + $form['#redirect'] = FALSE; $form['select_avatar'] = array( @@ -458,14 +495,12 @@ function avatar_selection_edit_form($for '#value' => t('Edit'), ); - $num_images_per_page = variable_get('avatar_selection_avatar_per_page', 30); - $js_settings = array( - 'num_images_per_page' => ($num_images_per_page ? $num_images_per_page : 1), - 'num_images' => $selects['total'], - ); - drupal_add_js(array('avatar_selection' => $js_settings), 'setting'); - drupal_add_js(drupal_get_path('module', 'avatar_selection') .'/js/avatar_selection.js', 'module', 'header'); - + $form['pager'] = array( + '#value' => variable_get('avatar_selection_pager_id_number', 0), + '#theme' => 'avatar_selection_pager_wrapper', + '#weight' => 100, + ); + } else if ($step == "edit") { $form['#redirect'] = array('admin/settings/avatar_selection/edit'); @@ -523,6 +558,41 @@ function avatar_selection_edit_form($for return $form; } +function avatar_selection_scan_form( $form_values = NULL ) +{ + if (!variable_get('user_pictures', 0)) { + drupal_set_message(t('User Pictures option is disabled. You will need to enable this option before you can use the Avatar Selection module. You may configure this setting on the User settings page.', array('@url' => url('admin/user/settings')))); + } + $form = array(); + $form['scan'] = array( + '#type' => 'submit', + '#value' => t('Scan'), + ); + return $form; +} + +function avatar_selection_scan_form_submit( $form_values ) +{ + $dir = file_create_path('avatar_selection'); + $mask = '.*\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)'; + $listings = file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, FALSE); + + $query = "SELECT avatar FROM {avatar_selection}"; + $db_result = db_query( $query ); + + while( $db_row = db_fetch_object( $db_result ) ) + { + unset( $listings[$db_row->avatar] ); + } + $count = 0; + $query = "INSERT INTO {avatar_selection} VALUES ('%s', NULL, NULL)"; + foreach( $listings as $listing ) + { + db_query( $query, $listing->filename ); + $count++; + } + drupal_set_message( t('Added %count avatars to the database.', array( '%count' => $count ) ) ); +} /** * Validate the submission. @@ -538,6 +608,11 @@ function avatar_selection_config_form_va form_set_error('avatar_per_page', t('Must be a number.')); $error = TRUE; } + if (!is_numeric($form_values['pager_id_number'])) { + form_set_error('pager_id_number', t('Must be a number.')); + $error = TRUE; + } + } } @@ -607,6 +682,7 @@ function avatar_selection_config_form_su 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_pager_id_number', $form_values['pager_id_number'] ); drupal_set_message(t('Configuration has been updated.')); } } @@ -726,3 +802,12 @@ function _avatar_selection_save_avatar_i } } + +function theme_avatar_selection_pager_wrapper( $elements ) +{ + $num_images_per_page = variable_get('avatar_selection_avatar_per_page', 30); + $output = theme_pager( array(), $num_images_per_page, $elements['#value'] ); + print $elements['#value']; + $output .= drupal_render( $elements ); + return $output; +}