Commit b9913ac0 authored by Marcello Pivanti's avatar Marcello Pivanti
Browse files

Initial Commit

parents
{
"name": "arter/amos-notify",
"description": "Modulo di gestione notifiche - plugin",
"keywords": [
"amos",
"notifiche",
"yii2"
],
"homepage": "http://www.elitedivision.it/",
"type": "component",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"require": {
"php": ">=5.4.0",
"arter/amos-admin": "^2.2.0",
"arter/amos-core": "^1.11.0",
"arter/amos-email-manager": "^1.1",
"arter/amos-workflow": "^1.0"
},
"config": {
"process-timeout": 1800,
"secure-http": false
},
"autoload": {
"psr-4": {
"elitedivision\\amos\\notificationmanager\\": "src"
}
},
"scripts": {
"post-install-cmd": "php yii migrate --migrationPath=@vendor/elitedivision/amos-notify/migrations --interactive=0"
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\notify
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\notificationmanager;
/**
*
* Plugin per la gestione delle notifiche.
*
* Si basa su una Behavior (NotifyBehavior) che aggiunge funzionalità al modello con gli Events:
*
* ActiveRecord::EVENT_AFTER_INSERT
* ActiveRecord::EVENT_AFTER_UPDATE
*
* quando un record del modello viene inserito o aggiornato, viene aggiunto un record nella coda delle notifica (Notification)
* per ogni canale configurato nella Behavior (default CHANNEL_ALL).
*
* ActiveRecord::EVENT_AFTER_FIND
* ogni volta che viene chiamato questo evento per il modello la Behavior inserisce o aggiorna un record per il canale CHANNEL_READ
* nella coda dei letti (NotificationsRead)
*
* CrudController::AFTER_FINDMODEL_EVENT (evento scatenato dal metodo findModel del CrudController)
* ogni volta che viene chiamato questo evento per il modello la Behavior inserisce o aggiorna un record per il canale CHANNEL_READ_DETAIL
* nella coda dei letti (NotificationsRead)
*
* Canali di notifica disponibili, possono essere configurati tramite il parametro 'channels' della NotifyBehavior es:
*
* 'channels' => [NotificationChannels::CHANNEL_MAIL,NotificationChannels::CHANNEL_IMMEDIATE_MAIL ,NotificationChannels::CHANNEL_ALL]
*
* se presente il valore NotificationChannels::CHANNEL_ALL ha la precedenza inserendo tutti i canali disponibili.
*
* CHANNEL_MAIL -- Canale per la gestione notifiche per mail (TODO)
* CHANNEL_IMMEDIATE_MAIL -- Canale per la gestione notifiche per mail immediata (TODO)
* CHANNEL_UI -- Canale per la gestione notifiche da User Interface (TODO)
* CHANNEL_SMS -- Canale per la gestione notifiche per SMS (TODO)
* CHANNEL_READ -- Canale per la gestione notifiche da ActiveRecord::EVENT_AFTER_FIND
* CHANNEL_READ_DETAIL -- Canale per la gestione notifiche da CrudController::AFTER_FINDMODEL_EVENT
* CHANNEL_FAVOURITES -- Canale per la gestione dei preferiti
* CHANNEL_ALL -- Tutti i Canali
*/
use elitedivision\amos\core\module\AmosModule;
use elitedivision\amos\core\record\Record;
use elitedivision\amos\notificationmanager\base\NotifierRepository;
use elitedivision\amos\notificationmanager\base\NotifyWidget;
use elitedivision\amos\notificationmanager\listeners\NotifyWorkflowListener;
use elitedivision\amos\notificationmanager\models\NotificationChannels;
use elitedivision\amos\notificationmanager\models\NotificationsConfOpt;
use elitedivision\amos\notificationmanager\utility\NotifyUtility;
use raoul2000\workflow\base\SimpleWorkflowBehavior;
use Yii;
use yii\base\Event;
use yii\db\ActiveQuery;
use yii\log\Logger;
/**
* Class AmosNotify
* @package elitedivision\amos\notificationmanager
*/
class AmosNotify extends AmosModule implements \yii\base\BootstrapInterface, NotifyWidget
{
public $batchFromDate; // format 'yyyy-mm-dd'
public $defaultSchedule = NotificationsConfOpt::EMAIL_DAY;
public $confirmEmailNotification = false;
public $enableNotificationContentLanguage = false;
private static $notifyworkflowlistener;
/**
* @var null |string
* [
* 'elitedivision\amos\admin\models\UserProfile' => '@common/mail/notify_validation/validator_user_profile',
* 'elitedivision\amos\news\models\News' => '@common/mail/notify_validation/validator_news',
* ]
*
*/
public $viewPathEmailNotifyValidator = [];
/**
* @var null |string
* [
* 'elitedivision\amos\admin\models\UserProfile' => '@common/mail/notify_validation/validated_user_profile',
* 'elitedivision\amos\news\models\News' => '@common/mail/notify_validation/validated_news',
* ]
*/
public $viewPathEmailNotifyValidated = [];
/**
* @inheritdoc
*/
public $controllerNamespace = 'elitedivision\amos\notificationmanager\controllers';
/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
self::$notifyworkflowlistener = new NotifyWorkflowListener();
parent::__construct($id, $parent, $config);
}
/**
* @inheritdoc
*/
public static function getModuleName()
{
return "notify";
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
\Yii::setAlias('@elitedivision/amos/notificationmanager/commands', __DIR__ . '/commands/');
// initialize the module with the configuration loaded from config.php
\Yii::configure($this, require(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'));
}
/**
* @param \yii\console\Application $app
*/
public function bootstrap($app)
{
if ($app instanceof \yii\console\Application) {
$this->controllerNamespace = 'elitedivision\amos\notificationmanager\commands';
} else {
Event::on(
Record::className(),
SimpleWorkflowBehavior::EVENT_AFTER_CHANGE_STATUS,
[self::$notifyworkflowlistener, 'afterChangeStatus']
);
}
}
/**
* @inheritdoc
*/
protected function getDefaultModels()
{
return [
'ChangeStatusEmail' => __NAMESPACE__ . '\\' . 'models\ChangeStatusEmail',
'Notification' => __NAMESPACE__ . '\\' . 'models\Notification',
'NotificationChannels' => __NAMESPACE__ . '\\' . 'models\NotificationChannels',
'NotificationConf' => __NAMESPACE__ . '\\' . 'models\NotificationConf',
'NotificationsConfOpt' => __NAMESPACE__ . '\\' . 'models\NotificationsConfOpt',
'NotificationconfNetwork' => __NAMESPACE__ . '\\' . 'models\NotificationconfNetwork',
'NotificationsRead' => __NAMESPACE__ . '\\' . 'models\NotificationsRead',
'NotificationSendEmail' => __NAMESPACE__ . '\\' . 'models\NotificationSendEmail',
];
}
/**
* @inheritdoc
*/
public function getWidgetGraphics()
{
return [];
}
/**
* @inheritdoc
*/
public function getWidgetIcons()
{
return [];
}
/**
* @param int $uid
* @param string $class_name
* @param ActiveQuery|null $externalquery
* @param NotificationChannels $channel
*/
public function notificationOff($uid, $class_name, $externalquery = null, $channel)
{
try {
$repository = new NotifierRepository();
$repository->notificationOff($uid, $class_name, $externalquery, $channel);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
}
/**
* @param int $uid
* @param string $class_name
* @param ActiveQuery|null $externalquery
* @param NotificationChannels $channel
*/
public function notificationOn($uid, $class_name, $externalquery = null, $channel)
{
try {
$repository = new NotifierRepository();
$repository->notificationOn($uid, $class_name, $externalquery, $channel);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
}
/**
* @param int $uid
* @param string $class_name
* @param ActiveQuery|null $externalquery
* @return false|int|null|string
*/
public function countNotRead($uid, $class_name, $externalquery = null)
{
$result = 0;
try {
$repository = new NotifierRepository();
$result = $repository->countNotRead($uid, $class_name, $externalquery);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $result;
}
/**
* @param $model
* @param int|null $uid
* @return bool
*/
public function modelIsRead($model, $uid = null)
{
$result = false;
try {
$repository = new NotifierRepository();
$result = $repository->modelIsRead($model, $uid);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $result;
}
/**
* @param string $modelClassName
* @param int $channel
* @param string $type
* @return array|bool
*/
public static function manageNewChannelNotifications($modelClassName, $channel, $type)
{
$retval = false;
try {
/** @var NotificationChannels $notificationChannel */
$notificationChannel = AmosNotify::instance()->createModel('NotificationChannels');
$retval = $notificationChannel->manageNewChannelNotifications($modelClassName, $channel, $type);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $retval;
}
/**
* @param int $uid
* @param string $class_name
* @param int $contentId
* @return bool
*/
public function favouriteOn($uid, $class_name, $contentId)
{
$ok = true;
try {
$repository = new NotifierRepository();
$ok = $repository->favouriteOn($uid, $class_name, $contentId);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $ok;
}
/**
* @param int $uid
* @param string $class_name
* @param int $contentId
* @return bool
*/
public function favouriteOff($uid, $class_name, $contentId)
{
$ok = true;
try {
$repository = new NotifierRepository();
$ok = $repository->favouriteOff($uid, $class_name, $contentId);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $ok;
}
/**
* @param $model
* @param int|null $uid
* @return bool
*/
public function isFavorite($model, $uid = null)
{
$result = false;
try {
//$repository = new NotifierRepository();
//$result = $repository->isFavorite($model, $uid);
} catch (\Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $result;
}
/**
* The method save the notification configuration.
* @param int $userId
* @param int $emailFrequency
* @param int $smsFrequency
* @return bool
*/
public function saveNotificationConf($userId, $emailFrequency = 0, $smsFrequency = 0, $params = [])
{
$notifyUtility = new NotifyUtility();
return $notifyUtility->saveNotificationConf($userId, $emailFrequency, $smsFrequency, $params);
}
/**
* This method set the user default notifications configurations.
* @param int $userId
* @return bool
*/
public function setDefaultNotificationsConfs($userId)
{
$notifyUtility = new NotifyUtility();
return $notifyUtility->setDefaultNotificationsConfs($userId);
}
}
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\notificationmanager\base
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\notificationmanager\base;
use elitedivision\amos\core\user\User;
/**
* Interface Builder
* @package elitedivision\amos\notificationmanager\base
*/
interface Builder
{
/**
* @param array $resultset
* @param User $user
* @return string
*/
public function renderEmail(array $resultset, User $user);
/**
* @param array $resultset
* @return string
*/
public function getSubject(array $resultset);
}
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\notify
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\notificationmanager\base;
use elitedivision\amos\notificationmanager\base\builder\ContentImmediateMailBuilder;
use elitedivision\amos\notificationmanager\base\builder\ContentMailBuilder;
use elitedivision\amos\notificationmanager\base\builder\CustomMailBuilder;
use elitedivision\amos\notificationmanager\base\builder\ValidatedMailBuilder;
use elitedivision\amos\notificationmanager\base\builder\ValidatorsMailBuilder;
use elitedivision\amos\notificationmanager\models\ChangeStatusEmail;
use yii\base\BaseObject;
class BuilderFactory extends BaseObject {
const CONTENT_MAIL_BUILDER = 1;
const VALIDATORS_MAIL_BUILDER = 2;
const VALIDATED_MAIL_BUILDER = 3;
const CUSTOM_MAIL_BUILDER = 4;
const CONTENT_IMMEDIATE_MAIL_BUILDER = 5;
/**
* @param $type
* @param ChangeStatusEmail|null $email
* @param string|null $endStatus
* @return ContentMailBuilder|CustomMailBuilder|ValidatedMailBuilder|ValidatorsMailBuilder|null
*/
public function create($type, $email = null, $endStatus = null){
$obj = null;
switch ($type){
case self::CONTENT_MAIL_BUILDER:
$obj = new ContentMailBuilder();
break;
case self::CONTENT_IMMEDIATE_MAIL_BUILDER:
$obj = new ContentImmediateMailBuilder();
break;
case self::VALIDATORS_MAIL_BUILDER:
$obj = new ValidatorsMailBuilder();
break;
case self::VALIDATED_MAIL_BUILDER:
$obj = new ValidatedMailBuilder();
break;
case self::CUSTOM_MAIL_BUILDER:
$obj = new CustomMailBuilder(['emailConf' => $email, 'endStatus' => $endStatus]);
break;
}
return $obj;
}
}
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\notify
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\notificationmanager\base;
use elitedivision\amos\core\record\Record;
use elitedivision\amos\core\user\AmosUser;
use elitedivision\amos\notificationmanager\AmosNotify;
use elitedivision\amos\notificationmanager\models\Notification;
use elitedivision\amos\notificationmanager\models\NotificationChannels;
use elitedivision\amos\notificationmanager\models\NotificationsRead;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\db\Query;
use yii\log\Logger;
/**
* Class NotifierRepository
* @package elitedivision\amos\notificationmanager\base
*/
class NotifierRepository
{
/**
* Method to count unviewed notification.
*
* @param int|null $uid
* @param string $class_name
* @param null $externalquery
* @return false|int|null|string
*/
public function countNotRead($uid, $class_name, $externalquery = null)
{
$result = 0;
$classObj = new $class_name;
try {
$subquery = new Query();
$subquery
->distinct()
->select('id')
->from(['subquery' => $externalquery]);
$query = new Query();
$query
->distinct()
->select('count(*) as number')
->from(Notification::tableName() . ' a')
->leftJoin(NotificationsRead::tableName() . ' b', 'a.id = b.notification_id and b.user_id = ' . $uid)
->leftJoin($classObj->tableName(), 'a.content_id = ' . $classObj->tableName() . '.id')
->andWhere([
'b.user_id' => null,
'a.channels' => NotificationChannels::CHANNEL_READ,
'a.class_name' => $class_name,
$classObj->tableName() . '.id' => $subquery
]);
$result = $query->scalar();
} catch (Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $result;
}
/**
* Method to verify if model is Read
*
* @param ActiveRecord $model
* @param int|null $uid
* @return boolean
*/
public function modelIsRead($model, $uid = null)
{
$result = 0;
$userId = $uid;
try {
if ($uid === null) {
$userId = Yii::$app->user->identity->profile->id;
}
$query = new Query();
$query
->distinct()
->select('count(*) as number')
->from(Notification::tableName() . ' a')
->innerJoin(
NotificationsRead::tableName() . ' b',
'a.id = b.notification_id and b.user_id = ' . $userId . ' and a.content_id = ' . $model->id
)
->andWhere([
'a.channels' => NotificationChannels::CHANNEL_READ_DETAIL,
'a.class_name' => get_class($model)
]);
$result = $query->scalar();
} catch (Exception $ex) {
Yii::getLogger()->log($ex->getTraceAsString(), Logger::LEVEL_ERROR);
}
return $result > 0;
}
/**
* @param int|null $uid
* @param $class_name
* @param null $externalquery
* @param NotificationChannels $channel
* @return bool
*/
public function notificationOff($uid, $class_name, $externalquery = null, $channel)
{
$allOk = true;
try {
$classObj = new $class_name;
$subquery = new Query();
$subquery->distinct()
->select('id')
->from(['subquery' => $externalquery]);
$query = new Query();
$query
->distinct()
->select('a.id as notification_id')
->from(Notification::tableName() . ' a')
->leftJoin(NotificationsRead::tableName() . ' b', 'a.id = b.notification_id and b.user_id = ' . $uid)
->leftJoin($classObj->tableName(), 'a.content_id = ' . $classObj->tableName() . '.id')
->andWhere([
'b.user_id' => null,
'a.channels' => $channel,
'a.class_name' => $class_name,
$classObj->tableName() . '.id' => $subquery
]);
$result = $query->all();