Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / admin / theme / functions.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /**
4 Main menu
5 */
6 function admin_menu() {
7
8 $prefix = Config::get('application.admin_folder');
9
10 $pages = array(
11 'Posts' => $prefix . '/posts',
12 'Pages' => $prefix . '/pages',
13 'Users' => $prefix . '/users',
14 'Metadata' => $prefix . '/metadata'
15 );
16
17 return $pages;
18 }
19
20 /**
21 Custom fields
22 */
23 function parse_fields($str) {
24 $data = json_decode($str, true);
25 return is_array($data) ? $data : array();
26 }
27
28 /**
29 Url helpers
30 */
31 function theme_url($file = '') {
32 return Config::get('application.base_url') . 'system/admin/theme/' . ltrim($file, '/');
33 }
34
35 function admin_url($url = '') {
36 return Url::make(Config::get('application.admin_folder') . '/' . ltrim($url, '/'));
37 }
38
39 /**
40 String helpers
41 */
42 function pluralise($amount, $str, $alt = '') {
43 return $amount === 1 ? $str : $str . ($alt !== '' ? $alt : 's');
44 }
45
46 function truncate($str, $limit = 10, $elipse = ' [...]') {
47 $words = preg_split('/\s+/', $str);
48
49 if(count($words) <= $limit) {
50 return $str;
51 }
52
53 return implode(' ', array_slice($words, 0, $limit)) . $elipse;
54 }
55
56 /**
57 Error checking
58 */
59 function latest_version() {
60 // check we have curl support
61 if(Curl::support() === false) {
62 return 0;
63 }
64
65 // only run the version check once per session
66 if(($version = Session::get('latest_version')) === false) {
67 // returns plain text string with version number or 0 on failure.
68 $version = "0.6";
69 Session::set('latest_version', $version);
70 }
71
72 return $version;
73 }
74
75 function error_check() {
76 $errors = array();
77
78 // Check for older versions
79 if(version_compare(VERSION, ($version = latest_version()), '<')) {
80 $errors[] = 'Your version of Test CMS is out of date. Please <a href="http://google.com">download the latest version</a>.';
81 }
82
83 // do something useful with it
84 return count($errors) ? $errors : false;
85 }
86
87 /**
88 Benchmarking
89 */
90 function execution_time() {
91 $miliseconds = microtime(true) - START;
92 return round($miliseconds, 4);
93 }
94
95 // return in mb
96 function memory_usage() {
97 return memory_get_peak_usage(true) / 1024;
98 }
99
100 // database profile information
101 function db_profile() {
102 // total query time
103 $total = 0;
104
105 $html = '';
106 $html .= '<table id="debug_table" class="debug">';
107 $html .= '<thead><tr><th>SQL</th><th>Bindings</th><th>Rows</th><th>Time</th></th></thead>';
108
109 $html .= '<tbody>';
110
111 foreach(Db::profile() as $row) {
112 $html .= '<tr><td>' . $row['sql'] . '</td><td>' . implode(', ', $row['binds']) . '</td><td>' . $row['rows'] . '</td><td>' . $row['time'] . '</td></tr>';
113 $total += $row['time'];
114 }
115
116 $html .= '</tbody>';
117
118 $html .= '<tfoot>';
119 $html .= '<tr><td colspan="3"><strong>Query Time</strong></td><td>' . round($total, 4) . '</td></tr>';
120 $html .= '<tr><td colspan="3"><strong>Execution Time</strong></td><td>' . execution_time() . '</td></tr>';
121 $html .= '<tr><td colspan="3"><strong>Memory Usage</strong></td><td>' . memory_usage() . 'Kb</td></tr>';
122 $html .= '</tfoot>';
123
124 $html .= '</table>';
125
126 return $html;
127 }