<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id: s_timeprotocol_create.php christian $
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

/**
 * Used to create, save, load and assign data for the report
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class S_timeprotocol_create
{
    
    /**
     * Shows the report
     *
     * DO NOT USE XAJAX WITHIN THIS METHOD
     *
     * @param   object  $CI         CI
     * @param   array   $params     The parameters
     * @return  string  The page
     *
     * @uses US_Smarty
     */
    public static function view($CI, array $params=array())
    {
        $CI->login->restrict_right(Login_model::R_WORKINPUT, Login_model::L_MEMBEROFSTAFF, __CLASS__.'::'.__METHOD__);
        
        $CI->load->us_library('report_data/s_projectmatrix_times');
        $CI->s_projectmatrix_times = new S_projectmatrix_times();
        $CI->load->us_library('report_data/staff_timeprotocol_times');
        $CI->staff_timeprotocol_times = new Staff_timeprotocol_times();
        $CI->load->model('Overtime_model', 'overtime', true);
        $CI->load->model('Staff_model', 'staff');
        
        $request_vars = $params['request_vars'];
        $datesetting = $CI->web_settings->getDateSetting();
        
        if (!isset($request_vars->date)) {
            basic_error('Date missing', null, __CLASS__.'::'.__FUNCTION__);
        }
        
        $staff_group_id = false;
        $staff_id = false;
        if (isset($request_vars->staff__staff)) {
            $staff_id = intval($request_vars->staff__staff);
        } elseif (isset($request_vars->staff__staff_group)) {
            $staff_group_id = intval($request_vars->staff__staff_group);
        }
        
        $cost_centre = false;
        if (isset($request_vars->cost_centre)) {
            $cost_centre = $request_vars->cost_centre;
        }
        $view_overtime_details = (isset($request_vars->view_overtime_details)) ? (boolean)$request_vars->view_overtime_details : false;
        $view_is_private_project = (isset($request_vars->view_is_private_project)) ? (boolean)$request_vars->view_is_private_project : false;
        
        $project_manager_id = false;
        $project_manager_id_other = false;
        if ($CI->login->getbool('respect_project_manager')) {
            $project_manager_id = $CI->login->get('project_manager_id');
            if ($CI->login->getbool('allow_staff_even_if_not_by_project_manager') && $CI->login->getbool('allow_own_project_if_other_project_manager')) {
                $project_manager_id_other = $CI->login->get_user_id();
            }
        }
        $division_manager_id = false;
        $division_manager_id_other = false;
        if ($CI->login->getbool('respect_division_manager')) {
            $division_manager_id = $CI->login->get('division_manager_id');
            if ($CI->login->getbool('allow_staff_even_if_not_by_division_manager') && $CI->login->getbool('allow_own_project_if_other_division_manager')) {
                $division_manager_id_other = $CI->login->get_user_id();
            }
        }
        
        switch ($params['group_by']) {
            case 'day':
                break;
            case 'month':
                $dates = us_first_last_day_of_month($request_vars->date);
                $request_vars->date = $dates->first;
                break;
            default:
                basic_error('Invalid group by '.$params['group_by'], My_controller::M_DIRECT, __CLASS__.'::'.__METHOD__);
        }
        
        $CI->staff_timeprotocol_times->set_filter(null, $request_vars->date, $params['group_by'], $view_overtime_details, $view_is_private_project);
        $staff_list = $CI->s_projectmatrix_times->get_staff($staff_id, $staff_group_id, $request_vars->date, $CI->staff_timeprotocol_times->get_to(), $cost_centre, $project_manager_id, $division_manager_id, $project_manager_id_other, $division_manager_id_other);
        
        $temp_file = Report_config::get_report_tmp_name(US_Reports::FILE_PREFIX_TEMP);
        
        $CI->smarty->assignGlobal('all_set_ok', true);
        $CI->smarty->assign('project_manager_id', $project_manager_id);
        $CI->smarty->assign('project_manager_id_other', $project_manager_id_other);
        $CI->smarty->assign('division_manager_id', $division_manager_id);
        $CI->smarty->assign('division_manager_id_other', $division_manager_id_other);
        $CI->smarty->assign('view_is_private_project', $view_is_private_project);
        
        $is_first = true;
        foreach ($staff_list as $staff_item) {
            $CI->staff_timeprotocol_times->set_filter($staff_item->id, $request_vars->date, $params['group_by'], $view_overtime_details, $view_is_private_project);
            
            $CI->staff_timeprotocol_times->get_projects();
            $totals = $CI->staff_timeprotocol_times->get_for_times();
            
            $CI->smarty->assign('totals', $totals);
            $CI->smarty->assign('staff_name', $staff_item->staff_name);
            
            if ($is_first) {
                if (isset($params['my_id'])) {
                    $CI->smarty->assign('timeprotocol_my_id', $params['my_id']);
                }
                $CI->smarty->assign('timeprotocol_group_by', $params['group_by']);
                $CI->smarty->assign('view_overtime_details', $view_overtime_details);
                $CI->smarty->assign('s_timeprotocol_file_id', Report::file_id_from_name($temp_file));
                $CI->smarty->assign('s_timeprotocol_type', $params['type']);
                $CI->smarty->assign('from_date', $request_vars->date);
                $CI->smarty->assign('to_date', $CI->staff_timeprotocol_times->get_to());
                $CI->smarty->assign('target_hours_header', 0);
                if ($datesetting=='arab') {
                    $CI->smarty->assign('view_weekend1', 6);
                    $CI->smarty->assign('view_weekend2', 7);
                } else {
                    $CI->smarty->assign('view_weekend1', 1);
                    $CI->smarty->assign('view_weekend2', 7);
                }
                file_put_contents($temp_file, $CI->smarty->view('reportsview/s_timeprotocol_header', true) );
                $is_first = false;
            }
            
            file_put_contents($temp_file, $CI->smarty->view('reportsview/staff_timeprotocol_totals', true), FILE_APPEND );
            
            unset($totals);
        }
        file_put_contents($temp_file, $CI->smarty->view('reportsview/s_timeprotocol_footer', true), FILE_APPEND );
        
        return file_get_contents($temp_file);
    }
    
    /**
     * View as pdf
     *
     * @param   object  $CI         CI
     * @param   array   $params     The parameters
     * @param   string  $op_name    Output design name
     * @return  void
     */
    public static function result_pdf($CI, array $params, $op_name)
    {
        $CI->login->restrict_right(Login_model::R_WORKINPUT, Login_model::L_MEMBEROFSTAFF, __CLASS__.'::'.__METHOD__);
        
        $file_id = $CI->input->post('val3');
        $report_design = $CI->input->post('val6');
        if ($report_design=='XNOX') {
            $report_design = false;
        }
        $report_format = $CI->input->post('val8');
        if ($report_format=='XNOX') {
            $report_format = 'A4';
        }
                
        $CI->load->helper('report/report_pdf');
        $CI->load->us_library('US_report_view_result');
        
        $temp_file_in = Report::name_from_file_id($file_id, true);
        
        $viewinfo = Report_config::get_view_settings('tab-pdf');
        $designinfo = Report_config::get_design_settings($report_design, $op_name);
        
        
        $designinfo['add_page_margin'] = true;
        Report::assign_for_smarty($designinfo, $params['lang_title']);
        $reply = '';

        $reply .= $CI->smarty->view('Reports/basics/header', true);
        $reply .= $CI->smarty->view('Reports/basics/head', true);
        
        $reply .= file_get_contents($temp_file_in);
        
        $reply .= $CI->smarty->view('Reports/basics/footer', true);
        
        
        $temp_file = Report_config::get_report_tmp_name(US_Reports::FILE_PREFIX_TEMP);
        
        Report::set_header($viewinfo);
        
        file_put_contents($temp_file, $reply);
        Report_pdf_helper::create(US_report_view_result::M_DIRECT, $report_format, 'landscape', $temp_file);
    }
    
}
