xref: /plugin/struct/meta/Assignments.php (revision fb31ca9fa3249b10a91554bcd044f27dda620b99)
1<?php
2
3namespace plugin\struct\meta;
4
5class Assignments {
6
7    /** @var \helper_plugin_sqlite|null */
8    protected $sqlite;
9
10    /** @var  array All the assignments */
11    protected $assignments;
12
13    /**
14     * Assignments constructor.
15     */
16    public function __construct() {
17        /** @var \helper_plugin_struct_db $helper */
18        $helper = plugin_load('helper', 'struct_db');
19        $this->sqlite = $helper->getDB();
20
21        if($this->sqlite) $this->load();
22    }
23
24    /**
25     * Load existing assignments
26     */
27    protected function load() {
28        $sql = 'SELECT * FROM schema_assignments ORDER BY assign';
29        $res = $this->sqlite->query($sql);
30        $this->assignments = $this->sqlite->res2arr($res);
31        $this->sqlite->res_close($res);
32    }
33
34    /**
35     * Returns a list of table names assigned to the given page
36     *
37     * @param string $page
38     * @return string[] tables assigned
39     */
40    public function getPageAssignments($page) {
41        $tables = array();
42
43        $page = cleanID($page);
44        $pns = ':' . getNS($page) . ':';
45
46        foreach($this->assignments as $row) {
47            $ass = $row['assign'];
48            $tbl = $row['tbl'];
49
50            $ans = ':' . cleanID($ass) . ':';
51
52            if(substr($ass, -2) == '**') {
53                // upper namespaces match
54                if(strpos($pns, $ans) === 0) {
55                    $tables[] = $tbl;
56                }
57            } else if(substr($ass, -1) == '*') {
58                // namespaces match exact
59                if($ans == $pns) {
60                    $tables[] = $tbl;
61                }
62            } else {
63                // exact match
64                if(cleanID($ass) == $page) {
65                    $tables[] = $tbl;
66                }
67            }
68        }
69
70        return array_unique($tables);
71    }
72}
73