You are here

class Ads in SimpleAds 8

Ads utility class.

Hierarchy

Expanded class hierarchy of Ads

6 files declare their use of Ads
All.php in src/Form/Ads/All.php
Create.php in src/Form/Ads/Create.php
Delete.php in src/Form/Ads/Delete.php
Edit.php in src/Form/Ads/Edit.php
Image.php in src/Plugin/SimpleAds/Type/Image.php

... See full list

File

src/Ads.php, line 10

Namespace

Drupal\simpleads
View source
class Ads extends UtilityBase {
  use StringTranslationTrait;
  protected $manager_name = 'plugin.manager.simpleads.types';
  protected $id;
  protected $user;
  protected $name;
  protected $description;
  protected $type;
  protected $group;
  protected $campaign;
  protected $options;
  protected $status;
  protected $created_at;
  protected $changed_at;
  public function __construct() {
    $this->user = \Drupal::currentUser();
  }
  public function setId($id) {
    $this->id = $id;
    return $this;
  }
  public function getId() {
    return $this->id;
  }
  public function setAdName($name) {
    $this->name = $name;
    return $this;
  }
  public function getAdName() {
    return $this->name;
  }
  public function setDescription($description) {
    $this->description = $description;
    return $this;
  }
  public function getDescription() {
    return $this->description;
  }
  public function setType($type) {
    $this->type = $type;
    return $this;
  }
  public function getType() {
    return $this->type;
  }
  public function setGroup(Groups $group) {
    $this->group = $group;
    return $this;
  }
  public function getGroup() {
    return $this->group;
  }
  public function setCampaign(Campaigns $campaign) {
    $this->campaign = $campaign;
    return $this;
  }
  public function getCampaign() {
    return $this->campaign;
  }
  public function setOptions(array $options = []) {
    $this->options = json_encode($options);
    return $this;
  }
  public function getOptions($decode = FALSE) {
    return $decode ? json_decode($this->options, TRUE) : $this->options;
  }
  public function setStatus($status = 1) {
    $this->status = $status;
    return $this;
  }
  public function getStatus() {
    return $this->status;
  }
  public function setCreatedAt($created_at) {
    $this->created_at = $created_at;
    return $this;
  }
  public function getCreatedAt() {
    return !empty($this->created_at) ? $this->created_at : time();
  }
  public function setChangedAt($changed_at) {
    $this->changed_at = $changed_at;
    return $this;
  }
  public function getChangedAt() {
    return !empty($this->changed_at) ? $this->changed_at : time();
  }
  public function load() {
    if ($id = $this
      ->getId()) {
      $record = db_select('simpleads', 's')
        ->fields('s')
        ->condition('s.id', $id)
        ->execute()
        ->fetchObject();
      $item = (new self())
        ->setId($id)
        ->setAdName($record->name)
        ->setDescription($record->description)
        ->setType($record->type)
        ->setGroup((new Groups())
        ->setId($record->group_id)
        ->load())
        ->setCampaign((new Campaigns())
        ->setId($record->campaign_id)
        ->load())
        ->setOptions(!empty($record->options) ? json_decode($record->options, TRUE) : [])
        ->setStatus($record->status)
        ->setCreatedAt($record->created_at)
        ->setChangedAt($record->changed_at);
      return $item;
    }
    return $this;
  }
  public function loadAll() {
    $items = [];
    $result = db_select('simpleads', 's')
      ->fields('s')
      ->orderBy('s.changed_at', 'DESC')
      ->execute();
    foreach ($result as $row) {
      $items[] = $this
        ->setId($row->id)
        ->load();
    }
    return $items;
  }
  public function loadAsOptions() {
    $options = [];
    $options[''] = $this
      ->t('- None -');
    foreach ($this
      ->loadAll() as $item) {
      $options[$item
        ->getId()] = $item
        ->getAdName();
    }
    return $options;
  }
  public function save() {
    $fields = [
      'uid' => $this->user
        ->id(),
      'name' => $this
        ->getAdName(),
      'description' => $this
        ->getDescription(),
      'type' => $this
        ->getType(),
      'group_id' => !empty($this->group
        ->getId()) ? $this->group
        ->getId() : NULL,
      'campaign_id' => !empty($this->campaign
        ->getId()) ? $this->campaign
        ->getId() : NULL,
      'options' => $this
        ->getOptions(),
      'status' => $this
        ->getStatus(),
      'created_at' => $this
        ->getCreatedAt(),
      'changed_at' => $this
        ->getChangedAt(),
    ];
    if ($id = $this
      ->getId()) {
      $query = db_update('simpleads')
        ->fields($fields)
        ->condition('id', $id);
      drupal_set_message($this
        ->t('Advertisement successfully updated.'));
    }
    else {
      $query = db_insert('simpleads')
        ->fields($fields);
      drupal_set_message($this
        ->t('New advertisement successfully created.'));
    }
    $query
      ->execute();
  }
  public function delete() {
    if ($id = $this
      ->getId()) {
      $group = $this
        ->load();
      db_delete('simpleads')
        ->condition('id', $id)
        ->execute();
      drupal_set_message($this
        ->t('Advertisement <em>@name</em> successfully delete.', [
        '@name' => $group
          ->getAdName(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Ads::$campaign protected property
Ads::$changed_at protected property
Ads::$created_at protected property
Ads::$description protected property
Ads::$group protected property
Ads::$id protected property
Ads::$manager_name protected property Overrides UtilityBase::$manager_name
Ads::$name protected property
Ads::$options protected property
Ads::$status protected property
Ads::$type protected property
Ads::$user protected property
Ads::delete public function
Ads::getAdName public function
Ads::getCampaign public function
Ads::getChangedAt public function
Ads::getCreatedAt public function
Ads::getDescription public function
Ads::getGroup public function
Ads::getId public function
Ads::getOptions public function
Ads::getStatus public function
Ads::getType public function
Ads::load public function
Ads::loadAll public function
Ads::loadAsOptions public function
Ads::save public function
Ads::setAdName public function
Ads::setCampaign public function
Ads::setChangedAt public function
Ads::setCreatedAt public function
Ads::setDescription public function
Ads::setGroup public function
Ads::setId public function
Ads::setOptions public function
Ads::setStatus public function
Ads::setType public function
Ads::__construct public function
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.
UtilityBase::getBuildForm public function
UtilityBase::getName public function Get ad type name by ID.
UtilityBase::getStatuses public function
UtilityBase::getStatusName public function
UtilityBase::getSubmitForm public function
UtilityBase::getTypes public function Get Simpleads types.