File size: 7,200 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php

namespace ProcessMaker\BusinessModel\Cases;

use AppDocument;
use Cases as ClassesCases;
use Dynaform;
use Exception;
use G;
use Propel;
use StdClass;
use Task as ClassesTask;

/**
 * Return the ChangeLog of a Dynaform
 */
class ChangeLog
{
    /**
     * List of variables that should not be considered
     * @var string[] $reserved
     */
    private $reserved = [
        'TASK',
        'INDEX',
        'DYN_CONTENT_HISTORY',
        '__VAR_CHANGED__',
    ];
    /**
     * List of reserved steps
     * @var string[] $reservedSteps
     */
    private $reservedSteps = [
        -1,
        -2,
    ];

    /**
     * Map of variables and its values
     * @var mixed[]
     */
    private $values = [];

    /**
     * List of variables changes
     * @var object[]
     */
    private $tree;

    /**
     * List of assigned permissions
     * @var string[]
     */
    private $permissions = [];

    public function getChangeLog($appUid, $proUid, $tasUid, $start, $limit)
    {
        $this->loadPermissions($appUid, $proUid, $tasUid);
        $result = $this->getResultSet($appUid);
        $totalCount = $this->readRecords($result, $start, $limit);
        return ['data' => $this->tree, 'totalCount' => $totalCount];
    }

    /**
     * This function get the appHistory related to the case
     *
     * @param string $appUid
     *
     * @return array;
    */
    private function getResultSet($appUid)
    {
        $conn = Propel::getConnection('workflow');
        $sql = 'SELECT APP_HISTORY.*, USERS.USR_USERNAME FROM APP_HISTORY'
            .' LEFT JOIN USERS ON(APP_HISTORY.USR_UID=USERS.USR_UID)'
            .' WHERE APP_UID=? ORDER BY HISTORY_DATE ASC';
        $stmt = $conn->prepareStatement($sql);
        $stmt->set(1, $appUid);
        if (!$stmt->executeQuery()) {
            throw Exception(G::LoadTranslation('ID_MSG_AJAX_FAILURE'));
        }
        return $stmt->getResultSet();
    }

    /**
     * This function read the records, related to the specific result and update the data
     *
     * @param object $result
     * @param integer $start
     * @param integer $limit
     *
     * @return integer;
     */
    private function readRecords($result, $start = 0, $limit = 15)
    {
        $index = 0;
        while ($result->next()) {
            $row = $result->getRow();
            $data = unserialize($row['HISTORY_DATA']);
            if ($this->isEmpty($data)) {
                continue;
            }
            if ($index < $start) {
                $index += $this->updateData(
                    $data, $row, $this->hasPermission($row['DYN_UID']), false);
                continue;
            }
            $a = $this->updateData($data, $row,
                                   $this->hasPermission($row['DYN_UID']), true);
            $limit-= $a;
            $index+= $a;
        }
        return $index;
    }

    /**
     * This function check if is empty
     *
     * @param array $data
     *
     * @return boolean;
     */
    private function isEmpty($data)
    {
        foreach ($data as $key => $value) {
            if (array_search($key, $this->reserved) !== false) {
                continue;
            }
            return false;
        }
        return true;
    }

    /**
     * This function update the data
     *
     * @param array $data
     * @param array $row
     * @param boolean $hasPermission
     * @param boolean $addToTree
     *
     * @return integer;
     */
    private function updateData($data, $row, $hasPermission, $addToTree = false)
    {
        $i = 0;
        foreach ($data as $key => $value) {
            if (array_search($key, $this->reserved) !== false) {
                continue;
            }
            if ($hasPermission && (!isset($this->values[$key]) || $this->values[$key]
                !== $value)) {
                if ($addToTree) {
                    $node = new StdClass();
                    $node->field = $key;
                    $previousValue = !isset($this->values[$key]) ? null : $this->values[$key];
                    if(!is_array($previousValue)){
                        $node->previousValue = (string) $previousValue;
                    } else {
                        $node->previousValue = "<br />".nl2br(print_r($previousValue, true));
                    }
                    if(!is_array($value)){
                        $node->currentValue = (string) $value;
                    } else {
                        $node->currentValue = "<br />".nl2br(print_r($value, true));
                    }
                    $node->previousValueType = gettype($previousValue);
                    $node->currentValueType = gettype($value);
                    $node->record = $this->getHistoryTitle($row);
                    $this->tree[] = $node;
                }
                $i++;
            }
            $this->values[$key] = $value;
        }
        return $i;
    }

    /**
     * This function get the title related to the row
     *
     * @param array $row
     *
     * @return string;
     */
    private function getHistoryTitle($row)
    {
        return $this->getObjectTitle($row['TAS_UID'], 'TASK')
            .' / '.$this->getObjectTitle($row['DYN_UID'], $row['OBJ_TYPE'])
            .' / '.G::LoadTranslation('ID_LAN_UPDATE_DATE').': '.$row['HISTORY_DATE']
            .' / '.G::LoadTranslation('ID_USER').': '.$row['USR_USERNAME'];
    }

    /**
     * This function get the object title
     *
     * @param string $uid
     * @param string $objType
     *
     * @return string;
     */
    private function getObjectTitle($uid, $objType)
    {
        switch ($objType) {
            case 'DYNAFORM':
                $obj = new Dynaform();
                $obj->Load($uid);
                $title = $obj->getDynTitle();
                break;
            case 'OUTPUT_DOCUMENT':
            case 'INPUT_DOCUMENT':
                $obj = new AppDocument();
                $obj->load($uid);
                $title = $obj->getDynTitle();
                break;
            case 'TASK':
                $obj = new ClassesTask();
                $obj->load($uid);
                $title = $obj->getTasTitle();
                break;
            default:
                $title = $uid;
        }
        return $title;
    }

    /**
     * This function get the permissions
     *
     * @param string $appUid
     * @param string $proUid
     * @param string $tasUid
     *
     * @return void;
     */
    private function loadPermissions($appUid, $proUid, $tasUid)
    {
        $oCase = new ClassesCases();
        $this->permissions = $oCase->getAllObjects(
            $proUid, $appUid, $tasUid, $_SESSION['USER_LOGGED']
        );
    }

    /**
     * This function verify if has permission
     *
     * @param string $uid
     *
     * @return boolean;
     */
    private function hasPermission($uid)
    {
        if(array_search($uid, $this->reservedSteps)!==false) {
            return false;
        }
        foreach ($this->permissions as $type => $ids) {
            if (array_search($uid, $ids) !== false) {
                return true;
            }
        }
        return false;
    }
}