<?php

App::uses('Helper', 'View/Helper');
App::uses('Task', 'Model');


/**
 * Custom Helper
 *
 * For custom theme specific methods.
 *
 * If your theme requires custom methods,
 * copy this file to /app/views/themed/your_theme_alias/helpers/custom.php and modify.
 *
 * You can then use this helper from your theme's views using $custom variable.
 *
 * @category Helper
 * @package  Bemmelhor
 * @version  1.0
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 * 
 * @link     http://www.bemmelhor.org
 */
class DashboardHelper extends Helper {

/**
 * Other helpers used by this helper
 *
 * @var array
 * @access public
 */
	public $helpers = array();
	
/**
 * Conversor de Task para o formato html
 */
	public function formatTasksResume() {
	
	    $Task = new Task();
	    $tasksOpen = $Task->find('count',
            array(
                'conditions' => array('Task.open' => true)
            )
	    		);
	    $tasksClose = $Task->find('count',
            array(
                'conditions' => array('Task.open' => false)
            )
	    		);
	    
	    $dados = array();	 
	    $dados['Aberto '] = $tasksOpen;
	    $dados['Fechado '] = $tasksClose;
    
	    return json_encode($dados);
	}

/**
 * Conversor de Task para o formato html
 */
	public function formatTasks($qtdeItens = 4) {

	    $Task = new Task();
	    $tasks = $Task->find('all',
            array(
                'fields' => array(
                    'Task.id',
                    'Task.created',
                    'Task.subject',
                ),
                'conditions' => array(
                    'Task.open' => true,
                ),
                'order' => array(
                    'Task.id DESC',
                ),       
                'recursive' => 0,
                'limit' => $qtdeItens,
            ));
	    
	    $html = sprintf("%s  %s", 
	            "<a href='admin/tasks'>List Tasks</a>",	    
	            "<a href='admin/tasks/add'>Add Task</a>");
	            
	    $html .= '<table>';
        foreach ($tasks as $task) {
        	$html .= sprintf("<tr><td><a href='admin/tasks/view/%s'>%s</a></td></tr>",
        		$task['Task']['id'],$task['Task']['subject']);
        }
        $html .= "</table>";
	    
	   return $html;
	}

/**
 * Conversor de Task resolvidas para o formato html
 */
	public function formatClosedTasks($qtdeItens = 4) {
	
	    $Task = new Task();
	    $tasks = $Task->find('all',
	            array(
	                    'fields' => array(
	                            'Task.id',
	                            'Task.created',
	                            'Task.subject',
	                    ),
	                    'conditions' => array(
	                            'Task.open' => false,
	                    ),
	                    'order' => array(
	                            'Task.updated DESC',
	                    ),
	                    'recursive' => 0,
	                    'limit' => $qtdeItens,
	            ));
	         
	    $html = '<table>';
	    foreach ($tasks as $task) {
	        $html .= sprintf("<tr><td><a href='admin/tasks/view/%s'>%s</a></td></tr>",
	                $task['Task']['id'],$task['Task']['subject']);
	    }
	    $html .= "</table>";
	     
	    return $html;
	}	
	
/**
 * Conversor de Usuarios para o formato html
 */
	public function formatUsers($qtdeItens = 4) {
	
	    $User = new User();
	    $users = $User->find('all',
            array(
                'fields' => array(
                    'User.id',
                    'User.name',
                ),
                'order' => array(
                    'User.id DESC',
                ),
                'recursive' => 0,
                'limit' => $qtdeItens,
            ));
	     
	    $html = '<table>';
	    foreach ($users as $user) {
	        $html .= sprintf("<tr><td><a href='admin/users/edit/%s'>%s</a></td></tr>",
	                $user['User']['id'],$user['User']['name']);
	    }
	    $html .= "</table>";
	     
	    return $html;
	}
}
