You are here

function pmperson_validate in Drupal PM (Project Management) 7

Implements hook_validate().

File

pmperson/pmperson.module, line 358

Code

function pmperson_validate($node, $form, &$form_state) {
  if (!empty($node->user_name)) {
    $username = $node->user_name;
    if ($ass_user = user_load_by_name($username)) {
      $query = db_select('node', 'n');
      $query
        ->fields('n', array(
        'nid',
        'title',
      ))
        ->condition('stp.user_uid', $ass_user->uid)
        ->join('pmperson', 'stp', 'n.nid = stp.nid');
      $row = $query
        ->execute()
        ->fetchAssoc();
      if ($row && (empty($form['#node']->user_name) || $form['#node']->user_name != $node->user_name)) {
        $message = t('This Drupal user is already assigned to a Project Management person (!link). The same Drupal user can not be assigned to two Project Management people.', array(
          '!link' => l($row['title'], 'node/' . $row['nid']),
        ));
        form_set_error('user_name', $message);
      }
    }
    else {
      form_set_error('user_name', t('@username is not a current user', array(
        '@username' => $username,
      )));
    }
  }
}