<?/*
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 Search_Query extends Base_Object {

  function Search_Query () {
    global $dbi;
    $this->db = &$dbi;

    $this->table_name = 'search_queries';
  }

  function Delete ($qid) {
    $h = $this->db->prepare ("
      delete from {$this->table_name}
       where query_id = ?
    ");
    $res = $this->db->execute ($h, array($qid));
    if (DB::isError($res)) { 
       $this->error = $res->getMessage();
       return;
    }
  }

  function Add ($in_ary) {
    foreach ($in_ary as $k => $v) {
      $add[strtoupper($k)] = $v;
    }

    $this->Set_Audit_Info ($add);

    if (!$add[QUERY_ID]) $add[QUERY_ID] 
      = $this->db->nextId('query_id');

    $h = $this->db->prepare ("
      insert into {$this->table_name} (
        person_id,
	query_id,
        query_name,
        sql_string,
        sql_params
      ) values (
        ?, ?, ?, ?, ?
      )
    ");
    $res = $this->db->execute ($h, array(
      $add[PERSON_ID],
      $add[QUERY_ID],
      $add[QUERY_NAME],
      $add[SQL_STRING],
      $add[SQL_PARAMS]
    ));

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

    return true;
  }

  function Get_Info ($qid, $type='') {
    $table = 'search_queries';

    if (strtolower($type) == 'g') $table = 'global_search_queries';

    $h = $this->db->prepare ("
      select *
        from $table
       where query_id = ?
    ");
    $res = $this->db->execute( $h, array($qid) );
    if (DB::isError($res)) { 
       $this->error = $res->getMessage();
       return;
    }
    $r = $res->fetchRow (DB_FETCHMODE_ASSOC);

    foreach ($r as $k => $v) {
      $k = strtolower ($k);
      $this->$k = $v;
    }
  }

  function List_by_Person_ID ($pid='') {
    if (!$pid) {
      global $REMOTE_USER;
      $pid = $REMOTE_USER;
    }

    if (!$pid) {
      $this->error = 'Search_Query::List_by_Person_ID: no person id found';
      return;
    }

    $sql = "
      SELECT query_id,
             query_name
        FROM {$this->table_name}
       WHERE person_id = ?
    ORDER BY query_name
    ";

    $res = $this->db->getAll( $sql, array($pid) );

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

    return $res;
  }

  function List_Global ($crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();

    $sql = '
      select query_id,
             query_name
        from global_search_queries
       where crm_id = ?
    order by query_name
    ';

    $res = $this->db->getAll( $sql, array($crm_id) );
    if (DB::isError($res)) { 
       return array();
    } else {
       return $res;
    }
  }
}
?>
