1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Validate the data for a whole schema
7 *
8 * Should be aquired through AccessDataTable::getValidator()
9 */
10class AccessDataValidator extends ValueValidator
11{
12    /** @var AccessTable */
13    protected $access;
14
15    /** @var array */
16    protected $data;
17
18    /**
19     * ValidationResult constructor.
20     * @param AccessTable $access
21     * @param array $data the data to validate (and save)
22     */
23    public function __construct(AccessTable $access, $data)
24    {
25        parent::__construct();
26        $this->access = $access;
27        $this->data = $data;
28    }
29
30    /**
31     * Validate the given data
32     *
33     * checks for assignments
34     * validates
35     * returns changed data only
36     *
37     * @param array $data array('schema' => ( 'fieldlabel' => 'value', ...))
38     * @param string $pageid
39     * @param string[] $errors validation errors
40     * @return AccessDataValidator[]|bool savable data or false on validation error
41     */
42    public static function validateDataForPage($data, $pageid, &$errors)
43    {
44        $tosave = array();
45        $valid = true;
46        $errors = array();
47
48        $assignments = Assignments::getInstance();
49        $tables = $assignments->getPageAssignments($pageid);
50        foreach ($tables as $table) {
51            $access = AccessTable::getPageAccess($table, $pageid);
52            $validation = $access->getValidator($data[$table]);
53            if (!$validation->validate()) {
54                $valid = false;
55                $errors = array_merge($errors, $validation->getErrors());
56            } else {
57                if ($validation->hasChanges()) {
58                    $tosave[] = $validation;
59                }
60            }
61        }
62        if ($valid) return $tosave;
63        return false;
64    }
65
66    /**
67     * Validate the data. This will clean the data according to type!
68     *
69     * @return bool
70     */
71    public function validate()
72    {
73        $result = true;
74        foreach ($this->access->getSchema()->getColumns() as $col) {
75            $label = $col->getType()->getLabel();
76            $result = $result && $this->validateValue($col, $this->data[$label]);
77        }
78        return $result;
79    }
80
81    /**
82     * Check if the data changed (selects current data)
83     *
84     * @return bool
85     */
86    public function hasChanges()
87    {
88        $olddata = $this->access->getDataArray();
89        return ($olddata != $this->data);
90    }
91
92    /**
93     * @return AccessTable
94     */
95    public function getAccessTable()
96    {
97        return $this->access;
98    }
99
100    /**
101     * Access the data after it has been cleand in the validation process
102     *
103     * @return array
104     */
105    public function getCleanData()
106    {
107        return $this->data;
108    }
109
110    /**
111     * Saves the data
112     *
113     * This saves no matter what. You have to chcek validation results and changes on your own!
114     *
115     * @param int $ts the timestamp to use when saving the data
116     * @return bool
117     */
118    public function saveData($ts = 0)
119    {
120        $this->access->setTimestamp($ts);
121        return $this->access->saveData($this->data);
122    }
123}
124