<?php
class controller_update extends controller
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        $data['package'] = package::get_list();
        $data['current'] = package::get_version();
        $this->render('index.html', $data);
    }
    
    protected function execute_install()
    {
        if (!$this->request->is_post()) {
            $this->response->set(json_encode(array('error' => 'bad request')));
            return;
        }
        
        try {
            $log = package::install($this->request->post('version'));
        } catch (Exception $e) {
            $this->response->set(json_encode(array('error' => $e->getMessage())));
            return;
        }
        
        $this->response->set(json_encode(array('log' => $log)));
    }
}
