View source
<?php
namespace Drupal\easymeta;
class Meta {
protected $id;
protected $value;
protected $url;
protected $language;
public function __construct($language = NULL, $url = NULL) {
if ($language && $url) {
$metas = $this
->getMetasForUrlAndLanguage($url, $language);
if ($metas) {
$this->id = $metas->emid;
$this->value = $metas->metas;
$this->url = $url;
$this->language = $language;
}
}
}
public function getValue() {
return unserialize($this->value);
}
public function setValue($value) {
$this->value = serialize($value);
}
public function getUrl() {
return $this->url;
}
public function setUrl($url) {
$this->url = $url;
}
public function getLanguage() {
return $this->language;
}
public function setLanguage($language) {
$this->language = $language;
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function save() {
$array = [
"url" => $this
->getUrl(),
"language" => $this
->getLanguage(),
"metas" => serialize($this
->getValue()),
];
if ($this->id) {
$query = \Drupal::database()
->update('easymeta');
$query
->fields($array);
$query
->condition('emid', $this->id);
return $query
->execute();
}
else {
$query = \Drupal::database()
->insert('easymeta');
$query
->fields($array);
return $query
->execute();
}
}
public function getMetasForUrlAndLanguage($url, $language) {
$query = \Drupal::database()
->select('easymeta', 'em');
$query
->fields('em');
$query
->condition('url', $url);
$query
->condition('language', $language);
return $query
->execute()
->fetch();
}
}