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

class Base_Object {

  function init_object ($obj_name) {
    if (!$this->obj_name) {
      require_once strtolower ($obj_name).'.php';

      $this->$obj_name = new $obj_name ();
    }
  }

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

  function Set_If_Exists ($var='', $val='', $val2='') {
    if ($val) {
      if ($val2) {
        $this->$var = $val2;
      } else {
        $this->$var = $val;
      }
    }
  }

  function Get ($var) {
    notices_off();
    $val = $this->$var;
    notices_on();

    return $val;
  }

  function Set_Audit_Info (&$in_ref) {
    ## assume upper case vars
    if (is_array($in_ref)) {

      $in_ref['UPDATED_DATE'] = date ('Y-m-d H:i:s');

      if (!array_key_exists('UPDATED_BY', $in_ref)) {
        $in_ref['UPDATED_BY'] = $this->Person_Logged_In ();
      }
  
      if (!array_key_exists('CREATED_DATE', $in_ref)) {
        $in_ref['CREATED_DATE'] = $in_ref['UPDATED_DATE'];
      }
  
      if (!array_key_exists('CREATED_BY', $in_ref)) {
        $in_ref['CREATED_BY'] = $in_ref['UPDATED_BY'];
      }

    ## default to object variables
    ## assume lower case vars
    } else {
      $in_ref->updated_date = date ('Y-m-d H:i:s');

      notices_off();
      $up_by   = $in_ref->updated_by;
      $cr_date = $in_ref->created_date;
      $cr_by   = $in_ref->created_by;
      notices_on();

      if (!$up_by)   $in_ref->updated_by = $this->Person_Logged_In ();
      if (!$cr_date) $in_ref->created_date = $this->updated_date;
      if (!$cr_by)   $in_ref->created_by = $this->updated_by;
    }
  }

  function Reset () {
    foreach (get_object_vars($this) as $name => $v) {
      if ($name != 'db') {
        $this->$name = '';
      }
    }
  }

  function Set_Updated_by ($add) {
    if (!$add[UPDATED_BY]) {
      $add[UPDATED_BY] = $this->Person_Logged_in ();
    }
  }

  function Person_Logged_In () {
    global $REMOTE_USER;
    return $REMOTE_USER;
  }

  function Current_CRM () {
    global $REMOTE_USER_CRM_ID;

    if (is_null($REMOTE_USER_CRM_ID)) {
      $this->error = 'Base_Object::Current_CRM(): REMOTE_USER_CRM_ID is null!';
      return;
    }

    return $REMOTE_USER_CRM_ID;
  }

  function &prep_add_array (&$in_ary) {
    foreach ($in_ary as $k => $v) {
      $add_ary[strtoupper($k)] = $v;
    }

    return $add_ary;
  }
}
?>
