Completed
Push — master ( 01c567...9c6f05 )
by Andreas
23:13
created

org_openpsa_relatedto_dba::_on_updating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.relatedto
4
 * @author Nemein Oy, http://www.nemein.com/
5
 * @copyright Nemein Oy, http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * MidCOM wrapped base class, keep logic here
11
 *
12
 * @property string $fromComponent
13
 * @property string $fromGuid
14
 * @property string $fromClass
15
 * @property string $fromExtra
16
 * @property string $toComponent
17
 * @property string $toGuid
18
 * @property string $toClass
19
 * @property string $toExtra
20
 * @property integer $status
21
 * @package org.openpsa.relatedto
22
 */
23
class org_openpsa_relatedto_dba extends midcom_core_dbaobject
24
{
25
    public $__midcom_class_name__ = __CLASS__;
26
    public $__mgdschema_class_name__ = 'org_openpsa_relatedto';
27
    public $_use_rcs = false;
28
29
    const SUSPECTED = 100;
30
    const CONFIRMED = 120;
31
    const NOTRELATED = 130;
32
33 12
    public function _on_creating()
34
    {
35 12
        $this->check_status();
36
        //PONDER: Should we call check_db() here and prevent creation of multiple very similar links ??
37 12
        return true;
38
    }
39
40 11
    public function _on_loaded()
41
    {
42 11
        $this->check_status();
43 11
    }
44
45 2
    public function _on_updating()
46
    {
47 2
        $this->check_status();
48 2
        return true;
49
    }
50
51 12
    private function check_status()
52
    {
53 12
        if (!$this->status) {
54 1
            $this->status = self::SUSPECTED;
55
        }
56 12
    }
57
58
    /**
59
     * Check database for essentially same relatedto object and returns GUID if found
60
     */
61 11
    public function check_db($check_status = true) : ?string
62
    {
63 11
        $mc = self::new_collector('toGuid', $this->toGuid);
64 11
        $mc->add_constraint('fromClass', '=', $this->fromClass);
65 11
        $mc->add_constraint('toClass', '=', $this->toClass);
66 11
        $mc->add_constraint('fromGuid', '=', $this->fromGuid);
67 11
        $mc->add_constraint('fromComponent', '=', $this->fromComponent);
68 11
        $mc->add_constraint('toComponent', '=', $this->toComponent);
69 11
        if ($check_status) {
70
            $mc->add_constraint('status', '=', $this->status);
71
        }
72 11
        $mc->set_limit(1);
73 11
        return key($mc->list_keys());
74
    }
75
76
    /**
77
     * By default all authenticated users should be able to do
78
     * whatever they wish with relatedto objects, later we can add
79
     * restrictions on object level as necessary.
80
     */
81
    public function get_class_magic_default_privileges()
82
    {
83
        $privileges = parent::get_class_magic_default_privileges();
84
        $privileges['USERS']['midgard:create']  = MIDCOM_PRIVILEGE_ALLOW;
85
        $privileges['USERS']['midgard:update']  = MIDCOM_PRIVILEGE_ALLOW;
86
        $privileges['USERS']['midgard:read']    = MIDCOM_PRIVILEGE_ALLOW;
87
        return $privileges;
88
    }
89
}
90