You are here

class CalendarEntry in Content Planner 8

Class CalendarEntry.

@package Drupal\content_calendar\Component

Hierarchy

Expanded class hierarchy of CalendarEntry

File

modules/content_calendar/src/Component/CalendarEntry.php, line 15

Namespace

Drupal\content_calendar\Component
View source
class CalendarEntry extends BaseEntry {

  /**
   * @var int
   */
  protected $month;

  /**
   * @var int
   */
  protected $year;

  /**
   * @var \Drupal\content_calendar\Entity\ContentTypeConfig
   */
  protected $contentTypeConfig;

  /**
   * @var \stdClass
   */
  protected $node;

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * CalendarEntry constructor.
   *
   * @param int $month
   * @param int $year
   * @param \Drupal\content_calendar\Entity\ContentTypeConfig $content_type_config
   * @param \stdClass $node
   */
  public function __construct($month, $year, ContentTypeConfig $content_type_config, \stdClass $node) {
    $this->month = $month;
    $this->year = $year;
    $this->contentTypeConfig = $content_type_config;
    $this->node = $node;
    $this->config = \Drupal::config(SettingsForm::CONFIG_NAME);
  }

  /**
   * Get Node ID.
   *
   * @return mixed
   */
  public function getNodeID() {
    return $this->node->nid;
  }

  /**
   * Get the relevant date for the current node.
   *
   * When the Scheduler date is empty, then take the creation date.
   *
   * @return int
   */
  public function getRelevantDate() {
    if ($this->node->publish_on) {
      return $this->node->publish_on;
    }
    return $this->node->created;
  }

  /**
   * Format creation date as MySQL Date only.
   *
   * @return string
   */
  public function formatSchedulingDateAsMySQLDateOnly() {
    $datetime = DateTimeHelper::convertUnixTimestampToDatetime($this->node->created);
    return $datetime
      ->format(DateTimeHelper::FORMAT_MYSQL_DATE_ONLY);
  }

  /**
   * Build.
   *
   * @return array
   */
  public function build() {

    // Get User Picture.
    $user_picture = $this
      ->getUserPictureURL();
    if ($this->node->publish_on) {
      $this->node->scheduled = true;
    }
    else {
      $this->node->scheduled = false;
    }

    // Add time to node object.
    $this->node->publish_on_time = DateTimeHelper::convertUnixTimestampToDatetime($this->node->publish_on)
      ->format('H:i');
    $this->node->created_on_time = DateTimeHelper::convertUnixTimestampToDatetime($this->node->created)
      ->format('H:i');

    // Build options.
    $options = $this
      ->buildOptions();
    if (\Drupal::currentUser()
      ->hasPermission('manage content calendar')) {
      $this->node->editoptions = true;
    }
    if (\Drupal::currentUser()
      ->hasPermission('manage own content calendar')) {
      if ($this->node->uid == \Drupal::currentUser()
        ->id()) {
        $this->node->editoptions = true;
      }
    }
    $build = [
      '#theme' => 'content_calendar_entry',
      '#node' => $this->node,
      '#node_type_config' => $this->contentTypeConfig,
      '#month' => $this->month,
      '#year' => $this->year,
      '#user_picture' => $user_picture,
      '#options' => $options,
    ];
    return $build;
  }

  /**
   * Build options before rendering.
   *
   * @return array
   */
  protected function buildOptions() {
    $options = [];

    // Background color for unpublished content.
    $options['bg_color_unpublished_content'] = $this->config
      ->get('bg_color_unpublished_content') ? $this->config
      ->get('bg_color_unpublished_content') : SettingsForm::DEFAULT_BG_COLOR_UNPUBLISHED_CONTENT;
    return $options;
  }

  /**
   * Get the URL of the user picture.
   *
   * @return bool|string
   */
  protected function getUserPictureURL() {

    // If show user thumb is active.
    if ($this->config
      ->get('show_user_thumb')) {
      return $this
        ->getUserPictureFromCache($this->node->uid, 'content_calendar_user_thumb');
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseEntry::getUserPictureFromCache public function Drupal static method to retrieve the user picture url by user id.
CalendarEntry::$config protected property
CalendarEntry::$contentTypeConfig protected property
CalendarEntry::$month protected property
CalendarEntry::$node protected property
CalendarEntry::$year protected property
CalendarEntry::build public function Build.
CalendarEntry::buildOptions protected function Build options before rendering.
CalendarEntry::formatSchedulingDateAsMySQLDateOnly public function Format creation date as MySQL Date only.
CalendarEntry::getNodeID public function Get Node ID.
CalendarEntry::getRelevantDate public function Get the relevant date for the current node.
CalendarEntry::getUserPictureURL protected function Get the URL of the user picture.
CalendarEntry::__construct public function CalendarEntry constructor.