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

  ViewVC Help
Powered by ViewVC 1.1.2