Sohy /_sohy/classes/theme.class.php

Language PHP Lines 493
MD5 Hash 18b00bf5377317495f1c0d7c102baddd
Repository https://github.com/ampersarnie/Sohy.git View Raw File
  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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php

/*
	======================
	== Sohy Theme Class ==
	======================
*/

if(!defined('INDEX')) die();

use Yaml\Yaml;

class theme {

	public function __construct() {
		global $yaml, $sohy;

		$this->_config();
		$this->get_theme();

	}

	public function _config() {
		global $sohy, $yaml;

		if(file_exists($sohy->base->dir.'config.yml')) {

			/*
				Get the theme config file.
			*/
			$config_file	= $sohy->base->dir.'themes/'.$sohy->site_theme.'/config.yml';
			$config_file	= file_get_contents($config_file);
			$this->config	= json_decode(json_encode(Yaml::parse($config_file)));

			$this->dir		= new stdClass();

			$this->name		= $this->config->name;
			$this->author	= $this->config->author;

			if(isset($this->config->customdata)) {
				$this->customdata			= $this->config->customdata;
			}

		}

	}

	/*
		get_theme()
		============
		Insert any necessary files depending on
		the viewed page.

		Arguments: none.
	*/
	private function get_theme() {
		global $sohy, $posts;

		/*
			Don't get pages if RSS or 'admin' class
			is defined.
		*/
		if(!class_exists('admin')) {

			if(is_null($sohy->check_file())) {

				header("HTTP/1.0 404 Not Found");

				require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/404.php';

			} elseif($sohy->is_post()) {

				if(isset($posts->post()->template)) {
					$template	= $posts->post()->template;
				} else {
					$template	= 'post.php';
				}

				require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$template;
			} elseif($sohy->is_cat()) {

				$default	= $sohy->base->dir.'themes/'.$sohy->site_theme.'/category.php';
				$template	= '';

                if(isset($posts->category()->template)) {
					$template	= $posts->category()->template;
					$template	= $sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$template;
				}

				if(file_exists($template)) {
					require_once $template;
				} else {
					require_once $default;
				}

			} else {
				require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/index.php';
			}

			$this->dir->js			= $sohy->url->home.'/themes/'.$sohy->site_theme.'/js/';
			$this->dir->css			= $sohy->url->home.'/themes/'.$sohy->site_theme.'/styles/';
			$this->dir->image		= $sohy->url->home.'/themes/'.$sohy->site_theme.'/img/';

		}

	}

	/*
		list()
		======
		Returns an array of installed themes
		and their config data.

		Arguments: none
	*/
	public function themes() {
		global $sohy, $yaml;

		/*
			Get directory list of all themes
		*/
		$themes	= glob($sohy->base->dir.'themes/*', GLOB_ONLYDIR);

		/*
			Process directories and pull out
			config information.
		*/
		foreach($themes as $key => $theme) {
			// load the file
			$file  = $yaml->load($theme.'/config.yml');

			$themes[$key]	= array(
									'name'		=> $file['name'],
									'css'		=> $file['css'],
									'author'	=> $file['author']
									);
		}

		return arrToObj($themes);

	}

	/*
		templates()
		===========
		Returns a list of templates.
		Category template files must be prefixed
		with "category." ex "/category.design.php"

		Arguments:
		$type	- prefix of file, ie; category
	*/
	public function templates($type) {
		global $sohy;

		/*
			Get list of files.
		*/
		$templates	= glob($sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$type.'.*.php');

		return arrToObj($templates);
	}

	/*
		Redundant?

		get_base()
		============
		Insert any necessary files depending on
		the viewed page.

		Arguments:
		$base	- The amount of posts to be returned.
	*/
	private function get_base($base) {
		global $sohy;

		if(!isset($base)) {
			$base = $sohy->home_url;
			$base = array_filter(explode('/',$base));
			$base = end($base);
		}

		return $base;
	}

	/*
		get_title()
		============
		Returns the title of a specific file.

		Arguments:
		$file	- The target file.
	*/
	private function get_title($file) {
		global $yaml;

		$file  = $yaml->load($file);

		if(isset($file->title)) {
			return $file->title;
		}

		return false;

	}

	/*
		title()
		============
		Returns the current page title for html
		<title> tag with the option of adding
		a delimete between site name and post title

		Example Output:
		Sohy / Some Post Title

		Arguments:
		$delimeter	- Addition between site title and
					  post title.
	*/
	public function title($delimeter = ' / ') {
		global $sohy, $posts;

		if($sohy->is_post()) {
			return $posts->post()->title.$delimeter.$sohy->site_name;
		} else {
			return $sohy->site_name;
		}

	}

	/*
		get_header()
		============
		Get the theme header.php, or that
		of a specified theme.

		Arguments:
		$theme	- Specify a specific theme to use
				  rather than the default.
		$vars	- Variables to be passed to the
				  requested header file.
	*/
	public function get_header($theme = NULL, $vars = array()) {
		global $sohy, $posts;

		/*
			Requires further tidying.
		*/

		if($sohy->is_post()) {

			if(isset($posts->post()->settings->header)) {
				$header	= $posts->post()->settings->header;
			} else {
				$header	= NULL;
			}

			if(isset($posts->post()->settings->theme)) {
				$theme	= $posts->post()->settings->theme;
			} else {
				$theme	= NULL;
			}

			$file	= 'themes/'.$theme.'/'.$header;

			if(isset($header) && isset($theme) && file_exists($file)) {
				require_once $sohy->base->dir.'themes/'.$theme.'/'.$header;
			} elseif(isset($header) && !isset($theme) && file_exists($sohy->base->dir.$file)) {
				require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$header;
			}

		}

		/*
			Create variables that can be passed to theme header.php
		*/

		if(!is_null($vars) || $vars != false) {

			foreach($vars as $key => $val) {

				if(is_numeric($key)) {
					$header[$key]	= $val;
				} else {
					${$key}	= $val;
				}

			}

		}

		// directories
		$req_theme		= $sohy->base->dir.'themes/'.$theme;
		$default_theme	= $sohy->base->dir.'themes/'.$sohy->site_theme;

		if(is_dir('themes/'.$theme) && file_exists($req_theme.'/header.php')) {
			require_once $req_theme.'/header.php';
		} elseif(is_file($default_theme.'/'.$theme)) {
			require_once $default_theme.'/'.$theme;
		} else {
			require_once $default_theme.'/header.php';
		}

	}

	/*
		get_footer()
		============
		Get the theme footer.php, or that
		of a specified theme.

		Arguments:
		$theme	- Specify a specific theme to use
				  rather than the default.
		$vars	- Variables to be passed to the
				  requested footer file.
	*/

	public function get_footer($theme = NULL, $vars = array()) {
		global $sohy;

		/*
			Create variables that can be passed to theme header.php
		*/

		if(!is_null($vars) || $vars != false) {

			foreach($vars as $key => $val) {

				if(is_numeric($key)) {
					$header[$key]	= $val;
				} else {
					${$key}	= $val;
				}

			}

		}

		if(is_dir($sohy->base->dir.'themes/'.$theme) && file_exists('themes/'.$theme.'/footer.php')) {
			require_once $sohy->base->dir.'themes/'.$theme.'/footer.php';
		} elseif(is_file($sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$theme)) {
			require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/'.$theme;
		} else {
			require_once $sohy->base->dir.'themes/'.$sohy->site_theme.'/footer.php';
		}

	}

	/*
		css()
		============
		Return the directory-file of the theme
		css file.

		Arguments:
		$style	- Use a speficied css file from
				  the theme config.
	*/

	public function css($style = 'default') {
		global $sohy;

		if($sohy->is_post()) {

			if(isset($sohy->the_post->settings->css)) {
				return $this->dir->css.$sohy->the_post->settings->css->{$style};
			}

		}

		return $this->dir->css.$this->config->css->{$style};

	}

	/*
		snippet()
		=========
		Allows you to use variable snippets across files.

		Arguments:
		$snippet	- The file to include from snippet dir
		$vars		- Array of variables to use. The array
					  keys become the variable names. If
					  the key is numeric (i.e. no key name),
					  the variable will be an array with keys
					  left intact.
		$theme		- If true, snippet will be included from
					  the current theme folder.
	*/
	public function snippet($snippet, $vars = array(), $theme = false) {
		global $sohy;

		if(!is_null($vars) || $vars != false) {

			foreach($vars as $key => $val) {

				if(is_numeric($key)) {
					${$snippet}[$key]	= $val;
				} else {
					${$key}	= $val;
				}

			}

		}

		if($theme) {
			$path	= $sohy->base->dir.'themes/'.$sohy->site_theme.'/snippets/';
		} else {
			$path	= $sohy->base->dir.'assets/snippets/';
		}

		$file	= $path.$snippet.'.php';
		$dir	= $path.$snippet.'/'.$snippet.'.php';

		if(is_file($file)) {
			require $file;
		} elseif(is_file($dir)) {
			require $dir;
		}

		if(function_exists($snippet)) {
			return $snippet($vars);
		}

	}

	/*
		pagination()
		============
	*/
	public function pagination($as = 'array', $text = NULL, $posts = NULL) {
		global $sohy, $posts;

		$output	= array();

		if(is_null($text)) {
			$text	= array('next' => 'Next', 'previous' => 'Previous');
		}

		if(is_array($posts)) {
			$page_count	= count($posts);
		} else {
			$page_count	= $sohy->page_count;
		}

		if(!isset($sohy->page)) {
			$next	= 0;
			$prev	= 2;
		} elseif($sohy->page > 1) {
			$next	= $sohy->page-1;
			$prev	= $sohy->page+1;
		}

		if($next > 1) {
			$output['next']	= $sohy->url->current.'/page/'.$next;
		} elseif($next == 1) {
			$output['next']	= $sohy->url->current;
		}

		if($prev <= $page_count) {
			$output['previous']	= $sohy->url->current.'/page/'.$prev;
		}

		if($as == 'html') {
			$html	= '<ul class="pagination">'."\n";

			if($next > 1) {
				$html	.= "\t".'<li class="next"><a href="'.$output['next'].'">'.$text['next'].'</a></li>'."\n";
			} elseif($next == 1) {
				$html	.= "\t".'<li class="next"><a href="'.$sohy->url->current.'">'.$text['next'].'</a></li>'."\n";
			}

			if($prev <= $sohy->page_count) {
				$html	.= "\t".'<li class="previous"><a href="'.$output['previous'].'">'.$text['previous'].'</a></li>'."\n";
			}

			$html	.= '</ul>'."\n";

			$output	= $html;
		}

		return $output;

	}

	// End of class.
}

?>
Back to Top