-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
169 lines (151 loc) · 5.03 KB
/
Plugin.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is a part of Mibew Title Notification Plugin.
*
* Copyright 2014 Dmitriy Simushev <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file The main file of Mibew:TitleNotification plugin.
*/
namespace Mibew\Mibew\Plugin\TitleNotification;
use Mibew\EventDispatcher\EventDispatcher;
use Mibew\EventDispatcher\Events;
use Symfony\Component\HttpFoundation\Request;
/**
* The main plugin's file definition.
*
* It only attaches needed CSS and JS files to chat windows.
*/
class Plugin extends \Mibew\Plugin\AbstractPlugin implements \Mibew\Plugin\PluginInterface
{
/**
* List of the plugin configs.
*
* @var array
*/
protected $config;
/**
* Indicates if the plugin was initialized correctly.
*
* @var boolean
*/
protected $initialized = false;
/**
* Class constructor.
*
* @param array $config List of the plugin config. The following options are
* supported:
* - "new_thread": boolean, if set to true window title of the visitors
* awaiting page will be changed when a visitor starts a new thread.
* The default value is true.
* - "new_message": string, indicates in what chat windows the title
* should be changed when a new message is came. The possible values
* are "client", "operator", "both", "none". The default value is "both".
*/
public function __construct($config)
{
$bad_config = isset($config['new_message'])
&& !in_array(
$config['new_message'],
array('client', 'operator', 'both', 'none')
);
if ($bad_config) {
// Config is invalid the plugin cannot be used. Nevertheless
// the system should work well without the plugin.
trigger_error(
'Wrong value of "new_message" configuration parameter',
E_USER_WARNING
);
return;
}
$this->initialized = true;
$this->config = $config + array(
'new_thread' => true,
'new_message' => 'both',
);
}
/**
* The main entry point of a plugin.
*/
public function run()
{
// Attach CSS and JS files of the plugin to chat window.
$dispatcher = EventDispatcher::getInstance();
$dispatcher->attachListener(Events::PAGE_ADD_JS, $this, 'attachJsFiles');
}
/**
* Event handler for "pageAddJS" event.
*
* @param array $args
*/
public function attachJSFiles(&$args)
{
$need_users_plugin = $this->needUsersPlugin($args['request']);
$need_chat_plugin = $this->needChatPlugin($args['request']);
if ($need_users_plugin || $need_chat_plugin) {
$base_path = str_replace(DIRECTORY_SEPARATOR, '/', $this->getFilesPath());
$args['js'][] = $base_path . '/vendor/jquery-titlealert/jquery.titlealert.js';
if ($need_users_plugin) {
$args['js'][] = $base_path . '/js/users_plugin.js';
} else {
$args['js'][] = $base_path . '/js/chat_plugin.js';
}
}
}
/**
* Specify version of the plugin.
*
* @return string Plugin's version.
*/
public static function getVersion()
{
return '1.0.1';
}
/**
* Specify dependencies of the plugin.
*
* @return array List of dependencies
*/
public static function getDependencies()
{
// This plugin does not depend on others so return an empty array.
return array();
}
/**
* Checks if the JS part for users page should be attached.
*
* @param Request $request Incoming request
* @return boolean
*/
protected function needUsersPlugin(Request $request)
{
return $request->attributes->get('_route') == 'users' && $this->config['new_thread'];
}
/**
* Checks if the JS part for chat page should be attached.
*
* @param Request $request Incoming request
* @return boolean
*/
protected function needChatPlugin(Request $request)
{
$route = $request->attributes->get('_route');
$new_message = $this->config['new_message'];
$notify_operator = ($new_message == 'operator') || ($new_message == 'both');
$notify_client = ($new_message == 'client') || ($new_message == 'both');
return ($route == 'chat_operator' && $notify_operator)
|| (in_array($route, array('chat_user', 'chat_user_start')) && $notify_client);
}
}