<?/*
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 'html.php';
require_once 'person.php';
require_once 'status.php';
require_once 'template.php';
require_once 'problem_type.php';
require_once 'source.php';
require_once 'modify_ticket_types.php';
require_once 'value_group.php';
require_once 'attribute.php';

class Drop_Downs extends HTML {

  function Drop_Downs () {
    $this->Set_Edit_Flag (1);

    global $dbi;
    $this->db = &$dbi;
    $this->person_obj = null;
  }

  function &person_list ($type='', $match='', $person_obj='') {
    $this->set_match ($match);

    if (!$this->person_obj and $person_obj) {
      $this->person_obj = $person_obj;
    } elseif (!$this->person_obj) {
      $this->person_obj = new Person ();
    }

    if (is_null($l = $this->person_obj->List_By ($type))) {
      $this->error = 'Drop_Downs::person_list(): ' . $this->person_obj->error;
      return;
    }

    return $l;
  }

  function &person ($type, $match='', $person_obj='') {
    $a = $this->person_list ($type, $match, $person_obj);

    foreach ($a as $ary) {
      $html .= $this->generate_option ($ary[person_id], $ary[last_name].', '.$ary[first_name]);
    }

    return $html;
  }

  function &support_person ($match='', $person_obj='') {
    $html = '';

    if (is_null($a = $this->person_list ('Support', $match, $person_obj))) {
      $this->error = 'Drop_Downs::support_person(): ' . $this->error;
      return;
    }

    foreach ($a as $ary) {
      if ($ary['support_drop_down'] == 'Y') {
        $html .= $this->generate_option ($ary['person_id'], $ary['last_name'].', '.$ary['first_name']);
      }
    }

    return $html;
  }

  function &escalated_person ($match='', $person_obj='') {
    $html = '';

    if (is_null($a = 
	$this->person_list ('Escalated Support', $match, $person_obj))) {
      $this->error = 'Drop_Downs::escalated_person(): ' . $this->error;
      return;
    }

    foreach ($a as $ary) {
      if ($ary['escalated_drop_down'] == 'Y') {
        if (is_null($g = $this->generate_option ($ary['person_id'], 
		      $ary['last_name'].', '.$ary['first_name']))) {
	  $this->error = 'Drop_Downs::escalated_person: ' . $this->error;
	  return;
	}
	$html .= $g;
      }
    }

    return $html;
  }

  function &status ($match='', $status_obj='') {
    $this->set_match ($match);
    $html = '';

    if (!$status_obj) $status_obj = new Status ();

    foreach ($status_obj->List_All () as $stat) {
      $html .= $this->generate_option ($stat);
    }

    return $html;
  }

  function &yes_no ($match='') {
    $this->set_match ($match);

    $sql = '
      select yes_no,
             yes_no
        from yes_no
    order by 1
    ';

    $res = $this->db->getall( $sql );

    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    return $this->Multi_Array_Drop_Down ($res, $match);
  }


  function &state ($match='') {
    $this->set_match ($match);

    $sql = '
      select state, state
        from states
    order by 1
    ';

    $res = $this->db->getall($sql);

    if (DB::isError($res)) {
       $this->error = $res->getMessage() . "\n" . $res->getUserInfo();
       return;
    }

    return $this->Multi_Array_Drop_Down ($res, $match);
  }

  function &person_type ($match='', $person_obj='') {
    if (!$person_obj) $person_obj = new Person ();

    foreach ($person_obj->List_All_Types () as $stat) {
      if (is_array($match) && in_array ($stat, $match)) {
        $this->set_match ($stat);
      } elseif ($match == $stat) {
        $this->set_match ($stat);
      } else {
        $this->set_match ();
      }

      $html .= $this->generate_option ($stat);
    }

    return $html;
  }

  function &templates ($match='', $template_obj='') {
    $this->set_match ($match);

    if (!$template_obj) $template_obj = new Template ();

    foreach ($template_obj->List_All () as $r) {
      $html .= $this->generate_option ($r[template_id], $r[template_name]);
    }

    return $html;
  }

  function &sources ($match='', $problem_type_obj='') {
    if (!$problem_type_obj) $problem_type_obj = new Source ();

    $sources = $problem_type_obj->List_All ();

    return $this->Multi_Array_Drop_Down ($sources, $match);
  }

  function &problem_types ($match='', $problem_type_obj='') {
    if (!$problem_type_obj) $problem_type_obj = new Problem_Type ();

    $probs = $problem_type_obj->List_All ();

    return $this->Multi_Array_Drop_Down ($probs, $match);
  }

  function &problem_types_ondisplay ($match='', $problem_type_obj='') {
    $this->set_match ($match);

    if (!$problem_type_obj) $problem_type_obj = new Problem_Type ();

    $probs = $problem_type_obj->List_All ('Y');

    return $this->Multi_Array_Drop_Down ($probs, $match);
  }

  function date ($year ='', $yfmt='%Y',
                 $mon  ='', $mfmt='%m',
                 $day  ='', $dfmt='%d') {
    $df = date_format('date_val', $dfmt);
    $h = $this->db->prepare ("
      select $df from date_list
      where type = ?
      order by date_val
    ");
    $res = $this->db->execute ( $h, array('DAYS') );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    $this->set_match_value ($day);

    while ($r = $res->fetchRow ()) {
      $dhtml .= $this->generate_option($r[0]);
    }

    $df = date_format('date_val', $mfmt);
    $h = $this->db->prepare ("
      select $df from date_list
      where type = ?
      order by date_val
    ");
    $res = $this->db->execute( $h, array('MON') );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    $this->set_match_value ($mon);

    while ($r = $res->fetchRow ()) {
      $mhtml .= $this->generate_option($r[0]);
    }

    $df = date_format('date_val', $yfmt);
    $h = $this->db->prepare ("
      select $df from date_list
      where type = ?
      order by date_val
    ");
    $res = $this->db->execute ( $h, array('YEAR') );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    $this->set_match_value ($year);

    while ($r = $res->fetchRow ()) {
      $yhtml .= $this->generate_option($r[0]);
    }

    return array (&$yhtml, &$mhtml, &$dhtml);
  }

  function &modify_ticket_types ($match='', $status_obj='') {
    $this->set_match ($match);

    if (!$status_obj) $status_obj = new Modify_Ticket_Types ();

    foreach ($status_obj->List_All () as $stat) {
      $html .= $this->generate_option ($stat);
    }

    return $html;
  }

  function value_group ($match='') {
    $this->set_match ($match);

    $vg = new Value_Group ();

    $ary = $vg->List_All ();
    for ($i=0; $i<count($ary[VALUE_GROUP_ID]); $i++) {
      $html .= $this->generate_option($ary[VALUE_GROUP_ID][$i],
                                      $ary[VALUE_GROUP_NAME][$i]);
    }

    return $html;
  }

  function attribute_types ($match='') {
    $this->set_match ($match);

    $at = new Attribute_Type ();

    foreach ($at->List_All () as $temp) {
      $html .= $this->generate_option ($temp);
    }

    return $html;
  }
}
?>
