diff --git a/module/execution/control.php b/module/execution/control.php index ff9790639d..58ad1fdd1f 100644 --- a/module/execution/control.php +++ b/module/execution/control.php @@ -1361,6 +1361,13 @@ class execution extends control if(!empty($_POST)) { + $oldPlans = $this->dao->select('plan')->from(TABLE_PROJECTPRODUCT)->where('project')->eq($executionID)->fetchPairs('plan'); + $oldPlanStories = $this->dao->select('t1.story')->from(TABLE_PROJECTSTORY)->alias('t1') + ->leftJoin(TABLE_PROJECTPRODUCT)->alias('t2')->on('t1.project=t2.project') + ->where('t1.project')->eq($executionID) + ->andWhere('t2.plan')->in(array_keys($oldPlans)) + ->fetchAll('story'); + $oldProducts = $this->execution->getProducts($executionID); $changes = $this->execution->update($executionID); if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); @@ -1386,6 +1393,16 @@ class execution extends control $actionID = $this->loadModel('action')->create($this->objectType, $executionID, 'edited', '', $products); $this->action->logHistory($actionID, $changes); } + + /* Link the plan stories. */ + $diffResult = array_diff($oldPlans, $_POST['plans']); + if(!empty($_POST['plans']) and !empty($diffResult)) + { + $projectID = $this->dao->select('project')->from(TABLE_EXECUTION)->where('id')->eq($executionID)->fetch('project'); + $this->loadModel('productplan')->linkProject($executionID, $_POST['plans'], $oldPlanStories); + $this->loadModel('productplan')->linkProject($projectID, $_POST['plans']); + } + $this->executeHooks($executionID); $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('view', "executionID=$executionID"))); } diff --git a/module/productplan/model.php b/module/productplan/model.php index 9b413759bd..3d78e0598a 100644 --- a/module/productplan/model.php +++ b/module/productplan/model.php @@ -577,6 +577,41 @@ class productplanModel extends model $this->loadModel('action')->create('bug', $bugID, 'unlinkedfromplan', '', $planID); } + /** + * Link project. + * + * @param int $projectID + * @param array $newPlans + * @param array $oldPlanStories + * @access public + * @return void + */ + public function linkProject($projectID, $newPlans, $oldPlanStories = '') + { + if(!empty($oldPlanStories)) $this->dao->delete()->from(TABLE_PROJECTSTORY)->where('project')->eq($projectID)->andWhere('story')->in(array_keys($oldPlanStories))->exec(); + + $this->loadModel('execution'); + foreach($newPlans as $planID) + { + $planStories = $planProducts = array(); + $planStory = $this->loadModel('story')->getPlanStories($planID); + if(!empty($planStory)) + { + foreach($planStory as $id => $story) + { + if($story->status == 'draft') + { + unset($planStory[$id]); + continue; + } + $planProducts[$story->id] = $story->product; + } + $planStories = array_keys($planStory); + $this->execution->linkStory($projectID, $planStories, $planProducts); + } + } + } + /** * Reorder for children plans. * diff --git a/module/project/control.php b/module/project/control.php index d379839dd4..c008174851 100644 --- a/module/project/control.php +++ b/module/project/control.php @@ -420,26 +420,7 @@ class project extends control /* Link the plan stories. */ if(!empty($_POST['plans']) and !empty($diffResult)) { - $this->dao->delete()->from(TABLE_PROJECTSTORY)->where('project')->eq($projectID)->andWhere('story')->in(array_keys($oldPlanStories))->exec(); - foreach($_POST['plans'] as $planID) - { - $planStories = $planProducts = array(); - $planStory = $this->loadModel('story')->getPlanStories($planID); - if(!empty($planStory)) - { - foreach($planStory as $id => $story) - { - if($story->status == 'draft') - { - unset($planStory[$id]); - continue; - } - $planProducts[$story->id] = $story->product; - } - $planStories = array_keys($planStory); - $this->execution->linkStory($projectID, $planStories, $planProducts); - } - } + $this->loadModel('productplan')->linkProject($projectID, $_POST['plans'], $oldPlanStories); } $locateLink = $this->session->projectList ? $this->session->projectList : inLink('view', "projectID=$projectID");