<?php
namespace App\Entity\Core\Textbook;
use App\Entity\Core\RelationTextbookElementImageReference;
use App\Entity\Core\TagMathproblem;
use App\Entity\Core\TemplateMathproblem;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Table('textbook_element')]
#[ORM\Entity(repositoryClass: TextbookElementRepository::class)]
class TextbookElement
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var int
*/
#[ORM\Column(name: 'sortorder', type: 'integer')]
private $order;
/**
* @var string
*/
#[ORM\Column(name: 'type', type: 'string', length: 255)]
private $type;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
private $name;
/**
* @var string
*/
#[ORM\Column(name: 'content', type: 'string', length: 5000)]
private $content;
/**
* @var TextbookSubTopic
*/
#[ORM\JoinColumn(name: 'subtopic_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: TextbookSubTopic::class, inversedBy: 'textbookElements')]
private $textbookSubTopic;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', nullable: true)]
private $title;
/**
* @var string
*/
#[ORM\Column(name: 'explanation', type: 'string', nullable: true, length: 1000)]
private $explanation;
/**
* @var string
*/
#[ORM\Column(name: 'math_index', type: 'string', nullable: true)]
private $mathIndex;
/**
* @var ArrayCollection|TagMathproblem[]
*/
#[ORM\JoinTable(name: 'relation_textbook_element_tag')]
#[ORM\JoinColumn(name: 'textbookelement_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: TagMathproblem::class, inversedBy: 'textbookElements')]
private $tags;
/**
* @var bool
*/
#[ORM\Column(type: 'boolean', name: 'use_as_hint', options: ['default' => true])]
private $useAsHint;
/**
* @var string
*/
#[ORM\Column(name: 'thumbnail', type: 'string', nullable: true)]
private $thumbnail;
/**
* @var Collection|RelationTextbookElementImageReference[]
*/
#[ORM\OneToMany(targetEntity: RelationTextbookElementImageReference::class, mappedBy: 'textbookElement', orphanRemoval: true, cascade: ['persist', 'remove'])]
private array|Collection|ArrayCollection $imageReferences;
public function __construct()
{
$this->imageReferences = new ArrayCollection();
}
/**
* @return int
*/
public function getOrder()
{
return $this->order;
}
/**
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @return TextbookSubTopic
*/
public function getTextbookSubTopic()
{
return $this->textbookSubTopic;
}
/**
* @return string|null
*/
public function getTitle()
{
return $this->title;
}
/**
* @return TagMathproblem[]|ArrayCollection
*/
public function getTags()
{
return $this->tags;
}
/**
* @param TagMathproblem[]|ArrayCollection $tags
*/
public function setTags($tags)
{
$this->tags = $tags;
}
public function isUseAsHint(): bool
{
return $this->useAsHint;
}
/**
* @return $this
*/
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function setUseAsHint(bool $useAsHint): void
{
$this->useAsHint = $useAsHint;
}
public function getMathIndex(): ?string
{
return $this->mathIndex;
}
public function setMathIndex(?string $mathIndex)
{
$this->mathIndex = $mathIndex;
}
public function getId(): int
{
return $this->id;
}
/**
* @return string
*/
public function getExplanation(): ?string
{
return $this->explanation;
}
public function setExplanation(string $explanation): void
{
$this->explanation = $explanation;
}
public function getThumbnail(): ?string
{
return $this->thumbnail;
}
/**
* @param string $thumbnail
*/
public function setThumbnail(?string $thumbnail): void
{
$this->thumbnail = $thumbnail;
}
public function getImageReferences(): Collection
{
return $this->imageReferences;
}
public function setImageReferences(Collection $imageReferences): void
{
$this->imageReferences = $imageReferences;
}
public function getTextbookImageByReference(): ?string
{
foreach ($this->imageReferences as $imageReference) {
if ($imageReference->getSorting() === 0)
return $imageReference->getImageReference()->getFullMediaPath();
}
return null;
}
public function getContentWithInlineImagesParsed(): string
{
$parsedContent = $this->content;
foreach ($this->imageReferences as $imageReference) {
$imageTag = '<img class="mp-img" ';
if ($imageReference->getWidth() !== null) {
$imageTag .= 'style="width: ' . $imageReference->getWidth() . '%" ';
}
$imageTag .= 'src="' . $imageReference->getImageReference()->getFullMediaPath() . '">';
$parsedContent = str_replace('{{ image_' . $imageReference->getSorting() . ' }}', $imageTag, $parsedContent);
}
return $parsedContent;
}
}