You are here

function avatar_selection_edit_form in Avatar Selection 7

Same name and namespace in other branches
  1. 5.2 avatar_selection.module \avatar_selection_edit_form()
  2. 5 avatar_selection.module \avatar_selection_edit_form()
  3. 6 avatar_selection.admin.inc \avatar_selection_edit_form()

Create the form structure for listing the avatars and managing them in the 'Manage Avatars' tab under the Avatar Selection administration page.

Parameters

$form_state: General variable, used to control the processing of the form.

Return value

Return the structure of the form.

2 string references to 'avatar_selection_edit_form'
avatar_selection_roles_page in ./avatar_selection.admin.inc
Returns appropriate avatar set by role or og for configuration.
avatar_selection_settings_page in ./avatar_selection.admin.inc
Select which form will be shown to the user, according to the permissions.

File

./avatar_selection.admin.inc, line 393
Administrative page callbacks for the avatar_selection module.

Code

function avatar_selection_edit_form($form, $form_state) {
  $form = array();
  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 <a href="@url">User settings</a> page.', array(
      '@url' => url('admin/config/people/accounts'),
    )));
  }
  drupal_add_css(drupal_get_path('module', 'avatar_selection') . '/avatar_selection.css');
  $set_type = arg(5);
  $set_id = arg(6);

  // We find out the current page number.
  $page = 0;
  if (isset($_GET['page']) && is_numeric($_GET['page'])) {
    $page = $_GET['page'];
  }
  $avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30);
  $selects = _avatar_selection_image_list('', $set_type, $set_id, $page * $avatars_per_page, $avatars_per_page);
  if (!count($selects['avatars'])) {
    drupal_set_message(t('There are no avatars configured.'));
  }
  else {
    if (!isset($form_state['values'])) {
      $step = 'list';
    }
    else {
      $step = 'edit';
    }
    $form['step'] = array(
      '#type' => 'value',
      '#value' => $step,
    );
    if ($step == 'list') {
      if ($set_type == 'role') {
        $sets = avatar_selection_handler_filter_role();
      }
      elseif ($set_type == 'og' && module_exists('og')) {
        $sets = avatar_selection_og_options();
      }
      $sets[0] = t('All roles');
      drupal_set_title(t('Manage Avatars - %name', array(
        '%name' => $sets[$set_id],
      )), PASS_THROUGH);
      drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js');
      $js_file = drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection.js';
      $form['select_avatar'] = array(
        '#type' => 'radios',
        '#title' => t('Select an avatar to edit'),
        '#options' => $selects['avatars'],
        '#required' => TRUE,
        '#attributes' => array(
          'class' => array(
            'user-avatar-select',
          ),
        ),
        '#suffix' => theme('avatar_selection_pager', array(
          'total' => $selects['total'],
          'limit' => $avatars_per_page,
          'js_file' => '/' . $js_file,
        )),
      );
      $form['search'] = array(
        '#type' => 'submit',
        '#value' => t('Edit'),
        '#submit' => array(
          'avatar_selection_edit_list_form_submit',
        ),
      );
      drupal_add_js($js_file);
    }
    elseif ($step == 'edit') {
      drupal_set_title(t('Manage Avatars'));
      $form_state['#redirect'] = array(
        'admin/config/people/avatar_selection/edit',
      );
      $roles = avatar_selection_handler_filter_role();
      $aid = 0;
      $avs_access = $og_access = array();
      $weight = 0;
      $name = '';
      $query = db_select('avatar_selection', 'avs')
        ->fields('avs', array(
        'aid',
        'avatar',
        'name',
        'weight',
      ))
        ->condition('avs.fid', $form_state['values']['select_avatar']);
      $query
        ->leftJoin('avatar_selection_roles', 'avsr', 'avs.aid = avsr.aid');
      $query
        ->addField('avsr', 'rid');
      $query
        ->leftJoin('avatar_selection_og', 'avso', 'avs.aid = avso.aid');
      $query
        ->addField('avso', 'ogid');
      $result = $query
        ->orderBy('weight', 'ASC')
        ->orderBy('name', 'ASC')
        ->orderBy('avatar', 'ASC')
        ->execute();
      foreach ($result as $avatar) {
        $aid = $avatar->aid;
        $name = $avatar->name;
        $weight = $avatar->weight;
        if ($avatar->rid) {
          array_push($avs_access, $avatar->rid);
        }
        if ($avatar->ogid) {
          array_push($og_access, $avatar->ogid);
        }
      }
      $image_path = file_build_uri('avatar_selection');
      file_prepare_directory($image_path, FILE_CREATE_DIRECTORY);
      $selected_avatar = $avatar->avatar;
      $image = theme('image', array(
        'path' => $image_path . '/' . $selected_avatar,
      ));
      $form['avatar_image'] = array(
        '#type' => 'item',
        '#title' => t('Image'),
        '#markup' => $image,
      );
      $form['aid'] = array(
        '#type' => 'value',
        '#value' => $aid,
      );
      $form['select_avatar'] = array(
        '#type' => 'value',
        '#value' => $form_state['values']['select_avatar'],
      );
      $form['avatar_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#description' => t("Image name or title which will be displayed when hovering over the image.  It's also used in conjunction with the weight setting for sorting the avatars."),
        '#size' => 48,
        '#default_value' => $name,
      );
      $form['avatar_weight'] = array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#delta' => 100,
        '#description' => t('Avatars with a lower weight appear before higher weighted avatars in lists.'),
        '#default_value' => $weight,
      );
      $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' => avatar_selection_og_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.'),
        );
      }
      $form['update'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
        '#weight' => 9,
        '#submit' => array(
          'avatar_selection_edit_update_form_submit',
        ),
      );
      $form['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#weight' => 10,
        '#submit' => array(
          'avatar_selection_edit_delete_form_submit',
        ),
      );
    }
  }
  return $form;
}