Completed
Push — master ( 8136d6...4fa96a )
by ARCANEDEV
03:06
created

Link::activateModalWithIcon()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 1
nop 4
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 2
1
<?php namespace Arcanesoft\Core\Helpers\UI;
2
3
use Illuminate\Contracts\Support\Htmlable;
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class     Link
9
 *
10
 * @package  Arcanesoft\Core\Helpers\UI
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @TODO: Refactoring
14
 */
15
class Link implements Htmlable
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
    /** @var string */
22
    protected $action;
23
24
    /** @var string */
25
    protected $url;
26
27
    /** @var \Illuminate\Support\Collection */
28
    protected $attributes;
29
30
    /** @var bool */
31
    protected $disabled;
32
33
    /** @var string */
34
    protected $size = 'md';
35
36
    /** @var bool */
37
    public $withTitle = true;
38
39
    /** @var bool */
40
    protected $withIcon = true;
41
42
    /** @var bool */
43
    protected $withTooltip = true;
44
45
    /* -----------------------------------------------------------------
46
     |  Constructor
47
     | -----------------------------------------------------------------
48
     */
49
    /**
50
     * LinkElement constructor.
51
     *
52
     * @param  string  $action
53
     * @param  string  $url
54
     * @param  array   $attributes
55
     * @param  bool    $disabled
56
     */
57 75
    public function __construct($action, $url, array $attributes = [], $disabled = false)
58
    {
59 75
        $this->disabled   = $disabled;
60 75
        $this->action     = $action;
61 75
        $this->url        = $url;
62
63 75
        $this->setAttributes($attributes);
64 75
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Getters & Setters
68
     | -----------------------------------------------------------------
69
     */
70
    /**
71
     * Get the action title.
72
     *
73
     * @return string
74
     */
75 63
    protected function getActionTitle()
76
    {
77 63
        return Str::ucfirst(trans("core::actions.{$this->action}"));
78
    }
79
80
    /**
81
     * Set the attributes.
82
     *
83
     * @param  array  $attributes
84
     *
85
     * @return self
86
     */
87 75
    public function setAttributes(array $attributes)
88
    {
89 75
        $this->attributes = collect($attributes);
90
91 75
        if ($this->disabled)
92 39
            $this->cleanAttributes();
93
94 75
        return $this;
95
    }
96
97
    /**
98
     * Set an attribute.
99
     *
100
     * @param  string  $key
101
     * @param  mixed   $value
102
     *
103
     * @return self
104
     */
105 6
    public function setAttribute($key, $value)
106
    {
107 6
        $this->attributes->put($key, $value);
108
109 6
        return $this;
110
    }
111
112
    protected function cleanAttributes()
113
    {
114 21
        $this->attributes = $this->attributes->reject(function ($value, $key) {
115 6
            return Str::startsWith($key, ['data-']);
116 21
        });
117
118 21
        return $this;
119
    }
120
121
    /**
122
     * Set the size.
123
     *
124
     * @param  string  $size
125
     *
126
     * @return self
127
     */
128 75
    public function size($size)
129
    {
130 75
        $this->size = $size;
131
132 75
        return $this;
133
    }
134
135
    /**
136
     * @param  bool  $withTitle
137
     *
138
     * @return self
139
     */
140 72
    public function withTitle($withTitle)
141
    {
142 72
        $this->withTitle = $withTitle;
143
144 72
        return $this;
145
    }
146
147
    /**
148
     * Show/Hide the icon.
149
     *
150
     * @var  bool  $icon
151
     *
152
     * @return self
153
     */
154 18
    public function icon($withIcon)
155
    {
156 18
        $this->withIcon = $withIcon;
157
158 18
        return $this;
159
    }
160
161
    /**
162
     * Enable the tooltip.
163
     *
164
     * @param  bool  $withTooltip
165
     *
166
     * @return self
167
     */
168 24
    public function tooltip($withTooltip)
169
    {
170 24
        $this->withTooltip = $withTooltip;
171
172 24
        return $this;
173
    }
174
175
    /**
176
     * Show only the icon and the title as tooltip.
177
     *
178
     * @return self
179
     */
180
    public function onlyIcon()
181
    {
182
        return $this->icon(true)->tooltip(true);
183
    }
184
185
    /* -----------------------------------------------------------------
186
     |  Main Methods
187
     | -----------------------------------------------------------------
188
     */
189
    /**
190
     * @param  string  $action
191
     * @param  string  $url
192
     * @param  array   $attributes
193
     * @param  bool    $disabled
194
     *
195
     * @return self
196
     */
197 75
    public static function make($action, $url, array $attributes = [], $disabled = false)
198
    {
199 75
        return new static($action, $url, $attributes, $disabled);
200
    }
201
202
    /* -----------------------------------------------------------------
203
     |  Other Functions
204
     | -----------------------------------------------------------------
205
     */
206
    /**
207
     * Get content as a string of HTML.
208
     *
209
     * @return string
210
     */
211 75
    public function toHtml()
212
    {
213 75
        return '<a'.$this->renderAttributes().'>'.$this->renderValue().'</a>';
214
    }
215
216
    /**
217
     * Render the link value.
218
     *
219
     * @return string
220
     */
221 75
    protected function renderValue()
222
    {
223 75
        return ($this->withTooltip || ! $this->withTitle)
224 67
            ? $this->renderIcon()
225 75
            : $this->renderIcon().' '.$this->getActionTitle();
226
    }
227
228
    /**
229
     * Render the icon.
230
     *
231
     * @return string
232
     */
233 75
    protected function renderIcon()
234
    {
235 75
        $icon = Arr::get([
236 75
            'add'     => 'fa fa-fw fa-plus',
237 25
            'delete'  => 'fa fa-fw fa-trash-o',
238 25
            'detach'  => 'fa fa-fw fa-chain-broken',
239 25
            'disable' => 'fa fa-fw fa-power-off',
240 25
            'edit'    => 'fa fa-fw fa-pencil',
241 25
            'enable'  => 'fa fa-fw fa-power-off',
242 25
            'restore' => 'fa fa-fw fa-reply',
243 25
            'show'    => 'fa fa-fw fa-search',
244 25
            'update'  => 'fa fa-fw fa-pencil',
245 75
        ], $this->action);
246
247 75
        return '<i class="'.$icon.'"></i>';
248
    }
249
250
    /**
251
     * Render the attributes.
252
     *
253
     * @return string
254
     */
255 75
    protected function renderAttributes()
256
    {
257 75
        $attributes = collect();
258 75
        $attributes->put('href',  $this->disabled ? 'javascript:void(0);' : $this->url);
259 75
        $attributes->put('class', $this->getLinkClass());
260
261 75
        if ($this->withTooltip) {
262 51
            $attributes->put('data-toggle', 'tooltip');
263 51
            $attributes->put('data-original-title', $this->getActionTitle());
264 17
        }
265
266 75
        if ($this->disabled) {
267 21
            $attributes->put('disabled', 'disabled');
268 7
        }
269
270 75
        $attributes = $attributes->merge($this->attributes);
271
272 75
        return html()->attributes($attributes->toArray());
273
    }
274
275
    /**
276
     * Get the link class.
277
     *
278
     * @return string
279
     */
280 75
    protected function getLinkClass()
281
    {
282 75
        return implode(' ', array_filter(['btn', $this->getLinkSize(), $this->getLinkColor()]));
283
    }
284
285 75
    protected function getLinkColor()
286
    {
287 75
        if ($this->disabled)
288 39
            return 'btn-default';
289
290 57
        return Arr::get([
291 57
            'add'     => 'btn-primary',
292 19
            'delete'  => 'btn-danger',
293 19
            'detach'  => 'btn-danger',
294 19
            'disable' => 'btn-inverse',
295 19
            'edit'    => 'btn-warning',
296 19
            'enable'  => 'btn-success',
297 19
            'restore' => 'btn-primary',
298 19
            'show'    => 'btn-info',
299 19
            'update'  => 'btn-warning',
300 57
        ], $this->action);
301
    }
302
303 75
    protected function getLinkSize()
304
    {
305 75
        return Arr::get([
306 75
            'lg' => 'btn-lg',
307 25
            'md' => '',
308 25
            'sm' => 'btn-sm',
309 25
            'xs' => 'btn-xs',
310 75
        ], $this->size);
311
    }
312
313
    /* -----------------------------------------------------------------
314
     |  Helpers Methods
315
     | -----------------------------------------------------------------
316
     */
317
    /**
318
     * Generate activate icon link.
319
     *
320
     * @param  bool    $active
321
     * @param  string  $url
322
     * @param  array   $attributes
323
     * @param  bool    $disabled
324
     *
325
     * @return self
326
     */
327 18
    public static function activateIcon($active, $url, array $attributes = [], $disabled = false)
328
    {
329 18
        return self::make($active ? 'enable' : 'disable', $url, $attributes, $disabled)
330 18
                   ->size('xs')
331 18
                   ->withTitle(false)
332 18
                   ->icon(true);
333
    }
334
335
    /**
336
     * Generate activate icon link for modals (reverse button based on active state).
337
     *
338
     * @param  bool    $active
339
     * @param  string  $url
340
     * @param  array   $attributes
341
     * @param  bool    $disabled
342
     *
343
     * @return self
344
     */
345 3
    public static function activateModalIcon($active, $url, array $attributes = [], $disabled = false)
346
    {
347 3
        $dataAttribute = 'data-current-status';
348
        $statuses      = [
349 3
            'enabled'  => 'enabled',
350 1
            'disabled' => 'disabled',
351 1
        ];
352
353 3
        return static::activateIcon( ! $active, $url, $attributes, $disabled)
354 3
                     ->setAttribute($dataAttribute, $statuses[$active ? 'enabled' : 'disabled']);
355
    }
356
357
    /**
358
     * Generate activate link with icon for modals (reverse button based on active state).
359
     *
360
     * @param  bool    $active
361
     * @param  string  $url
362
     * @param  array   $attributes
363
     * @param  bool    $disabled
364
     *
365
     * @return self
366
     */
367 3
    public static function activateModalWithIcon($active, $url, array $attributes = [], $disabled = false)
368
    {
369 3
        $dataAttribute = 'data-current-status';
370
        $statuses      = [
371 3
            'enabled'  => 'enabled',
372 1
            'disabled' => 'disabled',
373 1
        ];
374
375 3
        return static::activateIcon( ! $active, $url, $attributes, $disabled)
376 3
                     ->setAttribute($dataAttribute, $statuses[$active ? 'enabled' : 'disabled'])
377 3
                     ->size('sm')
378 3
                     ->withTitle(true)
379 3
                     ->tooltip(false);
380
    }
381
382
    /**
383
     * Generate add icon link.
384
     *
385
     * @param  string  $url
386
     * @param  array   $attributes
387
     * @param  bool    $disabled
388
     *
389
     * @return self
390
     */
391 9
    public static function addIcon($url, array $attributes = [], $disabled = false)
392
    {
393 9
        return self::make('add', $url, $attributes, $disabled)
394 9
                   ->size('xs')
395 9
                   ->withTitle(false);
396
    }
397
398
    /**
399
     * Generate delete icon link for modals.
400
     *
401
     * @param  string  $url
402
     * @param  array   $attributes
403
     * @param  bool    $disabled
404
     *
405
     * @return self
406
     */
407 9
    public static function deleteModalIcon($url, array $attributes = [], $disabled = false)
408
    {
409 9
        return self::make('delete', $url, $attributes, $disabled)
410 9
                   ->size('xs')
411 9
                   ->withTitle(false);
412
    }
413
414
    /**
415
     * Generate delete link with icon for modals.
416
     *
417
     * @param  string  $url
418
     * @param  array   $attributes
419
     * @param  bool    $disabled
420
     *
421
     * @return self
422
     */
423 3
    public static function deleteModalWithIcon($url, array $attributes = [], $disabled = false)
424
    {
425 3
        return static::make('delete', $url, $attributes, $disabled)
426 3
                     ->size('sm')
427 3
                     ->withTitle(true)
428 3
                     ->tooltip(false);
429
    }
430
431
    /**
432
     * Generate detach icon link for modals.
433
     *
434
     * @param  string  $url
435
     * @param  array   $attributes
436
     * @param  bool    $disabled
437
     *
438
     * @return self
439
     */
440 9
    public static function detachModalIcon($url, array $attributes = [], $disabled = false)
441
    {
442 9
        return self::make('detach', $url, $attributes, $disabled)
443 9
                   ->size('xs')
444 9
                   ->withTitle(false);
445
    }
446
447
    /**
448
     * Generate edit icon link.
449
     *
450
     * @param  string  $url
451
     * @param  array   $attributes
452
     * @param  bool    $disabled
453
     *
454
     * @return self
455
     */
456 9
    public static function editIcon($url, array $attributes = [], $disabled = false)
457
    {
458 9
        return self::make('edit', $url, $attributes, $disabled)
459 9
                   ->size('xs')
460 9
                   ->withTitle(false);
461
    }
462
463
    /**
464
     * Generate edit link with icon.
465
     *
466
     * @param  string  $url
467
     * @param  array   $attributes
468
     * @param  bool    $disabled
469
     *
470
     * @return self
471
     */
472 3
    public static function editWithIcon($url, array $attributes = [], $disabled = false)
473
    {
474 3
        return self::make('edit', $url, $attributes, $disabled)
475 3
                   ->size('sm')
476 3
                   ->tooltip(false);
477
    }
478
479
    /**
480
     * Generate restore icon link for modals.
481
     *
482
     * @param  string  $url
483
     * @param  array   $attributes
484
     * @param  bool    $disabled
485
     *
486
     * @return self
487
     */
488 3
    public static function restoreModalIcon($url, array $attributes = [], $disabled = false)
489
    {
490 3
        return self::make('restore', $url, $attributes, $disabled)
491 3
                   ->size('xs')
492 3
                   ->withTitle(false);
493
    }
494
495
    /**
496
     * Generate restore link with icon for modals.
497
     *
498
     * @param  string  $url
499
     * @param  array   $attributes
500
     * @param  bool    $disabled
501
     *
502
     * @return self
503
     */
504 3
    public static function restoreModalWithIcon($url, array $attributes = [], $disabled = false)
505
    {
506 3
        return static::make('restore', $url, $attributes, $disabled)
507 3
                     ->size('sm')
508 3
                     ->withTitle(true)
509 3
                     ->tooltip(false);
510
    }
511
512
    /**
513
     * Generate show icon link.
514
     *
515
     * @param  string  $url
516
     * @param  array   $attributes
517
     * @param  bool    $disabled
518
     *
519
     * @return self
520
     */
521 9
    public static function showIcon($url, array $attributes = [], $disabled = false)
522
    {
523 9
        return self::make('show', $url, $attributes, $disabled)
524 9
                   ->size('xs')
525 9
                   ->withTitle(false);
526
    }
527
}
528