ossn.lib.views.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 
12 $VIEW = new stdClass;
13 $VIEW->register = array();
14 
15 /**
16  * Include a specific file
17  *
18  * @param string $file Valid file name;
19  * @param array $params Options;
20  * @last edit: $arsalanshah
21  * @return mixed data
22  */
23 function ossn_include($file = '', $params = array()) {
24  if (!empty($file) && is_file($file)) {
25  ob_start();
26  $params = $params;
27  include($file);
28  $contents = ob_get_clean();
29  return $contents;
30  }
31 
32 }
33 
34 /**
35  * View a file
36  *
37  * @param string $file valid file name of php file without extension;
38  * @param array $params Options;
39  * @last edit: $arsalanshah
40  * @return mixed data
41  */
42 function ossn_view($path = '', $params = array()) {
43  global $VIEW;
44  if (isset($path) && !empty($path)) {
45  //call hook in case to over ride the view
46  if (ossn_is_hook('halt', "view:{$path}")) {
47  return ossn_call_hook('halt', "view:{$path}", $params);
48  }
49  $path = ossn_route()->www . $path;
50  $file = ossn_include($path . '.php', $params);
51  return $file;
52  }
53 }
54 /**
55  * ossn_arg
56  *
57  * @param array $params Options;
58  */
59 function ossn_args(array $attrs) {
60  $attrs = $attrs;
61  $attributes = array();
62 
63  foreach ($attrs as $attr => $val) {
64  $attr = strtolower($attr);
65  if ($val === TRUE) {
66  $val = $attr;
67  }
68  if ($val !== NULL && $val !== false && (is_array($val) || !is_object($val))
69  ) {
70  if (is_array($val)) {
71  $val = implode(' ', $val);
72  }
73  $val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8', false);
74  $attributes[] = "$attr=\"$val\"";
75  }
76  }
77  return implode(' ', $attributes);
78 }
79 
80 /**
81  * Register a view;
82  *
83  * @param string $view Path of view;
84  * @param stringn $file File name for view;
85  * @last edit: $arsalanshah
86  *
87  * @reason: Initial;
88  * @returnn mix data
89  */
90 function ossn_extend_view($views, $file) {
91  global $VIEW;
92  $VIEW->register[$views][] = $file;
93  return true;
94 }
95 
96 /**
97  * Fetch a register view
98  *
99  * @param string $layout Name of view;
100  * @params string $params Args for file;
101  * @last edit: $arsalanshah
102  *
103  * @reason: Initial;
104  * @return mixed data
105  */
106 function ossn_fetch_extend_views($layout, $params = array()) {
107  global $VIEW;
108  if (isset($VIEW->register[$layout]) && !empty($VIEW->register[$layout])) {
109  foreach ($VIEW->register[$layout] as $file) {
110  if (!function_exists($file)) {
111  $fetch[] = ossn_plugin_view($file, $params);
112  } else {
113  $fetch[] = call_user_func($file, ossn_get_context(), $params, current_url());
114  }
115  }
116  return implode('', $fetch);
117  }
118 }
119 
120 /**
121  * Unregister a view from system
122  *
123  * @param string $layout Name of view;
124  *
125  * @last edit: $arsalanshah
126  * @reason: Initial;
127  * @return void
128  */
129 function ossn_remove_extend_view($layout) {
130  global $VIEW;
131  unset($VIEW->register[$layout]);
132 }
133 
134 /**
135  * Add a context to page
136  *
137  * @param string $context Name of context;
138  * @last edit: $arsalanshah
139  *
140  * @Reason: Initial;
141  * @return void;
142  */
143 function ossn_add_context($context) {
144  global $VIEW;
145  $VIEW->context = $context;
146  return true;
147 }
148 
149 /**
150  * Check the if are in registered context or not
151  *
152  * @param: string $context Name of context;
153  * @last edit: $arsalanshah
154  * @reason: Initial;
155  * @return bool;
156  */
157 function ossn_is_context($context) {
158  global $VIEW;
159  if (isset($VIEW->context) && $VIEW->context == $context) {
160  return true;
161  }
162  return false;
163 }
164 
165 /**
166  * Get a current context;
167  *
168  * @last edit: $arsalanshah
169  * @reason: Initial;
170  *
171  * @return false|string;
172  */
173 function ossn_get_context() {
174  global $VIEW;
175  if (isset($VIEW->context)) {
176  return $VIEW->context;
177  }
178  return false;
179 }
180 
181 /**
182  * Fetch a layout;
183  *
184  * @last edit: $arsalanshah
185  * @Reason: Initial;
186  *
187  * @param string $layout
188  */
189 function ossn_set_page_layout($layout, $params = array()) {
190  if (!empty($layout)) {
191  $theme = new OssnThemes;
192  $active_theme = $theme->getActive();
193  return ossn_plugin_view("theme/page/layout/{$layout}", $params);
194  }
195 }
196 
197 /**
198  * View page;
199  *
200  * @param string $title Title for page;
201  * @param string $content Content for page;
202  *
203  * @last edit: $arsalanshah
204  * @reason Initial;
205  * @return mixed data;
206  */
207 function ossn_view_page($title, $content, $page = 'page') {
208  $params['title'] = $title;
209  $params['contents'] = $content;
210  return ossn_plugin_view("theme/page/{$page}", $params);
211 }
212 
213 /**
214  * Ossn get default theme path
215  *
216  * @return string
217  */
218 function ossn_default_theme() {
219  $theme = new OssnThemes;
220  $active_theme = $theme->getActive();
221  return ossn_route()->themes . $theme->getActive() . '/';
222 }
223 
224 /**
225  * Ossn view form
226  *
227  * @param string $name
228  * @return mix data
229  */
230 function ossn_view_form($name, $args = array(), $type = 'core') {
231  $args['name'] = $name;
232  $args['type'] = $type;
233  return ossn_plugin_view("output/form", $args);
234 }
235 
236 /**
237  * Ossn view widget
238  *
239  * @param string $name
240  * @param string $title
241  * @return mix data
242  */
243 function ossn_view_widget($name, $title, $contents) {
244  $theme = new OssnThemes;
245  $active_theme = $theme->getActive();
246  $widget['title'] = $title;
247  $widget['contents'] = $contents;
248  return ossn_view("themes/{$active_theme}/widgets/{$name}", $widget);
249 }
250 /**
251  * View a template
252  *
253  * Use a templates from core (image view, url view etc)
254  *
255  * @param string $template A name of template
256  * @param array $params
257  *
258  * @return mix data
259  */
260 function ossn_view_template($template = '', array $params){
261  if(!empty($template)){
262  return ossn_plugin_view("{$template}", $params);
263  }
264 }
265 /**
266  * Create a pagiantion using count and page limit
267  *
268  * @param integer $count total entities/objects
269  * @param integer $page_limit Number of entities/objects per page
270  *
271  * @return false|mixed data
272  */
273 function ossn_view_pagination($count = false, $page_limit = 10){
274  $page_limit = ossn_call_hook('pagination', 'page_limit', false, $page_limit);
275  if(!empty($count) && !empty($page_limit)){
276  $pagination = new OssnPagination;
277 
278  $params = array();
279  $params['limit'] = $count;
280  $params['page_limit'] = $page_limit;
281 
282  $offset = input('offset');
283  if(empty($offset)){
284  ossn_set_input('offset', 1);
285  }
286  return $pagination->pagination($params);
287  }
288  return false;
289 }