<?php
namespace Drupal\tmgmt_local\Entity;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Render\Element;
use Drupal\tmgmt_local\LocalTaskItemInterface;
class LocalTaskItem extends ContentEntityBase implements LocalTaskItemInterface {
use EntityChangedTrait;
protected $unserializedData;
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['tltiid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Local Task Item ID'))
->setDescription(t('The Local Task Item ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['tltid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Local task'))
->setDescription(t('The local task.'))
->setReadOnly(TRUE)
->setSetting('target_type', 'tmgmt_local_task');
$fields['tjiid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Job Item'))
->setDescription(t('The Job Item.'))
->setReadOnly(TRUE)
->setSetting('target_type', 'tmgmt_job_item');
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The job item UUID.'))
->setReadOnly(TRUE);
$fields['data'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Data'))
->setDescription(t('The source data'));
$fields['status'] = BaseFieldDefinition::create('integer')
->setLabel(t('Local task item status'))
->setDescription(t('The local task item status'))
->setDefaultValue(LocalTaskItemInterface::STATUS_PENDING);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the job was last edited.'));
$fields['count_untranslated'] = BaseFieldDefinition::create('integer')
->setLabel(t('Untranslated count'))
->setSetting('unsigned', TRUE);
$fields['count_translated'] = BaseFieldDefinition::create('integer')
->setLabel(t('Translated count'))
->setSetting('unsigned', TRUE);
$fields['count_completed'] = BaseFieldDefinition::create('integer')
->setLabel(t('Accepted count'))
->setSetting('unsigned', TRUE);
$fields['word_count'] = BaseFieldDefinition::create('integer')
->setLabel(t('Word count'))
->setSetting('unsigned', TRUE);
return $fields;
}
public function label() {
if ($job_item = $this
->getJobItem()) {
return $job_item
->label();
}
return t('Missing job item');
}
public function getTask() {
return $this
->get('tltid')->entity;
}
public function getJobItem() {
return $this
->get('tjiid')->entity;
}
public function getStatus() {
return $this
->get('status')->value;
}
public function isPending() {
return $this
->get('status')->value == LocalTaskItemInterface::STATUS_PENDING;
}
public function isCompleted() {
return $this
->get('status')->value == LocalTaskItemInterface::STATUS_COMPLETED;
}
public function isClosed() {
return $this
->get('status')->value == LocalTaskItemInterface::STATUS_CLOSED;
}
public function completed() {
$this
->set('status', LocalTaskItemInterface::STATUS_COMPLETED);
}
public function closed() {
$this
->set('status', LocalTaskItemInterface::STATUS_CLOSED);
}
public function updateData($key, $values = array(), $replace = FALSE) {
$this
->decodeData();
if ($replace) {
NestedArray::setValue($this->unserializedData, \Drupal::service('tmgmt.data')
->ensureArrayKey($key), $values);
}
foreach ($values as $index => $value) {
if (is_array($value)) {
$this
->updateData(array_merge(\Drupal::service('tmgmt.data')
->ensureArrayKey($key), array(
$index,
)), $value);
}
else {
NestedArray::setValue($this->unserializedData, array_merge(\Drupal::service('tmgmt.data')
->ensureArrayKey($key), array(
$index,
)), $value, TRUE);
}
}
}
public function getData($key = array(), $index = NULL) {
$this
->decodeData();
if (empty($this->unserializedData) && $this
->getTask()) {
$this->unserializedData = $this
->getJobItem()
->getData();
$this
->save();
}
if (empty($key)) {
return $this->unserializedData;
}
if ($index) {
$key = array_merge($key, array(
$index,
));
}
return NestedArray::getValue($this->unserializedData, $key);
}
public function getCountTranslated() {
return $this
->get('count_translated')->value;
}
public function getCountUntranslated() {
return $this
->get('count_untranslated')->value;
}
public function getCountCompleted() {
return $this
->get('count_completed')->value;
}
public function getWordCount() {
return $this
->get('word_count')->value;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this
->getTask()) {
$this
->recalculateStatistics();
}
if ($this->unserializedData) {
$this
->set('data', Json::encode($this->unserializedData));
}
elseif (empty($this
->get('data')->value)) {
$this
->set('data', Json::encode(array()));
}
}
public function recalculateStatistics() {
$this
->decodeData();
if (empty($this->unserializedData)) {
$this->unserializedData = $this
->getJobItem()
->getData();
}
if ($this
->isCompleted() || $this
->isClosed()) {
$this
->set('count_translated', 0);
$this
->set('count_completed', count(array_filter(\Drupal::service('tmgmt.data')
->flatten($this->unserializedData), array(
\Drupal::service('tmgmt.data'),
'filterData',
))));
$this
->set('count_untranslated', 0);
}
else {
$this
->set('count_translated', 0);
$this
->set('count_completed', 0);
$this
->set('word_count', 0);
$this
->set('count_untranslated', count(array_filter(\Drupal::service('tmgmt.data')
->flatten($this->unserializedData), array(
\Drupal::service('tmgmt.data'),
'filterData',
))));
$this
->count($this->unserializedData);
}
}
protected function count(array &$item) {
if (!empty($item['#text'])) {
if (\Drupal::service('tmgmt.data')
->filterData($item)) {
$text = is_array($item['#text']) ? $item['#text']['value'] : $item['#text'];
$this
->set('word_count', $this
->get('word_count')->value + \Drupal::service('tmgmt.data')
->wordCount($text));
if (!isset($item['#status'])) {
$item['#status'] = TMGMT_DATA_ITEM_STATE_UNTRANSLATED;
}
switch ($item['#status']) {
case TMGMT_DATA_ITEM_STATE_TRANSLATED:
$this
->set('count_untranslated', $this
->get('count_untranslated')->value - 1);
$this
->set('count_translated', $this
->get('count_translated')->value + 1);
break;
}
}
}
else {
foreach (Element::children($item) as $key) {
$this
->count($item[$key]);
}
}
}
public function getChangedTimeAcrossTranslations() {
return $this
->getChangedTime();
}
protected function invalidateTagsOnSave($update) {
parent::invalidateTagsOnSave($update);
$tags = $this
->getTask()
->getEntityType()
->getListCacheTags();
if ($update) {
$tags = Cache::mergeTags($tags, $this
->getTask()
->getCacheTagsToInvalidate());
}
Cache::invalidateTags($tags);
}
protected function decodeData() {
if (empty($this->unserializedData) && $this
->get('data')->value) {
$this->unserializedData = (array) Json::decode($this
->get('data')->value);
}
if (!is_array($this->unserializedData)) {
$this->unserializedData = [];
}
}
public static function getStatuses() {
return array(
LocalTaskItemInterface::STATUS_PENDING => t('Untranslated'),
LocalTaskItemInterface::STATUS_COMPLETED => t('Translated'),
LocalTaskItemInterface::STATUS_REJECTED => t('Rejected'),
LocalTaskItemInterface::STATUS_CLOSED => t('Completed'),
);
}
}