You are here

function content_profile_nodeapi in Content Profile 6

Implementation of hook_nodeapi().

File

./content_profile.module, line 370

Code

function content_profile_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'prepare' && is_content_profile($node) && !isset($node->nid) && $node->uid && !user_access('administer nodes') && arg(0) != 'admin') {

    // Check if this nodetype already exists
    if ($nid = content_profile_profile_exists($node, $node->uid)) {

      // This node already exists, redirect to edit page
      drupal_goto('node/' . $nid . '/edit', 'destination=user/' . $node->uid);
    }
  }
  elseif ($op == 'validate' && is_content_profile($node) && user_access('administer nodes')) {
    $form = $a3;

    // Only validate if the user-name changed or we add a new node
    if (!empty($node->nid) && $form['author']['name']['#default_value'] == $node->name) {
      return;
    }

    //check whether the selected user has already a profile
    $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $node->name));
    if ($uid && content_profile_profile_exists($node, $uid)) {
      form_set_error('name', t('This user already has a content profile of this type. You can only create one profile per user.'));
    }
  }
  elseif ($op == 'prepare translation' && is_content_profile($node->translation_source)) {

    // Make sure the translated profile belongs to the same user.
    $node->uid = $node->translation_source->uid;
    $node->name = $node->translation_source->name;
  }
}