<?/*
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_once 'person.php';
  require_once 'server_functions.php';

  globalize_request_keys('last_name', 'first_name');

  ## this is a bit of a security issue...
  if (!$crm_id) $crm_id = $REMOTE_USER_CRM_ID;
  if (array_key_exists('crm_id', $_REQUEST)) $crm_id = $_REQUEST['crm_id'];

  $person = new Person ();

  $limited = 0;

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

  $c = new CRM();
  $r = $c->Select_by_CRM_ID($crm_id);

  $crm_name = $r->Get('crm_name');
?>

<div class="athenatitleshadow">Add Existing People to <?php echo $crm_name; ?><div class="athenatitle">Add Existing People to <?php echo $crm_name; ?></div></div>
<br />

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="crm_id" value="<?php echo $crm_id; ?>" />

Searches are case insensitive.  Use % for wildcards, this is a substring search.

<table border="0">
<tr>
  <td>Last Name:</td>
  <td><input type="text" name="last_name" value="<?php echo $last_name; ?>" /></td>
</tr>
<tr>
  <td>First Name:</td>
  <td><input type="text" name="first_name" value="<?php echo $first_name; ?>" /></td>
</tr>
<tr>
  <td colspan="2"><input type="submit" name="submit" value="Search" /></td>
</tr>
</table>

</form>

<?php

  if ($last_name or $first_name) {
    $matches = $person->Find ($last_name, $first_name, $crm_id, $limited);

    if (is_null($matches)) {
      echo "<pre>\nadd_crm_people.php: " . $person->error . "</pre>\n";
      return;
    }
  } else {
    $matches = false;
  }

  if ($matches) {
?>

<form action="add_crm_people_2" method="post">
<input type="hidden" name="crm_id" value="<?php echo $crm_id; ?>" />

Add the following to this RMS (<?= htmlentities($crm_name) ?>):<br />
<br />

<table border="0">
<tr>
  <th>Add</th>
  <th>Person ID</th>
  <th align="left">Name</th>
  <th align="left">RMS</th>
  <th align="left">Person Type</th>
</tr>

<?php
  $prev_id = NULL;

  foreach ($matches as $r) {
    print "<tr>\n";

    if ($prev_id == $r['person_id']) {
      print <<<END
  <td colspan="3">&nbsp;</td>
END;
    } else {
      if (strlen($r['name']) > 27) $r['name'] = substr($r['name'],0,27).'...';
      $name = htmlentities (substr($r['name'],0,30));
      print <<<END
  <td align="center"><input type="checkbox" name="add_person_id[{$r['person_id']}]" value="$name" /></td>
  <td align="center">{$r['person_id']}</td>
  <td>$name</td>
END;
    }

    if (!array_key_exists('person_type', $r)) $r['person_type'] = '';

    print <<<EOF
  <td>{$r['crm_name']}</td>
  <td>{$r['person_type']}</td>
EOF;
    print "</tr>\n";

    $prev_id = $r['person_id'];
  }
?>

<tr>
  <td colspan="3"><input type="submit" name="submit" value="Submit" /></td>
</tr>

</table>

</form>

<?php } ?>
