Changeset 840

Show
Ignore:
Timestamp:
08/29/08 02:17:46 (3 months ago)
Author:
ayunyan
Message:

plugin:fireeagle added dashboard module

Location:
plugins/fireeagle/trunk
Files:
5 added
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/fireeagle/trunk/fireeagle.plugin.php

    r839 r840  
    4747 
    4848        Options::set('fireeagle__refresh_interval', 600); 
     49        Modules::add(_t('Fire Eagle', 'fireeagle')); 
    4950    } 
    5051 
     
    9091            $form->out(); 
    9192        } elseif ($action == _t('Authorize', 'fireeagle')) { 
     93            // get request token 
    9294            $fireeagle = new FireEagleAPI($this->consumer_key, $this->consumer_secret); 
    9395            $token = $fireeagle->getRequestToken(); 
     
    116118                return; 
    117119            } 
     120 
     121            // get access token 
    118122            $fireeagle = new FireEagleAPI($this->consumer_key, $this->consumer_secret, 
    119123                $_SESSION['fireeagle']['req_token'], $_SESSION['fireeagle']['req_token_secret']); 
     
    133137    public function on_success($form) 
    134138    { 
     139        $form->save(); 
     140 
    135141        $params = array( 
    136142            'name' => 'fireeagle:refresh', 
     
    146152 
    147153    /** 
     154     * action: admin_header 
     155     * 
     156     * @access public 
     157     * @param object $theme 
     158     * @return void 
     159     */ 
     160    public function action_admin_header($theme) 
     161    { 
     162        if ($theme->page != 1) return; 
     163        Stack::add('admin_header_javascript', $this->get_url() . '/js/admin.js'); 
     164        Stack::add('admin_stylesheet', array($this->get_url() . '/css/admin.css', 'screen')); 
     165    } 
     166 
     167    /** 
     168     * action: before_act_admin_ajax 
     169     * 
     170     * @access public 
     171     * @return void 
     172     */ 
     173    public function action_before_act_admin_ajax() 
     174    { 
     175        $handler_vars = Controller::get_handler_vars(); 
     176        switch ($handler_vars['context']) { 
     177        case 'fireeagle_update': 
     178            $user = User::identify(); 
     179            $access_token = Options::get('fireeagle__access_token_' . $user->id); 
     180            $access_token_secret = Options::get('fireeagle__access_token_secret_' . $user->id); 
     181            if (empty($access_token) || empty($access_token_secret)) { 
     182                echo json_encode(array('errorMessage' => _t('Authorize is not done!', 'fireeagle'))); 
     183                die(); 
     184            } 
     185 
     186            $fireeagle = new FireEagleAPI($this->consumer_key, $this->consumer_secret, $access_token, $access_token_secret); 
     187            try { 
     188                $result = $fireeagle->update(array('address' => $handler_vars['location'])); 
     189            } catch (FireEagleException $e) { 
     190                echo json_encode(array('errorMessage' => $e->getMessage())); 
     191                die(); 
     192            } 
     193            if (!is_object($result) || $result->stat != 'ok') { 
     194                echo json_encode(array('errorMessage' => _t('Update failed.', 'fireeagle'))); 
     195                die(); 
     196            } 
     197 
     198            // refresh location 
     199            if (!$this->update((int)$user->id)) { 
     200                echo json_encode(array('errorMessage' => _t('Update failed.', 'fireeagle'))); 
     201                die(); 
     202            } 
     203 
     204            $location = ''; 
     205            if (isset($user->info->fireeagle_location)) { 
     206                $location = $user->info->fireeagle_location; 
     207            } 
     208 
     209            echo json_encode(array('location' => $location, 'message' => sprintf(_t('Your present place was updated to "%s".', 'fireeagle'), $location))); 
     210            die(); 
     211        default: 
     212            break; 
     213        } 
     214    } 
     215 
     216    /** 
    148217     * filter: plugin_config 
    149218     * 
     
    167236 
    168237    /** 
     238     * filter: dash_modules 
     239     * 
     240     * @access public 
     241     * @param array $modules 
     242     * @return array 
     243     */ 
     244    public function filter_dash_modules($modules) 
     245    { 
     246        $modules[] = _t('Fire Eagle', 'fireeagle'); 
     247        $this->add_template('dash_fireeagle', dirname(__FILE__) . '/dash_fireeagle.php'); 
     248        return $modules; 
     249    } 
     250 
     251    /** 
     252     * filter: dash_module_fire_eagle 
     253     * 
     254     * @access public 
     255     * @param array $module 
     256     * @param string $module_id 
     257     * @param object $theme 
     258     * @return array 
     259     */ 
     260    public function filter_dash_module_fire_eagle($module, $module_id, $theme) 
     261    { 
     262        $module['title'] = _t('Fire Eagle', 'fireeagle'); 
     263 
     264        $form = new FormUI('dash_fireeagle'); 
     265        $form->append('text', 'location', 'null:unused', _t('Location: ', 'fireeagle')); 
     266        $user = User::identify(); 
     267        if (isset($user->info->fireeagle_location)) { 
     268            $form->location->value = $user->info->fireeagle_location; 
     269        } 
     270        $form->append('submit', 'submit', _t('Update', 'fireeagle')); 
     271        $form->properties['onsubmit'] = 'fireeagle.update(); return false;'; 
     272        $theme->fireeagle_form = $form->get(); 
     273 
     274        $module['content'] = $theme->fetch('dash_fireeagle'); 
     275        return $module; 
     276    } 
     277 
     278    /** 
    169279     * filter: fireeagle_refresh 
    170280     * 
     
    177287        $users = Users::get_all(); 
    178288        foreach ($users as $user) { 
    179             $location = Plugins::filter('fireeagle_user', (int)$user->id); 
     289            $location = $this->update((int)$user->id); 
    180290            if (!$location) continue; 
    181  
    182             $user->info->fireeagle_longitude = $location->longitude; 
    183             $user->info->fireeagle_latitude = $location->latitude; 
    184             $user->info->commit(); 
    185291        } 
    186292        $result = true; 
     
    190296 
    191297    /** 
    192      * filter: fireeagle_user 
    193      * 
    194      * @access public 
     298     * refresh location 
     299     * 
     300     * @access private 
    195301     * @param mixed $who user ID, username, or e-mail address 
    196      * @return mixed 
    197      */ 
    198     public function filter_fireeagle_user($who) 
     302     * @return boolean 
     303     */ 
     304    private function update($who) 
    199305    { 
    200306        $user = User::get($who); 
     
    208314        try { 
    209315            $result = $fireeagle->user(); 
    210             return $result->user->best_guess; 
    211316        } catch (FireEagleException $e) { 
    212317            return false; 
    213318        } 
     319 
     320        if (!isset($result->user->best_guess)) return false; 
     321        $location = $result->user->best_guess; 
     322 
     323        $user->info->fireeagle_longitude = $location->longitude; 
     324        $user->info->fireeagle_latitude = $location->latitude; 
     325        if (isset($location->name)) { 
     326            $user->info->fireeagle_location = $location->name; 
     327        } else { 
     328            $user->info->fireeagle_location = ''; 
     329        } 
     330        $user->info->commit(); 
     331        Plugins::act('fireeagle_after_update', $location); 
     332 
     333        return true; 
    214334    } 
    215335}