You are here

class apply_for_role_application_admin_page in Apply for role 8

Hierarchy

Expanded class hierarchy of apply_for_role_application_admin_page

File

src/Controller/apply_for_role_application_admin_page.php, line 18

Namespace

Drupal\apply_for_role\Controller
View source
class apply_for_role_application_admin_page extends ControllerBase {
  private $application_manager;
  private $applications_per_page;

  /**
   * Constructor, loads application manager and amount of applications per page is set here.
   */
  public function __construct() {
    $this->application_manager = new application_manager();
    $this->applications_per_page = 20;
  }

  /**
   * Content, renders table of applications, paged.
   */
  public function content() {
    $table_header = array(
      $this
        ->t('Application id'),
      $this
        ->t('User Name (UID)'),
      $this
        ->t('Roles'),
      $this
        ->t('Status'),
      $this
        ->t('Created'),
      $this
        ->t('Message'),
      $this
        ->t('Operations'),
    );
    $pager_number = \Drupal::request()->query
      ->getInt('page');
    $total_application_count = Database::getConnection()
      ->select('apply_for_role_applications', 'a')
      ->countQuery()
      ->execute()
      ->fetchField();
    $results = Database::getConnection()
      ->select('apply_for_role_applications', 'a')
      ->fields('a')
      ->orderBy('aid', 'DESC')
      ->range($pager_number * 20, 20)
      ->execute();
    $page = pager_default_initialize($total_application_count, $this->applications_per_page);
    $table_render = array(
      'table' => array(
        '#type' => 'table',
        '#header' => $table_header,
        '#empty' => $this
          ->t('Currently there are no applications available for review.'),
      ),
      'page' => array(
        '#type' => 'pager',
      ),
    );
    while ($result_row = $results
      ->fetchAssoc()) {
      $table_render['table'][$result_row['aid']] = array(
        'aid' => array(
          '#plain_text' => $result_row['aid'],
        ),
        'uid' => array(
          '#plain_text' => $this
            ->display_username($result_row['uid']),
        ),
        'rids' => array(
          '#plain_text' => $this->application_manager
            ->rids_to_text(Serialization\Json::decode($result_row['rids'])),
        ),
        'status' => array(
          '#plain_text' => $this
            ->display_status($result_row['status']),
        ),
        'created' => array(
          '#plain_text' => $result_row['created'],
        ),
        'message' => array(
          '#plain_text' => $result_row['message'],
        ),
        'operations' => array(
          '#markup' => $this
            ->generate_operations_links($result_row['aid'], $result_row['status']),
        ),
      );
    }
    return $table_render;
  }

  /**
   * Display a presentable username.
   */
  protected function display_username($uid) {
    $account = \Drupal\user\Entity\User::load($uid);
    return $account
      ->getDisplayName() . ' (' . $uid . ')';
  }

  /**
   * Helper function to display a clean text representation of a status.
   */
  protected function display_status($status) {
    switch ($status) {
      case 0:
        return 'Needs Review';
        break;
      case 1:
        return 'Accepted';
        break;
      case 2:
        return 'Denied';
        break;
      default:

        // @TODO: Add error handling here if desired.
        return FALSE;
    }
  }

  /**
   * Helper function that returns operations links based on AID and status.
   */
  protected function generate_operations_links($aid, $status) {
    if ($status == 0) {
      $approval_url = Url::fromRoute('apply_for_role.application_approve', array(
        'action' => 'approve',
        'aid' => intval($aid),
      ));
      $denial_url = Url::fromRoute('apply_for_role.application_deny', array(
        'action' => 'deny',
        'aid' => intval($aid),
      ));
      $approval_link = Link::fromTextAndUrl($this
        ->t('Approve Application'), $approval_url);
      $denial_link = Link::fromTextAndUrl($this
        ->t('Deny Application'), $denial_url);
      $approval_link_rendered = $approval_link
        ->toRenderable();
      $denial_link_rendered = $denial_link
        ->toRenderable();
      return render($approval_link_rendered) . ' | ' . render($denial_link_rendered);
    }
    else {
      return '';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
apply_for_role_application_admin_page::$applications_per_page private property
apply_for_role_application_admin_page::$application_manager private property
apply_for_role_application_admin_page::content public function Content, renders table of applications, paged.
apply_for_role_application_admin_page::display_status protected function Helper function to display a clean text representation of a status.
apply_for_role_application_admin_page::display_username protected function Display a presentable username.
apply_for_role_application_admin_page::generate_operations_links protected function Helper function that returns operations links based on AID and status.
apply_for_role_application_admin_page::__construct public function Constructor, loads application manager and amount of applications per page is set here.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.