ProjectKosztorysModel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class ProjectKosztorysModel {
  3. public $idProject;
  4. public $path;
  5. public $title;
  6. public $owner;
  7. public $ownerName;
  8. public $ownerByProjectId;
  9. public $subProjectList;
  10. public function __construct($idProject) {
  11. $this->idProject = $idProject;
  12. $this->fetchInfo($this->idProject);
  13. $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path);
  14. $this->fixSubProjectOwner($this->idProject, $this->owner, $this->ownerName, $this->ownerByProjectId);
  15. DBG::_('DBG', '>3', "this", $this, __CLASS__, __FUNCTION__, __LINE__);
  16. }
  17. public function getSubProjectIds() {
  18. if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path);
  19. return array_keys($this->subProjectList);
  20. }
  21. public function getSubProjectList() {
  22. if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path);
  23. return $this->subProjectList;
  24. }
  25. public function getProjectName($idProject) {
  26. if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path);
  27. if ($idProject == $this->idProject) return $this->title;
  28. if (!array_key_exists($idProject, $this->subProjectList)) return null;// TODO: throw exception or query for data?
  29. return $this->subProjectList[$idProject]['title'];
  30. }
  31. public function getSubProject($idSubProj) {
  32. if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path);
  33. return (array_key_exists($idSubProj, $this->subProjectList))? $this->subProjectList[$idSubProj] : null;
  34. }
  35. public function getPath() {
  36. if (null !== $this->path) return $this->path;
  37. $this->fetchInfo($this->idProject);
  38. return $this->path;
  39. }
  40. public function fetchInfo($idProject) {
  41. $rows = DB::getPDO()->fetchAll("
  42. select p.ID
  43. , p.path
  44. , p.P_ID
  45. , p.M_DIST_DESC as title
  46. , p.L_APPOITMENT_USER as owner
  47. , IF('' != p.L_APPOITMENT_USER
  48. , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1)
  49. , '') as ownerName
  50. from IN7_MK_BAZA_DYSTRYBUCJI p
  51. where p.ID = '{$idProject}'
  52. ");
  53. if (empty($rows)) throw new Exception("Cannot find path for project '{$idProject}'");
  54. $projectRaw = $rows[0];
  55. if (empty($projectRaw['path'])) throw new Exception("Empty path for project '{$idProject}'");
  56. $this->path = $projectRaw['path'];
  57. $this->title = $projectRaw['title'];
  58. $this->owner = $projectRaw['owner'];
  59. $this->ownerName = $projectRaw['ownerName'];
  60. $this->ownerByProjectId = null;
  61. if (empty($this->owner) && !empty($this->path)) {// find owner in parent projects
  62. $parentIds = explode('-', $this->path);
  63. if (0 == $parentIds[0]) array_shift($parentIds);
  64. $parentIds = array_reverse($parentIds);
  65. DBG::_('DBG', '>3', "parentIds", $parentIds, __CLASS__, __FUNCTION__, __LINE__);
  66. if (!empty($parentIds)) {
  67. $sqlIds = implode(",", $parentIds);
  68. $parentProjById = DB::getPDO()->fetchAllByKey("
  69. select p.ID
  70. , p.path
  71. , p.P_ID
  72. , p.M_DIST_DESC as title
  73. , p.L_APPOITMENT_USER as owner
  74. , IF('' != p.L_APPOITMENT_USER
  75. , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1)
  76. , '') as ownerName
  77. from IN7_MK_BAZA_DYSTRYBUCJI p
  78. where p.ID in({$sqlIds})
  79. ", $key = 'ID');
  80. //DBG::table("parentIds", $parentProjById, __CLASS__, __FUNCTION__, __LINE__);
  81. }
  82. foreach ($parentIds as $idParent) {
  83. if (!empty($parentProjById[$idParent])) {
  84. if (!empty($parentProjById[$idParent]['owner'])) {
  85. $this->owner = $parentProjById[$idParent]['owner'];
  86. $this->ownerName = $parentProjById[$idParent]['ownerName'];
  87. $this->ownerByProjectId = $idParent;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. public function fetchSubProjectList($idProject, $path) {
  95. $subProjectList = DB::getPDO()->fetchAllByKey("
  96. select p.ID, p.path, p.P_ID
  97. , p.M_DIST_DESC as title
  98. , p.L_APPOITMENT_USER as owner
  99. , IF('' != p.L_APPOITMENT_USER
  100. , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1)
  101. , '') as ownerName
  102. from IN7_MK_BAZA_DYSTRYBUCJI p
  103. where p.path like '{$path}-%'
  104. and p.A_STATUS not in('DELETED')
  105. ", 'ID');
  106. return $subProjectList;
  107. }
  108. public function fixSubProjectOwner($parentIdProject, $parentOwner, $parentOwnerName, $ownerByProjectId) {
  109. if (empty($this->owner)) return;
  110. if (empty($this->subProjectList)) return;
  111. foreach ($this->subProjectList as $idSubProj => $subProj) {
  112. if ($parentIdProject != $subProj['P_ID']) continue;
  113. if (!empty($subProj['owner'])) {
  114. $this->subProjectList[$idSubProj]['ownerByProjectId'] = $subProj['ID'];
  115. $this->fixSubProjectOwner($subProj['ID'], $subProj['owner'], $subProj['ownerName'], $subProj['ID']);
  116. continue;
  117. }
  118. $this->subProjectList[$idSubProj]['owner'] = $parentOwner;
  119. $this->subProjectList[$idSubProj]['ownerName'] = $parentOwnerName;
  120. $this->subProjectList[$idSubProj]['ownerByProjectId'] = $ownerByProjectId;
  121. $this->fixSubProjectOwner($subProj['ID'], $parentOwner, $parentOwnerName, $ownerByProjectId);
  122. }
  123. }
  124. }