<?/*
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

##
## this library now assumes the $match value is a ticket_id
##

require_once 'attribute.php';

class Attribute_Display extends Attribute {
  function Attribute_Display () {
    global $dbi;
    $this->db = &$dbi;

    $this->attv = new Attribute_Value ();

    $this->error = "";
  }

  function show_search_page ($page_id, $match='') {
    $recs = $this->atts_by_page_id( $page_id, 1 );

    $text = array ();

    foreach ($recs as $r) {
      $att_id = $r[attribute_id];
      $disp_id = $r[web_display_id];

      $text[$i] .= $this->attribute_name ($att_id)."<br />\n";

      if (in_array ($disp_id, array (1,2,3,4))) {
        $text[$i] .= $this->drop_down ($att_id, 1, $match);
      } elseif ($disp_id == 5) {
        $text[$i] .= $this->text ($att_id, $match, 1);
      }
    }

    $html = join ("<br />\n", $text);

    return $html;
  }

  function show_page ($page_id, $match='') {
    $recs = $this->atts_by_page_id( $page_id, 1 );

    $text = array ();

    foreach ($recs as $r) {
      $att_id = $r[attribute_id];
      $disp_id = $r[web_display_id];

      $text[$i] .= $this->attribute_name ($att_id)."<br />\n";

      if ($disp_id == 1) {
        $text[$i] .= $this->radio_buttons ($att_id, $match);

      } elseif ($disp_id == 2) {
        $text[$i] .= $this->checkboxes ($att_id, $match);

      } elseif ($disp_id == 3) {
        $text[$i] .= $this->drop_down ($att_id, 0, $match);

      } elseif ($disp_id == 4) {
        $text[$i] .= $this->drop_down ($att_id, 1, $match);

      } elseif ($disp_id == 5) {
        $text[$i] .= $this->text ($att_id, $match);
      }
    }

    $html = join ("<br />\n", $text);

    return $html;
  }

  function display_types () {
    $sql = '
      select web_display_id,
             display_description
        from web_display_types
    order by upper(display_description)
    ';

    return $this->db->getAll($sql);
  }

  function att_title ($att_id) {
    return $this->attribute_name ($att_id);
  }

  function init_att_info ($att_id, $match='', $ai=0) {
    $this->r = $this->members_by_att_id ($att_id);

    $this->av_set = $this->attv->members_set_by_att ($att_id, $match, $ai);

    if (!$this->av_set) { $this->av_set = array (); }
  }


  function drop_down ($att_id, $multi=0, $match='',$extra_option=0) {
    if ($multi) {
      if ($multi == 1) $multi = 4;
      $multiple = ' multiple="multiple" size="'.$multi.'" ';
    }

    $html = '';

    if ($this->Editable()) {
      $html .= "<select class=\"textsm\" name=\"atts[$att_id][option_id][]\" $multiple>\n";

      if ($extra_option == 1) {
        $html .= "<option value=\"\"></option>\n";
      } elseif ($extra_option == 2) {
        $html .= "<option value=\"\">Select</option>\n";
      }
    }

    $this->init_att_info ($att_id, $match);

    foreach ($this->r as $rec) {
      $mem_id = $rec[member_id];

      if (in_array ($mem_id, $this->av_set)) {
        $this->set_match ($mem_id);
      } else {
        $this->set_match ('');
      }
      $html .= $this->generate_option ($mem_id, $rec[member_name]);
    }

    if ($this->Editable()) $html .= "</select>";

    return $html;
  }


  function radio_buttons ($att_id, $match='') {
    return $this->buttons_and_boxes ('radio', $att_id, $match);
  }

  function checkboxes ($att_id, $match) {
    return $this->buttons_and_boxes ('checkbox', $att_id, $match);
  }

  function buttons_and_boxes ($type, $att_id, $match='') {
    if (!in_array ($type, array ('radio', 'checkbox'))) {
      return "Invalid Type";
    } 

    $this->init_att_info ($att_id, $match);

    $html = '';

    if ($type == 'checkbox') {
      $html .= "<input type=\"hidden\" name=\"atts[$att_id][]\" value=\"1\" />";
    }

    ## put in logic here to detect row length and wrap if necessary?
    foreach ($this->r as $rec) {
      $mem_id = $rec[member_id];

      if (in_array ($mem_id, $this->av_set)) {
        $check = ' checked="checked" ';
      } else {
        $check = '';
      }

      if (isset($this->edit_flag) and !$this->Editable ()) {
        if ($check) $html .= $rec[member_name]."\n";
        continue;
      }

      $html .= "<input class=\"textsm\" type=\"$type\" name=\"atts[$att_id][option_id][]\" value=\"$mem_id\" $check /> $rec[member_name]\n";
    }

    $html .= "\n";

    return $html;
  }

  ## expects only 1 member for the attribute
  function text ($att_id, $match='', $small=0) {
    $r = $this->members_by_att_id ($att_id);

    $this->init_att_info ($att_id, $match, 1);

    if (isset($this->edit_flag) and !$this->Editable ()) {
      $html .= "{$this->av_set[$mem_id]}\n";

    } else {
      $mem_id = $this->r[0][member_id];
  
      $html .= <<<HERETO
<input type="hidden" name="atts[$att_id][option_id][]" value="$mem_id" />
HERETO;

      if ($small) {
        $html .= <<<HERETO
<input class="textsm" type="text" name="atts[$att_id][ai]" value="{$this->av_set[$mem_id]}" />
HERETO;
      } else {
        $html .= <<<HERETO
<textarea class="textsm" name="atts[$att_id][ai]" rows="6" cols="60">{$this->av_set[$mem_id]}</textarea>
HERETO;
      }
    }

    return $html;
  }
}

?>
