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;
| Translatable fields
|--------------------------------------------------------------------------
*/
/**
* Add an input field
/*
* Add a translatable input 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
*/
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);
if (is_object($object)) {
......@@ -31,24 +33,57 @@ Form::macro('i18nInput', function ($name, $title, ViewErrorBag $errors, $lang, $
$string .= Form::text("{$lang}[{$name}]", old("{$lang}[{$name}]", $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
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 $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
*/
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
*/
Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang, $object = null, array $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);
if (is_object($object)) {
......@@ -59,18 +94,20 @@ Form::macro('i18nTextarea', function ($name, $title, ViewErrorBag $errors, $lang
$string .= Form::textarea("{$lang}[$name]", old("{$lang}[{$name}]", $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
return new HtmlString($string);
});
/**
* Add a checkbox input field
/*
* Add a translatable checkbox input 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
*/
Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang, $object = null) {
......@@ -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'";
if (is_object($object)) {
$currentData = $object->hasTranslation($lang) ? (bool) $object->translate($lang)->{$name} : '';
$currentData = $object->hasTranslation($lang) ? (bool)$object->translate($lang)->{$name} : '';
} else {
$currentData = false;
}
......@@ -88,30 +125,32 @@ Form::macro('i18nCheckbox', function ($name, $title, ViewErrorBag $errors, $lang
$string .= "value='1' {$oldInput}>";
$string .= $title;
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</label>";
$string .= "</div>";
$string .= '</label>';
$string .= '</div>';
return new HtmlString($string);
});
/**
* Add a dropdown select field
/*
* Add a translatable dropdown select 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 array $choice The choice of the select
* @param null|array $object The entity of the field
*
* @return HtmlString
*/
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][]";
} else {
$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>";
if (is_object($object)) {
......@@ -123,7 +162,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
/* Bootstrap default class */
$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']];
unset($options['class']);
}
......@@ -132,7 +171,7 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
$string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
return new HtmlString($string);
});
......@@ -142,18 +181,20 @@ Form::macro('i18nSelect', function ($name, $title, ViewErrorBag $errors, $lang,
| Standard fields
|--------------------------------------------------------------------------
*/
/**
/*
* Add an input field
*
* @param string $name The field name
* @param string $title The field title
* @param object $errors The laravel errors object
* @param null|object $object The entity of the field
*
* @return HtmlString
*/
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);
if (is_object($object)) {
......@@ -164,12 +205,12 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec
$string .= Form::text($name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
return new HtmlString($string);
});
/**
/*
* Add an input field of specified type
*
* @param string $type The type of field
......@@ -181,7 +222,7 @@ Form::macro('normalInput', function ($name, $title, ViewErrorBag $errors, $objec
* @return HtmlString
*/
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 .= Form::label($name, $title);
......@@ -194,23 +235,26 @@ Form::macro('normalInputOfType', function ($type, $name, $title, ViewErrorBag $e
$string .= Form::input($type, $name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
return new HtmlString($string);
});
/**
/*
* Add a textarea field
*
* @param string $name
* @param string $title
* @param ViewErrorBag $errors
* @param null|object $object
* @param array $options
*
* @return HtmlString
*/
Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $object = null, array $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);
if (is_object($object)) {
......@@ -221,17 +265,19 @@ Form::macro('normalTextarea', function ($name, $title, ViewErrorBag $errors, $ob
$string .= Form::textarea($name, old($name, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
return new HtmlString($string);
});
/**
/*
* Add a checkbox input field
*
* @param string $name The field name
* @param string $title The field title
* @param object $errors The laravel errors object
* @param null|object $object The entity of the field
*
* @return HtmlString
*/
Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $object = null) {
......@@ -241,7 +287,7 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob
$string .= "<input id='$name' name='$name' type='checkbox' class='flat-blue'";
if (is_object($object)) {
$currentData = isset($object->$name) && (bool) $object->$name ? 'checked' : '';
$currentData = isset($object->$name) && (bool)$object->$name ? 'checked' : '';
} else {
$currentData = false;
}
......@@ -250,23 +296,25 @@ Form::macro('normalCheckbox', function ($name, $title, ViewErrorBag $errors, $ob
$string .= "value='1' {$oldInput}>";
$string .= $title;
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</label>";
$string .= "</div>";
$string .= '</label>';
$string .= '</div>';
return new HtmlString($string);
});
/**
/*
* Add a dropdown select field
*
* @param string $name The field name
* @param string $title The field title
* @param object $errors The laravel errors object
* @param array $choice The choice of the select
* @param null|array $object The entity of the field
*
* @return HtmlString
*/
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 . '[]';
} else {
$nameForm = $name;
......@@ -284,7 +332,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array
/* Bootstrap default class */
$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']];
unset($options['class']);
}
......@@ -293,7 +341,7 @@ Form::macro('normalSelect', function ($name, $title, ViewErrorBag $errors, array
$string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options);
$string .= $errors->first($name, '<span class="help-block">:message</span>');
$string .= "</div>";
$string .= '</div>';
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