/**
* @return string
* @throws NotAcceptableHttpException
*/
public function run()
{
if (!Yii::$app->request->isAjax) {
throw new NotAcceptableHttpException('This action AJAX only!');
}
$post = Yii::$app->request->post();
$keys = array_keys($post);
$formName = $keys[0];
if (!isset($post[$formName][$this->textFormatField])) {
throw new InvalidParamException('Invalid POST data.');
}
$format = $post[$formName][$this->textFormatField];
$text = $post[$formName][$this->textField];
if ($this->context === null) {
$this->context = Yii::$app->controller;
if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) {
$this->context = Yii::$app->controller->module;
}
}
if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) {
throw new InvalidParamException('Invalid context. Add TextFormatsBehavior to module.');
}
$formats = $this->context->textFormats;
if (!isset($formats[$format])) {
throw new InvalidParamException('Format not found.');
}
$params = ['fieldName' => $formName . '[' . $this->textField . ']', 'text' => $text, 'formatInfo' => $formats[$format], 'widgetOptions' => $this->context->textEditorWidgetOptions];
if ($this->view) {
return $this->controller->renderAjax($this->view, $params);
} else {
return $this->controller->renderAjax('@vendor/maddoger/yii2-textformats/views/changeFormat.php', $params);
}
}