You are here

function user_badges_edit_form in User Badges 7

Same name and namespace in other branches
  1. 5 user_badges.module \user_badges_edit_form()
  2. 6.2 user_badges.admin.inc \user_badges_edit_form()
  3. 6 user_badges.admin.inc \user_badges_edit_form()
  4. 7.2 user_badges.admin.inc \user_badges_edit_form()
  5. 7.3 user_badges.admin.inc \user_badges_edit_form()

Define the edit form for userbadges.

1 string reference to 'user_badges_edit_form'
user_badges_menu in ./user_badges.module
Implements hook_menu().

File

./user_badges.admin.inc, line 213
@brief User Badges admin functions

Code

function user_badges_edit_form($form, $form_state, $badge = NULL) {

  // If we have been given an existing badge then pass the bid.
  if ($badge) {
    $form['bid'] = array(
      '#type' => 'value',
      '#value' => $badge->bid,
    );
  }

  // General information fieldset.
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General'),
    '#collapsible' => TRUE,
    '#weight' => -10,
  );
  $form['general']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $badge ? $badge->name : '',
    '#size' => 40,
    '#maxlength' => 100,
    '#description' => t('Name for the badge. Will be displayed as a tooltip when rolling over the badge.'),
    '#required' => TRUE,
    '#weight' => -10,
  );

  // Let the user choose the image location (URL, upload, library).
  $form['general']['image_location'] = array(
    '#type' => 'radios',
    '#title' => t('Image location'),
    '#options' => array(
      'external' => t('External'),
      'upload' => t('Upload new image'),
      'library' => t('Image from library'),
    ),
    // Are we using a library image (integer) or an image URL (string)?
    '#default_value' => !$badge || !file_valid_uri($badge->image) ? 'external' : 'library',
  );

  // External url.
  $form['general']['imageurl'] = array(
    '#type' => 'textfield',
    '#title' => t('Image URL'),
    '#default_value' => isset($badge->image) && !file_valid_uri($badge->image) ? $badge->image : '',
    '#size' => 60,
    '#maxlength' => 255,
    '#description' => t('The image URL for this badge. If you want to use an image from the user badges image library, select from the list below.'),
    '#states' => array(
      'visible' => array(
        ':input[name="image_location"]' => array(
          'value' => 'external',
        ),
      ),
    ),
  );

  // File upload.
  $form['general']['imagefile'] = array(
    '#type' => 'managed_file',
    '#title' => t('Upload image'),
    '#description' => t('The image file for this badge. This image will then be available in the library after badge creation.'),
    '#states' => array(
      'visible' => array(
        ':input[name="image_location"]' => array(
          'value' => 'upload',
        ),
      ),
    ),
  );

  // File from library.
  $form['general']['imagefromlibrary'] = array(
    '#title' => t('Image from library'),
    '#states' => array(
      'visible' => array(
        ':input[name="image_location"]' => array(
          'value' => 'library',
        ),
      ),
    ),
  );
  $images = _user_badges_images_from_library();
  if (count($images)) {
    $options = array();

    // Transform images to options
    foreach ($images as $path => $image_data) {
      $options[$path] = $image_data->image;
    }
    $form['general']['imagefromlibrary'] += array(
      '#type' => 'radios',
      '#default_value' => isset($badge->image) && file_valid_uri($badge->image) ? $badge->image : reset(array_keys($options)),
      '#options' => $options,
    );
  }
  else {
    $form['general']['imagefromlibrary'] += array(
      '#type' => 'item',
      '#description' => t('No badge images uploaded yet. You can upload an image by choosing "Upload image" above or choose to use an external URL.'),
    );
  }

  // Provide other general informations.
  $form['general']['weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Weight'),
    '#size' => 4,
    '#maxlength' => 10,
    '#default_value' => $badge ? $badge->weight : 0,
    '#description' => t('Lighter weighted items float to the top of lists. Heavier items go to the bottom. You must enter a number (negative values are allowed).'),
    '#required' => TRUE,
    '#weight' => 1,
  );
  $form['general']['href'] = array(
    '#type' => 'textfield',
    '#title' => t('Description URL'),
    '#size' => 60,
    '#maxlength' => 255,
    '#description' => t('You can specify here the link where your badge will redirect your user.
      This is useful for explanation pages about the badge, for instance. If you do not wish
      your badge to be clickable, please leave this field empty.') . '<br />' . '<u>' . t('Tips:') . '</u>' . '<ul>' . '<li>' . t('If you provide the full URL, it will be considered an external URL.') . '</li>' . '<li>' . t('If you provide only the path (e.g. "admin/content/node"), it is considered an
          internal link.') . '</li>' . '<li>' . t('Use %none to override a default link in the settings tab with no link for this badge.', array(
      '%none' => '<none>',
    )) . '</li>' . '</ul>',
    '#default_value' => $badge ? $badge->href : '',
    '#weight' => 1,
  );

  // Tokens help.
  if (module_exists('token')) {
    $form['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => -3,
    );
    $form['token_help']['help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'userbadge',
        'user',
      ),
    );
  }
  else {
    $form['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => -3,
    );
    $form['token_help']['help'] = array(
      '#value' => t('Install the <a href="!link">Token</a> module if you want this URL to include dynamic elements such as badge ID numbers.', array(
        '!link' => url('http://drupal.org/project/token', array(
          'absolute' => TRUE,
        )),
      )),
    );
  }

  // Display options.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -2,
  );
  $form['options']['unhideable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cannot be Hidden'),
    '#default_value' => $badge ? $badge->unhideable : '',
    '#description' => t('If this is set, the badge cannot be hidden by being moved down in weight. It will always show up.'),
  );
  $form['options']['fixedweight'] = array(
    '#type' => 'checkbox',
    '#title' => t('Fixed Weight'),
    '#default_value' => $badge ? $badge->fixedweight : '',
    '#description' => t('If this is set, the badge cannot have a user assigned weight, regardless of settings.'),
  );
  $form['options']['doesnotcounttolimit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Does Not Count to Limit'),
    '#default_value' => !empty($edit) ? $edit->doesnotcounttolimit : '',
    '#description' => t('If this is set, the badge does not count towards the limit for number of badges to display per user.'),
  );
  if (module_exists('taxonomy')) {
    $badges_vid = variable_get('user_badges_vid', '');
    if (!empty($badges_vid)) {
      $terms = user_badges_get_term_from_vid($badges_vid);
      $form['tid'] = array(
        '#title' => t('Taxonomy Relation'),
        '#type' => 'select',
        '#options' => !empty($terms) ? $terms : array(
          t('There are no terms in the selected vocabulery'),
        ),
        '#default_value' => $badge ? $badge->tid : '',
        '#weight' => -1,
      );
    }
    else {
      $form['tid']['help'] = array(
        '#value' => t('Activate the taxonomy module if you want to associate badges with taxonomy terms.'),
        '#weight' => -1,
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save badge'),
    '#weight' => 1,
  );
  return $form;
}