From bb9dc0a3dfdbb2019f2dfe8554f5033808b3d62b Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 28 Mar 2018 01:49:14 +0300 Subject: [PATCH 1/3] Report migration and models --- .../m180327_221657_add_report_table.php | 29 +++++ models/Report.php | 106 ++++++++++++++++++ models/ReportQuery.php | 45 ++++++++ 3 files changed, 180 insertions(+) create mode 100644 migrations/m180327_221657_add_report_table.php create mode 100644 models/Report.php create mode 100644 models/ReportQuery.php diff --git a/migrations/m180327_221657_add_report_table.php b/migrations/m180327_221657_add_report_table.php new file mode 100644 index 00000000..69150fa6 --- /dev/null +++ b/migrations/m180327_221657_add_report_table.php @@ -0,0 +1,29 @@ +createTable('{{%report}}', [ + 'id' => $this->primaryKey(), + 'status' => $this->smallInteger()->notNull()->defaultValue(10), + 'object_type' => $this->string()->notNull(), + 'object_id' => $this->integer()->notNull(), + 'content' => $this->text()->notNull(), + 'created_at' => $this->dateTime()->notNull(), + 'updated_at' => $this->dateTime(), + 'creator_id' => $this->integer(), + 'updater_id' => $this->integer(), + ], $this->tableOptions); + + $this->addForeignKey('fk-report-creator_id-user-id', '{{%report}}', 'creator_id', '{{%user}}', 'id', 'SET NULL'); + $this->addForeignKey('fk-report-updater_id-user-id', '{{%report}}', 'updater_id', '{{%user}}', 'id', 'SET NULL'); + } + + public function down() + { + $this->dropTable('{{%report}}'); + } +} diff --git a/models/Report.php b/models/Report.php new file mode 100644 index 00000000..665ee27c --- /dev/null +++ b/models/Report.php @@ -0,0 +1,106 @@ + 255], + ]; + } + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'timestamp' => $this->timeStampBehavior(), + 'blameable' => [ + 'class' => BlameableBehavior::class, + 'createdByAttribute' => 'creator_id', + 'updatedByAttribute' => 'updater_id', + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'status' => 'Status', + 'object_type' => 'Object Type', + 'object_id' => 'Object ID', + 'content' => 'Content', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + 'creator_id' => 'Creator ID', + 'updater_id' => 'Updater ID', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUpdater() + { + return $this->hasOne(User::class, ['id' => 'updater_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCreator() + { + return $this->hasOne(User::class, ['id' => 'creator_id']); + } + + /** + * @inheritdoc + * @return ReportQuery the active query used by this AR class. + */ + public static function find() + { + return new ReportQuery(static::class); + } +} diff --git a/models/ReportQuery.php b/models/ReportQuery.php new file mode 100644 index 00000000..b01522e6 --- /dev/null +++ b/models/ReportQuery.php @@ -0,0 +1,45 @@ +andWhere(['status' => Report::STATUS_OPEN]); + } + + /** + * @return $this + */ + public function done() + { + return $this->andWhere(['status' => Report::STATUS_DONE]); + } + + /** + * @inheritdoc + * @return Report[]|array + */ + public function all($db = null) + { + return parent::all($db); + } + + /** + * @inheritdoc + * @return Report|array|null + */ + public function one($db = null) + { + return parent::one($db); + } +} From 507d847b4e0aba1d4a31696e00de0b314ba20db1 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 28 Mar 2018 02:47:00 +0300 Subject: [PATCH 2/3] Added reporting form --- config/urls.php | 4 ++ controllers/ReportController.php | 73 ++++++++++++++++++++++++++++++++ models/CommentQuery.php | 18 ++++++++ models/Report.php | 36 +++++++++++++++- views/report/create.php | 33 +++++++++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 controllers/ReportController.php create mode 100644 views/report/create.php diff --git a/config/urls.php b/config/urls.php index c6b8df78..2bbc6ef3 100755 --- a/config/urls.php +++ b/config/urls.php @@ -68,6 +68,10 @@ 'extensions' => 'extension/index', 'extensions/' => 'extension/', // TODO forbidden names to avoid conflict! + // content reporting + + 'reports/' => 'report/', + // TODO handle URLs from old site // /ext/files/?id=864 'extensions/page/' => 'extension/index', diff --git a/controllers/ReportController.php b/controllers/ReportController.php new file mode 100644 index 00000000..73af22ef --- /dev/null +++ b/controllers/ReportController.php @@ -0,0 +1,73 @@ + [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['create'], + 'allow' => true, + 'roles' => ['@'], + ], + ], + ], + ]; + } + + /** + * @param string $type + * @param int $id + * @return string + * @throws BadRequestHttpException + */ + public function actionCreate($type, $id) + { + $this->sectionTitle = 'Report content'; + $this->headTitle = 'Report content'; + + $report = new Report(); + $report->object_id = $id; + $report->object_type = $type; + + $object = $report->getObject(); + + if ($object === null) { + throw new BadRequestHttpException('There is no such object.'); + } + + $content = Yii::$app->request->post('content'); + if ($content) { + $report->content = $content; + if ($report->save()) { + Yii::$app->session->setFlash('success', 'Report received. Thank you!'); + Yii::$app->response->redirect(Url::previous()); + } + } + + return $this->render('create', [ + 'report' => $report, + 'object' => $object, + ]); + } +} \ No newline at end of file diff --git a/models/CommentQuery.php b/models/CommentQuery.php index 0993b0c7..477d9a64 100644 --- a/models/CommentQuery.php +++ b/models/CommentQuery.php @@ -6,6 +6,24 @@ class CommentQuery extends ActiveQuery { + /** + * @inheritdoc + * @return Comment[]|array + */ + public function all($db = null) + { + return parent::all($db); + } + + /** + * @inheritdoc + * @return Comment|array|null + */ + public function one($db = null) + { + return parent::one($db); + } + /** * @return $this */ diff --git a/models/Report.php b/models/Report.php index 665ee27c..d5b41c35 100644 --- a/models/Report.php +++ b/models/Report.php @@ -2,6 +2,7 @@ namespace app\models; +use app\components\object\ClassType; use yii\behaviors\BlameableBehavior; /** @@ -25,6 +26,15 @@ class Report extends ActiveRecord const STATUS_OPEN = 10; const STATUS_DONE = 20; + /** + * @var string[] Available object types for reporting. + */ + public static $availableObjectTypes = [ + ClassType::WIKI, + ClassType::EXTENSION, + ClassType::COMMENT, + ]; + /** * @inheritdoc */ @@ -71,7 +81,7 @@ public function attributeLabels() 'status' => 'Status', 'object_type' => 'Object Type', 'object_id' => 'Object ID', - 'content' => 'Content', + 'content' => 'What\'s wrong with it?', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'creator_id' => 'Creator ID', @@ -103,4 +113,28 @@ public static function find() { return new ReportQuery(static::class); } + + /** + * @return Extension|Wiki|Comment|null + */ + public function getObject() + { + $query = null; + switch ($this->object_type) { + case ClassType::WIKI: + $query = Wiki::find()->active(); + break; + case ClassType::EXTENSION: + $query = Extension::find()->active(); + break; + case ClassType::COMMENT: + $query = Comment::find()->active(); + } + + if ($query === null) { + return null; + } + + return $query->where(['id' => $this->object_id])->one(); + } } diff --git a/views/report/create.php b/views/report/create.php new file mode 100644 index 00000000..6210c384 --- /dev/null +++ b/views/report/create.php @@ -0,0 +1,33 @@ +title = 'Create News'; +?> + +
+
+
+
+

You are going to report "getLinkTitle()), $object->getUrl())?>".

+ + + + field($report, 'content')->textarea(['name' => 'content']) ?> + +
+ 'btn btn-primary']) ?> +
+ + + +
+
+
+
From 0526c77cfc8903554f70d7dec03f01fda0e36075 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 14 Sep 2018 02:37:12 +0300 Subject: [PATCH 3/3] Update urls.php --- config/urls.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/config/urls.php b/config/urls.php index dba201f5..901d564f 100755 --- a/config/urls.php +++ b/config/urls.php @@ -80,10 +80,7 @@ // content reporting 'reports/' => 'report/', - - // TODO handle URLs from old site - // /ext/files/?id=864 - 'extensions/page/' => 'extension/index', + 'extension/' => 'extension/view', 'extensions' => 'extension/index', 'extensions/' => 'extension/',