You are here

function user_badges_edit_form_submit in User Badges 7.3

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 user_badges.admin.inc \user_badges_edit_form_submit()
  5. 7.2 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 468
@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,
    ));

    // Remove image usage for old image.
    $old_image = db_select('user_badges_badges', 'ubb')
      ->fields('ubb', array(
      'image',
    ))
      ->condition('ubb.bid', $edit->bid)
      ->execute()
      ->fetchField();
    if ($old_image) {
      $file = _user_badges_managed_file($old_image);
      if ($file) {
        file_usage_delete($file, 'user_badges', 'user_badge', $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 and execute.
  $result = $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();
  $bid = isset($edit->bid) ? $edit->bid : $result;
  if ($bid) {
    if ($edit->image_location == 'upload') {

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

          // 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();
          $image = $file->uri;
        }
        else {
          $result = FALSE;
          watchdog('user_badges', 'Failed to save image file after upload.', array(), WATCHDOG_ERROR);
        }
      }
      else {
        $result = FALSE;
        watchdog('user_badges', 'Failed to load image file after upload.', array(), WATCHDOG_ERROR);
      }
    }
    elseif ($edit->image_location == 'library') {

      // Record that the badge is using the file.
      $file = _user_badges_managed_file($image);
      if ($file) {
        file_usage_add($file, 'user_badges', 'user_badge', $bid);
      }
    }
  }
  else {
    $result = FALSE;
    watchdog('user_badges', 'Invalid badge ID.', array(), WATCHDOG_ERROR);
  }
  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'] = USER_BADGES_ADMIN_PATH;
}