<?php
/*
 * UPDATE SCRIPT FOR kOOL RELEASE R38
 *
 * ----------
 * IMPORTANT: Run this script before updating your kOOL database with changesR38.sql as this
 * ---------- SQL update deletes the newsletter tables.
 *
 *
 * Description: The newsletter module disappears with R38. This script converts all newsletters
 * in your kOOL into a group. All newsletter recipients will be assigned to these newly created groups.
 *
 * Instructions: Set the path to your kOOL installation below and run the script on your kOOL server.
 *
 */


/* Settings */

//Set path to your kOOL installation (absolute or relative)
$kOOL_path '/var/www/kOOL_demo/';

/* End of settings */




// Include ko.inc (and database settings from ko-config.php)
$ko_path $kOOL_path;
require_once(
$kOOL_path.'inc/ko.inc');

//Get all current newsletters (only email, sms and post)
$newsletters db_select_data('ko_newsletter'"WHERE `type` IN ('3', '4', '5') AND `recipients` != ''"'*');
$counter 0;
$done = array();
foreach(
$newsletters as $nlid => $nl) {
    
//Create new group
    
$group = array('name' => zerofill($counter3).'_NEWSLETTER_'.format_userinput($nl['name'], 'alphanum+'), 'crdate' => date('Y-m-d H:i:s'));
    
$gid zerofill(db_insert_data('ko_groups'$group), 6);
    
    
//Assign all recipients to the newly created group
    
foreach(explode(','$nl['recipients']) as $pid) {
        if(!
$pid) continue;
        
$person db_select_data('ko_leute'"WHERE `id` = '$pid'"'*'''''TRUE);
        if(!
$person['id']) continue;
        
$new_groups $person['groups'] != '' $person['groups'].',g'.$gid 'g'.$gid;
        
db_update_data('ko_leute'"WHERE `id` = '$pid'", array('groups' => $new_groups));
    }
    
$done[] = array('nl' => $nl'group' => $group);

    
$counter++;
}
//foreach(newsletters as nl)


//Output status
print "\nConversion completed:\n\n";
foreach(
$done as $d) {
    print 
'Newsletter "'.$d['nl']['name'].'" --> Group "'.$d['group']['name'].'"'."\n";
}
print 
"\n";



function 
zerofill($s$l) {
    while(
strlen($s) < $l$s '0'.$s;
    return 
$s;
}

?>