<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program 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, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/
/*********************************************************************************

 * Description:  TODO: To be written.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/

require_once('data/SugarBean.php');
require_once('include/utils.php');

class EcmMailingMessage extends SugarBean {
    var $field_name_map = array();

	var $id;
	var $date_entered;
	var $date_modified;
	var $modified_user_id;
	var $assigned_user_id;
	var $name;
	var $name_en;
	var $name_de;
	var $description;
	var $local_transportation;
	var $abroad_transportation;
	
	var $module_dir = 'EcmMailingMessages';
	var $table_name = "ecmmailingmessages";
	var $object_name = "EcmMailingMessage";
	
	var $new_schema = true;

	var $additional_column_fields = array('assigned_user_name', 'assigned_user_id');

	function EcmMailingMessage() {
		parent::SugarBean();
		$this->setupCustomFields('EcmMailingMessages');
		foreach($this->field_defs as $field){
        	$this->field_name_map[$field['name']] = $field;
        }
	}
	function get_summary_text(){
		return $this->name;
	}
	function create_list_query($order_by, $where, $show_deleted = 0){
		$custom_join = $this->custom_fields->getJOIN();
		$query ="SELECT ";
        $query.="ecmmailingmessages.*,users.user_name as assigned_user_name";
		if($custom_join)$query.=$custom_join['select'];
        $query.=" FROM ecmmailingmessages ";
		$query.="LEFT JOIN users ON ecmmailingmessages.assigned_user_id=users.id";
		$query.="  ";
		if($custom_join)$query.=$custom_join['join'];
        $where_auto='1=1';
		if($show_deleted==0)$where_auto=" $this->table_name.deleted=0 ";
		elseif($show_deleted==1)$where_auto=" $this->table_name.deleted=1 ";	
        if($where!="")$query.="where $where AND ".$where_auto;
		else $query.="where ".$where_auto;
		if(substr_count($order_by,'.')>0)$query .= " ORDER BY $order_by";
		elseif($order_by != "")$query .= " ORDER BY $order_by";
		else $query .= " ORDER BY ecmmailingmessages.name";		
		return $query;
	}
	function create_export_query($order_by,$where){
		$custom_join = $this->custom_fields->getJOIN();
		$query ="SELECT ";
        $query.="ecmmailingmessages.*,users.user_name as assigned_user_name";
		if($custom_join)$query.=$custom_join['select'];
        $query.=" FROM ecmmailingmessages ";
		$query.="LEFT JOIN users ON ecmmailingmessages.assigned_user_id=users.id";
		$query.="  ";
		if($custom_join)$query.=$custom_join['join'];
        $where_auto='1=1';
		if($show_deleted==0)$where_auto=" $this->table_name.deleted=0 ";
		elseif($show_deleted==1)$where_auto=" $this->table_name.deleted=1 ";	
        if($where!="")$query.="where $where AND ".$where_auto;
		else $query.="where ".$where_auto;
		if(substr_count($order_by,'.')>0)$query .= " ORDER BY $order_by";
		elseif($order_by != "")$query .= " ORDER BY $order_by";
		else $query .= " ORDER BY ecmmailingmessages.name";		
		return $query;
    }
	function fill_in_additional_list_fields(){
	}

	function fill_in_additional_detail_fields(){   
        parent::fill_in_additional_detail_fields();
	}		
	function get_list_view_data(){
		global $current_language;
		$the_array=parent::get_list_view_data();
		$app_list_strings=return_app_list_strings_language($current_language);
		$mod_strings=return_module_language($current_language,'EcmMailingMessages');

	    $the_array['NAME']=(($this->name == "") ? "<em>blank</em>" : $this->name);
	    $the_array['ENCODED_NAME']=$this->name;

		return $the_array;
	}
	function build_generic_where_clause($the_query_string){
		$where_clauses=array();
		$the_query_string=PearDatabase::quote(from_html($the_query_string));
		array_push($where_clauses,"ecmmailingmessages.name like '$the_query_string%'");

		$the_where="";
		foreach($where_clauses as $clause){
			if($the_where!="")$the_where.=" or ";
			$the_where.=$clause;
		}
		return $the_where;
	}
	function set_notification_body($xtpl,$ecmmailingmessage)
	{
		global $mod_strings,$app_list_strings;
		$xtpl->assign("ECMPRODUCTCATEGORY_SUBJECT",$ecmmailingmessage->name);
		return $xtpl;
	}
	
	function bean_implements($interface){
		switch($interface){
			case 'ACL':return true;
		}
		return false;
	}
	
	function save($check_notify=FALSE){
		return parent::save($check_notify);
		
		//$this->db->query("update ecmmailingmessages set send=0 where id='".$this->id."'");
	}
	

	function showPositions55(){
		$arr=$this->getPositionList55(true);
	
		global $mod_strings;
		if(count($arr)>0){
			$table='
			<table cellpadding="0" cellspacing="0" border="0" class="list view" width="40%">
			<tr class="oddListRowS1">
				<th><b>Nazwa grupy</b></td>
				<th><b>Aktywna</b></td>
			</tr>
			';
			$i == 0;
			foreach($arr as $a){
				$i++;
	
	
				$table.='
				<tr class="oddListRowS1">
					<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;"><a href="index.php?module=EcmMailingCategories&action=DetailView&record='.$a['cat_id'].'">'.$a['product_group'].'</a></td>
	
					<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">'.($a['discount']==1 ? 'Tak' : "Nie").'</td>
	
				';
			}
			$table.='</table>';
		}
	
		return $table;
	}

	function getPositionList56($array = false) {
		if(isset($this->id) && $this->id != '') {
			$query = "SELECT * FROM ecmaccountcategories_bean WHERE bean_id='".$this->id."' AND deleted='0' AND bean_name='MailingContacts'";
	
			$r = $this->db->query($query);
			$return_array = array();
			if($r) {
	
				while($w = $this->db->fetchByAssoc($r)) {
					//get category name && assigned_file
					$n = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT name FROM ecmmailingcategories WHERE id='".$w['ecmaccountcategory_id']."'"));
					$w['product_group'] = $n['name'];
					$w['cat_id'] =$w['ecmaccountcategory_id'];
	
					$return_array [] = $w;
	
				}
				$json = getJSONobj();
	
				return $array ? $return_array : $json->encode($return_array);
			}
		}
	
		return $array ? false : '[]';
	}
	function getPositionList55($array = false) {
		if(isset($this->id) && $this->id != '') {
			$query = "SELECT * FROM ecmaccountcategories_bean WHERE bean_id='".$this->id."' AND deleted='0' AND bean_name='Mailing'";
	
			$r = $this->db->query($query);
			$return_array = array();
			if($r) {
	
				while($w = $this->db->fetchByAssoc($r)) {
					//get category name && assigned_file
					$n = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT name FROM ecmaccountcategories WHERE id='".$w['ecmaccountcategory_id']."'"));
					$w['product_group'] = $n['name'];
					$w['cat_id'] =$w['ecmaccountcategory_id'];

					$return_array [] = $w;
	
				}
				$json = getJSONobj();
	
				return $array ? $return_array : $json->encode($return_array);
			}
		}
	
		return $array ? false : '[]';
	}
	
	//end managing addresses
	function savePositions55($pl) {
	
		global $current_user;
	
		$exists = array();
		foreach ($pl as $p) {
	
			if (!isset($p['product_group']) || $p['product_group']=='') continue;
	
	
			if (isset($p['id']) && $p['id']!='') {
	
				//update exists
				
				$q = "
				UPDATE ecmaccountcategories_bean set
					date_modified = '".date("Y-m-d H:i:s")."',
					modified_user_id = '".$current_user->id."',
					bean_id = '".addslashes($this->id)."',
					ecmaccountcategory_id = '".addslashes($p['cat_id'])."'
					WHERE id = '".$p['id']."'
				";
				/*
				$q = "
				UPDATE ecmmailingcategories_relation set
					parent_id = '".addslashes($this->id)."',
					ecmmailingcategoriy_id= '".addslashes($p['cat_id'])."',
					available = '".addslashes($p['discount'])."',
				    parent_type = 'MailingMessage',
					parent_name = '".addslashes($p['product_group'])."'
					WHERE id = '".$p['id']."'
				";
				*/
	
				//$GLOBALS['db']->query("INSERT INTO log VALUES ('".addslashes($q)."')");
				$GLOBALS['db']->query($q);
				$exists[] = $p['id'];
	
			} else {
				//insert new record
				$id = create_guid();
				/*
				$t = array(
						$id,
						addslashes($p['cat_id']),
						'0',
						addslashes($p['discount']),
						$this->id,
						addslashes($p['product_group']),
						'MailingMessage'
	
				); */
				$t = array(
						$id,
						addslashes($p['cat_id']),
						$this->id,
						"Mailing",
						date("Y-m-d H:i:s"),
						date("Y-m-d H:i:s"),
						$current_user->id,
						$current_user->id,
						'0'
				);
				$q = "INSERT INTO ecmaccountcategories_bean VALUES ('".implode("','", $t)."')";
	
				$GLOBALS['db']->query($q);
				
				$exists[] = $id;
			}
	
		}

		//delete old
		$GLOBALS['db']->query("UPDATE ecmaccountcategories_bean SET deleted='1', modified_user_id='".$current_user->id."',date_modified = '".date("Y-m-d H:i:s")."' WHERE bean_id='".$this->id."' AND bean_name='Mailing' and id NOT IN ('".implode("','",$exists)."')");
		}
		
		function savePositions56($pl) {
		
			global $current_user;
		
			$exists = array();
			foreach ($pl as $p) {
		
				if (!isset($p['product_group']) || $p['product_group']=='') continue;
		
		
				if (isset($p['id']) && $p['id']!='') {
		
					//update exists
		
					$q = "
				UPDATE ecmaccountcategories_bean set
					date_modified = '".date("Y-m-d H:i:s")."',
					modified_user_id = '".$current_user->id."',
					bean_id = '".addslashes($this->id)."',
					ecmaccountcategory_id = '".addslashes($p['cat_id'])."'
					WHERE id = '".$p['id']."'
				";
					/*
						$q = "
						UPDATE ecmmailingcategories_relation set
						parent_id = '".addslashes($this->id)."',
						ecmmailingcategoriy_id= '".addslashes($p['cat_id'])."',
						available = '".addslashes($p['discount'])."',
						parent_type = 'MailingMessage',
						parent_name = '".addslashes($p['product_group'])."'
						WHERE id = '".$p['id']."'
						";
						*/
		
					//$GLOBALS['db']->query("INSERT INTO log VALUES ('".addslashes($q)."')");
					$GLOBALS['db']->query($q);
					$exists[] = $p['id'];
		
				} else {
					//insert new record
					$id = create_guid();
					/*
						$t = array(
						$id,
						addslashes($p['cat_id']),
						'0',
						addslashes($p['discount']),
						$this->id,
						addslashes($p['product_group']),
						'MailingMessage'
		
						); */
					$t = array(
							$id,
							addslashes($p['cat_id']),
							$this->id,
							"MailingContacts",
							date("Y-m-d H:i:s"),
							date("Y-m-d H:i:s"),
							$current_user->id,
							$current_user->id,
							'0'
					);
					$q = "INSERT INTO ecmaccountcategories_bean VALUES ('".implode("','", $t)."')";
		
					$GLOBALS['db']->query($q);
					$exists[] = $id;
				}
		
			}
		
			//delete old
			$GLOBALS['db']->query("UPDATE ecmaccountcategories_bean SET deleted='1', modified_user_id='".$current_user->id."',date_modified = '".date("Y-m-d H:i:s")."' WHERE bean_id='".$this->id."' and bean_name='MailingContacts' AND id NOT IN ('".implode("','",$exists)."')");
		}
	
}
?>
