<?PHP
	// definitions for Word class: a word for which characteristics will be entered

class Word {
	var $is_on_db;
	var $status;
	var $id;
	var $word;
	var $language_id;
	var $date_added;
	var $date_updated;
	var $is_deleted;

	// constructor for Word (default language to English)
	function __construct ($word, $language_id = 1) {
		$this->is_on_db = FALSE;
		$this->status = "new";
		$this->id = "";
		$this->word = $word;
		$this->language_id = $language_id;
		$this->date_added = "";
		$this->date_updated = "";
		$this->is_deleted = FALSE;
	}

	// use database row to set object's values
	function setValues ($row) {
		$this->id = $row['id'];
		$this->word = $row['word'];
		$this->language_id = $row['language_id'];
		$this->date_added = $row['date_added'];
		$this->date_updated = $row['date_updated'];
		$this->is_deleted = $row['is_deleted'];
		$this->is_on_db = true;
		$this->status = "read from db";
	}

	// create json string (allows this object to be created in JavaScript)
	function toJson () {
		$json = '{"type":"Word"';
		$json .= ',"id":"' . $this->id . '"';
		$json .= ',"word":"' . $this->word . '"';
		$json .= ',"language_id":"' . $this->language_id . '"';
		$json .= ',"date_added":"' . $this->date_added . '"';
		$json .= ',"date_updated":"' . $this->date_updated . '"';
		$json .= ',"is_deleted":"' . $this->is_deleted . '"';
		$json .= ',"is_on_db":"' . $this->is_on_db . '"';
		$json .= ',"status":"' . $this->status . '"';
		$json .= '}';
		return $json;
	}

	// uses id to retrieve from db
	function getById ($db_link) {

		$db_sql = "SELECT * FROM words WHERE id = " . $this->id;
		$result = $db_link->connection->query($db_sql);
		if ( $result ) {
			$row = $result->fetch_array();
			if ($row) {
				$this->setValues($row);
				
			} else {
				$this->status = "Row not found on words table with key: id=" . $this->id;
			}
		} else {
			$this->status = "MySql error: " . $db_link->connection->error;
		}
	}

	// uses id to retrieve from db
	function getByWord ($db_link) {

		$db_sql = "SELECT * FROM words WHERE word = '" . $this->word . "' and language_id = " . $this->language_id;
		$result = $db_link->connection->query($db_sql);
		if ( $result ) {
			$row = $result->fetch_array();
			if ($row) {
				$this->setValues($row);
				
			} else {
				$this->status = "Row not found on words table with key: word=" . $this->word . ", language_id=" . $this->language_id;
			}
		} else {
			$this->status = "MySql error: " . $db_link->connection->error;
		}
	}

	// store object on database, optionally refresh object from database (caters for database generated values)
	function store ($db_link, $refresh = true) {

		// check minimum info supplied
		if (empty($this->word) || empty($this->language_id)) {
			$this->status = "No key information supplied: word value and language number required";
			return; // skip out
		}
		$db_sql = "SELECT * FROM words WHERE word = '" . $this->word . "' and language_id = " . $this->language_id;
        if($result = $db_link->connection->query($db_sql)){
            if($result->num_rows > 0){
     			$row = $result->fetch_array();
				$this->is_on_db = true;
				$this->id = $row['id'];
                // Free result set
                $result->free();
        		// timestamp
    			$setClause = "date_updated = NOW()";
  			
     			$db_sql = "UPDATE words SET " . $setClause . " WHERE id=" . $this->id;
                if($result = $db_link->connection->query($db_sql)){
    				$this->status = "Updated OK!";
    				$this->getById($db_link); //refresh values not updated from interface
    			} else {
    				$this->status = "Update error: " . $db_sql . $db_link->connection->error;
    			}

            } else{

    			$columns = "word";
    			$values = "'" . $this->word ."'";

    			$columns .= ", language_id";
    			$values .= ", " . $this->language_id;
    			
    			$db_sql = "INSERT INTO words (" . $columns . ") VALUES(" . $values . ")";
    
                if($result = $db_link->connection->query($db_sql)){
    				$this->id = $db_link->connection->insert_id;
    				// reread from db to get all values (some can be populated on INSERT)
    				$this->getById($db_link);
    				$this->status = "New word added!";
    			} else {
    				$this->status = "Insert error: " . $db_sql . ", Db error: " . $db_link->connection->error;
			    }
            }
        } else{
            $this->status = "Db error: " . $db_link->connection->error;
            echo "ERROR: Unable to update datebase. " . $db_link->connection->error;
        }
	}
}

?>
