idProject = $idProject; $this->fetchInfo($this->idProject); $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path); $this->fixSubProjectOwner($this->idProject, $this->owner, $this->ownerName, $this->ownerByProjectId); DBG::_('DBG', '>3', "this", $this, __CLASS__, __FUNCTION__, __LINE__); } public function getSubProjectIds() { if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path); return array_keys($this->subProjectList); } public function getSubProjectList() { if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path); return $this->subProjectList; } public function getProjectName($idProject) { if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path); if ($idProject == $this->idProject) return $this->title; if (!array_key_exists($idProject, $this->subProjectList)) return null;// TODO: throw exception or query for data? return $this->subProjectList[$idProject]['title']; } public function getSubProject($idSubProj) { if (null === $this->subProjectList) $this->subProjectList = $this->fetchSubProjectList($this->idProject, $this->path); return (array_key_exists($idSubProj, $this->subProjectList))? $this->subProjectList[$idSubProj] : null; } public function getPath() { if (null !== $this->path) return $this->path; $this->fetchInfo($this->idProject); return $this->path; } public function fetchInfo($idProject) { $rows = DB::getPDO()->fetchAll(" select p.ID , p.path , p.P_ID , p.M_DIST_DESC as title , p.L_APPOITMENT_USER as owner , IF('' != p.L_APPOITMENT_USER , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1) , '') as ownerName from IN7_MK_BAZA_DYSTRYBUCJI p where p.ID = '{$idProject}' "); if (empty($rows)) throw new Exception("Cannot find path for project '{$idProject}'"); $projectRaw = $rows[0]; if (empty($projectRaw['path'])) throw new Exception("Empty path for project '{$idProject}'"); $this->path = $projectRaw['path']; $this->title = $projectRaw['title']; $this->owner = $projectRaw['owner']; $this->ownerName = $projectRaw['ownerName']; $this->ownerByProjectId = null; if (empty($this->owner) && !empty($this->path)) {// find owner in parent projects $parentIds = explode('-', $this->path); if (0 == $parentIds[0]) array_shift($parentIds); $parentIds = array_reverse($parentIds); DBG::_('DBG', '>3', "parentIds", $parentIds, __CLASS__, __FUNCTION__, __LINE__); if (!empty($parentIds)) { $sqlIds = implode(",", $parentIds); $parentProjById = DB::getPDO()->fetchAllByKey(" select p.ID , p.path , p.P_ID , p.M_DIST_DESC as title , p.L_APPOITMENT_USER as owner , IF('' != p.L_APPOITMENT_USER , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1) , '') as ownerName from IN7_MK_BAZA_DYSTRYBUCJI p where p.ID in({$sqlIds}) ", $key = 'ID'); //DBG::table("parentIds", $parentProjById, __CLASS__, __FUNCTION__, __LINE__); } foreach ($parentIds as $idParent) { if (!empty($parentProjById[$idParent])) { if (!empty($parentProjById[$idParent]['owner'])) { $this->owner = $parentProjById[$idParent]['owner']; $this->ownerName = $parentProjById[$idParent]['ownerName']; $this->ownerByProjectId = $idParent; break; } } } } } public function fetchSubProjectList($idProject, $path) { $subProjectList = DB::getPDO()->fetchAllByKey(" select p.ID, p.path, p.P_ID , p.M_DIST_DESC as title , p.L_APPOITMENT_USER as owner , IF('' != p.L_APPOITMENT_USER , (select u.ADM_NAME from ADMIN_USERS u where u.ADM_ACCOUNT = p.L_APPOITMENT_USER limit 1) , '') as ownerName from IN7_MK_BAZA_DYSTRYBUCJI p where p.path like '{$path}-%' and p.A_STATUS not in('DELETED') ", 'ID'); return $subProjectList; } public function fixSubProjectOwner($parentIdProject, $parentOwner, $parentOwnerName, $ownerByProjectId) { if (empty($this->owner)) return; if (empty($this->subProjectList)) return; foreach ($this->subProjectList as $idSubProj => $subProj) { if ($parentIdProject != $subProj['P_ID']) continue; if (!empty($subProj['owner'])) { $this->subProjectList[$idSubProj]['ownerByProjectId'] = $subProj['ID']; $this->fixSubProjectOwner($subProj['ID'], $subProj['owner'], $subProj['ownerName'], $subProj['ID']); continue; } $this->subProjectList[$idSubProj]['owner'] = $parentOwner; $this->subProjectList[$idSubProj]['ownerName'] = $parentOwnerName; $this->subProjectList[$idSubProj]['ownerByProjectId'] = $ownerByProjectId; $this->fixSubProjectOwner($subProj['ID'], $parentOwner, $parentOwnerName, $ownerByProjectId); } } }