Issues (57)

core/extensions/helpers/html.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   KumbiaPHP
11
 * @package    Helpers
12
 *
13
 * @copyright  Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Helper para Tags Html.
19
 *
20
 * @category   KumbiaPHP
21
 */
22
class Html
23
{
24
    /**
25
     * Metatags.
26
     *
27
     * @var array
28
     */
29
    protected static $_metatags = array();
30
    /**
31
     * Enlaces de head.
32
     *
33
     * @var array
34
     */
35
    protected static $_headLinks = array();
36
37
    /**
38
     * Crea un enlace usando la constante PUBLIC_PATH, para que siempre funcione.
39
     *
40
     * @example <?= Html::link('usuario/crear','Crear usuario') ?>
41
     * @example Imprime un enlace al controlador usuario y a la acción crear, con el texto 'Crear usuario'
42
     * @example <?= Html::link('usuario/crear','Crear usuario', 'class="button"') ?>
43
     * @example El mismo anterior, pero le añade el atributo class con valor button
44
     *
45
     * @param string       $action Ruta a la acción
46
     * @param string       $text   Texto a mostrar
47
     * @param string|array $attrs  Atributos adicionales
48
     *
49
     * @return string
50
     */
51
    public static function link($action, $text, $attrs = '')
52
    {
53
        if (is_array($attrs)) {
54
            $attrs = Tag::getAttrs($attrs);
55
        }
56
57
        return '<a href="'.PUBLIC_PATH."$action\" $attrs>$text</a>";
58
    }
59
60
    /**
61
     * Crea un enlace a una acción del mismo controller que estemos.
62
     *
63
     * @example <?= Html::linkAction('crear/','Crear') ?>
64
     * @example Imprime un enlace a la acción crear del mismo controlador en el que estemos, con el texto 'Crear'
65
     * @example <?= Html::linkAction('usuario/crear','Crear usuario', 'class="button"') ?>
66
     * @example El mismo anterior, pero le añade el atributo class con valor button
67
     *
68
     * @param string       $action Ruta a la acción
69
     * @param string       $text   Texto a mostrar
70
     * @param string|array $attrs  Atributos adicionales
71
     *
72
     * @return string
73
     */
74
    public static function linkAction($action, $text, $attrs = '')
75
    {
76
        $action = Router::get('controller_path')."/$action";
0 ignored issues
show
Are you sure Router::get('controller_path') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $action = /** @scrutinizer ignore-type */ Router::get('controller_path')."/$action";
Loading history...
77
78
        return self::link($action, $text, $attrs);
79
    }
80
81
    /**
82
     * Permite incluir una imagen, por defecto va la carpeta public/img/
83
     *
84
     * @example <?= Html::img('logo.png','Logo de KumbiaPHP') ?>
85
     * @example Imprime una etiqueta img <img src="/img/logo.png" alt="Logo de KumbiaPHP">
86
     * @example <?= Html::img('logo.png','Logo de KumbiaPHP', 'width="100px" height="100px"') ?>
87
     * @example Imprime una etiqueta img <img src="/img/logo.png" alt="Logo de KumbiaPHP" width="100px" height="100px">
88
     *
89
     * @param string       $src   Ruta de la imagen a partir de la carpeta public/img/
90
     * @param string       $alt   Texto alternativo de la imagen.
91
     * @param string|array $attrs Atributos adicionales
92
     *
93
     * @return string
94
     */
95
    public static function img($src, $alt = '', $attrs = '')
96
    {
97
        return '<img src="'.PUBLIC_PATH."img/$src\" alt=\"$alt\" ".Tag::getAttrs($attrs).'/>';
98
    }
99
100
    /**
101
     * Crea un metatag.
102
     *
103
     * @param string       $content contenido del metatag
104
     * @param string|array $attrs   atributos
105
     */
106
    public static function meta($content, $attrs = '')
107
    {
108
        if (is_array($attrs)) {
109
            $attrs = Tag::getAttrs($attrs);
110
        }
111
112
        self::$_metatags[] = array('content' => $content, 'attrs' => $attrs);
113
    }
114
115
    /**
116
     * Incluye los metatags.
117
     *
118
     * @return string
119
     */
120
    public static function includeMetatags()
121
    {
122
        $code = '';
123
        foreach (self::$_metatags as $meta) {
124
            $code .= "<meta content=\"{$meta['content']}\" {$meta['attrs']}>" . PHP_EOL;
125
        }
126
        return $code;
127
    }
128
129
    /**
130
     * Crea una lista a partir de un array.
131
     *
132
     * @param array        $array Array con el contenido de la lista
133
     * @param string       $type  por defecto ul, y si no ol
134
     * @param string|array $attrs atributos
135
     *
136
     * @return string
137
     */
138
    public static function lists($array, $type = 'ul', $attrs = '')
139
    {
140
        if (is_array($attrs)) {
141
            $attrs = Tag::getAttrs($attrs);
142
        }
143
144
        $list = "<$type $attrs>".PHP_EOL;
145
        foreach ($array as $item) {
146
            $list .= "<li>$item</li>".PHP_EOL;
147
        }
148
149
        return "$list</$type>".PHP_EOL;
150
    }
151
152
    /**
153
     * Incluye los CSS.
154
     *
155
     * @return string
156
     */
157
    public static function includeCss()
158
    {
159
        $code = '';
160
        foreach (Tag::getCss() as $css) {
161
            $code .= '<link href="'.PUBLIC_PATH."css/{$css['src']}.css\" rel=\"stylesheet\" type=\"text/css\" media=\"{$css['media']}\" />".PHP_EOL;
162
        }
163
164
        return $code;
165
    }
166
167
    /**
168
     * Enlaza un recurso externo.
169
     *
170
     * @param string       $href  direccion url del recurso a enlazar
171
     * @param string|array $attrs atributos
172
     */
173
    public static function headLink($href, $attrs = '')
174
    {
175
        if (is_array($attrs)) {
176
            $attrs = Tag::getAttrs($attrs);
177
        }
178
179
        self::$_headLinks[] = array('href' => $href, 'attrs' => $attrs);
180
    }
181
182
    /**
183
     * Enlaza una accion.
184
     *
185
     * @param string       $action ruta de accion
186
     * @param string|array $attrs  atributos
187
     */
188
    public static function headLinkAction($action, $attrs = '')
189
    {
190
        self::headLink(PUBLIC_PATH.$action, $attrs);
191
    }
192
193
    /**
194
     * Enlaza un recurso de la aplicacion.
195
     *
196
     * @param string       $resource ubicacion del recurso en public
197
     * @param string|array $attrs    atributos
198
     */
199
    public static function headLinkResource($resource, $attrs = '')
200
    {
201
        self::headLink(PUBLIC_PATH.$resource, $attrs);
202
    }
203
204
    /**
205
     * Incluye los links para el head.
206
     *
207
     * @return string
208
     */
209
    public static function includeHeadLinks()
210
    {
211
        $code = '';
212
        foreach (self::$_headLinks as $link) {
213
            $code .= "<link href=\"{$link['href']}\" {$link['attrs']} />".PHP_EOL;
214
        }
215
216
        return $code;
217
    }
218
219
    /**
220
     * Incluye imágenes de gravatar.com.
221
     *
222
     * @example Simple: <?= Html::gravatar($email); ?>
223
     * @example Completo: echo Html::gravatar( $email, $name, 20, 'http://www.example.com/default.jpg') <br>
224
     * @example Un gravatar que es un link: echo Html::link( Html::gravatar($email), $url)
225
     *
226
     * @param string $email   Correo para conseguir su gravatar
227
     * @param string $alt     Texto alternativo de la imagen. Por defecto: gravatar
228
     * @param int    $size    Tamaño del gravatar. Un número de 1 a 512. Por defecto: 40
229
     * @param string $default URL gravatar por defecto si no existe, o un default de gravatar. Por defecto: mm
230
     *
231
     * @return string
232
     */
233
    public static function gravatar($email, $alt = 'gravatar', $size = 40, $default = 'mm')
234
    {
235
        $grav_url = 'https://secure.gravatar.com/avatar/'.md5(strtolower(trim($email))).'?d='.urlencode($default).'&amp;s='.$size;
236
237
        return '<img src="'.$grav_url.'" alt="'.$alt.'" class="avatar" width="'.$size.'" height="'.$size.'" />';
238
    }
239
}
240