PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/macros.php

https://gitlab.com/melentev-av/perki.dev
PHP | 125 lines | 94 code | 31 blank | 0 comment | 3 complexity | 31e3ae098d1318e74f8f01881d66116b MD5 | raw file
  1. <?php
  2. Form::macro('textField', function($name, $label = null, $value = null, $attributes = array())
  3. {
  4. $element = Form::text($name, $value, fieldAttributes($name, $attributes));
  5. return fieldWrapper($name, $label, $element);
  6. });
  7. Form::macro('passwordField', function($name, $label = null, $attributes = array())
  8. {
  9. $element = Form::password($name, fieldAttributes($name, $attributes));
  10. return fieldWrapper($name, $label, $element);
  11. });
  12. Form::macro('textareaField', function($name, $label = null, $value = null, $attributes = array())
  13. {
  14. $element = Form::textarea($name, $value, fieldAttributes($name, $attributes));
  15. return fieldWrapper($name, $label, $element);
  16. });
  17. Form::macro('selectField', function($name, $label = null, $options, $value = null, $attributes = array())
  18. {
  19. $element = Form::select($name, $options, $value, fieldAttributes($name, $attributes));
  20. return fieldWrapper($name, $label, $element);
  21. });
  22. Form::macro('selectMultipleField', function($name, $label = null, $options, $value = null, $attributes = array())
  23. {
  24. $attributes = array_merge($attributes, array('multiple' => true));
  25. $element = Form::select($name, $options, $value, fieldAttributes($name, $attributes));
  26. return fieldWrapper($name, $label, $element);
  27. });
  28. Form::macro('checkboxField', function($name, $label = null, $value = 1, $checked = null, $attributes = array())
  29. {
  30. $attributes = array_merge(array('id' => 'id-field-' . $name), $attributes);
  31. $out = '<div class="checkbox';
  32. $out .= fieldError($name) . '">';
  33. $out .= '<label>';
  34. $out .= Form::checkbox($name, $value, $checked, $attributes) . ' ' . $label;
  35. $out .= '</div>';
  36. return $out;
  37. });
  38. function fieldWrapper($name, $label, $element)
  39. {
  40. $out = '<div class="form-group';
  41. $out .= fieldError($name) . '">';
  42. $out .= fieldLabel($name, $label);
  43. $out .= $element;
  44. $out .= '</div>';
  45. return $out;
  46. }
  47. function fieldError($field)
  48. {
  49. $error = '';
  50. if ($errors = Input::old('errors'))
  51. {
  52. $error = $errors->first($field) ? ' has-error' : '';
  53. }
  54. return $error;
  55. }
  56. function fieldLabel($name, $label)
  57. {
  58. if (is_null($label)) return '';
  59. $name = str_replace('[]', '', $name);
  60. $out = '<label for="id-field-' . $name . '" class="control-label">';
  61. $out .= $label . '</label>';
  62. return $out;
  63. }
  64. function fieldAttributes($name, $attributes = array())
  65. {
  66. $name = str_replace('[]', '', $name);
  67. return array_merge(array('class' => 'form-control', 'id' => 'id-field-' . $name), $attributes);
  68. }
  69. Validator::extend('time24', function($attribute, $value, $parameters)
  70. {
  71. return preg_match('/(2[0-3]|[0-1]?[0-9]):[0-5]?[0-9](:[0-5]?[0-9])?/', $value);
  72. });
  73. Validator::extend('phone_ru', function($attribute, $value, $parameters)
  74. {
  75. return preg_match('/^\+7 \(\d{3}\) \d{3}\-\d{4}( x\d{1,6})?$/', $value);
  76. });
  77. Validator::extend('site', function($attribute, $value, $parameters)
  78. {
  79. return preg_match('/^(((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.)+(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|[\x{E000}-\x{F8FF}]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/ui', $value);
  80. });
  81. Validator::extend('ceil', function($attribute, $value, $parameters)
  82. {
  83. return $value % $parameters[0] == 0;
  84. });
  85. Password::validator(function($credentials)
  86. {
  87. return strlen($credentials['password']) >= 4;
  88. });
  89. Str::macro('prep_input', function($str) {
  90. return strip_tags(trim(strval($str)));
  91. });
  92. Str::macro('ucfirst', function($str, $enc = 'utf-8') {
  93. return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
  94. });