diff --git a/module/story/model.php b/module/story/model.php index 97804abbaf..eee0f8e49b 100644 --- a/module/story/model.php +++ b/module/story/model.php @@ -894,7 +894,7 @@ class storyModel extends model } /** - * update the story order of plan. + * Update the story order of plan. * * @param int $storyID * @param string $oldPlanIDList @@ -908,29 +908,23 @@ class storyModel extends model $oldPlanIDList = $oldPlanIDList ? explode(',', $oldPlanIDList) : array(); /* Get the ids to be inserted and deleted by comparing plan ids. */ - $insertedPlanIDList = array_diff($planIDList, $oldPlanIDList); - $deletedPlanIDList = array_diff($oldPlanIDList, $planIDList); + $plansTobeInsert = array_diff($planIDList, $oldPlanIDList); + $plansTobeDelete = array_diff($oldPlanIDList, $planIDList); /* Delete old story sort of plan. */ - if(!empty($deletedPlanIDList)) - { - foreach($deletedPlanIDList as $planID) - { - $this->dao->delete()->from(TABLE_PLANSTORY)->where('plan')->eq($planID)->andWhere('story')->eq($storyID)->exec(); - } - } + if(!empty($plansTobeDelete)) $this->dao->delete()->from(TABLE_PLANSTORY)->where('plan')->in($plansTobeDelete)->andWhere('story')->eq($storyID)->exec(); - if(!empty($insertedPlanIDList)) + if(!empty($plansTobeInsert)) { /* Get last story order of plan list. */ - $lastOrderOfPlans = $this->dao->select('plan, `order`')->from(TABLE_PLANSTORY)->where('plan')->in($insertedPlanIDList)->orderBy('order_asc')->fetchPairs(); + $maxOrders = $this->dao->select('plan, max(`order`) as `order`')->from(TABLE_PLANSTORY)->where('plan')->in($plansTobeInsert)->groupBy('plan')->fetchPairs(); - foreach($insertedPlanIDList as $planID) + foreach($plansTobeInsert as $planID) { /* Set story order in new plan. */ $data->plan = $planID; $data->story = $storyID; - $data->order = zget($lastOrderOfPlans, $planID, 0) + 1; + $data->order = zget($maxOrders, $planID, 0) + 1; $this->dao->replace(TABLE_PLANSTORY)->data($data)->exec(); }