| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file facebook_apps_theme.inc
|
| 4 |
* Theme functions and theme helper functions for the facebook_apps module.
|
| 5 |
**/
|
| 6 |
|
| 7 |
/**
|
| 8 |
* Load up all the CSS with $media set as 'facebook' and display it inline.
|
| 9 |
* This is the only form of style block FaceBook supports at the moment.
|
| 10 |
**/
|
| 11 |
function facebook_apps_get_inline_css() {
|
| 12 |
$css = drupal_add_css();
|
| 13 |
|
| 14 |
$style_data = '<style type="text/css">';
|
| 15 |
|
| 16 |
foreach ($css['facebook'] as $type) {
|
| 17 |
foreach ($type as $path => $preprocess) {
|
| 18 |
$paths[$path] = $preprocess;
|
| 19 |
|
| 20 |
// TODO: When this code is bullet proof, compact the CSS using a compressor then cache it.
|
| 21 |
$style_data .= "/* INLINE CSS FROM: $path */\n" . file_get_contents($path) . "\n\n";
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
$style_data .= '</style>';
|
| 26 |
|
| 27 |
return $style_data;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Rewrite's $content with rules from the hook_facebook_inline_css
|
| 32 |
**/
|
| 33 |
function facebook_apps_rewrite_inline_css($content) {
|
| 34 |
// Each instance of the hook facebook_inline_css should accept $content by reference.
|
| 35 |
// $rules should be of the form array('@from' => 'to', '@here' => 'to_there')
|
| 36 |
$rules = module_invoke_all('facebook_inline_css', $content);
|
| 37 |
|
| 38 |
return strtr($content, $rules);
|
| 39 |
}
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/**
|
| 44 |
* The functionality of the pager_query but works on an array.
|
| 45 |
*
|
| 46 |
* @param $dataset
|
| 47 |
* The dataset to be paged through.
|
| 48 |
* @param $limit
|
| 49 |
* How many results to return at a time.
|
| 50 |
* @param $element
|
| 51 |
* The pager element (see pager_query)
|
| 52 |
* @return $subset
|
| 53 |
* A slice of the array.
|
| 54 |
**/
|
| 55 |
function pager_query_array($dataset, $limit = 10, $element = 0) {
|
| 56 |
global $pager_page_array, $pager_total, $pager_total_items;
|
| 57 |
$page = isset($_GET['page']) ? $_GET['page'] : '';
|
| 58 |
|
| 59 |
// Substitute in query arguments.
|
| 60 |
$args = func_get_args();
|
| 61 |
$args = array_slice($args, 4);
|
| 62 |
// Alternative syntax for '...'
|
| 63 |
if (isset($args[0]) && is_array($args[0])) {
|
| 64 |
$args = $args[0];
|
| 65 |
}
|
| 66 |
|
| 67 |
// Convert comma-separated $page to an array, used by other functions.
|
| 68 |
$pager_page_array = explode(',', $page);
|
| 69 |
|
| 70 |
// We calculate the total of pages as ceil(items / limit).
|
| 71 |
$pager_total_items[$element] = count($dataset);
|
| 72 |
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
|
| 73 |
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
|
| 74 |
if (is_array($dataset) && count($dataset) > 1) {
|
| 75 |
return array_slice($dataset, $pager_page_array[0] * $limit, $limit);
|
| 76 |
} else {
|
| 77 |
return array();
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* A slight augmentation of the existing Drupal pager to better fit in the
|
| 83 |
* Facebook FBML framework.
|
| 84 |
**/
|
| 85 |
function theme_facebook_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) {
|
| 86 |
global $pager_total;
|
| 87 |
$output = '';
|
| 88 |
|
| 89 |
if ($pager_total[$element] > 1) {
|
| 90 |
$output .= '<ul id="pag_nav_links" class="pagerpro">';
|
| 91 |
// $output .= '<li>' . theme('facebook_pager_first', ($tags[0] ? $tags[0] : t('´ first')), $limit, $element, $parameters) . '</li>';
|
| 92 |
$output .= theme('facebook_pager_previous', ($tags[1] ? $tags[1] : t('Prev')), $limit, $element, 1, $parameters);
|
| 93 |
$output .= theme('facebook_pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $parameters);
|
| 94 |
$output .= theme('facebook_pager_next', ($tags[3] ? $tags[3] : t('Next')), $limit, $element, 1, $parameters);
|
| 95 |
// $output .= '<li>' . theme('facebook_pager_last', ($tags[4] ? $tags[4] : t('last ª')), $limit, $element, $parameters) . '</li>';
|
| 96 |
$output .= '</ul>';
|
| 97 |
|
| 98 |
return $output;
|
| 99 |
}
|
| 100 |
}
|
| 101 |
function theme_facebook_pager_first($text, $limit, $element = 0, $parameters = array()) {
|
| 102 |
global $pager_page_array;
|
| 103 |
$output = '';
|
| 104 |
|
| 105 |
// If we are anywhere but the first page
|
| 106 |
if ($pager_page_array[$element] > 0) {
|
| 107 |
$output = theme('facebook_pager_link', $text, pager_load_array(0, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-first'));
|
| 108 |
}
|
| 109 |
|
| 110 |
return $output;
|
| 111 |
}
|
| 112 |
function theme_facebook_pager_previous($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
|
| 113 |
global $pager_page_array;
|
| 114 |
$output = '';
|
| 115 |
|
| 116 |
// If we are anywhere but the first page
|
| 117 |
if ($pager_page_array[$element] > 0) {
|
| 118 |
$page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
|
| 119 |
|
| 120 |
// If the previous page is the first page, mark the link as such.
|
| 121 |
if ($page_new[$element] == 0) {
|
| 122 |
$output = theme('facebook_pager_first', $text, $limit, $element, $parameters);
|
| 123 |
}
|
| 124 |
// The previous page is not the first page.
|
| 125 |
else {
|
| 126 |
$output = theme('facebook_pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-previous'));
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
return $output;
|
| 131 |
}
|
| 132 |
function theme_facebook_pager_list($limit, $element = 0, $quantity = 5, $text = '', $parameters = array()) {
|
| 133 |
global $pager_page_array, $pager_total;
|
| 134 |
|
| 135 |
// $output = '<span>';
|
| 136 |
// Calculate various markers within this pager piece:
|
| 137 |
// Middle is used to "center" pages around the current page.
|
| 138 |
$pager_middle = ceil($quantity / 2);
|
| 139 |
// current is the page we are currently paged to
|
| 140 |
$pager_current = $pager_page_array[$element] + 1;
|
| 141 |
// first is the first page listed by this pager piece (re quantity)
|
| 142 |
$pager_first = $pager_current - $pager_middle + 1;
|
| 143 |
// last is the last page listed by this pager piece (re quantity)
|
| 144 |
$pager_last = $pager_current + $quantity - $pager_middle;
|
| 145 |
// max is the maximum page number
|
| 146 |
$pager_max = $pager_total[$element];
|
| 147 |
// End of marker calculations.
|
| 148 |
|
| 149 |
// Prepare for generation loop.
|
| 150 |
$i = $pager_first;
|
| 151 |
if ($pager_last > $pager_max) {
|
| 152 |
// Adjust "center" if at end of query.
|
| 153 |
$i = $i + ($pager_max - $pager_last);
|
| 154 |
$pager_last = $pager_max;
|
| 155 |
}
|
| 156 |
if ($i <= 0) {
|
| 157 |
// Adjust "center" if at start of query.
|
| 158 |
$pager_last = $pager_last + (1 - $i);
|
| 159 |
$i = 1;
|
| 160 |
}
|
| 161 |
// End of generation loop preparation.
|
| 162 |
|
| 163 |
// When there is more than one page, create the pager list.
|
| 164 |
if ($i != $pager_max) {
|
| 165 |
$output .= $text;
|
| 166 |
// if ($i > 1) {
|
| 167 |
// $output .= '<li><span>Ö</span></li>';
|
| 168 |
// }
|
| 169 |
|
| 170 |
// Now generate the actual pager piece.
|
| 171 |
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
|
| 172 |
if ($i < $pager_current) {
|
| 173 |
$output .= theme('facebook_pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters);
|
| 174 |
}
|
| 175 |
if ($i == $pager_current) {
|
| 176 |
$output .= theme('facebook_pager_link', $i, $limit, $element, $i, $parameters, true);
|
| 177 |
}
|
| 178 |
if ($i > $pager_current) {
|
| 179 |
$output .= theme('facebook_pager_next', $i, $limit, $element, ($i - $pager_current), $parameters);
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
// if ($i < $pager_max) {
|
| 184 |
// $output .= '<li><span>Ö</span></li>';
|
| 185 |
// }
|
| 186 |
}
|
| 187 |
// $output .= '</span>';
|
| 188 |
|
| 189 |
return $output;
|
| 190 |
}
|
| 191 |
function theme_facebook_pager_next($text, $limit, $element = 0, $interval = 1, $parameters = array()) {
|
| 192 |
global $pager_page_array, $pager_total;
|
| 193 |
$output = '';
|
| 194 |
|
| 195 |
// If we are anywhere but the last page
|
| 196 |
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
|
| 197 |
$page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
|
| 198 |
// If the next page is the last page, mark the link as such.
|
| 199 |
if ($page_new[$element] == ($pager_total[$element] - 1)) {
|
| 200 |
$output = theme('facebook_pager_last', $text, $limit, $element, $parameters);
|
| 201 |
}
|
| 202 |
// The next page is not the last page.
|
| 203 |
else {
|
| 204 |
$output = theme('facebook_pager_link', $text, $page_new, $element, $parameters, array('class' => 'pager-next'));
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
return $output;
|
| 209 |
}
|
| 210 |
function theme_facebook_pager_last($text, $limit, $element = 0, $parameters = array()) {
|
| 211 |
global $pager_page_array, $pager_total;
|
| 212 |
$output = '';
|
| 213 |
|
| 214 |
// If we are anywhere but the last page
|
| 215 |
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
|
| 216 |
$output = theme('facebook_pager_link', $text, pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), $element, $parameters, array('class' => 'pager-last'));
|
| 217 |
}
|
| 218 |
|
| 219 |
return $output;
|
| 220 |
}
|
| 221 |
function theme_facebook_pager_link($text, $page_new, $element, $parameters = array(), $attributes = array(), $current = false) {
|
| 222 |
$page = isset($_GET['page']) ? $_GET['page'] : '';
|
| 223 |
// drupal_set_message("page: " . $page . ", page_new: " . $page_new[$element] . ", element: " . $element . ", exploded: " . var_export(explode(',', $page), 1));
|
| 224 |
|
| 225 |
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
|
| 226 |
$parameters['page'] = $new_page;
|
| 227 |
}
|
| 228 |
|
| 229 |
$query = array();
|
| 230 |
if (count($parameters) && is_array($parameters)) {
|
| 231 |
$query[] = drupal_query_string_encode($parameters, array());
|
| 232 |
}
|
| 233 |
$querystring = facebook_pager_get_querystring();
|
| 234 |
if ($querystring != '') {
|
| 235 |
$query[] = $querystring;
|
| 236 |
}
|
| 237 |
|
| 238 |
// Set each pager link title
|
| 239 |
if (!isset($attributes['title'])) {
|
| 240 |
static $titles = NULL;
|
| 241 |
if (!isset($titles)) {
|
| 242 |
$titles = array(
|
| 243 |
t('´ first') => t('Go to first page'),
|
| 244 |
t('ã previous') => t('Go to previous page'),
|
| 245 |
t('next õ') => t('Go to next page'),
|
| 246 |
t('last ª') => t('Go to last page'),
|
| 247 |
);
|
| 248 |
}
|
| 249 |
if (isset($titles[$text])) {
|
| 250 |
$attributes['title'] = $titles[$text];
|
| 251 |
}
|
| 252 |
else if (is_numeric($text)) {
|
| 253 |
$attributes['title'] = t('Go to page @number', array('@number' => $text));
|
| 254 |
}
|
| 255 |
}
|
| 256 |
|
| 257 |
global $fb_conf;
|
| 258 |
|
| 259 |
return ($current ? '<li class="current">' : '<li>') . l($text, $fb_conf['app_url'] . '/' . facebook_apps_get_app_path(), $attributes, count($query) ? implode('&', $query) : NULL) . '</li>';
|
| 260 |
}
|
| 261 |
|
| 262 |
function facebook_pager_get_querystring() {
|
| 263 |
static $string = NULL;
|
| 264 |
if (!isset($string)) {
|
| 265 |
$request_no_fb_vars = array();
|
| 266 |
foreach ($_REQUEST as $key => $val) {
|
| 267 |
if (substr($key, 0, 6) != 'fb_sig') {
|
| 268 |
$request_no_fb_vars[$key] = $val;
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
$string = drupal_query_string_encode($request_no_fb_vars, array_merge(array('q', 'page'), array_keys($_COOKIE)));
|
| 273 |
}
|
| 274 |
return $string;
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* Writes out a string like 'Displaying 10 out of 256 NOUNs'
|
| 279 |
**/
|
| 280 |
function theme_facebook_pager_stats($element, $limit = 10, $noun = 'Widget') {
|
| 281 |
global $pager_page_array, $pager_total, $pager_total_items;
|
| 282 |
|
| 283 |
$start = $pager_page_array[$element] * $limit + 1;
|
| 284 |
$end = ($start <= ($pager_total_items[$element] - $limit) ? $start - 1 + $limit : $pager_total_items[$element]);
|
| 285 |
|
| 286 |
if ($pager_total[$element] > 1) {
|
| 287 |
return t(
|
| 288 |
'<div class="summary">Displaying <span id="pag_obj_first_on_page">!start</span> - <span id="pag_obj_last_on_page">!end</span> out of <span id="pag_obj_total">!total</span> of your !nouns.</div>',
|
| 289 |
array(
|
| 290 |
'!start' => $start,
|
| 291 |
'!end' => $end,
|
| 292 |
'!total' => $pager_total_items[$element],
|
| 293 |
'!noun' => $noun,
|
| 294 |
)
|
| 295 |
);
|
| 296 |
} else {
|
| 297 |
return t(
|
| 298 |
'<div class="summary">Displaying your !nouns.</div>',
|
| 299 |
array(
|
| 300 |
'!noun' => $noun,
|
| 301 |
)
|
| 302 |
);
|
| 303 |
}
|
| 304 |
}
|