Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
CheckEnd.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 class CheckEnd extends CheckData
8 {
9  /**
10  * max column for a table in postgresql
11  */
12  const maxSqlColumn = 1600;
13  /**
14  * @var DocFam
15  */
16  protected $doc;
17  /**
18  * @var importDocumentDescription
19  */
20  protected $importer = null;
22  {
23  $this->importer = $importer;
24  }
25  /**
26  * @param array $data
27  * @param Doc $doc
28  * @return CheckEnd
29  */
30  public function check(array $data, &$doc = null)
31  {
32  $this->doc = $doc;
33  if (strstr($doc->usefor, 'W')) {
34  $checkW = new CheckWorkflow($doc->classname, $doc->name);
35  $checkCr = $checkW->verifyWorkflowComplete();
36  if (count($checkCr) > 0) {
37  $this->addError(implode("\n", $checkCr));
38  }
39  }
40 
41  $this->checkSetAttributes();
42  $this->checkOrderAttributes();
44  $this->checkDefault();
45  $this->checkParameters();
46  $this->checkLinks();
47  return $this;
48  }
49  private function getColumnCount()
50  {
51 
52  $c = count($this->doc->fields) + count($this->doc->sup_fields);
53  $ancestor = $this->doc->getFathersDoc();
54  $ancestor[] = $this->doc->id;
55 
56  $sql = sprintf("select count(*) from docattr where type != 'frame' and type != 'tab' and type != 'array' and %s", GetSqlCond($ancestor, "docid", true));
57  simpleQuery('', $sql, $r, true, true);
58  $c+= $r;
59  return $c;
60  }
61  /**
62  * Verify if max sql column is reached
63  * @param Doc $doc
64  */
65  public function checkMaxAttributes(Doc & $doc)
66  {
67  $this->doc = $doc;
68  $c = $this->getColumnCount();
69  if ($c > self::maxSqlColumn) {
70  $this->addError(ErrorCode::getError('ATTR1701', $c, self::maxSqlColumn));
71  }
72  }
73  protected function checkSetAttributes()
74  {
75  $this->doc->getAttributes(); // force reattach attributes
76  $la = $this->doc->GetNormalAttributes();
77  foreach ($la as & $oa) {
78  $foa = $oa->fieldSet;
79  if (!$foa) {
80  $this->addError(ErrorCode::getError('ATTR0203', $oa->id, $this->doc->name));
81  } elseif ((!is_a($foa, "FieldSetAttribute")) && ($foa->type != 'array')) {
82  $this->addError(ErrorCode::getError('ATTR0204', $foa->id, $oa->id));
83  } else {
84  $type = $oa->type;
85  $ftype = $oa->fieldSet->type;
86  if (($ftype != 'frame') && ($ftype != 'array')) {
87  $this->addError(ErrorCode::getError('ATTR0205', $foa->id, $oa->id));
88  }
89  }
90  }
91 
92  $la = $this->doc->GetFieldAttributes();
93  foreach ($la as & $oa) {
94  $foa = $oa->fieldSet;
95  if ($foa) {
96  $type = $oa->type;
97  $ftype = $oa->fieldSet->type;
98  if (($type == 'frame') && ($ftype != 'tab') && ($oa->fieldSet->id != Adoc::HIDDENFIELD)) {
99  $this->addError(ErrorCode::getError('ATTR0207', $foa->id, $oa->id));
100  }
101  }
102  }
103  }
104 
105  protected function checkOrderAttributes()
106  {
107  $la = $this->doc->getAttributes(); // force reattach attributes
108  foreach ($la as & $oa) {
109  if ($oa) {
110  $relativeOrder = $oa->getOption("relativeOrder");
111  if ($relativeOrder && $relativeOrder !== \Dcp\FamilyAbsoluteOrder::firstOrder && $relativeOrder !== \Dcp\FamilyAbsoluteOrder::autoOrder) {
112  if (!$this->doc->getAttribute($relativeOrder)) {
113  $this->addError(ErrorCode::getError('ATTR0212', $oa->id, $relativeOrder));
114  }
115  }
116  }
117  }
118  }
119 
121  {
122  $this->doc->getAttributes(); // force reattach attributes
123  $la = $this->doc->GetNormalAttributes();
124  foreach ($la as & $oa) {
125  if (($oa->phpfile == '' || $oa->phpfile == '-') && (preg_match('/^[a-z0-9_]*::/i', $oa->phpfunc))) {
126  $this->checkMethod($oa);
127  }
128  if (preg_match('/^[a-z0-9_]*::/i', $oa->phpconstraint)) {
129  $this->checkConstraint($oa);
130  }
131  }
132  }
133  /**
134  * check method validity for phpfunc property
135  * @param NormalAttribute $oa
136  */
137  private function checkMethod(NormalAttribute & $oa)
138  {
139  $oParse = new parseFamilyMethod();
140  $strucFunc = $oParse->parse($oa->phpfunc);
141  $error = $oParse->getError();
142  if ($error) {
143  $this->addError(ErrorCode::getError('ATTR1262', $oa->phpfunc, $oa->id, $error));
144  } else {
145 
146  $err = $this->verifyMethod($strucFunc, $oa, "phpFunc");
147  if ($err) {
148  $this->addError($err);
149  $this->addError(ErrorCode::getError('ATTR1265', $this->doc->name, $err));
150  }
151  }
152  }
153  /**
154  * Verify all links which references document's method
155  */
156  protected function checkLinks()
157  {
158  $la = $this->doc->getAttributes();
159  foreach ($la as & $oa) {
160  if ($oa) $this->checkLinkMethod($oa);
161  }
162  }
163  /**
164  * check method validity for phpfunc property
165  *
166  * @param BasicAttribute|MenuAttribute|NormalAttribute $oa
167  */
168  private function checkLinkMethod(BasicAttribute & $oa)
169  {
170  if (empty($oa->link)) return;
171  $link = '';
172  if (preg_match('/action=FDL_METHOD&.*method=([^&]*)/', $oa->link, $reg)) {
173  $link = urldecode($reg[1]);
174  if (preg_match('/^[a-z0-9_]+$/i', $link)) $link = '::' . $link . '()';
175  } elseif (preg_match('/^[a-z0-9_]*::/i', $oa->link, $reg)) {
176  $link = $oa->link;
177  }
178  if (!$link) return;
179  //og($oa->id. '=>'.$oa->link);
180  $oParse = new parseFamilyMethod();
181  $strucLink = $oParse->parse($link);
182  $error = $oParse->getError();
183  if ($error) {
184  $this->addError(ErrorCode::getError('ATTR1000', $link, $oa->id, $error));
185  } else {
186  /*
187  * @var ReflectionMethod $refMeth
188  */
189  $err = $this->verifyMethod($strucLink, $oa, "Link", $refMeth);
190  if ($err) {
191  $this->addError($err);
192  $this->addError(ErrorCode::getError('ATTR1001', $this->doc->name, $err));
193  } else {
194  $methodComment = $refMeth->getDocComment();
195  if (!preg_match('/@apiExpose\b/', $methodComment)) {
196  $completeMethod = $refMeth->getDeclaringClass()->getName() . '::' . $refMeth->getName() . '()';
197  $this->addError(ErrorCode::getError('ATTR1002', $completeMethod, $oa->id));
198  }
199  }
200  }
201  }
202  private function checkDefault()
203  {
204  $defaults = $this->doc->getOwnDefValues();
205  foreach ($defaults as $attrid => $def) {
206  /**
207  * @var $oa NormalAttribute
208  */
209  $oa = $this->doc->getAttribute($attrid);
210  if (!$oa) {
211  $this->addError(ErrorCode::getError('DFLT0005', $attrid, $this->doc->name));
212  } else {
213  $oParse = new parseFamilyMethod();
214  $strucFunc = $oParse->parse($def);
215  $error = $oParse->getError();
216  if (!$error) {
217 
218  $err = $this->verifyMethod($strucFunc, $oa, "Default value");
219  if ($err) {
220  $this->addError(ErrorCode::getError('DFLT0004', $attrid, $this->doc->name, $err));
221  }
222  } else {
223  if ($oa->type == "array") {
224  $value = json_decode($def);
225  if ($value === null) {
226  $this->addError(ErrorCode::getError('DFLT0006', $attrid, $def, $this->doc->name));
227  }
228  }
229  }
230  }
231  }
232  }
233 
234  private function checkParameters()
235  {
236  $parameters = $this->doc->getOwnParams();
237  foreach ($parameters as $attrid => $def) {
238  /**
239  * @var $oa NormalAttribute
240  */
241  $oa = false;
242  /*
243  * Try to get the up-to-date attribute from the current importer if
244  * it has been defined or updated in the current import session.
245  */
246  if (is_object($this->importer)) {
247  $oa = $this->importer->getImportedAttribute($this->doc->id, $attrid);
248  }
249  /*
250  * Otherwise, try to get the attribute from the family's class
251  */
252  if (!$oa) {
253  $oa = $this->doc->getAttribute($attrid);
254  }
255  if (!$oa) {
256  $this->addError(ErrorCode::getError('INIT0005', $attrid, $this->doc->name));
257  } else {
258  if ($oa->usefor != 'Q') {
259  // TODO : cannot test here because DEFAULT set parameters systematicaly
260  // $this->addError(ErrorCode::getError('INIT0006', $attrid, $this->doc->name));
261 
262  } else {
263  $oParse = new parseFamilyMethod();
264  $strucFunc = $oParse->parse($def);
265  $error = $oParse->getError();
266  if (!$error) {
267 
268  $err = $this->verifyMethod($strucFunc, $oa, "Parameters");
269  if ($err) {
270  $this->addError(ErrorCode::getError('INIT0004', $attrid, $this->doc->name, $err));
271  }
272  }
273  }
274  }
275  }
276  $this->checkSetParameters();
277  }
278  protected function checkSetParameters()
279  {
280  $this->doc->getAttributes(); // force reattach attributes
281  $la = $this->doc->getParamAttributes();
282  foreach ($la as & $oa) {
283  $foa = $oa->fieldSet;
284  if (!$foa) {
285  $this->addError(ErrorCode::getError('ATTR0208', $oa->id, $this->doc->name));
286  } elseif ((!is_a($foa, "FieldSetAttribute")) && ($foa->type != 'array')) {
287  $this->addError(ErrorCode::getError('ATTR0209', $foa->id, $oa->id));
288  } else {
289  $type = $oa->type;
290  $ftype = $oa->fieldSet->type;
291  if (($ftype != 'frame') && ($ftype != 'array')) {
292  $this->addError(ErrorCode::getError('ATTR0210', $foa->id, $oa->id));
293  } elseif ($ftype == 'array') {
294  if ($oa->needed) {
295  $this->addError(ErrorCode::getError('ATTR0903', $oa->id));
296  }
297  }
298  }
299  }
300  }
301  private function verifyMethod($strucFunc, $oa, $ctx, &$refMeth = null)
302  {
303  $err = '';
304  $phpMethName = $strucFunc->methodName;
305  if ($strucFunc->className) {
306  $phpClassName = $strucFunc->className;
307  } else {
308  $phpClassName = sprintf("Doc%d", $this->doc->id);
309  }
310  $phpLongName = $strucFunc->className . '::' . $phpMethName;
311  try {
312  $refMeth = new ReflectionMethod($phpClassName, $phpMethName);
313  $numArgs = $refMeth->getNumberOfRequiredParameters();
314  if ($numArgs > count($strucFunc->inputs)) {
315  $err = (ErrorCode::getError('ATTR1261', $phpLongName, $ctx, $numArgs, $oa->id));
316  } else {
317  if ($strucFunc->className && (!$refMeth->isStatic())) {
318  $err = (ErrorCode::getError('ATTR1263', $phpLongName, $ctx, $oa->id));
319  }
320  }
321  }
322  catch(Exception $e) {
323  if ($oa->docid == $this->doc->id) {
324  $err = (ErrorCode::getError('ATTR1260', $phpLongName, $ctx, $oa->id));
325  } else {
326  $err = (ErrorCode::getError('ATTR1266', $phpLongName, $ctx, getNameFromId($this->doc->dbaccess, $oa->docid) , $oa->id));
327  }
328  }
329  return $err;
330  }
331  /**
332  * check method validity for constraint property
333  * @param NormalAttribute $oa
334  */
335  private function checkConstraint(NormalAttribute & $oa)
336  {
337  $oParse = new parseFamilyMethod();
338  $strucFunc = $oParse->parse($oa->phpconstraint, true);
339  $error = $oParse->getError();
340  if ($error) {
341  $this->addError(ErrorCode::getError('ATTR1404', $oa->phpconstraint, $oa->id, $error));
342  } else {
343 
344  $phpMethName = $strucFunc->methodName;
345  if ($strucFunc->className) {
346  $phpClassName = $strucFunc->className;
347  } else {
348  $phpClassName = sprintf("Doc%d", $this->doc->id);
349  }
350  $phpLongName = $strucFunc->className . '::' . $phpMethName;
351  try {
352  $refMeth = new ReflectionMethod($phpClassName, $phpMethName);
353  $numArgs = $refMeth->getNumberOfRequiredParameters();
354  if ($numArgs > count($strucFunc->inputs)) {
355  $this->addError(ErrorCode::getError('ATTR1401', $phpLongName, $numArgs, count($strucFunc->inputs) , $oa->id));
356  } else {
357  if ($strucFunc->className && (!$refMeth->isStatic())) {
358  $this->addError(ErrorCode::getError('ATTR1403', $phpLongName, $oa->id));
359  }
360  }
361  }
362  catch(Exception $e) {
363  $this->addError(ErrorCode::getError('ATTR1402', $phpLongName, $oa->id));
364  }
365  }
366  }
367 }
checkOrderAttributes()
Definition: CheckEnd.php:105
__construct(importDocumentDescription &$importer=null)
Definition: CheckEnd.php:21
checkComputedConstraintAttributes()
Definition: CheckEnd.php:120
static getError($code, $args=null)
Definition: ErrorCode.php:27
const maxSqlColumn
Definition: CheckEnd.php:12
checkSetAttributes()
Definition: CheckEnd.php:73
checkSetParameters()
Definition: CheckEnd.php:278
addError($msg)
Definition: CheckData.php:29
GetSqlCond($Table, $column, $integer=false)
getNameFromId($dbaccess, $id)
Check worflow definition when importing definition.
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
checkLinks()
Definition: CheckEnd.php:156
if($file) if($subject==""&&$file) if($subject=="") $err
$value
check(array $data, &$doc=null)
Definition: CheckEnd.php:30
checkMaxAttributes(Doc &$doc)
Definition: CheckEnd.php:65
$data
← centre documentaire © anakeen