Completed
Push — master ( 8f71bf...ff33fd )
by Angus
04:36
created

History::arrayToCSV()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 4
nop 5
dl 0
loc 21
ccs 0
cts 0
cp 0
crap 42
rs 8.7624
c 0
b 0
f 0
1
<?php defined('BASEPATH') or exit('No direct script access allowed');
2
3
class History extends Auth_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
	}
7
8 View Code Duplication
	public function index(int $page = 1) {
9
		if($page === 0) redirect('user/history/1');
10
11
		$this->header_data['title'] = "History";
12
		$this->header_data['page']  = "history";
13
14
		$historyData = $this->History->userGetHistory($page);
15
		$this->body_data['historyData'] = $historyData['rows'];
16
		$this->body_data['currentPage'] = (int) $page;
17
		$this->body_data['totalPages']  = $historyData['totalPages'];
18
19
		if($page > $this->body_data['totalPages'] && $page <= 1) redirect('user/history/1');
20
21
		$this->_render_page('User/History');
22
	}
23
24
	public function export(string $type) : void {
25
		$historyData = $this->History->userGetHistoryAll();
26
27
		switch($type) {
28
			case 'json':
29
				$this->_render_json($historyData, TRUE, 'tracker-history');
30
				break;
31
32
			case 'csv':
33
				$this->output->set_content_type('text/csv', 'utf-8');
34
				$this->_render_content($this->arrayToCSVRecursive($historyData), 'csv',TRUE, 'tracker-history');
35
				break;
36
37
			default:
38
				//404
39
				break;
40
		}
41
	}
42
43
	/**
44
	 * Formats a line (passed as a fields  array) as CSV and returns the CSV as a string.
45
	 * Adapted from http://us3.php.net/manual/en/function.fputcsv.php#87120
46
	 * SEE: http://stackoverflow.com/a/3933816/1168377
47
	 */
48
	private function arrayToCSV(array &$fields, $delimiter = ',', $enclosure = '"', $encloseAll = FALSE, $nullToMysqlNull = FALSE) {
49
		$delimiter_esc = preg_quote($delimiter, '/');
50
		$enclosure_esc = preg_quote($enclosure, '/');
51
52
		$output = array();
53
		foreach ($fields as $field) {
54
			if ($field === NULL && $nullToMysqlNull) {
55
				$output[] = 'NULL';
56
				continue;
57
			}
58
59
			// Enclose fields containing $delimiter, $enclosure or whitespace
60
			if ($encloseAll || preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field)) {
61
				$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
62
			} else {
63
				$output[] = $field;
64
			}
65
		}
66
67
		return implode($delimiter, $output);
68
	}
69
	private function arrayToCSVRecursive(array &$fields, $delimiter = ',', $enclosure = '"', $encloseAll = FALSE, $nullToMysqlNull = FALSE) {
70
		$csvArr = [
71
			'Date/Time,Title,URL,Site,Status'
72
		];
73
		foreach ($fields as $field) {
74
			$csvArr[] = $this->arrayToCSV($field, $delimiter, $enclosure,$encloseAll,$nullToMysqlNull);
75
		}
76
77
		return implode(PHP_EOL,$csvArr);
78
	}
79
}
80