<?php

/*
# --------------------------------------------------------------------------
# Change format of comma-separated e-mail lists to <cr><nl>-separated lists
# (used by the new smallTextarea setting type)
# --------------------------------------------------------------------------
*/

# --------------------------------------------------------------------------
# Table fields that could already hold multiple E-mails
# --------------------------------------------------------------------------
$changes = array(
    array('escalationrules', 'transcription_destinataries'),
    array('routingrules', 'emails'),
    array('routingrules', 'copy_destinataries'),
);

foreach ($changes as $change) {
    _migrateTable($change[0], $change[1], $this->db);
}

# --------------------------------------------------------------------------
# Migration of setting "Invoice BCC" from YES/NO to smallTextarea type
# --------------------------------------------------------------------------
$query = "SELECT value FROM setting WHERE name='Invoice BCC'";
$result = $this->db->query($query);
if ($row = $result->fetch()) {
    if ($row['value']) {
        $query2 = "SELECT value FROM setting WHERE name='Billing E-mail'";
        $result = $this->db->query($query2);
        $row2 = $result->fetch();
        $newValue = $row2['value'];
    } else {
        $newValue = '';
    }

    $query = "UPDATE setting SET value=? WHERE name='Invoice BCC'";
    $this->db->query($query, $newValue);
}

# --------------------------------------------------------------------------
# Migration of "setting" table items.
# --------------------------------------------------------------------------
$settingChanges = array(
    'Application Error Notification',
    'E-mail For New High Priority Support Tickets',
    'E-mail For New Signups',
    'plugin_maxmind_MaxMind Warning E-mail',
    'plugin_backup_Deliver to E-mail address',
    'plugin_autosuspend_E-mail Notifications',
    'plugin_order_E-mail Notifications',
    'plugin_batchnotifier_Summary E-mail',
    'plugin_serverstatus_Admin E-mail',
);

foreach ($settingChanges as $settingChange) {
    _migrateSetting($settingChange, $this->db);
}

/*
# --------------------------------------------------------------------------
# HELPER FUNCTIONS
# --------------------------------------------------------------------------
*/

function _migrateTable($table, $field, &$db)
{
    $query = "SELECT id, $field FROM $table";
    $result = $db->query($query);
    while ($row = $result->fetch()) {
        $emails = _transform($row[$field]);
        $query2 = "UPDATE $table SET $field=? WHERE id=?";
        $db->query($query2, $emails, $row['id']);
    }
}

function _migrateSetting($settingName, &$db)
{
    $query = "SELECT id, value FROM setting WHERE name=?";
    $result = $db->query($query, $settingName);
    while ($row = $result->fetch()) {
        $emails = _transform($row['value']);
        $query2 = "UPDATE setting SET value=?, issmalltextarea=1 WHERE id=?";
        $db->query($query2, $emails, $row['id']);
    }
}

function _transform($oldEmailStr)
{
    $emails = array();
    foreach (explode(',', $oldEmailStr) as $email) {
        if (trim($email) != '') {
            $emails[] = trim($email);
        }
    }
    $emails = implode("\r\n", $emails);

    return $emails;
}



?>
