You are here

class UCWishlistController in UC Wish List 8

Hierarchy

Expanded class hierarchy of UCWishlistController

File

src/Controller/UCWishlistController.php, line 15

Namespace

Drupal\uc_wishlist\Controller
View source
class UCWishlistController extends ControllerBase {

  /**
   * @var \Drupal\uc_wishlist\Database\UcWishlistManager
   */
  protected $wishlistManager;

  /**
   * Constructs a UCWishlistController object.
   *
   * @param \Drupal\uc_wishlist\Database\UcWishlistManager $wishlist_manager
   */
  public function __construct(UcWishlistManager $wishlist_manager) {
    $this->wishlistManager = $wishlist_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('uc_wishlist.manager'));
  }

  /**
   * This function returns a list of user wish lists by displaying
   * the user, title and expiration status.
   *
   * @return array
   *   a paged list of wish lists.
   */
  public function AdminWishlist() {
    $rows = [];
    $header = [
      [
        'data' => t('User'),
        'field' => 'u.name',
        'sort' => 'asc',
      ],
      [
        'data' => t('Title'),
        'field' => 'w.title',
      ],
      [
        'data' => t('Expiration date'),
        'field' => 'w.expiration',
      ],
      [
        'data' => t('Status'),
      ],
    ];

    // Database returns a paged list of wish lists.
    $result = $this->db
      ->getAllWishlist();
    foreach ($result as $wishlist) {
      $deleteUrl = Url::fromRoute('uc_wishlist.admin_delete', [
        'uc_wishlist' => $wishlist->wid,
      ]);
      $link_options = [
        'attributes' => [],
      ];
      $deleteUrl
        ->setOptions($link_options);

      // Wishlist expiration status.
      $expired = '';
      if ($wishlist->expiration < REQUEST_TIME) {
        $expired = t('Expired');
      }
      elseif ($wishlist->expiration > 0) {
        $expired = t('Active');
      }
      $deleteUrl = Link::fromTextAndUrl($expired . ' | Delete', $deleteUrl)
        ->toString();
      $account = User::load($wishlist->uid);
      $name = $account
        ->getAccountName();
      $rows[] = [
        $name ? Link::fromTextAndUrl($name, Url::fromRoute('entity.user.canonical', [
          'user' => $wishlist->uid,
        ]))
          ->toString() : t('Anonymous'),
        Link::fromTextAndUrl($wishlist->title, Url::fromRoute('uc_wishlist.wishlist_view', [
          'wid' => $wishlist->wid,
        ]))
          ->toString(),
        \Drupal::service('date.formatter')
          ->format($wishlist->expiration),
        $deleteUrl,
      ];
    }
    if (empty($rows)) {
      $rows[] = [
        [
          'data' => t('No wish lists found'),
          'colspan' => 4,
        ],
      ];
    }
    return [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
  }

  /**
   *
   */
  public function viewWishlist($wid = NULL) {
    $render = [];
    $output = '';
    $title = '';
    $wishlist = NULL;
    $rendered_wishlistview_form = '';
    $own = FALSE;
    if (!$own && $wid == uc_wishlist_get_wid()) {
      return $this
        ->redirect('uc_wishlist.wishlist');
    }

    // Load the wish list.
    $wishlist = uc_wishlist_load($wid);

    // Handle a non-existent wish list.
    if (!$wishlist) {

      // Otherwise send them to the search form.
      drupal_set_message($this
        ->t('The wish list you requested could not be found. Perhaps you can try looking for it through the wish list search form below.'));
      return $this
        ->redirect('uc_wishlist.wishlist.search');
    }

    // Display only if the users wishlist is not set to private.
    if (!$wishlist->private) {

      // Set the title to the wishlist title.
      $title = $wishlist->title;

      // Adding expiration info to display.
      if ($wishlist->expiration < REQUEST_TIME) {
        $output .= '<p>' . $this
          ->t('This wish list may no longer be valid. It was for an event on @date.', [
          '@date' => \Drupal::service('date.formatter')
            ->format($wishlist->expiration),
        ]) . '</p>';
      }
      elseif ($wishlist->expiration > 0) {
        $output .= '<p>' . $this
          ->t('This wish list is valid until @date.', [
          '@date' => \Drupal::service('date.formatter')
            ->format($wishlist->expiration),
        ]) . '</p>';
      }
      $items = uc_wishlist_get_contents($wid);
      if (empty($items)) {

        // @TODO: add the users name to the output string
        $output = '<p>There are no products in this wish list.</p>';

        // Return $render;.
      }
      else {
        $form = \Drupal::formBuilder()
          ->getForm('Drupal\\uc_wishlist\\Form\\WishlistViewForm', $items, $wid, FALSE);
        $rendered_wishlistview_form = \Drupal::service('renderer')
          ->render($form);
      }
    }
    else {
      drupal_set_message($this
        ->t('This users wish list is set to private. You may search for another user\'s wish list below.'));
      return $this
        ->redirect('uc_wishlist.wishlist.search');
    }
    $render['#theme'] = 'uc_wishlist_view_wishlist';
    $render['#type'] = 'theme';
    $render['#title'] = $title;
    $render['#message'] = $output;
    $render['#form'] = $rendered_wishlistview_form;
    $render['#wishlist'] = $wishlist;
    return $render;
  }

  /**
   *
   */
  public function myWishlist() {

    // Setup our render array for all our page variables and configurations.
    $render = [];

    // Get the wishlist id based off of the current users id.
    $wid = uc_wishlist_get_wid();

    // Attempt to load the wish list.
    $wishlist = uc_wishlist_load($wid);
    $output = '';
    if (!$wishlist) {

      // Display a message letting them know their list is empty.
      $title = 'Wish list';
      drupal_set_message($this
        ->t("You have not added any products to your wish list. You can add any product from the store to your wish list by clicking the 'Add to wish list' button on the product's page."));
      $render['#markup'] = 'There are no products on this wish list.';
      $render['#title'] = $title;
      return $render;
    }
    $title = 'My Wish List';

    // Adding expiration info to display.
    if ($wishlist->expiration < REQUEST_TIME) {
      $output .= '<p>' . $this
        ->t('This wish list may no longer be valid. It was for an event on @date.', [
        '@date' => \Drupal::service('date.formatter')
          ->format($wishlist->expiration),
      ]) . '</p>';
    }
    elseif ($wishlist->expiration > 0) {
      $output .= '<p>' . $this
        ->t('This wish list is valid until @date.', [
        '@date' => \Drupal::service('date.formatter')
          ->format($wishlist->expiration),
      ]) . '</p>';
    }
    $items = uc_wishlist_get_contents($wid);
    if (empty($items)) {
      $render['#markup'] = '<p>There are no products in your wish list.</p>';
      return $render;
    }
    $form = \Drupal::formBuilder()
      ->getForm('Drupal\\uc_wishlist\\Form\\WishlistViewForm', $items, $wid, TRUE);
    $rendered_wishlistview_form = \Drupal::service('renderer')
      ->render($form);
    $render['#theme'] = 'uc_wishlist_view_wishlist';
    $render['#type'] = 'theme';
    $render['#title'] = $title;
    $render['#message'] = $output;
    $render['#form'] = $rendered_wishlistview_form;
    $render['#wishlist'] = $wishlist;
    return $render;
  }

  /**
   *
   */
  public function userWishlist($user) {
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
UCWishlistController::$wishlistManager protected property
UCWishlistController::AdminWishlist public function This function returns a list of user wish lists by displaying the user, title and expiration status.
UCWishlistController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
UCWishlistController::myWishlist public function
UCWishlistController::userWishlist public function
UCWishlistController::viewWishlist public function
UCWishlistController::__construct public function Constructs a UCWishlistController object.
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.