WorkflowLogFunctionsBehavior.php 2.42 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
<?php
Aster's avatar
Aster committed
2

Marcello Pivanti's avatar
Marcello Pivanti committed
3
/**
Aster's avatar
Aster committed
4
5
 * Emilia Romagna Open Innovation
 * Arter
Marcello Pivanti's avatar
Marcello Pivanti committed
6
7
 *
 *
Aster's avatar
Aster committed
8
 * @package    arter\amos\workflow\behaviors
Marcello Pivanti's avatar
Marcello Pivanti committed
9
10
11
 * @category   CategoryName
 */

Aster's avatar
Aster committed
12
namespace arter\amos\workflow\behaviors;
Marcello Pivanti's avatar
Marcello Pivanti committed
13

Aster's avatar
Aster committed
14
15
16
use arter\amos\core\record\Record;
use arter\amos\workflow\components\events\SimpleWorkFlowEventsListener;
use arter\amos\workflow\models\WorkflowTransitionsLog;
Marcello Pivanti's avatar
Marcello Pivanti committed
17
18
19
20
21
use yii\base\Behavior;
use yii\helpers\Json;

/**
 * Class WorkflowLogFunctionsBehavior
Aster's avatar
Aster committed
22
 * @package arter\amos\workflow\behaviors
Marcello Pivanti's avatar
Marcello Pivanti committed
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
 */
class WorkflowLogFunctionsBehavior extends Behavior
{

    /**
     * @var string $changeStatusComment - attribute for model to send comment data on status change
     */
    public $changeStatusComment;

    public function getStatusLastUpdateTime($status)
    {
        /** @var Record $owner */
        $owner = $this->owner;
        return SimpleWorkFlowEventsListener::getLastUpdateTime($owner->className(), $owner->primaryKey, $status);
    }

    public function getStatusLastUpdateUser($status)
    {
        /** @var Record $owner */
        $owner = $this->owner;
        return SimpleWorkFlowEventsListener::getLastUpdateUser($owner->className(), $owner->primaryKey, $status);
    }

    public function getStatusFirstUpdateUser($status)
    {
        /** @var Record $owner */
        $owner = $this->owner;
        return SimpleWorkFlowEventsListener::getFirstUpdateUser($owner->className(), $owner->primaryKey, $status);
    }

    /**
     * @param $status string, the status of SimpleWorkflowBehavior
     * @return null|number (user)
     */
    public function getStatusLastUpdateComment($status)
    {
        /** @var Record $owner */
        $owner = $this->owner;
        $class = $owner->className();
        $primaryKey = $owner->primaryKey;
        $primaryKeyJson = Json::encode($primaryKey);
        $logList = WorkflowTransitionsLog::find()->andWhere([
            'classname' => $class,
            'owner_primary_key' => $primaryKeyJson,
            'end_status' => $status
        ])->orderBy('created_at DESC')->all();
        if (count($logList) >= 1) {
            /** @var WorkflowTransitionsLog $wtl */
            $wtl = reset($logList);
            return isset($wtl->comment) ? $wtl->comment : null;
        }
        return null;
    }

    public function getStatusUpdateHistory()
    {
        /** @var Record $owner */
        $owner = $this->owner;
        return SimpleWorkFlowEventsListener::getUpdateHistory($owner->className(), $owner->primaryKey);
    }
}