You are here

function user_badges_edit_form_submit in User Badges 7

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

Process user_badges_edit_form form submissions.

Inserts the badge into the DB and sets a success message

File

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

Code

function user_badges_edit_form_submit($form, &$form_state) {
  $edit = $form_state['values'];

  // Let other modules know about this.
  module_invoke_all('user_badges_edit_badge', $edit);
  $edit = (object) $edit;
  if (isset($edit->bid)) {

    // If the badge already exists, delete it and re-insert it.
    $result = db_merge('user_badges_badges')
      ->key(array(
      'bid' => $edit->bid,
    ));
  }
  else {

    // If the badge does not exist, insert it.
    $result = db_insert('user_badges_badges');
  }

  // Define image data (string or integer)
  switch ($edit->image_location) {
    case 'external':
      $image = trim($edit->imageurl);
      break;
    case 'upload':
      $image = $edit->imagefile;
      break;
    case 'library':
      $image = $edit->imagefromlibrary;
  }

  // Add the rest of the fields.
  $bid = $result
    ->fields(array(
    'name' => trim($edit->name),
    'image' => $image,
    'weight' => $edit->weight,
    'href' => !empty($edit->href) ? trim($edit->href) : NULL,
    'unhideable' => $edit->unhideable,
    'fixedweight' => $edit->fixedweight,
    'doesnotcounttolimit' => $edit->doesnotcounttolimit,
    'tid' => !empty($edit->tid) ? $edit->tid : NULL,
  ))
    ->execute();
  if ($edit->image_location == 'upload') {
    $bid = isset($edit->bid) ? $edit->bid : $bid;

    // Make the uploaded file permanent.
    $file = file_load($form_state['values']['imagefile']);
    $file->status = FILE_STATUS_PERMANENT;
    $file = file_move($file, 'public://badges');

    // Record that the module is using the file.
    file_usage_add($file, 'user_badges', 'user_badge', $bid);

    // Update the image url for the badge
    $result = db_merge('user_badges_badges')
      ->key(array(
      'bid' => $bid,
    ))
      ->fields(array(
      'image' => $file->uri,
    ))
      ->execute();
  }
  if ($result) {
    drupal_set_message(t('Badge %badgename saved.', array(
      '%badgename' => $edit->name,
    )));
  }
  else {
    drupal_set_message(t('There was a problem saving the badge information into the database.'));
  }
  $form_state['redirect'] = BASE_ADMIN_PATH;
}