Code: Select all
// fetch unserialize notification_data, modify it, reserialize it and update it into the db. supports multiple notifications
public function modify_notification_username($generic_id, $username, $toggle_anonymous, $user_id = NULL)
{
// fetch and unserialize notification_data for the item_id
$notification_query = 'SELECT notification_data, user_id
FROM ' . NOTIFICATIONS_TABLE . '
WHERE item_id = ' . $generic_id . '
ORDER BY user_id, notification_time ASC';
$result = $notification_data_ary = $user_id_ary = array();
$result = $this->db->sql_query($notification_query);
while($row = $this->db->sql_fetchrow($result))
{
$notification_data_ary[] = unserialize($row['notification_data']);
$user_id_ary[] = $row['user_id'];
}
$num_rows = $result->num_rows;
$this->db->sql_freeresult($result);
unset($result);
// if there's anything more to do
if($num_rows > 0)
{
// change poster_id and post_username according to toggle_anonymous, then serialize the row
foreach($notification_data_ary as &$row)
{
$row['poster_id'] = $toggle_anonymous ? 1 : $user_id;
$row['post_username'] = $username;
$row = serialize($row);
}
// unset $row as it was referenced
unset($row);
// build each notification data case in order
$individual_update_data = '';
foreach($user_id_ary as $index => $value)
$individual_update_data .= 'WHEN (item_id = ' . $generic_id . ' AND user_id = ' . $value . ') THEN \'' . $notification_data_ary[$index] . '\' ELSE \'\' ';
// serialize each notification_data for each user_id (usually just 1 user)
$update_notifications_table = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET notification_data = CASE ' .
$individual_update_data . '
END';
$result = array();
$result = $this->db->sql_query($update_notifications_table);
unset($result);
exit();
}
}