-
Notifications
You must be signed in to change notification settings - Fork 1
/
Uview.php
199 lines (160 loc) · 6.18 KB
/
Uview.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* Uview class
* @author arhen <[email protected]>
* @version 1.0.0
* @copyright 2019 Arhen
* @license MIT
*/
class Uview
{
private $_default_index = 'lihat';
private $_parser = false;
private $_view_name = null;
private $_breadcumb = null;
private $_js_path = null;
private $_css_path = null;
private $_breadcumb_default_class = "";
private $_breadcumb_separator = '-'; // bisa diinput html
private $_layout_path = '_layout'; //default is _layout
// set Instansce Null Object CI
public $CI = null;
public function __construct()
{
$this->CI = &get_instance();
}
/**
* ajax_response
*
* @param array $data
* @param bool $flatten
*
* @return json
*/
public function json_response($data, $flatten = false)
{
if ($flatten) {
$data = $this->_flatten_json($data);
}
return $this->CI->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit();
}
/**
* Function For Generate View Codeiginiter by arhen
*
* @param string $view_name this is for view name folder
* @param array $data_view (Data for store to view)
* @param string $path_layout (path layout if there is existing)
* @param string $breadcumb (custom breadcumb)
* @return void
*/
public function builder(array $data_view, $title, $path_layout = '', $is_parser = FALSE)
{
$trace = debug_backtrace();
$caller_function = strtolower($trace[1]['function']);
if ($caller_function == 'index') {
$caller_function = $this->_default_index;
}
//set_view name file path
if ($this->_view_name)
$caller_function = $this->_view_name;
$caller_class = strtolower($trace[1]['class']);
if ($path_layout != '') {
$path_layout = strtolower($path_layout) . '/';
}
$data = array(); //Set variabel for view
if (!empty($data_view)) {
foreach ($data_view as $key => $value) {
$data[$key] = $value;
}
}
$data['title'] = ucwords($title);
//set_breadcumbs
if ($this->_breadcumb){
if (is_array($this->_breadcumb)) {
foreach ($this->_breadcumb as $key => $value) {
$this->_breadcumb[$key] = anchor( strtolower($path_layout . $value), ucwords($key), array('class' => $this->_breadcumb_default_class));
}
$data['breadcumb'] = $trace[1]['class'] . " {$this->_breadcumb_separator} " . implode(" {$this->_breadcumb_separator} " ,$this->_breadcumb);
} else {
// $data['breadcumb'] = ucwords($this->_breadcumb);
$data['breadcumb'] = $trace[1]['class'] . " {$this->_breadcumb_separator} " . anchor(current_url().'/#', ucwords($this->_breadcumb), array('class' => $this->_breadcumb_default_class));
}
} else {
$args = '';
if(! empty($trace[1]['args'])) {
foreach($trace[1]['args'] as $key => $value){
$trace[1]['args'][$key] = anchor(current_url().'/#', $value, array('class' => $this->_breadcumb_default_class));
}
$args = " {$this->_breadcumb_separator} " . implode(" {$this->_breadcumb_separator} " ,$trace[1]['args']);
}
$data['breadcumb'] = anchor(strtolower($path_layout . $trace[1]['class']), ucwords($title), array('class' => $this->_breadcumb_default_class)) . " {$this->_breadcumb_separator} " . anchor( strtolower($path_layout . $trace[1]['class'] . "/{$caller_function}"), ucwords($caller_function), array('class' => $this->_breadcumb_default_class)) . $args;
}
//set_js
if($this->_js_path)
$js_path = $path_layout . $caller_class . '/' . 'js' . '/' . $this->_js_path;
else
$js_path = $path_layout . $caller_class . '/' . 'js' . '/' . $caller_function;
//set_css
if ($this->_css_path)
$css_path = $path_layout . $caller_class . '/' . 'css' . '/' . $this->_css_path;
else
$css_path = $path_layout . $caller_class . '/' . 'css' . '/' . $caller_function;
$data['isi'] = $path_layout . $caller_class . '/' . $caller_function;
//optional
$data['js'] = $this->_is_file_exist($js_path) ? $js_path : '';
$data['css'] = $this->_is_file_exist($css_path) ? $css_path : '';
$this->_generate($path_layout, $data, $is_parser);
}
public function set_filename($view_name){
$this->_view_name = $view_name;
return $this;
}
public function set_breadcumb($breadcumb){
$this->_breadcumb = $breadcumb;
return $this;
}
public function set_js_path($js_path){
$this->_js_path = $js_path;
return $this;
}
public function set_css_path($css_path){
$this->_css_path = $css_path;
return $this;
}
public function set_layout($layout_path){
$this->_layout_path = $layout_path;
return $this;
}
private function _generate($path_layout, $data, $is_parser)
{
$wrapper_path = $path_layout . $this->_layout_path .'/wrapper';
if($is_parser === FALSE)
$this->CI->load->view($wrapper_path, $data);
else{
$this->CI->load->library('parser');
$this->CI->parser->parse($wrapper_path, $data);
}
}
private function _is_file_exist($file_path)
{
$target_file = APPPATH . 'views/' . $file_path . '.php';
if (! file_exists($target_file)) {
return false;
}
return true;
}
public function _flatten_json(array $array)
{
$output = array();
foreach ($array as $v) {
$output[$v['id']] = $v['nama'];
}
return $output;
}
}