zentaomax.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Get not imported stories.
  4. *
  5. * @param array $libraries
  6. * @param int $libID
  7. * @param int $projectID
  8. * @param int $productID
  9. * @param string $orderBy
  10. * @param string $browseType
  11. * @param int $queryID
  12. * @access public
  13. * @return array
  14. */
  15. public function getNotImported($libraries, $libID, $projectID, $productID, $orderBy = 'id_desc', $browseType = '', $queryID = 0)
  16. {
  17. $query = '';
  18. if($browseType == 'bysearch')
  19. {
  20. if($queryID)
  21. {
  22. $this->session->set('projectstoryQuery', ' 1 = 1');
  23. $query = $this->loadModel('search')->getQuery($queryID);
  24. if($query)
  25. {
  26. $this->session->set('projectstoryQuery', $query->sql);
  27. $this->session->set('projectstoryForm', $query->form);
  28. }
  29. }
  30. else
  31. {
  32. if($this->session->projectstoryQuery == false) $this->session->set('projectstoryQuery', ' 1 = 1');
  33. }
  34. $query = $this->session->projectstoryQuery;
  35. $allLib = "`lib` = 'all'";
  36. $withAllLib = strpos($query, $allLib) !== false;
  37. if($withAllLib) $query = str_replace($allLib, 1, $query);
  38. if(!$withAllLib) $query .= " AND `lib` = '$libID'";
  39. }
  40. $project = $this->loadModel('project')->fetchByID($projectID);
  41. $stories = $this->dao->select('*')->from(TABLE_STORY)
  42. ->where('deleted')->eq(0)
  43. ->andWhere('status')->eq('active')
  44. ->andWhere('type')->in($project->storyType)
  45. ->andWhere('lib')->in(array_keys($libraries))
  46. ->beginIF($browseType != 'bysearch')->andWhere('lib')->eq($libID)->fi()
  47. ->beginIF($browseType == 'bysearch')->andWhere($query)->fi()
  48. ->orderBy($orderBy)
  49. ->fetchAll('id');
  50. $imported = $this->dao->select('t1.fromStory,t1.fromVersion')->from(TABLE_STORY)->alias('t1')
  51. ->leftJoin(TABLE_PROJECTSTORY)->alias('t2')->on('t1.id=t2.story')
  52. ->where('t1.lib')->eq(0)
  53. ->andWhere('t1.fromStory')->ne(0)
  54. ->andWhere('t1.product')->eq($productID)
  55. ->andWhere('t2.project')->eq($projectID)
  56. ->andWhere('t1.deleted')->eq(0)
  57. ->orderBy('t1.fromVersion_asc')
  58. ->fetchPairs();
  59. if(empty($imported)) return $stories;
  60. foreach($stories as $story)
  61. {
  62. if(!isset($imported[$story->id])) continue;
  63. if($story->version == $imported[$story->id]) unset($stories[$story->id]);
  64. }
  65. return $stories;
  66. }
  67. /**
  68. * Import from library.
  69. *
  70. * @param int $projectID
  71. * @param int $productID
  72. * @access public
  73. * @return void
  74. */
  75. public function importFromLib($projectID, $productID)
  76. {
  77. $this->loadModel('story');
  78. $this->loadModel('action');
  79. $data = fixer::input('post')->get();
  80. $storyIdList = $data->storyIdList;
  81. $branches = isset($data->branches) ? $data->branches : array();
  82. $storyList = $this->dao->select('*')->from(TABLE_STORY)->where('id')->in($storyIdList)->fetchAll('id');
  83. $lastOrder = $this->dao->select('*')->from(TABLE_PROJECTSTORY)->where('project')->eq($projectID)->orderBy('order_desc')->fetchPairs('story', 'order');
  84. $storyContent = $this->dao->select('story,spec,verify')->from(TABLE_STORYSPEC)->where('story')->in(array_values($storyIdList))->fetchAll('story');
  85. $project = $this->loadModel('project')->fetchByID($projectID);
  86. $now = helper::now();
  87. $idMap = array();
  88. foreach($storyList as $id => $story)
  89. {
  90. if(strpos($project->storyType, $story->type) === false) continue;
  91. $oldID = $story->id;
  92. $story->product = $productID;
  93. $story->branch = isset($branches[$id]) ? $branches[$id] : 0;
  94. $story->status = 'active';
  95. $story->fromVersion = $story->version;
  96. $story->version = 1;
  97. $story->fromStory = $story->id;
  98. $story->openedBy = $this->app->user->account;
  99. $story->openedDate = $now;
  100. $fromLib = $story->lib;
  101. $needUnsetFields = array('lib','id','lastEditedBy','lastEditedDate','assignedTo','assignedDate','approvedDate');
  102. foreach($needUnsetFields as $field) unset($story->$field);
  103. foreach($story as $field => $value)
  104. {
  105. if(strpos($field, 'Date') !== false && helper::isZeroDate($value)) unset($story->$field);
  106. }
  107. $this->dao->insert(TABLE_STORY)->data($story)->autoCheck()->exec();
  108. if(dao::isError()) return false;
  109. $storyID = $this->dao->lastInsertID();
  110. $idMap[$oldID] = $storyID;
  111. $data = new stdclass();
  112. $data->project = $projectID;
  113. $data->product = $productID;
  114. $data->story = $storyID;
  115. $data->version = 1;
  116. $data->order = reset($lastOrder) + 1;
  117. $this->dao->insert(TABLE_PROJECTSTORY)->data($data)->exec();
  118. $content = $storyContent[$id];
  119. $specData = new stdclass();
  120. $specData->story = $storyID;
  121. $specData->version = 1;
  122. $specData->title = $story->title;
  123. $specData->spec = $content->spec;
  124. $specData->verify = $content->verify;
  125. $this->dao->insert(TABLE_STORYSPEC)->data($specData)->exec();
  126. $this->action->create('story', $storyID, 'importfromstorylib', '', $fromLib);
  127. }
  128. $newStories = $this->loadModel('story')->getByList($idMap);
  129. foreach($newStories as $newStory)
  130. {
  131. $data = new stdclass();
  132. $data->parent = isset($idMap[$newStory->parent]) ? $idMap[$newStory->parent] : 0;
  133. $data->root = isset($idMap[$newStory->root]) ? $idMap[$newStory->root] : $newStory->id;
  134. $path = '';
  135. foreach(explode(',', $newStory->path) as $storyID)
  136. {
  137. if(!$storyID) continue;
  138. if(isset($idMap[$storyID])) $path .= ',' . $idMap[$storyID];
  139. }
  140. $data->path = trim($path, ',');
  141. $this->dao->update(TABLE_STORY)->data($data)->where('id')->eq($newStory->id)->exec();
  142. $this->story->setStage($newStory->id);
  143. }
  144. }