From 80c7d090e97941d2047661a729b07e7ea7282e7f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 5 Mar 2020 20:35:58 +0100 Subject: [PATCH] fixes Orange-Management/phpOMS#224 and fixes Orange-Management/phpOMS#212 --- Models/NullQAAnswer.php | 38 ++++++++++++++++++++++++++ Models/NullQACategory.php | 38 ++++++++++++++++++++++++++ Models/NullQAQuestion.php | 38 ++++++++++++++++++++++++++ Models/QAAnswer.php | 45 +++++++++++++++++++++++-------- Models/QAAnswerMapper.php | 34 ++++++++++++----------- Models/QACategory.php | 12 ++++----- Models/QACategoryMapper.php | 13 +++++++++ Models/QAQuestion.php | 41 +++++++++++++++++++--------- Models/QAQuestionMapper.php | 6 ++--- Theme/Backend/qa-question.tpl.php | 2 +- 10 files changed, 218 insertions(+), 49 deletions(-) create mode 100644 Models/NullQAAnswer.php create mode 100644 Models/NullQACategory.php create mode 100644 Models/NullQAQuestion.php diff --git a/Models/NullQAAnswer.php b/Models/NullQAAnswer.php new file mode 100644 index 0000000..7dfd3fb --- /dev/null +++ b/Models/NullQAAnswer.php @@ -0,0 +1,38 @@ +id = $id; + } +} diff --git a/Models/NullQACategory.php b/Models/NullQACategory.php new file mode 100644 index 0000000..5870ef0 --- /dev/null +++ b/Models/NullQACategory.php @@ -0,0 +1,38 @@ +id = $id; + } +} diff --git a/Models/NullQAQuestion.php b/Models/NullQAQuestion.php new file mode 100644 index 0000000..5e1b126 --- /dev/null +++ b/Models/NullQAQuestion.php @@ -0,0 +1,38 @@ +id = $id; + } +} diff --git a/Models/QAAnswer.php b/Models/QAAnswer.php index c27e27c..c34f5f6 100644 --- a/Models/QAAnswer.php +++ b/Models/QAAnswer.php @@ -14,6 +14,9 @@ declare(strict_types=1); namespace Modules\QA\Models; +use Modules\Admin\Models\Account; +use Modules\Admin\Models\NullAccount; + /** * Answer class. * @@ -50,7 +53,13 @@ class QAAnswer implements \JsonSerializable */ private $answerRaw = ''; - private $question = 0; + /** + * Question + * + * @var QAQuestion + * @since 1.0.0 + */ + private QAQuestion $question; /** * Is accepted answer. @@ -60,8 +69,20 @@ class QAAnswer implements \JsonSerializable */ private bool $isAccepted = false; - private $createdBy = 0; + /** + * Created by. + * + * @var Account + * @var 1.0.0 + */ + private Account $createdBy; + /** + * Created at. + * + * @var \DateTime + * @var 1.0.0 + */ private \DateTime $createdAt; /** @@ -72,6 +93,8 @@ class QAAnswer implements \JsonSerializable public function __construct() { $this->createdAt = new \DateTime('now'); + $this->createdBy = new NullAccount(); + $this->question = new NullQAQuestion(); } /** @@ -115,11 +138,11 @@ class QAAnswer implements \JsonSerializable /** * Get the question * - * @return int + * @return QAQuestion * * @since 1.0.0 */ - public function getQuestion() : int + public function getQuestion() : QAQuestion { return $this->question; } @@ -127,13 +150,13 @@ class QAAnswer implements \JsonSerializable /** * Set the question * - * @param int $question Question + * @param QAQuestion $question Question * * @return void * * @since 1.0.0 */ - public function setQuestion(int $question) : void + public function setQuestion(QAQuestion $question) : void { $this->question = $question; } @@ -193,11 +216,11 @@ class QAAnswer implements \JsonSerializable /** * Get created by * - * @return int|\phpOMS\Account\Account + * @return Account * * @since 1.0.0 */ - public function getCreatedBy() + public function getCreatedBy() : Account { return $this->createdBy; } @@ -205,15 +228,15 @@ class QAAnswer implements \JsonSerializable /** * Set created by * - * @param mixed $id Creator + * @param Account $account Creator * * @return void * * @since 1.0.0 */ - public function setCreatedBy($id) : void + public function setCreatedBy(Account $account) : void { - $this->createdBy = $id; + $this->createdBy = $account; } /** diff --git a/Models/QAAnswerMapper.php b/Models/QAAnswerMapper.php index b3bc17c..b2e7812 100644 --- a/Models/QAAnswerMapper.php +++ b/Models/QAAnswerMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\QA\Models; -use Modules\Profile\Models\ProfileMapper; +use Modules\Admin\Models\AccountMapper; use phpOMS\DataStorage\Database\DataMapperAbstract; /** @@ -39,10 +39,27 @@ final class QAAnswerMapper extends DataMapperAbstract 'qa_answer_question' => ['name' => 'qa_answer_question', 'type' => 'int', 'internal' => 'question'], 'qa_answer_status' => ['name' => 'qa_answer_status', 'type' => 'int', 'internal' => 'status'], 'qa_answer_accepted' => ['name' => 'qa_answer_accepted', 'type' => 'bool', 'internal' => 'isAccepted'], - 'qa_answer_created_by' => ['name' => 'qa_answer_created_by', 'type' => 'int', 'internal' => 'createdBy'], + 'qa_answer_created_by' => ['name' => 'qa_answer_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], 'qa_answer_created_at' => ['name' => 'qa_answer_created_at', 'type' => 'DateTime', 'internal' => 'createdAt', 'readonly' => true], ]; + /** + * Belongs to. + * + * @var array + * @since 1.0.0 + */ + protected static array $belongsTo = [ + 'createdBy' => [ + 'mapper' => AccountMapper::class, + 'self' => 'qa_answer_created_by', + ], + 'question' => [ + 'mapper' => QAQuestionMapper::class, + 'self' => 'qa_answer_question', + ], + ]; + /** * Primary table. * @@ -66,17 +83,4 @@ final class QAAnswerMapper extends DataMapperAbstract * @since 1.0.0 */ protected static string $primaryField = 'qa_answer_id'; - - /** - * Belongs to. - * - * @var array - * @since 1.0.0 - */ - protected static array $belongsTo = [ - 'createdBy' => [ - 'mapper' => ProfileMapper::class, - 'self' => 'qa_answer_created_by', - ], - ]; } diff --git a/Models/QACategory.php b/Models/QACategory.php index dc8fe6b..09d064d 100644 --- a/Models/QACategory.php +++ b/Models/QACategory.php @@ -43,10 +43,10 @@ class QACategory implements \JsonSerializable /** * Parent category. * - * @var null|int|QACategory + * @var null|QACategory * @since 1.0.0 */ - private $parent = null; + private ?self $parent = null; /** * Get id. @@ -89,11 +89,11 @@ class QACategory implements \JsonSerializable /** * Get the parent category * - * @return null|int + * @return null|self * * @since 1.0.0 */ - public function getParent() : ?int + public function getParent() : ?self { return $this->parent; } @@ -101,13 +101,13 @@ class QACategory implements \JsonSerializable /** * Set the parent category * - * @param int $parent Parent category + * @param null|self $parent Parent category * * @return void * * @since 1.0.0 */ - public function setParent(int $parent) : void + public function setParent(?self $parent) : void { $this->parent = $parent; } diff --git a/Models/QACategoryMapper.php b/Models/QACategoryMapper.php index 29ffe59..371c054 100644 --- a/Models/QACategoryMapper.php +++ b/Models/QACategoryMapper.php @@ -38,6 +38,19 @@ final class QACategoryMapper extends DataMapperAbstract 'qa_category_parent' => ['name' => 'qa_category_parent', 'type' => 'int', 'internal' => 'parent'], ]; + /** + * Belongs to. + * + * @var array + * @since 1.0.0 + */ + protected static array $belongsTo = [ + 'parent' => [ + 'mapper' => self::class, + 'self' => 'qa_category_parent', + ], + ]; + /** * Primary table. * diff --git a/Models/QAQuestion.php b/Models/QAQuestion.php index 7e0926b..12ecebb 100644 --- a/Models/QAQuestion.php +++ b/Models/QAQuestion.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace Modules\QA\Models; +use Modules\Admin\Models\Account; +use Modules\Admin\Models\NullAccount; use Modules\Tag\Models\Tag; /** @@ -61,16 +63,28 @@ class QAQuestion implements \JsonSerializable /** * Category. * - * @var int + * @var QACategory * @since 1.0.0 */ - private $category = 0; + private ?QACategory $category = null; private $language = ''; - private $createdBy = 0; + /** + * Created by. + * + * @var Account + * @since 1.0.0 + */ + private Account $createdBy; - private $createdAt = null; + /** + * Created at. + * + * @var \DateTime + * @since 1.0.0 + */ + private \DateTime $createdAt; /** * Badges. @@ -96,6 +110,7 @@ class QAQuestion implements \JsonSerializable public function __construct() { $this->createdAt = new \DateTime('now'); + $this->createdBy = new NullAccount(); } /** @@ -257,21 +272,21 @@ class QAQuestion implements \JsonSerializable * * @since 1.0.0 */ - public function getCategory() + public function getCategory() : QACategory { - return $this->category; + return $this->category ?? new NullQACategory(); } /** * Set the category * - * @param int $category Category + * @param null|QACategory $category Category * * @return void * * @since 1.0.0 */ - public function setCategory(int $category) : void + public function setCategory(?QACategory $category) : void { $this->category = $category; } @@ -279,11 +294,11 @@ class QAQuestion implements \JsonSerializable /** * Get created by * - * @return int|\phpOMS\Account\Account + * @return Account * * @since 1.0.0 */ - public function getCreatedBy() + public function getCreatedBy() : Account { return $this->createdBy; } @@ -291,15 +306,15 @@ class QAQuestion implements \JsonSerializable /** * Set created by * - * @param mixed $id Created by + * @param Account $account Created by * * @return void * * @since 1.0.0 */ - public function setCreatedBy($id) : void + public function setCreatedBy(Account $account) : void { - $this->createdBy = $id; + $this->createdBy = $account; } /** diff --git a/Models/QAQuestionMapper.php b/Models/QAQuestionMapper.php index f4c0b02..547971b 100644 --- a/Models/QAQuestionMapper.php +++ b/Models/QAQuestionMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\QA\Models; -use Modules\Profile\Models\ProfileMapper; +use Modules\Admin\Models\AccountMapper; use phpOMS\DataStorage\Database\DataMapperAbstract; /** @@ -40,7 +40,7 @@ final class QAQuestionMapper extends DataMapperAbstract 'qa_question_question' => ['name' => 'qa_question_question', 'type' => 'string', 'internal' => 'question'], 'qa_question_status' => ['name' => 'qa_question_status', 'type' => 'int', 'internal' => 'status'], 'qa_question_category' => ['name' => 'qa_question_category', 'type' => 'int', 'internal' => 'category'], - 'qa_question_created_by' => ['name' => 'qa_question_created_by', 'type' => 'int', 'internal' => 'createdBy'], + 'qa_question_created_by' => ['name' => 'qa_question_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], 'qa_question_created_at' => ['name' => 'qa_question_created_at', 'type' => 'DateTime', 'internal' => 'createdAt', 'readonly' => true], ]; @@ -80,7 +80,7 @@ final class QAQuestionMapper extends DataMapperAbstract */ protected static array $belongsTo = [ 'createdBy' => [ - 'mapper' => ProfileMapper::class, + 'mapper' => AccountMapper::class, 'self' => 'qa_question_created_by', ], ]; diff --git a/Theme/Backend/qa-question.tpl.php b/Theme/Backend/qa-question.tpl.php index 303a15a..3d17611 100644 --- a/Theme/Backend/qa-question.tpl.php +++ b/Theme/Backend/qa-question.tpl.php @@ -43,7 +43,7 @@ echo $this->getData('nav')->render();
- printHtml($answer->getAnswer()); ?>printHtml($answer->getCreatedAt()->format('Y-m-d')); ?>printHtml($answer->getCreatedBy()); ?>printHtml($answer->getStatus()); ?>printHtml($answer->isAccepted()); ?> + printHtml($answer->getAnswer()); ?>printHtml($answer->getCreatedAt()->format('Y-m-d')); ?>printHtml($answer->getCreatedBy()->getId()); ?>printHtml($answer->getStatus()); ?>printHtml($answer->isAccepted()); ?>