<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php
  require 'dd.php';
  require_once 'person.php';
  require_once 'server_functions.php';

  globalize_request_keys('person_type', 'action', 'first_name',
    'last_name', 'middle_initial', 'email', 'home_phone', 'work_phone',
    'cell_phone', 'pager_num', 'address1', 'address2', 'city', 'state',
    'zip');

  $statedd = $dd->state($state);
  if (is_null($statedd)) pretty_die('person_add.php: ' . $dd->error);

  $person = new Person ();
  $p_rec = new Person_Record ();

  if (!$person->Is_Super_Admin ()) {
    return;
  }

  $person_types = count($person_type);

  if ($person_type) {
    $ptypes = $person_type;
  } else {
    $ptypes = array ('Support');
  }

  if (!$state) $state = 'CA';

  if (($action == 'Add') and $person_types and $first_name and $email) {
    $add = array (
      'crm_id' => $REMOTE_USER_CRM_ID,
    );

    foreach (array (
      'first_name',
      'last_name',
      'middle_initial',
      'email',
      'home_phone',
      'work_phone',
      'cell_phone',
      'pager_num',
      'address1',
      'address2',
      'city',
      'state',
      'zip',
    ) as $set) {
      $add[$set] = $$set;
    }

    $add['person_id'] = $person->Add ($add);

    if (!$add['person_id']) {
      print '<font color="red">Failed to create person: '
	  . htmlentities($person->error) . '.</font>';
      return;
    }

    $pcrm_map_id = $person->Add_Map ($add);

    if (!$pcrm_map_id) {
      print "<font color=\"red\">Failed to create person/rms map: <pre>\n"
	. htmlentities($person->error) . "\n</pre></font>\n";
      return;
    }

    $p_rec->Set_Types ($person_type, $pcrm_map_id);

    print "Support person added.";

    return;

  } elseif ($action == 'Add') {
    foreach (array ('person_types', 'first_name', 'email') as $set) {
      if (!$$set) print '<font color="red">Missing '.$set.'.</font><br />'."\n";
    }
  }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="athenatitleshadow">Add Support Person<div class="athenatitle">Add Support Person</div></div>
<br />

<table cellpadding="2" cellspacing="0">
<tr>
  <td class="text">First Name:</td>
  <td class="text"><input type="text" name="first_name" maxlength="255" value="<?php echo $first_name; ?>" /></td>
</tr>
<tr>
  <td class="text">Last Name:</td>
  <td class="text"><input type="text" name="last_name" maxlength="255" value="<?php echo $last_name; ?>" /></td>
</tr>

<tr>
  <td class="text">Middle Initial:</td>
  <td class="text"><input type="text" name="middle_initial" maxlength="1" value="<?php echo $middle_initial; ?>" /></td>
</tr>

<tr>
  <td class="text">Email:</td>
  <td class="text"><input type="text" name="email" maxlength="255" value="<?php echo $email; ?>" /></td>
</tr>

<tr>
  <td class="text">Home Phone:</td>
  <td class="text"><input type="text" name="home_phone" maxlength="50" value="<?php echo $home_phone; ?>" /></td>
</tr>

<tr>
  <td class="text">Work Phone:</td>
  <td class="text"><input type="text" name="work_phone" maxlength="50" value="<?php echo $work_phone; ?>" /></td>
</tr>

<tr>
  <td class="text">Cellular Phone:</td>
  <td class="text"><input type="text" name="cell_phone" maxlength="50" value="<?php echo $cell_phone; ?>" /></td>
</tr>

<tr>
  <td class="text">Pager:</td>
  <td class="text"><input type="text" name="pager_num" maxlength="50" value="<?php echo $pager_num; ?>" /></td>
</tr>

<tr>
  <td class="text">Address:</td>
  <td class="text"><input type="text" name="address1" value="<?php echo $address1; ?>" /></td>
</tr>

<tr>
  <td class="text"></td>
  <td class="text"><input type="text" name="address2" value="<?php echo $address2; ?>" /></td>
</tr>

<tr>
  <td class="text">City:</td>
  <td class="text"><input type="text" name="city" maxlength="50" value="<?php echo $city; ?>" /></td>
</tr>

<tr>
  <td class="text">State:</td>
  <td class="text"><select name="state"><?php echo $statedd;?></select></td>
</tr>

<tr>
  <td class="text">Zip:</td>
  <td class="text"><input type="text" name="zip" maxlength="50" value="<?php echo $zip; ?>" /></td>
</tr>

<tr>
  <td class="text">Person Type:</td>
  <td class="text"><select name="person_type[]" multiple><?php echo $dd->person_type ($ptypes, $person);?></select></td>
</tr>

<tr>
  <td colspan="2" align="center">
  <input type="submit" name="action" value="Add" />
  </td>
</tr>
</table>
</form>

