Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / helpers / application_helper.php
blobbcc752420a66a6c41d90cf5ccabeb6778a520ff5
1 <?php
2 class application_helper { // extends Controller {
3 // all functions must be public and static, and must take the following
4 // parameters: ($params, &$smarty)
5 public $treat_as_filter = array('ago', 'human_readable', 'inspect', 'size_by_popularity');
7 public static function resource($params, &$smarty) {
8 $request = $params['request'];
9 $resource = $params['resource'];
10 return sprintf('%s://%s/%s/%s', $request->protocol, $request->host, $request->directory, $resource);
13 // link_to
14 public static function link_to($params, &$smarty) {
15 // get location
16 $location = $smarty->_tpl_vars['request']->location;
18 // ancient stuff, for review (pull out the sanitize stuff)
19 // $controller = !empty($params["controller"]) ? $params["controller"] : $smarty->_tpl_vars['request']->route['controller'];
20 // $action = !empty($params["action"]) ? $params["action"] : $smarty->_tpl_vars['request']->route['action'];
21 // $id = !empty($params["id"]) ? self::sanitize_id($params["id"]) : "";
22 // $mul = !empty($params["mul"]) ? $params["mul"] : "";
23 // $mul_id = !empty($params["mulid"]) ? self::sanitize_id($params["mulid"]) : "";
24 // if(!empty($mul)) $id = $id . $mul;
25 // if(!empty($mul_id)) $id = $id . $mul_id;
27 // assemble link
28 $rel = !empty($params['rel']) ? " rel='{$params['rel']}'" : "";
29 $html_options = $params["extra"]; // change from 'extra' to 'html_options' (will affect template code)
30 $link_item = $params["title"];
32 $onclick = !empty($params["confirm"]) ? sprintf(' onClick="if(confirm(\'%s\')) return true; else return false;"', $params["confirm"]) : "";
33 $onclick = !empty($params["onclick"]) ? sprintf(' onClick="%s"', $params["onclick"]) : $onclick;
35 if(empty($link_item) && !empty($params['image'])) $link_item = self::img($params, $smarty);
36 // if(empty($link_item)) $link_item = $url;
38 // remove non-route params
39 unset($params['rel']);
40 unset($params['extra']);
41 unset($params['title']);
42 unset($params['confirm']);
43 unset($params['onclick']);
45 // assemble route
46 $route = Router2::url_for($params);
48 // conjoin location and route
49 $url = empty($params['url']) ? "{$location}{$route}" : $params['url'];
51 $link = '<a href="%s" %s%s%s>%s</a>';
53 return sprintf($link, $url, $html_options, $rel, $onclick, $link_item);
56 // creates an image
57 public static function img($params, &$smarty) {
58 $location = $smarty->_tpl_vars['request']->location;
59 $img_dir = empty($params['img_dir']) ? "res/" : $params['img_dir'];
60 $image = $params['image'];
61 $border = empty($params['img_border']) ? "0" : $params['img_border'];
62 $alt_text = empty($params['img_alt']) ? "" : $params['img_alt'];
63 $extra = $params['img_extra'];
65 // string template
66 $img = '<img src="%s%s%s" border="%s" alt="%s" %s />';
68 // return image tag
69 return sprintf($img, $location, $img_dir, $image, $border, $alt_text, $extra);
72 // create_link
73 public static function create_link($params, &$smarty) {
74 return self::url_for($params, $smarty);
76 public static function url_for($params, &$smarty) {
77 // get location
78 $location = $smarty->_tpl_vars['request']->location;
80 // assemble route
81 $route = Router2::url_for($params);
83 // old school (glean the default values from it)
84 // $controller = !empty($params["controller"]) ? $params["controller"] : $smarty->_tpl_vars['request']->route['controller'];
85 // $action = !empty($params["action"]) ? $params["action"] : $smarty->_tpl_vars['request']->route['action'];
86 // $id = !empty($params["id"]) ? self::sanitize_id($params["id"]) : "";
88 $link = '%s%s';
90 return sprintf($link, $location, $route);
93 // sanitizes IDs (for IDs like Tags or Categories that can be separated by spaces...)
94 public static function sanitize_id($id) {
95 return urlencode($id);
98 // select
99 public static function select($params, &$smarty) {
100 // @params name, options, selected, html_options
101 // set variables
102 $name = $params["name"];
103 $select_options = $params["options"];
104 $selected = $params["selected"];
105 $html_options = $params["extra"]; // change from 'extra' to something else considering there are already 'options'
107 // templates
108 $select_template = '<select name="%s" %s>%s</select>';
109 $option_template = '<option value="%s" %s>%s</option>';
111 foreach($select_options as $value=>$option) {
112 $option_selected = ($selected == $value) ? 'selected="selected"' : '';
113 $options .= sprintf($option_template, $value, $option_selected, $option);
116 return sprintf($select_template, $name, $html_options, $options);
119 public static function textile($params, &$smarty) {
120 return RedCloth::to_html($params['text']);
123 // format_post
124 public static function format_post($params, &$smarty) {
125 $output = RedCloth::to_html($params['post']);
126 return $output;
128 $post = stripslashes($params["post"]);
130 $p = '<p>%s</p>';
132 foreach(explode("\n\n", str_replace("\r", "", $post)) as $para) {
133 if(empty($para)) continue;
135 $paras .= sprintf($p, $para) . "\n";
137 if($params['truncate'] == 'intro') break; // just have the first paragraph (ingenious, no?)
139 $post = $paras;
141 // do specialized formatting (Textile style)
143 return $post;
146 // format_comment
147 public static function format_comment($params, &$smarty) {
148 $comment = htmlentities(stripslashes($params["comment"]));
150 $p = '<p>%s</p>';
151 foreach(explode("\n\n", str_replace("\r", "", $comment)) as $para) {
152 $paras .= sprintf($p, $para) . "\n";
154 $comment = $paras;
156 // do specialized formatting (Textile style)
158 return $comment;
161 // count_comments
162 public static function count_comments($params, &$smarty) {
163 return count($params["comments"]);
166 // pluralize
167 public static function pluralize($params, &$smarty) {
168 $word = $params['word'];
169 $number = (is_array($params['number'])) ? count($params['number']) : intval($params['number']);
170 $string_together = ($params['string_together'] == 'true') ? true : false;
172 // pluralize word if the count is greater than 1
173 if($number > 1 || $number == 0) $word = Pluralize::pluralize($word);
175 // Pluralize supports alternate syntax:
176 // - $Pluralize::word // returns 'words' regardless of number
177 // - $Pluralize::word = number // returns 'word' or 'words' depending on number given to it
178 // note: this is only supported for instances and not static syntax
180 // if you want to have the string "12 comments" or "1 feast" returned instead of just the pluralized form
181 if($string_together) $word = sprintf("%d %s", $number, $word);
183 return $word;
185 // @refer_to "An Algorithmic Approach to English Pluralization":http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html
188 // date functions
189 public $date_formats = array(
190 'standard'=>'Y-m-d',
191 'standard+time'=>'Y-m-d H:i:s',
192 'natural'=>'F j, Y, g:i a',
193 'mysql'=>'Y-m-d H:i:s',
195 public static function now($params, &$smarty) {
196 if(!empty(self::$date_formats[$params['format']])) $format = self::$date_formats[$params['format']];
197 if(empty($params['format'])) $format = "Y-m-d H:i:s"; else $format = $params['format'];
198 return date($format);
201 // filters (make sure these are registered in $treat_as_filter at the top)
202 public static function ago($date) {
203 $date = $date . ' ' . date('H:i:s');
204 $date = strtotime($date);
205 $days_ago = (strftime("%j") + strftime("%Y") * 365) - (strftime("%j", $date) + strftime("%Y", $date) * 365);
206 if($days_ago < 1) {
207 $hours_ago = (strftime("%H") - strftime("%H", $date));
208 if($hours_ago == 0) return "under a minute ago";
209 if($hours_ago < 1) return "under an hour ago";
210 if($hours_ago == 1) return "about an hour ago";
211 return "today";
212 } else if($days_ago < 7) {
213 // handle under a week
214 if($days_ago == 1) return "yesterday";
215 return "about {$days_ago} days ago";
216 } else if($days_ago < 27) {
217 // handle weeks
218 $weeks_ago = round($days_ago/7);
219 if($weeks_ago == 1) return "about a week ago";
220 return "about {$weeks_ago} weeks ago";
221 } else if($days_ago < 360) {
222 // handle months
223 $months_ago = round($days_ago/30);
224 if($months_ago == 1) return "about a month ago";
225 return "about {$months_ago} months ago";
226 } else {
227 // handle years
228 $years_ago = round($days_ago/366);
229 if($years_ago == 1) return "about a year ago";
230 return "about {$years_ago} years ago";
233 public static function human_readable($filesize) {
234 if(($filesize/1024) < 1024 )
235 return round(($filesize/1024), 2) . 'kb'; // return filesize in KB
236 else
237 return round((($filesize/1024)/1024), 2) . 'mb'; // otherwise, filesize in MB
239 public static function size_by_popularity($tag, $full_usage = 50, $smallest = 6) {
240 // popularity, uses, total
241 return (string)(round((float)$tag->popularity) + 0.8);// smaller ems
242 return (string)(round((float)$tag->popularity + 0.8));// smaller ems
243 return (string)(round((float)$tag->popularity) + 1);// ems
244 return (string)((((float)$tag->popularity * 2) + 1) * $smallest); // weighted
245 return (string)(((float)$tag->popularity + 1) * $smallest); // simple trajectory
246 return (string)((int)($full_usage - ($full_usage / (((float)$tag->popularity) / 1.8)))); // old
248 public static function inspect($object) {
249 print "<pre>";
250 print_r($object);
251 print "</pre>";