<?php

App::uses('AppModel', 'Model');

/**
 * Schedule Model
 */
class Schedule extends AppModel {

    /**
     * Campos usado nos combos como campo label
     */
    public $displayField = 'name';


    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Church' => array(
            'className' => 'Church',
            'foreignKey' => 'church_id',
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'fields' => array(
                'User.id',
                'User.name',
                'User.email'
            ),
        )
    );
    /**
     * Retorna um json de itens para popular um combo
     */
    public function list_all($current_item_id = null) {
        $jsonItems = '';
        $where = array();

        if ($current_item_id != null) {
            $current_item = $this->read(array(
                'id',
                'name'
            ), $current_item_id);

            $jsonItems .= "{id: '{$current_item['Schedule']['id']}', 'text': '{$current_item['Schedule']['name']}'},";
            $where = array(
                'id !=' => $current_item['Schedule']['id']
            );
        }

        $this->recursive = -1;
        $items = $this->find('all', array(
            'fields' => array(
                'id',
                'name'
            ),
            'order' => array(
                'default_selected DESC',
                'name'
            ),
            'conditions' => $where
        ));
        foreach ($items as $item) {
            $jsonItems .= "{id: '{$item['Schedule']['id']}', 'text': '{$item['Schedule']['name']}'},";
        }
        return "[{$jsonItems}]";
    }

    /**
     * EXEMPLO HABTM
     *
     * Retorna a lista de chatqueues formatada
     * $resource = 'list', 'all', 'keys'
     *
     * ==> all
     *
     * array(
     *	(int) 0 => array(
     *		'ChatQueue' => array(
     *			'id' => (int) 1,
     *			'name' => 'Comercial',
     *			'code' => '1'
     *		)
     *	),
     *	(int) 1 => array(
     *		'ChatQueue' => array(
     *			'id' => (int) 2,
     *			'name' => 'Suporte',
     *			'code' => '2'
     *		)
     *	));
     *
     * ==> list
     *
     * array(
     *	(int) 1 => array(
     *		(int) 1 => 'Comercial'
     *	),
     *	(int) 2 => array(
     *		(int) 2 => 'Suporte'
     *	));
     *
     * ==> keys
     *
     * array(
     *	(int) 0 => (int) 1,
     *	(int) 1 => (int) 2,
     * );
     *
     * No Controller
     *
     * $selecteds = $this->ChatQueue->getChatQueuesByUserId($this->Auth->user('id'), 'keys');
     * $this->set(compact('selecteds'));
     *
     * Na View
     *
     * <\?php
     *    echo $this->Form->input('ChatQueue.ChatQueue', array(
     *        'label' => false,
     *        'type' => 'select',
     *        'multiple' => 'checkbox',
     *        'class' => 'no-border',
     *        'selected' => $selecteds
     *    ));
     * ?>
     */
    public function getChatQueuesByUserId($userId = null, $resource = 'list') {

        if (empty($userId)) {
            return false;
        }

        $action = $resource;
        if ($resource == 'keys') {
            $action = 'list';
        }

        $chatQueues = $this->find($action, array(
            'fields' => array(
                'ChatQueue.id',
                'ChatQueue.name',
                'ChatQueue.code',
            ),
            'joins' => array(
                array('table' => 'chat_queue_users',
                    'alias' => 'ChatQueueUser',
                    'type' => 'INNER',
                    'conditions' => array(
                        'ChatQueueUser.user_id' => $userId,
                        'ChatQueueUser.chat_queue_id = ChatQueue.id'
                    )
                )
            ),
            'group' => 'ChatQueue.id',
            'recursive' => -1,
        ));

        if ($resource == 'keys') {
            return array_keys($chatQueues);
        }
        return $chatQueues;
    }
}