<?/*
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
*/?>
<div class="athenatitleshadow">Create or Edit an RMS<div class="athenatitle">Create or Edit an RMS</div></div>
<br />

<?php print Sub_Heading ('Edit a Current Request Management Systems'); ?>
<br />
To change info for each RMS, switch to the RMS and go to the admin page.<br />

<?php
  require_once 'crm.php';
  require_once 'problem_type.php';
  require_once 'source.php';
  require_once 'company.php';
  require_once 'person.php';
  require_once 'server_functions.php';

  globalize_request_keys('email_source', 'web_path', 'users_url', 
    'default_problem', 'default_source', 'crm_short_name', 'crm_name',
    'company_id', 'prev_comp', 'web_domain', 'confirmation_url',
    'email_sig', 'extended_email_sig', 'email_domain');

  $crm = new CRM ();

  if (!$email_source) $email_source = 'Email';
  if (!$web_path) $web_path = '/';
  $web_path = preg_replace('/([^\/])$/', '$1/', $web_path);
  if (!$users_url) $users_url = 'user/browse/';
  if (!$default_problem) $default_problem = 'not categorized';
  if (!$default_source) $default_source = 'default';
  if (!$crm_short_name) $crm_short_name = $crm_name;

  $crm_short_name = preg_replace('/[^a-z0-9_]/', '', 
    strtolower(substr($crm_short_name, 0, 32)));

  if ($company_id and $crm_name and $crm_short_name and $default_problem and 
      $default_source and $email_source and $email_domain and $users_url and 
      $email_sig and $web_domain and $web_path) {

    $add = array (
      'company_id' 		    => $company_id,
      'crm_name' 		    => $crm_name,
      'crm_short_name' 		    => $crm_short_name,
      'default_problem_id' 	    => 1,
      'default_source_id' 	    => 1,
      'email_source_id'		    => 1,
      'default_support_person_id'   => 1,
      'default_escalated_to_id'     => 1,
      'email_domain' 		    => strtolower($email_domain),
      'web_domain' 		    => strtolower($web_domain),
      'web_path' 		    => $web_path,
      'users_url' 		    => $users_url,
      'email_sig' 		    => $email_sig,
      'extended_email_sig' 	    => $extended_email_sig,
      'confirmation_url' 	    => $confirmation_url,
    );

    $add['WEB_PAGE_ID'] = $dbi->nextId('web_page_id');

    $res = $dbi->query("
      insert into web_page (
        web_page_id,
        updated_by
      ) values (
        ?, ?
      )
    ", array($add['WEB_PAGE_ID'], $REMOTE_USER));
    if (DB::isError($res)) 
	die ('index1: ' . $res->getMessage() . $res->getUserInfo());

    $crm_id = $crm->Add ($add);
    if (!$crm_id) die('index2: ' . $crm->error);

    $person = new Person();

    $pcrmid = $person->Add_Map(array('person_id' => 1, 'crm_id' => $crm_id));
    if (!$pcrmid) die('index2.5: ' . $person->error);

    $pcrmtaid = $person->Add_Types(array('Support', 'Escalated Support'), 
      $pcrmid);
    if (!$pcrmtaid) die('index2.6: ' . $person->error);

    $src = new Source ();
    $pt = new Problem_Type ();

    $default_problem_id = $pt->Add ( array (
      'problem_type' => $default_problem,
      'crm_id' => $crm_id
    ));
    if (!$default_problem_id) die ('index3: ' . $pt->error);

    $default_source_id = $src->Add ( array (
      'source' => $default_source,
      'crm_id' => $crm_id
    ));
    if (!$default_source_id) die ('index4: ' . $src->error);

    $email_source_id = $src->Add ( array (
      'source' => $email_source,
      'crm_id' => $crm_id
    ));
    if (!$email_source_id) die ('index5: ' . $src->error);

    ## create problems & sources

    $crm_r = $crm->Select_by_CRM_ID($crm_id);
    if (!$crm_r) die('index6: ' . $crm->error);

    foreach (array (
      'default_problem_id',
      'default_source_id',
      'email_source_id',
    ) as $set) {
      $crm_r->Set ($set, $$set);
    }

    $crm_r->Set('default_support_person_id', 1);
    $crm_r->Set('default_escalated_to_id',   1);

    if (!$crm_r->Update()) die ('index7: ' . $crm_r->error);

    print "<br/><font color=\"red\">Be sure to update the <a href=\"${web_path}admin/edit_crm\">RMS settings</a> so everything works.</font><br/>";
  }

  print '<table cellpadding="0" cellspacing="0" border="0">'."\n";

  foreach ($crm->List_All () as $r) {
    if ($prev_comp != $r['company_id']) {
      if ($prev_comp) print "\n";
      print "<tr>\n <td><br /></td>\n</tr><tr class='supersubttyl'>\n <td>&nbsp;<b class='supersubttyl'>{$r['company_id']}</b>&nbsp;</td>\n</tr>\n";
    }

    print "<tr>\n <td><ul><li> <a href=\"crm_to_crm_access?crm_id={$r['crm_id']}\">{$r['crm_name']}</a>&nbsp;</li><ul></td>\n</tr>\n";

    $prev_comp = $r['company_id'];
  }

  $company = new Company ();

  $comps = $company->List_All ();

  for ($i=0; $i<count($comps); $i++) {
    $comps[$i][1] = $comps[$i][0].' - '.$comps[$i][1];
  }
?>
</table>
<br />
<?php print Sub_Heading ('Create Another Request Management System'); ?>
<br />
Check List for Adding a RMS:
<ul>
  <li>if using a dedicated hostname:
   <ol>
    <li>Add the hostname to DNS, and point the IP at the athena server</li>
    <li>if using qmail:
     <ol style="list-style-type: lower-alpha">
      <li>Edit <tt>/var/qmail/control/virtualdomains</tt> and add an entry
       of the form: <tt><i>domainname</i>:athena5</tt></li>
      <li>Make sure you have a <tt>/var/qmail/alias/.qmail-athena5-default</tt>
       file which contains: <tt>|preline /path/to/php/bin/php -q 
       /path/to/athena5/lib/handle_email.php</tt></li>
      <li>Restart qmail-send; if using the recommended installation directions 
	you can do this with <tt>svc -t /service/qmail-send</tt></li>
     </ol></li>
   </ol>
  </li>
  <li>if using addresses on an existing email domain:
   <ol>
    <li>if using qmail, then for each RMS, make a file of the form 
       <tt>/var/qmail/alias/.qmail-<i>RMS Short Name</i></tt> which contains:
       <tt>|preline /path/to/php/bin/php -q 
	/path/to/athena5/lib/handle_email.php</tt>  (After the first, you can
	use symlinks)</li>
   </ol>
  </li>
  <li>prepare the filesystem</li>
    <ul>
      <li><tt>ln -s /path/to/athena5/htdocs /path/into/your/webtree</tt></li>
    </ul>
  <li>Create RMS below</li>
  <li>Once added, add yourself to the new RMS by clicking the name of the new RMS above and click &quot;Assign People to this RMS&quot;</li>
  <li>Switch to the new RMS (<a href="../../support/change_crm">here</a>)</li>
  <li>Add some people, problems, sources (<a href="../">here</a>)</li>
  <li>Edit RMS settings to reflect the proper defaults (<a href="../edit_crm">here</a>)</li>
</ul>

<font color="red">All fields are required to add a new RMS.</font>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<table border="0">
<tr>
  <td>Customer:</td>
  <td><select name="company_id"><?php echo $company->Multi_Array_Drop_Down ($comps, $company_id); ?></select></td>
</tr>
<tr>
  <td>RMS name:</td>
  <td><input type="text" name="crm_name" value="<?php echo htmlentities($crm_name); ?>" /></td>
</tr>
<tr>
  <td>RMS Short name:</td>
  <td><input type="text" name="crm_short_name" value="<?php echo $crm_short_name; ?>" /> (must be unique: a-z, 0-9, and _ only)</td>
</tr>
<tr>
  <td>Default Problem Name:</td>
  <td><input type="text" name="default_problem" value="<?php echo $default_problem; ?>" /></td>
</tr>
<tr>
  <td>Default Source Name:</td>
  <td><input type="text" name="default_source" value="<?php echo $default_source; ?>" /></td>
</tr>
<tr>
  <td>Default Email Source:</td>
  <td><input type="text" name="email_source" value="<?php echo $email_source; ?>" /></td>
</tr>
<tr>
  <td>Email Domain:</td>
  <td><input type="text" name="email_domain" value="<?php echo $email_domain; ?>"/></td>
</tr>
<tr>
  <th colspan="2">Each RMS must have its own Web Domain/Web Path combination</th>
</tr>
<tr>
  <td>Web Domain:</td>
  <td><input type="text" name="web_domain" value="<?php echo $web_domain; ?>"/></td>
</tr>
<tr>
  <td>Web Path:</td>
  <td><input type="text" name="web_path" value="<?php echo $web_path; ?>"/>
   (must not be below another path on a given domain)</td>
</tr>
<tr>
  <td>Users URL:</td>
  <td><input type="text" name="users_url" value="<?php echo $users_url; ?>" /> shouldn't have to change this</td>
</tr>
<tr>
  <td>Confirmation URL:</td>
  <td><input type="text" name="confirmation_url" value="<?php echo $confirmation_url; ?>" /></td>
</tr>
<tr>
  <td>Email Signature:</td>
  <td><input type="text" name="email_sig" value="<?php echo $email_sig; ?>" /></td>
</tr>
<tr>
  <td colspan="2">Extended Email Signature:</td>
</tr>
<tr>
  <td colspan="2">(can be empty, only shown below regular email sig. on outbound emails)</td>
</tr>
<tr>
  <td colspan="2"><textarea name="extended_email_sig" cols="50" rows="8"><?php echo $extended_email_sig; ?></textarea></td>
</tr>
<tr>
  <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>

</form>
