arg(1)))) { // // //houston, we have a user. // $items[] = array( // 'title' => t('%username\'s account', array('%username' => $user->name)), // 'path' => 'profile/' . $user->uid, // 'type' => MENU_DYNAMIC_ITEM, // 'callback' => 'profiles_as_nodes_page', // 'callback arguments' => array($user), // 'access' => user_access('access user profiles'), // ); // } // } // // return $items; // } /** * Implementation of hook_settings(). */ function profiles_as_nodes_settings() { $form['show_as_category'] = array( '#type' => 'checkbox', '#title' => t('Show as categories'), '#default_value' => variable_get('show_as_category', NULL), '#description' => t('Show the node types as sub-items under the "edit" local task'), ); $form['profiles_as_nodes_types'] = array(); foreach (node_get_types() as $type => $type_name) { $setting_name = $type . '_as_profile_node'; $setting = variable_get($setting_name, 0); $weight_name = $type . '_as_profile_node_weight'; $weight_value = variable_get($weight_name, array(10)); $fieldset = array( '#type' => 'fieldset', '#title' => $type_name, '#collapsible' => true, '#collapsed' => !$setting, ); $fieldset_name = 'fieldset_' . $type; $form['profiles_as_nodes_types'][$fieldset_name] = $fieldset; $setting = array( '#type' => 'checkbox', '#title' => t('enable as profile node'), '#default_value' => $setting, ); $form['profiles_as_nodes_types'][$fieldset_name][$setting_name] = $setting; $weight_form = array( '#type' => 'weight', '#title' => t('enable as profile node'), '#default_value' => $weight_value, ); $form['profiles_as_nodes_types'][$fieldset_name][$weight_name] = $weight_form; } // $form['profiles_as_nodes_rendering'] = array( // '#type' => 'radios', // '#title' => t('Link behaviour'), // '#default_value' => _profiles_as_nodes_get_types(), // '#options' => array( // 'link' => t('Links to nodes'), // 'inline' => t('Render inline')), // '#description' => t('Choose the method of rendering the profiles. A "link" is simple: your profile has a link to the profile node, or nodes. One link for each type. The "inline" option, will render the nodes, completely under the account page.'), // '#required' => TRUE); // $form['profiles_as_nodes_default'] = array( // '#type' => 'radios', // '#title' => t('Default type'), // '#default_value' => variable_get('profiles_as_nodes_default', NULL), // '#options' => node_get_types(), // '#description' => t('Choose the node types that render on the register screen.'), // '#required' => TRUE); return $form; } /** * Implementation of hook_user(). */ function profiles_as_nodes_user($op, &$edit, &$user, $type = NULL) { switch ($op) { case 'load': return profiles_as_nodes_load_profile(&$user); case 'view': return profiles_as_nodes_view_profile($user); case 'form': return profiles_as_nodes_form_profile($edit, $user, $type); case 'categories': if (variable_get('show_as_category', NULL)) { return profiles_as_nodes_categories(); } case 'register': //return profiles_as_nodes_render_form(); } } /** * This is not needed in the hook views, * But its very usefull for developing: You get all the nodes that are used for profiles with the user object; **/ function profiles_as_nodes_load_profile(&$user) { foreach (_profiles_as_nodes_get_types() as $type) { if ($type && $node = _profiles_as_nodes_get_node($user, $type)) { $user->nodes[$type] = _profiles_as_nodes_get_node($user, $type); } } } /** * Redirect to the node edit screen. KISS. */ function profiles_as_nodes_form_profile($edit, $user, $type) { if (_profiles_as_nodes_is_type_allowed($type)) { if ($node = _profiles_as_nodes_get_node($user, $type)) { //redirect ti the edit screen of the existing node drupal_goto('node/'. $node->nid .'/edit'); } else { drupal_goto('node/add/'. $type); } } } /** * Redirect to the specific node. */ function profiles_as_nodes_view_profile($user) { foreach (_profiles_as_nodes_get_types() as $type) { if ($type && $node = _profiles_as_nodes_get_node($user, $type)) { $name = node_get_name($node); $profile[t('Profile details')][] = array( 'title' => NULL, 'value' => l(t('view %name', array('%name' => $name)), 'node/'.$node->nid), 'class' => $type, ); } } return $profile; } function profiles_as_nodes_categories() { foreach (_profiles_as_nodes_get_weighted_types() as $weight => $type) { if ($type) { $node->type = $type; $name = node_get_name($node); $data[] = array('name' => $type, 'title' => ucfirst($name), 'weight' => $weight); } } return $data; } /** * implementation of form alter **/ function profiles_as_nodes_form_alter($form_id, &$form) { global $user; if (isset($form['type']) && strstr($form_id, '_node_form')) { foreach (_profiles_as_nodes_get_types() as $type) { if (strstr($form_id, $type) && //it is a node/add form for one of the profile types !$form['#node']->nid && //we are not looking at an node/xx/edit form $node = _profiles_as_nodes_get_node($user, $type)) { //user already has a node of this type) drupal_goto('node/'. $node->nid .'/edit'); } elseif (strstr($form_id, $type)) { $name = node_get_name($form['type']['#value']); $form['title'] = array( '#type' => 'hidden', '#value' => t('%user_name \'s %type', array('%type' => $name, '%user_name' => $user->name)), ); } } } } /** * Menu callback. Builds a page. * @param $user a user oject * @ingroup themable. **/ function theme_profiles_as_nodes_page($user) { if ($user->uid) { foreach (_profiles_as_nodes_get_weighted_types() as $type) { if($node = $user->nodes[$type]) { $output .= node_view($node, TRUE, FALSE, FALSE); } } return $output; } return ''; } /** * themes a node page. * @param $user a user oject * @param $type a string representing a node type used as profile. * @param $render_links Boolean. Whether or not the links with the nodes should * @param $title, provide a string if you wish to override the default node title, usefull whyen rendering inside context, like the account page. * be printed. * @ingroup themable. **/ function theme_profiles_as_nodes_page_by_type($user, $type, $render_links = FALSE, $title = FALSE) { if ($user->uid) { if (_profiles_as_nodes_is_type_allowed($type)) { if($node = $user->nodes[$type]) { $title ? $node->title = $title : NULL; return node_view($node, TRUE, FALSE, $render_links); } } } return ''; } /** * Implementation of hook_block **/ function profiles_as_nodes_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('Authors account'); $blocks[1]['info'] = t('My account'); return $blocks; case 'view': $block = array(); switch ($delta) { case 0: if ($viewed_user = profiles_as_nodes_get_user_by_context()) { $block = profiles_as_nodes_build_author_block($viewed_user); } break; case 1: $block = profiles_as_nodes_build_my_block(); break; } return $block; } } /** * Helper for hook_block **/ function profiles_as_nodes_build_author_block($viewed_user) { global $user; $block = array(); $block['subject'] = t('%user_name\'s profile', array('%user_name' => theme('username', $viewed_user))); // his/her account if (user_access('access user profiles')) { $items[] = l(t('%user_name\'s %type', array('%user_name' => $viewed_user->name, '%type' =>t('account information'))), 'user/' . $user->uid); } foreach (_profiles_as_nodes_get_weighted_types() as $type) { $path = ''; $name = node_get_name($type); if (($node = _profiles_as_nodes_get_node($viewed_user, $type)) && node_access('view', $node, $user->uid)) { //load the nodes from the viewed_user; $path = 'node/'. $node->nid; } if ($path) { $items[] = l(t('%user_name\'s %type', array('%user_name' => $viewed_user->name, '%type' => $name)), $path); } } if (count($items)) { $block['content'] = theme('profile_as_nodes_block', $items); } return $block; } /** * Helper for hook_block **/ function profiles_as_nodes_build_my_block() { $block = array(); global $user; $block['subject'] = t('My account'); // my account $items[] = l(t('my %type', array('%type' =>t('account information'))), 'user'); foreach (_profiles_as_nodes_get_weighted_types() as $type) { $path = ''; $name = node_get_name($type); $node = _profiles_as_nodes_get_node($user, $type); if (!$node) { if (node_access('create', $type, $user->uid)) { //I can create my profile_node, because I have rights to do so, and because I do not yet have one. $path = 'node/add/'.$type; } } elseif (node_access('view', $node, $user->uid)) { //provided we have permissions to do so, we want to view our nodes. $path = 'node/'. $node->nid; } if ($path) { $items[] = l(t('my %type', array('%type' => $name)), $path); } } if (count($items)) { $block['content'] = theme('profile_as_nodes_block', $items); } return $block; } /** * Private functions **/ function _profiles_as_nodes_get_node($user, $type) { return node_load(array('uid' => $user->uid, 'type' => $type)); } function _profiles_as_nodes_get_user($node) { $result = db_fetch_object(db_query("SELECT uid FROM {node} WHERE nid = %d LIMIT 1", $node->nid)); if ($result->nid) { return user_load(array('uid' => $result->uid)); } } /** * Check if the type is a profile node type */ function _profiles_as_nodes_is_type_allowed($type) { if ($type) { return in_array($type, _profiles_as_nodes_get_types(), TRUE); } } function _profiles_as_nodes_get_types() { $types = array(); foreach (node_get_types() as $type => $type_name) { $setting_name = $type . '_as_profile_node'; $setting = variable_get($setting_name, 0); if ($setting) { $types[] = $type; } } return $types; } function _profiles_as_nodes_get_weighted_types() { $types = array(); foreach (node_get_types() as $type => $type_name) { $setting_name = $type . '_as_profile_node'; $setting = variable_get($setting_name, 0); if ($setting) { $weight_name = $type . '_as_profile_node_weight'; $weight_value = variable_get($weight_name, array(10)); $types[$weight_value] = $type; } } ksort($types); return $types; } /* * Check if the page we're viewing is a profile page and if so, if it's ours ore someone elses. */ function profiles_as_nodes_is_profile_page($account_type) { global $user; if ($uid = profiles_as_nodes_get_uid_by_context()) { switch ($account_type) { case 'my': $ret_val = ($uid == $user->uid); break; default: $ret_val = ($uid != $user->uid); break; } } else { $ret_val = false; } return $ret_val; } /* * Obtain the user id of the profile page being viewed, if any. */ function profiles_as_nodes_get_uid_by_context() { if (!($uid = profiles_as_nodes_get_uid_by_user_path())) { $uid = profiles_as_nodes_get_uid_by_node_path(); } return $uid; } function profiles_as_nodes_get_user_by_context() { global $user; if ($uid = profiles_as_nodes_get_uid_by_context()) { $ret_val = ($uid == $user->uid) ? $user : user_load(array('uid' => $uid)); } else { $ret_val = NULL; } return $ret_val; } /* * Obtain user id of a 'profile as nodes' page. */ function profiles_as_nodes_get_uid_by_node_path() { $uid = 0; if (arg(0) == 'node' && (is_numeric(arg(1)) || in_array(arg(1), array('add', 'edit', 'delete', 'track')))) { if (is_numeric(arg(1))) { $node = node_load(arg(1)); $node_uid = $node->uid; $node_type = $node->type; } else { global $user; $node_uid = $user->uid; $node_type = arg(2); } if (_profiles_as_nodes_is_type_allowed($node_type)) { $uid = $node_uid; } } return $uid; } /* * Obtain user id of a user specific none 'profile as nodes' page. */ function profiles_as_nodes_get_uid_by_user_path() { $uid = 0; $regexps = array( 'user[^\/]', 'user\/([0-9]+)', 'gebruiker\/([0-9]+)', 'buddylist[^\/]', 'buddylist\/([0-9]+)', 'privatemsg[^\/]', 'privatemsg\/*', ); $paths_to_test[] = $_GET['q']; $paths_to_test[] = drupal_get_path_alias($_GET['q']); foreach ($paths_to_test as $path) { foreach ($regexps as $regexp) { if ($match = preg_match('/' . $regexp . '/', $path, $matches)) { break 2; } } } if ($match) { if ($matches[1]) { $uid = $matches[1]; } else { global $user; $uid = $user->uid; } } return $uid; } function theme_profile_as_nodes_block($links) { $output = ''; foreach ($links as $link) { $output .= theme('profile_as_nodes_block_link', $link); } if ($output) { $output = "\n\n"; } return $output; } function theme_profile_as_nodes_block_link($link, $children = NULL, $leaf = TRUE) { return '
  • '. $link . $children ."
  • \n"; }