2015-05-22 12:55:17 +03:00
< ? php
/**
* Created by PhpStorm .
* User : VisioN
* Date : 21.05 . 2015
* Time : 17 : 26
*/
namespace vision\messages\components ;
use Yii ;
use yii\base\Component ;
use vision\messages\models\Messages ;
use vision\messages\exceptions\EceptionMessages ;
use vision\messages\events\MessageEvent ;
class MyMessages extends Component {
const EVENT_SEND = 'sendMessage' ;
const EVENT_STATUS = 'changeStatus' ;
/** @var ActiveRecord */
public $modelUser ;
/** @var string */
public $attributeNameUser = 'username' ;
2015-06-08 10:26:12 +03:00
/** @var string */
public $nameController ;
2015-06-06 17:29:04 +03:00
/** @var boolean */
public $enableEmail = false ;
2015-07-02 20:54:24 +03:00
/** @var boolean */
public $isSystem = false ;
2015-06-06 17:29:04 +03:00
/** @var function */
public $getEmail = null ;
/** @var array */
public $templateEmail = [];
/** @var string */
public $subject = 'Private message' ;
2015-05-22 12:55:17 +03:00
/** @var string */
protected $userTableName ;
public function init (){
if ( ! $this -> modelUser ) {
$this -> modelUser = \Yii :: $app -> user -> identityClass ;
}
$this -> userTableName = call_user_func ( Array ( $this -> modelUser , 'tableName' ));
}
2015-06-06 17:29:04 +03:00
public function sendMessage ( $whom_id , $message , $sendEmail = false ) {
2015-06-02 11:33:40 +03:00
$result = null ;
2015-07-02 20:54:24 +03:00
if ( ! is_numeric ( $whom_id ) && is_string ( $whom_id )){
$ids = $this -> getUsersByRoles ( $whom_id );
return $this -> sendMessage ( $ids , $message , $send_email );
}
2015-06-02 11:33:40 +03:00
if ( is_array ( $whom_id )) {
2015-06-06 17:29:04 +03:00
$result = $this -> _sendMessages ( $whom_id , $message , $sendEmail );
2015-06-02 11:33:40 +03:00
} else {
2015-06-06 17:29:04 +03:00
$result = $this -> _sendMessage ( $whom_id , $message , $sendEmail );
2015-06-02 11:33:40 +03:00
}
return $result ;
}
2015-07-02 20:54:24 +03:00
public function systemSend ( $whom_id , $message , $sendEmail = false ) {
$this -> isSystem = true ;
$this -> sendMessage ( $whom_id , $message , $sendEmail );
$this -> isSystem = false ;
}
2015-06-02 18:03:56 +03:00
/**
* Method to getMyMessages .
*
* @ throws EceptionMessages
* @ return array
*/
public function getMyMessages () {
2015-07-02 20:54:24 +03:00
$id = $this -> getIdCurrentUser ();
return $this -> getMessages ( $id );
2015-06-02 18:03:56 +03:00
}
2015-06-04 18:00:43 +03:00
2015-06-06 17:29:04 +03:00
public function checkMessage (){
$result = $this -> getAllUsers ();
return array_filter ( $result , function ( $arr ) { return $arr [ 'cnt_mess' ] > 0 ;});
2015-06-04 18:00:43 +03:00
}
2015-06-02 18:03:56 +03:00
/**
* Method to getAllMessages .
*
* @ param $whom_id
* @ param $from_id
*
* @ throws EceptionMessages
* @ return array
*/
public function getAllMessages ( $whom_id , $from_id ) {
return $this -> getMessages ( $whom_id , $from_id );
}
2015-06-02 11:33:40 +03:00
2015-06-03 18:04:47 +03:00
public function getNewMessages ( $whom_id , $from_id ) {
return $this -> getMessages ( $whom_id , $from_id , 1 );
}
2015-05-22 12:55:17 +03:00
/**
* Method to sendMessage .
*
* @ param $whom_id
* @ param $message
*
* @ return array
*/
2015-06-06 17:29:04 +03:00
protected function _sendMessage ( $whom_id , $message , $send_email = false ) {
2015-05-22 12:55:17 +03:00
$model = new Messages ();
2015-07-02 20:54:24 +03:00
$model -> from_id = $this -> getIdCurrentUser ();
2015-05-22 12:55:17 +03:00
$model -> whom_id = $whom_id ;
$model -> message = $message ;
2015-06-06 17:29:04 +03:00
if ( $this -> enableEmail && $send_email ) {
$this -> _sendEmail ( $whom_id , $message );
}
2015-06-04 14:47:47 +03:00
2015-05-22 12:55:17 +03:00
return $this -> saveData ( $model , self :: EVENT_SEND );
}
2015-06-06 17:29:04 +03:00
/**
* Method to _sendEmail .
*
* @ param $whom_id
* @ param $message
*
* @ throws EceptionMessages
*
* @ return boolean , array
*/
protected function _sendEmail ( $whom_id , $message ) {
if ( ! is_callable ( $this -> getEmail )) {
throw new EceptionMessages ( 'Email not send. Set in config "getEmail" to callable func.' );
}
if ( ! isset ( $this -> templateEmail [ 'html' ], $this -> templateEmail [ 'text' ])) {
throw new EceptionMessages ( 'Email not send. Set in config "templateEmail".' );
}
$user = $this -> getUser ( $whom_id );
if ( $user ) {
$email = call_user_func ( $this -> getEmail , $user );
}
if ( ! empty ( $email )) {
return \Yii :: $app -> mailer
-> compose ([ 'html' => $this -> templateEmail [ 'html' ], 'text' => $this -> templateEmail [ 'text' ]], [ 'message' => $message ])
-> setFrom ([ \Yii :: $app -> params [ 'supportEmail' ] => \Yii :: $app -> name . ' private Message' ])
-> setTo ( $email )
-> setSubject ( $this -> subject )
-> send ();
}
return false ;
}
/*
public $getEmail = null ;
public $templateEmail = [];
*/
2015-05-22 12:55:17 +03:00
2015-06-02 11:33:40 +03:00
/**
* Method to many messages .
*
* @ param $whom_ids
* @ param $message
*
* @ return array
*/
2015-06-06 17:29:04 +03:00
protected function _sendMessages ( Array $whom_ids , $message , $sendEmail = false ) {
2015-06-02 11:33:40 +03:00
$result = Array ();
foreach ( $whom_ids as $id ) {
2015-07-01 18:02:44 +03:00
$result [] = $this -> _sendMessage ( $id , $message , $sendEmail );
2015-06-02 11:33:40 +03:00
}
return $result ;
}
2015-05-22 12:55:17 +03:00
/**
* Method to getNewMessages .
*
* @ param $whom_id
* @ param $from_id
*
* @ throws EceptionMessages
* @ return array
*/
2015-06-02 11:33:40 +03:00
protected function _getNewMessages ( $whom_id , $from_id ) {
2015-05-22 12:55:17 +03:00
return $this -> getMessages ( $whom_id , $from_id , Messages :: STATUS_NEW );
}
/**
* Method to changeStatusMessage .
*
* @ param $id
* @ param $status
*
* @ throws EceptionMessages
* @ return array
*/
2015-06-05 13:47:34 +03:00
protected function changeStatusMessage ( $id , $status , $is_delete = false ) {
2015-05-22 12:55:17 +03:00
$model = Messages :: findOne ( $id );
2015-06-02 18:03:56 +03:00
$status_name = 'status' ;
2015-07-02 20:54:24 +03:00
$current_user_id = $this -> getIdCurrentUser ();
2015-05-22 12:55:17 +03:00
if ( ! $model ) {
throw new EceptionMessages ( 'Message not found.' );
}
2015-06-02 11:43:54 +03:00
if ( $model -> from_id != $current_user_id && $model -> whom_id != $current_user_id ) {
throw new EceptionMessages ( 'Message not found for this user.' );
}
2015-06-02 18:03:56 +03:00
if ( $is_delete ) {
switch ( $current_user_id ) {
case $model -> from_id :
$status_name = 'is_delete_from' ;
break ;
case $model -> whom_id :
$status_name = 'is_delete_whom' ;
break ;
}
}
$model -> $status_name = $status ;
2015-05-22 12:55:17 +03:00
return $this -> saveData ( $model , self :: EVENT_STATUS );
}
/**
* Method to deleteMessage .
*
* @ param $id
*
* @ throws EceptionMessages
* @ return Messages
*/
public function deleteMessage ( $id ) {
2015-06-02 18:03:56 +03:00
return $this -> changeStatusMessage ( $id , 1 , 1 );
2015-05-22 12:55:17 +03:00
}
/**
2015-06-06 17:29:04 +03:00
* Method to getUser .
2015-05-22 12:55:17 +03:00
*
* @ throws EceptionMessages
2015-06-06 17:29:04 +03:00
* @ return array
*/
public function getUser ( $id ) {
$model = new $this -> modelUser ();
$user = $model :: findOne ( $id );
return $user ;
}
/**
* Method to getAllUsers .
*
* @ throws EceptionMessages
* @ return array
2015-05-22 12:55:17 +03:00
*/
public function getAllUsers () {
2015-06-02 18:03:56 +03:00
$table_name = Messages :: tableName ();
2015-06-04 14:47:47 +03:00
$sql = " select usr.id, usr. $this->attributeNameUser as username, msg.cnt as cnt_mess " ;
$sql .= " from $this->userTableName as usr " ;
$sql .= " left join " ;
$sql .= " (select from_id, count(id) as cnt from $table_name where status = 1 and whom_id = :user_id GROUP by from_id) as msg ON usr.id = msg.from_id " ;
$sql .= " where usr.id != :user_id " ;
$connection = \Yii :: $app -> db ;
$model = $connection -> createCommand ( $sql );
2015-07-02 20:54:24 +03:00
$model -> bindValue ( ':user_id' , $this -> getIdCurrentUser ());
2015-06-04 14:47:47 +03:00
$users = $model -> queryAll ();
2015-05-22 12:55:17 +03:00
return $users ;
}
/**
* Method to saveData .
*
* @ param $model
* @ param $name_event
*
* @ throws EceptionMessages
* @ return array
*/
protected function saveData ( $model , $name_event = null ) {
if ( ! $model -> save ()) {
2015-07-02 20:54:24 +03:00
$mess = $model -> hasErrors () ? implode ( ', ' , $model -> getFirstErrors ()) : 'Not saved. ' . $name_event ;
2015-05-22 12:55:17 +03:00
throw new EceptionMessages ( $mess );
} else {
if ( $name_event ) {
$event = new MessageEvent ;
$event -> message = $model ;
$this -> trigger ( self :: EVENT_SEND , $event );
}
}
return $model -> toArray ();
}
/**
* Method to getMessages .
*
* @ param $whom_id
* @ param $from_id
* @ param $type
*
* @ throws EceptionMessages
* @ return array
*/
2015-06-02 11:33:40 +03:00
protected function getMessages ( $whom_id , $from_id = null , $type = null , $last_id = null ) {
2015-05-22 12:55:17 +03:00
$table_name = Messages :: tableName ();
2015-07-02 20:54:24 +03:00
$my_id = $this -> getIdCurrentUser ();
2015-05-22 12:55:17 +03:00
$query = new \yii\db\Query ();
$query
2015-06-05 13:47:34 +03:00
-> select ([ 'FROM_UNIXTIME(msg.created_at, "%d-%m-%Y %H:%i:%S") as created_at' , 'msg.id' , 'msg.status' , 'msg.message' , " usr1.id as from_id " , " usr1. $this->attributeNameUser as from_name " , " usr2.id as whom_id " , " usr2. $this->attributeNameUser as whom_name " ])
2015-05-22 12:55:17 +03:00
-> from ( " $table_name as msg " )
-> leftJoin ( " $this->userTableName as usr1 " , 'usr1.id = msg.from_id' )
2015-06-04 18:00:43 +03:00
-> leftJoin ( " $this->userTableName as usr2 " , 'usr2.id = msg.whom_id' );
if ( $from_id ) {
$query
-> where ([ 'msg.whom_id' => $whom_id , 'msg.from_id' => $from_id ])
-> orWhere ([ 'msg.from_id' => $whom_id , 'msg.whom_id' => $from_id ]);
} else {
$query -> where ([ 'msg.whom_id' => $whom_id ]);
}
2015-06-02 11:33:40 +03:00
2015-06-05 13:47:34 +03:00
//if not set type
//send all message where no delete
2015-05-22 12:55:17 +03:00
if ( $type ) {
$query -> andWhere ([ '=' , 'msg.status' , $type ]);
2015-06-05 13:47:34 +03:00
} else {
$query -> andWhere ( '((msg.is_delete_from != 1 AND from_id = :my_id) OR (msg.is_delete_whom != 1 AND whom_id = :my_id) ) ' , [
':my_id' => $my_id ,
]);
2015-05-22 12:55:17 +03:00
}
2015-06-02 11:33:40 +03:00
if ( $last_id ){
$query -> andWhere ([ '>' , 'msg.id' , $last_id ]);
}
2015-06-03 18:04:47 +03:00
$return = $query -> orderBy ( 'msg.id' ) -> all ();
2015-05-22 12:55:17 +03:00
$ids = Array ();
2015-06-03 18:04:47 +03:00
foreach ( $return as $m ) {
2015-06-05 13:47:34 +03:00
if ( $m [ 'whom_id' ] == $my_id ) {
2015-06-04 18:00:43 +03:00
$ids [] = $m [ 'id' ];
}
2015-05-22 12:55:17 +03:00
}
//change status to is_read
2015-06-04 18:00:43 +03:00
if ( count ( $ids ) > 0 ) {
2015-05-22 12:55:17 +03:00
Messages :: updateAll ([ 'status' => Messages :: STATUS_READ ], [ 'in' , 'id' , $ids ]);
}
2015-07-02 20:54:24 +03:00
$user_id = $this -> getIdCurrentUser ();
2015-06-03 18:04:47 +03:00
return array_map ( function ( $r ) use ( $user_id ) { $r [ 'i_am_sender' ] = $r [ 'from_id' ] == $user_id ; return $r ;}, $return );
2015-05-22 12:55:17 +03:00
}
2015-07-01 18:02:44 +03:00
protected function getUsersByRoles ( $role ) {
$users = new \yii\db\Query ();
2015-07-02 20:54:24 +03:00
$result = $users
2015-07-01 18:02:44 +03:00
-> select ([
'usr.id'
])
-> from ( " $this->userTableName as usr " )
-> leftJoin ( 'auth_assignment as ath' , 'usr.id = ath.user_id' )
-> where ([ 'ath.item_name' => $role ])
-> all ();
2015-07-02 20:54:24 +03:00
return array_map ( function ( $r ) { return $r [ 'id' ];}, $result );
}
protected function getIdCurrentUser () {
return \Yii :: $app -> user -> isGuest || $this -> isSystem ? null : \Yii :: $app -> user -> id ;
2015-07-01 18:02:44 +03:00
}
2015-06-02 18:03:56 +03:00
}