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

class Attribute extends HTML {

  function Attribute () {
    global $dbi;
    $this->db = &$dbi;
    $this->error = "";
  }

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

  ## returns the option_name
  function get_option_by_id ($aid, $id) {
    $ao = new Attribute_Option ();

    return $ao->option_name ($this->get_option_id_by_id ($aid, $id));
  }

  function get_group_id ($aid) {
    $stmt = $this->db->prepare ('
      select value_group_id
        from attribute
       where attribute_id = ?
    ');
    $res = $this->db->execute($stmt,array($aid));
    if (DB::isError($res)) {
	$this->error = $res->getMessage();
	return '';
    }
    $r = $res->fetchRow();

    return $r[0];
  }

  function attribute_name ($aid) {
    $h = $this->db->prepare ('
      select attribute_name
        from attribute
       where attribute_id = ?
    ');
    $res = $this->db->execute($h,array($aid));
    if (DB::isError($res)) {
	$this->error = $res->getMessage();
	return '';
    }
    $r = $res->fetchRow();

    return $r[0];
  }

  function members ($aid, $assoc=DB_FETCHMODE_DEFAULT) {
    $sql = '
      select member_id,
             member_name
        from value_group_members
       where value_group_id = (
               select value_group_id
                 from attribute
                where attribute_id = ?
             )
    ';
    return $this->db->getAll($sql,array($aid),$assoc);
  }

  function Delete ($aid) {
    $h = $this->db->prepare('
      delete from attribute
            where attribute_id = ?
    ');
    $res = $this->db->execute($h, array($aid));

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

    return 1;
  }

  function Update ($aid, $an, $ai='', $upd_by='') {
    $h = $this->db->prepare('
      update attribute
         set attribute_name = ?,
             additional_info = ?,
             updated_by = ?
       where attribute_id = ?
    ');
    $res = $this->db->execute($h, 
	array(
	  $an,
	  $ai,
	  $upd_by,
	  $aid
	)
    );

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

    return 1;
  }

  function Add ($an, $ai='', $upd_by='', $at, $vgid) {
    $in_ary = array (
      'attribute_name'  => $an,
      'additional_info' => $ai,
      'updated_by'      => $upd_by,
      'attribute_type'  => $at,
      'value_group_id'  => $vgid,
    );

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

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

    $h = $this->db->prepare('
      insert into attribute (
        attribute_id,
        attribute_name,
        additional_info,
        updated_by,
        attribute_type,
        value_group_id
      ) values (
        ?, ?, ?, ?, ?, ?
      )
    ');
    $res = $this->db->execute ($h,
      array(
	$add['ATTRIBUTE_ID'],
	$add['ATTRIBUTE_NAME'],
	$add['ADDITIONAL_INFO'],
	$add['UPDATED_BY'],
	$add['ATTRIBUTE_TYPE'],
	$add['VALUE_GROUP_ID']
      )
    );

    if (!DB::isError($res)) return $add['ATTRIBUTE_ID'];

    $this->error = $res->getMessage() . "\n" . $res->getUserInfo();

    return;
  }

  function atts_by_page_id ($page_id,$assoc=0) {
    $sql = '
  select am.attribute_id,
         am.web_display_id,
         a.attribute_name,
         am.order_id
    from web_page_attributes_map am,
         attribute a
   where am.attribute_id = a.attribute_id
     and am.web_page_id = ?
order by am.order_id,
         upper(a.attribute_name)
    ';

    if ($assoc) {
      $assoc = DB_FETCHMODE_ASSOC;
    } else {
      $assoc = DB_FETCHMODE_ORDERED;
    }

    return $this->db->getAll($sql,array($page_id),$assoc);
  }

  function members_by_att_id ($att_id, $rtype=0) {
    return $this->members_by_group_id ($this->get_group_id ($att_id), $rtype);
  }

  function members_by_group_id ($group_id) {
    $sql = '
      select *
        from value_group_members
       where value_group_id = ?
    ';

    return $this->db->getAll($sql, array($group_id), DB_FETCHMODE_ASSOC);
  }

  function Delete_From_Page ($att_id, $page_id) {
    $h = $this->db->prepare('
      delete from web_page_attributes_map
       where attribute_id = ?
         and web_page_id = ?
    ');
    $res = $this->db->execute($h, array($att_id, $page_id));
    if (DB::isError($res)) {
      $this->error = $res->getMessage;
      return 0;
    }
    return 1;
  }

  function Update_Page_Att ($att_id, $page_id, $disp_id, $order_id='', $upd_by){
    if (!strlen($order_id)) $order_id = 0;

    $h = $this->db->prepare('
      update web_page_attributes_map
         set web_display_id = ?,
             order_id = ?,
             updated_by = ?
       where attribute_id = ?
         and web_page_id = ?
    ');

    $res =  $this->db->execute ($h,
      array(
	$disp_id,
	$order_id,
	$upd_by,
	$att_id,
	$page_id
      )
    );

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

    return true;
  }

  function Add_to_Page ($att_id, $page_id, $disp_id, $order_id='', $upd_by) {
    if (!$disp_id) $disp_id = 1;

    if (!strlen($order_id)) $order_id = 0;

    $to_date = to_date();
    $sql = "
      insert into web_page_attributes_map (
        attribute_id,
        web_page_id,
        web_display_id,
        order_id,
        updated_by,
        created_by,
        created_on,
        updated_on
      ) values (
        ?, ?, ?, ?, ?, ?,
	$to_date,
	$to_date
      )
    ";

    $binds_audit = array();
    $this->Set_Audit_Info( $binds_audit );

    $binds = array(
      $att_id,
      $page_id,
      $disp_id,
      $order_id,
      $binds_audit['UPDATED_BY'],
      $binds_audit['CREATED_BY'],
      $binds_audit['CREATED_DATE'],
      $binds_audit['UPDATED_DATE'],
    );

    $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;
  }
}

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

  function List_All () {
    $sql = '
      select attribute_type
        from attribute_types
    order by attribute_type
    ';
    return $this->db->getCol( $sql );
  }
}

class Attribute_Value extends HTML {
  function Attribute_Value () {
    global $dbi;
    $this->db = &$dbi;
    $this->error = "";
  }

## need to test 03/20/01 - BWD
  # this function uses the global REMOTE_USER, should
  # be passed for cleanest implementation
  #
  # $hash[id] = &$array;
  # $array[att_id][option_id] = []
  # $array[att_id][ai] = 'some additional info'
  #
  function add_cleared_att_vals ($id, &$att_ref) {
    global $REMOTE_USER;

    #echo "<!-- LIBRARY -->\n";
    #echo "<!-- $id -->\n";

    foreach ($att_ref as $att_id => $opt_ref) {
      #echo "<!-- deleting att_id $att_id -->\n";
      $this->Delete ($att_id, $id);

      if (count($opt_ref[option_id]) > 0) {
        foreach ($opt_ref[option_id] as $opt_id) {
          if (!$opt_id) continue;

          #echo "<!-- creating $opt_id ".$opt_ref[ai]." -->\n";
          $this->Add (
            $att_id,
            $id,
            $opt_id,
            $opt_ref[ai],
            $REMOTE_USER
          );
        }
      }
    }
  }

  function Add ($att_id, $id, $memb_id, $ai='', $upd_by='') {
    $ins = $this->db->prepare('
      insert into attribute_values (
        attribute_id,
        id,
        member_id,
        additional_info,
        updated_by
      ) values (
        ?, ?, ?, ?, ?
      )
    ');
    $res = $this->db->execute ($ins,
      array(
	$att_id,
	$id,
	$memb_id,
	$ai,
	$upd_by
      )
    );

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

    return 1;
  }

  ## returns the option_id
  function get_option_id_by_id ($aid, $id) {
    $h = $this->db->prepare('
      select member_id 
        from attribute_values
       where id = ?
         and attribute_id = ?
    ');
    $res = $this->db->execute($h, array($id, $aid));
    if (DB::isError($res)) {
      $this->error = $res->getMessage();
      return;
    }
    $r = $res->fetchRow();

    return $r[0];
  }

  ## check if an option is set by counting the records
  function option_is_set ($aid, $id, $opt) {
    $stmt = $this->db->prepare('
      select count(*) as count
        from attribute_values
       where id = ?
         and member_id = ?
         and attribute_id = ?
    ');
    $res = $this->db->execute($stmt, array($id, $opt, $aid));
    if (DB::isError($res)) {
	$this->error = $res->getMessage();
	return;
    }
    $r = $res->fetchRow();

    return $r[0];
  }

  function members_by_id_att ($id, $aid) {
    $sql = '
      select av.additional_info,
             vgm.member_id,
	     vgm.member_name
	from attribute_values av,
	     value_group_members vgm
       where av.id = ?
         and av.attribute_id = ?
	 and av.member_id = vgm.member_id
    ';

    return $this->db->getAll($sql, array($id, $aid), DB_FETCHMODE_ASSOC);
  }

  function members_set_by_att ($aid, $id='', $ai=0) {
    if (!$id) return array ();

    $sql = '
      select member_id,
             additional_info
        from attribute_values
       where id = ?
         and attribute_id = ?
    ';

    ## hash based on member_id
    if ($ai) {
      $s = array();

      $recs = $this->db->getAll( $sql, 0, array($id,$aid) );

      foreach ($recs as $r) {
        $s[$r[0]] = $r[1];
      }

      return $s;

    } else {
      return $this->db->getCol( $sql, 0, array($id,$aid) );
    }
  }

  function att_val_record ($id, $aid, $mid) {
    $h = $this->db->prepare('
      select *
        from attribute_values
       where id = ?
         and attribute_id = ?
	 and member_id = ?
    ');
    $res = $this->db->execute($h, array($id, $aid, $mid));
    if (DB::isError($res)) {
	$this->error = $res->getMessage();
	return array();
    }
    return $res->fetchRow(DB_FETCHMODE_ASSOC);
  }

  function att_val_list ($aid, $oid) {
    $sql = '
      select id,
             additional_info 
        from attribute_values
       where attribute_id = ?
         and member_id    = ?
    ';
    $res = $this->db->getAll( $sql, array($aid,$oid) );
    if (DB::isError($res)) {
	$this->error = $res->getMessage();
	return array();
    }
    $ret = array();

    foreach ($res as $r) {
      $ret[ID][] = $r[0];
      $ret[ADDITIONAL_INFO][] = $r[1];
    }

    return array(&$ret[ID], &$ret[ADDITIONAL_INFO]);
  }

  function Delete ($att_id, $id) {
    $del = $this->db->prepare('
      delete from attribute_values
       where attribute_id = ?
         and id           = ?
    ');
    $res = $this->db->execute($del, array($att_id, $id));

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

}

?>
