View source
<?php
namespace Drupal\opigno_ilt\Entity;
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\Field\FieldStorageDefinitionInterface;
use Drupal\opigno_calendar_event\Entity\CalendarEvent;
use Drupal\opigno_ilt\ILTInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
class ILT extends ContentEntityBase implements ILTInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$uid = \Drupal::currentUser()
->id();
$values += [
'user_id' => $uid,
];
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getStartDate() {
return $this
->get('date')->value;
}
public function getEndDate() {
return $this
->get('date')->end_value;
}
public function getDate() {
return $this
->get('date')
->getValue()[0];
}
public function setDate(array $date) {
$this
->set('date', $date);
return $this;
}
public function getPlace() {
return $this
->get('place')->value;
}
public function setPlace($place) {
$this
->set('place', $place);
return $this;
}
public function getTrainerId() {
return $this
->get('trainer')->target_id;
}
public function setTrainerId($uid) {
$this
->set('trainer', $uid);
return $this;
}
public function getTrainingId() {
return $this
->get('training')->target_id;
}
public function setTrainingId($id) {
$this
->set('training', [
'target_id' => $id,
]);
return $this;
}
public function getTraining() {
return $this
->get('training')->entity;
}
public function getResultsIds() {
$iltr_ids = \Drupal::entityQuery('opigno_ilt_result')
->condition('opigno_ilt', $this
->id())
->execute();
if ($iltr_ids) {
return $iltr_ids;
}
return NULL;
}
public function setTraining($training) {
$this
->set('training', [
'target_id' => $training
->id(),
]);
return $this;
}
public function getCalendarEventId() {
return $this
->get('calendar_event')->target_id;
}
public function setCalendarEventId($id) {
$this
->set('calendar_event', [
'target_id' => $id,
]);
return $this;
}
public function getCalendarEvent() {
return $this
->get('calendar_event')->entity;
}
public function setCalendarEvent(CalendarEvent $event) {
$this
->set('calendar_event', [
'target_id' => $event
->id(),
]);
return $this;
}
public function addMember($uid) {
$this
->get('members')
->appendItem([
'target_id' => $uid,
]);
return $this;
}
public function removeMember($uid) {
$values = $this
->get('members')
->getValue();
$values = array_filter($values, function ($value) use ($uid) {
return $value['target_id'] != $uid;
});
$this
->set('members', $values);
return $this;
}
public function getMembersIds() {
$values = $this
->get('members')
->getValue();
return array_map(function ($value) {
return $value['target_id'];
}, $values);
}
public function setMembersIds(array $ids) {
$this
->set('members', $ids);
return $this;
}
public function getMembers() {
$ids = $this
->getMembersIds();
return User::loadMultiple($ids);
}
public function addNotifiedMember($uid) {
$this
->get('notified_members')
->appendItem([
'target_id' => $uid,
]);
return $this;
}
public function removeNotifiedMember($uid) {
$values = $this
->get('notified_members')
->getValue();
$values = array_filter($values, function ($value) use ($uid) {
return $value['target_id'] != $uid;
});
$this
->set('notified_members', $values);
return $this;
}
public function getNotifiedMembersIds() {
$values = $this
->get('notified_members')
->getValue();
return array_map(function ($value) {
return $value['target_id'];
}, $values);
}
public function setNotifiedMembersIds(array $ids) {
$this
->set('notified_members', $ids);
return $this;
}
public function getNotifiedMembers() {
$ids = $this
->getNotifiedMembersIds();
return User::loadMultiple($ids);
}
public function isMember($user_id) {
$members_ids = $this
->getMembersIds();
if (empty($members_ids)) {
$training = $this
->getTraining();
if (isset($training)) {
$members_ids = array_map(function ($member) {
return $member
->getUser()
->id();
}, $training
->getMembers());
}
}
if (!in_array($user_id, $members_ids)) {
return FALSE;
}
return TRUE;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the ILT entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the ILT entity.'))
->setReadOnly(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The owner of the ILT entity.'))
->setSettings([
'target_type' => 'user',
'handler' => 'default',
])
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of the ILT entity.'))
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['date'] = BaseFieldDefinition::create('daterange')
->setName('date')
->setLabel(t('Date'))
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['place'] = BaseFieldDefinition::create('string')
->setLabel(t('Place'))
->setDescription(t('The address of the ILT entity.'))
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['trainer'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Trainer'))
->setDescription(t('The trainer of the ILT entity.'))
->setSettings([
'target_type' => 'user',
'handler' => 'default',
])
->setReadOnly(TRUE);
$fields['training'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Related training'))
->setDescription(t('The related training of the ILT entity.'))
->setSettings([
'target_type' => 'group',
'handler' => 'default:group',
'handler_settings' => [
'target_bundles' => [
'learning_path' => 'learning_path',
],
'sort' => [
'field' => '_none',
],
],
]);
$fields['calendar_event'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Related calendar event'))
->setDescription(t('The related calendar event of the ILT entity.'))
->setSettings([
'target_type' => 'opigno_calendar_event',
'handler' => 'default',
]);
$fields['members'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Members'))
->setDescription(t('The members of the ILT entity.'))
->setSettings([
'target_type' => 'user',
'handler' => 'default',
])
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$fields['notified_members'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Notified Members'))
->setDescription(t('Members of the ILT entity that marked as notified by email.'))
->setSettings([
'target_type' => 'user',
'handler' => 'default',
])
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
return $fields;
}
}