diff --git a/module/program/model.php b/module/program/model.php index 05a721e8cb..888fdba364 100644 --- a/module/program/model.php +++ b/module/program/model.php @@ -94,6 +94,61 @@ class programModel extends model ->fetchPairs('id', 'name'); } + /** + * Get product pairs which name with program. + * + * @param int $programID + * @param string $mode + * @param string $status + * @param string $append + * @param int $shadow + * @access public + * @return array + */ + public function getProductPairsWithProgram($programID = 0, $mode = 'assign', $status = 'all', $append = '', $shadow = 0) + { + /* Get the top programID. */ + if($programID) + { + $program = $this->getByID($programID); + $path = array_filter(explode(',', $program->path)); + $programID = current($path); + } + + /* When mode equals assign and programID equals 0, you can query the standalone product. */ + if(!empty($append) and is_array($append)) $append = implode(',', $append); + $views = empty($append) ? $this->app->user->view->products : $this->app->user->view->products . ",$append"; + + $products = $this->dao->select("t1.id, t1.program, concat(t2.name, '/', t1.name) AS name")->from(TABLE_PRODUCT)->alias('t1') + ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.program = t2.id') + ->where('t1.deleted')->eq('0') + ->andWhere('t1.vision')->eq($this->config->vision) + ->beginIF($shadow !== 'all')->andWhere('t1.shadow')->eq((int)$shadow)->fi() + ->beginIF($mode == 'assign')->andWhere('t1.program')->eq($programID)->fi() + ->beginIF(strpos($status, 'noclosed') !== false)->andWhere('t1.status')->ne('closed')->fi() + ->beginIF(!$this->app->user->admin)->andWhere('t1.id')->in($views)->fi() + ->fetchGroup('program'); + + /* Put products of current program first.*/ + if($mode != 'assign' && $programID) + { + $currentProgramProducts = $products[$programID]; + + unset($products[$programID]); + + array_unshift($products, $currentProgramProducts); + } + + $productPairs = array(); + foreach($products as $programProducts) + { + foreach($programProducts as $product) $productPairs[$product->id] = $product->name; + } + + return $productPairs; + } + + /** * Get program by id. * diff --git a/module/project/control.php b/module/project/control.php index 5b11495ee9..ce695c8a09 100755 --- a/module/project/control.php +++ b/module/project/control.php @@ -280,10 +280,6 @@ class project extends control $data['selectedProgramPath'] = explode(',', $selectedProgram->path); } - $allProducts = array(0 => '') + $this->program->getProductPairs(0, 'all', 'noclosed'); - $data['allProducts'] = html::select("products[0]", $allProducts, '', "class='form-control chosen' onchange='loadBranches(this)'"); - $data['plans'] = html::select('plans[][][]', '', '', 'class=\'form-control chosen\' multiple'); - /* Finish task #64882.Get the path of the last selected program. */ if(!empty($objectID)) $data['objectPath'] = explode(',', $object->path); if(isset($availableBudget)) $data['availableBudget'] = $availableBudget; @@ -603,7 +599,7 @@ class project extends control $this->view->users = $this->user->getPairs('noclosed|nodeleted'); $this->view->copyProjects = $this->project->getPairsByModel($model); $this->view->products = $products; - $this->view->allProducts = array('0' => '') + $this->program->getProductPairs(0, 'all', 'noclosed', '', $shadow); + $this->view->allProducts = array('0' => '') + $this->program->getProductPairsWithProgram(0, 'all', 'noclosed', '', $shadow); $this->view->productPlans = array('0' => '') + $productPlans; $this->view->branchGroups = $this->loadModel('branch')->getByProducts(array_keys($products), 'noclosed'); $this->view->programID = $programID; @@ -711,7 +707,7 @@ class project extends control $productPlans = array(0 => ''); $branches = $this->project->getBranchesByProject($projectID); $linkedProductIdList = empty($branches) ? '' : array_keys($branches); - $allProducts = $this->program->getProductPairs(0, 'all', 'noclosed', $linkedProductIdList); + $allProducts = $this->program->getProductPairsWithProgram($project->parent, 'all', 'noclosed'); $linkedProducts = $this->loadModel('product')->getProducts($projectID, 'all', '', true, $linkedProductIdList); $parentProject = $this->program->getByID($project->parent); $plans = $this->productplan->getGroupByProduct(array_keys($linkedProducts), 'skipParent|unexpired'); @@ -729,6 +725,7 @@ class project extends control { $linkedBranchList[$branchID] = $branchID; $linkedBranches[$productID][$branchID] = $branchID; + if($branch != BRANCH_MAIN) $productPlans[$productID][$branchID] = isset($plans[$productID][BRANCH_MAIN]) ? $plans[$productID][BRANCH_MAIN] : array(); $productPlans[$productID][$branchID] += isset($plans[$productID][$branchID]) ? $plans[$productID][$branchID] : array(); diff --git a/module/project/js/create.js b/module/project/js/create.js index a8eff3f81a..971c161ab0 100644 --- a/module/project/js/create.js +++ b/module/project/js/create.js @@ -135,23 +135,7 @@ function setParentProgram(parentProgram) selectedParent = parentProgram != 0 ? data.selectedProgramPath[1] : 0; lastSelectedParent = lastSelectedID != 0 ? data.objectPath[1] : 0; - if(selectedParent != lastSelectedParent) - { - $('#budget').val(''); - /* Hide product and plan dropdown controls. */ - $('#productsBox .row .col-sm-4:not(:last)').remove(); - $('#productsBox .row .col-sm-4:last select').remove(); - $('#productsBox .row .col-sm-4:last .chosen-container').remove(); - var select = data.allProducts; - $('#productsBox .row .col-sm-4 .input-group').prepend(select) - $('#productsBox .row .col-sm-4 .input-group select').chosen(); - - $('#plansBox .col-sm-4:not(:last)').remove(); - $('#plansBox .col-sm-4').children().remove(); - var planSelect = data.plans; - $('#plansBox .col-sm-4').prepend(planSelect); - $('#plansBox .col-sm-4 select').chosen(); - } + if(selectedParent != lastSelectedParent) $('#budget').val(''); if(parentProgram != 0) {