summaryrefslogtreecommitdiffstats
path: root/mebeam_chat.module
blob: 7b71c2caee45734d7e9e5c8f018fbb11d6866237 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
// $Id$

/**
 * @file: mebeam_chat.module
 * Adds a MeBeam audio/Video/Text chat page to your Drupal Site.
 * @author: Gary Varnell <gary@dgswa.com>
 * @copyright: 2009 DGS
*/


#================================================================================
define('MEBEAM_CHAT_DEBUG', 1);

/**
 * Implementation of hook_help
 */
function mebeam_chat_help($section) {
  switch ($section) {
    case 'admin/help#mebeam_chat':
      $output = '<p>'. t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.') .'</p>';
      return $output;
      break;
    case 'admin/modules#description':
      return t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.');
      break;
  }
}
//-----------------------------------------

/**
 * Implementation of hook_perm
 */
function mebeam_chat_perm() {
  return array('view_mebeam_chat');
}
//-----------------------------------------



/**
 * Implementation of hook_menu().
 */
function mebeam_chat_menu($may_cache) {
//    menu_rebuild();

  
  if (!$may_cache) {
    # non-cachable menus
    

  }
  else{
    # cachable menu items
    # admin menus
    $items[] = array( 'path' => 'admin/settings/mebeam-chat',
                      'title' => t('MeBeam Chat Settings'),
                      'description' => t('Configure MeBeam Chat Settings.'),
                      'access' => user_access('administer nodes'),
                      'callback' => 'mebeam_chat_show_settings_form',
                      'type' => MENU_NORMAL_ITEM,
                    );
    # user menus
    $items[] = array( 'path' => 'mebeam-chat',
                      'title' => t('MeBeam Chat'),
                      'callback' => 'mebeam_chat_view_chat',
                      'access' => user_access('view_mebeam_chat'),
                      'type' => MENU_NORMAL_ITEM,
                    );
  }
  return $items;
}
//-----------------------------------------


/**
 * Implementation of hook_cron
 */
function mebeam_chat_cron() {
  $mebeam_chat_rm_name = variable_get('mebeam_chat_rm_name', '');
  if (variable_get('mebeam_chat_rm_name_random', 0)) {
    $mebeam_chat_rm_name = mebeam_chat_randomize_name($mebeam_chat_rm_name);
    variable_set('mebeam_chat_rm_rname', $mebeam_chat_rm_name);
  }
}
//-----------------------------------------

function mebeam_chat_randomize_name($name) {
  # generate a unique room name
  $id = uniqid();
  $id = md5($id);
  $name = "$name-$id";
  $name = substr($name, 0, 15);
  return $name;
}
//-----------------------------------------

function mebeam_chat_view_chat() {
    $default = mebeam_chat_get_defaults();
    $pg_title = variable_get('mebeam_chat_pg_title', $default['pg_title']);
    if (!empty($pg_title)) {
      drupal_set_title($pg_title);
    }
    
    $pg_header = variable_get('mebeam_chat_pg_header', $default['pg_header']);
    $pg_footer = variable_get('mebeam_chat_pg_footer', $default['pg_footer']);
    
    $rm_name = variable_get('mebeam_chat_rm_name', $default['rm_name']);
    if (variable_get('mebeam_chat_rm_name_random', 0)) {
      $rm_name = variable_get('mebeam_chat_rm_rname', substr(time(),0,10));
    }
    # clean up room name
    $rm_name = preg_replace('|[^\d\w-]|', '', $rm_name);
    $rm_name = substr($rm_name, 0, 15);
    $rm_show = variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']);
    if ($rm_show) {
      $rm_show = "<div class='room_name'>$rm_name</div>";
    }
    else{
      $rm_show = '';
    }
    $rm_width = variable_get('mebeam_chat_rm_width', $default['rm_width']);
    $rm_height = variable_get('mebeam_chat_rm_height', $default['rm_height']);
    
  $output = <<<EOF
    $pg_header
 <center>
 <div id='mebeam-chat'>
    $rm_show
 <center>
 <div id='hotroom'>
 <object  width='$rm_width' height='$rm_height' id='uhzy' align='middle'>
 <param name='allowScriptAccess' value='never' >
 <param name='movie' value='http://www.mebeam.com/mebeam.swf'>
 <param name='quality' value='best' >
 <param name='scale' value='noscale' >
 <param name='salign' value='t' >
 <param name='flashvars' value='room=$rm_name'>
 <embed flashvars='room=$rm_name' src='http://www.mebeam.com/mebeam.swf'
  quality='best' scale='noscale' salign='t'
  width='$rm_width' height='$rm_height' name='uhzy' align='middle'
  allowScriptAccess='never'
  type='application/x-shockwave-flash'
  pluginspage='http://www.macromedia.com/go/getflashplayer' >
 </object>
 </center>
 </div>
    $pg_footer
    
EOF;

  return $output;
}

function mebeam_chat_get_defaults() {
 #so we don't have defaults spread around - user might not goto module settings
 $default['rm_name'] = variable_get('site_name', 'undefined') .'-chat';
 $default['pg_title'] = variable_get('site_name', '') .'Live Chat';
 $default['pg_header'] = t("Provided by <a href=\"http://www.mebeam.com\" target=\"_blank\">MeBeam.com</a>.<br/>
                            The site owner has no control over who enters this chat room.");
 $default['pg_footer'] = t("Mouse over your image to control your audio and video settings<br/>
                            Mouse over a user and click Mute or Fade to ignore that user.");
 $default['rm_name_random'] = FALSE;
 $default['rm_show_room'] = TRUE;
 $default['rm_width'] = 640;
 $default['rm_height'] = 540;
 
 return $default;
}
//-----------------------------------------
// Administration form
//-----------------------------------------
function mebeam_chat_show_settings_form() {
  return drupal_get_form('mebeam_chat_settings_form');
}

function mebeam_chat_settings_form() {
  $default = mebeam_chat_get_defaults();
  $rname = variable_get('mebeam_chat_rm_rname', $default['rm_name']);

  # Certification Request User Messages
  $form['page'] = array(
    '#title' => t('Page Options'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['page']['mebeam_chat_pg_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Page Title'),
    '#default_value' => variable_get('mebeam_chat_pg_title', $default['pg_title']),
    '#description' => t("Title for your chat page. Leave blank to use the menu title"),
  );
  
  $form['page']['mebeam_chat_pg_header'] = array(
    '#type' => 'textarea',
    '#title' => t('Page Header'),
    '#default_value' => variable_get('mebeam_chat_pg_header', $default['pg_header']),
    '#description' => t("Title for your chat page. Leave blank for no header"),
  );
  
  $form['page']['mebeam_chat_pg_footer'] = array(
    '#type' => 'textarea',
    '#title' => t('Page Footer'),
    '#default_value' => variable_get('mebeam_chat_pg_footer', $default['pg_footer']),
    '#description' => t("Footer for your chat page. Leave blank for no footer"),
  );
  
  # widget options
  $form['widget'] = array(
    '#title' => t('MeBeam Widget Options'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['widget']['mebeam_chat_rm_name'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam Room Name'),
    '#default_value' => variable_get('mebeam_chat_rm_name', $default['rm_name']),
    '#description' => t("The name of your chat room - must be between 4 and 16 characters.
    Also, if you do not have randomize turned on you should use a dash in your room name. ex. chatroom-01 or chatroom-b"),
  );

  $form['widget']['mebeam_chat_rm_name_random'] = array(
    '#type' => 'checkbox',
    '#title' => t('Randomize'),
    '#default_value' => variable_get('mebeam_chat_rm_name_random', $default['rm_name_random']),
    '#description' => t("Randomize the room name - Randomly generates a new room name everytime cron.php is run. This helps keep your room somewhat private.
     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.
     <br/>%rname", array('%rname' => $rname)),
  );
  $form['widget']['mebeam_chat_rm_show_room'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Room Name'),
    '#default_value' => variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']),
    '#description' => t("Show the room name above the chat widget"),
  );
  $form['widget']['mebeam_chat_rm_width'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam widget width'),
    '#default_value' => variable_get('mebeam_chat_rm_width', $default['rm_width']),
    '#description' => t("The width of the mebeam widget on your page in pixels"),
  );
  $form['widget']['mebeam_chat_rm_height'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam widget width'),
    '#default_value' => variable_get('mebeam_chat_rm_height', $default['rm_height']),
    '#description' => t("The height of the mebeam widget on your page in pixels"),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );
  return $form;
}
//-----------------------------------------

function mebeam_chat_settings_form_validate($form_id, $form_values) {
    if (strlen($form_values['mebeam_chat_rm_name']) >= 16) {
      form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be less than 16 characters."));
    }
    if (strlen($form_values['mebeam_chat_rm_name']) <= 4) {
      form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be more than 4 characters."));
    }
    if (!is_numeric($form_values['mebeam_chat_rm_width'])) {
      form_set_error('mebeam_chat_rm_width', t("MeBeam widget width must be numeric."));
    }
    if (!is_numeric($form_values['mebeam_chat_rm_height'])) {
      form_set_error('mebeam_chat_rm_height', t("MeBeam widget height must be numeric."));
    }
}
//---------------------------------------------------------

function mebeam_chat_settings_form_submit($form_id, $form_values) {
    variable_set('mebeam_chat_pg_title', $form_values['mebeam_chat_pg_title']);
    variable_set('mebeam_chat_pg_header', $form_values['mebeam_chat_pg_header']);
    variable_set('mebeam_chat_pg_footer', $form_values['mebeam_chat_pg_footer']);
    
    variable_set('mebeam_chat_rm_name', $form_values['mebeam_chat_rm_name']);
    if ($form_values['mebeam_chat_rm_name_random']) {
      $rname = mebeam_chat_randomize_name($form_values['mebeam_chat_rm_name']);
      variable_set('mebeam_chat_rm_rname', $rname);
    }
    else{
      variable_set('mebeam_chat_rm_rname', '');
    }
    variable_set('mebeam_chat_rm_name_random', $form_values['mebeam_chat_rm_name_random']);
    variable_set('mebeam_chat_rm_show_room', $form_values['mebeam_chat_rm_show_room']);
    variable_set('mebeam_chat_rm_width', $form_values['mebeam_chat_rm_width']);
    variable_set('mebeam_chat_rm_height', $form_values['mebeam_chat_rm_height']);
    
    $msg = t("Form options saved successfully."); 
    if ($form_values['mebeam_chat_rm_name_random']) {
      $msg .= t('Your random room name is currently %rname and will be updated automatically the next time cron runs', array('%rname' => $rname));
    }
    drupal_set_message($msg, 'status');
}
//---------------------------------------------------------

###########################################################################
# Helper Functions


function mebeam_chat_debug($msg) {
  if (MEBEAM_CHAT_DEBUG) {
    if (is_array($msg) || is_object($msg)) {
      drupal_set_message('<pre>'. print_r($msg, true) .'</pre>', 'debug');
    }
   else{
      drupal_set_message('mebeam_chat: '. $msg, 'debug');
    }
  }
}