You are here

public function application_manager::approve_application in Apply for role 8

Parameters

mixed $application: Pass either an AID to load an application, or an actual application object.

Return value

bool

File

src/application_manager.php, line 137
Contains two classes.

Class

application_manager
Application manager object used for performing any tasks relating to applications.

Namespace

Drupal\apply_for_role

Code

public function approve_application($application) {
  if (!is_object($application)) {
    $application = $this
      ->get_application($application);
  }

  // Check if the application has been denied/approved already, and do not proceed if so.
  if ($application
    ->get('status') != 0) {
    return FALSE;
  }
  else {
    $user = \Drupal::service('entity_type.manager')
      ->getStorage('user')
      ->load($application
      ->get('uid'));
    foreach ($application
      ->get('rids') as $role_to_add) {
      $user
        ->addRole($role_to_add);
    }
    $user
      ->save();
    if ($this->apply_for_role_config
      ->get('send_user_approval_email')) {

      // @TODO: Replace deprecated user_load function.
      $to = user_load($application
        ->get('uid'))
        ->getEmail();
      $subject = $this->apply_for_role_config
        ->get('send_user_approval_subject');
      $body = $this->apply_for_role_config
        ->get('send_user_approval_body');
      $replacements = array(
        '%URL' => \Drupal::request()
          ->getHost(),
        '%ROLE' => $this
          ->rids_to_text($application
          ->get('rids')),
      );
      $this
        ->send_email($to, $subject, $body, $replacements);
    }

    // Mark the application as approved.
    Database::getConnection()
      ->update('apply_for_role_applications')
      ->fields(array(
      'status' => 1,
    ))
      ->condition('aid', $application
      ->get('aid'))
      ->execute();
  }
}