diff -r 1a2961f1238b -r 55629a4349e1 avatar_selection/avatar_selection.install --- a/avatar_selection/avatar_selection.install Thu Jan 03 20:20:49 2008 +0100 +++ b/avatar_selection/avatar_selection.install Sun Jan 06 21:05:20 2008 +0100 @@ -12,21 +12,38 @@ function avatar_selection_install() { case 'mysqli': case 'mysql': db_query("CREATE TABLE if not exists {avatar_selection} ( - avatar varchar(255) NOT NULL UNIQUE, - access varchar(255), - og_access varchar(255), - PRIMARY KEY (avatar) - ) /*!40100 DEFAULT CHARACTER SET utf8 */"); - break; - case 'pgsql': - db_query("CREATE TABLE {avatar_selection} ( - avatar varchar(255) NOT NULL UNIQUE, - access varchar(255), - og_access varchar(255), - PRIMARY KEY (avatar) - )"); - break; - } + avs_gid int( 10 ) NOT NULL DEFAULT 0, + avatar varchar(255) NOT NULL, + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avatar) + ) /*!40100 DEFAULT CHARACTER SET utf8 */"); + db_query("CREATE TABLE if not exists {avatar_selection_groups} ( + avs_gid int( 10 ) NOT NULL, + name varchar(255) NOT NULL UNIQUE, + access varchar(255), + og_access varchar(255), + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avs_gid) + ) /*!40100 DEFAULT CHARACTER SET utf8 */"); + break; + case 'pgsql': + db_query("CREATE TABLE if not exists {avatar_selection} ( + avs_gid int( 10 ) NOT NULL DEFAULT 0, + avatar varchar(255) NOT NULL, + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avatar) + )"); + db_query("CREATE TABLE if not exists {avatar_selection_groups} ( + avs_gid int( 10 ) NOT NULL, + name varchar(255) NOT NULL UNIQUE, + access varchar(255), + og_access varchar(255), + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avs_gid) + )"); + break; + } + db_query( "INSERT INTO {avatar_selection_groups} (avs_gid,name,access,og_access,weight) VALUES(0,'Default',NULL,NULL,0)"); } /** @@ -109,3 +126,95 @@ function avatar_selection_update_2() { } return $ret; } + +function avatar_selection_update_3() { + $ret = array(); + + switch ($GLOBALS['db_type']) { + case 'mysqli': + case 'mysql': + $ret[] = update_sql("CREATE TABLE if not exists {avatar_selection_groups} ( + avs_gid INT( 10 ) NOT NULL, + name varchar(255) NOT NULL UNIQUE, + access varchar(255), + og_access varchar(255), + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avs_gid) + ) /*!40100 DEFAULT CHARACTER SET utf8 */"); + $ret[] = update_sql("ALTER TABLE {avatar_selection} ADD COLUMN avs_gid int( 10 ) NOT NULL DEFAULT 0 FIRST"); + $ret[] = update_sql("ALTER TABLE {avatar_selection} ADD COLUMN weight tinyint( 3 ) NOT NULL DEFAULT 0"); + break; + case 'pgsql': + $ret[] = db_query("CREATE TABLE {avatar_selection_groups} ( + avs_gid INT( 10 ) NOT NULL, + name varchar(255) NOT NULL UNIQUE, + access varchar(255), + og_access varchar(255), + weight tinyint(3) NOT NULL DEFAULT 0, + PRIMARY KEY (avs_gid) + )"); + db_add_column( $ret, 'avatar_selection', 'avs_gid', 'int', array( 'not null' => true, 'default' => 0 ) ); + db_add_column( $ret, 'avatar_selection', 'weight', 'int', array( 'not null' => true, 'default' => 0 ) ); + break; + } + + $ret[] = update_sql( "UPDATE {avatar_selection} SET access = NULL WHERE access = ''" ); + $ret[] = update_sql( "UPDATE {avatar_selection} SET og_access = NULL WHERE og_access = ''" ); + $ret[] = update_sql( "INSERT INTO {avatar_selection_groups} (avs_gid, name, weight) VALUES(0, 'Default', 0)" ); + + $index = 1; + $db_results = db_query( "SELECT DISTINCT access, og_access FROM {avatar_selection} WHERE access IS NOT NULL OR og_access IS NOT NULL" ); + while( $entry = db_fetch_object( $db_results ) ) + { + _avatar_selection_update_3( $ret, $entry, $index ); + $index++; + } + $ret[] = update_sql( "ALTER TABLE {avatar_selection} DROP access" ); + $ret[] = update_sql( "ALTER TABLE {avatar_selection} DROP og_access" ); + + return $ret; +} + + +function _avatar_selection_update_3( &$ret, &$entry, $index ) +{ + $gname = 'Group ' . $index; + + $query = "INSERT INTO {avatar_selection_groups} VALUES ( " . $index . ", '" . $gname . "', "; + $update_query = "UPDATE {avatar_selection} SET avs_gid = " . $index . " WHERE "; + + if( is_null( $entry->access ) ) + { + $query .= "NULL,"; + $update_query .= "access IS NULL AND "; + } + else + { + $update_query .= "access = '" . $entry->access . "' AND "; + $exploded = explode( ',', $entry->access ); + $gids = array(); + foreach( $exploded as $gid ) + $gids[] = trim( $gid ); + $query .= "'" . implode(',',$gids) . "', "; + } + + if( is_null( $entry->og_access ) ) + { + $query .= "NULL"; + $update_query .= "og_access IS NULL"; + } + else + { + $update_query .= "og_access = '" . $entry->og_access . "'"; + $exploded = explode( ',', $entry->og_access ); + $gids = array(); + foreach( $exploded as $gid ) + $gids[] = trim( $gid ); + $query .= "'" . implode(',',$gids) ."'"; + } + + $query .= ",0 )"; + + $ret[] = update_sql( $query ); + $ret[] = update_sql( $update_query ); +} diff -r 1a2961f1238b -r 55629a4349e1 avatar_selection/avatar_selection.module --- a/avatar_selection/avatar_selection.module Thu Jan 03 20:20:49 2008 +0100 +++ b/avatar_selection/avatar_selection.module Sun Jan 06 21:05:20 2008 +0100 @@ -47,13 +47,16 @@ function avatar_selection_access($op, $n * Implementation of hook_menu(). */ function avatar_selection_menu($may_cache) { - $access = user_access('administer avatar selection'); $items = array(); if ($may_cache) { + + $access = user_access('administer avatar selection'); + $items[] = array('path' => 'admin/settings/avatar_selection', 'title' => t('Avatar Selection'), - 'callback' => 'avatar_selection_settings_page', + 'callback' => 'drupal_get_form', + 'callback arguments' => array('avatar_selection_config_form'), 'access' => $access, 'description' => t('Allows the user to upload and delete avatars.'), ); @@ -68,15 +71,16 @@ function avatar_selection_menu($may_cach 'weight' => -10, ); $items[] = array( - 'path' => 'admin/settings/avatar_selection/images', - 'title' => t('Upload'), - 'description' => t('Allows the user to upload avatars.'), - 'callback' => 'drupal_get_form', - 'callback arguments' => array('avatar_selection_images_form'), + 'path' => 'admin/settings/avatar_selection/groups', + 'title' => t('Manage Groups'), + 'description' => t('Administer avatar groups.'), + 'callback' => 'avatar_selection_groups_page', + 'callback arguments' => NULL, 'access' => $access, 'type' => MENU_LOCAL_TASK, 'weight' => -9, ); + $items[] = array( 'path' => 'admin/settings/avatar_selection/edit', 'title' => t('Manage avatars'), @@ -85,8 +89,28 @@ function avatar_selection_menu($may_cach 'callback arguments' => array('avatar_selection_edit_form'), 'access' => $access, 'type' => MENU_LOCAL_TASK, - ); - + 'weight' => -8, + ); + $items[] = array( + 'path' => 'admin/settings/avatar_selection/images', + 'title' => t('Upload'), + 'description' => t('Allows the user to upload avatars.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('avatar_selection_images_form'), + 'access' => $access, + 'type' => MENU_LOCAL_TASK, + 'weight' => -7, + ); + $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, + 'weight' => -6, + ); } return $items; @@ -135,6 +159,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, ); } @@ -208,7 +237,6 @@ function avatar_selection_user($op, &$ed } } - function avatar_selection_get_random_image($user) { $avatars = _avatar_selection_image_list($user); if ($avatars['total'] > 0) { @@ -218,105 +246,115 @@ function avatar_selection_get_random_ima return ' '; } -function _avatar_selection_image_list($user = "") { +function _avatar_selection_groups_list( $user = '' ) { + $query = "SELECT * FROM {avatar_selection_groups} avsg ORDER BY weight"; + $db_result = db_query( $query ); + $groups = array(); + while( $db_row = db_fetch_array( $db_result ) ) + { + $groups[$db_row['avs_gid']] = $db_row; + } + return $groups; +} + +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"); - while ($avatar = db_fetch_object($result)) { - $avs_image = $avatar->avatar; - $avs_access = preg_split('/\s*,\s*/', $avatar->access); - $og_access = preg_split('/\s*,\s*/', $avatar->og_access); - $del_avatar = 1; - - // check the organic groups - if (module_exists("og")) { - if (!empty($avatar->og_access)) { - if (!empty($user->og_groups) && is_array($user->og_groups)) { - foreach ($user->og_groups as $gid => $grp) { - if (in_array($gid, $og_access)) { - $del_avatar = 2; - break; - } - } - } - // delete it if it's not in one of the valid groups - if ($del_avatar == 1 && !empty($user) && $user->uid != 1) { - unset($listings[$avs_image]); - } + + $listing = array(); + + $query = "SELECT * FROM {avatar_selection} avs " . + "LEFT JOIN {avatar_selection_groups} avsg ON avs.avs_gid=avsg.avs_gid "; + + if( !empty( $user ) && !user_access('administer avatar selection') ) + { + $query .= "WHERE "; + $query .= "(avsg.access IS NULL AND avsg.og_access IS NULL) "; + + if( !empty($user->roles) ) + { + $query .= "OR ("; + $count = count( $user->roles ); + foreach( $user->roles as $rid => $role ) + { + $query .= "FIND_IN_SET( '" . $rid . "', avsg.access )"; + $count--; + + if( $count > 0 ) + $query .= " OR "; } - } - - // check the user roles - if (!empty($avatar->access)) { - if (!empty($user->roles) && is_array($user->roles)) { - foreach ($user->roles as $rid => $role) { - if (in_array($rid, $avs_access)) { - $del_avatar = 0; - break; - } - } + $query .= ") "; + } + if( module_exists("og") && !empty($user->og_access) ) { + $query .= "OR ("; + $count = count( $user->og_access ); + foreach( $user->og_access as $gid => $grp ) + { + $query .= "FIND_IN_SET( '" . $rid . "', avsg.og_access )"; + $count--; + + if( $count > 0 ) + $query .= " OR "; } - // delete it if it's not in a valid role - if ($del_avatar != 0 && !empty($user) && $user->uid != 1) { - unset($listings[$avs_image]); - } - } - } - + $query .= ") "; + } + } + + $query .= "ORDER BY avsg.weight ASC, avs.weight ASC"; + + $db_result = pager_query( $query, $avatars_per_page, $pager_id_number ); + while( $db_row = db_fetch_object($db_result) ) + { + $basename = basename( $db_row->avatar ); + $avatars[$db_row->avatar] = theme( 'image', file_create_url( $db_row->avatar ), $basename, $basename, null, false ); + } + // remove ones already in use if necessary if (!empty($user) && variable_get('avatar_selection_distinctive_avatars', FALSE)) { $result = db_query("SELECT DISTINCT avs.avatar FROM {avatar_selection} avs LEFT JOIN {users} u ON u.picture = avs.avatar WHERE u.picture IS NOT NULL AND u.uid <> %d", $user->uid); while ($avatar = db_fetch_object($result)) { $avs_image = $avatar->avatar; - unset($listings[$avs_image]); - } - } - - $total_avatars = count($listings); - - foreach ($listings as $listing) { - $avatars[$dir .'/'. $listing->basename] = theme('image', file_create_url($dir .'/'. $listing->basename), $listing->basename, $listing->basename, NULL, FALSE); + unset($avatars[$avs_image]); + } } $selects['avatars'] = $avatars; - $selects['total'] = $total_avatars; + $selects['total'] = count( $avatars ); return $selects; } -function avatar_selection_settings_page($op = NULL, $aid = NULL) { - - switch ($op) { - case 'edit': - if (is_numeric($aid)) { - $output = drupal_get_form('avatar_selection_config_form', $aid); - } - break; - case 'upload': - if (is_numeric($aid)) { - $output = drupal_get_form('avatar_selection_images_form', $aid); - } - break; - case 'list' : - if (is_numeric($aid)) { - $output = drupal_get_form('avatar_selection_edit_form'); - } - break; - default: - $form[] = array( - '#type' => 'fieldset', - '#title' => t('Add another'), - ); - $output .= drupal_get_form('avatar_selection_config_form'); - break; +function avatar_selection_groups_page( $op = NULL, $gid = NULL ) +{ + $output = ''; + if( isset( $op ) && $op == 'edit' && isset( $gid ) && is_numeric( $gid ) ) + { + $output .= drupal_get_form( 'avatar_selection_edit_group_form', $gid ); + } + else + { + $groups = _avatar_selection_groups_list(); + + $header = array( + t('Group Name'), + ' ', + ); + $rows = array(); + foreach( $groups as $gid => $group ) + { + $rows[] = array( + $group['name'], + l( t('edit'), 'admin/settings/avatar_selection/groups/edit/' . $gid ), + ); + } + + $output .= theme( 'table', $header, $rows ); + $output .= drupal_get_form( 'avatar_selection_new_group_form' ); } return $output; } - /** * Define a form to configure the module settings @@ -333,7 +371,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.'), @@ -370,6 +415,150 @@ function avatar_selection_config_form() return $form; } +function avatar_selection_groups_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')))); + } + $groups = _avatar_selection_groups_list(); + $form = array(); + + if( !isset( $form_values ) ) + { + $step = 'list'; + } + else + { + $step = 'edit'; + } + $form['step'] = array( + '#type' => 'value', + '#value' => $step, + ); + $form['#multistep'] = TRUE; + + if( $step == 'list' ) { + $options = array(); + foreach( $groups as $gid => $group ) { + $options[$gid] = $group['name']; + } + $form['edit'] = array( + '#type' => 'fieldset', + ); + $form['edit']['avs_gid'] = array( + '#type' => 'select', + '#title' => t('Avatar Group'), + '#options' => $options, + '#default_value' => '', + '#description' => t('Select an avatar group to edit.'), + '#weight' => 0, + ); + $form['edit']['edit'] = array( + '#type' => 'submit', + '#value' => t('Edit'), + '#weight' => 1, + ); + + $form['#redirect'] = FALSE; + } + else if( $step == 'edit' ) { + + + } + + return $form; +} + +function avatar_selection_edit_group_form( $gid, $form_values = NULL ) +{ + $roles = avatar_selection_handler_filter_role(); + $group = db_fetch_array( db_query( "SELECT * FROM {avatar_selection_groups} avsg WHERE avs_gid = %d", $gid ) ); + + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#description' => t('Must be unique.'), + '#default_value' => $group['name'], + '#weight' => 0, + ); + + $form['avs_gid'] = array( + '#type' => 'hidden', + '#value' => $gid, + ); + + $form['permissions'] = array( + '#type' => 'fieldset', + '#title' => t('Permissions'), + '#weight' => 1, + ); + $form['permissions']['access'] = array( + '#type' => 'checkboxes', + '#title' => t('User Roles'), + '#default_value' => explode( ',', $group['access'] ), + '#options' => $roles, + '#description' => t('Only the checked roles will be able to select avatar icons belonging to this group. If none are checked, anyone can select avatars in this group.'), + ); + + if (module_exists("og")) { + $form['permissions']['og_access'] = array('#type' => 'checkboxes', + '#title' => t('Organic Groups'), + '#default_value' => explode( ',', $group['og_access'] ), + '#options' => og_all_groups_options(), + '#description' => t('Only users in the checked organic groups will be able to select avatar icons belonging to this group. If none are checked, any organic group can select avatars in this group.'), + ); + } + + $weights = array(); + for( $i = -10; $i < 10; $i++ ) + $weights[$i] = $i; + + $form['weight'] = array( + '#type' => 'select', + '#title' => t('Weight'), + '#description' => t('Lighter things float, heavy things sink.'), + '#options' => $weights, + '#default_value' => $group['weight'], + '#weight' => 8, + ); + + $form['update'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#weight' => 9, + ); + if( $gid != 0 ) + { + $form['delete'] = array( + '#type' => 'submit', + '#value' => t('Delete'), + '#weight' => 10, + ); + } + + return $form; +} + +function avatar_selection_new_group_form() { + $form = array(); + + $form['new'] = array( + '#type' => 'fieldset', + ); + $form['new']['name'] = array( + '#type' => 'textfield', + '#title' => t('New Group'), + '#description' => t('The name of the new group, must be unique.'), + '#weight' => 0, + ); + $form['new']['create'] = array( + '#type' => 'submit', + '#value' => t('Create'), + '#weight' => 1, + ); + + return $form; +} + /** * Define a form to upload the avatar images. */ @@ -385,23 +574,37 @@ function avatar_selection_images_form() '#size' => 48, '#description' => t('A new avatar image. Maximum dimensions are %dimensions and the maximum size is %size kB. Images must have one of the following extensions (case sensitive): png, jpg, jpeg, gif, PNG, JPG, JPEG, GIF.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''), ); - $form['permissions'] = array( - '#type' => 'fieldset', - '#title' => t('Permissions'), - '#weight' => 1, - ); - $form['permissions']['access'] = array('#type' => 'checkboxes', - '#title' => t('User Roles'), - '#options' => avatar_selection_handler_filter_role(), - '#description' => t('Only the checked roles will be able to see this avatar icon; if no roles are checked, access will not be restricted.'), - ); - if (module_exists("og")) { - $form['permissions']['og_access'] = array('#type' => 'checkboxes', - '#title' => t('Organic Groups'), - '#options' => og_all_groups_options(), - '#description' => t('Only users in the checked organic groups will be able to see this avatar icon; if no groups are checked, access will not be restricted.'), - ); - } + + $groups = _avatar_selection_groups_list(); + $options = array(); + foreach( $groups as $gid => $group ) { + $options[$gid] = $group['name']; + } + + $weights = array(); + for( $i = -10; $i < 10; $i++ ) + $weights[$i] = $i; + + $form['edit'] = array( + '#type' => 'fieldset', + '#title' => t('Avatar Properties'), + ); + $form['edit']['avs_gid'] = array( + '#type' => 'select', + '#title' => t('Group'), + '#options' => $options, + '#default_value' => 0, + '#description' => t('Select an avatar group to edit.'), + '#weight' => 5, + ); + $form['edit']['weight'] = array( + '#type' => 'select', + '#title' => t('Weight'), + '#options' => $weights, + '#default_value' => 0, + '#description' => t('The weight of this avatar. Lighter things float heavy things sink.' ), + '#weight' => 6, + ); $form['attach'] = array( '#type' => 'submit', @@ -443,6 +646,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,54 +669,60 @@ 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'); - $roles = avatar_selection_handler_filter_role(); - - $result = db_query("SELECT avatar, access, og_access FROM {avatar_selection} avs WHERE avatar = '%s'", $form_values['select_avatar']); - while ($avatar = db_fetch_object($result)) { - $avs_access = preg_split('/\s*,\s*/', $avatar->access); - $og_access = preg_split('/\s*,\s*/', $avatar->og_access); + + $avatar = db_fetch_array( db_query("SELECT * FROM {avatar_selection} avs WHERE avatar = '%s'", $form_values['select_avatar']) ); + + $filename = $form_values['select_avatar']; + $image = theme('image', $filename, basename($filename), basename($filename) ); + + $form['avatar_image'] = array( + '#value' => $image + ); + $form['avatar'] = array( + '#type' => 'hidden', + '#value' => $filename, + ); + + $groups = _avatar_selection_groups_list(); + $options = array(); + foreach( $groups as $gid => $group ) { + $options[$gid] = $group['name']; } - - $image = theme('image', $form_values['select_avatar']); - $form['avatar_image'] = array('#value' => $image); - $form['select_avatar'] = array( - '#type' => 'value', - '#value' => $form_values['select_avatar'], - ); - - $form['permissions'] = array( - '#type' => 'fieldset', - '#title' => t('Permissions'), - '#weight' => 1, - ); - $form['permissions']['access'] = array( - '#type' => 'checkboxes', - '#title' => t('User Roles'), - '#default_value' => $avs_access, - '#options' => $roles, - '#description' => t('Only the checked roles will be able to see this avatar icon; if no roles are checked, access will not be restricted.'), - ); - - if (module_exists("og")) { - $form['permissions']['og_access'] = array('#type' => 'checkboxes', - '#title' => t('Organic Groups'), - '#default_value' => $og_access, - '#options' => og_all_groups_options(), - '#description' => t('Only users in the checked organic groups will be able to see this avatar icon; if no groups are checked, access will not be restricted.'), - ); - } - + + $weights = array(); + for( $i = -10; $i < 10; $i++ ) + $weights[$i] = $i; + + $form['edit'] = array( + '#type' => 'fieldset', + '#title' => t('Avatar Properties'), + ); + $form['edit']['avs_gid'] = array( + '#type' => 'select', + '#title' => t('Group'), + '#options' => $options, + '#default_value' => $avatar['avs_gid'], + '#description' => t('Select an avatar group to edit.'), + '#weight' => 5, + ); + $form['edit']['weight'] = array( + '#type' => 'select', + '#title' => t('Weight'), + '#options' => $weights, + '#default_value' => $avatar['weight'], + '#description' => t('The weight of this avatar. Lighter things float heavy things sink.' ), + '#weight' => 6, + ); + $form['update'] = array( '#type' => 'submit', '#value' => t('Update'), @@ -523,8 +740,35 @@ 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(); + + $groups = _avatar_selection_groups_list(); + $options = array(); + foreach( $groups as $group ) + { + $options[$group['avs_gid']] = $group['name']; + } + + $form['avs_gid'] = array( + '#type' => 'select', + '#title' => t('Avatar Group'), + '#options' => $options, + '#default_value' => 'Default', + '#description' => t('Any new avatar images found will be added to this avatar group.'), + ); + $form['scan'] = array( + '#type' => 'submit', + '#value' => t('Scan Now'), + ); + return $form; +} + +/**" . $values_names . " * Validate the submission. * * Check whether: @@ -538,9 +782,13 @@ 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; + } + + } +} /** * Validate the submission. @@ -607,6 +855,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.')); } } @@ -625,12 +874,8 @@ function avatar_selection_images_form_su if ($file = file_save_upload($source, $dir)) { if (image_get_info($file->filepath)) { - $access = implode(', ', array_keys(array_filter($form_values['access']))); - if (module_exists("og")) { - $og = implode(', ', array_keys(array_filter($form_values['og_access']))); - } - _avatar_selection_save_avatar_info($file->filepath, $access, $og); - drupal_set_message(t('New image saved.')); + _avatar_selection_save_avatar_info( $form_values['avs_gid'], $file->filepath, $form_values['weight'] ); + drupal_set_message(t('New image saved.')); } else { file_delete($file->filepath); @@ -649,18 +894,119 @@ function avatar_selection_edit_form_subm $op = $form_values['op']; if ($op == t('Update')) { - $access = implode(', ', array_keys(array_filter($form_values['access']))); - if (module_exists("og")) { - $og = implode(', ', array_keys(array_filter($form_values['og_access']))); - } - _avatar_selection_save_avatar_info($form_values['select_avatar'], $access, $og); - drupal_set_message(t('Image updated.')); - } - + _avatar_selection_save_avatar_info( $form_values['avs_gid'], $form_values['avatar'], $form_values['weight'] ); + } else if ($op == t('Delete')) { - $image = $form_values['select_avatar']; + $image = $form_values['avatar']; avatar_selection_image_delete($image); } +} + +function avatar_selection_edit_group_form_validate( $form_id, $form_values ) { + $error = FALSE; + if( $form_values['op'] == t('Update') ) { + if( empty( $form_values['name'] ) ) { + form_set_error( 'name', t('Groups must have a name.') ); + $error = TRUE; + } + + if( !$error ) + { + $result = db_result( + db_query( + "SELECT name FROM {avatar_selection_groups} WHERE name LIKE '%s' AND avs_gid <> %d", + $form_values['name'], + $form_values['avs_gid'] + ) + ); + + if( $result ) + { + form_set_error( 'name', t('Name must be unique.') ); + $error = TRUE; + } + } + } + else if( $form_values['op'] == t('Delete') ) { + if( $form_values['avs_gid'] == 0 ) { + form_set_error( '', t('You cannot delete this group.') ); + $error = TRUE; + } + + } +} + +function avatar_selection_edit_group_form_submit( $form_id, $form_values ) { + $op = $form_values['op']; + + if( $op == t('Update') ) { + $access = array(); + foreach( $form_values['access'] as $role => $checked ) + { + if( $checked != 0 ) + $access[] = $role; + } + $og_access = array(); + if (module_exists("og")) { + foreach( $form_values['og_access'] as $role => $checked ) + { + if( $checked != 0 ) + $og_access[] = $role; + } + } + _avatar_selection_save_group_info( $form_values['avs_gid'], $form_values['name'], $form_values['weight'], $access, $og_access ); + } + else if ( $op == t('Delete') ) { + db_query( "DELETE FROM {avatar_selection_groups} WHERE avs_gid = %d", $form_values['avs_gid'] ); + db_query( "UPDATE {avatar_selection} SET avs_gid = 0 WHERE avs_gid = %d", $form_values['avs_gid'] ); + drupal_set_message( t('Group deleted.') ); + } + return 'admin/settings/avatar_selection/groups'; +} + +function avatar_selection_new_group_form_validate( $form_id, $form_values ) { + $error = FALSE; + if( empty( $form_values['name'] ) ) { + form_set_error( 'name', t('Groups must have a name.') ); + $error = TRUE; + } + $result = db_result( db_query( "SELECT name FROM {avatar_selection_groups} WHERE name LIKE '%s'", $form_values['name'] ) ); + + if( $result ) + { + form_set_error( 'name', t('Name must be unique.') ); + $error = TRUE; + } +} + +function avatar_selection_new_group_form_submit( $form_id, $form_values ) { + $avs_gid = db_result( db_query( "SELECT MAX(avs_gid)+1 FROM {avatar_selection_groups}" ) ); + _avatar_selection_save_group_info( $avs_gid, $form_values['name'], 0, array(), array() ); + return 'admin/settings/avatar_selection/groups/edit/' . $avs_gid; +} + +function avatar_selection_scan_form_submit( $form_id, $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); + + $group = $form_values['avs_gid']; + + $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; + foreach( $listings as $listing ) + { + _avatar_selection_save_avatar_info( $group, $listing->filename, 0 ); + $count++; + } + drupal_set_message( t('Added %count avatars to the database.', array( '%count' => $count ) ) ); } function avatar_selection_image_delete($image) { @@ -711,18 +1057,67 @@ function avatar_selection_handler_filter /* * function to save avatar icon details to the avatar_selection table */ -function _avatar_selection_save_avatar_info($filepath, $access, $og = "") { - +function _avatar_selection_save_avatar_info( $group, $filepath, $weight ) { + $value_names = "(avs_gid, avatar, weight)"; + $values_str = "(%d,'%s', %d)"; + $values = array( $group, $filepath, $weight ); + switch ($GLOBALS['db_type']) { case 'mysqli': case 'mysql': - $result = db_query("REPLACE INTO {avatar_selection} (avatar, access, og_access) VALUES('%s', '%s', '%s')", $filepath, $access, $og); + $result = db_query("REPLACE INTO {avatar_selection} ". $value_names . " VALUES" . $values_str, $values ); break; case 'pgsql': $result = db_query("DELETE FROM {avatar_selection} WHERE avatar = '%s'", $filepath); - $result = db_query("INSERT INTO {avatar_selection} (avatar, access, og_access) VALUES('%s', '%s', '%s')", $file->filepath, $access, $og); + $result = db_query("INSERT INTO {avatar_selection} ". $value_names . " VALUES" . $values_str, $values ); break; } - -} +} + +/* + * function to save avatar group details to the avatar_selection_group table + */ +function _avatar_selection_save_group_info( $gid, $name, $weight, $access = array(), $og_access = array() ) +{ + $value_names = ""; + $values_str = "(%d, '%s', %d, "; + $values = array( $gid, $name, $weight ); + + $access = implode( ',', $access ); + $og_access = implode( ',', $og_access ); + + if( $access == '' ) + { + $access = NULL; + $values_str .= "NULL, "; + } + else + { + $values_str .= "'%s', "; + $values[] = $access; + } + + if( $og_access == '' ) + { + $og_access = NULL; + $values_str .= "NULL )"; + } + else + { + $values_str .= "'%s' )"; + $values[] = $og_access; + } + + $result = db_query("DELETE FROM {avatar_selection_groups} WHERE avs_gid = %d", $gid); + $result = db_query("INSERT INTO {avatar_selection_groups} (avs_gid, name, weight, access, og_access) VALUES" . $values_str, $values ); +} + + +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'] ); + $output .= drupal_render( $elements ); + return $output; +}