FeatureAttrSelected.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class FeatureAttrSelected {
  3. static function select($typeName, array $listPrimaryKeys) {
  4. $idUser = User::getID();
  5. self::prepareUserTable($typeName, $idUser);
  6. $tableName = self::getAttributeTableName($typeName, $idUser);
  7. foreach ($listPrimaryKeys as $pk) {
  8. DB::getPDO()->execSql(" insert ignore `{$tableName}` (`primaryKey`) values ( :pk ) ", [ ':pk' => $pk ]);
  9. }
  10. }
  11. static function unselect($typeName, array $listPrimaryKeys) {
  12. $idUser = User::getID();
  13. self::prepareUserTable($typeName, $idUser);
  14. $tableName = self::getAttributeTableName($typeName, $idUser);
  15. foreach ($listPrimaryKeys as $pk) {
  16. DB::getPDO()->execSql(" delete from `{$tableName}` where `primaryKey` = :pk ", [ ':pk' => $pk ]);
  17. }
  18. }
  19. static function prepareUserTable($typeName, $idUser) {
  20. static $_created = [];
  21. $key = "{$typeName}-{$idUser}";
  22. if (!array_key_exists($key, $_created)) {
  23. self::_prepareUserTable($typeName, $idUser);
  24. $_created[$key] = true;
  25. }
  26. }
  27. static function _prepareUserTable($typeName, $idUser) {
  28. $tableName = self::getAttributeTableName($typeName, $idUser);
  29. // DB::getPDO()->execSql(" DROP TABLE IF EXISTS `{$tableName}` ");
  30. // TODO: primaryKey type from $acl
  31. DB::getPDO()->execSql("
  32. CREATE TABLE IF NOT EXISTS `{$tableName}` (
  33. `primaryKey` int(11) NOT NULL,
  34. UNIQUE KEY `primaryKey` (`primaryKey`)
  35. ) ENGINE=MyISAM DEFAULT CHARSET=latin2;
  36. ");
  37. }
  38. static function getAttributeTableName($typeName, $idUser) {
  39. static $_created = [];
  40. $key = "{$typeName}-{$idUser}";
  41. if (!array_key_exists($key, $_created)) {
  42. $_created[$key] = self::_getAttributeTableName($typeName, $idUser);
  43. }
  44. return $_created[$key];
  45. }
  46. static function _getAttributeTableName($typeName, $idUser) {
  47. $acl = ACL::getAclByTypeName($typeName);
  48. $rootTableName = $acl->getRootTableName();
  49. return "{$rootTableName}__@selected_{$idUser}";
  50. }
  51. }