<?php
  /**************************************************************************\
  * phpGroupWare                                                             *
  * http://www.phpgroupware.org/                                             *
  * This file written by Patrick Walsh <patrick@phpgroupware.org>            *
  * with some part done by Mathieu van Loon <matje@phpgroupware.org>         *
  * ------------------------------------------------------------------------ *
  * Please see the documentation for these classes in the README file in     *
  * this directory.                                                          *
  * ------------------------------------------------------------------------ *
  *  This program 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.                                              *
  \**************************************************************************/

  /* $Id: contact.class.php,v 1.3 2000/11/30 23:17:35 pjw Exp $ */

  /**************************************************************************\
  * Developers: please execute a search on TODO for things to be done        *
  * Also, execute a search on NOTE for stuff I wasn't sure about, or         *
  * might require attention some other way                                   *
  * mathieu                                                                  *
  \**************************************************************************/
  
class cdb_contact_class extends cdb_generic_class {

  /////////////////////////////////////////////////
 ////////////// Public Variables /////////////////
/////////////////////////////////////////////////
/* These vars are declared like: 'var $varname;' */

  /////////////////////////////////////////////////
 /////////////// Local Variables /////////////////
/////////////////////////////////////////////////
/* PHP does not support local vars.  Use         */
/* 'var $_varname;' to signify local vars.       */
  var $_client; // my subclass client
  var $_meta; // my subclass meta


  /////////////////////////////////////////////////
 //////////// Constructor Functions //////////////
/////////////////////////////////////////////////
	function cdb_contact_class() {
	  $this->_=array(
      /* TODO */
	    /* add vars that have get/let interface
	       to this array.                       */
      "main_organization_location_selector" => 0,
	    "first_name" => "",
	    "middle_name" => "",
	    "last_name_prefix" => "",
	    "last_name" => "",
      "initials" => "",
      "location" => "",
      "nickname" => "",
      "profession" => "",
      "suffix" => "",
      "title" => "",
      "email_selector" => 0,
      "web_selector" => 0,
      "mailing_address_selector" => 0,
      "phone1_selector" => 0,
      "phone2_selector" => 0,
      "phone3_selector" => 0,
      "phone4_selector" => 0,
      "phone5_selector" => 0,
      "phone7_selector" => 0,
      "phone8_selector" => 0,
      "note" => "",
	  );
	}

  /////////////////////////////////////////////////
 ////////////// Public Functions /////////////////
/////////////////////////////////////////////////

  // NOTE: I removed function id() because it is
  //       called directly from the inherited class
  //         -Patrick

	function create($strName) {
    /* TODO */
    

		return int $id || 0;
	}

	function save() {
    /* TODO: for now, we don't check for dirty flags. will add later */
    
    /* NOTE: $this->_note is not stored in the contact_main class, */
    /* but stored in contact_notes. It will be stored by this function */
    /* though */
    $mySuccess = FALSE;
    
    // Check to make sure we have been loaded or created prior to this save
    if($this->id() && $this->exists($this->id()))
    {
       // Build query
       $query = "UPDATE pgw_cdb_contact_main SET ".
//                "main_organization_location_selector=0". WHAT IS IT FOR?
                 "first_name='".$this->get(first_name)."', ".
                 "middle_name='".$this->get(middle_name)."', ".
                 "last_name='".$this->get(last_name)."', ".
                 "last_name_prefix='".$this->get(last_name_prefix)."', ".
                 "initials='".$this->get(initials)."', ".
                 "location='".$this->get(location)."', ".
                 "nickname='".$this->get(nickname)."', ".                
                 "profession='".$this->get(profession)."', ".
                 "suffix='".$this->get(suffix)."', ".
                 "title='".$this->get(title)."', ".
                 "email_selector=".$this->get(email_selector).", ".                
                 "web_selector='".$this->get(web_selector)." ".
                "WHERE contact_id=".$this->id();
      
       // Execute query
       $phpgw->db->query($query);
       
       // the note field is stored in a different table.
       // We don't know whether an actual record exists for this note
       // So we check first
       $query = "SELECT contact_id FROM pgw_cdb_contact_notes ".
                "WHERE contact_id=".$this->id());
       $phpgw->db->query($query);
       if($phpgw->db->num_rows() == 1) // we'll have to update
       {
         $query = "UPDATE pgw_cdb_contact_notes SET note='".$this->get(note)."' ".
                  "WHERE contact_id=".$this->id());
         $phpgw->db->query($query);
       }
       else // we'll have to insert
       {
         $query = "INSERT INTO pgw_cdb_contact_notes (contact_id, note) ".
                  "VALUES (".$this->id().",'".$this->get(note)."')";
         $phpgw->db->query($query);
       }
       
       // Done
       $mySuccess = TRUE; 
    }
    return $mySuccess;
	}

	function load($intID) {
    /* TODO */
    $mySuccess = FALSE;
    return $mySuccess;
	}

/* Function exists is in generic.class.php, which is inherited
	function exists($intID) {
    $mySuccess = FALSE;
    if(is_int($intID)
    {
      
      // do stuff
    }
    return $mySuccess;
	}
*/
	function delete() {
    /* NOTE: I assume someone with write access (can_write($intID) evaluates */
    /* to TRUE, has delete access as well */
    $mySuccess = FALSE;
    if($this->can_write($this->_id)) // check for write permission
    {
      if($this->meta->let_status("delete")) // try to set status to 'delete'
      {
        $mySuccess = TRUE; // report TRUE on success
      }
    }
    return $mySuccess;
	}


	function get_client_data() {
    /* TODO */
		return $this->_client;
	}
	function get_meta_data() {
    /* TODO */
		return $this->_meta;
	}
	function get_personal_data() {
    /* TODO */
		return contact.personal.class;
	}
	function get_internet_data() {
    /* TODO */
		return contact.internet.class;
	}
	function get_phone_data() {
    /* TODO */
		return contact.phone.class;
	}
	function get_address_data() {
    /* TODO */
		return contact.address.class;
	}
	function get_main_organization() {
    /* TODO */
		return organization.class;
	}
	function let_main_organization(organization.class) {
    /* TODO */
		return;
	}
	function get_first_name() {
		return $this->get("first_name");
	}
	function let_first_name($inStr) {
    /* NOTE due to database restrictions, the first_name can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("first_name",$inStr); 
    }
		return $mySuccess;
	}
	function get_middle_name() {
		return $this->get("middle_name");
	}
	function let_middle_name($inStr) {
    /* NOTE due to database restrictions, the middle_name can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("middle_name",$inStr);
    }
		return $mySuccess;
	}
	function get_last_name() {
		return $this->get("last_name");
	}
	function let_last_name($inStr) {
    /* NOTE due to database restrictions, the last_name can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("last_name",$inStr);
    }
		return $mySuccess;
	}
  function get_last_name_prefix() {
		return $this->get("last_name_prefix");
	}
	function let_last_name_prefix($inStr) {
    /* NOTE due to database restrictions, the last_name_prefix can */
    /* be no longer than 10 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=10)
    {
      $mySuccess = $this->let("last_name_prefix",$inStr);
    }
		return $mySuccess;
	}
	function get_initials() {
		return $this->get("initials");
	}
	function let_initials($inStr) {
    /* NOTE due to database restrictions, the initials can be no */
    /* longer than 6 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=6)
    {
      $mySuccess = $this->let("initials",$inStr);
    }
		return $mySuccess;
	}
	function get_location() {
		return $this->get("location");
	}
	function let_location($inStr) {
    /* NOTE due to database restrictions, the location can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("location",$inStr);
    }
		return $mySuccess;
	}
	function get_nickname() {
		return $this->get("nickname");
	}
	function let_nickname($inStr) {
    /* NOTE due to database restrictions, the nickname can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("nickname",$inStr);
    }
		return $mySuccess;
	}
	function get_profession() {
		return $this->get("profession");
	}
	function let_profession($inStr) {
    /* NOTE due to database restrictions, the profession can */
    /* be no longer than 255 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=255)
    {
       $mySuccess = $this->let("profession",$inStr);
    }
		return $mySuccess;
	}
	function get_suffix() {
		return $this->get("suffix");
	}
	function let_suffix($inStr) {
    /* NOTE Due to database restrictions the suffix */
    /* can be no longer than 20 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=20)
    {
       $mySuccess = $this->let("suffix",$inStr);
    }
		return $mySuccess;
	}
	function get_title() {
		return $this->get("title");
	}
	function let_title($inStr) {
    /* NOTE Due to database restrictions the title */
    /* can be no longer than 20 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=20)
    {
      $mySuccess = $this->let("title",$inStr);
    }
		return $mySuccess;
	}
	function get_mailing_address() {
    /* TODO */
		return string;
	}
	function get_mailing_address_selector() {
		return $this->get("mailing_address_selector");
	}
	function let_mailing_address_selector($inID) {
    $mySuccess = FALSE;
    if(is_int($inID))
    {
      $mySuccess = $this->let("mailing_address_selector",$inID);
    }
		return $mySuccess;
    }
	}
	function get_web() {
    /* TODO */
		return string;
	}
	function get_web_selector() {
		return $this->get("web_selector");
	}
	function let_web_selector($inID) {
    $mySuccess = FALSE;
    if(is_int($inID))
    {
      $mySuccess = $this->let("web_selector",$inID);
    }
		return $mySuccess;
	}
	function get_email() {
    /* TODO */
		return string;
	}
	function get_email_selector() {
		return $this->get("email_selector");
	}
	function let_email_selector($inID) {
    $mySuccess = FALSE;
    if(is_int($inID))
    {
      $mySuccess = $this->let("email_selector",$inID);
    }
		return $mySuccess;
	}
	function get_phonex($inID) {
    /* TODO */
    if(is_int($inID) && 0 < $inID && $inID < 9)
    {
      // do stuff
    }
		return string;
	}
	function get_phonex_selector($inID) {
    $myReply = 0;
    if(is_int($inID) && 0 < $inID && $inID < 9)
    {
      $phonex_selector_varname = "phone".$inID."_selector";
      $myReply = $this->get($phonex_selector_varname);
    }
		return $myReply;
	}

	function let_phonex_selector($inID, $inNewVal) {
    $mySuccess = FALSE;
    if(is_int($inID) &)
    {
      $phonex_selector_varname = "phone".$inID."_selector";  
      $mySuccess = $this->let($phonex_selector_varname,$inNewVal);
    }
		return $mySuccess;
  }
	function get_note() {
		return $this->get("note");
	}
	function let_note($inStr) {
    /* NOTE Due to database restrictions the note */
    /* can be no longer than 65535 characters */
    $mySuccess = FALSE;
    if(strlen($inStr)<=65535)
    {
      $mySuccess = $this->let("note", $inStr);
    }
		return $mySuccess;
	}

}
?>
