Need some work on the PHP side but I think this works