From ae32126971466d066c03770652b38f23e2e57fb6 Mon Sep 17 00:00:00 2001 From: Mikron Date: Tue, 3 Oct 2023 23:39:38 +0200 Subject: [PATCH] Added domain for Scribble Concerns #253 --- common/models/Scribble.php | 73 +++++++++++++++ common/models/ScribblePack.php | 94 ++++++++++++++++++++ common/models/ScribblePackQuery.php | 35 ++++++++ common/models/ScribbleQuery.php | 32 +++++++ console/migrations/m230910_123838_v1_1_0.php | 80 +++++++++++++++++ 5 files changed, 314 insertions(+) create mode 100644 common/models/Scribble.php create mode 100644 common/models/ScribblePack.php create mode 100644 common/models/ScribblePackQuery.php create mode 100644 common/models/ScribbleQuery.php diff --git a/common/models/Scribble.php b/common/models/Scribble.php new file mode 100644 index 0000000..a791eb1 --- /dev/null +++ b/common/models/Scribble.php @@ -0,0 +1,73 @@ + true, 'targetClass' => ScribblePack::class, 'targetAttribute' => ['scribble_pack_id' => 'scribble_pack_id']], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + public function attributeLabels() + { + return [ + 'scribble_id' => Yii::t('app', 'Scribble ID'), + 'scribble_pack_id' => Yii::t('app', 'Scribble Pack ID'), + 'user_id' => Yii::t('app', 'User ID'), + 'favorite' => Yii::t('app', 'Favorite'), + ]; + } + + /** + * Gets query for [[ScribblePack]]. + * + * @return ActiveQuery|ScribblePackQuery + */ + public function getScribblePack(): ActiveQuery|ScribblePackQuery + { + return $this->hasOne(ScribblePack::class, ['scribble_pack_id' => 'scribble_pack_id']); + } + + /** + * Gets query for [[User]]. + * + * @return ActiveQuery + */ + public function getUser(): ActiveQuery + { + return $this->hasOne(User::class, ['id' => 'user_id']); + } + + /** + * @return ScribbleQuery the active query used by this AR class. + */ + public static function find(): ScribbleQuery + { + return new ScribbleQuery(get_called_class()); + } +} diff --git a/common/models/ScribblePack.php b/common/models/ScribblePack.php new file mode 100644 index 0000000..1b112e7 --- /dev/null +++ b/common/models/ScribblePack.php @@ -0,0 +1,94 @@ + 20], + ]; + } + + public function attributeLabels(): array + { + return [ + 'scribble_pack_id' => Yii::t('app', 'SCRIBBLE_PACK_ID'), + 'class' => Yii::t('app', 'SCRIBBLE_PACK_CLASS'), + ]; + } + + /** + * Gets query for [[Characters]]. + * + * @return ActiveQuery + */ + public function getCharacters(): ActiveQuery + { + return $this->hasMany(Character::class, ['scribble_pack_id' => 'scribble_pack_id']); + } + + /** + * Gets query for [[Groups]]. + * + * @return ActiveQuery + */ + public function getGroups(): ActiveQuery + { + return $this->hasMany(Group::class, ['scribble_pack_id' => 'scribble_pack_id']); + } + + /** + * Gets query for [[Scribbles]]. + * + * @return ActiveQuery|ScribbleQuery + */ + public function getScribbles() + { + return $this->hasMany(Scribble::class, ['scribble_pack_id' => 'scribble_pack_id']); + } + + public static function find(): ScribblePackQuery + { + return new ScribblePackQuery(get_called_class()); + } + + public function canUserReadYou(): bool + { + $className = 'common\models\\' . $this->class; + /** @var HasEpicControl $object */ + $object = ($className)::findOne(['description_pack_id' => $this->description_pack_id]); + return $object->canUserViewYou(); + } + + public function canUserControlYou(): bool + { + $className = 'common\models\\' . $this->class; + /** @var HasEpicControl $object */ + $object = ($className)::findOne(['description_pack_id' => $this->description_pack_id]); + return $object->canUserControlYou(); + } +} diff --git a/common/models/ScribblePackQuery.php b/common/models/ScribblePackQuery.php new file mode 100644 index 0000000..7560d8e --- /dev/null +++ b/common/models/ScribblePackQuery.php @@ -0,0 +1,35 @@ +andWhere('[[status]]=1'); + }*/ + + /** + * @return ScribblePack[]|array + */ + public function all($db = null): array + { + return parent::all($db); + } + + /** + * @param null $db + * @return array|ActiveRecord|null + */ + public function one($db = null): array|ActiveRecord|null + { + return parent::one($db); + } +} diff --git a/common/models/ScribbleQuery.php b/common/models/ScribbleQuery.php new file mode 100644 index 0000000..66c6196 --- /dev/null +++ b/common/models/ScribbleQuery.php @@ -0,0 +1,32 @@ +andWhere('[[status]]=1'); + }*/ + + /** + * @return Scribble[]|array + */ + public function all($db = null): array + { + return parent::all($db); + } + + /** + * @return Scribble|array|null + */ + public function one($db = null): Scribble|array|null + { + return parent::one($db); + } +} diff --git a/console/migrations/m230910_123838_v1_1_0.php b/console/migrations/m230910_123838_v1_1_0.php index db3f4ce..4f7fc1e 100644 --- a/console/migrations/m230910_123838_v1_1_0.php +++ b/console/migrations/m230910_123838_v1_1_0.php @@ -24,10 +24,90 @@ public function safeUp() /* Session location added */ $this->addColumn('{{%game}}', 'planned_location', $this->string(80)->after('planned_date')); + + /* Scribbles created */ + $this->createTable( + '{{%scribble_pack}}', + [ + 'scribble_pack_id' => $this->primaryKey()->unsigned(), + 'class' => $this->string(20)->notNull()->comment("Name of class this pack belongs to; necessary for proper type assignment"), + ], + $tableOptions + ); + + $this->createTable( + '{{%scribble}}', + [ + 'scribble_id' => $this->primaryKey()->unsigned(), + 'scribble_pack_id' => $this->integer(11)->unsigned(), + 'user_id' => $this->integer(10)->unsigned(), + 'favorite' => $this->boolean(), + ], + $tableOptions + ); + + $this->addForeignKey( + 'scribble_pack', + '{{%scribble}}', + 'scribble_pack_id', + '{{%scribble_pack}}', + 'scribble_pack_id', + 'RESTRICT', + 'CASCADE' + ); + $this->addForeignKey( + 'scribble_user', + '{{%scribble}}', + 'user_id', + '{{%user}}', + 'id', + 'RESTRICT', + 'CASCADE' + ); + + $this->addColumn( + '{{%character}}', + 'scribble_pack_id', + $this->integer(11)->unsigned()->after('importance_pack_id') + ); + $this->addColumn( + '{{%group}}', + 'scribble_pack_id', + $this->integer(11)->unsigned()->after('importance_pack_id') + ); + + $this->addForeignKey( + 'character_scribble_pack', + '{{%character}}', + 'scribble_pack_id', + '{{%scribble_pack}}', + 'scribble_pack_id', + 'RESTRICT', + 'CASCADE' + ); + $this->addForeignKey( + 'group_scribble_pack', + '{{%group}}', + 'scribble_pack_id', + '{{%scribble_pack}}', + 'scribble_pack_id', + 'RESTRICT', + 'CASCADE' + ); } public function safeDown() { + /* Scribbles removed */ + $this->dropForeignKey('group_scribble_pack', '{{%group}}'); + $this->dropForeignKey('character_scribble_pack', '{{%character}}'); + + $this->dropColumn('{{%group}}', 'scribble_pack_id'); + $this->dropColumn('{{%character}}', 'scribble_pack_id'); + + $this->dropTable('{{%scribble}}'); + $this->dropTable('{{%scribble_pack}}'); + /* Group updated_at removed */ $this->dropColumn('{{%group}}', 'updated_at');