<?php

/*
# --------------------------------------------------------
# Preserve old registrar selection
# --------------------------------------------------------
*/

$query = "SELECT name FROM setting WHERE name LIKE '%to check and register domains' AND value='1'";
$result = mysql_query($query) or die("ERROR in query: $query");
if (mysql_num_rows($result) > 0) {
    list($pluginName) = mysql_fetch_array($result);
    $splitName = explode('_', $pluginName);
    $query = "UPDATE `tld` SET `plugin` = '".$splitName[1]."'";
    $result = mysql_query($query) or die("ERROR in query: $query");
}

/*
# --------------------------------------------------------
# Change Trouble -> Support
# --------------------------------------------------------
*/

$query1 = "SELECT id, value FROM setting WHERE value LIKE '%trouble ticket%'";
$result1 = mysql_query($query1) or die("ERROR in query: $query1");
while(list($settingId, $settingValue) = mysql_fetch_array($result1)){
    $newSettingValue = str_replace(array('trouble ticket', 'Trouble Ticket'), 'Support Ticket', $settingValue);
    $query = "UPDATE `setting` SET `value` = '".$newSettingValue."' WHERE `id` = ".$settingId;
    $result = mysql_query($query) or die("ERROR in query: $query");
}


$query2 = "SELECT id, value_alternate FROM setting WHERE value_alternate LIKE '%trouble ticket%'";
$result2 = mysql_query($query2) or die("ERROR in query: $query2");
while(list($settingIdAlternate, $settingValueAlternate) = mysql_fetch_array($result2)){
    $newSettingValueAlternate = str_replace(array('trouble ticket', 'Trouble Ticket'), 'Support Ticket', $settingValueAlternate);
    $query = "UPDATE `setting` SET `value_alternate` = '".$newSettingValueAlternate."' WHERE `id` =".$settingIdAlternate;
    $result = mysql_query($query) or die("ERROR in query: $query");
}

/*
# --------------------------------------------------------
# Set Renewal Price for domain for existents tlds
# --------------------------------------------------------
*/

$query3 = "SELECT id, price FROM tld";
$result3 = mysql_query($query3) or die("ERROR in query: $query3");
while(list($tldId, $tldPrice) = mysql_fetch_array($result3)){
    $query = "UPDATE `tld` SET `renewal` = $tldPrice WHERE id =$tldId ";
    $result = mysql_query($query) or die("ERROR in query: $query");
}

/*
# --------------------------------------------------------
# Changing report name in the user's custom fields
# --------------------------------------------------------
*/
$query = "SELECT id FROM customuserfields WHERE name='QUICK_REPORTS'";
$result = mysql_query($query) or die("ERROR in query: $query");
if ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $query = "SELECT userid, value FROM user_customuserfields WHERE customid=$id";
    $result = mysql_query($query) or die("ERROR in query: $query");
    while(list($userId, $value) = mysql_fetch_array($result)) {
        $reports = unserialize($value);
        if ($reports) {
            $fixName = false;
            for ($i = 0; $i < count($reports); $i++) {
                if ($reports[$i] == 'Trouble_Tickets_Opened-Support.php') {
                    $reports[$i] = 'Support_Tickets_Opened-Support.php';
                    $fixName = true;
                    break;
                }
            }

            if ($fixName) {
                $reports = mysql_real_escape_string(serialize($reports));
                $query2 = "UPDATE user_customuserfields SET value='$reports' WHERE userid=$userId AND customid=$id";
                mysql_query($query2) or die("ERROR in query: $query2");
            }
        }
    }
}

?>
