"Friends", // "groups" => "Groups", ); public $friends_per_page = 20; public $showing_states = array( 'all' => "Showing first blue, then grey highlighted friends from below", 'only' => "Showing ONLY blue highlighted friends from below", ); function __construct($user, $badge_tag) { $this->user = $user; $this->badge_tag = $badge_tag; try { $this->state = $user->get_blog_badge_config($this->badge_tag); } catch (PAException $e) { if ($e->code != ROW_DOES_NOT_EXIST) throw $e; $this->state = array("friends"=>array()); } $this->url = BASE_URL_REL."/badge_create.php"; } private function save_state() { try { $this->user->save_blog_badge_config($this->badge_tag, $this->state); } catch (PAException $e) { echo "

error occurred saving badge!

$e->code | ".$e->getMessage()."

"; exit; } } function op($op, $section, $params) { switch ($op) { case 'enable': if (!isset($this->sections[$section])) return "invalid section for enable"; if (!isset($this->state[$section])) $this->state[$section] = array(); $this->state[$section]['enabled'] = TRUE; $this->save_state(); return $this->render_section_inner($section); case 'disable': if (!isset($this->sections[$section])) return "invalid section for disable"; if (isset($this->state[$section])) $this->state[$section]['enabled'] = FALSE; $this->save_state(); return $this->render_section_inner($section); default: return $this->{"op_$section"}($op, $params); } } function op_friends($op, $params) { switch ($op) { case 'include': case 'exclude': @list($subid) = $params; //FIXME: verify that the subid is actually a friend $incl =& $this->state['friends']['included']; if (!isset($incl)) {$incl = array(); $this->save_state();} if ($op == 'include') $incl[(int)$subid] = 1; else if (isset($incl)) unset($incl[(int)$subid]); $this->save_state(); return $this->render_friend_image((int)$subid); case 'show': @list($show_state) = $params; if (!@$this->showing_states[$show_state]) return "invalid show_state"; $this->state['friends']['show'] = $show_state; $this->save_state(); return $this->render_section_inner("friends"); case 'display': return $this->render_section_inner("friends"); default: return "invalid friends op"; } } // render the entire editing interface function render() { $ret = ""; foreach ($this->sections as $key => $title) { $ret .= $this->render_section($key, $title, "this:render_$key"); } return $ret; } // render an entire section (e.g. friends, groups, ...) function render_section($key, $title, $body) { $inner = $this->render_section_inner($key, $title, $body); return << $inner ENS; } // render just the internal part of a section div function render_section_inner($key) { $title = $this->sections[$key]; $enabled = @$this->state[$key]['enabled']; $enable_op = $enabled ? "disable" : "enable"; $inner = $enabled ? $this->{"render_$key"}() : "

This section is disabled. Click 'enable' above to show it in your blog badge.

"; return <<$title $enable_op $inner ENS; } // section-specific renderers function render_friend_image($rel_or_id) { $rel = new User(); if (is_integer($rel_or_id)) { // it's an id $rel->load($rel_or_id); } else { foreach (array("user_id", "first_name", "last_name", "picture") as $k) $rel->{$k} = $rel_or_id[$k]; } $friends = $this->state['friends']; $included = isset($friends['included'][(int)$rel->user_id]); $img = uihelper_resize_mk_user_img($rel->picture, 75, 75); $name = $rel->first_name.' '.$rel->last_name; $cls = "friend_pic"; if ($included) $cls .= " included_friend"; $include_op = $included ? "exclude" : "include"; $onclick = "reload_section('friend_$rel->user_id', '$this->url/op/friends/$include_op/$rel->user_id');"; return <<

$img

$name

ENS; } function render_friends() { $page = (int)@$_REQUEST['page']; $friend_state =& $this->state['friends']; if (!isset($friend_state['included'])) { $friend_state['included'] = array(); $this->save_state(); } // paging controls $total_friends = Relation::count_relations($this->user->user_id); $total_pages = (int)ceil((float)$total_friends / $this->friends_per_page); if ($page < 1) $page = 1; if ($page > $total_pages) $page = $total_pages; $paging = "Show page: "; for ($i = 1; $i < $total_pages+1; ++$i) { if ($i == $page) { $paging .= "$i "; } else { $paging .= "url/op/friends/display?page=$i');\">$i "; } } $first_friend = ($page-1)*$this->friends_per_page + 1; $last_friend = min($first_friend + $this->friends_per_page - 1, $total_friends); $paging .= "(showing $first_friend-$last_friend of $total_friends friends)"; // 'showing XXX' link $keys = array_keys($this->showing_states); if (!@$friend_state['show']) { $friend_state['show'] = $keys[0]; $this->save_state(); } // default $next_showing_state = $keys[(array_search($friend_state['show'], $keys) + 1) % count($keys)]; // next one $showing = "url/op/friends/show/$next_showing_state?page=$page')\">".$this->showing_states[$friend_state['show']]." (click to change)"; // facewall display $facewall = ""; foreach (Relation::get_all_relations($this->user->user_id, 0, FALSE, $this->friends_per_page, $page) as $rel) { $facewall .= '
'.$this->render_friend_image($rel).'
'; } // outer template return <<$showing

$facewall

$paging

ENS; } function render_groups() { return <<this is the group display

ENS; } } $user = new user(); $user->load((int)$login_uid); $badge_tag = "default"; $badge = new Badge($user, $badge_tag); // --- controller $path_info = @$_SERVER['PATH_INFO']; $bits = preg_split("|/|", $path_info); if (@$bits[0] == "" && @$bits[1] == "op") { $section = @$bits[2]; $op = @$bits[3]; $params = @array_slice($bits, 4); echo $badge->op($op, $section, $params); exit; } // --- main center column html ob_start(); ?>

Design your blog badge

render(); ?>

Paste this HTML into your blog to display this information:

Preview

badges: "; var_dump($user->list_blog_badges()); echo "

"; try { $default_badge = $user->get_blog_badge_config("default"); echo "

default badge: "; var_dump($default_badge); echo "

"; } catch (PAException $e) { echo "

no default badge available.

"; } $left_sidebar = ob_get_contents(); ob_end_clean(); // --- $page = new PageRenderer(NULL, PAGE_BADGE_CREATE, "Design your blog badge"); $page->onload = "update_badge();"; $page->add_header_css("$current_theme_path/badge_create.css"); $page->add_module("left", "top", " "); // is there a better way to push stuff into the middle of the page? //$page->add_module("left", "top", $left_sidebar); // debugging $page->add_module("middle", "top", $page_html); $page->add_module("right", "top", $preview_html); echo $page->render(); ?>