<?/*
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 $web_page_id = $RUSER_REC['WEB_PAGE_ID']; ?>

<div class="athenatitleshadow">Attributes for your RMS (web page id: <?php echo $web_page_id; ?>)<div class="athenatitle">Attributes for your RMS (web page id: <?php echo $web_page_id; ?>)</div></div>

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

<ul>
  <li>Click the ID to edit the attribute's elements.</li>
  <li>Order and Display Type don't matter unless you are using the show_page function in Attribute_Display.</li>
</ul>

<?php
  require_once 'attribute.php';
  require_once 'value_group.php';
  require_once 'attribute_display.php';
  require_once 'server_functions.php';

  globalize_request_keys('del', 'add_att_name', 'submit', 'add_att_disp_type',
    'attinfo');

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

  $errors = array ();
  $changes = 0;

  if (count($del)) {
    foreach ($del as $d => $v) {
      $continue = 1;

##
## removed because I'm not sure if we should be deleting all of the data
##
/*
      $group_id = $att->get_group_id ($d);
      $mem_list = $vgm->By_Value_Group ($group_id);

      ## del memebers
      foreach ($mem_list as $r) {
        $ok = $vgm->Delete ($r[member_id]);

        if (!$ok) {
          $continue = 0;
          $errors[] = "Unable to delete member ($r[member_id]).";
        }
      }

      ## del group
      if ($continue) {
        $ok = $vg->Delete ($group_id);

        if (!$ok) {
          $continue = 0;
          $errors[] = "Unable to delete value group ($group_id).";
        }
      }
*/

      ## del from web page list
      if ($continue) {
        $ok = $att->Delete_From_Page ($d, $web_page_id);
        if (!$ok) {
          $continue = 0;
          $errors[] = "Unable to delete attribute from web page map ($d/$web_page_id).";
        }
      }

      ## del att
      if ($continue) {
        $ok = $att->Delete ($d);
        if (!$ok) $errors[] = "Unable to delete attribute ($d).";
      }
    }
  }

  $att_list = $att->atts_by_page_id ($web_page_id);

  if ($add_att_name) {
    $vgid = $vg->Add ($add_att_name, '', $REMOTE_USER);

    if ($vgid) {
      $att_id = $att->Add ($add_att_name, '', $REMOTE_USER, 'Service', $vgid);

      if ($att_id) {
        $ok = $att->Add_To_Page ($att_id, $web_page_id, $add_att_disp_type, '', $REMOTE_USER);

        if ($ok) {
          $changes = 1;
        } else {
          $errors[] = "Unable to create Attribute/Page mapping:<pre>{$att->error})</pre>";
        }

      } else {
        $errors[] = "Unable to create attribute.";
      }

    } else {
      $errors[] = "Unable to create value group:<pre>{$vg->error}</pre>.";
    }
  }

  if ($submit) {

    foreach ($att_list as $row) {
      list ($att_id, $disp_id, $name, $order) = $row;

      $new = $attinfo[$att_id];

      if ($name != $new['name']) {
        $changes = 1;
        $ok = $att->Update ($att_id, $new['name'], '', $REMOTE_USER);
        if (!$ok) $errors[] = "Unable to save name changes to attribute ($att_id).";
      }

      if (($disp_id != $new['disp']) or
          ($order != $new['order'])) {
        $changes = 1;
        $ok = $att->Update_Page_Att ($att_id, $web_page_id, $new['disp'], 
	  $new['order'], $REMOTE_USER);
        if (!$ok) $errors[] = "Unable to save display type changes to attribute ($att_id).";
      }
    }
  }

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

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

  $display_types = $ad->display_types ();
?>

<table border="0">
<tr>
  <th>Del</th>
  <th>ID</th>
  <th>Order</th>
  <th>Name</th>
  <th>Display Type</th>
</tr>

<?php
  foreach ($att_list as $row) {
    list ($att_id, $disp_id, $name, $order) = $row;
    $name = htmlentities ($name);
    $dd = $ad->Multi_Array_Drop_Down ($display_types, $disp_id);

    print <<<EOF
<tr>
  <td><input type="checkbox" name="del[$att_id]" value="$att_id" /></td>
  <td><a href="details?att_id=$att_id">$att_id</a></td>
  <td><input type="text" name="attinfo[$att_id][order]" value="$order" size="3" /></td>
  <td><input type="text" name="attinfo[$att_id][name]" value="$name" size="40" /></td>
  <td><select name="attinfo[$att_id][disp]">$dd</select></td>
</tr>
EOF;
  }

  $disp_type_dd = $ad->Multi_Array_Drop_Down ($display_types);
?>

<tr>
  <td colspan="3">Add</td>
  <td><input type="text" name="add_att_name" size="40" value="" />
  <td><select name="add_att_disp_type"><?php echo $disp_type_dd; ?></select></td>
</tr>
<tr>
  <td colspan="5" align="center"><input type="submit" name="submit" value="Update" /></td>
</tr>
</table>

</form>

