Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/Backend/Modules/Quotes/Entity/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ final class Quote
/** @var string $quote */
private $quote;

/** @var string $image */
/** @var string|null $image */
private $image;

/** @var string $author */
private $author;

/** @var bool $visible */
private $visible;

/** @var DateTime $createdOn */
private $createdOn;

Expand All @@ -40,6 +46,8 @@ final class Quote
* @param string $editedOn
* @param string $language
* @param string|null $image
* @param string $author
* @param bool $visible
* @param int|null $id
* @param int|null $modulesExtrasId
*/
Expand All @@ -49,14 +57,18 @@ private function __construct(
$createdOn,
$editedOn,
$language,
$author,
$image = null,
$visible = true,
$id = null,
$modulesExtrasId = null
) {
$this->id = $id;
$this->name = $name;
$this->quote = $quote;
$this->image = $image;
$this->author = $author;
$this->visible = $visible;
$this->createdOn = $createdOn;
$this->editedOn = $editedOn;
$this->language = $language;
Expand All @@ -66,19 +78,23 @@ private function __construct(
/**
* @param string $name
* @param string $quote
* @param string $image
* @param string $author
* @param string|null $image
* @param bool $visible
*
* @return Quote
*/
public static function create($name, $quote, $image = null)
public static function create($name, $quote, $author, $image = null, $visible = true)
{
return new self(
$name,
$quote,
new DateTime(),
new DateTime(),
Language::callLanguageFunction('getWorkingLanguage'),
$image
$author,
$image,
$visible
);
}

Expand Down Expand Up @@ -115,13 +131,15 @@ public function setModulesExtrasId($modulesExtrasId)
/**
* @param string $name
* @param string $quote
* @param string|null $author
*
* @return Quote
*/
public function changeQuote($name, $quote)
public function changeQuote($name, $quote, $author = null)
{
$this->name = $name;
$this->quote = $quote;
$this->author = $author;
$this->editedOn = new DateTime();

return $this;
Expand Down Expand Up @@ -152,6 +170,8 @@ public function toArray()
'name' => $this->name,
'quote' => $this->quote,
'image' => $this->image,
'author' => $this->author,
'visible' => $this->visible,
'created_on' => $this->createdOn,
'edited_on' => $this->editedOn,
'language' => $this->language,
Expand Down Expand Up @@ -199,6 +219,22 @@ public function getQuote()
return $this->quote;
}

/**
* @return string
*/
public function getAuthor()
{
return $this->author;
}

/**
* @return bool
*/
public function isVisible(): bool
{
return $this->visible;
}

/**
* @return null|string
*/
Expand Down Expand Up @@ -268,9 +304,19 @@ public static function fromArray(array $quoteRecord)
DateTime::createFromFormat('Y-m-d H:i:s', $quoteRecord['created_on']),
DateTime::createFromFormat('Y-m-d H:i:s', $quoteRecord['edited_on']),
$quoteRecord['language'],
$quoteRecord['author'],
$quoteRecord['image'],
(bool) $quoteRecord['visible'],
$quoteRecord['id'],
$quoteRecord['modules_extras_id']
);
}

/**
* @param bool $visible
*/
public function changeVisibility($visible = true)
{
$this->visible = $visible;
}
}
22 changes: 19 additions & 3 deletions src/Backend/Modules/Quotes/Form/QuoteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ public function build()
null,
'form-control title',
'form-control danger title'
);
)->makeRequired();

$this->form->addText(
'author',
$this->quote instanceof Quote ? $this->quote->getAuthor() : null
)->makeRequired();

$this->form->addEditor(
'quote',
$this->quote instanceof Quote ? $this->quote->getQuote() : null
)->makeRequired();

$this->form->addCheckbox(
'visible',
$this->quote instanceof Quote ? $this->quote->isVisible() : true
);

$this->form->addImage('image');
Expand All @@ -78,6 +88,7 @@ private function isValid()

$fields['name']->isFilled(Language::err('FieldIsRequired'));
$fields['quote']->isFilled(Language::err('FieldIsRequired'));
$fields['author']->isFilled(Language::err('FieldIsRequired'));

return $this->form->isCorrect();
}
Expand All @@ -95,9 +106,12 @@ public function handle()
if ($this->quote instanceof Quote) {
$this->quote->changeQuote(
$fields['name']->getValue(),
$fields['quote']->getValue()
$fields['quote']->getValue(),
$fields['author']->getValue()
);

$this->quote->changeVisibility($fields['visible']->isChecked());

if ($fields['image']->isFilled()) {
$this->quote->deleteImage();
}
Expand All @@ -110,7 +124,9 @@ public function handle()
$this->quote = Quote::create(
$fields['name']->getValue(),
$fields['quote']->getValue(),
$this->handleImage($fields['image'])
$fields['author']->getValue(),
$this->handleImage($fields['image']),
$fields['visible']->isChecked()
);

return true;
Expand Down
2 changes: 2 additions & 0 deletions src/Backend/Modules/Quotes/Installer/Data/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ CREATE TABLE IF NOT EXISTS `quotes` (
`language` varchar(2) NOT NULL DEFAULT 'nl',
`quote` text NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`author` varchar(255) NOT NULL DEFAULT '',,
`image` varchar(255) DEFAULT NULL,
`visible` tinyint(1) NOT NULL DEFAULT '1',
`edited_on` datetime NOT NULL,
`created_on` datetime NOT NULL,
PRIMARY KEY (`id`),
Expand Down
17 changes: 17 additions & 0 deletions src/Backend/Modules/Quotes/Layout/Templates/Add.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
</label>
{% form_field name %} {% form_field_error name %}
</div>
<div class="form-group">
<label for="visible" class="control-label">
{{ 'lbl.Visible'|trans|capitalize }}
</label>
{% form_field visible %} {% form_field_error visible %}
</div>
<div class="panel panel-default panel-editor">
<div class="panel-heading">
<label for="quote" class="control-label">
Expand All @@ -23,6 +29,17 @@
{% form_field quote %} {% form_field_error quote %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<label for="author" class="control-label">
{{ 'lbl.Author'|trans|capitalize }}
<abbr data-toggle="tooltip" title="{{ 'lbl.RequiredField'|trans|capitalize }}">*</abbr><br>
</label>
</div>
<div class="panel-body">
{% form_field author %} {% form_field_error author %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<label for="quote" class="control-label">
Expand Down
17 changes: 17 additions & 0 deletions src/Backend/Modules/Quotes/Layout/Templates/Edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
</label>
{% form_field name %} {% form_field_error name %}
</div>
<div class="form-group">
<label for="visible" class="control-label">
{% form_field visible %} {{ 'lbl.VisibleOnSite'|trans|capitalize }}
</label>
{% form_field_error visible %}
</div>
<div class="panel panel-default panel-editor">
<div class="panel-heading">
<label for="quote" class="control-label">
Expand All @@ -23,6 +29,17 @@
{% form_field quote %} {% form_field_error quote %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<label for="author" class="control-label">
{{ 'lbl.Author'|trans|capitalize }}
<abbr data-toggle="tooltip" title="{{ 'lbl.RequiredField'|trans|capitalize }}">*</abbr><br>
</label>
</div>
<div class="panel-body">
{% form_field author %} {% form_field_error author %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<label for="quote" class="control-label">
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/Modules/Quotes/Repository/QuotesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(SpoonDatabase $database)
*/
public function getDataGridQuery()
{
return 'SELECT id, name, quote, created_on, edited_on
return 'SELECT id, name, quote, author, visible, created_on, edited_on
FROM quotes WHERE language = :language';
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public function find($id)
*/
public function getAllQuotes()
{
$quotes = (array) $this->database->getRecords('SELECT * FROM quotes');
$quotes = (array) $this->database->getRecords('SELECT * FROM quotes WHERE visible = 1');

return array_map(
function (array $quote) {
Expand All @@ -90,7 +90,7 @@ function (array $quote) {
*/
public function getRandomQuote()
{
$randomId = (int) $this->database->getVar('SELECT id FROM quotes ORDER BY RAND() LIMIT 1');
$randomId = (int) $this->database->getVar('SELECT id FROM quotes WHERE visible = 1 ORDER BY RAND() LIMIT 1');

return $this->find($randomId);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Frontend/Modules/Quotes/Layout/Templates/Quote.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<div class="row align-items-center">
{% if quote.image %}
<div class="col-2">
<img src="{{ quote.imageDirectoryUrl }}/x150/{{ quote.image }}" alt="{{ quote.name }}" class="img-responsive">
<img src="{{ quote.imageDirectoryUrl }}/x150/{{ quote.image }}" alt="{{ quote.author }}" class="img-responsive">
</div>
{% endif %}
<div class="{% if quote.image %} col-10{% else %} col-12{% endif %}">
{{ quote.quote|raw }}
<cite>{{ quote.name }}</cite>
<cite>{{ quote.author }}</cite>
</div>
</div>
</blockquote>
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Modules/Quotes/Widgets/SingleQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ private function parse()
{
$quote = $this->get('quotes_repository')->find($this->data['id']);

$this->template->assign('quote', $quote);
$this->template->assign('quote', $quote->isVisible() ? $quote : []);
}
}