-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuestionnaireExport.php
152 lines (141 loc) · 4.49 KB
/
QuestionnaireExport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* QuestionnaireExport Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('QuestionnairesAppModel', 'Questionnaires.Model');
App::uses('WysiwygZip', 'Wysiwyg.Utility');
/**
* Summary for Questionnaire Model
*/
class QuestionnaireExport extends QuestionnairesAppModel {
/**
* Use table config
*
* @var bool
*/
public $useTable = 'questionnaires';
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'AuthorizationKeys.AuthorizationKey',
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @see Model::__construct()
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->loadModels([
'Questionnaire' => 'Questionnaires.Questionnaire',
'QuestionnairePage' => 'Questionnaires.QuestionnairePage',
'QuestionnaireQuestion' => 'Questionnaires.QuestionnaireQuestion',
]);
}
/**
* getExportData
*
* @param string $questionnaireKey アンケートキー
* @return array QuestionnaireData for Export
*/
public function getExportData($questionnaireKey) {
// アンケートデータをjsonにして記述した内容を含むZIPファイルを作成する
$zipData = array();
// バージョン情報を取得するためComposer情報を得る
$Plugin = ClassRegistry::init('PluginManager.Plugin');
$composer = $Plugin->getComposer('netcommons/questionnaires');
// 最初のデータはアンケートプラグインのバージョン
$zipData['version'] = $composer['version'];
// 言語数分
$Language = ClassRegistry::init('M17n.Language');
$languages = $Language->getLanguage();
$questionnaires = array();
foreach ($languages as $lang) {
// 指定のアンケートデータを取得
$questionnaire = $this->Questionnaire->find('first', array(
'conditions' => array(
'Questionnaire.key' => $questionnaireKey,
'Questionnaire.is_active' => true,
'Questionnaire.is_latest' => true,
'Questionnaire.language_id' => $lang['Language']['id']
),
'recursive' => 0
));
// 指定の言語データがない場合もあることを想定
if (empty($questionnaire)) {
continue;
}
$questionnaire = [
'Questionnaire' => $questionnaire['Questionnaire'],
'QuestionnairePage' => $questionnaire['QuestionnairePage'],
];
$this->Questionnaire->clearQuestionnaireId($questionnaire);
$questionnaires[] = $questionnaire;
}
// Exportするデータが一つも見つからないって
if (empty($questionnaires)) {
return false;
}
$zipData['Questionnaires'] = $questionnaires;
return $zipData;
}
/**
* putToZip
*
* @param ZipDownloader $zipFile ZIPファイルオブジェクト
* @param array $zipData zip data
* @return void
*/
public function putToZip($zipFile, $zipData) {
$wysiswyg = new WysiwygZip();
// アンケートデータの中でもWYSISWYGデータのものについては
// フォルダ別に確保(フォルダの中にZIPがある)
$flatQuestionnaire = Hash::flatten($zipData);
foreach ($flatQuestionnaire as $key => &$value) {
$model = null;
if (strpos($key, 'QuestionnaireQuestion.') !== false) {
$model = $this->QuestionnaireQuestion;
} elseif (strpos($key, 'QuestionnairePage.') !== false) {
$model = $this->QuestionnairePage;
} elseif (strpos($key, 'Questionnaire.') !== false) {
$model = $this->Questionnaire;
}
if (!$model) {
continue;
}
$columnName = substr($key, strrpos($key, '.') + 1);
if ($model->hasField($columnName)) {
if ($model->getColumnType($columnName) == 'text') {
$wysiswygZipFile = $wysiswyg->createWysiwygZip($value, $model->alias . '.' . $columnName);
$wysiswygFileName = $key . '.zip';
$zipFile->addFile($wysiswygZipFile, $wysiswygFileName);
$value = $wysiswygFileName;
}
}
}
$questionnaire = Hash::expand($flatQuestionnaire);
// jsonデータにして書き込み
$zipFile->addFromString(
QuestionnairesComponent::QUESTIONNAIRE_JSON_FILENAME,
json_encode($questionnaire));
}
}