Location: PHPKode > projects > Nuke Modules CVS > modules/PostCalendar/pninit.php
<?php
// $Id: pninit.php,v 1.19.2.2 2002/06/05 20:00:23 epsilon7 Exp $
/********************************************************/
/* PostCalendar                                         */
/* Version 2.6,  see changes.txt for details            */
/* Originally by Rob Sutton (Events Calendar)           */
/* Development continued by                             */
/*                                                      */
/*  The PostCalendar Development Team                   */
/*     Mahmood Al-Yousif - hide@address.com          */
/*     Craig Hamlin - hide@address.com             */
/*     HotBird - hide@address.com                        */
/*     Roger Raymond - hide@address.com                 */
/*                                                      */
/* This program is opensource so you can do whatever    */
/* you want with it. However, I'm happy about any       */
/* clever idea or nifty change you might have or have   */
/* done, send them to                                   */
/* hide@address.com                                 */
/*                                                      */
/* Any new versions can be found at                     */
/* http://www.bahraini.tv first                         */
/********************************************************/

/**
 * initialise the PostCalendar module
 * This function is only ever called once during the lifetime of a particular
 * module instance
 */
function PostCalendar_init()
{
    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
    $events_table   = $pntable['postcalendar_events'];
    $events_column  = &$pntable['postcalendar_events_column'];
    
    $sql = "CREATE TABLE $events_table (
            $events_column[eid] bigint(20) NOT NULL auto_increment,
            $events_column[aid] varchar(30) NOT NULL default '',
            $events_column[title] varchar(150) default NULL,
            $events_column[time] datetime default NULL,
            $events_column[hometext] text default NULL,
            $events_column[comments] int(11) default '0',
            $events_column[counter] mediumint(8) unsigned default NULL,
            $events_column[topic] int(3) NOT NULL default '1',
            $events_column[informant] varchar(20) NOT NULL default '',
            $events_column[eventDate] date NOT NULL default '0000-00-00',
            $events_column[endDate] date NOT NULL default '0000-00-00',
            $events_column[recurrtype] int(1) NOT NULL default '0',
            $events_column[recurrspec] int(3) default NULL,
            $events_column[recurrfreq] int(3) NOT NULL default 0,
            $events_column[startTime] time default NULL,
            $events_column[endTime] time default NULL,
            $events_column[alldayevent] int(1) NOT NULL default '0',
            $events_column[barcolor] char(1) default NULL,
            $events_column[location] varchar(50) default NULL,
            $events_column[conttel] varchar(12) default NULL,
            $events_column[contname] varchar(50) default NULL,
            $events_column[contemail] varchar(50) default NULL,
            $events_column[website] varchar(50) default '',
            $events_column[fee] varchar(50) default NULL,
            $events_column[eventstatus] int(11) NOT NULL default 0,
            PRIMARY KEY  (pc_eid))";
    $dbconn->Execute($sql);
    if ($dbconn->ErrorNo() != 0) {
        pnSessionSetVar('errormsg', _CREATETABLEFAILED);
        return false;
    }
    
    // PostCalendar Default Settings
    pnModSetVar('PostCalendar', 'time24hours',              '0');
    pnModSetVar('PostCalendar', 'eventsopeninnewwindow',    '0');
    pnModSetVar('PostCalendar', 'useinternationaldates',    '0');
    pnModSetVar('PostCalendar', 'firstdayofweek',           '0');
    pnModSetVar('PostCalendar', 'doappointments',           '1');
    pnModSetVar('PostCalendar', 'dayhighlightcolor',        'red');
    pnModSetVar('PostCalendar', 'monthtableborder',         '0');
    pnModSetVar('PostCalendar', 'monthtablecellspacing',    '1');
    pnModSetVar('PostCalendar', 'monthtablecellpadding',    '1');
    pnModSetVar('PostCalendar', 'yeartableborder',          '0');
    pnModSetVar('PostCalendar', 'yeartablecellspacing',     '1');
    pnModSetVar('PostCalendar', 'yeartablecellpadding',     '1');
    pnModSetVar('PostCalendar', 'category1',                'Birthdays');
    pnModSetVar('PostCalendar', 'category2',                'Business');
    pnModSetVar('PostCalendar', 'category3',                'Community');
    pnModSetVar('PostCalendar', 'category4',                'Events');
    pnModSetVar('PostCalendar', 'category5',                'Sports');
    pnModSetVar('PostCalendar', 'displayballs',             '1');
    pnModSetVar('PostCalendar', 'showtimeonevents',         '1');
    pnModSetVar('PostCalendar', 'usepopups',                '1');
    pnModSetVar('PostCalendar', 'showeventsinyear',         '0');
    pnModSetVar('PostCalendar', 'numcolsinyearview',        '4');
    pnModSetVar('PostCalendar', 'allowdirectsubmit',        '0');
    pnModSetVar('PostCalendar', 'listhowmanyevents',        '15');
    
    return true;
}

/**
 * upgrade the PostCalendar module from an old version
 * This function can be called multiple times
 */
function PostCalendar_upgrade($oldversion)
{
    // Upgrade dependent on old version number
    // of course, none of this will really work for the old style modules
    // so we'll probably be forced to write an upgrade script for those using older
    // versions of the code.
    // 
    // The following section should provide as a decent reference for the upgrade paths
    // please make any necessary changes.
    
    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
    $events_table           = $pntable['postcalendar_events'];
    $events_column          = &$pntable['postcalendar_events_column'];
    $events_queue_table     = $pntable['postcalendar_events_queue'];
    $events_queue_column    = &$pntable['postcalendar_events_queue_column'];
    
    switch($oldversion) {
        case '2.6':
            // upgrade from verion 2.6
            
            $sql = "SELECT $events_column[recurrfreq] FROM $events_table";
            $result = $dbconn->execute($sql);
            if ($dbconn->ErrorNo() == 0) {
                $sql = "ALTER TABLE $events_table
                        ADD $events_column[recurrfreq] int(3) NOT NULL default 0";
                $dbconn->Execute($sql);
                if ($dbconn->ErrorNo() != 0) {
                    pnSessionSetVar('errormsg', 'Events Table Update Failed');
                    return false;
                }
            }
            
            $sql = "SELECT $events_queue_column[recurrfreq] FROM $events_queue_table";
            $result = $dbconn->execute($sql);
            if ($dbconn->ErrorNo() == 0) {
                $sql = "ALTER TABLE $events_queue_table
                        ADD $events_queue_column[recurrfreq] int(3) NOT NULL default 0";
                $dbconn->Execute($sql);
                if ($dbconn->ErrorNo() != 0) {
                    pnSessionSetVar('errormsg', 'Events Queue Table Update Failed');
                    return false;
                }
            }
            /*
            // bug in PostNuke .71 does not set new module version
            // so i will do it :)
            $modid = pnModGetIDFromName('PostCalendar');
            $modules_table = $pntable['modules'];
            $modules_column = &$pntable['modules_column'];
            $sql = "UPDATE $modules_table SET $modules_column[version] = '2.6.1' 
                    WHERE $modules_column[id] = $modid";
            $dbconn->Execute($sql);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Modules Table Update Failed');
                return false;
            }
            */
            return PostCalendar_upgrade('2.6.1');
        
        case '2.6.1':
            // upgrade from version 2.6.1
            
            $sqlADD     = "ALTER TABLE $events_table ADD $events_column[eventstatus] int(3) NOT NULL default 0";
            $sqlMODIFY  = "ALTER TABLE $events_table MODIFY $events_column[hometext] TEXT DEFAULT NULL";
            $sqlUPDATE  = "UPDATE $events_table SET $events_column[eventstatus] = 1 WHERE 1";
                    
            $dbconn->Execute($sqlADD);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Events Table ADD Failed');
                return false;
            }
            
            $dbconn->Execute($sqlMODIFY);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Events Table MODIFY Failed');
                return false;
            }
            
            $dbconn->Execute($sqlUPDATE);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Events Table UPDATE Failed');
                return false;
            }
            
            
            // get the queued events and move them to the main table
            $sqlSELECT = "SELECT $events_queue_column[uname],
                           $events_queue_column[title],
                           $events_queue_column[story],
                           $events_queue_column[timestamp],
                           $events_queue_column[topic],
                           $events_queue_column[eventDate],
                           $events_queue_column[endDate],
                           $events_queue_column[recurrtype],
                           $events_queue_column[recurrspec],
                           $events_queue_column[recurrfreq],
                           $events_queue_column[startTime],
                           $events_queue_column[endTime],
                           $events_queue_column[alldayevent],
                           $events_queue_column[barcolor],
                           $events_queue_column[location],
                           $events_queue_column[conttel],
                           $events_queue_column[contname],
                           $events_queue_column[contemail],
                           $events_queue_column[website],
                           $events_queue_column[fee]

                    FROM $events_queue_table
                     
                    ORDER BY $events_queue_column[timestamp]";

            $result = $dbconn->Execute($sqlSELECT);
            $insertsql = '';  
            if ($dbconn->ErrorNo() == 0) {
                for(; !$result->EOF; $result->MoveNext()) {
                
                    // grab em
                    list($uname,$title,$story,$timestamp,$topic,$eventDate,
                         $endDate,$recurrtype,$recurrspec,$recurrfreq,$startTime,
                         $endTime,$alldayevent,$barcolor,$location,$conttel,$contname,
                         $contemail,$website,$fee) = $result->fields;
                    // prep em
                    list($uname,$title,$story,$timestamp,$topic,$eventDate,
                         $endDate,$recurrtype,$recurrspec,$recurrfreq,$startTime,
                         $endTime,$alldayevent,$barcolor,$location,$conttel,$contname,
                         $contemail,$website,$fee) = pnVarPrepForStore($uname,$title,$story,$timestamp,$topic,$eventDate,
                                                                       $endDate,$recurrtype,$recurrspec,$recurrfreq,$startTime,
                                                                       $endTime,$alldayevent,$barcolor,$location,$conttel,$contname,
                                                                       $contemail,$website,$fee);
               
        
        
                    $insertsql = "INSERT INTO $events_table (
                                               $events_column[title],
                                               $events_column[time],
                                    $events_column[hometext],
                                    $events_column[topic],
                                    $events_column[informant],
                                    $events_column[eventDate],
                                    $events_column[endDate],
                                    $events_column[recurrtype],
                                    $events_column[recurrspec],
                                    $events_column[recurrfreq],
                                    $events_column[startTime],
                                    $events_column[endTime],
                                    $events_column[alldayevent],
                                    $events_column[barcolor],
                                    $events_column[location],
                                    $events_column[conttel],
                                    $events_column[contname],
                                    $events_column[contemail],
                                    $events_column[website],
                                    $events_column[fee],
                                    $events_column[eventstatus])
                                VALUES ('$title', 
                                        '$timestamp',
                                        '$story',
                                         $topic,
                                        '$uname',
                                        '$eventDate',
                                        '$endDate',
                                         $recurrtype,
                                         '$recurrspec',
                                         $recurrfreq,
                                        '$startTime',
                                        '$endTime',
                                         $alldayevent,
                                        '$barcolor',
                                        '$location',
                                        '$conttel',
                                        '$contname',
                                        '$contemail',
                                        '$website',
                                        '$fee',
                                        0);";
                                        
                    $dbconn->Execute($insertsql);
                    if ($dbconn->ErrorNo() != 0) {
                        pnSessionSetVar('errormsg', 'Event Queue Update Failed');
                        return false;
                    }
                }
            }
            
            // get rid of the old table queue
            $sqlDROP = "DROP TABLE IF EXISTS $events_queue_table";
            $dbconn->Execute($sqlDROP);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Events Queue Table DROP Failed');
                return false;
            }
            
            /*
            // bug in PostNuke .71 does not set new module version
            // so i will do it :)
            $modid = pnModGetIDFromName('PostCalendar');
            $modules_table = $pntable['modules'];
            $modules_column = &$pntable['modules_column'];
            $sql = "UPDATE $modules_table SET $modules_column[version] = '3.0' 
                    WHERE $modules_column[id] = $modid";
            $dbconn->Execute($sql);
            if ($dbconn->ErrorNo() != 0) {
                pnSessionSetVar('errormsg', 'Modules Table Update Failed');
                return false;
            }
            */
            break;
        }

    // Update successful
    return true;
}

/**
 * delete the PostCalendar module
 * This function is only ever called once during the lifetime of a particular
 * module instance
 */
function PostCalendar_delete()
{
    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
    $events_table           = $pntable['postcalendar_events'];
    $events_column          = &$pntable['postcalendar_events_column'];
    $events_queue_table     = $pntable['postcalendar_events_queue'];
    $events_queue_column    = &$pntable['postcalendar_events_queue_column'];
    $module_vars_table      = $pntable['module_vars'];
    $module_vars_column     = &$pntable['module_vars_column'];
    $blocks_table           = $pntable['blocks'];
    $blocks_column          = &$pntable['blocks_column'];
    
    // get the module id
    $modid = pnModGetIDFromName('PostCalendar');
    
    $sql = "DROP TABLE $events_table";
    $dbconn->Execute($sql);
    if ($dbconn->ErrorNo() != 0) {
        pnSessionSetVar('errormsg', 'Events Table Drop Failed');
        return false;
    }
    
    // remove all the PostCalendar variables from the DB
    $sql = "DELETE FROM $module_vars_table WHERE $module_vars_column[modname] LIKE 'PostCalendar%'";
    $dbconn->Execute($sql);
    if ($dbconn->ErrorNo() != 0) {
        pnSessionSetVar('errormsg', 'PostCalendar Module Variable Clean-Up Failed');
        return false;
    }
    
    // remove any blocks associated with PostCalendar
    $sql = "DELETE FROM $blocks_table WHERE $blocks_column[mid] = $modid";
    $dbconn->Execute($sql);
    if ($dbconn->ErrorNo() != 0) {
        pnSessionSetVar('errormsg', 'PostCalendar Blocks Clean-Up Failed');
        return false;
    }
    
    // Deletion successful
    return true;
}

?>
Return current item: Nuke Modules CVS