From 6943d492b0a110a4159fb76f0f5abe4f3774a761 Mon Sep 17 00:00:00 2001 From: winternet-studio Date: Tue, 11 Jul 2017 18:29:17 +0200 Subject: [PATCH] Added class Common --- Common.php | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Common.php diff --git a/Common.php b/Common.php new file mode 100644 index 0000000..4455fd7 --- /dev/null +++ b/Common.php @@ -0,0 +1,115 @@ + 0) {". ($options['on_error'] ? $options['on_error'] .'({form:form, rsp:rsp, errorCount:errorCount});' : '') ."} else {". ($options['on_success'] ? $options['on_success'] .'({form:form, rsp:rsp, errorCount:errorCount});' : '') ."}"; + } + if ($options['on_complete']) { + $js .= $options['on_complete'] .'({form:form, rsp:rsp, errorCount:errorCount});'; + } + $js .= "}"; + return new \yii\web\JsExpression($js); + } + + public static function processAjaxSubmitError($options = []) { + /* + DESCRIPTION: + - generate Javascript code for handling a failed Ajax request with a JSON response, eg. a 500 Internal Server Error + INPUT: + - $options : associative array with any of these keys: + - 'minimal' : set to true to generate very minimal code + OUTPUT: + - Javascript expression + */ + if ($options['minimal']) { + $js = "function(r,t,e) {"; + $js .= "alert(e+\"\\n\\n\"+\$('
').html(r.responseJSON.message).text());"; + $js .= "}"; + } else { + $js = "function(xhr, textStatus, errorThrown) {"; + $js .= "var \$bg = \$('
').addClass('jfw-yii2-ajax-error-bg').css({position: 'fixed', top: '0px', left: '0px', width: '100%', backgroundColor: '#595959'}).height(\$(window).height());"; + $js .= "var \$modal = \$('
').addClass('msg').css({position: 'fixed', top: '100px', left: '50%', transform: 'translateX(-50%)', width: '70%', marginLeft: 'auto', marginRight: 'auto', backgroundColor: '#EEEEEE', padding: '30px', boxShadow: '0px 0px 28px 5px #232323'});"; + $js .= "\$modal.html('

'+ errorThrown +'

'+ xhr.responseJSON.message +'
');"; + $js .= "\$bg.append(\$modal);"; + $js .= "\$('body').append(\$bg);"; + $js .= "}"; + } + return new \yii\web\JsExpression($js); + } + + public static function addResultErrors($result, &$model, $options = []) { + /* + DESCRIPTION: + - add errors from a Yii model to a standard result array + - the new key 'err_msg_ext' MUST then be used for processing it (because 'err_msg' might not contain all error messages) + INPUT: + - $result : empty variable (null, false, whatever) or an associative array in this format: ['status' => 'ok|error', 'result_msg' => [], 'err_msg' => []] + - $model : a Yii model + - $options : associative array with any of these keys: + - 'add_existing' : add the existing 'err_msg' array entries to 'err_msg_ext' + OUTPUT: + - associative array in the format of $result but with the new key 'err_msg_ext' + */ + if (!is_array($result)) { + $result = [ + 'status' => 'ok', + 'result_msg' => [], + 'err_msg' => [], + ]; + } + + $modelErrors = $model->getErrors(); + + foreach ($modelErrors as $attr => $errors) { + // Generate the form field ID so Yii ActiveForm client-side can apply the error message + if (!$modelName) { + $modelName = $model::className(); + $modelName = mb_strtolower(substr($modelName, strrpos($modelName, '\\')+1)); + } + + $result['err_msg_ext'][$modelName .'-'. mb_strtolower($attr) ] = $errors; + } + + + if ($options['add_existing']) { + if (!empty($result['err_msg'])) { + $result['err_msg_ext']['_global'] = $result['err_msg']; + } + } + + // Ensure correct status + if (!empty($result['err_msg']) || !empty($result['err_msg_ext'])) { + $result['status'] = 'error'; + } + + return $result; + } +}