/[drupal]/contributions/modules/bio/bio.module
ViewVC logotype

Contents of /contributions/modules/bio/bio.module

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


Revision 1.9 - (show annotations) (download) (as text)
Sat Apr 11 04:22:39 2009 UTC (7 months, 2 weeks ago) by vauxia
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +2 -1 lines
File MIME type: text/x-php
#298349 by bflora, joshk - No bio shows up when new users register themselves
1 <?php
2 // $Id: bio.module,v 1.8 2009/04/11 01:32:10 vauxia Exp $
3
4 /**
5 * @file
6 * Bio module optionally creates a "Biography" node per user.
7 *
8 * Use cases for this module include:
9 * - Add a Biography for each of your employees.
10 * - Make user profiles into nodes, for extra functionality.
11 * - Add CCK fields to the user registration form.
12 */
13
14 // Panels integration.
15 if (module_exists('panels')) {
16 include_once drupal_get_path('module', 'bio') .'/bio_panels.inc';
17 }
18
19 /**
20 * Implementation of hook_init().
21 */
22 function bio_init() {
23 // TODO: This interferes with aggressive caching. Can we do this in
24 // hook_menu() instead? Looks like we'd need to make bio follow after node.
25 // Could cause problems w/ registration and/or CRUD functions.
26
27 // Don't allow non-admin users to create more than one bio.
28 if ($_GET['q'] == 'node/add/'. bio_get_type()) {
29 if (($nid = bio_for_user()) && !user_access('administer nodes')) {
30 drupal_goto('node/'. $nid .'/edit');
31 }
32 }
33 }
34
35 /**
36 * Implementation of hook_menu().
37 */
38 function bio_menu($may_cache) {
39 global $user;
40 $items = array();
41
42 if ($may_cache) {
43 $items[] = array(
44 'path' => 'admin/user/bio',
45 'title' => t('User biographies'),
46 'description' => t('User biographies'),
47 'callback' => 'drupal_get_form',
48 'callback arguments' => array('bio_settings'),
49 );
50 }
51 else {
52 if (variable_get('bio_profile', 0) && (arg(0) == 'user') && (is_numeric(arg(1)))) {
53 $type = bio_get_type();
54 $nid = bio_for_user(arg(1));
55
56 // Determine access based on whether user has a bio.
57 if ($nid) {
58 $node = node_load($nid);
59 $access = node_access('update', $node);
60 }
61 else {
62 $node = (object) array('type' => $type, 'uid' => arg(1));
63 $access = (($user->uid == arg(1)) && node_access('create', $type)) || user_access('administer nodes');
64 }
65 $items[] = array(
66 'path' => 'user/'. arg(1) .'/bio',
67 'title' => node_get_types('name', $type),
68 'callback' => 'node_page_edit',
69 'callback arguments' => array($node),
70 'type' => MENU_LOCAL_TASK,
71 'access' => $access,
72 );
73 }
74 elseif (variable_get('bio_profile', 0) && (arg(0) == 'node') && is_numeric(arg(1)) && !arg(2)) {
75 // If we're about to visit a bio node page, but we've got "use bios for profiles" selected,
76 // redirect to the user page.
77 $node = node_load(arg(1));
78 if (user_access('access user profiles') && $node->type == bio_get_type()) {
79 drupal_goto('user/'. $node->uid);
80 }
81 }
82 }
83 return $items;
84 }
85
86 /**
87 * Implementation of hook_form_alter().
88 */
89 function bio_form_alter($form_id, &$form) {
90 if ($form_id == bio_get_type() .'_node_form' && arg(0) == 'user') {
91 // We're editing the bio in the user area... be sure we end up here when we
92 // finish submission, and disallow changing the author.
93 $account = user_load(array('uid' => arg(1)));
94 $form['#redirect'] = "user/$account->uid";
95 if (isset($form['author']['name'])) {
96 $form['author']['name']['#default_value'] = $account->name;
97 $form['author']['name']['#value'] = $account->name;
98 $form['author']['name']['#disabled'] = TRUE;
99 $form['uid']['#value'] = $account->uid;
100 unset($form['author']['name']['#autocomplete_path']);
101 $form['author']['name']['#description'] = t('This field is disabled. You cannot alter the author of this entry from within the user area.');
102 }
103 }
104 }
105
106 /**
107 * Returns a FAPI form array for use on the user registration form.
108 *
109 * @see bio_user().
110 */
111 function bio_user_register_form() {
112 $form = array();
113 if (variable_get('bio_regstration_form', 0) && module_exists('content')) {
114 $widget_types = _content_widget_types();
115 $fields = _bio_get_fields();
116 $default_values = variable_get('bio_regstration_form_fields', array());
117
118 // Create a dummy node to pass along to the cck hooks.
119 $node = new stdClass();
120 $node->type = bio_get_type();
121
122 $bio_type_name = node_get_types('name', bio_get_type());
123 $form['bio_info'] = array(
124 '#type' => 'fieldset',
125 '#title' => t('@bio_type_name information', array('@bio_type_name' => $bio_type_name)),
126 );
127
128 foreach ($fields as $field_name => $field) {
129 if ($field['required'] || !empty($default_values[$field_name])) {
130 $node_field = content_default_value($node, $field, array());
131
132 // Figure out what widget function to call.
133 $module = $widget_types[$field['widget']['type']]['module'];
134 $function = $module .'_widget';
135
136 // Get the form field and add it to the form.
137 $cck_field = $function('prepare form values', $node, $field, $node_field);
138 $cck_field = $function('form', $node, $field, $node_field);
139 $form['bio_info'][] = $cck_field;
140 }
141 }
142
143 // Add custom validate handler.
144 $form['#validate']['bio_user_register_validate'] = array();
145 }
146 return $form;
147 }
148
149 /**
150 * Validate handler for user registration form. Validate CCK fields.
151 */
152 function bio_user_register_validate($form_id, $form_values) {
153 // Create a dummy node to pass along to CCK.
154 $node = new stdClass();
155 $node->type = bio_get_type();
156 foreach ($form_values as $field_name => $value) {
157 if (preg_match('/^field_/', $field_name)) {
158 $node->$field_name = $form_values[$field_name];
159 }
160 }
161
162 // Call validation routines on the CCK fields.
163 content_validate($node);
164 }
165
166 /**
167 * Submit handler for user registration form. Automatically creates a bio node
168 * on registration if any bio fields are set to show on the registration form.
169 */
170 function bio_user_register_submit($form_values) {
171 // Create bio node for this user.
172 $node = new StdClass;
173 $node->type = bio_get_type();
174 $node->uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $form_values['name']));
175 $node->title = $form_values['name'];
176 $node->name = $form_values['name'];
177 node_object_prepare($node);
178
179 $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
180 foreach (array('status', 'promote', 'sticky') as $key) {
181 $node->$key = in_array($key, $node_options);
182 }
183 // Always use the default revision setting.
184 $node->revision = in_array('revision', $node_options);
185
186 foreach ($form_values as $field_name => $value) {
187 if (preg_match('/^field_/', $field_name)) {
188 $node->$field_name = $form_values[$field_name];
189 }
190 }
191
192 // Create the node.
193 $node = node_submit($node);
194 node_save($node);
195
196 // Give us a nice log message.
197 if ($node->nid) {
198 watchdog('content', t('Bio: added %user bio upon registration.', array('%user' => $node->name)), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
199 }
200 }
201
202 /**
203 * Implementation of hook_profile_alter().
204 */
205 function bio_profile_alter(&$account, &$fields) {
206 // Replace user profile with user bio.
207 if (variable_get('bio_profile_takeover', 0) && $bio = bio_for_user($account->uid)) {
208 $bio = node_load($bio);
209 $typename = node_get_types('name', bio_get_type());
210 foreach ($fields as $key => $val) {
211 if ($key != $typename) {
212 unset($fields[$key]);
213 }
214 }
215 $account->name = $bio->title;
216 }
217 }
218
219 /**
220 * Implementation of hook_node_info().
221 */
222 function bio_node_info() {
223 if (bio_get_type() == 'bio') {
224 // Create a default bio type that's as simple as possible.
225 return array(
226 'bio' => array(
227 'name' => t('Biography'),
228 'module' => 'node',
229 'has_title' => TRUE,
230 'has_body' => TRUE,
231 'custom' => TRUE,
232 'modified' => TRUE,
233 'locked' => FALSE,
234 ),
235 );
236 }
237 }
238
239 /**
240 * Implementation of hook_nodeapi().
241 */
242 function bio_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
243 if ($node->type != bio_get_type()) return;
244
245 switch ($op) {
246 case 'validate':
247 // Ensure this user doesn't already have a bio node.
248 $account = user_load(array('name' => $node->name));
249 $nid = bio_for_user($account->uid);
250 if ($nid && ($nid != $node->nid)) {
251 form_set_error('name', t('This user already has a @bio. Edit it <a href="@link">here</a> or assign this entry to another user.', array('@bio' => node_get_types('name', $node), '@link' => url('node/'. $nid .'/edit'))));
252 }
253 break;
254 case 'insert':
255 // Record user's bio in bio table.
256 db_query('INSERT INTO {bio} (nid, uid) VALUES (%d, %d)', $node->nid, $node->uid);
257 break;
258 case 'delete':
259 // Remove the user's entry from the bio table when the user is deleted.
260 db_query('DELETE FROM {bio} where nid = %d', $node->nid);
261 break;
262 }
263 }
264
265 /**
266 * Implementation hook_user().
267 */
268 function bio_user($op, &$edit, &$account, $category = NULL) {
269 // If there's no bio for this user, nothing to do here.
270 if ($op != 'register' && $op != 'insert' && !$nid = bio_for_user($account->uid)) {
271 return;
272 }
273
274 switch ($op) {
275 case 'view':
276 // Add bio to main user profile page, if option is enabled and bio is accessible.
277 if (variable_get('bio_profile', 0) && node_access('view', $node = node_load($nid))) {
278 $name = node_get_types('name', bio_get_type());
279 $bio[$name]['bio']['value'] = node_view($node, FALSE, TRUE, FALSE);
280 return $bio;
281 }
282 break;
283
284 case 'delete':
285 node_delete($nid);
286 break;
287
288 case 'update':
289 // Publish or unpublish the bio node on changes to the user's status
290 // (active vs. blocked).
291 if (isset($edit['status']) && $edit['status'] != $account->status) {
292 $node = node_load($nid);
293 $node->status = (int)$edit['status'];
294 node_save($node);
295 }
296 break;
297
298 case 'register':
299 // Display CCK fields on the user registration form, if they've been
300 // marked as such.
301 return bio_user_register_form();
302 break;
303
304 case 'insert':
305 return bio_user_register_submit($edit);
306 break;
307 }
308 }
309
310 /**
311 * Implementation of hook_link().
312 */
313 function bio_link($type, $node = NULL, $teaser = FALSE) {
314 if ($type == 'node' && $node->type != bio_get_type()) {
315 // Add "View *username*'s biography" link to nodes.
316 $bio_link = variable_get('bio_link', array($node->type => 1));
317 if ($bio_link[$node->type] && ($nid = bio_for_user($node->uid))) {
318 $account = user_load(array('uid' => $node->uid));
319 return array(
320 'bio' => array(
321 'title' => t('by @user', array('@user' => $account->name)),
322 'href' => 'node/'. $nid,
323 'attributes' => array(
324 'title' => t('View @user\'s @bio.', array('@user' => $account->name, '@bio' => node_get_types('name', bio_get_type()))),
325 ),
326 ),
327 );
328 }
329 }
330 }
331
332 /**
333 * Find bio for given user.
334 *
335 * @param $uid
336 * User ID.
337 * @return
338 * Node ID of bio, or FALSE if no bio node was found.
339 */
340 function bio_for_user($uid = NULL) {
341 if (is_null($uid)) {
342 global $user;
343 $uid = $user->uid;
344 }
345
346 return db_result(db_query('SELECT nid FROM {bio} WHERE uid = %d', $uid));
347 }
348
349 /**
350 * Menu callback; administration settings page.
351 */
352 function bio_settings() {
353 // Bio node type.
354 $types = array();
355 foreach (node_get_types() as $key => $type) {
356 $types[$key] = $type->name;
357 }
358 $form['bio_nodetype'] = array(
359 '#type' => 'select',
360 '#title' => t('Content type for user biographies'),
361 '#description' => t('The content type for user biographies. Each user may create only one node of this type'),
362 '#options' => $types,
363 '#default_value' => bio_get_type(),
364 );
365
366 // Link to author's bio.
367 $form['bio_link'] = array(
368 '#type' => 'checkboxes',
369 '#title' => t('Display bio link'),
370 '#description' => t("Display a link to author's bio if it is available."),
371 '#options' => $types,
372 '#default_value' => variable_get('bio_link', $types),
373 );
374
375 // Bio profile options.
376 $form['bio_profile'] = array(
377 '#type' => 'checkbox',
378 '#title' => t('Use bio for user profiles'),
379 '#description' => t('View, edit, and display biography information on the user account page.'),
380 '#default_value' => variable_get('bio_profile', 0),
381 );
382 $form['bio_profile_takeover'] = array(
383 '#type' => 'checkbox',
384 '#title' => t('Takeover profile.'),
385 '#description' => t('Display nothing but the bio node on the user profile page.'),
386 '#default_value' => variable_get('bio_profile_takeover', 0),
387 );
388
389 // Show fields on the registration form.
390 if (module_exists('content')) {
391 $form['bio_regstration_form'] = array(
392 '#type' => 'radios',
393 '#title' => t('Show fields on registration form'),
394 '#options' => array(t('Disabled'), t('Enabled')),
395 '#default_value' => variable_get('bio_regstration_form', 0),
396 '#description' => t('Enable this option to display bio fields on the user registration form. This will automatically create a bio record for a user when they register.'),
397 );
398
399 // Determine the options and default values.
400 $fields = _bio_get_fields();
401 $default_values = variable_get('bio_regstration_form_fields', array());
402 foreach ($fields as $field_name => $properties) {
403 $options[$field_name] = check_plain($properties['widget']['label']);
404 // Required fields are always shown on registration form.
405 if ($properties['required'] || !empty($default_values[$field_name])) {
406 $default_values[$field_name] = $field_name;
407 }
408 else {
409 $default_vales[$field_name] = 0;
410 }
411 }
412 // Display list of fields.
413 $form['bio_regstration_form_fields'] = array(
414 '#type' => 'checkboxes',
415 '#title' => t('Registration form fields'),
416 '#options' => $options,
417 '#default_value' => $default_values,
418 '#description' => t('Fields checked here will be displayed on the user registration form. Required fields are always shown.'),
419 '#theme' => 'bio_registration_fields',
420 );
421 }
422
423 // Strange hack for invalidating Views cache.
424 $add_a_submit = system_settings_form($form);
425 $add_a_submit['#validate']['bio_settings_validate_xxx'] = array();
426 return $add_a_submit;
427 }
428
429 /**
430 * Theme function for bio registration form options that adds a disabled flag
431 * to required fields.
432 *
433 * @ingroup themeable
434 */
435 function theme_bio_registration_fields($form) {
436 $fields = _bio_get_fields();
437 foreach (element_children($form) as $field_name) {
438 // Disable required fields; they always show up.
439 if ($fields[$field_name]['required']) {
440 $form[$field_name]['#attributes'] = array('disabled' => 'disabled');
441 $form[$field_name]['#value'] = $field_name;
442 }
443 }
444
445 return drupal_render($form);
446 }
447
448 /**
449 * Invalidate Views cache when bio settings form is submitted.
450 *
451 * This is required in case the bio node type has changed. We need the _xxx
452 * suffix to keep it from interfering with the #base in system_settings_form().
453 * Also, we do it in the validate phase because submits were interfering with
454 * the #base attribute. Yuck.
455 *
456 * @todo Less hackish way to do this?
457 */
458 function bio_settings_validate_xxx($form_id, $form_values) {
459 if (module_exists('views')) {
460 views_invalidate_cache();
461 }
462 }
463
464 /**
465 * Retrieve field info for Bio CCK type.
466 */
467 function _bio_get_fields() {
468 $bio_nodetype = bio_get_type();
469 $type = content_types($bio_nodetype);
470 return $type['fields'];
471 }
472
473 /**
474 * Short-hand function to return bio node type.
475 *
476 * @return $string
477 * Node type short name currently assigned as the Bio type.
478 */
479 function bio_get_type() {
480 return variable_get('bio_nodetype', 'bio');
481 }
482
483 /*
484 * Views integration for Bio module.
485 *
486 * Bio module's Views integration works by cloning many features of Views
487 * module's existing node integration. To avoid a large amount of code being
488 * loaded on every page, these functions therefore merely call private
489 * functions in bio_views.inc.
490 */
491
492 /**
493 * Implementation of hook_views_table_alter().
494 *
495 * @see _bio_views_tables_alter()
496 */
497 function bio_views_tables_alter(&$tables) {
498 require_once drupal_get_path('module', 'bio') .'/bio_views.inc';
499 return _bio_views_tables_alter($tables);
500 }
501
502 /**
503 * Copy of views_handler_filter_isnew($op, $filter, $filterinfo, &$query)
504 *
505 * @see _bio_handler_filter_isnew()
506 */
507 function bio_handler_filter_isnew($op, $filter, $filterinfo, &$query) {
508 require_once drupal_get_path('module', 'bio') .'/bio_views.inc';
509 return _bio_handler_filter_isnew($op, $filter, $filterinfo, $query);
510 }
511
512 /**
513 * Implementation of hook_views_query_alter().
514 *
515 * @see _bio_views_query_alter()
516 */
517 function bio_views_query_alter(&$query, $view, $summary, $level) {
518 require_once drupal_get_path('module', 'bio') .'/bio_views.inc';
519 return _bio_views_query_alter($query, $view, $summary, $level);
520 }
521
522 /**
523 * Implementation of hook_views_default_views().
524 *
525 * @see _bio_views_default_views()
526 */
527 function bio_views_default_views() {
528 require_once drupal_get_path('module', 'bio') .'/bio_views.inc';
529 return _bio_views_default_views();
530 }
531
532 /**
533 * Implementation of hook_views_tables().
534 *
535 * @see _bio_views_tables()
536 */
537 function bio_views_tables() {
538 require_once drupal_get_path('module', 'bio') .'/bio_views.inc';
539 return _bio_views_tables();
540 }

  ViewVC Help
Powered by ViewVC 1.1.2