2022-03-08 15:55:41 +01:00
|
|
|
<?php namespace ProcessWire;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serves as a multi-language value placeholder for field values that contain a value in more than one language.
|
|
|
|
*
|
2023-03-10 19:41:40 +01:00
|
|
|
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
|
2022-03-08 15:55:41 +01:00
|
|
|
* https://processwire.com
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LanguagesPageFieldValue extends Wire implements LanguagesValueInterface, \IteratorAggregate {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit default language value when blank
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const langBlankInheritDefault = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't inherit any value when blank
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const langBlankInheritNone = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Values per language indexed by language ID
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached ID of default language page
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected $defaultLanguagePageID = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LanguageSupport|null
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected $languageSupport = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to Field that this value is for
|
2022-11-05 18:32:48 +01:00
|
|
|
*
|
|
|
|
* @var Field|null
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
2022-11-05 18:32:48 +01:00
|
|
|
protected $field = null;
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to Page that this value is for
|
|
|
|
*
|
2022-11-05 18:32:48 +01:00
|
|
|
* @var Page|null
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
2022-11-05 18:32:48 +01:00
|
|
|
protected $page = null;
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the multi language value
|
|
|
|
*
|
2022-11-05 18:32:48 +01:00
|
|
|
* @param Page|null $page
|
|
|
|
* @param Field|null $field
|
2022-03-08 15:55:41 +01:00
|
|
|
* @param array|string $values
|
|
|
|
*
|
|
|
|
*/
|
2022-11-05 18:32:48 +01:00
|
|
|
public function __construct($page = null, $field = null, $values = null) { // #98
|
2023-03-10 19:41:40 +01:00
|
|
|
parent::__construct();
|
2022-03-08 15:55:41 +01:00
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
if($page) $this->setPage($page);
|
|
|
|
if($field) $this->setField($field);
|
2022-03-08 15:55:41 +01:00
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
if($page) {
|
|
|
|
$page->wire($this);
|
|
|
|
} else if($field) {
|
|
|
|
$field->wire($this);
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:55:41 +01:00
|
|
|
if(!is_array($values)) {
|
|
|
|
$values = array('data' => $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->importArray($values);
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Wired to API
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function wired() {
|
|
|
|
parent::wired();
|
|
|
|
$this->languageSupport();
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:55:41 +01:00
|
|
|
/**
|
|
|
|
* Import array of language values
|
|
|
|
*
|
|
|
|
* Indexes may be:
|
|
|
|
* - “data123” where 123 is language ID or “data” for default language
|
|
|
|
* - "en” language name (may be any language name)
|
|
|
|
* - “123” language ID
|
|
|
|
*
|
|
|
|
* One index style must be used, you may not combine multiple.
|
|
|
|
*
|
|
|
|
* #pw-internal
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function importArray(array $values) {
|
|
|
|
|
|
|
|
reset($values);
|
|
|
|
$testKey = key($values);
|
|
|
|
|
|
|
|
if($testKey === null) return;
|
|
|
|
|
|
|
|
if(strpos($testKey, 'data') !== 0) {
|
|
|
|
// array does not use "data123" indexes, so work with language ID or language name indexes
|
|
|
|
// and convert to "data123" indexes
|
|
|
|
$_values = array();
|
|
|
|
foreach($values as $key => $value) {
|
|
|
|
if(ctype_digit("$key")) $key = (int) $key;
|
2022-11-05 18:32:48 +01:00
|
|
|
$language = $this->wire()->languages->get($key);
|
2022-03-08 15:55:41 +01:00
|
|
|
if($language && $language->id) {
|
|
|
|
$dataKey = $language->isDefault() ? "data" : "data$language->id";
|
|
|
|
$_values[$dataKey] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($_values)) $values = $_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(array_key_exists('data', $values)) {
|
|
|
|
if(is_null($values['data'])) $values['data'] = '';
|
2022-11-05 18:32:48 +01:00
|
|
|
$this->data[$this->defaultLanguagePageID()] = $values['data'];
|
2022-03-08 15:55:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
$languageSupport = $this->languageSupport();
|
|
|
|
if($languageSupport) {
|
|
|
|
foreach($languageSupport->otherLanguagePageIDs as $id) {
|
|
|
|
$key = 'data' . $id;
|
|
|
|
$value = empty($values[$key]) ? '' : $values[$key];
|
|
|
|
$this->data[$id] = $value;
|
|
|
|
}
|
2022-03-08 15:55:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value for a given language
|
|
|
|
*
|
|
|
|
* @param int|Language|string $languageID Language object, id, or name
|
|
|
|
* @param mixed $value
|
|
|
|
* @return $this
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setLanguageValue($languageID, $value) {
|
2023-03-10 19:41:40 +01:00
|
|
|
if($languageID instanceof Language) $languageID = $languageID->id;
|
|
|
|
if(is_string($languageID) && !ctype_digit("$languageID")) {
|
|
|
|
$languageID = $this->wire()->languages->get($languageID)->id;
|
|
|
|
}
|
2022-03-08 15:55:41 +01:00
|
|
|
$existingValue = isset($this->data[$languageID]) ? $this->data[$languageID] : '';
|
|
|
|
if($value instanceof LanguagesPageFieldValue) {
|
|
|
|
// to avoid potential recursion
|
|
|
|
$value = $value->getLanguageValue($languageID);
|
|
|
|
}
|
|
|
|
if($value !== $existingValue) {
|
|
|
|
$this->trackChange('data', $existingValue, $value);
|
|
|
|
$this->trackChange('data' . $languageID, $existingValue, $value);
|
|
|
|
}
|
|
|
|
$this->data[(int)$languageID] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab language values from Inputfield and populate to this object
|
|
|
|
*
|
|
|
|
* @param Inputfield $inputfield
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setFromInputfield(Inputfield $inputfield) {
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
foreach($this->wire()->languages as $language) {
|
2023-03-10 19:41:40 +01:00
|
|
|
/** @var Language $language */
|
|
|
|
if($language->isDefault()) {
|
2022-03-08 15:55:41 +01:00
|
|
|
$key = 'value';
|
|
|
|
} else {
|
|
|
|
$key = 'value' . $language->id;
|
|
|
|
}
|
|
|
|
$this->setLanguageValue($language->id, $inputfield->$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate language values from this object to given Inputfield
|
|
|
|
*
|
|
|
|
* @param Inputfield $inputfield
|
|
|
|
* @since 3.0.170
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setToInputfield(Inputfield $inputfield) {
|
|
|
|
foreach($this->wire()->languages as $language) {
|
2023-03-10 19:41:40 +01:00
|
|
|
/** @var Language $language */
|
|
|
|
$key = $language->isDefault() ? "value" : "value$language->id";
|
2022-03-08 15:55:41 +01:00
|
|
|
$inputfield->set($key, $this->getLanguageValue($language->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a language, returns the value in that language
|
|
|
|
*
|
|
|
|
* @param Language|int|string Language object, id, or name
|
2023-03-10 19:41:40 +01:00
|
|
|
* @return string|mixed
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getLanguageValue($languageID) {
|
2023-03-10 19:41:40 +01:00
|
|
|
if($languageID instanceof Language) $languageID = $languageID->id;
|
2022-11-05 18:32:48 +01:00
|
|
|
if(is_string($languageID) && !ctype_digit("$languageID")) $languageID = $this->wire()->languages->get($languageID)->id;
|
2022-03-08 15:55:41 +01:00
|
|
|
$languageID = (int) $languageID;
|
|
|
|
return isset($this->data[$languageID]) ? $this->data[$languageID] : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value in the default language
|
2022-11-05 18:32:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getDefaultValue() {
|
2022-11-05 18:32:48 +01:00
|
|
|
$id = $this->defaultLanguagePageID();
|
|
|
|
return isset($this->data[$id]) ? $this->data[$id] : '';
|
2022-03-08 15:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get non-empty value in this order: current lang, default lang, other lang, failValue
|
|
|
|
*
|
|
|
|
* @param string $failValue Value to use if we cannot find a non-empty value
|
|
|
|
* @return string
|
|
|
|
* @since 3.0.147
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getNonEmptyValue($failValue = '') {
|
|
|
|
|
|
|
|
$value = (string) $this;
|
|
|
|
if(strlen($value)) return $value;
|
|
|
|
|
|
|
|
$value = (string) $this->getDefaultValue();
|
|
|
|
if(strlen($value)) return $value;
|
|
|
|
|
2023-03-10 19:41:40 +01:00
|
|
|
foreach($this->wire()->languages as $language) {
|
2022-03-08 15:55:41 +01:00
|
|
|
$value = $this->getLanguageValue($language->id);
|
|
|
|
if(strlen($value)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strlen($value)) $value = $failValue;
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The string value is the value in the current user's language
|
2022-11-05 18:32:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
2022-11-05 18:32:48 +01:00
|
|
|
if($this->wire()->hooks->isHooked('LanguagesPageFieldValue::getStringValue()')) {
|
2022-03-08 15:55:41 +01:00
|
|
|
return $this->__call('getStringValue', array());
|
|
|
|
} else {
|
|
|
|
return $this->___getStringValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Get string value (for hooks)
|
|
|
|
*
|
|
|
|
* #pw-hooker
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
protected function ___getStringValue() {
|
|
|
|
|
|
|
|
$template = $this->page->template;
|
|
|
|
$language = $this->wire()->user->language;
|
2022-11-05 18:32:48 +01:00
|
|
|
$defaultValue = (string) $this->data[$this->defaultLanguagePageID()];
|
2022-03-08 15:55:41 +01:00
|
|
|
|
|
|
|
if(!$language || !$language->id || $language->isDefault()) return $defaultValue;
|
|
|
|
if($template && $template->noLang) return $defaultValue;
|
|
|
|
|
|
|
|
$languageValue = (string) (empty($this->data[$language->id]) ? '' : $this->data[$language->id]);
|
|
|
|
|
|
|
|
if(!strlen($languageValue)) {
|
|
|
|
// value is blank
|
|
|
|
if($this->field) {
|
2022-11-05 18:32:48 +01:00
|
|
|
if($this->field->get('langBlankInherit') == self::langBlankInheritDefault) {
|
2022-03-08 15:55:41 +01:00
|
|
|
// inherit value from default language
|
|
|
|
$languageValue = $defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $languageValue;
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Set field that value is for
|
|
|
|
*
|
|
|
|
* @param Field $field
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function setField(Field $field) {
|
|
|
|
$this->field = $field;
|
|
|
|
}
|
2022-11-05 18:32:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set page that value is for
|
|
|
|
*
|
|
|
|
* @param Page $page
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function setPage(Page $page) {
|
|
|
|
$this->page = $page;
|
|
|
|
}
|
2022-11-05 18:32:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get page that value is for
|
|
|
|
*
|
|
|
|
* @return Page|null
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function getPage() {
|
|
|
|
return $this->page;
|
|
|
|
}
|
2022-11-05 18:32:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get field that value is for
|
|
|
|
*
|
|
|
|
* @return Field|null
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function getField() {
|
|
|
|
return $this->field;
|
|
|
|
}
|
2022-11-05 18:32:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Debug info
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function __debugInfo() {
|
|
|
|
$info = parent::__debugInfo();
|
2022-11-05 18:32:48 +01:00
|
|
|
foreach($this->wire()->languages as $language) {
|
2022-03-08 15:55:41 +01:00
|
|
|
$info[$language->name] = isset($this->data[$language->id]) ? $this->data[$language->id] : '';
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Get array of language values stored in here
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @since 3.0.188
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getArray() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get hash of all language values stored in here
|
|
|
|
*
|
|
|
|
* @param bool $verbose Specify true for the hash to also include page and field
|
|
|
|
* @return string
|
|
|
|
* @since 3.0.188
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getHash($verbose = false) {
|
|
|
|
$str = '';
|
|
|
|
if($verbose) $str .= "[$this->page,$this->field]\n";
|
|
|
|
foreach($this->data as $k => $v) {
|
|
|
|
if(!is_string($v)) continue;
|
|
|
|
$str .= "$k:$v\n";
|
|
|
|
}
|
|
|
|
return sha1($str);
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:55:41 +01:00
|
|
|
/**
|
|
|
|
* Allows iteration of the languages values
|
|
|
|
*
|
|
|
|
* Fulfills \IteratorAggregate interface.
|
|
|
|
*
|
|
|
|
* @return \ArrayObject
|
|
|
|
*
|
|
|
|
*/
|
2022-11-05 18:32:48 +01:00
|
|
|
#[\ReturnTypeWillChange]
|
2022-03-08 15:55:41 +01:00
|
|
|
public function getIterator() {
|
|
|
|
return new \ArrayObject($this->data);
|
|
|
|
}
|
2022-11-05 18:32:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|LanguageSupport
|
|
|
|
* @throws WireException
|
|
|
|
* @throws WirePermissionException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function languageSupport() {
|
|
|
|
if($this->languageSupport) return $this->languageSupport;
|
|
|
|
$this->languageSupport = $this->wire()->modules->get('LanguageSupport');
|
|
|
|
$this->defaultLanguagePageID = $this->languageSupport->defaultLanguagePageID;
|
|
|
|
return $this->languageSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function defaultLanguagePageID() {
|
|
|
|
if(!$this->defaultLanguagePageID) $this->languageSupport();
|
|
|
|
return $this->defaultLanguagePageID;
|
|
|
|
}
|
2022-03-08 15:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|