Commit 9141e3d5 authored by Marcello Pivanti's avatar Marcello Pivanti
Browse files

Initial Commit

parents
#Amos Report
Extension for report sending on contents like news, discussions, etc...
Installation
------------
1. The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
composer require elitedivision/amos-report
```
or add this row
```
"elitedivision/amos-report": "dev-master"
```
to the require section of your `composer.json` file.
2. Add module to your main config in backend:
```php
'modules' => [
'comments' => [
'class' => 'elitedivision\amos\report\AmosReport',
'modelsEnabled' => [
/**
* Add here the classnames of the models for whose you want to enable reports
* (i.e. 'elitedivision\amos\news\models\News')
*/
]
],
],
```
3. To send report notification not only to the content creator but also to the content validator, installation of amos-workflow is needed too
a. Add workflow to composer
```
"elitedivision/amos-workflow": "dev-master"
```
b. check in config/main for 'workflow' in modules array, if present
```php
'workflow' => [
'class' => 'cornernote\workflow\manager\Module',
],
```
change the entry in:
```php
'workflow-manager' => [
'class' => 'cornernote\workflow\manager\Module',
],
```
c. add workflow entry (config/main in modules array):
```php
'workflow' => [
'class' => 'elitedivision\amos\workflow\AmosWorkflow',
],
```
d. add 'workflow' entry to your bootstrap:
```php
'bootstrap' => [
.
.
.
'workflow',
.
.
.
],
```
4. Apply migrations
a. amos-report migrations
```bash
php yii migrate/up --migrationPath=@vendor/elitedivision/amos-report/src/migrations
```
or add this row to your migrations config in console:
```php
return [
.
.
.
'@vendor/elitedivision/amos-report/src/migrations',
.
.
.
];
```
b. if workflow module is installed (see step 3), also add amos-workflow migrations:
```bash
php yii migrate/up --migrationPath=@vendor/elitedivision/amos-workflow/src/migrations
```
or add this row to your migrations config in console:
```php
return [
.
.
.
'@vendor/elitedivision/amos-workflow/src/migrations',
.
.
.
];
```
Widgets
-----------
Amos Report provides two Widgets:
* **ReportWidget** *elitedivision\amos\report\widgets\ReportWidget*
Draw a flag icon related to a model. On flag click, the system opens form to insert a new report on a modal popup.
* **TabReportsWidget** *elitedivision\amos\report\widgets\TabReportsWidget*
Draw the Report tab in a model view/form, containing the list of reports a specif content.
If a model has been enabled for reports, the tab is automatically injected in update phase (form) by AmosCore widget 'Tabs'.
Email Sending
-----------
After the creation of a new report on a content a mail is sent to:
* Content creator
* Content validator (if exixts)
* Users having the REPORT_MONITOR role for that content type (if role exists)
The role name must follow the general permission naming convention <MODELNAME>_REPORT_MONITOR as for eg. create and update permissions (MODELNAME_CREATE, MODELNAME_UPDATE).
\ No newline at end of file
# Deploy changes to elite division gitlab
pipelines:
branches:
master:
- step:
script:
- git push --all git@git.elitedivision.it:mirror/amos-report.git
\ No newline at end of file
{
"name": "arter/amos-report",
"description": "Sistema AMOS per le segnalazioni",
"keywords": [
"amos",
"yii2",
"report"
],
"homepage": "http://www.elitedivision.it/",
"type": "component",
"license": "BSD-3-Clause",
"require": {
"php": ">=5.4.0",
"2amigos/yii2-ckeditor-widget": "~1.0",
"cornernote/yii2-workflow-manager": "*",
"arter/amos-admin": "~2.0",
"arter/amos-core": "^1.9.50",
"arter/amos-notify": "~1.4",
"raoul2000/yii2-workflow": "*",
"raoul2000/yii2-workflow-view": "*",
"yiidoc/yii2-redactor": "*"
},
"config": {
"process-timeout": 1800,
"secure-http": false
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
},
"autoload": {
"psr-4": {
"elitedivision\\amos\\report\\": "src"
}
},
"require-dev": {
"yiisoft/yii2-faker": "*"
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\report
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\report;
use elitedivision\amos\core\module\AmosModule;
use elitedivision\amos\core\module\ModuleInterface;
use elitedivision\amos\notificationmanager\models\Notification;
use elitedivision\amos\notificationmanager\models\NotificationsRead;
use elitedivision\amos\notificationmanager\models\NotificationChannels;
use elitedivision\amos\report\models\Report;
use Yii;
use yii\db\ActiveQuery;
/**
* Class AmosReport
* @package elitedivision\amos\report
*/
class AmosReport extends AmosModule implements ModuleInterface
{
public static $CONFIG_FOLDER = 'config';
/**
* @var string|boolean the layout that should be applied for views within this module. This refers to a view name
* relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
* will be taken. If this is false, layout will be disabled within this module.
*/
public $layout = 'main';
public $name = 'Report';
public $controllerNamespace = 'elitedivision\amos\report\controllers';
/**
* @var array
*/
public $modelsEnabled = [
];
/**
* This is the html used to render the subject of the e-mail.
* @var string
*/
public $htmlMailSubject = '@vendor/elitedivision/amos-report/src/views/report/email/report_notification_subject';
/**
* This is the html used to render the message of the e-mail.
* @var string
*/
public $htmlMailContent = '@vendor/elitedivision/amos-report/src/views/report/email/report_notification';
public static function getModuleName()
{
return "report";
}
public function init()
{
parent::init();
\Yii::setAlias('@elitedivision/amos/'.static::getModuleName().'/controllers', __DIR__.'/controllers');
// initialize the module with the configuration loaded from config.php
Yii::configure($this, require(__DIR__.DIRECTORY_SEPARATOR.self::$CONFIG_FOLDER.DIRECTORY_SEPARATOR.'config.php'));
}
public function getWidgetIcons()
{
return [
];
}
public function getWidgetGraphics()
{
return [
];
}
/**
* Get default model classes
*/
protected function getDefaultModels()
{
return [
'Report' => __NAMESPACE__.'\\'.'models\Report',
'ReportType' => __NAMESPACE__.'\\'.'models\ReportType',
'ReportSearch' => __NAMESPACE__.'\\'.'models\ReportSearch',
];
}
/**
* query for all unread reports sent to user
*
* @param null|integer $userId - if null logged user is considered
* @return ActiveQuery $query of unread report sent to $userId
*
*/
public function getOwnUnreadReports($userId = null)
{
if (empty($userId)) {
$userId = \Yii::$app->user->id;
}
$notificationTable = Notification::tableName();
$notificationReadTable = NotificationsRead::tableName();
$query = Report::find()->andWhere('report.creator_id = '.$userId.' OR report.validator_id = '.$userId);
$query->leftJoin($notificationTable,
$notificationTable.".class_name = '".Report::className()."' AND ".$notificationTable.".content_id = report.id AND notification.channels = '".NotificationChannels::CHANNEL_READ."'");
$query->leftJoin($notificationReadTable,
$notificationReadTable.'.notification_id = '.$notificationTable.'.id AND notificationread.user_id = '.$userId);
$query->andWhere('report.deleted_at is NULL');
$query->andWhere($notificationReadTable.'.user_id is null OR '.$notificationReadTable.'.user_id <> '.$userId);
return $query;
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package amos-report
* @category CategoryName
* @author Elite Division S.r.l.
*/
return [];
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\news\controllers
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\report\controllers;
use elitedivision\amos\dashboard\controllers\base\DashboardController;
use yii\helpers\Url;
class DefaultController extends DashboardController
{
/**
* @var string $layout Layout per la dashboard interna.
*/
public $layout = 'dashboard_interna';
/**
* @inheritdoc
*/
public function init() {
parent::init();
$this->setUpLayout();
// custom initialization code goes here
}
/**
* @return string|\yii\web\Response
*/
public function actionIndex()
{
return $this->redirect(['/report/report/index']);
Url::remember();
$params = [
'currentDashboard' => $this->getCurrentDashboard()
];
return $this->render('index', $params);
}
/**
* @param null $layout
* @return bool
*/
public function setUpLayout($layout = null){
if ($layout === false){
$this->layout = false;
return true;
}
$module = \Yii::$app->getModule('layout');
if(empty($module)){
$this->layout = '@vendor/elitedivision/amos-core/views/layouts/' . (!empty($layout) ? $layout : $this->layout);
return true;
}
$this->layout = (!empty($layout)) ? $layout : $this->layout;
return true;
}
}
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\report\controllers
* @category Controller
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\report\controllers;
use elitedivision\amos\core\controllers\CrudController;
use elitedivision\amos\core\helpers\Html;
use elitedivision\amos\core\icons\AmosIcons;
use elitedivision\amos\core\user\User;
use elitedivision\amos\core\utilities\Email;
use elitedivision\amos\notificationmanager\models\NotificationsRead;
use elitedivision\amos\report\AmosReport;
use elitedivision\amos\report\models\Report;
use elitedivision\amos\report\models\search\ReportSearch;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
* Class ReportController
* @package elitedivision\amos\report\controllers
*/
class ReportController extends CrudController
{
/**
* @var string $layout
*/
public $layout = 'list';
/**
* @inheritdoc
*/
public function init()
{
$this->setModelObj(new Report());
$this->setModelSearch(new ReportSearch());
$this->setAvailableViews([
'grid' => [
'name' => 'grid',
'label' => AmosReport::t('amosreport', '{iconaTabella}' . Html::tag('p', AmosReport::t('amosreport', 'Tabella')), [
'iconaTabella' => AmosIcons::show('view-list-alt')
]),
'url' => '?currentView=grid'
]
]);
parent::init();
$this->setUpLayout();
}
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = ArrayHelper::merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => [
'index',
'read-confirmation'
],
'roles' => ['REPORT_ADMINISTRATOR', 'REPORT_CONTRIBUTOR', 'REPORT_MODERATOR']
],
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post', 'get']
]
]
]);
return $behaviors;
}
/**
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function actionCreate()
{
$this->setUpLayout('form');
$model = new Report;
if (Yii::$app->request->isAjax) {
if ($model->load(Yii::$app->request->post())) {
if ($model->validate()) {
if ($model->save()) {
$this->sendReportNotification($model);
$message = '<p>' . AmosReport::t("amosreport", "Your report has been correctly sent.") . '</p>' .
'<div class="form-group"><div class="bk-btnFormContainer">' .
Html::button(AmosReport::t('amosreport', 'Close'), ['class' => 'btn btn-secondary pull-right', 'data-dismiss' => 'modal']) . '<br/>' .
'</div></div>';
$retVal = [
'success' => 1,
'message' => $message
];
return json_encode($retVal);
}
} else {
return json_encode([]);
}
} else {
$message = '<p>' . AmosReport::t("amosreport", "Error occured while creating the report. Please, try again later.") . '</p>' .
'<div class="form-group"><div class="bk-btnFormContainer">' .
Html::button(AmosReport::t('amosreport', 'Close'), ['class' => 'btn btn-secondary pull-right', 'data-dismiss' => 'modal']) . '<br/>' .
'</div></div>';
$retVal = [
'success' => 0,
'message' => $message
];
return json_encode($retVal);
}
}
}
/**
* @param null $id
* @return \yii\web\Response
* @throws \yii\base\InvalidConfigException
* @throws \yii\web\NotFoundHttpException
*/
public function actionReadConfirmation($id = null)
{
if (Yii::$app->request->isAjax) {
$post = Yii::$app->request->post();
if (!empty($post['report_id'])) {
$id = $post['report_id'];
$model = $this->findModel($id);
$read_by = \Yii::$app->user->id;
$ok = true;
if (!empty($post['notification_id'])) {
$notificationRead = new \elitedivision\amos\notificationmanager\models\NotificationsRead();
$notificationRead->user_id = $read_by;
$notificationRead->notification_id = $post['notification_id'];
$ok = $notificationRead->save(false);
}
$model->read_at = date('Y-m-d H:i:s');
$model->read_by = $read_by;
$model->status = 1;
$ok = $ok && $model->save();
if ($ok) {
$message = '<p>' . AmosReport::t("amosreport", "The report has been correctly updated.") . '</p>' .
'<div class="form-group"><div class="bk-btnFormContainer">' .
Html::button(AmosReport::t('amosreport', 'Close'), ['class' => 'btn btn-secondary pull-right', 'data-dismiss' => 'modal']) . '<br/>' .
'</div></div>';
$retVal = [
'success' => 1,
'message' => $message
];
return json_encode($retVal);
}
}
} else {
$ok = false;
if (!empty($id)) {
$model = $this->findModel($id);
$read_by = \Yii::$app->user->id;
$ok = true;
$notification = \elitedivision\amos\notificationmanager\models\Notification::findOne([
'class_name' => Report::className(),
'content_id' => $model->id,
]);
if (!empty($notification)) {
$notificationRead = new NotificationsRead();
$notificationRead->user_id = $read_by;
$notificationRead->notification_id = $notification->id;
$ok = $notificationRead->save(false);
}
$model->read_at = date('Y-m-d H:i:s');
$model->read_by = $read_by;
$model->status = 1;
$ok = $ok && $model->save();
}
if ($ok) {
Yii::$app->getSession()->addFlash('success', AmosReport::t('amosreport', 'The report has been correctly updated.'));
} else {
Yii::$app->getSession()->addFlash('danger', AmosReport::t('amosreport', 'Error occurred while updating the report'));
}
return $this->redirect(Url::previous());
}
}
/**
* Used for set page title and breadcrumbs.
* @param string $reportTitle News page title (ie. Created by , ...)
*/
private function setTitleAndBreadcrumbs($reportTitle)
{
Yii::$app->session->set('previousTitle', $reportTitle);
Yii::$app->session->set('previousUrl', Url::previous());
Yii::$app->view->title = $reportTitle;
Yii::$app->view->params['breadcrumbs'][] = ['label' => $reportTitle];
}
/**
* Set a view param used in \elitedivision\amos\core\forms\CreateNewButtonWidget
*/
private function setCreateNewBtnLabel()
{
Yii::$app->view->params['createNewBtnParams'] = [
'createNewBtnLabel' => 'Add report'
];
}
/**
* Action for search all news.
*
* @return string
*/