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 parent::__construct(); 25 $this->access = $access; 26 $this->data = $data; 27 } 28 29 /** 30 * Validate the given data 31 * 32 * checks for assignments 33 * validates 34 * returns changed data only 35 * 36 * @param array $data array('schema' => ( 'fieldlabel' => 'value', ...)) 37 * @param string $pageid 38 * @param string[] $errors validation errors 39 * @return AccessDataValidator[]|bool savable data or false on validation error 40 */ 41 static public function validateDataForPage($data, $pageid, &$errors) { 42 $tosave = array(); 43 $valid = true; 44 $errors = array(); 45 46 $assignments = Assignments::getInstance(); 47 $tables = $assignments->getPageAssignments($pageid); 48 foreach($tables as $table) { 49 // FIXME pass some revision ts or we get the wrong type of access! 50 $access = AccessTable::byTableName($table, $pageid, time()); 51 $validation = $access->getValidator($data[$table]); 52 if(!$validation->validate()) { 53 $valid = false; 54 $errors = array_merge($errors, $validation->getErrors()); 55 } else { 56 if($validation->hasChanges()) { 57 $tosave[] = $validation; 58 } 59 } 60 } 61 if($valid) return $tosave; 62 return false; 63 } 64 65 /** 66 * Validate the data. This will clean the data according to type! 67 * 68 * @return bool 69 */ 70 public function validate() { 71 $result = true; 72 foreach($this->access->getSchema()->getColumns() as $col) { 73 $label = $col->getType()->getLabel(); 74 $result = $result && $this->validateValue($col, $this->data[$label]); 75 } 76 return $result; 77 } 78 79 /** 80 * Check if the data changed (selects current data) 81 * 82 * @return bool 83 */ 84 public function hasChanges() { 85 $olddata = $this->access->getDataArray(); 86 return ($olddata != $this->data); 87 } 88 89 /** 90 * @return AccessTable 91 */ 92 public function getAccessTable() { 93 return $this->access; 94 } 95 96 /** 97 * Access the data after it has been cleand in the validation process 98 * 99 * @return array 100 */ 101 public function getCleanData() { 102 return $this->data; 103 } 104 105 /** 106 * Saves the data 107 * 108 * This saves no matter what. You have to chcek validation results and changes on your own! 109 * 110 * @param int $ts the timestamp to use when saving the data 111 * @return bool 112 */ 113 public function saveData($ts = 0) { 114 $this->access->setTimestamp($ts); 115 return $this->access->saveData($this->data); 116 } 117 118} 119