Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/customeredit / src / CustomerText.php
Size: Mime:
<?php

namespace Drupal\customeredit;

use Drupal\Core\Url;

class CustomerText {
  protected $machine_name;
  protected $langcode;
  protected $type;
  protected $title;
  protected $description;
  protected $category;
  protected $subcategory;
  /** @var $translatable boolean */
  protected $translatable;
  /** @var $clear_cache boolean */
  protected $clear_cache;
  protected $text;
  protected $values = array();
  protected $token_types = array();
  protected $location = array('url' => '', 'selector' => '');

  /** @var $storage CustomerTextsStorage */
  protected $storage;
  
  public function __construct($machine_name, $langcode = '') {
    if (empty($langcode)) {
      //$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
      $langcode = 'en';
    }
    $this->langcode = $langcode;
    $this->machine_name = $machine_name;
    $this->storage = \Drupal::service('customeredit.texts.storage');
    $this->load();
  }

  public function load() {
    $text = $this->storage->getFullCustomerText($this->machine_name, $this->langcode);
    $this->text = $text['text'];
    $this->type = $text['type'];
    $this->title = $text['title'];
    $this->description = $text['description'];
    $this->category = $text['category'];
    $this->subcategory = $text['subcategory'];
    $this->translatable = $text['translatable'];
    $this->location['url'] = $text['location_url'];
    $this->location['selector'] = $text['location_selector'];
    $this->token_types = explode(',', $text['token_types']);
    $this->clear_cache = $text['clear_cache'];
    if (in_array($this->type,['button', 'link'])) {
      $this->values = json_decode($this->text, TRUE);
    }
  }

  public function save() {
    $this->storage->setText($this->machine_name, $this->text);
  }

  public function toArray() {
    return [
      'machine_name' => $this->machine_name,
      'langcode' => $this->langcode,
      'text' => $this->text,
      'values' => $this->values,
      'type' => $this->type,
      'title' => $this->title,
      'description' => $this->description,
      'category' => $this->category,
      'subcategory' => $this->subcategory,
      'translatable' => $this->translatable,
      'location_url' => $this->location['url'],
      'location_selector' => $this->location['selector'],
      'token_types' => $this->token_types,
      'clear_cache' => $this->clear_cache,
    ];
  }

  /**
   * @return \Drupal\Core\Url|bool
   */
  protected function toUrl() {
    return customeredit_textToUrl($this->text);
  }

  /**
   * @return mixed
   */
  public function getText() {
    return $this->text;
  }

  /**
   * @param mixed $text
   */
  public function setText($text) {
    $this->text = $text;
  }

  /**
   * @param string $key
   * @return string
   */
  public function getValue($key) {
    return isset($this->values[$key]) ? $this->values[$key] : '';
  }

  /**
   * @param string $key
   * @param string $value
   */
  public function setValue($key, $value) {
    $this->values[$key] = $value;
    $this->text = json_encode($this->values);
  }

  /**
   * @return boolean
   */
  public function isTranslatable() {
    return $this->translatable;
  }

  /**
   * @return boolean
   */
  public function mustClearCache() {
    return $this->clear_cache;
  }

  /**
   * @return string
   */
  public function getCategory() {
    return $this->category;
  }

  /**
   * @return string
   */
  public function getSubcategory() {
    return $this->subcategory;
  }

  /**
   * @return mixed
   */
  public function getDescription() {
    return $this->description;
  }

  /**
   * @return mixed
   */
  public function getTitle() {
    return $this->title;
  }

  /**
   * @return mixed
   */
  public function getType() {
    return $this->type;
  }

  /**
   * @return mixed
   */
  public function getMachineName() {
    return $this->machine_name;
  }

  /**
   * @return array
   */
  public function getTokenTypes() {
    return $this->token_types;
  }

  /**
   * @param array $token_types
   */
  public function setTokenTypes($token_types) {
    $this->token_types = $token_types;
  }

  /**
   * @return string
   */
  public function getLanguage() {
    return $this->langcode;
  }

  /**
   * @param string $langcode
   */
  public function setLanguage($langcode) {
    $this->langcode = $langcode;
    $this->load();
  }

  /**
   * @param string $url
   * @param string $selector
   */
  public function setLocation($url, $selector) {
    $this->location['url'] = $url;
    $this->location['selector'] = $selector;
  }

  /**
   * @return array
   */
  public function getLocation() {
    return $this->location;
  }
}