summaryrefslogtreecommitdiff
path: root/app/Model/ActionParameter.php
blob: f170ef6617a7d22c2a61363ce0b1fbcc9f2f31de (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

namespace Kanboard\Model;

use SimpleValidator\Validator;
use SimpleValidator\Validators;

/**
 * Action Parameter Model
 *
 * @package  model
 * @author   Frederic Guillot
 */
class ActionParameter extends Base
{
    /**
     * SQL table name
     *
     * @var string
     */
    const TABLE = 'action_has_params';

    /**
     * Get all action params
     *
     * @access public
     * @param  integer $action_id
     * @return array
     */
    public function getAll($action_id)
    {
        return $this->db->hashtable(self::TABLE)->eq('action_id', $action_id)->getAll('name', 'value');
    }

    /**
     * Insert new parameters for an action
     *
     * @access public
     * @param  integer $action_id
     * @param  array  $values
     * @return boolean
     */
    public function create($action_id, array $values)
    {
        foreach ($values['params'] as $name => $value) {
            $param = array(
                'action_id' => $action_id,
                'name' => $name,
                'value' => $value,
            );

            if (! $this->db->table(self::TABLE)->save($param)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Duplicate action parameters
     *
     * @access public
     * @param  integer  $project_id
     * @param  integer  $action_id
     * @param  array    $params
     * @return boolean
     */
    public function duplicateParameters($project_id, $action_id, array $params)
    {
        foreach ($params as $name => $value) {
            $value = $this->resolveParameter($project_id, $name, $value);

            if ($value === false) {
                $this->logger->error('ActionParameter::duplicateParameters => unable to resolve '.$name.'='.$value);
                return false;
            }

            $values = array(
                'action_id' => $action_id,
                'name' => $name,
                'value' => $value,
            );

            if (! $this->db->table(self::TABLE)->insert($values)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Resolve action parameter values according to another project
     *
     * @access private
     * @param  integer $project_id
     * @param  string  $name
     * @param  string  $value
     * @return mixed
     */
    private function resolveParameter($project_id, $name, $value)
    {
        switch ($name) {
            case 'project_id':
                return $value != $project_id ? $value : false;
            case 'category_id':
                return $this->category->getIdByName($project_id, $this->category->getNameById($value)) ?: false;
            case 'src_column_id':
            case 'dest_column_id':
            case 'dst_column_id':
            case 'column_id':
                $column = $this->board->getColumn($value);
                return empty($column) ? false : $this->board->getColumnIdByTitle($project_id, $column['title']) ?: false;
            case 'user_id':
            case 'owner_id':
                return $this->projectPermission->isAssignable($project_id, $value) ? $value : false;
            default:
                return $value;
        }
    }
}