<?/*
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 'base_object.php';

class Value_Group_Member extends Base_Object {
  function Value_Group_Member () {
    global $dbi;
    $this->db = &$dbi;
  }

  function Set ($var, $val) {
    $this->$var = $val;
  }

  function Delete ($opt_id) {
    $h=$this->db->query('
      delete from value_group_members
            where member_id = ?
    ', array($opt_id));

    if (DB::isError($h)) {
      $this->error = 'Value_Group_Member::Delete(): ' . $h->getMessage()
	. "\n" . $h->getUserInfo();
      return;
    }
    return true;
  }

  function Update ($vgmid, $vgn, $mem_desc='', $upd_by='') {
    $upd = array( 'UPDATED_BY' => $upd_by );
    $this->Set_Audit_Info( $upd );

    $binds = array(
      $vgn, $mem_desc, $upd[UPDATED_BY], $upd[UPDATED_DATE], $vgmid
    );

    $to_date = to_date();
    $sql = "
      update value_group_members
         set member_name = ?,
             member_description = ?,
             updated_by = ?,
             updated_on = $to_date
       where member_id = ?
    ";

    $h = $this->db->prepare( $sql );
    $res = $this->db->execute( $h, $binds );

    if (DB::isError($res)) {
      $this->error = $res->getMessage()."\n$sql\n".join(', ',$binds);
      return 0;
    }

    return 1;
  }

  function Add ($vgid, $mn, $md='', $ai='', $upd_by='') {
    $mid = $this->db->nextId('member_id');
    $res = $this->db->query('
      insert into value_group_members (
	member_id,
        value_group_id,
        member_name,
        member_description,
        additional_info,
        updated_by
      ) values (
        ?, ?, ?, ?, ?, ?
      )
    ', array($mid, $vgid, $mn, $md, $ai, $upd_by));

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

    return true;
  }

  function By_Value_Group ($vgid) {
    $stmt='
       select *
        from value_group_members
       where value_group_id = ?
       ';
     
    return $this->db->getAll($stmt, array($vgid), DB_FETCHMODE_ASSOC);
  }

  function Member_Name ($oid) {
    $h = $this->db->prepare('
      select member_name
        from value_group_members
       where member_id = ?
    ');
     
    $res = $this->db->execute($h, array($oid));

    if (DB::isError($res)) return '';

    $r = $res->fetchRow();

    return $r[0];
  }
}

class Value_Group extends Base_Object {
  function Value_Group () {
    global $dbi;
    $this->db = &$dbi;
  }

  function Set ($var, $val) {
    $this->$var = $val;
  }

  function Delete ($opt_id) {
    $h = $this->db->prepare('
      delete from value_group
            where value_group_id = ?
    ');
    $res = $this->db->execute($h, array($opt_id));

    if (DB::isError($res)) return 0;

    return 1;
  }

  function Add ($vgn, $ai='', $upd_by='') {
    $in_ary = array (
      'value_group_name' => $vgn,
      'additional_info'  => $ai,
      'updated_by'       => $upd_by,
    );

    $add = $this->prep_add_array ($in_ary);

    if (!array_key_exists('VALUE_GROUP_ID', $add))
      $add['VALUE_GROUP_ID'] = $this->db->nextID( 'value_group_id' );

    $this->Set_Audit_Info( $add );

    $binds = array(
      $add['VALUE_GROUP_ID'],
      $add['VALUE_GROUP_NAME'],
      $add['ADDITIONAL_INFO'],
      $add['UPDATED_BY'],
      $add['CREATED_BY'],
      $add['CREATED_DATE'],
      $add['UPDATED_DATE']
    );

    $to_date = to_date();
    $sql = "
      insert into value_group (
        value_group_id,
        value_group_name,
        additional_info,
        updated_by,
        created_by,
        created_on,
        updated_on
      ) values (
        ?, ?, ?, ?, ?,
        $to_date,
        $to_date
      )
    ";

    $h = $this->db->prepare( $sql );
    $ok = $this->db->execute ( $h, $binds );

    if (DB::isError($ok)) {
      $this->error = $ok->getMessage()."\n$sql\n".join(', ',$binds);
      return 0;
    }

    return $add['VALUE_GROUP_ID'];
  }

  function List_All () {
    $sql = '
      select value_group_id,
             value_group_name
        from value_group
    order by upper(value_group_name)
    ';

    $recs = $this->db->getAll( $sql );

    $ret = array();

    foreach ($recs as $r) {
      $ret[VALUE_GROUP_ID] = $r[0];
      $ret[VALUE_GROUP_NAME] = $r[1];
    }

    return $ret;
  }
}
?>
