<?php

/**
 * This file defines the UnreadTicketListResultset class.
 *
 * @package CGClientTickets
 * @category Application
 * @author  calguy1000 <calguy1000@cmsmadesimple.org>
 * @copyright Copyright 2010 by Robert Campbell
 */

namespace CGClientTickets\Repository;

/**
 * This is a simple class that geerates a resultset for unread tickets.
 * it returns an array of priority, and tickets with that priority that have unviewed messages.
 *
 * @property int $owner The integer FEU uid of the ticket owner.  If specified only unread tickets for this owner will be returned.
 */
class UnreadTicketResultset extends \CGExtensions\query\resultset
{
    /**
     * Constructor
     *
     * @param array $parms Array of initial properties for this object.
     */
    public function __construct($parms = array()) {
        // @todo: check the params
        $this->_data = $parms;
    }

    /**
     * @ignore
     */
    protected function _query()
    {
        if( $this->_rs ) return;

        $db = \cge_utils::get_db();
        $parms = array();
        if( isset($this->_filter['owner']) ) {
            $sql = 'SELECT SQL_CALC_FOUND_ROWS T.priority,COUNT(M.id) AS cnt FROM '.CGST_TICKETS_TABLE.' T
                    LEFT JOIN '.CGST_MSGS_TABLE.' M ON T.id = M.ticket_id AND T.owner = M.dest AND T.owner = ? AND viewed IS NULL GROUP BY T.priority';
            $parms[] = (int) $this->_filter['owner'];
        }
        else {
            $sql = 'SELECT T.priority,COUNT(M.id) AS cnt FROM '.CGST_TICKETS_TABLE.' T
                    LEFT JOIN '.CGST_MSGS_TABLE.' M ON T.id = M.ticket_id AND T.alert_group = M.dest AND admin_viewed IS NULL GROUP BY T.priority';
        }
        $this->_rs = $db->Execute($sql,$parms);
        $this->_totalmatching = $db->GetOne('SELECT FOUND_ROWS()');
    }

    /**
     * Get the data for the current record in the resultset.
     *
     * @return array
     */
    public function &get_object()
    {
        $fields = $this->fields;
        return $fields;
    }

} // end of class
