You are here

class AbjsDefaultController in A/B Test JS 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/AbjsDefaultController.php \Drupal\abjs\Controller\AbjsDefaultController

Default controller for the abjs module.

Hierarchy

Expanded class hierarchy of AbjsDefaultController

File

src/Controller/AbjsDefaultController.php, line 15

Namespace

Drupal\abjs\Controller
View source
class AbjsDefaultController extends ControllerBase {

  /**
   * Turns a render array into a HTML string.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $userStorage;

  /**
   * Provides a service to handle various date related functionality.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Turns a render array into a HTML string.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The database connection.
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new FeedTypeForm object.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The services of date.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The render object.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, Connection $database) {
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
    $this->database = $database;
    $this->userStorage = $this
      ->entityTypeManager()
      ->getStorage('user');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('date.formatter'), $container
      ->get('renderer'), $container
      ->get('database'));
  }

  /**
   * Lists all tests in a default table theme.
   *
   * Sorted by active tests first, then modified most recently, then
   * created most recently. For each test, link to test, and list active
   * status, conditions applied (with links), experiences with fractions
   * assigned (with links), and created and edited info.
   */
  public function abjsTestAdmin() {

    //$db = Database::getConnection();
    $date_Formatter = $this->dateFormatter;
    $output = [];
    $header = [
      $this
        ->t('ID'),
      $this
        ->t('Name'),
      $this
        ->t('Status'),
      $this
        ->t('Conditions'),
      $this
        ->t('Experiences'),
      $this
        ->t('Created'),
      $this
        ->t('Created By'),
      $this
        ->t('Changed'),
      $this
        ->t('Changed By'),
    ];
    $rows = [];
    $active_array = [
      $this
        ->t('Inactive'),
      $this
        ->t('Active'),
    ];
    $tests = $this->database
      ->query("SELECT * FROM {abjs_test} ORDER BY active DESC, changed DESC, created DESC")
      ->fetchAll();
    foreach ($tests as $test) {
      $test_link = [
        '#title' => $test->name,
        '#type' => 'link',
        '#url' => Url::fromRoute('abjs.test_edit_form', [
          'tid' => $test->tid,
        ]),
      ];
      $condition_list = [];
      $condition_output = '';
      $conditions = $this->database
        ->query("SELECT tc.cid, c.name FROM {abjs_test_condition} AS tc INNER JOIN {abjs_condition} AS c ON tc.cid = c.cid WHERE tc.tid = :tid", [
        ':tid' => $test->tid,
      ])
        ->fetchAll();
      if (!empty($conditions)) {
        foreach ($conditions as $condition) {
          $condition_link = [
            '#title' => $condition->name . ' (c_' . $condition->cid . ')',
            '#type' => 'link',
            '#url' => Url::fromRoute('abjs.condition_edit_form', [
              'cid' => $condition->cid,
            ]),
          ];
          $condition_list[] = $this->renderer
            ->render($condition_link);
        }
        $condition_output = [
          '#theme' => 'item_list',
          '#items' => $condition_list,
        ];
      }
      $experience_list = [];
      $experience_output = '';
      $experiences = $this->database
        ->query("SELECT te.eid, te.fraction, e.name FROM {abjs_test_experience} AS te INNER JOIN {abjs_experience} AS e ON te.eid = e.eid WHERE te.tid = :tid", [
        ':tid' => $test->tid,
      ])
        ->fetchAll();
      if (!empty($experiences)) {
        foreach ($experiences as $experience) {
          $experience_link = [
            '#title' => '[' . $experience->fraction . '] ' . $experience->name . ' (e_' . $experience->eid . ')',
            '#type' => 'link',
            '#url' => Url::fromRoute('abjs.experience_edit_form', [
              'eid' => $experience->eid,
            ]),
          ];
          $experience_list[] = $this->renderer
            ->render($experience_link);
        }
        $experience_output = [
          '#theme' => 'item_list',
          '#items' => $experience_list,
        ];
      }
      $user_created = $this->userStorage
        ->load($test->created_by);
      $user_changed = $this->userStorage
        ->load($test->changed_by);
      $rows[] = [
        't_' . $test->tid,
        $this->renderer
          ->render($test_link),
        $active_array[$test->active],
        $this->renderer
          ->render($condition_output),
        $this->renderer
          ->render($experience_output),
        $date_Formatter
          ->format($test->created),
        $user_created
          ->toLink(),
        $date_Formatter
          ->format($test->changed),
        $user_changed
          ->toLink(),
      ];
    }
    $output['add'] = [
      '#title' => $this
        ->t('Add new test'),
      '#type' => 'link',
      '#url' => Url::fromRoute('abjs.test_add_form'),
      '#attributes' => [
        'class' => 'button button-action button--primary button--small',
      ],
      '#suffix' => '<br><br>',
    ];
    $output['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
    return $output;
  }

  /**
   * Lists all conditions in default table, sorted by modified date.
   *
   * For each condition, link to edit form, and list created and edited info.
   */
  public function abjsConditionAdmin() {

    //$db = Database::getConnection();
    $date_Formatter = $this->dateFormatter;
    $output = [];
    $header = [
      $this
        ->t('ID'),
      $this
        ->t('Name'),
      $this
        ->t('Created'),
      $this
        ->t('Created By'),
      $this
        ->t('Changed'),
      $this
        ->t('Changed By'),
    ];
    $rows = [];
    $conditions = $this->database
      ->query("SELECT * FROM {abjs_condition} ORDER BY changed DESC, created DESC")
      ->fetchAll();
    foreach ($conditions as $condition) {
      $condition_link = [
        '#title' => $condition->name,
        '#type' => 'link',
        '#url' => Url::fromRoute('abjs.condition_edit_form', [
          'cid' => $condition->cid,
        ]),
      ];
      $user_created = $this->userStorage
        ->load($condition->created_by);
      $user_changed = $this->userStorage
        ->load($condition->changed_by);
      $rows[] = [
        'c_' . $condition->cid,
        $this->renderer
          ->render($condition_link),
        $date_Formatter
          ->format($condition->created),
        $user_created
          ->toLink(),
        $date_Formatter
          ->format($condition->changed),
        $user_changed
          ->toLink(),
      ];
    }
    $output['add'] = [
      '#title' => $this
        ->t('Add new condition'),
      '#type' => 'link',
      '#url' => Url::fromRoute('abjs.condition_add_form'),
      '#attributes' => [
        'class' => 'button button-action button--primary button--small',
      ],
      '#suffix' => '<br><br>',
    ];
    $output['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
    return $output;
  }

  /**
   * Lists all experiences in default table, sorted by modified date.
   *
   * For each experience, link to edit form, and list created and edited info.
   */
  public function abjsExperienceAdmin() {

    //$db = Database::getConnection();
    $date_Formatter = $this->dateFormatter;
    $output = [];
    $header = [
      $this
        ->t('ID'),
      $this
        ->t('Name'),
      $this
        ->t('Created'),
      $this
        ->t('Created By'),
      $this
        ->t('Changed'),
      $this
        ->t('Changed By'),
    ];
    $rows = [];
    $experiences = $this->database
      ->query("SELECT * FROM {abjs_experience} ORDER BY changed DESC, created DESC")
      ->fetchAll();
    foreach ($experiences as $experience) {
      $experience_link = [
        '#title' => $experience->name,
        '#type' => 'link',
        '#url' => Url::fromRoute('abjs.experience_edit_form', [
          'eid' => $experience->eid,
        ]),
      ];
      $user_created = $this->userStorage
        ->load($experience->created_by);
      $user_changed = $this->userStorage
        ->load($experience->changed_by);
      $rows[] = [
        'e_' . $experience->eid,
        $this->renderer
          ->render($experience_link),
        $date_Formatter
          ->format($experience->created),
        $user_created
          ->toLink(),
        $date_Formatter
          ->format($experience->changed),
        $user_changed
          ->toLink(),
      ];
    }
    $output['add'] = [
      '#title' => $this
        ->t('Add new experience'),
      '#type' => 'link',
      '#url' => Url::fromRoute('abjs.experience_add_form'),
      '#attributes' => [
        'class' => 'button button-action button--primary button--small',
      ],
      '#suffix' => '<br><br>',
    ];
    $output['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbjsDefaultController::$database protected property The database connection.
AbjsDefaultController::$dateFormatter protected property Provides a service to handle various date related functionality.
AbjsDefaultController::$renderer protected property Turns a render array into a HTML string.
AbjsDefaultController::$userStorage protected property Turns a render array into a HTML string.
AbjsDefaultController::abjsConditionAdmin public function Lists all conditions in default table, sorted by modified date.
AbjsDefaultController::abjsExperienceAdmin public function Lists all experiences in default table, sorted by modified date.
AbjsDefaultController::abjsTestAdmin public function Lists all tests in a default table theme.
AbjsDefaultController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
AbjsDefaultController::__construct public function Constructs a new FeedTypeForm object.
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::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.