Commit de50abc5 authored by Micheal Mand's avatar Micheal Mand Committed by Nicolas Widart

Add i18nInputOfType macro to match new normalInputOfType from #247 (#250)

Also run CS fixer on macros file
Signed-off-by: 's avatarMicheal Mand <micheal@kmdwebdesigns.com>
parent 71a5f393
...@@ -8,19 +8,21 @@ use Illuminate\Support\ViewErrorBag; ...@@ -8,19 +8,21 @@ use Illuminate\Support\ViewErrorBag;
| Translatable fields | Translatable fields
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
/** /*
* Add an input field * Add a translatable input field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param string $lang the language of the field * @param string $lang the language of the field
* @param null|object $object The entity of the field * @param null|object $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('i18nInput', function ($name, $title, ViewErrorBag $errors, $lang, $object = null, array $options = []) { Form::macro('i18nInput', function ($name, $title, ViewErrorBag $errors, $lang, $object = null, array $options = []) {
$options = array_merge(['class' => "form-control", 'placeholder' => $title], $options); $options = array_merge(['class' => 'form-control', 'placeholder' => $title], $options);
$string = "<div class='form-group " . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group " . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>";
$string .= Form::label("{$lang}[{$name}]", $title); $string .= Form::label("{$lang}[{$name}]", $title);
if (is_object($object)) { if (is_object($object)) {
...@@ -31,24 +33,57 @@ Form::macro('i18nInput', function ($name, $title, ViewErrorBag $errors, $lang, $ ...@@ -31,24 +33,57 @@ Form::macro('i18nInput', function ($name, $title, ViewErrorBag $errors, $lang, $
$string .= Form::text("{$lang}[{$name}]", old("{$lang}[{$name}]", $currentData), $options); $string .= Form::text("{$lang}[{$name}]", old("{$lang}[{$name}]", $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>'); $string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a textarea * Add a translatable input field of specified type
*
* @param string $type The type of field
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param string $lang the language of the field * @param string $lang the language of the field
* @param null|object $object The entity of the field * @param null|object $object The entity of the field
*
* @return HtmlString
*/
Form::macro('i18nInputOfType', function ($type, $name, $title, ViewErrorBag $errors, $lang, $object = null, array $options = []) {
$options = array_merge(['class' => 'form-control', 'placeholder' => $title], $options);
$string = "<div class='form-group " . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>";
$string .= Form::label("{$lang}[{$name}]", $title);
if (is_object($object)) {
$currentData = $object->hasTranslation($lang) ? $object->translate($lang)->{$name} : '';
} else {
$currentData = '';
}
$string .= Form::input($type, "{$lang}[{$name}]", old("{$lang}[{$name}]", $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= '</div>';
return new HtmlString($string);
});
/*
* Add a translatable textarea field
*
* @param string $name The field name
* @param string $title The field title
* @param object $errors The laravel errors object
* @param string $lang the language of the field
* @param null|object $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang, $object = null, array $options = []) { Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang, $object = null, array $options = []) {
$options = array_merge(['class' => 'ckeditor', 'rows' => 10, 'cols' => 10], $options); $options = array_merge(['class' => 'ckeditor', 'rows' => 10, 'cols' => 10], $options);
$string = "<div class='form-group " . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group " . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>";
$string .= Form::label("{$lang}[{$name}]", $title); $string .= Form::label("{$lang}[{$name}]", $title);
if (is_object($object)) { if (is_object($object)) {
...@@ -59,18 +94,20 @@ Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang ...@@ -59,18 +94,20 @@ Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang
$string .= Form::textarea("{$lang}[$name]", old("{$lang}[{$name}]", $currentData), $options); $string .= Form::textarea("{$lang}[$name]", old("{$lang}[{$name}]", $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>'); $string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a checkbox input field * Add a translatable checkbox input field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param string $lang the language of the field * @param string $lang the language of the field
* @param null|object $object The entity of the field * @param null|object $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang, $object = null) { Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang, $object = null) {
...@@ -79,7 +116,7 @@ Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang ...@@ -79,7 +116,7 @@ Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang
$string .= "<input id='{$lang}[{$name}]' name='{$lang}[{$name}]' type='checkbox' class='flat-blue'"; $string .= "<input id='{$lang}[{$name}]' name='{$lang}[{$name}]' type='checkbox' class='flat-blue'";
if (is_object($object)) { if (is_object($object)) {
$currentData = $object->hasTranslation($lang) ? (bool) $object->translate($lang)->{$name} : ''; $currentData = $object->hasTranslation($lang) ? (bool)$object->translate($lang)->{$name} : '';
} else { } else {
$currentData = false; $currentData = false;
} }
...@@ -88,30 +125,32 @@ Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang ...@@ -88,30 +125,32 @@ Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang
$string .= "value='1' {$oldInput}>"; $string .= "value='1' {$oldInput}>";
$string .= $title; $string .= $title;
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</label>"; $string .= '</label>';
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a dropdown select field * Add a translatable dropdown select field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param string $lang the language of the field * @param string $lang the language of the field
* @param array $choice The choice of the select * @param array $choice The choice of the select
* @param null|array $object The entity of the field * @param null|array $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang, array $choice, $object = null, array $options = []) { Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang, array $choice, $object = null, array $options = []) {
if (array_key_exists("multiple", $options)) { if (array_key_exists('multiple', $options)) {
$nameForm = "{$lang}[$name][]"; $nameForm = "{$lang}[$name][]";
} else { } else {
$nameForm = "{$lang}[$name]"; $nameForm = "{$lang}[$name]";
} }
$string = "<div class='form-group dropdown" . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group dropdown" . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>";
$string .= "<label for='$nameForm'>$title</label>"; $string .= "<label for='$nameForm'>$title</label>";
if (is_object($object)) { if (is_object($object)) {
...@@ -123,7 +162,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang, ...@@ -123,7 +162,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
/* Bootstrap default class */ /* Bootstrap default class */
$array_option = ['class' => 'form-control']; $array_option = ['class' => 'form-control'];
if (array_key_exists("class", $options)) { if (array_key_exists('class', $options)) {
$array_option = ['class' => $array_option['class'] . ' ' . $options['class']]; $array_option = ['class' => $array_option['class'] . ' ' . $options['class']];
unset($options['class']); unset($options['class']);
} }
...@@ -132,7 +171,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang, ...@@ -132,7 +171,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
$string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options); $string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>'); $string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
...@@ -142,18 +181,20 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang, ...@@ -142,18 +181,20 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
| Standard fields | Standard fields
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
/** /*
* Add an input field * Add an input field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param null|object $object The entity of the field * @param null|object $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $object = null, array $options = []) { Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $object = null, array $options = []) {
$options = array_merge(['class' => "form-control", 'placeholder' => $title], $options); $options = array_merge(['class' => 'form-control', 'placeholder' => $title], $options);
$string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>";
$string .= Form::label($name, $title); $string .= Form::label($name, $title);
if (is_object($object)) { if (is_object($object)) {
...@@ -164,12 +205,12 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec ...@@ -164,12 +205,12 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec
$string .= Form::text($name, old($name, $currentData), $options); $string .= Form::text($name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add an input field of specified type * Add an input field of specified type
* *
* @param string $type The type of field * @param string $type The type of field
...@@ -181,7 +222,7 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec ...@@ -181,7 +222,7 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec
* @return HtmlString * @return HtmlString
*/ */
Form::macro('normalInputOfType', function ($type, $name, $title, ViewErrorBag $errors, $object = null, array $options = []) { Form::macro('normalInputOfType', function ($type, $name, $title, ViewErrorBag $errors, $object = null, array $options = []) {
$options = array_merge(['class' => "form-control", 'placeholder' => $title], $options); $options = array_merge(['class' => 'form-control', 'placeholder' => $title], $options);
$string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>";
$string .= Form::label($name, $title); $string .= Form::label($name, $title);
...@@ -194,23 +235,26 @@ Form::macro('normalInputOfType', function ($type, $name, $title, ViewErrorBag $e ...@@ -194,23 +235,26 @@ Form::macro('normalInputOfType', function ($type, $name, $title, ViewErrorBag $e
$string .= Form::input($type, $name, old($name, $currentData), $options); $string .= Form::input($type, $name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a textarea field
*
* @param string $name * @param string $name
* @param string $title * @param string $title
* @param ViewErrorBag $errors * @param ViewErrorBag $errors
* @param null|object $object * @param null|object $object
* @param array $options * @param array $options
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $object = null, array $options = []) { Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $object = null, array $options = []) {
$options = array_merge(['class' => 'ckeditor', 'rows' => 10, 'cols' => 10], $options); $options = array_merge(['class' => 'ckeditor', 'rows' => 10, 'cols' => 10], $options);
$string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>"; $string = "<div class='form-group " . ($errors->has($name) ? ' has-error' : '') . "'>";
$string .= Form::label($name, $title); $string .= Form::label($name, $title);
if (is_object($object)) { if (is_object($object)) {
...@@ -221,17 +265,19 @@ Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $ob ...@@ -221,17 +265,19 @@ Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $ob
$string .= Form::textarea($name, old($name, $currentData), $options); $string .= Form::textarea($name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a checkbox input field * Add a checkbox input field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param null|object $object The entity of the field * @param null|object $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $object = null) { Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $object = null) {
...@@ -241,7 +287,7 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob ...@@ -241,7 +287,7 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob
$string .= "<input id='$name' name='$name' type='checkbox' class='flat-blue'"; $string .= "<input id='$name' name='$name' type='checkbox' class='flat-blue'";
if (is_object($object)) { if (is_object($object)) {
$currentData = isset($object->$name) && (bool) $object->$name ? 'checked' : ''; $currentData = isset($object->$name) && (bool)$object->$name ? 'checked' : '';
} else { } else {
$currentData = false; $currentData = false;
} }
...@@ -250,23 +296,25 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob ...@@ -250,23 +296,25 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob
$string .= "value='1' {$oldInput}>"; $string .= "value='1' {$oldInput}>";
$string .= $title; $string .= $title;
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</label>"; $string .= '</label>';
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
/** /*
* Add a dropdown select field * Add a dropdown select field
*
* @param string $name The field name * @param string $name The field name
* @param string $title The field title * @param string $title The field title
* @param object $errors The laravel errors object * @param object $errors The laravel errors object
* @param array $choice The choice of the select * @param array $choice The choice of the select
* @param null|array $object The entity of the field * @param null|array $object The entity of the field
*
* @return HtmlString * @return HtmlString
*/ */
Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array $choice, $object = null, array $options = []) { Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array $choice, $object = null, array $options = []) {
if (array_key_exists("multiple", $options)) { if (array_key_exists('multiple', $options)) {
$nameForm = $name . '[]'; $nameForm = $name . '[]';
} else { } else {
$nameForm = $name; $nameForm = $name;
...@@ -284,7 +332,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array ...@@ -284,7 +332,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array
/* Bootstrap default class */ /* Bootstrap default class */
$array_option = ['class' => 'form-control']; $array_option = ['class' => 'form-control'];
if (array_key_exists("class", $options)) { if (array_key_exists('class', $options)) {
$array_option = ['class' => $array_option['class'] . ' ' . $options['class']]; $array_option = ['class' => $array_option['class'] . ' ' . $options['class']];
unset($options['class']); unset($options['class']);
} }
...@@ -293,7 +341,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array ...@@ -293,7 +341,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array
$string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options); $string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>'); $string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>"; $string .= '</div>';
return new HtmlString($string); return new HtmlString($string);
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment