-
Notifications
You must be signed in to change notification settings - Fork 38
/
Module.php
113 lines (107 loc) · 3.87 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @link https://github.com/bubasuma/yii2-simplechat
* @copyright Copyright (c) 2015 bubasuma
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace bubasuma\simplechat;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\di\Instance;
use yii\web\Application as Web;
use yii\console\Application as Console;
/**
* Module extends [[\yii\base\Module]] and represents a message system that stores
* messages in database.
*
* The database must contain at less the following two tables:
*
* ~~~
*
* CREATE TABLE user (
* id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
* .. ..
* );
*
* CREATE TABLE message (
* id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
* sender_id BIGINT UNSIGNED NOT NULL,
* receiver_id BIGINT UNSIGNED NOT NULL,
* text VARCHAR(1020) NOT NULL
* is_new BOOLEAN DEFAULT TRUE,
* is_deleted_by_sender BOOLEAN DEFAULT FALSE,
* is_deleted_by_receiver BOOLEAN DEFAULT FALSE,
* created_at DATETIME NOT NULL,
* CONSTRAINT fk_message_sender_id FOREIGN KEY (id)
* REFERENCES user (id) ON DELETE NO ACTION ON UPDATE CASCADE,
* CONSTRAINT fk_message_receiver_id FOREIGN KEY (id)
* REFERENCES user (id) ON DELETE NO ACTION ON UPDATE CASCADE,
* );
* ~~~
*
* The `user` table stores users, and the `message` table stores messages
*
* @author Buba Suma <bubasuma@gmail.com>
* @since 1.0
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
/**
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
*/
public $db = 'db';
public $controllerNamespace = 'bubasuma\simplechat\controllers';
/**
* Initializes simplechat module.
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
* @throws InvalidConfigException if [[db]] is invalid.
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
if ($app instanceof Web) {
$app->getUrlManager()->addRules([
'messages/<contactId:\d+>' => $this->id . '/default/index',
'messages' => $this->id . '/default/index',
'login-as/<userId:\d+>' => $this->id . '/default/login-as',
'chat/get/messages/<contactId:\d+>' => $this->id . '/default/messages',
'chat/get/conversations' => $this->id . '/default/conversations',
'chat/delete/message/<id:\d+>' => $this->id . '/default/delete-message',
'chat/delete/conversation/<contactId:\d+>' => $this->id . '/default/delete-conversation',
'chat/post/message/<contactId:\d+>' => $this->id . '/default/create-message',
'chat/unread/conversation/<contactId:\d+>' => $this->id . '/default/mark-conversation-as-unread',
'chat/read/conversation/<contactId:\d+>' => $this->id . '/default/mark-conversation-as-read',
], false);
if (!isset($app->getView()->renderers['twig'])) {
$app->getView()->renderers['twig'] = [
'class' => 'yii\twig\ViewRenderer',
];
}
$app->getView()->renderers['twig']['globals']['html'] = '\yii\helpers\Html';
} elseif ($app instanceof Console) {
$app->controllerMap[$this->id] = [
'class' => 'bubasuma\simplechat\console\DefaultController',
'module' => $this,
];
}
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
$this->db->tablePrefix = $this->id . '_';
return true;
}
}