# shiftcontroller/4.9.95/sh4/schedule/html/view/download.php

ShiftController Employee Shift Scheduling, version 4.9.95. 222 lines.

- Page: https://pluginprobe.com/plugins/shiftcontroller/4.9.95/code/sh4/schedule/html/view/download.php
- Raw: https://pluginprobe.com/plugins/shiftcontroller/4.9.95/raw/sh4/schedule/html/view/download.php
- Modified: 2025-10-02T06:10:46+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/shiftcontroller/4.9.95/code/sh4/schedule/html/view/download.php#L10-L20`.

```php
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
#[AllowDynamicProperties]
class SH4_Schedule_Html_View_Download
{
	public function __construct( 
		HC3_Hooks $hooks,

		HC3_Ui $ui,
		HC3_Request $request,
		HC3_Time $t,
		HC3_Translate $translate,
		HC3_Settings $settings,

		SH4_Shifts_Presenter $shiftsPresenter,

		SH4_Shifts_Query $shiftsQuery,
		SH4_Calendars_Presenter $calendarsPresenter,
		SH4_Shifts_View_Widget $widget,
		SH4_Schedule_Html_View_Common $common
		)
	{
		$this->ui = $ui;
		$this->t = $t;
		$this->request = $request;
		$this->translate = $translate;

		$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter );
		$this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
		$this->shiftsQuery = $hooks->wrap( $shiftsQuery );

		$this->widget = $hooks->wrap( $widget );
		$this->common = $hooks->wrap( $common );
		$this->settings = $hooks->wrap($settings);

		$this->self = $hooks->wrap( $this );
	}

    private function report()
    {
        $params = $this->request->getParams();
        $startDate = $params['start'];

        $session = HC3_Session::instance();
        $separator = $session->getUserdata('pref_csv_separator');
        if (!$separator) {
            $separator = $this->settings->get('pref_csv_separator');
        }
        if (!$separator) {
            $separator = ',';
        }

        $groupby = $session->getUserdata('scheduleViewGroupby');
        if (!$groupby) {
            $groupby = $this->settings->get('default_schedule_groupby');
        }
        if (!$groupby) {
            $groupby = 'employee';
        }

        $shifts = $this->common->getShifts();

        $dt1 = $this->shiftsQuery->getStart();
        $dt2 = $this->shiftsQuery->getEnd();
        // $d1 = $this->t->setDateTimeDb($dt1)->formatDateDb();
        // $d2 = $this->t->setDateTimeDb($dt2)->formatDateDb();

        $d1 = $this->t->setDateTimeDb($dt1)->formatDate();
        $d1 = $this->translate->translate($d1);
        $d2 = $this->t->setDateTimeDb($dt2)->formatDate();
        $d2 = $this->translate->translate($d2);

        $allEmployees = $this->common->findAllEmployees();
        $allCalendars = $this->common->findAllCalendars();

        $body = [];
        foreach ($shifts as $shift) {
            $shiftOut = $this->shiftsPresenter->export($shift);

            if ('employee' == $groupby) {
                $key = $shiftOut['employee_id'];
            } elseif ('calendar' == $groupby) {
                $key = $shiftOut['calendar_id'];
            } else {
                $key = 0;
            }

            if (!isset($body[$key])) {
                if ('employee' == $groupby) {
                    $body[$key]['employee_id'] = $shiftOut['employee_id'];
                    $body[$key]['employee'] = $shiftOut['employee'];
                } elseif ('calendar' == $groupby) {
                    $body[$key]['calendar_id'] = $shiftOut['calendar_id'];
                    $body[$key]['calendar'] = $shiftOut['calendar'];
                } else {
                    $key = 0;
                }

                $body[$key]['duration'] = 0;
                $body[$key]['qty'] = 0;
                if (array_key_exists('salary', $shiftOut)) {
                    $body[$key]['salary'] = 0;
                }
            }

            $body[$key]['qty'] += 1;
            $body[$key]['duration'] += $shiftOut['duration'];
            if (isset($shiftOut['salary'])) {
                $body[$key]['salary'] += $shiftOut['salary'];
            }
        }

        $out = array();
        foreach ($body as $line) {
            $keys = array_keys($line);

            reset($keys);
            foreach ($keys as $k) {
                if (isset($line[$k])) {
                    $line[$k] = $this->translate->translate($line[$k]);
                }
            }

            $out[] = HC3_Functions::buildCsv($line, $separator);
        }

        if ($out) {
            $header = array_keys(current($body));
            $header = array_combine($header, $header);
            $header = HC3_Functions::buildCsv($header, $separator);
            array_unshift($out, $header);
        }

        $out = join("\n", $out);

        $fileName = 'report-' . $d1 . '-' . $d2;
        // $fileName .= '-' . date('Y-m-d_H-i') . '.csv';
        $fileName .= '--' . date('YmdHi') . '.csv';
// echo $fileName;
// exit;

        HC3_Functions::pushDownload( $fileName, $out );
        exit;
    }

	public function render()
	{
		$params = $this->request->getParams();
        if (isset($params['report']) && $params['report']) {
            return $this->report();
        }


		$startDate = $params['start'];

		$shifts = $this->common->getShifts();

		$iknow = array();
		$hori = TRUE;

		$allEmployees = $this->common->findAllEmployees();
		$allCalendars = $this->common->findAllCalendars();

		$session = HC3_Session::instance();
		$separator = $session->getUserdata( 'pref_csv_separator' );
		
		if( ! $separator ){
			$separator = $this->settings->get( 'pref_csv_separator' );
		}
		if( ! $separator ){
			$separator = ',';
		}

		$shiftsOut = array();
		$header = array();

		foreach( $shifts as $shift ){
			$thisOut = $this->shiftsPresenter->export( $shift );

			$thisHeader = array_keys( $thisOut );
			foreach( $thisHeader as $th ){
				if( ! isset($header[$th]) ){
					$header[$th] = $th;
				}
			}

			$shiftsOut[] = $thisOut;
		}

		$keys = array_keys( $header );
		$out = array();
		reset( $shiftsOut );
		foreach( $shiftsOut as $shiftOut ){
			$thisOut = array();

			reset( $keys );
			foreach( $keys as $k ){
				if( isset($shiftOut[$k]) ){
					$thisOut[$k] = $this->translate->translate( $shiftOut[$k] );
				}
				else {
					$thisOut[$k] = NULL;
				}
			}

			$thisOut = HC3_Functions::buildCsv( $thisOut, $separator );
			$out[] = $thisOut;
		}

		if( $out ){
			$header = HC3_Functions::buildCsv( $header, $separator );
			$header = array_unshift( $out, $header );
		}

		$out = join("\n", $out);

		$fileName = 'export';
		$fileName .= '-' . date('Y-m-d_H-i') . '.csv';

		HC3_Functions::pushDownload( $fileName, $out );
		exit;
	}
}
```
