Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content reporting #309

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions config/urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
'extension/<vendorName:[\w\-\.]+>/<name:[\w\-\.]+>/files/<filename>' => 'extension/download',
'extension/<name:[A-z][A-z0-9\-]*>/files/<filename>' => 'extension/download',
'extension/<vendorName:[\w\-\.]+>/<name:[\w\-\.]+>' => 'extension/view',

// content reporting

'reports/<action:[\w-]+>' => 'report/<action>',

'extension/<name:[A-z][A-z0-9\-]*>' => 'extension/view',
'extensions' => 'extension/index',
'extensions/<action:[\w-]+>' => 'extension/<action>',
Expand Down
73 changes: 73 additions & 0 deletions controllers/ReportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php


namespace app\controllers;


use app\models\Report;
use app\models\ReportContentForm;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\BadRequestHttpException;

class ReportController extends BaseController
{
public $sectionTitle = 'Content reports';
public $headTitle = 'Content reports';

/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'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,
]);
}
}
29 changes: 29 additions & 0 deletions migrations/m180327_221657_add_report_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use app\migrations\BaseMigration;

class m180327_221657_add_report_table extends BaseMigration
{
public function up()
{
$this->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}}');
}
}
18 changes: 18 additions & 0 deletions models/CommentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
140 changes: 140 additions & 0 deletions models/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace app\models;

use app\components\object\ClassType;
use yii\behaviors\BlameableBehavior;

/**
* This is the model class for table "{{%report}}".
*
* @property int $id
* @property int $status
* @property string $object_type
* @property int $object_id
* @property string $content
* @property string $created_at
* @property string $updated_at
* @property int $creator_id
* @property int $updater_id
*
* @property User $updater
* @property User $creator
*/
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
*/
public static function tableName()
{
return '{{%report}}';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['status', 'object_id'], 'integer'],
[['object_type', 'object_id', 'content'], 'required'],
[['content'], 'string'],
[['object_type'], 'string', 'max' => 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' => 'What\'s wrong with it?',
'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);
}

/**
* @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();
}
}
45 changes: 45 additions & 0 deletions models/ReportQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace app\models;

/**
* This is the ActiveQuery class for [[Report]].
*
* @see Report
*/
class ReportQuery extends \yii\db\ActiveQuery
{
/**
* @return $this
*/
public function open()
{
return $this->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);
}
}
33 changes: 33 additions & 0 deletions views/report/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/* @var $report \app\models\Report */
/* @var $object \app\models\Linkable */

use yii\bootstrap\ActiveForm;
use yii\bootstrap\Html;

/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */

$this->title = 'Create News';
?>

<div class="container">
<div class="row">
<div class="content">
<div class="report-create">
<p>You are going to report "<?= Html::a(Html::encode($object->getLinkTitle()), $object->getUrl())?>".</p>

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($report, 'content')->textarea(['name' => 'content']) ?>

<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
</div>
</div>
</div>