<?php

/*
* Move the client's comments to a staff note
*/

$query = "SELECT id FROM users WHERE groupid=2";
$result = mysql_query($query) or die("ERROR in query: $query");
list($superadminId) = mysql_fetch_row($result);

$query = "SELECT id, comments FROM users WHERE groupid=1 AND comments IS NOT NULL AND comments != ''";
$result = mysql_query($query) or die("ERROR in query: $query");
$date = date('Y-m-d H:i:s');
while(list($userId, $comment) = mysql_fetch_row($result)) {
    $note = mysql_real_escape_string($comment);
    $note = "Note migrated from the old comments field:\n\n" . $note;
    $query2 = "INSERT INTO clients_notes (date, target_id, is_target_group, admin_id, note, visible_client) VALUES ('$date', $userId, 0, $superadminId, '$note', 0)";
    $result2 = mysql_query($query2) or die("ERROR in query: $query2");
}

$query3 = 'ALTER TABLE `users` DROP `comments`';
$result3 = mysql_query($query3) or die("ERROR in query: $query3");

$query = "SELECT subject_id, is_group, target_id FROM `permissions` WHERE `permission` = 'clients_create_customer_packages'";
$result = mysql_query($query) or die("ERROR in query: $query");
while(list($subjectId,$isGroup,$targetId) = mysql_fetch_row($result)){
    $query2 ="INSERT INTO `permissions` (subject_id, is_group, permission, target_id) VALUES($subjectId,$isGroup,'clients_activate',$targetId)";
    $result2 = mysql_query($query2) or die("ERROR in query: $query2");
}


?>
