P5.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. Lib::loadClass('Core_AclHelper');
  3. Lib::loadClass('TableAcl');
  4. Lib::loadClass('SchemaFactory');
  5. /**
  6. * Zasoby:
  7. * DATABASE
  8. * TABELA
  9. * KOMORKA
  10. */
  11. class P5 {
  12. public static function insert($namespace, $item) {
  13. $ns = Core_AclHelper::parseNamespace($namespace);
  14. $idDatabase = Core_AclHelper::getIdDatabaseFromNamespace($namespace);
  15. if ('default_db' == $ns['sourceName']) {
  16. // TODO: check if has A_RECORD_CREATE_AUTHOR field
  17. if (empty($item['A_RECORD_CREATE_AUTHOR'])) $item['A_RECORD_CREATE_AUTHOR'] = User::getLogin();
  18. // TODO: check if has A_RECORD_CREATE_DATE field
  19. if (empty($item['A_RECORD_CREATE_DATE'])) $item['A_RECORD_CREATE_DATE'] = 'NOW()';
  20. $id = DB::getPDO($idDatabase)->insert($ns['name'], $item);
  21. // TODO: check if has _HIST table
  22. $item['ID_USERS2'] = $id;
  23. DB::getPDO($idDatabase)->insert("{$ns['name']}_HIST", $item);
  24. return $id;
  25. } else if ('table_objects' == $ns['sourceName']) {
  26. return Core_AclHelper::getAclByNamespace($namespace)->addItem($item);
  27. } else if ('default_objects' == $ns['sourceName']) {
  28. return Core_AclHelper::getAclByNamespace($namespace)->addItem($item);
  29. }
  30. }
  31. public static function getAclByNamespace($namespace) {
  32. DBG::log("getAclByNamespace({$namespace})");
  33. $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, ['propertyName' => '*,field']);
  34. DBG::log($objectItem, 'array', "getAclByNamespace({$namespace})");
  35. if ($objectItem['isStructInstalled']) {
  36. return self::buildAclFromSystemObject($objectItem, User::getID());
  37. }
  38. throw new Exception("Object not installed '{$namespace}'");
  39. }
  40. public static function buildAclFromSystemObject($objItem, $idUser) {
  41. if ('AntAcl' == $objItem['_type']) {
  42. throw new Exception("TODO: buildAclFromSystemObject - type = '{$objItem['_type']}'");
  43. }
  44. if ('TableAcl' == $objItem['_type']) {
  45. // $this->_db = $arr['db'];
  46. // $this->_name = $arr['name'];
  47. // $this->_rootTableName = V::get('_rootTableName', null, $arr);
  48. // $this->_label = $arr['label'];
  49. // $this->_opis = $arr['opis'];
  50. // $this->_fields = V::get('fields', array(), $arr);
  51. // $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  52. // $this->_types = V::get('types', array(), $arr);
  53. $idTable = $objItem['idZasob'];
  54. $tableConfig = [
  55. 'db' => $objItem['idDatabase'],
  56. 'name' => $objItem['_rootTableName'],
  57. '_rootTableName' => $objItem['_rootTableName'],
  58. 'label' => '...', // TODO: fetch from Zasoby
  59. 'opis' => '...', // TODO: fetch from Zasoby
  60. 'fields' => [], // TODO: ...
  61. 'virtualFieldsIdList' => [], // TODO: fetch additional fields from Zasoby - or add this fields to Storage::reinstallObject action
  62. 'types' => [], // TODO: ...
  63. ];
  64. foreach ($objItem['field'] as $field) {
  65. $fieldName = $field['fieldNamespace'];
  66. $tableConfig['fields'][$fieldName] = [
  67. 'name' => $fieldName,
  68. 'xsdType' => $field['xsdType'],
  69. ];
  70. }
  71. return TableAcl::buildInstance($idTable, $tableConfig);
  72. }
  73. throw new Exception("TODO: buildAclFromSystemObject - type = '{$objItem['_type']}'");
  74. }
  75. public static function getAclById($id) {
  76. $sqlIdZasob = DB::getPDO()->quote($id, PDO::PARAM_INT);
  77. $zasobInfo = DB::getPDO()->fetchFirst("
  78. select t.ID as id
  79. , t.`DESC` as name
  80. , t.DESC_PL as label
  81. , t.OPIS as description
  82. , d.ID as idDatabase
  83. from CRM_LISTA_ZASOBOW t
  84. join CRM_LISTA_ZASOBOW d on(d.ID = t.PARENT_ID)
  85. where t.`TYPE` = 'TABELA'
  86. and t.A_STATUS in('NORMAL','WAITING')
  87. and d.A_STATUS in('NORMAL','WAITING')
  88. and t.ID = {$sqlIdZasob}
  89. ");
  90. if (!$zasobInfo) throw new Exception("Obiekt Nr '{$i}' nie istnieje w bazie zasobów");
  91. DBG::nicePrint($zasobInfo, '$zasobInfo');
  92. if (false !== strpos($zasobInfo['name'], '/')) {
  93. $namespace = "{$zasobInfo['name']}";
  94. } else {
  95. $sourceName = (DB::getPDO()->getZasobId() == $zasobInfo['idDatabase'])
  96. ? 'default_db'
  97. : "zasob_{$zasobInfo['idDatabase']}";
  98. $namespace = "{$sourceName}/{$zasobInfo['name']}";
  99. }
  100. $ns = Core_AclHelper::parseNamespaceUrl($namespace);
  101. DBG::nicePrint($ns, "P5::getAcl by idZasob({$id}) Namespace({$namespace})");
  102. }
  103. public static function getAcl($arg) {
  104. if (is_numeric($arg)) {// $idZasob
  105. return self::getAclById($arg);
  106. } else if (is_string($arg)) {// $namespace
  107. $namespace = $arg;
  108. $ns = Core_AclHelper::parseNamespaceUrl($namespace);
  109. DBG::nicePrint($ns, "P5::getAcl by Namespace({$namespace}) TODO: find idZasob");
  110. if ('default_db' == substr($ns['sourceName'], 0, strlen('default_db'))) {
  111. $idDatabase = DB::getPDO()->getZasobId();
  112. } else if ('zasob_' == substr($ns['sourceName'], 0, strlen('zasob_'))) {
  113. // 'zasob_931', 'zasob_931__x3A__...'
  114. $idDatabase = substr($ns['sourceName'], strlen('zasob_'));
  115. if (false !== strpos($idDatabase, '_')) $idDatabase = substr($idDatabase, 0, strpos($idDatabase, '_'));
  116. DBG::nicePrint($idDatabase, "P5::getAcl by Namespace({$namespace}) TODO: find idZasob - \$idDatabase");
  117. if (!$idDatabase || !is_numeric($idDatabase)) throw new Exception("Not implemented idDatabase({$idDatabase})");
  118. } else {
  119. throw new Exception("Error Processing Request", 1);
  120. }
  121. $sqlObjectName = DB::getPDO()->quote($ns['name'], PDO::PARAM_STR);
  122. $zasobInfo = DB::getPDO()->fetchFirst("
  123. select t.ID as id
  124. , t.`DESC` as name
  125. , t.DESC_PL as label
  126. , t.OPIS as description
  127. , d.ID as idDatabase
  128. from CRM_LISTA_ZASOBOW t
  129. join CRM_LISTA_ZASOBOW d on(d.ID = t.PARENT_ID)
  130. where t.`TYPE` = 'TABELA'
  131. and t.A_STATUS in('NORMAL','WAITING')
  132. and d.A_STATUS in('NORMAL','WAITING')
  133. and d.ID = {$idDatabase}
  134. and t.`DESC` = {$sqlObjectName}
  135. ");
  136. DBG::nicePrint($zasobInfo, "P5::getAcl by Namespace({$namespace}) DB({$idDatabase}) zasobInfo");
  137. if (!$zasobInfo) {// try to install object
  138. }
  139. // $acl = User::getAcl()->getObjectAcl($ns['prefix'], $ns['name']);
  140. // $acl->init($forceTblAclInit);
  141. // return $acl;
  142. }
  143. throw new Exception("TODO: P5::getAcl L." . __LINE__);
  144. }
  145. }