You are here

class Groups in SimpleAds 8

Groups utility class.

Hierarchy

Expanded class hierarchy of Groups

7 files declare their use of Groups
All.php in src/Form/Groups/All.php
Create.php in src/Form/Groups/Create.php
Create.php in src/Form/Ads/Create.php
Delete.php in src/Form/Groups/Delete.php
Edit.php in src/Form/Groups/Edit.php

... See full list

2 string references to 'Groups'
simpleads.links.task.yml in ./simpleads.links.task.yml
simpleads.links.task.yml
simpleads.routing.yml in ./simpleads.routing.yml
simpleads.routing.yml

File

src/Groups.php, line 12

Namespace

Drupal\simpleads
View source
class Groups {
  use StringTranslationTrait;
  protected $id;
  protected $user;
  protected $name;
  protected $description;
  protected $options;
  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 setGroupName($name) {
    $this->name = $name;
    return $this;
  }
  public function getGroupName() {
    return $this->name;
  }
  public function setDescription($description) {
    $this->description = $description;
    return $this;
  }
  public function getDescription() {
    return $this->description;
  }
  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 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_groups', 's')
        ->fields('s')
        ->condition('s.id', $id)
        ->execute()
        ->fetchObject();
      $item = (new self())
        ->setId($id)
        ->setGroupName($record->name)
        ->setDescription($record->description)
        ->setOptions(!empty($record->options) ? json_decode($record->options, TRUE) : [])
        ->setCreatedAt($record->created_at)
        ->setChangedAt($record->changed_at);
      return $item;
    }
    return $this;
  }
  public function loadAll() {
    $items = [];
    $result = db_select('simpleads_groups', '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
        ->getGroupName();
    }
    return $options;
  }
  public function save() {
    $fields = [
      'uid' => $this->user
        ->id(),
      'name' => $this
        ->getGroupName(),
      'description' => $this
        ->getDescription(),
      'options' => $this
        ->getOptions(),
      'created_at' => $this
        ->getCreatedAt(),
      'changed_at' => $this
        ->getChangedAt(),
    ];
    if ($id = $this
      ->getId()) {
      $query = db_update('simpleads_groups')
        ->fields($fields)
        ->condition('id', $id);
      drupal_set_message($this
        ->t('Group successfully updated.'));
    }
    else {
      $query = db_insert('simpleads_groups')
        ->fields($fields);
      drupal_set_message($this
        ->t('New Group successfully created.'));
    }
    $query
      ->execute();
  }
  public function delete() {
    if ($id = $this
      ->getId()) {
      $group = $this
        ->load();
      db_delete('simpleads_groups')
        ->condition('id', $id)
        ->execute();
      drupal_set_message($this
        ->t('Group <em>@name</em> successfully delete.', [
        '@name' => $group
          ->getGroupName(),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Groups::$changed_at protected property
Groups::$created_at protected property
Groups::$description protected property
Groups::$id protected property
Groups::$name protected property
Groups::$options protected property
Groups::$user protected property
Groups::delete public function
Groups::getChangedAt public function
Groups::getCreatedAt public function
Groups::getDescription public function
Groups::getGroupName public function
Groups::getId public function
Groups::getOptions public function
Groups::load public function
Groups::loadAll public function
Groups::loadAsOptions public function
Groups::save public function
Groups::setChangedAt public function
Groups::setCreatedAt public function
Groups::setDescription public function
Groups::setGroupName public function
Groups::setId public function
Groups::setOptions public function
Groups::__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.