summaryrefslogtreecommitdiff
path: root/app/Model/BaseHistory.php
blob: 31578a3bcc56867ce378f5c2daec3a5fd9abe6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace Model;

use PDO;
use Core\Template;

/**
 * Task history model
 *
 * @package  model
 * @author   Frederic Guillot
 */
abstract class BaseHistory extends Base
{
    /**
     * SQL table name
     *
     * @access protected
     * @var    string
     */
    protected $table = '';

    /**
     * Remove old event entries to avoid a large table
     *
     * @access public
     * @param  integer    $max    Maximum number of items to keep in the table
     */
    public function cleanup($max)
    {
        if ($this->db->table($this->table)->count() > $max) {

            $this->db->execute('
                DELETE FROM '.$this->table.'
                WHERE id <= (
                    SELECT id FROM (
                        SELECT id FROM '.$this->table.' ORDER BY id DESC LIMIT 1 OFFSET '.$max.'
                    ) foo
                )');
        }
    }

    /**
     * Get all events for a given project
     *
     * @access public
     * @return array
     */
    public function getAllByProjectId($project_id)
    {
        return $this->db->table($this->table)
                        ->eq('project_id', $project_id)
                        ->desc('id')
                        ->findAll();
    }

    /**
     * Get the event html content
     *
     * @access public
     * @param  array     $params    Event properties
     * @return string
     */
    public function getContent(array $params)
    {
        $tpl = new Template;
        return $tpl->load('event_'.str_replace('.', '_', $params['event_name']), $params);
    }
}