url = $url; } } class Badge { public $sections = array( "profile" => "Profile", "friends" => "Friends", "friends_ext" => "Imported friends", "groups" => "Groups", "networks" => "Networks", "posts" => "Blog posts and SB content", "images" => "Photos", "audio" => "Audio", "video" => "Videos", ); public $section_singular_names = array( "friends" => "friend", "friends_ext" => "friend", "groups" => "group", "networks" => "network", ); public $display_keys = array( "friends" => "friends", "friends_ext" => "friends", "groups" => "groups", "networks" => "networks", ); public $friends_per_page = 20; public $showing_states = array( 'only' => "Showing ONLY blue highlighted %KEY% from below", 'all' => "Showing first blue, then grey highlighted %KEY% from below", ); function __construct($user, $badge_tag) { $this->user = $user; $this->badge_tag = $badge_tag; try { $this->widget = $user->load_widget($this->badge_tag); $this->state =& $this->widget->config; } catch (PAException $e) { switch ($e->code) { case ROW_DOES_NOT_EXIST: // we need to have a default widget, so if we don't // have one, make it now. otherwise rethrow the // exception, and the user will be redirected to the // default widget. if ($badge_tag != 'default') throw $e; $this->widget = new Widget($this->user->user_id); $this->widget->badge_tag = $this->widget->title = $badge_tag; $this->widget->save(); $this->state =& $this->widget->config; break; default: throw $e; } } $this->url = BASE_URL_REL."/badge_create.php"; } private function save_state() { try { $this->widget->save(); } catch (PAException $e) { echo "
error occurred saving badge!
$e->code | ".$e->getMessage()."
"; exit; } } function op($op, $section, $params) { $section_singular = @$this->section_singular_names[$section]; if (method_exists($this, "op_$section")) { $ret = $this->{"op_$section"}($op, $params); if ($ret !== NULL) return $ret; } switch ($op) { // enable/disable a section 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); // include/exclude an individual friend/group/etc case 'include': case 'exclude': $subid = @$params['id']; if (is_string($subid) && substr($subid, 0, 1) == '_') { $subid = pack("H*", substr($subid, 1)); } // echo "op with section $section ..."; //FIXME: verify that the subid is actually a valid network $this->handle_op_include_exclude($section, $op, $subid); return $this->{"render_".$section_singular."_image"}($subid); // change show_state case 'show': $show_state = $params['show']; $this->handle_op_show($section, $show_state); return $this->render_section_inner($section); case 'display': return $this->render_section_inner($section); case 'create': $this->assert_post(); return $this->handle_op_create_new_badge($_POST); break; case 'rename': $this->assert_post(); return $this->handle_op_rename_badge($_POST); break; case 'delete': $this->assert_post(); return $this->handle_op_delete_badge($_POST); break; default: return "invalid $section op"; } } private function assert_post() { if ($_SERVER['REQUEST_METHOD'] != "POST") throw new PAException(OPERATION_NOT_PERMITTED, "This operation requires an HTTP POST"); } private function handle_op_create_new_badge($params) { $title = $params['new_widget_name']; // sanitize name $new_name = preg_replace("~[^A-Za-z0-9\-\_]~", "_", $title); // make sure we don't already have one with that name try { $this->user->load_widget($new_name); // that should throw an exception; if it doesn't, the name is a duplicate return new Badge_Redirect("$this->url?focus=new_widget_name&error=".urlencode("You already have a widget called $new_name. Please select another name.")); } catch (PAException $e) { switch ($e->getCode()) { case ROW_DOES_NOT_EXIST: // Good - this is what we want (no existing widget with this name). break; default: // A real error occurred; pass it through. throw $e; } } $widget = new Widget($this->user->user_id); $widget->badge_tag = $new_name; $widget->title = $title; $widget->save(); return new Badge_Redirect("$this->url/$new_name"); } private function handle_op_rename_badge($params) { $widget = $this->user->load_widget($this->badge_tag); $widget->rename($params['new_name']); return new Badge_Redirect("$this->url/$this->badge_tag"); } private function handle_op_delete_badge($params) { $widget = $this->user->load_widget($this->badge_tag); $widget->delete(); return new Badge_Redirect($this->url); } private function handle_op_include_exclude($section, $op, $subid) { $incl =& $this->state[$section]['included']; // echo "section: $section; id: $subid"; echo "This section is disabled. Click the checkbox above to show it in your sidebar widget.
'; $checkbox_checked = $enabled ? 'checked="checked"' : ''; $enabled_text = $enabled ? 'included' : '← click to include'; return <<$img
$name
ENS; } private function make_showing_link(&$state, $section_key, $qs) { $keys = array_keys($this->showing_states); if (!@$state['show']) { $state['show'] = $keys[0]; $this->save_state(); } // default $next_showing_state = $keys[(array_search($state['show'], $keys) + 1) % count($keys)]; // next one $all_checked = $state['show'] == 'all' ? 'checked="checked"' : ''; $only_checked = $state['show'] == 'only' ? 'checked="checked"' : ''; $section_key_friendly = strtolower($this->sections[$section_key]); return "$paging
ENS; } function render_group_image($grp_or_id) { $grp = new Group(); if (is_numeric($grp_or_id)) { // it's an id $grp->load((int)$grp_or_id); } else { $grp->load($grp_or_id['gid']); } $groups = $this->state['groups']; $included = isset($groups['included'][(int)$grp->collection_id]); $img = uihelper_resize_mk_user_img($grp->picture, 75, 75); $name = $grp->title; $cls = "friend_pic"; if ($included) $cls .= " included_friend"; $include_op = $included ? "exclude" : "include"; $onclick = "badge.include_obj('groups', 'group_$grp->collection_id', $grp->collection_id, '$include_op');"; return <<$img
$name
ENS; } function render_groups() { $page = 1; $group_state =& $this->state['groups']; $showing = $this->make_showing_link($group_state, "groups", ""); $facewall = ""; foreach (Group::get_user_groups($this->user->user_id, FALSE, $this->friends_per_page, $page) as $grp) { $facewall .= '$img
$name
ENS; } function render_networks() { $page = 1; $network_state =& $this->state['networks']; $showing = $this->make_showing_link($network_state, "networks", ""); $facewall = ""; foreach (Network::get_user_networks($this->user->user_id, FALSE, $this->friends_per_page, $page) as $net) { $facewall .= 'Showing recent blog posts in sidebar widget.
"; } function render_images() { return "Showing recent photos in sidebar widget.
"; } function render_audio() { return "Showing recent audio in sidebar widget.
"; } function render_video() { return "Showing recent video in sidebar widget.
"; } private function _render_template($template) { $tpl =& new Template(CURRENT_THEME_FSPATH."/widget_$template.tpl"); return $tpl->fetch(); } } // --- controller (part 2) // find user and badge $user = new user(); $user->load((int)$login_uid); try { $badge = new Badge($user, $badge_tag); } catch (PAException $e) { switch ($e->code) { case CONTENT_HAS_BEEN_DELETED: case ROW_DOES_NOT_EXIST: header("Location: $base_url/badge_create.php"); exit; default: throw $e; } } function badge_disp($content) { if ($content instanceof Badge_Redirect) { header("Location: ".$content->url); } else { echo $content; } exit; } // execute op if required if (@$params['op']) { badge_disp($badge->op($params['op'], $section, $params)); } else if (@$section) { badge_disp($badge->op('display', $section, $params)); } // --- left sidebar (widget selection) html function render_left_sidebar() { global $user, $base_url; $badge_list = ""; foreach ($user->list_widgets() as $badge_info) { list($badge_id, $title) = $badge_info; $badge_list .= 'Paste this HTML into your blog to display this information:
default badge: "; var_dump($default_badge); echo "
"; } catch (PAException $e) { echo "no default badge available.
"; } $debug_sidebar = ob_get_contents(); ob_end_clean(); return $debug_sidebar; } // --- $user_generaldata = User::load_user_profile($uid, (int)$_SESSION['user']['id'], GENERAL); $user_data_general = sanitize_user_data ($user_generaldata); $page = new PageRenderer(NULL, PAGE_BADGE_CREATE, "Design your sidebar widget", "container_three_column.tpl", 'header_user.tpl'); $page->onload = "badge.update();"; $page->add_header_css("$current_theme_path/badge_create.css"); $page->add_module("left", "top", render_left_sidebar()); //$page->add_module("left", "top", render_debug_sidebar()); // debugging $page->add_module("middle", "top", $page_html); $page->add_module("right", "top", render_right_sidebar()); $page->header->set('display_image', $login_user->get_profile_field(GENERAL, 'desktop_image_display')); // PP: Is this boilerplace stuff? Can we move it into PageRenderer? $theme_details = get_user_theme($uid); if (is_array($theme_details['css_files'])) { foreach ($theme_details['css_files'] as $key => $value) { $page->add_header_css($value); } } $page->header->set('theme_details', $theme_details); echo $page->render(); ?>