/[drupal]/contributions/modules/chatroom/chatroom.forms.inc
ViewVC logotype

Contents of /contributions/modules/chatroom/chatroom.forms.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Oct 29 21:41:18 2009 UTC (4 weeks, 2 days ago) by justinrandell
Branch: MAIN
Changes since 1.4: +165 -167 lines
File MIME type: text/x-php
pulling in changes from dev branch to head
1 <?php
2 // $Id: chatroom.forms.inc,v 1.2.2.9 2009/10/29 20:43:01 justinrandell Exp $
3
4 /**
5 * @file
6 * Form functions for chatroom.module.
7 */
8
9 /**
10 * Implementation of hook_forms()
11 */
12 function chatroom_forms() {
13 $forms['chatroom_chat_form'] = array(
14 'callback' => 'chatroom_chat_interface',
15 );
16 $forms['chatroom_create_chat_form'] = array(
17 'callback' => 'chatroom_create_chat_form',
18 );
19 $forms['chatroom_archive_chat_form'] = array(
20 'callback' => 'chatroom_archive_chat_form',
21 );
22 $forms['chatroom_unarchive_chat_form'] = array(
23 'callback' => 'chatroom_unarchive_chat_form',
24 );
25 return $forms;
26 }
27
28 /**
29 * Implementation of hook_form().
30 */
31 function chatroom_form(&$node, $form_state) {
32 global $user;
33 $form['title'] = array(
34 '#type' => 'textfield',
35 '#title' => t('Name'),
36 '#default_value' => check_plain($node->title),
37 '#required' => TRUE,
38 );
39 $form['body_filter']['body'] = array(
40 '#type' => 'textarea',
41 '#title' => t('Description'),
42 '#default_value' => $node->body,
43 '#rows' => 3,
44 '#description' => t('Describe your chat room so other people will know if they want to join.'),
45 );
46 $form['body_filter']['format'] = filter_form($node->format);
47 $form['kicked_out_message'] = array(
48 '#type' => 'textarea',
49 '#title' => t('Chat room kicked out message'),
50 '#default_value' => isset($node->chatroom->kicked_out_message) ? $node->chatroom->kicked_out_message : null,
51 '#rows' => 3,
52 '#description' => t('This text will appear on the page kicked out users are sent to. Defaults to, "You have been kicked out of %chat for misbehaving."', array('%chat' => t('chat-name'))),
53 );
54 $form['banned_message'] = array(
55 '#type' => 'textarea',
56 '#title' => t('Chat room banned message'),
57 '#default_value' => isset($node->chatroom->banned_message) ? $node->chatroom->banned_message : null,
58 '#rows' => 3,
59 '#description' => t('This text will appear on the page banned users are sent to. Defaults to, "You have been banned from %chatroom."', array('%chatroom' => t('chat-room'))),
60 );
61 if (!empty($node->chatroom->banned_users)) {
62 $form['chatroom_banned_users'] = array(
63 '#type' => 'fieldset',
64 '#title' => t('Manage banned users'),
65 '#collapsible' => TRUE,
66 );
67 foreach ($node->chatroom->banned_users as $banned_user) {
68 $banned_users[$banned_user->uid] = check_plain($banned_user->name);
69 }
70 $form['chatroom_banned_users']['unban_list'] = array(
71 '#type' => 'checkboxes',
72 '#options' => $banned_users,
73 '#description' => t('Check the users you would like to unban'),
74 );
75 }
76 $form['chat_settings'] = array(
77 '#type' => 'fieldset',
78 '#title' => t('Chat settings'),
79 '#collapsible' => TRUE,
80 );
81 $form['chat_settings']['poll_freq'] = array(
82 '#type' => 'select',
83 '#title' => t('Update frequency'),
84 '#default_value' => empty($node->chatroom->poll_freq) ? 1 : $node->chatroom->poll_freq / 1000,
85 '#options' => drupal_map_assoc(range(1, 10)),
86 '#description' => t('How many seconds between each request for updates from the server.'),
87 );
88 $form['chat_settings']['idle_freq'] = array(
89 '#type' => 'select',
90 '#title' => t('Idle time'),
91 '#default_value' => empty($node->chatroom->idle_freq) ? 60 : $node->chatroom->idle_freq / 1000,
92 '#options' => drupal_map_assoc(array(20, 40, 60, 80, 100, 120, 140, 160, 180)),
93 '#description' => t('How many seconds between each message before a last message time is shown in the chat.'),
94 );
95 $old_msg_range = array();
96 for ($i = 1; $i <= 25; $i++) {
97 $old_msg_range[$i] = $i * 10;
98 }
99 $form['chat_settings']['previous_messages_display_count'] = array(
100 '#type' => 'select',
101 '#title' => t('Old messages'),
102 '#description' => t('How many old messages to show when entering a chat.'),
103 '#default_value' => empty($node->chatroom->previous_messages_display_count) ? 20 : $node->chatroom->previous_messages_display_count,
104 '#options' => drupal_map_assoc($old_msg_range),
105 );
106 $form['chat_settings']['popout'] = array(
107 '#type' => 'checkbox',
108 '#title' => t("Load this rooms' chats in a popout?"),
109 '#description' => t('Should chats in this room default to loading in a popup window?'),
110 '#default_value' => isset($node->chatroom) ? $node->chatroom->popout : 0,
111 );
112 $form['chatroom_profile_picture_support'] = array(
113 '#type' => 'fieldset',
114 '#title' => t('User profile picture support'),
115 '#collapsible' => TRUE,
116 '#collapsed' => TRUE,
117 );
118 $form['chatroom_profile_picture_support']['profile_picture'] = array(
119 '#type' => 'checkbox',
120 '#title' => t('User profile picture.'),
121 '#description' => t('Enable User profile picture for chatroom.'),
122 '#default_value' => $node->chat->profile_picture,
123 '#disabled' => !variable_get('user_pictures', FALSE),
124 );
125 if (module_exists('imagecache')) {
126 $presets = array('');
127 foreach (imagecache_presets() as $key => $preset) {
128 $presets[$key] = check_plain($preset['presetname']);
129 }
130 $form['chatroom_profile_picture_support']['profile_picture_preset'] = array(
131 '#type' => 'select',
132 '#title' => t('Profile picture preset'),
133 '#default_value' => variable_get('chatroom_profile_picture_preset', ''),
134 '#options' => $presets,
135 '#description' => t("This will set the picture size."),
136 );
137 }
138 if (!empty($node->chatroom->chats)) {
139 foreach ($node->chatroom->chats as $chat) {
140 if ($chat->section != 'archives') {
141 $chats[$chat->ccid] = check_plain($chat->chatname);
142 }
143 else {
144 $closed_chats[$chat->ccid] = check_plain($chat->chatname);
145 }
146 }
147 if (!empty($chats)) {
148 $form['chatroom_chats'] = array(
149 '#type' => 'fieldset',
150 '#title' => t('Manage open chats'),
151 '#collapsible' => TRUE,
152 '#collapsed' => FALSE,
153 );
154 $form['chatroom_chats']['chat_list'] = array(
155 '#type' => 'checkboxes',
156 '#options' => $chats,
157 '#description' => t('Check the chats you would like to close'),
158 );
159 }
160 if (!empty($closed_chats)) {
161 $form['closed_chats'] = array(
162 '#type' => 'fieldset',
163 '#title' => t('Manage archived chats'),
164 '#collapsible' => TRUE,
165 '#collapsed' => FALSE,
166 );
167 $form['closed_chats']['closed_chat_list'] = array(
168 '#type' => 'checkboxes',
169 '#options' => $closed_chats,
170 '#description' => t('Check the chats you would like to delete'),
171 );
172 }
173 }
174 return $form;
175 }
176
177 /**
178 * Menu callback; display site-wide chat room settings.
179 */
180 function chatroom_admin_settings() {
181 $form['chatroom_auto_archive'] = array(
182 '#type' => 'checkbox',
183 '#title' => t('Automatically archive old messages.'),
184 '#description' => t('If there are a lot of old messages, archiving will improve chat performance.'),
185 '#default_value' => variable_get('chatroom_auto_archive', FALSE),
186 );
187 $form['chatroom_block_update_interval'] = array(
188 '#type' => 'textfield',
189 '#title' => t('Chat room block update interval'),
190 '#default_value' => variable_get('chatroom_block_update_interval', 5),
191 '#description' => t('Determines how often blocks should update active chat rooms, active chats, and on-line users.'),
192 '#size' => 2,
193 );
194 $form['chatroom_guest_user_prefix'] = array(
195 '#type' => 'textfield',
196 '#title' => t('Guest user prefix'),
197 '#description' => t('Prefixed to guest ID to provide user name for anonymous users.'),
198 '#default_value' => variable_get('chatroom_guest_user_prefix', t('guest-')),
199 '#size' => 20,
200 );
201 $form['chatroom_chat_message_text'] = array(
202 '#type' => 'textfield',
203 '#title' => t('Chat message help text'),
204 '#description' => t('The text to be displayed next to the chat message form field.'),
205 '#default_value' => variable_get('chatroom_chat_message_text', t('Type your message and hit enter')),
206 '#size' => 20,
207 );
208 $form['chatroom_chat_date_format'] = array(
209 '#type' => 'textfield',
210 '#title' => t('Chat date format'),
211 '#attributes' => array('class' => 'custom-format'),
212 '#default_value' => variable_get('chatroom_chat_date_format', '* \S\e\n\t \a\t G:i'),
213 '#description' => t('Format for system time messages in chats. See the <a href="@url">PHP manual</a> for available options. This format is currently set to display as <span>%date</span>.', array('@url' => 'http://php.net/manual/function.date.php', '%date' => format_date(time(), 'custom', variable_get('chatroom_chat_date_format', '* \S\e\n\t \a\t G:i')))),
214 );
215 if (module_exists('smileys')) {
216 $form['chatroom_smileys_support'] = array(
217 '#type' => 'fieldset',
218 '#title' => t('Smileys module support'),
219 '#collapsible' => TRUE,
220 '#collapsed' => TRUE,
221 );
222 $form['chatroom_smileys_support']['chatroom_smileys_enabled'] = array(
223 '#type' => 'checkbox',
224 '#title' => t('Enable Smileys module support.'),
225 '#default_value' => variable_get('chatroom_smileys_enabled', FALSE),
226 );
227 $form['chatroom_smileys_support']['chatroom_smileys_showtextentry'] = array(
228 '#type' => 'checkbox',
229 '#title' => t('Show smileys in text entry box.'),
230 '#default_value' => variable_get('chatroom_smileys_enabled', FALSE) && variable_get('chatroom_smileys_showtextentry', FALSE),
231 '#disabled' => !variable_get('chatroom_smileys_enabled', FALSE),
232 );
233 }
234 $form['chatroom_alerts'] = array(
235 '#type' => 'fieldset',
236 '#title' => t('Chat alerts'),
237 '#collapsible' => TRUE,
238 '#collapsed' => TRUE,
239 );
240 $form['chatroom_alerts']['chatroom_alerts'] = array(
241 '#type' => 'checkbox',
242 '#title' => t('Enable chat alerts.'),
243 '#description' => t('Checking this box will allow users to turn on alerts for chat events.'),
244 '#default_value' => variable_get('chatroom_alerts', FALSE),
245 );
246 $form['chatroom_alerts']['chatroom_alerts_default'] = array(
247 '#type' => 'checkbox',
248 '#title' => t('Turn alerts on by default.'),
249 '#description' => t('Check this box if you want chats to open with alerts on.'),
250 '#default_value' => variable_get('chatroom_alerts', FALSE) && variable_get('chatroom_alerts_default', FALSE),
251 '#disabled' => !variable_get('chatroom_alerts', FALSE),
252 );
253 $form['chatroom_alerts']['chatroom_custom_sounds'] = array(
254 '#type' => 'checkbox',
255 '#title' => t('Use custom sounds for chat alerts.'),
256 '#description' => t('Check this box if you want to replace default chat alert sounds with your own MP3s.'),
257 '#default_value' => variable_get('chatroom_alerts', FALSE) && variable_get('chatroom_custom_sounds', FALSE),
258 '#disabled' => !variable_get('chatroom_alerts', FALSE),
259 );
260 return system_settings_form($form);
261 }
262
263 /**
264 * Validate site-wide chat room settings.
265 */
266 function chatroom_admin_settings_validate($form, &$form_state) {
267 // Check for a valid update interval.
268 $interval = $form_state['values']['chatroom_block_update_interval'];
269 if (is_numeric($interval)) {
270 if ($interval == 0) {
271 form_set_error('chatroom_block_update_interval', t('The block update interval cannot be zero.'));
272 }
273 elseif ($interval < 0) {
274 form_set_error('chatroom_block_update_interval', t('The block update interval cannot be negative.'));
275 }
276 }
277 else {
278 form_set_error('chatroom_block_update_interval', t('The block update interval must be a number.'));
279 }
280 }
281
282 /**
283 * Implementation of hook_form().
284 *
285 * TODO: make the default values cascade.
286 */
287 function chatroom_chat_form(&$node, $form_state) {
288
289 global $user;
290 $form['title'] = array(
291 '#type' => 'textfield',
292 '#title' => t('Name'),
293 '#default_value' => check_plain($node->title),
294 '#description' => t('The name for your chat.'),
295 '#required' => TRUE,
296 );
297 $form['body_filter']['body'] = array(
298 '#type' => 'textarea',
299 '#title' => t('Description'),
300 '#default_value' => $node->body,
301 '#rows' => 3,
302 '#description' => t('Describe your chat so other people will know if they want to join.'),
303 );
304 $form['body_filter']['format'] = filter_form($node->format);
305 $form['kicked_out_message'] = array(
306 '#type' => 'textarea',
307 '#title' => t('Chat kicked out message'),
308 '#default_value' => '',
309 '#rows' => 3,
310 '#description' => t('This text will appear on the page kicked out users are sent to. Defaults to, "You have been kicked out of %chat for misbehaving."', array('%chat' => t('chat-name'))),
311 );
312 $form['banned_message'] = array(
313 '#type' => 'textarea',
314 '#title' => t('Chat room banned message'),
315 '#default_value' => '',
316 '#rows' => 3,
317 '#description' => t('This text will appear on the page banned users are sent to. Defaults to, "You have been banned from %chatroom."', array('%chatroom' => t('chat-room'))),
318 );
319 if (!empty($node->banned_users)) {
320 $form['chatroom_banned_users'] = array(
321 '#type' => 'fieldset',
322 '#title' => t('Manage banned users'),
323 '#collapsible' => TRUE,
324 );
325 foreach ($node->banned_users as $banned_user) {
326 $banned_users[$banned_user->uid] = check_plain($banned_user->name);
327 }
328 $form['chatroom_banned_users']['unban_list'] = array(
329 '#type' => 'checkboxes',
330 '#options' => $banned_users,
331 '#description' => t('Check the users you would like to unban'),
332 );
333 }
334 $form['chat_settings'] = array(
335 '#type' => 'fieldset',
336 '#title' => t('Chat settings'),
337 '#collapsible' => TRUE,
338 );
339 $form['chat_settings']['poll_freq'] = array(
340 '#type' => 'select',
341 '#title' => t('Update frequency'),
342 '#default_value' => empty($node->poll_freq) ? 1 : $node->poll_freq / 1000,
343 '#options' => drupal_map_assoc(range(1, 10)),
344 '#description' => t('How many seconds between each request for updates from the server.'),
345 );
346 $form['chat_settings']['idle_freq'] = array(
347 '#type' => 'select',
348 '#title' => t('Idle time'),
349 '#default_value' => empty($node->idle_freq) ? 60 : $node->idle_freq / 1000,
350 '#options' => drupal_map_assoc(array(20, 40, 60, 80, 100, 120, 140, 160, 180)),
351 '#description' => t('How many seconds between each message before a last message time is shown in the chat.'),
352 );
353 $form['chat_settings']['popout'] = array(
354 '#type' => 'checkbox',
355 '#title' => t('Load this chat in a popout?'),
356 '#description' => t('Should this chat load in a popup window?'),
357 '#default_value' => isset($node->chat) ? $node->chat->popout : 0,
358 );
359 $previous_msg_range = array();
360 for ($i = 1; $i <= 25; $i++) {
361 $previous_msg_range[$i] = $i * 10;
362 }
363 $form['chat_settings']['previous_messages_display_count'] = array(
364 '#type' => 'select',
365 '#title' => t('Old messages'),
366 '#description' => t('How many old messages to show when entering a chat.'),
367 '#default_value' => empty($node->previous_messages_display_count) ? 20 : $node->previous_messages_display_count,
368 '#options' => drupal_map_assoc($previous_msg_range),
369 );
370 $form['chatroom_profile_picture_support'] = array(
371 '#type' => 'fieldset',
372 '#title' => t('User profile picture support'),
373 '#collapsible' => TRUE,
374 '#collapsed' => TRUE,
375 );
376 $form['chatroom_profile_picture_support']['profile_picture'] = array(
377 '#type' => 'checkbox',
378 '#title' => t('User profile picture.'),
379 '#description' => t('Enable User profile picture for chatroom.'),
380 '#default_value' => $node->chat->profile_picture,
381 '#disabled' => !variable_get('user_pictures', FALSE),
382 );
383 if (module_exists('imagecache')) {
384 $presets = array('');
385 foreach (imagecache_presets() as $key => $preset) {
386 $presets[$key] = check_plain($preset['presetname']);
387 }
388 $form['chatroom_profile_picture_support']['profile_picture_preset'] = array(
389 '#type' => 'select',
390 '#title' => t('Profile picture preset'),
391 '#default_value' => variable_get('chatroom_profile_picture_preset', ''),
392 '#options' => $presets,
393 '#description' => t("This will set the picture size."),
394 );
395 }
396 return $form;
397 }
398
399 function chatroom_chat_interface(&$form_state, $node) {
400 // The message board itself.
401 $form['board']['#value'] = theme('chatroom_user_list', $node->chat->users);
402 $form['board']['#value'] .= '<div id="chatroom-board">';
403 // Messages for initial display.
404 if (!$form_state['submitted']) {
405 $messages = chatroom_chat_load_messages($chat->nid, 0, 20);
406 $form['board']['#value'] .= theme('chatroom_messages', $messages);
407 }
408 $form['board']['#value'] .= '</div>';
409 // Control data for the form
410 $form['control'] = array(
411 '#type' => 'fieldset',
412 '#attributes' => array('id' => 'edit-control-fieldset'),
413 );
414 $data = array('nid');
415 // The actual submit button. Not displayed so the user isn't troubled with a
416 // whole bunch of throbbers.
417 $form['control']['submit'] = array(
418 '#type' => 'submit',
419 '#name' => 'submit',
420 '#ahah' => array(
421 'path' => 'chatroom/js',
422 'wrapper' => 'chatroom-board',
423 'method' => 'append',
424 ),
425 );
426 // Other form elements.
427 $form['message'] = array(
428 '#type' => 'textfield',
429 '#title' => variable_get('chatroom_chat_message_text', t('Type your message and hit enter')),
430 '#size' => 50,
431 '#maxlength' => variable_get('chatroom_max_message_size', 1000),
432 );
433 $form['away'] = array(
434 '#type' => 'checkbox',
435 '#title' => t('Show me as away.'),
436 '#name' => 'away',
437 );
438 if (variable_get('chatroom_alerts', FALSE)) {
439 $checked = variable_get('chatroom_alerts_default', FALSE) ? ' checked' : '';
440 $form['alerts'] = array(
441 '#type' => 'checkbox',
442 '#title' => t('Alert me if new messages are received.'),
443 '#name' => 'alert',
444 );
445 }
446 return $form;
447 }
448
449 /**
450 * Return a create chat form.
451 */
452 function chatroom_create_chat_form(&$form_state, $room) {
453 $form['chatroom_create_chat'] = array(
454 '#type' => 'fieldset',
455 '#title' => t('Create a new chat'),
456 '#collapsible' => TRUE,
457 '#collapsed' => FALSE,
458 );
459 $form['chatroom_create_chat']['chat_name'] = array(
460 '#type' => 'textfield',
461 '#title' => t('Chat name'),
462 '#size' => 30,
463 '#required' => TRUE,
464 '#description' => t('Enter the name for the chat'),
465 );
466 $form['chatroom_create_chat']['nid'] = array(
467 '#type' => 'hidden',
468 '#value' => $room->nid,
469 );
470 $form['chatroom_create_chat']['submit'] = array(
471 '#type' => 'submit',
472 '#value' => t('Create chat'),
473 );
474 $form['#validate'][] = 'chatroom_create_chat_form_validate';
475 $form['#submit'][] = 'chatroom_create_chat_form_submit';
476 return $form;
477 }
478
479 /**
480 * Create a chat.
481 */
482 function chatroom_create_chat_form_submit($form, &$form_state) {
483 global $user;
484 $values = (object) $form_state['values'];
485 $chat = new StdClass();
486 $chat->type = 'chat';
487 $chat->title = $values->chat_name;
488 $chat->uid = $user->uid;
489 $chat->created = time();
490 $chat->crid = $values->nid;
491 $chat->idle_freq = 20000;
492 $chat->poll_freq = 1000;
493 $chat->kicked_out_message = '';
494 $chat->banned_message = '';
495 $chat->previous_messages_display_count = 20;
496 $chat->popout = 0;
497 node_save($chat);
498 }
499
500 /**
501 * Form to unarchived a chat.
502 *
503 * @param array $form_state
504 * @param integer $chat_id
505 * @return array
506 */
507 function chatroom_unarchive_chat_form($form_state, $chat_id) {
508 $form['chat_id'] = array(
509 '#type' => 'value',
510 '#value' => $chat_id,
511 );
512 $form['unarchive'] = array(
513 '#type' => 'submit',
514 '#value' => t('Unarchive this chat'),
515 );
516 return $form;
517 }
518
519 /**
520 * Unarchive an open chat.
521 *
522 * @param array $form
523 * @param array $form_state
524 * @return string
525 */
526 function chatroom_unarchive_chat_form_submit($form, &$form_state) {
527 chatroom_unarchive_chat($form_state['values']['chat_id']);
528 cache_clear_all(TRUE, 'cache_block');
529 }
530
531 /**
532 * form for archived chat view of open chat to archive that chat
533 *
534 * @param array $form_state
535 * @param integer $chat_id
536 * @return array
537 */
538 function chatroom_archive_chat_form($form_state, $chat_id) {
539 $form['chat_id'] = array(
540 '#type' => 'value',
541 '#value' => $chat_id,
542 );
543 $form['delete'] = array(
544 '#type' => 'submit',
545 '#value' => t('Archive this chat'),
546 );
547 return $form;
548 }
549
550 /**
551 * Archive an open chat.
552 *
553 * @param array $form_id
554 * @param array $form_values
555 * @return string
556 */
557 function chatroom_archive_chat_form_submit($form, &$form_state) {
558 chatroom_archive_chat($form_state['values']['chat_id']);
559 cache_clear_all(TRUE, 'cache_block');
560 }
561

  ViewVC Help
Powered by ViewVC 1.1.2