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

  globalize_request_keys('del', 'submit', 'add_mem_name', 'add_mem_ai', 'mems');

  $att = new Attribute ();
  $vgm = new Value_Group_Member ();
  $vg = new Value_Group ();

  $att_list = $att->atts_by_page_id ($RUSER_REC['WEB_PAGE_ID']);

  $att_id = $_REQUEST['att_id'];
  $valid_id = 0;

  foreach ($att_list as $row) {
    if ($row[0] == $att_id) $valid_id = 1;
  }

  if (!$att_id or !$valid_id) {
    print "You don't have permission to edit that attribute.\n";
    return;
  }

  $name = $att->attribute_name ($att_id);

  print <<<EOF
<form method="post" action="{$_SERVER['PHP_SELF']}">
<div class="athenatitleshadow">Attribute $att_id ($name)<div class="athenatitle">Attribute $att_id ($name)</div></div><br />
EOF;

  $errors = array ();

  ## do deletes first so they aren't in the check for update list
  if (count($del)) {
    foreach ($del as $d => $v) {
      $ok = $vgm->Delete ($d);

      if (!$ok) $errors[] = "Unable to delete member, data probably exists for it.";
    }
  }

  $group_id = $att->get_group_id ($att_id);
  $members = $att->members_by_group_id ($group_id);

  $changes = 0;

  if ($add_mem_name) {
    $ok = $vgm->Add ($group_id, $add_mem_name, $add_mem_ai, '', $REMOTE_USER);

    if (!$ok) $errors[] = "failed to add new member: <pre>\n"
      . htmlentities($vgm->error) . "</pre>\n";

    $changes = 1;
  }

  if ($submit) {

    foreach ($members as $row) {
      $id = $row['member_id'];

      $upd_name = $mems[$id]['name'];
      $upd_desc = $mems[$id]['desc'];

      if (($row['member_name'] != $upd_name) or
          ($row['member_description'] != $upd_desc)) {

        $changes = 1;
        $ok = $vgm->Update ($id, $upd_name, $upd_desc, $REMOTE_USER);

        if (!$ok) $errors[] = "failed to update \"$upd_name\" ($id).<pre>{$vgm->error}</pre>";
      }
    }

  }

  if ($changes) $members = $att->members_by_group_id ($group_id, 1);
?>

<input type="hidden" name="att_id" value="<?php echo $att_id; ?>" />

<a href=".">« Back to Attributes</a>
<br />
<br />

<?php
  if (count($errors)) {
    foreach ($errors as $e) {
      print "<font color=\"red\">$e</font><br />\n";
    }
  }
?>

<table border="0">
<tr>
  <th>Del</th>
  <th>ID</th>
  <th>Name</th>
  <th>Description</th>
</tr>

<?php
  foreach ($members as $row) {
    $id = $row['member_id'];
    $name = htmlentities ($row['member_name']);
    $desc = htmlentities ($row['member_description']);

    print <<<EOF
<tr>
  <td><input type="checkbox" name="del[$id]" value="$id" /></td>
  <td>$id</td>
  <td><input type="text" name="mems[$id][name]" value="$name" size="40" /></td>
  <td><input type="text" name="mems[$id][desc]" value="$desc" /></td>
</tr>
EOF;
  }
?>

<tr>
  <td colspan="2">Add</td>
  <td><input type="text" name="add_mem_name" size="40" value="" />
  <td><input type="text" name="add_mem_ai" value="" />
</tr>
<tr>
  <td colspan="4" align="center"><input type="submit" name="submit" value="Update" /></td>
</tr>
</table>

</form>

