<?php

require_once "aps/2/runtime.php";

/**
 * Main class of my web application
 * @type("http://my-company.com/my/webapp/1.0")
 * @implements("http://www.aps-standard.org/core/resource/1")
 */
class MyWebApp extends ResourceBase
{
    /** 
     * @type(string) 
     * @title("Administrator Name")
     */
    public $adminName;
    
    /** 
     * @title("Administrator password")
     * @encrypted
     * @type(string) 
     */
    public $adminPassword;
    

    /** 
     * @type(string) 
     * @format(email)
     * @title("Administrator E-Mail")
     */
    public $adminEmail;


    /** 
     * @link("http://parallels.com/aps/types/pa/database/mysql/1.0")
     */ 
    public $db;
    
    /** 
     * @link("http://aps-standard.org/types/core/web-environment/1.0")
     * @required
     * @requirement(engines == 'php' && php.version > '5.0')
     */
    public $environment;

    /** 
     * This method will be called on provision of application
     */
    public function provision()
    {
        $dbh = new PDO($this->db->dsn, $this->db->login, $this->db->password);
        $stmt = $dbh->prepare("INSERT INTO configuration (login, password, email) VALUES(?, ?, ?)");
        $stmt->execute(array($this->adminName, $this->adminPassword, $this->adminEmail));
    }

    /** 
     * This method will be called on reconfiguration of application
     */
    public function configure($new)
    {
        $this->adminName; 
        $dbh = new PDO($this->db->dsn, $this->db->login, $this->db->password);
        $stmt = $dbh->prepare("UPDATE configuration SET login = ?, password = ?, email = ?");
        $stmt->execute(array($new->adminName, $new->adminPassword, $new->adminEmail));
    }

    /**
     * Additional method called from appliction UI or API 
     * @verb(POST)
     * @path("/blog")
     * @param(string,query)
     * @param(string,query)
     */
    public function blog($title, $entry = "new blog entry")
    {
        echo "Blog Entry [$title]\n$entry\n";
    }
    
}


?>
