Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 328 lines (300 sloc) 16.08 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
<?php
/**
*
* ThinkUp/webapp/_lib/controller/class.DashboardController.php
*
* Copyright (c) 2009-2012 Gina Trapani, Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Dashboard controller
*
* The main controller which displays a given view for a give instance user.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Gina Trapani, Mark Wilkie
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
class DashboardController extends ThinkUpController {
    /**
* Instance user
* @var Instance
*/
    var $instance;
    /**
* View name
* @var str
*/
    var $view_name;

    public function control() {
        $this->setViewTemplate('dashboard.tpl');
        if ($this->shouldRefreshCache() ) {
            $this->setInstance();

            $this->view_name = (isset($_GET['v']))?$_GET['v']:'default';
            $webapp_plugin_registrar = PluginRegistrarWebapp::getInstance();
            if (isset($this->instance)) {
                $webapp_plugin_registrar->setActivePlugin($this->instance->network);
                $sidebar_menu = $webapp_plugin_registrar->getDashboardMenu($this->instance);
                $this->addToView('sidebar_menu', $sidebar_menu);
                $this->loadView();
            } else {
                if (!Session::isLoggedIn()) {
                    $this->addInfoMessage('There are no public accounts set up in this ThinkUp installation.');
                } else {
                    $this->addInfoMessage('Welcome to ThinkUp. Let\'s get started.');

                    $plugin_dao = DAOFactory::getDAO('PluginDAO');
                    $plugins = $plugin_dao->getInstalledPlugins();
                    $add_user_buttons = array();
                    foreach ($plugins as $plugin) {
                        if ($plugin->folder_name == 'twitter' || $plugin->folder_name == 'facebook'
                        || $plugin->folder_name == 'googleplus') {
                            if ($plugin->is_active) {
                                $add_user_buttons[] = $plugin->folder_name;
                            }
                        }
                    }
                    $add_user_buttons = array_reverse($add_user_buttons);
                    $this->addToView('add_user_buttons', $add_user_buttons);
                }
            }
        }
        return $this->generateView();
    }

    /**
* Load the view with required variables
*/
    private function loadView() {
        $webapp_plugin_registrar = PluginRegistrarWebapp::getInstance();
        if ($this->view_name == 'default') {
            $this->loadDefaultDashboard();
        } else {
            $menu_item = $webapp_plugin_registrar->getDashboardMenuItem($this->view_name, $this->instance);
            if (isset($menu_item)) {
                $this->addToView('data_template', $menu_item->view_template);
                $this->addToView('display', $this->view_name);
                $this->addToView('header', $menu_item->name);
                $this->addToView('description', $menu_item->description);
                $this->addToView('parent', $menu_item->parent);

                $this->setPageTitle($this->instance->network_username.' on '.ucfirst($this->instance->network));
                $page = (isset($_GET['page']) && is_numeric($_GET['page']))?$_GET['page']:1;
                foreach ($menu_item->datasets as $dataset) {
                    if (array_search('#page_number#', $dataset->method_params) !== false) { //there's paging
                        $this->addToView('next_page', $page+1);
                        $this->addToView('last_page', $page-1);
                    }
                    $this->addToView($dataset->name, $dataset->retrieveDataset($page));
                    if (Session::isLoggedIn() && $dataset->isSearchable()) {
                        $view_name = 'is_searchable';
                        $this->addToView($view_name, true);
                    }
                    $this->view_mgr->addHelp($this->view_name, $dataset->getHelp());
                }
            } else {
                $this->loadDefaultDashboard();
            }
        }
    }

    /**
* Set the instance variable based on request and logged-in status
* Add the list of avaiable instances to the view you can switch to in the dropdown based on logged-in status
*/
    private function setInstance() {
        $instance_dao = DAOFactory::getDAO('InstanceDAO');
        $config = Config::getInstance();
        if ($this->isLoggedIn()) {
            $owner_dao = DAOFactory::getDAO('OwnerDAO');
            $owner = $owner_dao->getByEmail($this->getLoggedInUser());
            $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
            if (isset($_GET["u"]) && isset($_GET['n'])) {
                $instance = $instance_dao->getByUsernameOnNetwork(stripslashes($_GET["u"]), $_GET['n']);
                if (isset($instance)) {
                    if ($owner_instance_dao->doesOwnerHaveAccessToInstance($owner, $instance)) {
                        $this->instance = $instance;
                    } else {
                        $this->instance = null;
                        $this->addErrorMessage("Insufficient privileges");
                    }
                } else {
                    $this->addErrorMessage(stripslashes($_GET["u"])." on ".ucfirst($_GET['n']) ." is not in ThinkUp.");
                }
            } else {
                $this->instance = $instance_dao->getFreshestByOwnerId($owner->id);
            }
            $owner_instance = $owner_instance_dao->get($owner->id, $this->instance->id);
            if (isset($owner_instance) && $owner_instance->auth_error != '') {
                $this->addErrorMessage("ThinkUp can't connect to your ". ucwords($this->instance->network).
                " account. This is probably normal - the connection expires after a certain amount of time. ".
                "To fix it, in <a href=\"account/?p=".
                (($this->instance->network=='facebook page')?'facebook':$this->instance->network)."\">".
                ucwords($this->instance->network).
                " settings</a>, re-add this account.", null, true);
            }
            $this->addToView('instances', $instance_dao->getByOwner($owner));
        } else {
            if (isset($_GET["u"]) && isset($_GET['n'])) {
                $instance = $instance_dao->getByUsernameOnNetwork(stripslashes($_GET["u"]), $_GET['n']);
                if (isset($instance)) {
                    if ($instance->is_public) {
                        $this->instance = $instance;
                    } else {
                        $this->addErrorMessage("Insufficient privileges");
                    }
                } else {
                    $this->addErrorMessage(stripslashes($_GET["u"])." on ".ucfirst($_GET['n']) ." is not in ThinkUp.");
                }
            }
            $this->addToView('instances', $instance_dao->getPublicInstances());
        }
        if (!isset($this->instance)) {
            // A specific instance wasn't passed in the URL (or isn't accessible), get a default one
            $instance_id_to_display = $config->getValue('default_instance');
            $instance_id_to_display = intval($instance_id_to_display);
            if ( $instance_id_to_display != 0) {
                $this->instance = $instance_dao->get($instance_id_to_display);
            }
            if (!isset($this->instance) || !$this->instance->is_public) {
                $this->instance = $instance_dao->getInstanceFreshestPublicOne();
            }
        }
        if (isset($this->instance)) {
            //user
            $user_dao = DAOFactory::getDAO('UserDAO');
            $user = $user_dao->getDetails($this->instance->network_user_id, $this->instance->network);
            $this->addToView('user_details', $user);
            if (Session::isLoggedIn() && !isset($user)) {
                $this->addInfoMessage("Oops! There's no information about ".$this->instance->network_username.
                " on ".ucfirst($this->instance->network)." to display.");
                $this->addToView('show_update_now_button', true);
            }

            SessionCache::put('selected_instance_network', $this->instance->network);
            SessionCache::put('selected_instance_username', $this->instance->network_username);

            //check Realtime last update and overwrite instance->last_update
            $stream_proc_dao = DAOFactory::getDAO('StreamProcDAO');
            $process = $stream_proc_dao->getProcessInfoForInstance($this->instance->id);
            if (isset($process)) {
                //$this->instance->crawler_last_run = $process['last_report'];
                $this->instance->crawler_last_run = 'realtime';
            }

            $this->addToView('instance', $this->instance);
        } else {
            SessionCache::put('selected_instance_network', null);
            SessionCache::put('selected_instance_username', null);
        }

        $this->addToView('developer_log', $config->getValue('is_log_verbose'));
    }

    /**
* Load instance dashboard
* @param str $username
* @param str $network
*/
    private function loadDefaultDashboard() {
        if (isset($this->instance)) {
            $this->setPageTitle($this->instance->network_username . "'s Dashboard");

            $insight_dao = DAOFactory::getDAO('InsightDAO');

            $hot_posts_data = $insight_dao->getPreCachedInsightData('PostMySQLDAO::getHotPosts', $this->instance->id,
            date('Y-m-d'));
            if (isset($hot_posts_data)) {
                $this->addToView('hot_posts_data', $hot_posts_data);
            }

            $yearly_popular = $insight_dao->getPreCachedInsightData('PostMySQLDAO::getMostPopularPostsOfTheYear',
            $this->instance->id, date('Y-m-d'));
            if (isset($yearly_popular)) {
                if (date('n') == 12) {
                    $yearly_popular_year = date('Y');
                } else {
                    $yearly_popular_year = intval(date('Y'))-1;
                }
                $this->addToView('yearly_popular', $yearly_popular);
                $this->addToView('yearly_popular_year', $yearly_popular_year);
            }

            $click_stats_data = $insight_dao->getPreCachedInsightData( 'ShortLinkMySQLDAO::getRecentClickStats',
            $this->instance->id, date('Y-m-d'));
            if (isset($click_stats_data)) {
                $this->addToView('click_stats_data', $click_stats_data);
            }

            $post_dao = DAOFactory::getDAO('PostDAO');
            $most_replied_to_1wk = $insight_dao->getPreCachedInsightData(
            'PostMySQLDAO::getMostRepliedToPostsInLastWeek', $this->instance->id, date('Y-m-d'));
            $this->addToView('most_replied_to_1wk', $most_replied_to_1wk);

            $most_retweeted_1wk = $insight_dao->getPreCachedInsightData(
            'PostMySQLDAO::getMostRetweetedPostsInLastWeek', $this->instance->id, date('Y-m-d'));
            $this->addToView('most_retweeted_1wk', $most_retweeted_1wk);
            //for now, only show most liked/faved posts on Facebook dashboard
            //once we cache fave counts for Twitter, we can remove this conditional
            if ($this->instance->network == "facebook" || $this->instance->network == "facebook page"
            || $this->instance->network == "google+") {
                $most_faved_1wk = $post_dao->getMostFavedPostsInLastWeek($this->instance->network_username,
                $this->instance->network, 5);
                $this->addToView('most_faved_1wk', $most_faved_1wk);
            }

            //follows - these are pre-cached in insights
            $least_likely_followers = $insight_dao->getPreCachedInsightData(
            'FollowMySQLDAO::getLeastLikelyFollowersThisWeek', $this->instance->id, date('Y-m-d'));
            $this->addToView('least_likely_followers', $least_likely_followers);

            //follower count history
            //by day
            $follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
            $follower_count_history_by_day = $follower_count_dao->getHistory($this->instance->network_user_id,
            $this->instance->network, 'DAY', 5);
            $this->addToView('follower_count_history_by_day', $follower_count_history_by_day);

            //by week
            $follower_count_history_by_week = $follower_count_dao->getHistory($this->instance->network_user_id,
            $this->instance->network, 'WEEK', 5);
            $this->addToView('follower_count_history_by_week', $follower_count_history_by_week);

            list($all_time_clients_usage, $latest_clients_usage) =
            $insight_dao->getPreCachedInsightData(
            'PostMySQLDAO::getClientsUsedByUserOnNetwork', $this->instance->id, date('Y-m-d'));

            if (is_array($all_time_clients_usage)) {
                // The sliceVisibilityThreshold option in the chart will prevent small slices from being created
                $all_time_clients_usage =
                DashboardModuleCacher::getClientUsageVisualizationData($all_time_clients_usage);
                $this->addToView('all_time_clients_usage', $all_time_clients_usage);
            }

            if (is_array($latest_clients_usage) && sizeof($latest_clients_usage > 1)) {
                // Only show the two most used clients for the last 25 posts
                $latest_clients_usage = array_slice($latest_clients_usage, 0, 2);
                $this->addToView('latest_clients_usage', $latest_clients_usage);
            }

            $posts_flashback = $insight_dao->getPreCachedInsightData('PostMySQLDAO::getOnThisDayFlashbackPosts',
            $this->instance->id, date('Y-m-d'));
            $this->addToView('posts_flashback', $posts_flashback);

            // Foursquare items
            if ($this->instance->network == "foursquare") {
                // Checkins per hour
                $checkins_per_hour = $insight_dao->getPreCachedInsightData(
                'PostMySQLDAO::getPostsPerHourDataVis', $this->instance->id, date('Y-m-d'));
                $this->addToView('checkins_per_hour', $checkins_per_hour);

                // Checkins by type of place - all time
                $place_types = $insight_dao->getPreCachedInsightData(
                'PostMySQLDAO::countCheckinsToPlaceTypes', $this->instance->id, date('Y-m-d'));
                $this->addToView('checkins_by_type', $place_types);

                // Checkins by type of place - last week
                $place_types_last_week = $insight_dao->getPreCachedInsightData(
                'PostMySQLDAO::countCheckinsToPlaceTypesLastWeek', $this->instance->id, date('Y-m-d'));
                $this->addToView('checkins_by_type_last_week', $place_types_last_week);

                // Map of checkins in the last week
                $checkins_map = $insight_dao->getPreCachedInsightData(
                'PostMySQLDAO::getAllCheckinsInLastWeekAsGoogleMap', $this->instance->id, date('Y-m-d'));
                $this->addToView('checkins_map', $checkins_map);
            }
        } else {
            $this->addErrorMessage($username." on ".ucwords($this->instance->network).
            " isn't set up on this ThinkUp installation.");
        }
    }
}
Something went wrong with that request. Please try again.