KorespondencjaStorageAcl.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('FileStorage');
  4. class Schema_KorespondencjaStorageAcl extends Core_AclBase {
  5. public function __construct() {
  6. $this->parentAcl = User::getAcl()->getObjectAcl('default_db', 'IN7_DZIENNIK_KORESP');
  7. // DBG::_(true, true, "parentAcl", $this->parentAcl, __CLASS__, __FUNCTION__, __LINE__);
  8. }
  9. public function getSourceName() { return 'objects'; }
  10. public function init($force = false) {}
  11. public function isInitialized() { return true; }
  12. public function getName() { return 'Korespondencja'; }
  13. public function getRootTableName() { return 'IN7_DZIENNIK_KORESP'; }
  14. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  15. public function getVirtualFieldListByIdZasob() { return array(); }
  16. public function getRealFieldListByIdZasob($force = false) {
  17. $cols = $this->parentAcl->getRealFieldListByIdZasob();
  18. $cols[100000] = 'File';
  19. return $cols;
  20. }
  21. public function getFieldIdByName($fieldName) {
  22. $fields = $this->getRealFieldListByIdZasob();
  23. if (empty($fieldName)) return null;
  24. foreach ($fields as $idField => $vFieldName) {
  25. if ($vFieldName == $fieldName) return $idField;
  26. }
  27. return null;
  28. }
  29. public function getFieldType($colName) { return null; }
  30. public function isAllowed($idZasob, $taskPerm, $record = null) {
  31. if ($this->parentAcl->hasField($idZasob)) return $this->parentAcl->isAllowed($idZasob, $taskPerm, $record);
  32. $fields = $this->getRealFieldListByIdZasob();
  33. if (array_key_exists($idZasob, $fields)) {
  34. if ('File' == $fields[$idZasob]) {
  35. foreach ($fields as $idFld => $name) {
  36. if ($idFld == $idZasob) continue;
  37. if ($this->parentAcl->isAllowed($idFld, $taskPerm, $record)) return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43. public function hasFieldPerm($idZasob, $taskPerm) {
  44. if ($this->parentAcl->hasField($idZasob)) return $this->parentAcl->hasFieldPerm($idZasob, $taskPerm);
  45. $fields = $this->getRealFieldListByIdZasob();
  46. if (array_key_exists($idZasob, $fields)) {
  47. if ('File' == $fields[$idZasob]) {
  48. foreach ($fields as $idFld => $name) {
  49. if ($idFld == $idZasob) continue;
  50. if ($this->parentAcl->hasFieldPerm($idFld, $taskPerm)) return true;
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  57. public function canCreateField($fieldName) {
  58. if ('File' == $fieldName) return true;
  59. return $this->parentAcl->canCreateField($fieldName);
  60. }
  61. public function canReadField($fieldName) {
  62. if ('File' == $fieldName) return true;
  63. return $this->parentAcl->canReadField($fieldName);
  64. }
  65. public function canReadObjectField($fieldName, $record) {
  66. if ('File' == $fieldName) return true;
  67. return $this->parentAcl->canReadObjectField($fieldName, $record);
  68. }
  69. public function canWriteField($fieldName) {
  70. if ('File' == $fieldName) return true;
  71. return $this->parentAcl->canWriteField($fieldName);
  72. }
  73. public function canWriteObjectField($fieldName, $record) {
  74. if ('File' == $fieldName) return true;
  75. return $this->parentAcl->canWriteObjectField($fieldName, $record);
  76. }
  77. public function getItems($params = array()) {
  78. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  79. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";}
  80. $items = array();
  81. $rawItems = $this->parentAcl->getItems($params);
  82. foreach ($rawItems as $pk => $item) $items[$pk] = (array)$item;
  83. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";}
  84. if (empty($items)) return $items;
  85. $this->fetchItemRef($items);
  86. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems after fetchItemRef \$items:";print_r($items);echo "\n";}
  87. return $items;
  88. }
  89. public function fetchItemRef(&$items) {
  90. $DBG = V::get('DBG_DS', 0, $_GET, 'int');
  91. $refs = array();// fieldName => xsdType
  92. foreach ($this->getRealFieldListByIdZasob() as $id => $fieldName) {
  93. $fieldType = $this->getXsdFieldType($fieldName);
  94. if ('ref:' == substr($fieldType, 0, 4)) $refs[$fieldName] = substr($fieldType, 4);
  95. else if ('alias_ref:' == substr($fieldType, 0, 10)) $refs[$fieldName] = substr($fieldType, 10);
  96. }
  97. if (empty($refs)) return $items;
  98. $fidList = array_keys($items);
  99. $sqlPk = array(); foreach ($fidList as $pk) { $sqlPk[] = "'{$pk}'"; } $sqlPk = implode(", ", $sqlPk);
  100. $baseRefTableName = $this->getRootTableName() . "__#REF__";
  101. $refRows = array();// $fieldName => [ pk, ... ]
  102. foreach ($refs as $fieldName => $type) {
  103. $acl = $this->getAclFromTypeName($type);
  104. $refTableName = $this->getRootTableName() . "__#REF__" . $acl->getRootTableName();
  105. $refRows[$fieldName] = DB::getPDO()->fetchAllByKey("
  106. select r.*
  107. from `{$refTableName}` r
  108. where r.PRIMARY_KEY in({$sqlPk})
  109. -- TODO and r.INSTANCE = '{$refInstanceName}' -- for multiple types based on the same root table
  110. ", $key = 'PRIMARY_KEY');
  111. if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems loop(\$fieldName:{$fieldName}) \$refTableName:({$refTableName}), \$refRows[$fieldName]";print_r($refRows[$fieldName]);echo"\n";}
  112. }
  113. foreach ($refRows as $fieldName => $refList) {
  114. foreach ($refList as $pk => $ref) {
  115. $items[ $pk ][ $fieldName ][] = array('xlink' => "{$refs[$fieldName]}.{$ref['REMOTE_PRIMARY_KEY']}");
  116. }
  117. }
  118. }
  119. public function addItem($itemTodo) {
  120. return $this->parentAcl->addItem($itemTodo);
  121. }
  122. public function updateItem($itemPatch) {
  123. return $this->parentAcl->updateItem($itemPatch);
  124. }
  125. public function getGeomFieldType($fieldName) { return null; }
  126. public function getPrimaryKeyField() { return 'ID'; }
  127. public function getID() { return 0; }
  128. public function getAttributesFromZasoby() {
  129. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  130. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  131. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  132. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  133. return $attributes;
  134. }
  135. public function isEnumerationField($fieldName) { return false; }
  136. public function getXsdFieldType($fieldName) {
  137. if ('File' == $fieldName) return 'ref:p5_objects:File';
  138. return $this->parentAcl->getXsdFieldType($fieldName);
  139. }
  140. public function isGeomField($fldName) { return $this->parentAcl->isGeomField($fldName); }
  141. }