aboutsummaryrefslogtreecommitdiff
path: root/mod/less/start.php
blob: 476ad5104218fb38e3d266ac7c691c68238a58fc (plain)
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
<?php
/**
 * Elgg LESS compiler
 *
 * @package ElggLESS
 */

elgg_register_event_handler('init', 'system', 'less_init');

function less_init() {
	
	less_override_css();
	
	//make sure this runs after everyone else is done
	elgg_register_plugin_hook_handler('view', 'all', 'less_views', 999);
}

function less_views($hook, $type, $content, $params) {
	$view = $params['view'];
	$vars = $params['vars'];
	
	if (preg_match("/^css\//", $view) && !$vars['less_flag']) {
		global $CONFIG;
		if ($CONFIG->less->view[$view]) {
			$vars['less_flag'] = true;
			$content = elgg_view($CONFIG->less->view[$view], $vars);
		}
	}
	
	if (preg_match("/^less\//", $view) && $vars['less_flag']) {
		if (include_once(elgg_get_plugins_path() . 'less/vendors/lessphp/lessc.inc.php')) {
			$parser = new lessc();
			return $parser->parse($content);
		}
	}
	return $content;
}

function less_override_css() {
	$plugins = elgg_get_plugins('active');
	if ($plugins) {
		foreach ($plugins as $plugin) {
			$view_dir = elgg_get_plugins_path() . $plugin->getID() . "/views/";

			if (!is_dir($view_dir) || FALSE === ($handle = opendir($view_dir))) {
				continue;
			}

			while (FALSE !== ($view_type = readdir($handle))) {
				$view_type_dir = $view_dir . $view_type;

				if ('.' !== substr($view_type, 0, 1) && is_dir($view_type_dir)) {
					less_autoregister_views('', $view_type_dir, $view_dir, $view_type);
				}
			}
		}
	}
}

function less_autoregister_views($view_base, $folder, $base_location_path, $viewtype) {
	if (!empty($view_base)) {
		$view_base_new = $view_base . "/";
	} else {
		$view_base_new = "";
	}
	
	if ($handle = opendir($folder)) {
		while ($view = readdir($handle)) {
			if (!in_array($view, array('.', '..', '.svn', 'CVS')) && !is_dir($folder . "/" . $view)) {
				if (substr_count($view, ".php") > 0) {
					less_update_view($view_base_new . str_replace('.php', '', $view));
				}
			} else if (!in_array($view, array('.', '..', '.svn', 'CVS')) && is_dir($folder . "/" . $view)) {
				if ($view == 'css' || $view == 'less') {
					less_autoregister_views($view_base_new . $view, $folder . "/" . $view,
						$base_location_path, $viewtype);
				}
			}
		}
		return TRUE;
	}
	return FALSE;
}

function less_update_view($view) {
	global $CONFIG;
	
	if (substr($view, 0, 5) == "less/") {
		$css_view = substr($view, 5);
	} else {
		$css_view = $view;
	}

	if (!isset($CONFIG->less)) {
		$CONFIG->less = new stdClass;
	}

	if (!isset($CONFIG->less->view)) {
		$CONFIG->less->view = array($css_view => $view);

	} else {
		$CONFIG->less->view[$css_view] = $view;
	}
}