UserConfigStorageAcl.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. Lib::loadClass('UsersHelper');
  5. /**
  6. * @require idUser (ref from parent object - User)
  7. */
  8. class Schema_UserConfigStorageAcl extends Core_AclSimpleSchemaBase {
  9. public $_simpleSchema = [
  10. 'root' => [
  11. '@namespace' => 'default_objects/UserConfig',
  12. '@primaryKey' => 'ID_USER',
  13. 'ID_USER' => [ '@type' => 'xsd:integer' ],
  14. 'ID_PROCES' => [ '@type' => 'xsd:integer' ],
  15. 'lastLogin' => [ '@type' => 'xsd:dateTime', '@confKey' => 'auth_user_{ID_USER}' ],// TODO: store last login time
  16. 'lastAuthCacheUpdate' => [ '@type' => 'xsd:dateTime', '@confKey' => 'acl_user_{ID_USER}_{ID_PROCES}_cache_update' ],
  17. // 'appFilterProces' => [ '@type' => 'xsd:integer' ],// TODO: filter proces id set by app (web app stores in session - create UserSession Storage)
  18. ]
  19. ];
  20. public $_rootTableName = 'CRM_CONFIG__#USER_CONF_VIEW';// 'CRM_CONFIG';
  21. public $idUser = null;
  22. public $idProcesFilter = null;
  23. public function __construct($simpleSchema = null) {
  24. parent::__construct($simpleSchema);
  25. $this->idUser = User::getID();// default - current user
  26. $this->idProcesFilter = 0;
  27. }
  28. public function setIdUser($idUser) {
  29. $this->idUser = intval($idUser);
  30. if ($this->idUser <= 0) throw new Exception("Missing id user");
  31. }
  32. public function getIdUser() { return $this->idUser; }
  33. public function setIdProcesFilter($idProcesFilter) { $this->idProcesFilter = intval($idProcesFilter); }
  34. public function getIdProcesFilter() { return $this->idProcesFilter; }
  35. public function getTotal($params = []) { return 1; }
  36. public function _parseSqlWhere($params = []) { return ''; }
  37. public function sqlFieldConfKey($fieldName) {
  38. if ('@' == substr($fieldName, 0, 1)) return null;
  39. if (!array_key_exists($fieldName, $this->_simpleSchema['root'])) return null;
  40. $field = $this->_simpleSchema['root'][$fieldName];
  41. if ('ID_USER' == $fieldName) return null;
  42. if ('ID_PROCES' == $fieldName) return null;
  43. if (empty($field['@confKey'])) return null;
  44. $confKey = $field['@confKey'];
  45. $confKey = str_replace('{ID_USER}', $this->idUser, $confKey);
  46. $confKey = str_replace('{ID_PROCES}', $this->idProcesFilter, $confKey);
  47. return $confKey;
  48. }
  49. public function updateItem($itemPatch) {
  50. DBG::log($itemPatch, 'array', '$itemPatch');
  51. $affected = 0;
  52. foreach ($itemPatch as $fieldName => $value) {
  53. if (!array_key_exists($fieldName, $this->_simpleSchema['root'])) throw new Exception("Field '{$fieldName}' not exists in '" . $this->getNamespace() . "'");
  54. $field = $this->_simpleSchema['root'][$fieldName];
  55. $confKey = $this->sqlFieldConfKey($fieldName);
  56. if ($confKey) {
  57. $sqlConfKey = DB::getPDO()->quote($confKey, PDO::PARAM_STR);
  58. $sqlValue = '';
  59. if ('NOW()' == $value) {
  60. $sqlValue = 'NOW()';
  61. } else if ('NULL' == $value) {
  62. $sqlValue = 'NULL';
  63. } else {
  64. $sqlValue = DB::getPDO()->quote($value, PDO::PARAM_STR);
  65. }
  66. $fieldAffected = DB::getPDO()->execSql("
  67. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  68. values ({$sqlConfKey}, {$sqlValue})
  69. on duplicate key update CONF_VAL = {$sqlValue}
  70. ");
  71. if ($fieldAffected > 0) $affected = 1;
  72. }
  73. }
  74. return $affected;
  75. }
  76. public function sqlSelect($params = []) {
  77. $select = [];
  78. $select[] = ['ID_USER', $this->idUser];
  79. $select[] = ['ID_PROCES', $this->idProcesFilter];
  80. foreach ($this->_simpleSchema['root'] as $fieldName => $conf) {
  81. if ('@' == substr($fieldName, 0, 1)) continue;
  82. $confKey = $this->sqlFieldConfKey($fieldName);
  83. if ($confKey) {
  84. $sqlLastLoginKey = DB::getPDO()->quote($confKey, PDO::PARAM_STR);
  85. $select[] = [$fieldName, "(select CONF_VAL from CRM_CONFIG where CONF_KEY = {$sqlLastLoginKey})"];
  86. }
  87. }
  88. return implode("\n\t, ", array_map(function ($s) { return "{$s[1]} as {$s[0]}"; }, $select));
  89. }
  90. public function getItem($primaryKey, $params = []) {
  91. $sqlSelect = $this->sqlSelect();
  92. return DB::getPDO()->fetchFirst("
  93. select {$sqlSelect}
  94. ");
  95. }
  96. public function getItems($params = []) {
  97. $sqlOrderBy = "";
  98. $sqlLimitOffset = "";
  99. $sqlWhereAnd = $this->_parseSqlWhere($params);
  100. $currSortCol = V::get('order_by', 'ID', $params);
  101. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  102. // TODO: validate $currSortCol is in field list
  103. // TODO: validate $currSortFlip ('asc' or 'desc')
  104. $aliasMap = array();
  105. foreach ($this->_simpleSchema['root'] as $key => $field) {
  106. if ('@' === substr($key, 0, 1)) continue;
  107. $aliasMap[ $key ] = (!empty($field['@alias'])) ? $field['@alias'] : $key;
  108. }
  109. // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort");
  110. $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null;
  111. if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) {
  112. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  113. }
  114. $limit = V::get('limit', 0, $params);
  115. $limit = ($limit < 0) ? 0 : $limit;
  116. $offset = V::get('limitstart', 0, $params);
  117. $offset = ($offset < 0) ? 0 : $offset;
  118. if ($limit > 0) $sqlLimitOffset = "limit {$limit} offset {$offset}";
  119. $sqlIdProces = ($this->idProcesFilter > 0) ? $this->idProcesFilter : 'NULL';
  120. $sqlWhereAndIdProces = ($this->idProcesFilter > 0) ? "and c.ID_PROCES = {$this->idProcesFilter}" : "and c.ID_PROCES is NULL";
  121. $items = [];
  122. $items[$this->idUser] = $this->getItem(1);
  123. // array_walk($items, function (&$item, $key) {
  124. // $item['link_uruchom_filtr_procesu'] = Request::getPathUri() . "index.php?FUNCTION_INIT=MENU_SELECT_PROCES&_action=setPermsByProces&id_proces={$item['ID']}";
  125. // });
  126. return $items;
  127. }
  128. }