<?php

namespace CGClientTickets\SubData;

/**
 * A simple data class for managing items for a ticket list.
 * This class is used for the admin tickets tab, and for the
 * frontend users ticket list.
 */
class TicketListItem
{
    private $_data = array('id'=>null,'owner'=>null,'alert_group'=>null,'status'=>null,'priority'=>null,'e_key1'=>null,'e_key2'=>null,'e_key3'=>null,'nmsgs'=>null,'subject'=>null,
                           'n_unread'=>null,'created'=>null,'modified_date'=>null);

    public function __get($key)
    {
        if( array_key_exists($key,$this->_data) ) return $this->_data[$key];
        throw new \LogicException($key.' is an invalid member of '.__CLASS__);
    }

    public function __set($key,$value)
    {
        throw new \LogicException('Modifying a TicketListItem in this way is unsupported');
    }

    public function __isset($key)
    {
        return isset($this->_data[$key]);
    }

    public function __unset($key)
    {
        throw new \LogicException('Modifying a TicketListItem in this way is unsupported');
    }

    public static function create_from_data($data)
    {
        $obj = new self();
        foreach( $data as $key => $val ) {
            if( !array_key_exists($key,$obj->_data) ) throw new \RuntimeException($key.' is an invalid member of '.__CLASS__);
            // @todo: validate values
            $obj->_data[$key] = $val;
        }
        return $obj;
    }

    public function get_dataref()
    {
        if( !$this->e_key1 ) return;
        $dr = new \CGExtensions\LinkDefinition\DataRef;
        $dr->key1 = $this->e_key1;
        $dr->key2 = $this->e_key2;
        $dr->key3 = $this->e_key3;
        return $dr;
    }

    public function get_linkdefinition()
    {
        $dr = $this->get_dataref();
        if( !is_object($dr) ) return;

        try {
            $factory = new \CGExtensions\LinkDefinition\LinkDefinitionGeneratorFactory();
            $generator = $factory->get_generator($dr);
            $ld = $generator->get_linkdefinition();
            return $ld;
        }
        catch( \Exception $e ) {
            audit('','CGClientTickets','Could not retrieve link definition for dataref '.$dr);
        }
    }

} // end of class
?>
