Cleaned up code via coder.module.
[project/mebeam_chat.git] / mebeam_chat.module
1 <?php
2 // $Id$
3
4 /**
5 * @file: mebeam_chat.module
6 * Adds a MeBeam audio/Video/Text chat page to your Drupal Site.
7 * @author: Gary Varnell <gary@dgswa.com>
8 * @copyright: 2009 DGS
9 */
10
11
12 #================================================================================
13 define('MEBEAM_CHAT_DEBUG', 1);
14
15 /**
16 * Implementation of hook_help
17 */
18 function mebeam_chat_help($section) {
19 switch ($section) {
20 case 'admin/help#mebeam_chat':
21 $output = '<p>'. t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.') .'</p>';
22 return $output;
23 break;
24 case 'admin/modules#description':
25 return t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.');
26 break;
27 }
28 }
29 //-----------------------------------------
30
31 /**
32 * Implementation of hook_perm
33 */
34 function mebeam_chat_perm() {
35 return array('view_mebeam_chat');
36 }
37 //-----------------------------------------
38
39
40
41 /**
42 * Implementation of hook_menu().
43 */
44 function mebeam_chat_menu($may_cache) {
45 // menu_rebuild();
46
47
48 if (!$may_cache) {
49 # non-cachable menus
50
51
52 }
53 else{
54 # cachable menu items
55 # admin menus
56 $items[] = array( 'path' => 'admin/settings/mebeam-chat',
57 'title' => t('MeBeam Chat Settings'),
58 'description' => t('Configure MeBeam Chat Settings.'),
59 'access' => user_access('administer nodes'),
60 'callback' => 'mebeam_chat_show_settings_form',
61 'type' => MENU_NORMAL_ITEM,
62 );
63 # user menus
64 $items[] = array( 'path' => 'mebeam-chat',
65 'title' => t('MeBeam Chat'),
66 'callback' => 'mebeam_chat_view_chat',
67 'access' => user_access('view_mebeam_chat'),
68 'type' => MENU_NORMAL_ITEM,
69 );
70 }
71 return $items;
72 }
73 //-----------------------------------------
74
75
76 /**
77 * Implementation of hook_cron
78 */
79 function mebeam_chat_cron() {
80 $mebeam_chat_rm_name = variable_get('mebeam_chat_rm_name', '');
81 if (variable_get('mebeam_chat_rm_name_random', 0)) {
82 $mebeam_chat_rm_name = mebeam_chat_randomize_name($mebeam_chat_rm_name);
83 variable_set('mebeam_chat_rm_rname', $mebeam_chat_rm_name);
84 }
85 }
86 //-----------------------------------------
87
88 function mebeam_chat_randomize_name($name) {
89 # generate a unique room name
90 $id = uniqid();
91 $id = md5($id);
92 $name = "$name-$id";
93 $name = substr($name, 0, 15);
94 return $name;
95 }
96 //-----------------------------------------
97
98 function mebeam_chat_view_chat() {
99 $default = mebeam_chat_get_defaults();
100 $pg_title = variable_get('mebeam_chat_pg_title', $default['pg_title']);
101 if (!empty($pg_title)) {
102 drupal_set_title($pg_title);
103 }
104
105 $pg_header = variable_get('mebeam_chat_pg_header', $default['pg_header']);
106 $pg_footer = variable_get('mebeam_chat_pg_footer', $default['pg_footer']);
107
108 $rm_name = variable_get('mebeam_chat_rm_name', $default['rm_name']);
109 if (variable_get('mebeam_chat_rm_name_random', 0)) {
110 $rm_name = variable_get('mebeam_chat_rm_rname', substr(time(),0,10));
111 }
112 # clean up room name
113 $rm_name = preg_replace('|[^\d\w-]|', '', $rm_name);
114 $rm_name = substr($rm_name, 0, 15);
115 $rm_show = variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']);
116 if ($rm_show) {
117 $rm_show = "<div class='room_name'>$rm_name</div>";
118 }
119 else{
120 $rm_show = '';
121 }
122 $rm_width = variable_get('mebeam_chat_rm_width', $default['rm_width']);
123 $rm_height = variable_get('mebeam_chat_rm_height', $default['rm_height']);
124
125 $output = <<<EOF
126 $pg_header
127 <center>
128 <div id='mebeam-chat'>
129 $rm_show
130 <center>
131 <div id='hotroom'>
132 <object width='$rm_width' height='$rm_height' id='uhzy' align='middle'>
133 <param name='allowScriptAccess' value='never' >
134 <param name='movie' value='http://www.mebeam.com/mebeam.swf'>
135 <param name='quality' value='best' >
136 <param name='scale' value='noscale' >
137 <param name='salign' value='t' >
138 <param name='flashvars' value='room=$rm_name'>
139 <embed flashvars='room=$rm_name' src='http://www.mebeam.com/mebeam.swf'
140 quality='best' scale='noscale' salign='t'
141 width='$rm_width' height='$rm_height' name='uhzy' align='middle'
142 allowScriptAccess='never'
143 type='application/x-shockwave-flash'
144 pluginspage='http://www.macromedia.com/go/getflashplayer' >
145 </object>
146 </center>
147 </div>
148 $pg_footer
149
150 EOF;
151
152 return $output;
153 }
154
155 function mebeam_chat_get_defaults() {
156 #so we don't have defaults spread around - user might not goto module settings
157 $default['rm_name'] = variable_get('site_name', 'undefined') .'-chat';
158 $default['pg_title'] = variable_get('site_name', '') .'Live Chat';
159 $default['pg_header'] = t("Provided by <a href=\"http://www.mebeam.com\" target=\"_blank\">MeBeam.com</a>.<br/>
160 The site owner has no control over who enters this chat room.");
161 $default['pg_footer'] = t("Mouse over your image to control your audio and video settings<br/>
162 Mouse over a user and click Mute or Fade to ignore that user.");
163 $default['rm_name_random'] = FALSE;
164 $default['rm_show_room'] = TRUE;
165 $default['rm_width'] = 640;
166 $default['rm_height'] = 540;
167
168 return $default;
169 }
170 //-----------------------------------------
171 // Administration form
172 //-----------------------------------------
173 function mebeam_chat_show_settings_form() {
174 return drupal_get_form('mebeam_chat_settings_form');
175 }
176
177 function mebeam_chat_settings_form() {
178 $default = mebeam_chat_get_defaults();
179 $rname = variable_get('mebeam_chat_rm_rname', $default['rm_name']);
180
181 # Certification Request User Messages
182 $form['page'] = array(
183 '#title' => t('Page Options'),
184 '#type' => 'fieldset',
185 '#collapsible' => TRUE,
186 '#collapsed' => FALSE,
187 );
188
189 $form['page']['mebeam_chat_pg_title'] = array(
190 '#type' => 'textfield',
191 '#title' => t('Page Title'),
192 '#default_value' => variable_get('mebeam_chat_pg_title', $default['pg_title']),
193 '#description' => t("Title for your chat page. Leave blank to use the menu title"),
194 );
195
196 $form['page']['mebeam_chat_pg_header'] = array(
197 '#type' => 'textarea',
198 '#title' => t('Page Header'),
199 '#default_value' => variable_get('mebeam_chat_pg_header', $default['pg_header']),
200 '#description' => t("Title for your chat page. Leave blank for no header"),
201 );
202
203 $form['page']['mebeam_chat_pg_footer'] = array(
204 '#type' => 'textarea',
205 '#title' => t('Page Footer'),
206 '#default_value' => variable_get('mebeam_chat_pg_footer', $default['pg_footer']),
207 '#description' => t("Footer for your chat page. Leave blank for no footer"),
208 );
209
210 # widget options
211 $form['widget'] = array(
212 '#title' => t('MeBeam Widget Options'),
213 '#type' => 'fieldset',
214 '#collapsible' => TRUE,
215 '#collapsed' => FALSE,
216 );
217 $form['widget']['mebeam_chat_rm_name'] = array(
218 '#type' => 'textfield',
219 '#title' => t('MeBeam Room Name'),
220 '#default_value' => variable_get('mebeam_chat_rm_name', $default['rm_name']),
221 '#description' => t("The name of your chat room - must be between 4 and 16 characters.
222 Also, if you do not have randomize turned on you should use a dash in your room name. ex. chatroom-01 or chatroom-b"),
223 );
224
225 $form['widget']['mebeam_chat_rm_name_random'] = array(
226 '#type' => 'checkbox',
227 '#title' => t('Randomize'),
228 '#default_value' => variable_get('mebeam_chat_rm_name_random', $default['rm_name_random']),
229 '#description' => t("Randomize the room name - Randomly generates a new room name everytime cron.php is run. This helps keep your room somewhat private.
230 A dash and the randomized string will be added to your room name, and the entire string will be truncated to 16 characters. For a completely random room name, leave the room name blank and select the Randomize option.
231 <br/>%rname", array('%rname' => $rname)),
232 );
233 $form['widget']['mebeam_chat_rm_show_room'] = array(
234 '#type' => 'checkbox',
235 '#title' => t('Show Room Name'),
236 '#default_value' => variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']),
237 '#description' => t("Show the room name above the chat widget"),
238 );
239 $form['widget']['mebeam_chat_rm_width'] = array(
240 '#type' => 'textfield',
241 '#title' => t('MeBeam widget width'),
242 '#default_value' => variable_get('mebeam_chat_rm_width', $default['rm_width']),
243 '#description' => t("The width of the mebeam widget on your page in pixels"),
244 );
245 $form['widget']['mebeam_chat_rm_height'] = array(
246 '#type' => 'textfield',
247 '#title' => t('MeBeam widget width'),
248 '#default_value' => variable_get('mebeam_chat_rm_height', $default['rm_height']),
249 '#description' => t("The height of the mebeam widget on your page in pixels"),
250 );
251
252 $form['submit'] = array(
253 '#type' => 'submit',
254 '#value' => t('Save Settings'),
255 );
256 return $form;
257 }
258 //-----------------------------------------
259
260 function mebeam_chat_settings_form_validate($form_id, $form_values) {
261 if (strlen($form_values['mebeam_chat_rm_name']) >= 16) {
262 form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be less than 16 characters."));
263 }
264 if (strlen($form_values['mebeam_chat_rm_name']) <= 4) {
265 form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be more than 4 characters."));
266 }
267 if (!is_numeric($form_values['mebeam_chat_rm_width'])) {
268 form_set_error('mebeam_chat_rm_width', t("MeBeam widget width must be numeric."));
269 }
270 if (!is_numeric($form_values['mebeam_chat_rm_height'])) {
271 form_set_error('mebeam_chat_rm_height', t("MeBeam widget height must be numeric."));
272 }
273 }
274 //---------------------------------------------------------
275
276 function mebeam_chat_settings_form_submit($form_id, $form_values) {
277 variable_set('mebeam_chat_pg_title', $form_values['mebeam_chat_pg_title']);
278 variable_set('mebeam_chat_pg_header', $form_values['mebeam_chat_pg_header']);
279 variable_set('mebeam_chat_pg_footer', $form_values['mebeam_chat_pg_footer']);
280
281 variable_set('mebeam_chat_rm_name', $form_values['mebeam_chat_rm_name']);
282 if ($form_values['mebeam_chat_rm_name_random']) {
283 $rname = mebeam_chat_randomize_name($form_values['mebeam_chat_rm_name']);
284 variable_set('mebeam_chat_rm_rname', $rname);
285 }
286 else{
287 variable_set('mebeam_chat_rm_rname', '');
288 }
289 variable_set('mebeam_chat_rm_name_random', $form_values['mebeam_chat_rm_name_random']);
290 variable_set('mebeam_chat_rm_show_room', $form_values['mebeam_chat_rm_show_room']);
291 variable_set('mebeam_chat_rm_width', $form_values['mebeam_chat_rm_width']);
292 variable_set('mebeam_chat_rm_height', $form_values['mebeam_chat_rm_height']);
293
294 $msg = t("Form options saved successfully.");
295 if ($form_values['mebeam_chat_rm_name_random']) {
296 $msg .= t('Your random room name is currently %rname and will be updated automatically the next time cron runs', array('%rname' => $rname));
297 }
298 drupal_set_message($msg, 'status');
299 }
300 //---------------------------------------------------------
301
302 ###########################################################################
303 # Helper Functions
304
305
306 function mebeam_chat_debug($msg) {
307 if (MEBEAM_CHAT_DEBUG) {
308 if (is_array($msg) || is_object($msg)) {
309 drupal_set_message('<pre>'. print_r($msg, true) .'</pre>', 'debug');
310 }
311 else{
312 drupal_set_message('mebeam_chat: '. $msg, 'debug');
313 }
314 }
315 }