Location: PHPKode > projects > Teampopor > teampopor-0.5/teampopor/modules/network/network.pages.inc
<?php

/**
 * page callback: network/%network
 */
function network_page($network) {
  $_SESSION['nid'] = $network->nid;

  return theme('network', $network);
}

/**
 * page callback: network/%network/js/%
 */
function network_js($network, $action) {
  switch ($action) {
    case 'typeahead':
      switch (arg(4)) {
        case 'tags':
          $matches = array();
          $search = trim(arg(5));
          if (!empty($search)) {
            $result = db_query_range("SELECT * FROM {tags} WHERE nid = %d AND name LIKE '%s%%'", $network->nid, $search, 0, 10);
            while ($tag = db_fetch_object($result)) {
              $matches[$tag->name] = $tag->name;
            }
          }
          return json($matches);
      }
    case 'select-file':
      $output = theme('network_files', $network);
      $output .= '<a id="select-file" class="btn-primary" href="#">' . t('Attach') . '</a>';
      return json($output);
  }
}

/**
 * network_admin_form
 */
function network_admin_form($form_state, $network) {
  $form['#attributes']['class'] = 'standard-form stacked';
  $form['network'] = array('#type' => 'value', '#value' => $network);

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Network Name'),
    '#required' => TRUE,
    '#default_value' => $network->name,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $network->description,
  );
  $form['anyone_can_invite'] = array(
    '#type' => 'radios',
    '#title' => t('Network Privacy'),
    '#options' => array(
      0 => t('Only admins can invite new members.'),
      1 => t('All members can invite new members.'),
    ),
    '#default_value' => $network->anyone_can_invite,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

function network_admin_form_submit($form, &$form_state) {
  network_save($form_state['values']['network'], $form_state['values']);
  set_message(t('Your network information has been updated.'), 'success');
}

/**
 * create_network_form
 */
function create_network_form($form_state, $network = NULL) {
  $form['#attributes']['class'] = 'standard-form stacked';
  $form['network'] = array('#type' => 'value', '#value' => $network);

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Network Name'),
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
  );
  $form['anyone_can_invite'] = array(
    '#type' => 'radios',
    '#title' => t('Permissions'),
    '#options' => array(
      1 => '<strong>' . t('Open') . '</strong> - <small>' . t('All members can invite new members') . '</small>',
      0 => '<strong>' . t('Closed') . '</strong> - <small>' . t('Only Admins can invite new members.') . '</small>',
    ),
    '#default_value' => 1,
  );
  /*$form['is_moderated'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow %domain members to join without an invite'),
  );*/

  $form['submit'] = array('#type' => 'submit', '#value' => t('Create Network'));

  return $form;
}

function create_network_form_validate($form, &$form_state) {
  if (network_load($form_state['values']['name'])) {
    form_set_error('name', t('The name %name is already taken.', array('%name' => $form_state['values']['name'])));
  }
}

function create_network_form_submit($form, &$form_state) {
  $current_network = $form_state['values']['network'];
  $network = network_save(NULL, array(
    'cid' => $current_network->cid,
    'name' => $form_state['values']['name'],
    'description' => $form_state['values']['description'],
    'is_external' => 1,
    'anyone_can_invite' => $form_state['values']['anyone_can_invite'],
  ));
}

/**
 * send_invitations_form
 */
function send_invitations_form($form_state, $network = NULL) {
  $form['#attributes']['class'] = 'standard-form stacked';
  $form['network'] = array('#type' => 'value', '#value' => $network);

  $form['emailRecipients'] = array(
    '#type' => 'textarea',
    '#required' => TRUE,
  );
  $form['submit'] = array('#type' => 'submit', '#value' => t('Invite'));

  return $form;
}

function send_invitations_form_validate($form, &$form_state) {
  $network = $form_state['values']['network'];

  $recipients = $form_state['values']['emailRecipients'];
  $recipients = explode(',', $recipients);
  $mails = array();
  foreach ($recipients as $recipient) {
    $mail = trim($recipient);
    if (!empty($mail)) {
      $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
      $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
      $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
      $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
      if (!preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail, $matches)) {
        form_set_error('emailRecipients', t('%mail is not a valid email address.', array('%mail' => $mail)));
      }
      if (!$network->is_external) {
        if ($d = domain_load(array('cid' => $network->cid))) {
          if ($matches[1] != $d->name) {
            form_set_error('emailRecipients', t('%mail is not a valid company email address.', array('%mail' => $mail)));
          }
        }
      }
      $mails[] = $mail;
    }
  }
  if (empty($mails)) {
    form_set_error('emailRecipients', t('Please input valid email addresses.'));
  }

  $form_state['values']['mails'] = $mails;
}

function send_invitations_form_submit($form, &$form_state) {
  $network = $form_state['values']['network'];
  global $user;
  $language = user_preferred_language($user);

  $already_members = array();
  $send_users = array();
  foreach ($form_state['values']['mails'] as $mail) {
    if ($u = user_load(array('mail' => $mail))) {
      if (network_member_load(array('nid' => $network->nid, 'uid' => $u->uid))) {
        $already_members[] = $mail;
        break;
      }
    }

    if (!($invite = invite_load(array('uid' => $user->uid, 'nid' => $network->nid, 'mail' => $mail)))) {
      $invite = invite_save(NULL, array(
        'nid' => $network->nid,
        'mail' => $mail,
      ));
    }

    $params = array();
    $params['network'] = $network;
    $params['invite'] = $invite;
    drupal_mail('network', 'send_invitations', $mail, $language, $params);
    $send_users[] = $mail;
  }

  if (!empty($already_members)) {
    set_message(t('These users is already a network member: !users', array('!users' => implode(', ', $already_members))));
  }
  if (!empty($send_users)) {
    set_message(t('Invitations has been send to !users', array('!users' => implode(', ', $send_users))), 'success');
  }
}

Return current item: Teampopor