<?php
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*********************************************************************************
 * $Id: Note.php,v 1.56 2005/02/26 00:00:42 robert Exp $
 * Description:  TODO: To be written.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/

include_once('config.php');
require_once('include/logging.php');
require_once('include/database/PearDatabase.php');
require_once('data/SugarBean.php');
require_once('include/upload_file.php');

// Note is used to store customer information.
class Note extends SugarBean {
	var $field_name_map;
	// Stored fields
	var $id;
	var $date_entered;
	var $date_modified;
	var $modified_user_id;
	var $created_by;
	var $created_by_name;
	var $modified_by_name;
	var $description;
	var $name;
	var $filename;
	// handle to an upload_file object
	// used in emails
	var $file;
	var $parent_type;
	var $parent_id;
	var $contact_id;
	var $portal_flag;




	var $parent_name;
	var $contact_name;
	var $contact_phone;
	var $contact_email;

	var $default_note_name_dom = array('Meeting notes', 'Reminder');
	var $required_fields =  array("name"=>1);
	var $table_name = "notes";

	var $object_name = "Note";

	var $column_fields = Array("id"
		, "date_entered"
		, "date_modified"
		, "modified_user_id"
		, "created_by"
		, "description"
		, "name"
		, "filename"
		, "file_mime_type"
		, "parent_type"
		, "parent_id"
		, "contact_id"
		, "portal_flag"



		);
		/*
	DO NOT DEPEND ON THIS THIS DATA STRUCTURE
	IT IS ONLY TEMPORARY AND MAY NOT BE SUPPORTED IN FUTURE RELEASES
*/
		var $field_defs = array(
		array("name"=>"date_entered",
        		"vname"=>"LBL_DATE_CREATED","type"=>"date","len"=>"255" ),
        array("name"=>"date_modified",
        		"vname"=>"LBL_DATE_MODIFIED","type"=>"date","len"=>"255" ),
        array("name"=>"filename",
        		"vname"=>"LBL_FILENAME","type"=>"varchar","len"=>"255" ),
        array("name"=>"name",
        		"vname"=>"LBL_NOTE_SUBJECT","type"=>"varchar","len"=>"50" ),
        array("name"=>"description",
        		"vname"=>"LBL_DESCRIPTION","type"=>"text","len"=>"255" ),
        array('name'=>'portal_flag','vname'=>'LBL_PORTAL_FLAG','type'=>'bool'),
        array('name'=> 'parent_name', 'parent_type'=>'record_type_display' , 'type_name'=>'parent_type','id_name'=>'parent_id', 'vname'=>'LBL_RELATED_TO', 'type'=>'parent'),
        array('name'=>'contact_name','rname'=>'last_name','id_name'=>'contact_id','vname'=>'LBL_CONTACT_NAME','type'=>'relate','table'=>'contacts','isnull'=>'true','module'=>'Contacts'),



         array('name'=>'created_by','rname'=>'user_name',
              'id_name'=>'created_by','vname'=>'LBL_CREATED',
              'type'=>'assigned_user_name','table'=>'created_by_users',
              'isnull'=>'false'),
         array('name'=>'modified_user_id','rname'=>'user_name',
              'id_name'=>'modified_user_id','vname'=>'LBL_MODIFIED',
              'type'=>'assigned_user_name','table'=>'modified_user_id_users',
              'isnull'=>'false'),

        );



	// This is used to retrieve related fields from form posts.
	var $additional_column_fields = Array('contact_name', 'contact_phone', 'contact_email', 'parent_name','first_name','last_name');

	var $list_fields = Array('id', 'name', 'parent_type', 'parent_name', 'parent_id','date_modified', 'contact_id', 'contact_name','filename'



	);

	function Note() {
		$this->log = LoggerManager::getLogger('note');
		$this->db = new PearDatabase();
                $custom = new CustomFields();
                $custom->setFieldDefs($this); 
		foreach ($this->field_defs as $field)
		{
			$this->field_name_map[$field['name']] = $field;
		}



	}

	var $new_schema = true;

	function create_tables () {
		$query = 'CREATE TABLE '.$this->table_name.' ( ';
		$query .='id char(36) NOT NULL';
		$query .=', date_entered datetime NOT NULL';
		$query .=', date_modified datetime NOT NULL';
		$query .=', modified_user_id char(36)';
		$query .=', created_by char(36)';
		$query .=', name char(255)';
		$query .=', filename char(255)';
		$query .=', file_mime_type char(100)';
		$query .=', parent_type char(25)';
		$query .=', parent_id char(36)';
		$query .=', contact_id char(36)';
		$query .=', portal_flag bool NOT NULL default 0';
		$query .=', description text';
		$query .=', deleted bool NOT NULL default 0';



		$query .=', PRIMARY KEY ( ID ) )';

		$this->db->query($query,true,"Error creating table: ");

		//TODO Clint 4/27 - add exception handling logic here if the table can't be created.

		// Create the indexes
		$this->create_index("create index idx_note_name on notes (name)");
	}

	function drop_tables () {
		$query = 'DROP TABLE IF EXISTS '.$this->table_name;

		$this->db->query($query);

		//TODO Clint 4/27 - add exception handling logic here if the table can't be dropped.

	}

	function get_summary_text()
	{
		return "$this->name";
	}

	function create_list_query(&$order_by, &$where)
	{
		$contact_required = ereg("contacts\.first_name", $where);
		$contact_required = 1;
		$query = "SELECT ";
		$custom_fields = new CustomFields();
                $query .= $custom_fields->get_list_query_custom_select($this);


		if($contact_required)
		{

                		$query .= "
                                        notes.id,
                                        notes.name,
                                        notes.parent_type,
                                        notes.parent_id,
                                        notes.contact_id,
                                        notes.filename,
                                        notes.date_modified,
                                        CONCAT(contacts.first_name,' ',contacts.last_name) as contact_name,
                                        contacts.last_name,
                                        contacts.phone_work,
                                        contacts.email1
                                        FROM notes ";





			$query .= " LEFT JOIN contacts ON notes.contact_id=contacts.id ";
			$query .= $custom_fields->get_list_query_custom_from($this);

			$where_auto = " (contacts.deleted IS NULL OR contacts.deleted=0) AND notes.deleted=0";
		}
		else
		{
			$query .= ' id, name, parent_type, parent_id, contact_id, filename, date_modified FROM notes ';
			$query .= $custom_fields->get_list_query_custom_from($this);
			$where_auto = "deleted=0";
		}

		if($where != "")
			$query .= "where $where AND ".$where_auto;
		else
			$query .= "where ".$where_auto;

		if($order_by != "")
			$query .= " ORDER BY $order_by";
		else
			$query .= " ORDER BY name";

		return $query;
	}




        function create_export_query(&$order_by, &$where)
        {

                        $query = "SELECT
                                        notes.*,
                                        contacts.first_name,
                                        contacts.last_name
                                        FROM notes ";




			$query .= 				"	LEFT JOIN contacts
                                        ON notes.contact_id=contacts.id ";
                        $where_auto = " notes.deleted=0 AND contacts.deleted=0";

                if($where != "")
                        $query .= "where $where AND ".$where_auto;
                else
                        $query .= "where ".$where_auto;

                if($order_by != "")
                        $query .= " ORDER BY $order_by";
                else
                        $query .= " ORDER BY name";

                return $query;
        }




	function fill_in_additional_list_fields()
	{
//		$this->fill_in_additional_detail_fields();
		$this->fill_in_additional_parent_fields();
	}

	function fill_in_additional_detail_fields()
	{
		# TODO:  Seems odd we need to clear out these values so that list views don't show the previous rows value if current value is blank
		$this->contact_name = '';
		$this->contact_phone = '';
		$this->contact_email = '';
		$this->parent_name = '';

		if (isset($this->contact_id) && $this->contact_id != '') {
			require_once("modules/Contacts/Contact.php");
			$contact = new Contact();
			$query = "SELECT first_name, last_name, phone_work, email1 from $contact->table_name where id = '$this->contact_id'";
			$result =$this->db->query($query,true," Error filling in additional detail fields: ");

			// Get the id and the name.
			$row = $this->db->fetchByAssoc($result);

			if($row != null)
			{
				$this->contact_name = return_name($row, 'first_name', 'last_name');
				if ($row['phone_work'] != '') $this->contact_phone = $row['phone_work'];
				else $this->contact_phone = '';
				if ($row['email1'] != '') $this->contact_email = $row['email1'];
				else $this->contact_email = '';
			}

		}

		$this->created_by_name = get_assigned_user_name($this->created_by);
		$this->modified_by_name = get_assigned_user_name($this->modified_user_id);

		$this->fill_in_additional_parent_fields();
	}

	function fill_in_additional_parent_fields()
	{
		global $app_strings;
		if ($this->parent_type == "Opportunities") {
			require_once("modules/Opportunities/Opportunity.php");
			$parent = new Opportunity();
			$query = "SELECT name from $parent->table_name where id = '$this->parent_id'";
			$result =$this->db->query($query,true," Error filling in additional detail fields: ");

			// Get the id and the name.
			$row = $this->db->fetchByAssoc($result);

			if($row != null)
			{
				if ($row['name'] != '') stripslashes($this->parent_name = $row['name']);
			}
		}
		else if ($this->parent_type == "Emails") {
			require_once("modules/Emails/Email.php");
			$parent = new Email();
			$query = "SELECT name from $parent->table_name where id = '$this->parent_id'";
			$result =$this->db->query($query,true," Error filling in additional detail fields: ");

			// Get the id and the name.
			$row = $this->db->fetchByAssoc($result);

			if($row != null)
			{
				if ($row['name'] != '') stripslashes($this->parent_name = $row['name']);
			}
		}
		elseif ($this->parent_type == "Cases") {
			require_once("modules/Cases/Case.php");
			$parent = new aCase();
			$query = "SELECT name from $parent->table_name where id = '$this->parent_id'";
			$result =$this->db->query($query,true," Error filling in additional detail fields: ");

			// Get the id and the name.
			$row = $this->db->fetchByAssoc($result);

			if($row != null)
			{
				if ($row['name'] != '') $this->parent_name = stripslashes($row['name']);
			}
		}
		elseif ($this->parent_type == "Accounts") {
			require_once("modules/Accounts/Account.php");
			$parent = new Account();
			$query = "SELECT name from $parent->table_name where id = '$this->parent_id'";
			$result =$this->db->query($query,true," Error filling in additional detail fields: ");

			// Get the id and the name.
			$row = $this->db->fetchByAssoc($result);

			if($row != null)
			{
				if ($row['name'] != '') $this->parent_name = stripslashes($row['name']);
			}
		}
		elseif ($this->parent_type == "Leads") {
                        require_once("modules/Leads/Lead.php");
                        $parent = new Lead();
                        $query = "SELECT first_name, last_name from $parent->table_name where id = '$this->parent_id'";

                        $result =$this->db->query($query,true, " Error filling in additional detail fields: ");


                        // Get the id and the name.

                        $row = $this->db->fetchByAssoc($result);


                        if($row != null)
                        {
                                $this->parent_name = '';
                                if ($row['first_name'] != '') $this->parent_name .= stripslashes($row['first_name']). ' ';
                                if ($row['last_name'] != '') $this->parent_name .= stripslashes($row['last_name']);
                        }
                }
		elseif ($this->parent_type == "Issues") {
        	require_once("modules/Issues/Issue.php");
            $parent = new Issue();

            $query = "SELECT name from $parent->table_name where id = '$this->parent_id'";
			$result = $this->db->query($query, TRUE, "Error filling in additional detail fields: ");
			$row = $this->db->fetchByAssoc($result);

			if (!is_null($row)) {
 				$this->parent_name = '';
				if (!empty($row['name'])) $this->parent_name .= stripslashes($row['name']);
			}
		}








































	}
	function get_list_view_data(){
		$note_fields = $this->get_list_view_array();
		global $app_list_strings, $focus, $action, $currentModule;
		$note_fields["DATE_MODIFIED"] = substr($note_fields["DATE_MODIFIED"], 0 , 10);
		if (isset($this->parent_type)) {
			$note_fields['PARENT_MODULE'] = $this->parent_type;
		}

		if (! isset($this->filename) || $this->filename != '')
                {
                        $note_fields['FILENAME'] = $this->filename;
                        $note_fields['FILE_URL'] = UploadFile::get_url($this->filename,$this->id);
                }


		return $note_fields;
	}

}
?>
