-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuestionEditHelper.php
More file actions
209 lines (198 loc) · 6.81 KB
/
QuestionEditHelper.php
File metadata and controls
209 lines (198 loc) · 6.81 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Question Edit Helper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator Co., Ltd. <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppHelper', 'View/Helper');
/**
* Question edit Helper
*
* @author Allcreator Co., Ltd. <info@allcreator.net>
* @package NetCommons\Questionnaires\View\Helper
*/
class QuestionEditHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
'NetCommons.Token',
'Form'
);
/**
* 質問属性設定作成
*
* @param string $fieldName フィールド名
* @param string $title 見出しラベル
* @param array $options INPUT要素に与えるオプション属性
* @param string $label checkboxの時のラベル
* @return string HTML
*/
public function questionInput($fieldName, $title, $options, $label = '') {
if (isset($options['ng-model'])) {
$ngModel = $options['ng-model'];
$modelNames = explode('.', $ngModel);
$errorMsgModelName = $modelNames[0] . '.errorMessages.' . $modelNames[1];
$ret = '<div class="row form-group" ng-class="{\'has-error\': ' . $errorMsgModelName . '}">';
} else {
$ret = '<div class="row form-group">';
}
$ret .= '<label class="col-xs-2 control-label">' . $title;
if (isset($options['required']) && $options['required'] == true) {
$ret .= $this->_View->element('NetCommons.required');
}
$ret .= '</label><div class="col-xs-10">';
$type = $options['type'];
if ($this->_View->viewVars['isPublished']) {
$options = array_merge($options, array('disabled' => true));
}
$options = array_merge($options, array('div' => false, 'label' => false));
if ($type == 'wysiwyg') {
if ($this->_View->viewVars['isPublished']) {
$ret .= '<div class="well well-sm" ng-bind-html="' . $ngModel . ' | ncHtmlContent"></div>';
} else {
$ret .= $this->NetCommonsForm->wysiwyg($fieldName, $options);
}
} elseif ($type == 'checkbox') {
$options = array_merge($options, array('label' => $label));
$ret .= $this->NetCommonsForm->checkbox($fieldName, $options);
} else {
$ret .= $this->NetCommonsForm->input($fieldName, $options);
}
if (isset($options['ng-model'])) {
$ret .= '<div class="has-error" ng-if="' . $errorMsgModelName . '">';
$ret .= '<div class="help-block" ng-repeat="errorMessage in ' . $errorMsgModelName . '">';
$ret .= '{{errorMessage}}</div></div>';
}
$ret .= '</div></div>';
return $ret;
}
/**
* アンケート属性設定作成
*
* @param string $fieldName フィールド名
* @param string $label checkboxの時のラベル
* @param array $options INPUT要素に与えるオプション属性
* @param string $help 追加説明文
* @return string HTML
*/
public function questionnaireAttributeCheckbox(
$fieldName, $label, $options = array(), $help = '') {
$ngModel = 'questionnaire.questionnaire.' . Inflector::variable($fieldName);
$ret = '<div class=" checkbox"><label>';
$options = array_merge(array(
'type' => 'checkbox',
'div' => false,
'label' => false,
'class' => '',
'error' => false,
'ng-model' => $ngModel,
//'ng-checked' => $ngModel . '==' . RegistrationsComponent::USES_USE,
// この記述でないと チェックON,OFFが正常に動作しない。
'ng-false-value' => '"0"',
'ng-true-value' => '"1"'
),
$options
);
$ret .= $this->NetCommonsForm->input($fieldName, $options);
$ret .= $label;
if (!empty($help)) {
$ret .= '<span class="help-block">' . $help . '</span>';
}
$ret .= '</label>';
$ret .= '<div class="has-error">';
$ret .= $this->NetCommonsForm->error($fieldName, null, array('class' => 'help-block'));
$ret .= '</div>';
$ret .= '</div>';
return $ret;
}
/**
* アンケート期間設定作成
*
* @param string $fieldName フィールド名
* @param array $options オプション
* @return string HTML
*/
public function questionnaireAttributeDatetime($fieldName, $options) {
$ngModel = 'questionnaire.questionnaire.' . Inflector::variable($fieldName);
$defaultOptions = array(
'type' => 'datetime',
'id' => $fieldName,
'ng-model' => $ngModel,
);
$options = array_merge($defaultOptions, $options);
if (isset($options['min']) && isset($options['max'])) {
$min = $options['min'];
$max = $options['max'];
$options = array_merge($options, array(
'ng-focus' => 'setMinMaxDate($event, \'' . $min . '\', \'' . $max . '\')',
));
}
$ret = '';
$ret .= $this->NetCommonsForm->input($fieldName, $options);
if (!empty($help)) {
$ret .= '<span class="help-block">' . $help . '</span>';
}
return $ret;
}
/**
* questionnaireGetFinallySubmit
*
* アンケートは質問編集画面、集計結果編集画面では分割送信をする
* 分割送信後、最終POSTをする時に使用するFormを作成する
*
* @param array $postUrl POST先URL情報
* @param array $ngValues 最終POSTに合わせて送る付加情報のフィールド名とng-Value名
* @return string HTML
*/
public function questionnaireGetFinallySubmit($postUrl, $ngValues = array()) {
$html = '';
$html .= $this->NetCommonsForm->create('QuestionnaireQuestion',
array_merge(array('id' => 'finallySubmitForm'), $postUrl)
);
$html .= $this->NetCommonsForm->hidden('Frame.id');
$html .= $this->NetCommonsForm->hidden('Block.id');
$html .= $this->NetCommonsForm->hidden('Questionnaire.key');
if (! empty($ngValues)) {
foreach ($ngValues as $hiddenName => $ngValue) {
$html .= $this->NetCommonsForm->hidden($hiddenName, array('ng-value' => $ngValue));
$this->NetCommonsForm->unlockField($hiddenName);
}
}
$this->NetCommonsForm->unlockField('QuestionnairePage');
$html .= $this->NetCommonsForm->end();
return $html;
}
/**
* getJsPostData
*
* アンケートは分割送信をAjaxで行う
* AjaxでPostを行うときにtoken含みの配列を取得する
*
* @param string $questionnaireKey アンケートキー(Postデータの一つとして必要)
* @param string $ajaxPostUrl Post先URL(セッション保存キーが含まれるためコントローラーからもらわないとわからない)
* @return array
*/
public function getJsPostData($questionnaireKey, $ajaxPostUrl) {
$postData = array(
'Frame' => array('id' => Current::read('Frame.id')),
'Block' => array('id' => Current::read('Block.id')),
'Questionnaire' => array('key' => $questionnaireKey),
);
$tokenFields = Hash::flatten($postData);
$hiddenFields = $tokenFields;
$hiddenFields = array_keys($hiddenFields);
$this->Token->unlockField('QuestionnairePage');
$tokens = $this->Token->getToken('Questionnaire', $ajaxPostUrl, $tokenFields, $hiddenFields);
$postData += $tokens;
return $postData;
}
}