<?php

/* $Id: class.php,v 1.4 2005/09/29 15:10:35 tuupola Exp $ */

/*
+-----------------------------------------------------------------------+
| Copyright (c) 2005 Mika Tuupola                                       |
| All rights reserved.                                                  |
|                                                                       |
| Redistribution and use in source and binary forms, with or without    |
| modification, are permitted provided that the following conditions    |
| are met:                                                              |
|                                                                       |
| o Redistributions of source code must retain the above copyright      |
|   notice, this list of conditions and the following disclaimer.       |
| o Redistributions in binary form must reproduce the above copyright   |
|   notice, this list of conditions and the following disclaimer in the |
|   documentation and/or other materials provided with the distribution.|
| o The names of the authors may not be used to endorse or promote      |
|   products derived from this software without specific prior written  |
|   permission.                                                         |
|                                                                       |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
|                                                                       |
+-----------------------------------------------------------------------+
| Author: Mika Tuupola <tuupola@appelsiini.net>                         |
| Install: <path_to_mint>/pepper/tuupola/currentactivity/               |
| Version: 0.9.0                                                        |
+-----------------------------------------------------------------------+

*/


$install_plugin "Tuupola_CurrentActivity";

class 
Tuupola_CurrentActivity {

    var 
$Mint;
    var 
$info;
    var 
$panes;    
    var 
$pluginid;

    function 
Tuupola_CurrentActivity($plugin_id) {
        global 
$Mint;

        
$this->Mint      =& $Mint;
        
$this->plugin_id =  $plugin_id;
        
        
/* Pepper info */
        
$this->info['developer']     = "Mika Tuupola";
        
$this->info['plugin']        = "Current Activity";
        
$this->info['description']   = "Displays what pages are currently 
                                        being browsed."
;
        
$this->info['developer_url'] = "http://www.appelsiini.net/~tuupola/";    
        
$this->info['documentation'] = "http://www.appelsiini.net/~tuupola/";

        
$this->info['src']           = 'tuupola/currentactivity/';
        
$this->info['class']         = 'Tuupola_CurrentActivity';

        
/* Which panes this plugin creates */
        
$this->panes['Current Activity']   = array();
    }

    function 
install() {

        
/* $prefs['tuupola_resolve'] = 1; */
        
$prefs['tuupola_timespan'] = 15;
        
$this->Mint->savePluginPreferences($this->plugin_id$prefs);
        
$this->Mint->registerPlugIn($this->info['src'], 
                                     
$this->info['class'],
                                     
$this->panes);
    }

    function 
uninstall() { 
    }

    function 
onRecord() {
        return array();
    }

    function 
onJavaScript() { 
    }

    function 
onDisplay($pane$tab$column=''$sort='') {

        
$html '';

        
/* unnessecary switches left here, to make it easier to add */
        /* new tabs later                                           */
        
switch($pane) {
            case 
'Current Activity':
                switch(
$tab) {
                    default:
                        
$html $this->getHTML_CurrentActivity();
                        break;
                }
            break;
        }
        return 
$html;
    }

    function 
onWidget() { 
    }

    function 
onDisplayPreferences() {
        
$prefs $this->Mint->getPluginPreferences($this->plugin_id);
        
$html['Global']  =  '
<table class="snug" border=1>
  <tr>
    <th scope="row">Timespan</th>
    <td>
      <table class="snug">
        <tr>
          <td><span class="inline" style="margin-left: 0;"><input type="text" id="tuupola_timespan" name="tuupola_timespan" maxlength="5" size="5" value="' 
$prefs['tuupola_timespan'] . '" /></span></td>
          <td>minutes.</td>
        </tr>
      </table>
    </td>
  </tr>
<!--
  <tr>
    <td></td>
    <td><label><input type="checkbox" id="tuupola_resolve" name="tuupola_resolve" value="1" checked="checked" /> Resolve hostnames (not implemented)</label></td>
  </tr>
-->
</table>
'
;
        return 
$html;
    }

    function 
onSavePreferences() {
        
$prefs['tuupola_timespan'] = (isset($_POST['tuupola_timespan']) ? 
                                            
$_POST['tuupola_timespan'] : 
                                            
15 );
        
$this->Mint->savePluginPreferences($this->plugin_id,$prefs);
    }

    function 
onCustom() {
        if (isset(
$_POST['action']) && $_POST['action']=='tuupola_getpages' &&
            isset(
$_POST['ip'])) {
            
$ip $this->Mint->escapeSQL($_POST['ip']); 
            print 
$this->getHTML_CurrentPages($ip);
        }
    }

    function 
getHTML_CurrentActivity() {

        
$prefs $this->Mint->getPluginPreferences($this->plugin_id);
        
        
$tablename $this->Mint->db['tblprefix'] . 'visit';
        
$timestamp time() - $prefs['tuupola_timespan'] * 60;

        
$query sprintf("SELECT DISTINCT(ip) AS ip
                          FROM %s
                          WHERE dt>%d
                          ORDER BY dt DESC"
,
                          
$tablename$timestamp);

        
/* if the data has folders this has to be boolean true */
        /* additionally table class has to be 'folder' */
        
$table['hasFolders'] = true;
        
$table['table'] = array('id'=>'','class'=>'folder');
        
$table['thead'] = array(
            
/* display name, CSS class(es) for each column */
            
array('value'=>'IP','class'=>'stacked-rows'),
            array(
'value'=>'Last access','class'=>''),
        );


        if (
$result mysql_query($query)) {
            while (
$row mysql_fetch_array($resultMYSQL_NUM)) {
//               $row[1] = $this->Mint->formatDateTimeRelative($row[1]);  
               
$row[] = ' ';
               
               
/* these get passed to onCustom() method */
               
$row['folderargs']['action'] = 'tuupola_getpages';
               
$row['folderargs']['ip']     = $row[0];
               
$table['tbody'][] = $row;
            }
        }

        
$html $this->Mint->generateTable($table);
        return 
$html;
    }


    function 
getHTML_CurrentPages($ip) {

        
$prefs $this->Mint->getPluginPreferences($this->plugin_id);
        
        
$tablename $this->Mint->db['tblprefix'] . 'visit';
        
$timestamp time() - $prefs['tuupola_timespan'] * 60;

        
$query sprintf("SELECT resource_title, dt
                          FROM %s
                          WHERE dt>%d AND ip='%s'
                          ORDER BY dt DESC"
,
                          
$tablename$timestamp$ip);

        if (
$result mysql_query($query)) {
            while (
$row mysql_fetch_array($resultMYSQL_NUM)) {
               
$row[1] = $this->Mint->formatDateTimeRelative($row[1]); 
               
$table['tbody'][] = $row;
            }
        }

/* debug stuff. 
        error_reporting(E_ALL);
ob_start();
print_r($table);
$msg = ob_get_clean();
        error_log($msg);
        error_reporting(0);
*/

        
$html $this->Mint->generateTableRows($table);
        return 
$html;
    }



    function 
getDefaultPluginId() {
        
$plugin $this->Mint->getCfgValue('plugins');
        foreach (
$plugin as $key=>$val) {
            if (
$val['class']=='SI_DefaultPepper') {
                
$retval $key;
                break;
            }
        }
        return 
$retval;
    }

}

?>