|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Common; |
|
4
|
|
|
|
|
5
|
|
|
trait CommonHelper |
|
6
|
|
|
{ |
|
7
|
|
|
protected function getPaginationLinks($page, $perPage, $numRows = null) :array |
|
8
|
|
|
{ |
|
9
|
|
|
$this->setPagination($page, $perPage, isset($numRows) ? $numRows : $this->model()->numRows()); |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
return $this->pagination()->getLinks(); |
|
|
|
|
|
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
protected function getPaginationViewData($page, $limit, $uri, &$links) :array |
|
15
|
|
|
{ |
|
16
|
|
|
return [ |
|
17
|
|
|
'limit' => $limit, |
|
18
|
|
|
'page' => $page['id'], |
|
19
|
|
|
'uri' => $uri, |
|
20
|
|
|
'links' => $links |
|
21
|
|
|
]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected function validationErrors(array $result, string $type = 'alert') :void |
|
25
|
|
|
{ |
|
26
|
|
|
if ($type == 'API') { |
|
27
|
|
|
$this->container()->jsonResponse($this->validation()->flash($result, [])); |
|
|
|
|
|
|
28
|
|
|
exit(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$data = ($type == 'value') ? $this->validation()->get($result, ['csrf_field']) |
|
32
|
|
|
: $this->validation()->flash($result, ['csrf_field']); |
|
33
|
|
|
|
|
34
|
|
|
foreach ($data as $key => $message) { |
|
35
|
|
|
$this->setSession($type, $message, $key); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function getIdFromSlug(string $slug) :string |
|
40
|
|
|
{ |
|
41
|
|
|
$slug = strip_tags($slug); |
|
42
|
|
|
|
|
43
|
|
|
return (strpos($slug, '_') !== false) ? strstr($slug, '_', true) : $slug; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function redirectToSelf(string $uri, $page = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if (isset($page)) { |
|
49
|
|
|
$page = $this->validation()->sanitize($page)->required()->run(); |
|
50
|
|
|
|
|
51
|
|
|
if ($this->validation()->access($page)) { |
|
52
|
|
|
return $this->redirect($uri . '/page/' . $page[0]); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirect($uri); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function searchArrayKey($array, $arrayKey) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($array as $key => $value) { |
|
62
|
|
|
if (stristr($key, $arrayKey) !== false) { |
|
63
|
|
|
return $key; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function dateFormat($value) |
|
69
|
|
|
{ |
|
70
|
|
|
$date = \DateTime::createFromFormat('d.m.Y H:i', $value); |
|
71
|
|
|
$value = $date->format('Y-m-d H:i'); |
|
72
|
|
|
|
|
73
|
|
|
return $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function checkbox($value) |
|
77
|
|
|
{ |
|
78
|
|
|
return ($value) ? '1' : '0'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function model() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->model; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setModel(string $modelName): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->model = $this->container()->new($modelName); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|