<?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".
 * ****************************************************************************** */

require_once('include/Dashlets/Dashlet.php');
require_once('include/Sugar_Smarty.php');

class MyEcmCalendarsDashlets extends Dashlet {

	var $height = '200';
	var $act_status;
	var $act_user;
	var $act_type;

	function MyEcmCalendarsDashlets($id, $def) {
		$this->loadLanguage('MyEcmCalendarsDashlets'); // load the language strings here

		if (!empty($def['savedText']))  // load default text is none is defined
			$this->savedText = $def['savedText'];
		else
			$this->savedText = $this->dashletStrings['LBL_DEFAULT_TEXT'];

		if (!empty($def['height'])) // set a default height if none is set
			$this->height = $def['height'];

		$this->act_status = $def['act_status'];
		$this->act_user = $def['act_user'];
		$this->act_type = $def['act_type'];

		parent::Dashlet($id); // call parent constructor

		$this->isConfigurable = true; // dashlet is configurable
		$this->hasScript = true;  // dashlet has javascript attached to it
		// if no custom title, use default
		if (empty($def['title']))
			$this->title = $this->dashletStrings['LBL_TITLE'];
		else
			$this->title = $def['title'];
	}

	/**
	 * Displays the dashlet
	 * 
	 * @return string html to display dashlet
	 */
	function display() {
		$ss = new Sugar_Smarty();
		$ss->assign('savedText', $this->savedText);
		$ss->assign('saving', $this->dashletStrings['LBL_SAVING']);
		$ss->assign('saved', $this->dashletStrings['LBL_SAVED']);
		$ss->assign('id', $this->id);
		$ss->assign('height', $this->height);

		$str = $ss->fetch('modules/Home/Dashlets/MyEcmCalendarsDashlets/MyEcmCalendarsDashlets.tpl');
		require_once("modules/EcmCalendars/Calendar.php");
		//	require_once("modules/EcmCalendars/language/en_us.lang.php");
		$cal = new Calendar();
		if ($_REQUEST['date']) {
			$exp = explode("-", $_REQUEST['date']);
			$cal->year = $exp[0];
			$cal->month = $exp[1];
		} else {
			$cal->year = date("Y");
			$cal->month = date("m");
		}
		$cal->act_status = $this->act_status;
		$cal->act_user = $this->act_user;
		$cal->act_type = $this->act_type;
		if (!$this->act_type)
			$cal->act_type = "All";
		$_SESSION['calendar_dashlet_id'] = $this->id;
		$str = '
		<link href="modules/EcmCalendars/style.css" rel="stylesheet" type="text/css">
		<script src="modules/EcmCalendars/helper.js" language="javascript"></script>
		<script src="modules/EcmCalendars/Calendar.js" language="javascript"></script>
		<div id="calendar_dashlet">' . $cal->showTableDashlet($cal->month, $cal->year) . '</div>
		';
		return parent::display("") . $str . '<br />'; // return parent::display for title and such
	}

	/**
	 * Displays the javascript for the dashlet
	 * 
	 * @return string javascript to use with this dashlet
	 */
	function displayScript() {
		$ss = new Sugar_Smarty();
		$ss->assign('saving', $this->dashletStrings['LBL_SAVING']);
		$ss->assign('saved', $this->dashletStrings['LBL_SAVED']);
		$ss->assign('id', $this->id);

		$str = $ss->fetch('modules/Home/Dashlets/MyEcmCalendarsDashlets/MyEcmCalendarsDashletsScript.tpl');
		return $str; // return parent::display for title and such
	}

	/**
	 * Displays the configuration form for the dashlet
	 * 
	 * @return string html to display form
	 */
	function displayOptions() {
		global $current_language;
		$app_list_strings = return_app_list_strings_language($current_language);

		global $app_strings;
		$ss = new Sugar_Smarty();
		$as = '<option value="All"';
		if ($this->act_status == 'All')
			$as.=' selected';
		$as.='>'.$app_strings['LBL_ALL'].'</option>';
		foreach ($app_list_strings['meeting_status_dom'] as $k => $v) {
			$as.='<option value="' . $k . '"';
			if ($this->act_status == $k)
				$as.=" selected";
			$as.='>' . $v . '</option>';
		}
		$ss->assign('act_status_options', $as);
		//TO DO
		$arr = array("Calls" => "Rozmowy telefoniczne", "Meetings" => "Spotkania", "Tasks" =>"Zadania");
		$at = '<option value="All"';
		if ($this->act_status == 'All')
			$at.=' selected';
		$at.='>'.$app_strings['LBL_ALL'].'</option>';
		
		foreach ($arr as $ar => $v) {
			$at.='<option value="' . $ar . '"';
			if ($this->act_type == $ar)
				$at.=' selected';
			$at.='>' . $v . '</option>';
		}
		$ss->assign('act_type_options', $at);

		//gotowe
		$ua = '<option value="All"';
		if ($this->act_user == 'All')
			$ua.=' selected';
		$ua.='>'.$app_strings['LBL_ALL'].'</option>';
		$db = $GLOBALS ['db'];
		$q = "select CONCAT(ifnull(first_name,'') , ' ', ifnull(last_name,'')) as user_name,id from users where deleted='0' order by user_name asc";
		$rs = $db->query($q);
		$result = array();
		if ($rs->num_rows > 0) {
			while ($r = $db->fetchByAssoc($rs)) {
				$ua.='<option value="' . $r['id'] . '"';
				if ($r['id'] == $this->act_user)
					$ua.=' selected';
				$ua.='>' . $r['user_name'] . '</option>';
			}
		}

		$ss->assign('act_user_options', $ua);

		$ss->assign('titleLbl', $this->dashletStrings['LBL_CONFIGURE_TITLE']);
		$ss->assign('statusLbl', $this->dashletStrings['LBL_CONFIGURE_STATUS']);
		$ss->assign('userLbl', $this->dashletStrings['LBL_CONFIGURE_USER']);
		$ss->assign('typeLbl', $this->dashletStrings['LBL_CONFIGURE_TYPE']);
		$ss->assign('saveLbl', $app_strings['LBL_SAVE_BUTTON_LABEL']);
		$ss->assign('title', $this->title);
		$ss->assign('height', $this->height);
		$ss->assign('id', $this->id);

		return parent::displayOptions() . $ss->fetch('modules/Home/Dashlets/MyEcmCalendarsDashlets/MyEcmCalendarsDashletsOptions.tpl');
	}

	/**
	 * called to filter out $_REQUEST object when the user submits the configure dropdown
	 * 
	 * @param array $req $_REQUEST
	 * @return array filtered options to save
	 */
	function saveOptions($req) {
		global $sugar_config, $timedate, $current_user, $theme;
		$options = array();
		$options['title'] = $_REQUEST['title'];
		$options['act_status'] = $_REQUEST['act_status'];
		$options['act_user'] = $_REQUEST['act_user'];
		$options['act_type'] = $_REQUEST['act_type'];

		if (is_numeric($_REQUEST['height'])) {
			if ($_REQUEST['height'] > 0 && $_REQUEST['height'] <= 300)
				$options['height'] = $_REQUEST['height'];
			elseif ($_REQUEST['height'] > 300)
				$options['height'] = '300';
			else
				$options['height'] = '100';
		}

//        $options['savedText'] = br2nl($this->savedText);
		$options['savedText'] = $this->savedText;

		return $options;
	}

	/**
	 * Used to save text on textarea blur. Accessed via Home/CallMethodDashlet.php
	 * This is an example of how to to call a custom method via ajax
	 */
	function saveText() {
		$json = getJSONobj();
		if (isset($_REQUEST['savedText'])) {
			$optionsArray = $this->loadOptions();
//            _pp($_REQUEST['savedText']);
			$optionsArray['savedText'] = $json->decode(html_entity_decode($_REQUEST['savedText']));
			$optionsArray['savedText'] = nl2br($optionsArray['savedText']['jsonObject']);
			$this->storeOptions($optionsArray);
		} else {
			$optionsArray['savedText'] = '';
		}
		echo 'result = ' . $json->encode(array('id' => $_REQUEST['id'],
			'savedText' => $optionsArray['savedText']));
	}

}

?>
