Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / functions / helpers.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /**
4 Theme helpers functions
5 */
6
7
8 // Url helpers
9 function base_url($url = '') {
10 return Url::make($url);
11 }
12
13 function theme_url($file = '') {
14 return Config::get('application.base_url') . 'themes/' . Config::get('metadata.theme') . '/' . ltrim($file, '/');
15 }
16
17 function current_url() {
18 return Url::make(Request::uri());
19 }
20
21 function admin_url($url = '') {
22 return Url::make(Config::get('application.admin_folder') . '/' . ltrim($url, '/'));
23 }
24
25 function search_url() {
26 return Url::make('search');
27 }
28
29 function rss_url() {
30 return Url::make('rss');
31 }
32
33 // Custom function helpers
34 function bind($page, $fn) {
35 Events::bind($page, $fn);
36 }
37
38 function recieve($name = '') {
39 return Events::call($name);
40 }
41
42 // page type helpers
43 function is_homepage() {
44 if($itm = IoC::resolve('page')) {
45 return $itm->id == Config::get('metadata.home_page');
46 }
47
48 return false;
49 }
50
51 function is_postspage() {
52 if($itm = IoC::resolve('page')) {
53 return $itm->id == Config::get('metadata.posts_page');
54 }
55
56 return false;
57 }
58
59 function is_debug() {
60 return Config::get('debug', false);
61 }
62
63 // benchmarking
64 function execution_time() {
65 $miliseconds = microtime(true) - START;
66 return round($miliseconds, 4);
67 }
68
69 // return in mb
70 function memory_usage() {
71 return memory_get_peak_usage(true) / 1024;
72 }
73
74 // database profile information
75 function db_profile() {
76 // total query time
77 $total = 0;
78
79 $html = '<style>';
80 $html .= '.debug {display: none;font-size: 13px; margin-bottom: 1em;}';
81 $html .= '.debug td, .debug th {padding: 4px 6px; border-bottom: 1px solid #ddd;}';
82 $html .= '.debug th {font-weight: bold; text-align: center;}';
83 $html .= '.debug tfoot td:first-child {text-align: right;}';
84 $html .= '</style>';
85
86 $html .= '<table id="debug_table" class="debug">';
87 $html .= '<thead><tr><th>SQL</th><th>Bindings</th><th>Rows</th><th>Time</th></th></thead>';
88
89 $html .= '<tbody>';
90
91 foreach(Db::profile() as $row) {
92 $html .= '<tr><td>' . $row['sql'] . '</td><td>' . implode(', ', $row['binds']) . '</td><td>' . $row['rows'] . '</td><td>' . $row['time'] . '</td></tr>';
93 $total += $row['time'];
94 }
95
96 $html .= '</tbody>';
97
98 $html .= '<tfoot>';
99 $html .= '<tr><td colspan="3"><strong>Query Time</strong></td><td>' . round($total, 4) . '</td></tr>';
100 $html .= '<tr><td colspan="3"><strong>Execution Time</strong></td><td>' . execution_time() . '</td></tr>';
101 $html .= '<tr><td colspan="3"><strong>Memory Usage</strong></td><td>' . memory_usage() . 'Kb</td></tr>';
102 $html .= '</tfoot>';
103
104 $html .= '</table>';
105
106 return $html;
107 }