<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*
 * Creates demo data for the user table
 *
 * 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".
 */



class UserDemoData {
	var $_user;
	var $_large_scale_test;
	var $guids = array(
		'jim'	=> 'jim_id',
		'sarah'	=> 'sarah_id',
		'sally'	=> 'sally_id',
		'max'	=> 'max_id',
		'will'	=> 'will_id',
		'chris'	=> 'chris_id',
	/*
	 * Pending fix of demo data mechanism
		'jim'	=> 'jim00000-0000-0000-0000-000000000000',
		'sarah'	=> 'sarah000-0000-0000-0000-000000000000',
		'sally'	=> 'sally000-0000-0000-0000-000000000000',
		'max'	=> 'max00000-0000-0000-0000-000000000000',
		'will'	=> 'will0000-0000-0000-0000-000000000000',
		'chris'	=> 'chris000-0000-0000-0000-000000000000',
	*/
	);
	
	/**
	 * Constructor for creating user demo data
	 */
	function UserDemoData($seed_user, $large_scale_test = false)
	{
		// use a seed user so it does not have to be known which file to
		// include the User class from
		$this->_user = $seed_user;
		$this->_large_scale_test = $large_scale_test;
	}
	
	/**
	 * 
	 */
	function create_demo_data() {
		// Create the VP
		if(!$this->_user->retrieve($this->guids['jim'])) {
			$this->_create_seed_user($this->guids['jim'], "Brennan", "Jim", "jim",
				"VP Sales", false, null, null, "jim@example.com");
		}
	
		// Create the west team
		if(!$this->_user->retrieve($this->guids['sarah'])) {
			$this->_create_seed_user($this->guids['sarah'], "Smith", "Sarah", "sarah",
				"Sales Manager West", false, $this->guids['jim'], "Brennan, Jim", "sarah@example.com");
		}
		if(!$this->_user->retrieve($this->guids['sally'])) {
			$this->_create_seed_user($this->guids['sally'], "Bronsen", "Sally", "sally",
				"Senior Account Rep", false, $this->guids['sarah'], "Smith, Sarah", "sally@example.com");
		}
		if(!$this->_user->retrieve($this->guids['max'])) {
			$this->_create_seed_user($this->guids['max'], "Jensen", "Max", "max",
				"Account Rep", false, $this->guids['sarah'], "Smith, Sarah", "max@example.com");
		}
	
		// Create the east team
		if(!$this->_user->retrieve($this->guids['will'])) {
			$this->_create_seed_user($this->guids['will'], "Westin", "Will", "will",
				"Sales Manager East", false, $this->guids['jim'], "Brennan, Jim", "will@example.com");
		}
		if(!$this->_user->retrieve($this->guids['chris'])) {
			$this->_create_seed_user($this->guids['chris'], "Olliver", "Chris", "chris",
				"Senior Account Rep", false, $this->guids['will'], "Westin, Will", "chris@example.com");
		}
		
		if($this->_large_scale_test) {
			$user_list = $this->_seed_data_get_user_list();
			foreach($user_list as $user_name)
			{
				$this->_quick_create_user($user_name);
			}
		}
	}

	/**
	 * creates seed users using multi-byte characters to simulate real-world
	 * login conditions
	 */
	function create_demo_data_jp() {
		// Create the VP
		if(!$this->_user->retrieve($this->guids['jim'])) {
			$this->_create_seed_user($this->guids['jim'], "ブレナン", "ジーム", "jim",
				"VP Sales", false, null, null, "jim@example.com");
		}
	
		// Create the west team
		if(!$this->_user->retrieve($this->guids['sarah'])) {
			$this->_create_seed_user($this->guids['sarah'], "スミス", "サーラー", "sarah",
				"Sales Manager West", false, $this->guids['jim'], "ブレナン, ジーム", "sarah@example.com");
		}
		if(!$this->_user->retrieve($this->guids['sally'])) {
			$this->_create_seed_user($this->guids['sally'], "ブロンソン", "サーリー", "sally",
				"Senior Account Rep", false, $this->guids['sarah'], "スミス, サーラー", "sally@example.com");
		}
		if(!$this->_user->retrieve($this->guids['max'])) {
			$this->_create_seed_user($this->guids['max'], "ジェンソン", "マクス", "max",
				"Account Rep", false, $this->guids['sarah'], "スミス、サーラー", "max@example.com");
		}
	
		// Create the east team
		if(!$this->_user->retrieve($this->guids['will'])) {
			$this->_create_seed_user($this->guids['will'], "ウエストン", "ウイル", "will",
				"Sales Manager East", false, $this->guids['jim'], "ブレナン, ジーム", "will@example.com");
		}
		if(!$this->_user->retrieve($this->guids['chris'])) {
			$this->_create_seed_user($this->guids['chris'], "オリバー", "クリス", "chris",
				"Senior Account Rep", false, $this->guids['will'], "ウエストン, ウイル", "chris@example.com");
		}
	}


	function create_demo_data_jp_mssql() {
   		// mssql is broken for mbcs
   		$this->create_demo_data();
	}



	/**
	 *  Create a user in the seed data.
	 */
	function _create_seed_user($id, $last_name, $first_name, $user_name,
		$title, $is_admin, $reports_to, $reports_to_name, $email)
	{
        $u = new User();
		
		$u->id=$id;
		$u->new_with_id = true;
		$u->last_name = $last_name;
		$u->first_name = $first_name;
		$u->user_name = $user_name;
		$u->title = $title;
		$u->status = 'Active';
		$u->employee_status = 'Active';
		$u->is_admin = $is_admin;
		//$u->user_password = $u->encrypt_password($user_name);
		$u->user_hash = strtolower(md5($user_name));
		$u->reports_to_id = $reports_to;
		$u->reports_to_name = $reports_to_name;
		//$u->email1 = $email;
		$u->emailAddress->addAddress($email, true);
		$u->emailAddress->addAddress("reply.".$email, false, true);
		$u->emailAddress->addAddress("alias.".$email);
		
		// bug 15371 tyoung set a user preference so that Users/DetailView.php can find something without repeatedly querying the db in vain
		$u->setPreference('max_tabs','12'); 
		UserPreference::savePreferencesToDB($u,true);
		
		$u->save();
	}
	
	/**
	 * 
	 */
	function _seed_data_get_user_list()
	{
		$users = Array();

		$users[] = "north";
		$users[] = "south";
		$users[] = "east";
		$users[] = "west";
		$users[] = "left";
		$users[] = "right";
		$users[] = "in";
		$users[] = "out";
		$users[] = "fly";
		$users[] = "walk";
		$users[] = "crawl";
		$users[] = "pivot";
		$users[] = "money";
		$users[] = "dinero";
		$users[] = "shadow";
		$users[] = "roof";
		$users[] = "sales";
		$users[] = "pillow";
		$users[] = "feather";

		return $users;
	}
	
	/**
	 * 
	 */
	function _quick_create_user($name)
	{
		if (!$this->_user->retrieve($name.'_id'))
		{
			$this->_create_seed_user("{$name}_id", $name, $name, $name,
				"Sales Manager of no territory", false, "jim_id", "Brennan, Jim", "jim@example.com");
		}
	}
	
}
?>
