ossn.lib.css.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 ossn_register_callback('ossn', 'init', 'ossn_css');
12 /**
13  * Initialize the css library
14  *
15  * @return void
16  */
17 function ossn_css() {
18  ossn_register_page('css', 'ossn_css_pagehandler');
19  ossn_add_hook('css', 'register', 'ossn_css_trigger');
20  ossn_extend_view('ossn/site/head', 'ossn_css_site');
21  ossn_extend_view('ossn/admin/head', 'ossn_css_admin');
22 }
23 
24 /**
25  * Add css page handler
26  *
27  * @return false|null
28  */
29 function ossn_css_pagehandler($css) {
30  if(ossn_site_settings('cache') == 1) {
31  return false;
32  }
33  header("Content-type: text/css");
34  $page = $css[0];
35  if(empty($css[1])) {
36  header('Content-Type: text/html; charset=utf-8');
38  }
39  if(empty($page)) {
40  $page = 'view';
41  }
42  switch($page) {
43  case 'view':
44  if(ossn_site_settings('cache') == 1) {
45  return false;
46  }
47  if(ossn_is_hook('css', "register")) {
48  echo ossn_call_hook('css', "register", $css);
49  }
50  break;
51  default:
52  header('Content-Type: text/html; charset=utf-8');
54  break;
55 
56  }
57 }
58 
59 /**
60  * Register a new css to system
61  *
62  * @param string $name The name of the css
63  * $file path to css file
64  *
65  * @return void
66  */
67 function ossn_new_css($name, $file) {
68  global $Ossn;
69  $Ossn->css[$name] = $file;
70 }
71 
72 /**
73  * Remove a css from system
74  *
75  * This will not remove css file it will just unregister it
76  * @param string $name The name of the css
77  *
78  * @return void
79  */
80 function ossn_unlink_new_css($name, $file) {
81  global $Ossn;
82  if(isset($Ossn->css[$name])) {
83  unset($Ossn->css[$name]);
84  }
85 }
86 
87 /**
88  * Get a tag for inserting css
89  *
90  * @params array $args array()
91  *
92  * @return string
93  */
94 function ossn_html_css($args) {
95  if(!is_array($args)) {
96  return false;
97  }
98  $default = array(
99  'rel' => 'stylesheet',
100  'type' => 'text/css'
101  );
102  $args = array_merge($default, $args);
103  return "\r\n<link " . ossn_args($args) . " />";
104 }
105 
106 /**
107  * Load css to system
108  *
109  * @params string $name = name of css
110  * $type site or admin
111  *
112  * @return void
113  */
114 function ossn_load_css($name, $type = 'site') {
115  global $Ossn;
116  $Ossn->csshead[$type][] = $name;
117 }
118 /**
119  * Ossn system unloads css from head
120  *
121  * @param string $name The name of the css
122  *
123  * @return void
124  */
125 function ossn_unload_css($name, $type = 'site') {
126  global $Ossn;
127  $css = array_search($name, $Ossn->csshead[$type]);
128  if($css !== false) {
129  unset($Ossn->csshead[$type][$css]);
130  }
131 }
132 /**
133  * Load registered css to system for site
134  *
135  * @return html.tag
136  */
137 function ossn_css_site() {
138  global $Ossn;
139  $url = ossn_site_url();
140  //load external css
141  $external = $Ossn->cssheadExternal['site'];
142  if(!empty($external)) {
143  foreach($external as $item) {
144  echo ossn_html_css(array(
145  'href' => $Ossn->cssExternal[$item]
146  ));
147  }
148  }
149 
150  //load internal css
151  if(isset($Ossn->csshead['site'])) {
152  foreach($Ossn->csshead['site'] as $css) {
153  $href = "{$url}css/view/{$css}.css";
154  if(ossn_site_settings('cache') == 1) {
155  $cache = ossn_site_settings('last_cache');
156  $href = "{$url}cache/css/{$cache}/view/{$css}.css";
157  }
158  echo ossn_html_css(array(
159  'href' => $href
160  ));
161  }
162  }
163 
164 }
165 
166 /**
167  * Load registered css to system for admin
168  *
169  * @return html.tag
170  */
171 function ossn_css_admin() {
172  global $Ossn;
173  $url = ossn_site_url();
174  //load external css
175  $external = $Ossn->cssheadExternal['admin'];
176  if(!empty($external)) {
177  foreach($external as $item) {
178  echo ossn_html_css(array(
179  'href' => $Ossn->cssExternal[$item]
180  ));
181  }
182  }
183 
184  //load internal css
185  if(isset($Ossn->csshead['admin'])) {
186  foreach($Ossn->csshead['admin'] as $css) {
187  $href = "{$url}css/view/{$css}.css";
188  if(ossn_site_settings('cache') == 1) {
189  $cache = ossn_site_settings('last_cache');
190  $href = "{$url}cache/css/{$cache}/view/{$css}.css";
191  }
192  echo ossn_html_css(array(
193  'href' => $href
194  ));
195  }
196  }
197 }
198 
199 /**
200  * Check if the requested css is registered then load css
201  *
202  * @return string|false
203  */
204 function ossn_css_trigger($hook, $type, $value, $params) {
205  global $Ossn;
206  if(isset($params[1]) && substr($params[1], '-4') == '.css') {
207  $params[1] = str_replace('.css', '', $params[1]);
208  if(isset($Ossn->css[$params[1]])) {
209  $file = ossn_plugin_view($Ossn->css[$params[1]]);
210  $extended = ossn_fetch_extend_views("css/{$params[1]}");
211  $data = array(
212  $file,
213  $extended
214  );
215  return implode(' ', $data);
216  }
217  }
218  return false;
219 }
220 /**
221  * Register a new external css to system
222  *
223  * @param string $name The name of the css
224  * $file complete url path to css file
225  *
226  * @return void
227  */
228 function ossn_new_external_css($name, $file, $type = true) {
229  global $Ossn;
230  if($type) {
231  $Ossn->cssExternal[$name] = ossn_site_url($file);
232  } else {
233  $Ossn->cssExternal[$name] = $file;
234  }
235 }
236 /**
237  * Remove a external css from system
238  *
239  * @param string $name The name of the css
240  * $file complete url path to css file
241  *
242  * @return void
243  */
244 function ossn_unlink_external_css($name) {
245  global $Ossn;
246  unset($Ossn->cssExternal[$name]);
247 }
248 /**
249  * Load registered css to system for site
250  *
251  * @return html.tag
252  */
253 function ossn_load_external_css($name, $type = 'site') {
254  global $Ossn;
255  $Ossn->cssheadExternal[$type][] = $name;
256 }
257 /**
258  * Ossn system unloads css from head
259  *
260  * @param string $name The name of the css
261  *
262  * @return void
263  */
264 function ossn_unload_external_css($name, $type = 'site') {
265  global $Ossn;
266  $css = array_search($name, $Ossn->cssheadExternal[$type]);
267  if($css !== false) {
268  unset($Ossn->cssheadExternal[$type][$css]);
269  }
270 }