<?php
/*********************************************************************************
 * The contents of this file are subject to the TimeTrex Public License Version
 * 1.1.0 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.TimeTrex.com/TPL
 * 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 TimeTrex" logo and
 *    (ii) the TimeTrex copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: TimeTrex Open Source
 * The Initial Developer of the Original Code is TimeTrex Payroll Services
 * Portions created by TimeTrex are Copyright (C) 2004-2007 TimeTrex Payroll Services;
 * All Rights Reserved.
 *
 ********************************************************************************/
/*
 * $Revision: 1414 $
 * $Id: EditUserPreference.php 1414 2007-11-14 18:50:05Z ipso $
 * $Date: 2007-11-14 10:50:05 -0800 (Wed, 14 Nov 2007) $
 */
require_once('../../includes/global.inc.php');
require_once(Environment::getBasePath() .'includes/Interface.inc.php');

if ( !$permission->Check('user_preference','enabled')
		OR !( $permission->Check('user_preference','edit') OR $permission->Check('user_preference','edit_child') OR $permission->Check('user_preference','edit_own') ) ) {
	$permission->Redirect( FALSE ); //Redirect
}

$smarty->assign('title', TTi18n::gettext($title = 'Employee Preferences')); // See index.php

/*
 * Get FORM variables
 */
extract	(FormVariables::GetVariables(
										array	(
												'action',
												'user_id',
												'incomplete',
												'pref_data',
												'data_saved',
												) ) );

$hlf = new HierarchyListFactory();
$permission_children_ids = $hlf->getHierarchyChildrenByCompanyIdAndUserIdAndObjectTypeId( $current_company->getId(), $current_user->getId() );
//Include current user in list.
if ( $permission->Check('user_preference','edit_own') ) {
	$permission_children_ids[] = $current_user->getId();
}

$upf = new UserPreferenceFactory();
$ulf = new UserListFactory();
$action = Misc::findSubmitButton('action');
switch ($action) {
	case 'submit':
		//Debug::setVerbosity( 11 );
		Debug::Text('Submit!', __FILE__, __LINE__, __METHOD__,10);

		if ( $pref_data['id'] != '' ) {
			$upf->setId( $pref_data['id'] );
		}

		if ( isset($pref_data['user_id']) AND $pref_data['user_id'] != '' ) {
			$upf->setUser( $pref_data['user_id'] );
		} else {
			$upf->setUser( $current_user->getId() );
		}

		$upf->setLanguage( $pref_data['language'] );
		if ( $pref_data['language'] == 'en' ) {
			$upf->setDateFormat( $pref_data['date_format'] );
		} else {
			$upf->setDateFormat( $pref_data['other_date_format'] );
		}

		$upf->setTimeFormat( $pref_data['time_format']);
		$upf->setTimeUnitFormat( $pref_data['time_unit_format'] );
		$upf->setTimeZone( $pref_data['time_zone'] );
		//$upf->setTimeSheetView( $pref_data['timesheet_view'] );
		$upf->setStartWeekDay( $pref_data['start_week_day'] );
		$upf->setItemsPerPage( $pref_data['items_per_page'] );

		if ( isset($pref_data['enable_email_notification_exception']) ) {
			$upf->setEnableEmailNotificationException( TRUE );
		} else {
			$upf->setEnableEmailNotificationException( FALSE );
		}

		if ( isset($pref_data['enable_email_notification_message']) ) {
			$upf->setEnableEmailNotificationMessage( TRUE );
		} else {
			$upf->setEnableEmailNotificationMessage( FALSE );
		}

		if ( isset($pref_data['enable_email_notification_home']) ) {
			$upf->setEnableEmailNotificationHome( TRUE );
		} else {
			$upf->setEnableEmailNotificationHome( FALSE );
		}

		if ( $upf->isValid() ) {
			$upf->Save();

			if ( $current_user->getId() == $pref_data['user_id'] ) {
				TTi18n::setLocaleCookie( $pref_data['language'].'_'.$current_user->getCountry() );
			}

			Redirect::Page( URLBuilder::getURL( array('user_id' => $pref_data['user_id'], 'data_saved' => 1), Environment::getBaseURL().'/users/EditUserPreference.php') );

			break;
		}
	default:
		if ( !isset($user_id) OR (isset($user_id) AND $user_id == '' ) ) {
			$user_id = $current_user->getId();
		}

		$ulf->getByIdAndCompanyId( $user_id, $current_company->getId() );
		if ( $ulf->getRecordCount() > 0 ) {
			$user_obj = $ulf->getCurrent();
		}

		if ( !isset($action) ) {
			BreadCrumb::setCrumb($title);

			$uplf = new UserPreferenceListFactory();
			$uplf->getByUserIDAndCompanyID( $user_id, $current_company->getId() );

			if ( isset($user_obj) AND is_object( $user_obj) ) {
				$is_owner = $permission->isOwner( $user_obj->getCreatedBy(), $user_obj->getId() );
				$is_child = $permission->isChild( $user_obj->getId(), $permission_children_ids );

				if ( $permission->Check('user_preference','edit')
						OR ( $permission->Check('user_preference','edit_own') AND $is_owner === TRUE )
						OR ( $permission->Check('user_preference','edit_child') AND $is_child === TRUE ) ) {

					foreach ($uplf as $user_preference) {
							$pref_data = array(
												'id' => $user_preference->getId(),
												'user_id' => $user_preference->getUser(),
												'user_full_name' => $user_obj->getFullName(),
												'language' =>  $user_preference->getLanguage(),
												'date_format' => $user_preference->getDateFormat(),
												'other_date_format'=> $user_preference->getDateFormat(),
												'time_format' => $user_preference->getTimeFormat(),
												'time_zone' => $user_preference->getTimeZone(),
												'time_unit_format' => $user_preference->getTimeUnitFormat(),
												'timesheet_view' => $user_preference->getTimeSheetView(),
												'start_week_day' => $user_preference->getStartWeekDay(),
												'items_per_page' => $user_preference->getItemsPerPage(),
												'enable_email_notification_exception' => $user_preference->getEnableEmailNotificationException(),
												'enable_email_notification_message' => $user_preference->getEnableEmailNotificationMessage(),
												'enable_email_notification_home' => $user_preference->getEnableEmailNotificationHome(),
												'created_date' => $user_preference->getCreatedDate(),
												'created_by' => $user_preference->getCreatedBy(),
												'updated_date' => $user_preference->getUpdatedDate(),
												'updated_by' => $user_preference->getUpdatedBy(),
												'deleted_date' => $user_preference->getDeletedDate(),
												'deleted_by' => $user_preference->getDeletedBy()
											);
					}
				}
			}
		}

		if ( !isset($pref_data) AND isset($user_obj) ) {
			$udlf = new UserDefaultListFactory();
			$udlf->getByCompanyId( $current_company->getId() );
			if ( $udlf->getRecordCount() > 0 ) {
				Debug::Text('Using User Defaults', __FILE__, __LINE__, __METHOD__,10);
				$udf_obj = $udlf->getCurrent();

				$pref_data = array(
								'user_id' => $user_obj->getId(),
								'user_full_name' => $user_obj->getFullName(),
								'language' =>  $udf_obj->getLanguage(),
								'date_format' => $udf_obj->getDateFormat(),
								'other_date_format' => $udf_obj->getDateFormat(),
								'time_format' => $udf_obj->getTimeFormat(),
								'time_zone' => $udf_obj->getTimeZone(),
								'time_unit_format' => $udf_obj->getTimeUnitFormat(),
								'start_week_day' => $udf_obj->getStartWeekDay(),
								'items_per_page' => $udf_obj->getItemsPerPage(),
							);
			} else {
				$pref_data = array(
								'user_id' => $user_obj->getId(),
								'user_full_name' => $user_obj->getFullName(),
								'language' =>  'en',
								'items_per_page' => 25,
							);
			}
		}

		//Select box options;
		$pref_data['language_options'] = TTi18n::getLanguageArray();
		$pref_data['date_format_options'] = $upf->getOptions('date_format');
		$pref_data['other_date_format_options'] = $upf->getOptions('other_date_format');

		$pref_data['time_format_options'] = $upf->getOptions('time_format');
		$pref_data['time_unit_format_options'] = $upf->getOptions('time_unit_format');
		$pref_data['timesheet_view_options'] = $upf->getOptions('timesheet_view');
		$pref_data['start_week_day_options'] = $upf->getOptions('start_week_day');

		$timezone_options = Misc::prependArray( array(-1 => '---'), $upf->getOptions('time_zone') );
		$pref_data['time_zone_options'] = $timezone_options;

		$smarty->assign_by_ref('pref_data', $pref_data);
		$smarty->assign_by_ref('incomplete', $incomplete);
		$smarty->assign_by_ref('data_saved', $data_saved);
		break;
}
$smarty->assign_by_ref('upf', $upf);

$smarty->display('users/EditUserPreference.tpl');
?>