|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Craft; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Export controller. |
|
7
|
|
|
* |
|
8
|
|
|
* Handles mapping and export requests. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
|
11
|
|
|
* @copyright Copyright (c) 2015, Bob Olde Hampsink |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
* |
|
14
|
|
|
* @link http://github.com/boboldehampsink |
|
15
|
|
|
*/ |
|
16
|
|
|
class ExportController extends BaseController |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get available entry types for section. |
|
20
|
|
|
* |
|
21
|
|
|
* @return string JSON |
|
22
|
|
|
*/ |
|
23
|
|
|
public function actionGetEntryTypes() |
|
24
|
|
|
{ |
|
25
|
|
|
// Only ajax post requests |
|
26
|
|
|
$this->requirePostRequest(); |
|
27
|
|
|
$this->requireAjaxRequest(); |
|
28
|
|
|
|
|
29
|
|
|
// Get section |
|
30
|
|
|
$section = craft()->request->getPost('section'); |
|
31
|
|
|
$section = craft()->sections->getSectionById($section); |
|
32
|
|
|
|
|
33
|
|
|
// Get entry types |
|
34
|
|
|
$entrytypes = $section->getEntryTypes(); |
|
35
|
|
|
|
|
36
|
|
|
// Return JSON |
|
37
|
|
|
$this->returnJson($entrytypes); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Process input for mapping. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string HTML |
|
44
|
|
|
*/ |
|
45
|
|
|
public function actionMap() |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
// Get export posts |
|
49
|
|
|
$export = craft()->request->getRequiredPost('export'); |
|
50
|
|
|
$reset = craft()->request->getPost('reset'); |
|
51
|
|
|
|
|
52
|
|
|
// Send variables to template and display |
|
53
|
|
|
$this->renderTemplate('export/_map', array( |
|
54
|
|
|
'export' => $export, |
|
55
|
|
|
'reset' => $reset, |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Download export. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string CSV |
|
63
|
|
|
*/ |
|
64
|
|
|
public function actionDownload() |
|
65
|
|
|
{ |
|
66
|
|
|
|
|
67
|
|
|
// Get export post |
|
68
|
|
|
$settings = craft()->request->getRequiredPost('export'); |
|
69
|
|
|
|
|
70
|
|
|
// Get mapping fields |
|
71
|
|
|
$map = craft()->request->getParam('fields'); |
|
72
|
|
|
|
|
73
|
|
|
// Save map |
|
74
|
|
|
craft()->export->saveMap($settings, $map); |
|
75
|
|
|
|
|
76
|
|
|
// Set more settings |
|
77
|
|
|
$settings['map'] = $map; |
|
78
|
|
|
|
|
79
|
|
|
// Get data |
|
80
|
|
|
$data = craft()->export->download($settings); |
|
81
|
|
|
|
|
82
|
|
|
// Download the csv |
|
83
|
|
|
craft()->request->sendFile('export.csv', $data, array('forceDownload' => true, 'mimeType' => 'text/csv')); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|