tao.php 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203
  1. <?php
  2. class convertTao extends convertModel
  3. {
  4. /**
  5. * 构建user数据。
  6. * Build user data.
  7. *
  8. * @param array $data
  9. * @access protected
  10. * @return object
  11. */
  12. protected function buildUserData($data)
  13. {
  14. $user = new stdclass();
  15. $user->id = $data['id'];
  16. $user->account = isset($data['lowerUserName']) ? $data['lowerUserName'] : '';
  17. $user->realname = isset($data['lowerDisplayName']) ? $data['lowerDisplayName'] : '';
  18. $user->email = isset($data['emailAddress']) ? $data['emailAddress'] : '';
  19. $user->join = isset($data['createdDate']) ? $data['createdDate'] : null;
  20. return $user;
  21. }
  22. /**
  23. * 构建project数据。
  24. * Build project data.
  25. *
  26. * @param array $data
  27. * @access protected
  28. * @return object
  29. */
  30. protected function buildProjectData($data)
  31. {
  32. $project = new stdclass();
  33. $project->id = $data['id'];
  34. $project->pname = isset($data['name']) ? $data['name'] : '';
  35. $project->pkey = isset($data['key']) ? $data['key'] : '';
  36. $project->originalkey = isset($data['originalkey']) ? $data['originalkey'] : '';
  37. $project->description = isset($data['description']) ? $data['description'] : '';
  38. $project->lead = isset($data['lead']) ? $data['lead'] : '';
  39. $project->pstatus = isset($data['status']) ? $data['status'] : '';
  40. $project->created = isset($data['created']) ? $data['created'] : null;
  41. $project->archived = isset($data['archived']) ? $data['archived'] : false;
  42. $project->versions = isset($data['versions']) ? $data['versions'] : array();
  43. return $project;
  44. }
  45. /**
  46. * 构建issue数据。
  47. * Build issue data.
  48. *
  49. * @param array $data
  50. * @access protected
  51. * @return object
  52. */
  53. protected function buildIssueData($data)
  54. {
  55. $issue = new stdclass();
  56. $issue->id = $data['id'];
  57. $issue->summary = isset($data['summary']) ? $data['summary'] : '';
  58. $issue->priority = isset($data['priority']) ? $data['priority'] : '';
  59. $issue->project = isset($data['project']) ? $data['project'] : 0;
  60. $issue->issuestatus = isset($data['status']) ? $data['status'] : '';
  61. $issue->created = isset($data['created']) ? $data['created'] : '';
  62. $issue->creator = isset($data['creator']) ? $data['creator'] : '';
  63. $issue->issuetype = isset($data['type']) ? $data['type'] : '';
  64. $issue->assignee = isset($data['assignee']) ? $data['assignee'] : '';
  65. $issue->resolution = isset($data['resolution']) ? $data['resolution'] : '';
  66. $issue->timeoriginalestimate = isset($data['timeoriginalestimate']) ? $data['timeoriginalestimate'] : '';
  67. $issue->timeestimate = isset($data['timeestimate']) ? $data['timeestimate'] : '';
  68. $issue->timespent = isset($data['timespent']) ? $data['timespent'] : '';
  69. $issue->issuenum = isset($data['number']) ? $data['number'] : '';
  70. $issue->description = isset($data['description']) ? $data['description'] : '';
  71. $issue->duedate = isset($data['duedate ']) ? $data['duedate'] : '';
  72. $issue->fixVersions = isset($data['fixVersions']) ? $data['fixVersions'] : array();
  73. $issue->comments = !empty($data['comments']) ? $data['comments'] : array();
  74. $issue->changeGroups = !empty($data['changeGroups']) ? $data['changeGroups'] : array();
  75. $issue->changeItems = !empty($data['changeItems']) ? $data['changeItems'] : array();
  76. $issue->worklogs = !empty($data['worklogs']) ? $data['worklogs'] : array();
  77. $issue->links = !empty($data['links']) ? $data['links'] : array();
  78. $issue->files = !empty($data['files']) ? $data['files'] : array();
  79. /* 获取自定义字段的值。 */
  80. foreach($data as $fieldKey => $fieldValue)
  81. {
  82. if(strpos($fieldKey, 'customfield_') === false) continue;
  83. $customFieldKey = str_replace('customfield_', '', $fieldKey);
  84. if(is_array($fieldValue))
  85. {
  86. if(!empty($fieldValue['content']) && !empty($data['renderedFields'][$fieldKey]))
  87. {
  88. $issue->{$customFieldKey} = $data['renderedFields'][$fieldKey];
  89. }
  90. elseif(!empty($fieldValue['id']))
  91. {
  92. $issue->{$customFieldKey} = $fieldValue['id'];
  93. }
  94. elseif(!empty($fieldValue['accountId']))
  95. {
  96. $issue->{$customFieldKey} = $fieldValue['accountId'];
  97. }
  98. else
  99. {
  100. $issue->{$customFieldKey} = '';
  101. foreach($fieldValue as $field)
  102. {
  103. if(!empty($field['id']))
  104. {
  105. $issue->{$customFieldKey} .= $field['id'] . ',';
  106. }
  107. elseif(!empty($field['accountId']))
  108. {
  109. $issue->{$customFieldKey} .= $field['accountId'] . ',';
  110. }
  111. }
  112. $issue->{$customFieldKey} = rtrim($issue->{$customFieldKey}, ',');
  113. }
  114. }
  115. else
  116. {
  117. $issue->{$customFieldKey} = $fieldValue;
  118. if(!empty($fieldValue) && preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{4}$/', (string)$fieldValue)) $issue->{$customFieldKey} = date('Y-m-d H:i:s', strtotime($fieldValue));
  119. }
  120. }
  121. return $issue;
  122. }
  123. /**
  124. * 构建issue类型数据。
  125. * Build issue type data.
  126. *
  127. * @param array $data
  128. * @access protected
  129. * @return object
  130. */
  131. protected function buildIssueTypeData($data)
  132. {
  133. $issueType = new stdclass();
  134. $issueType->id = $data['id'];
  135. $issueType->pname = $data['name'];
  136. $issueType->description = zget($data, 'description', '');
  137. $issueType->pstyle = zget($data, 'style', '');
  138. $issueType->iconurl = zget($data, 'iconurl', '');
  139. $issueType->avatar = zget($data, 'avatar', '');
  140. return $issueType;
  141. }
  142. /**
  143. * 构建issue关联关系数据。
  144. * Build issue link type data.
  145. *
  146. * @param array $data
  147. * @access protected
  148. * @return object
  149. */
  150. protected function buildIssueLinkTypeData($data)
  151. {
  152. $issueLinkType = new stdclass();
  153. $issueLinkType->id = $data['id'];
  154. $issueLinkType->linkname = zget($data, 'name', zget($data, 'outward', ''));
  155. $issueLinkType->inward = zget($data, 'inward', '');
  156. $issueLinkType->outward = zget($data, 'outward', '');
  157. $issueLinkType->pstyle = zget($data, 'style', '');
  158. return $issueLinkType;
  159. }
  160. /**
  161. * 构建解决方案数据。
  162. * Build resolution data.
  163. *
  164. * @param array $data
  165. * @access protected
  166. * @return object
  167. */
  168. protected function buildResolutionData($data)
  169. {
  170. $resolution = new stdclass();
  171. $resolution->id = $data['id'];
  172. $resolution->sequence = zget($data, 'sequence', '');
  173. $resolution->pname = $data['name'];
  174. $resolution->description = zget($data, 'description', '');
  175. return $resolution;
  176. }
  177. /**
  178. * 构建issue状态数据。
  179. * Build issue status data.
  180. *
  181. * @param array $data
  182. * @access protected
  183. * @return object
  184. */
  185. protected function buildStatusData($data)
  186. {
  187. $status = new stdclass();
  188. $status->id = $data['id'];
  189. $status->sequence = zget($data, 'sequence', '');
  190. $status->pname = $data['name'];
  191. $status->description = zget($data, 'description', '');
  192. return $status;
  193. }
  194. /**
  195. * 构建build数据。
  196. * Build build data.
  197. *
  198. * @param array $data
  199. * @access protected
  200. * @return object
  201. */
  202. protected function buildBuildData($data)
  203. {
  204. $build = new stdclass();
  205. $build->id = $data['id'];
  206. $build->project = isset($data['project']) ? $data['project'] : 0;
  207. $build->vname = isset($data['name']) ? $data['name'] : '';
  208. $build->startdate = isset($data['startdate']) ? $data['startdate'] : null;
  209. $build->releasedate = isset($data['releasedate']) ? $data['releasedate'] : '';
  210. $build->released = isset($data['released']) ? $data['released'] : '';
  211. $build->archived = isset($data['archived']) ? $data['archived'] : false;
  212. $build->description = isset($data['description']) ? $data['description'] : '';
  213. return $build;
  214. }
  215. /**
  216. * 构建issuelink数据。
  217. * Build issuelink data.
  218. *
  219. * @param array $data
  220. * @access protected
  221. * @return object
  222. */
  223. protected function buildIssuelinkData($data)
  224. {
  225. $issueLink = new stdclass();
  226. $issueLink->id = $data['id'];
  227. $issueLink->linktype = isset($data['linktype']) ? $data['linktype'] : '';
  228. $issueLink->source = isset($data['source']) ? $data['source'] : '';
  229. $issueLink->destination = isset($data['destination']) ? $data['destination'] : '';
  230. return $issueLink;
  231. }
  232. /**
  233. * 构建action数据。
  234. * Build action data.
  235. *
  236. * @param array $data
  237. * @access protected
  238. * @return object
  239. */
  240. protected function buildActionData($data)
  241. {
  242. $action = new stdclass();
  243. $action->id = $data['id'];
  244. $action->issueid = $data['issue'];
  245. $action->actionbody = isset($data['body']) ? $data['body'] : '';
  246. $action->author = isset($data['author']) ? $data['author'] : '';
  247. $action->created = isset($data['created']) ? $data['created'] : '';
  248. return $action;
  249. }
  250. /**
  251. * 构建file数据。
  252. * Build file data.
  253. *
  254. * @param array $data
  255. * @access protected
  256. * @return object
  257. */
  258. protected function buildFileData($data)
  259. {
  260. $file = new stdclass();
  261. $file->issueid = $data['issue'];
  262. $file->id = $data['id'];
  263. $file->filename = isset($data['filename']) ? $data['filename'] : '';
  264. $file->mimetype = isset($data['mimetype']) ? $data['mimetype'] : '';
  265. $file->filesize = isset($data['filesize']) ? $data['filesize'] : '';
  266. $file->created = isset($data['created']) ? $data['created'] : '';
  267. $file->author = isset($data['author']) ? $data['author'] : '';
  268. $file->content = isset($data['content']) ? $data['content'] : '';
  269. return $file;
  270. }
  271. /**
  272. * 构建优先级数据。
  273. * Build Priority data.
  274. *
  275. * @param array $data
  276. * @access protected
  277. * @return object
  278. */
  279. protected function buildPriorityData($data)
  280. {
  281. $priority = new stdclass();
  282. $priority->id = $data['id'];
  283. $priority->pname = isset($data['name']) ? $data['name'] : '';
  284. return $priority;
  285. }
  286. /**
  287. * 构建自定义字段数据。
  288. * Build custom field data.
  289. *
  290. * @param array $data
  291. * @access protected
  292. * @return object
  293. */
  294. protected function buildCustomFieldData($data)
  295. {
  296. $field = new stdclass();
  297. $field->id = $data['id'];
  298. $field->cfname = $data['name'];
  299. $field->description = isset($data['description']) ? $data['description'] : '';
  300. $field->customfieldtypekey = isset($data['customfieldtypekey']) ? $data['customfieldtypekey'] : '';
  301. $field->customfieldsearcherkey = isset($data['customfieldsearcherkey']) ? $data['customfieldsearcherkey'] : '';
  302. $field->issueTypeIds = isset($data['issueTypeIds']) ? $data['issueTypeIds'] : array();
  303. return $field;
  304. }
  305. /**
  306. * 构建工作流字段值数据。
  307. * Build custom field value data.
  308. *
  309. * @param array $data
  310. * @access protected
  311. * @return object
  312. */
  313. protected function buildCustomFieldValueData($data)
  314. {
  315. $fieldValue = new stdclass();
  316. $fieldValue->id = $data['id'];
  317. $fieldValue->issue = $data['issue'];
  318. $fieldValue->customfield = $data['customfield'];
  319. $fieldValue->stringvalue = zget($data, 'stringvalue', '');
  320. $fieldValue->datevalue = zget($data, 'datevalue', '');
  321. $fieldValue->numbervalue = zget($data, 'numbervalue', '');
  322. $fieldValue->textvalue = zget($data, 'textvalue', '');
  323. return $fieldValue;
  324. }
  325. /**
  326. * 构建工作流字段配置数据。
  327. * Build custom field option data.
  328. *
  329. * @param array $data
  330. * @access protected
  331. * @return object
  332. */
  333. protected function buildCustomFieldOptionData($data)
  334. {
  335. $fieldOption = new stdclass();
  336. $fieldOption->id = $data['id'];
  337. $fieldOption->customfield = $data['customfield'];
  338. $fieldOption->customfieldconfig = $data['customfieldconfig'];
  339. $fieldOption->customvalue = $data['value'];
  340. $fieldOption->disabled = $data['disabled'];
  341. return $fieldOption;
  342. }
  343. /**
  344. * 构建属性条目数据。
  345. * Build os property entry data.
  346. *
  347. * @param array $data
  348. * @access protected
  349. * @return object
  350. */
  351. protected function buildOSPropertyEntryData($data)
  352. {
  353. $property = new stdclass();
  354. $property->id = $data['id'];
  355. $property->entity_name = $data['entityName'];
  356. $property->entity_id = $data['entityId'];
  357. $property->property_key = $data['propertyKey'];
  358. $property->propertytype = $data['type'];
  359. return $property;
  360. }
  361. /**
  362. * 构建上下文数据。
  363. * Build configuration context data.
  364. *
  365. * @param array $data
  366. * @access protected
  367. * @return object
  368. */
  369. protected function buildConfigurationcontextData($data)
  370. {
  371. $config = new stdclass();
  372. $config->id = $data['id'];
  373. $config->customfield = isset($data['key']) ? $data['key'] : '';
  374. $config->fieldconfigscheme = isset($data['fieldconfigscheme']) ? $data['fieldconfigscheme'] : '';
  375. $config->project = isset($data['project']) ? $data['project'] : '';
  376. return $config;
  377. }
  378. /**
  379. * 构建配置选择数据。
  380. * Build option configuration data.
  381. *
  382. * @param array $data
  383. * @access protected
  384. * @return object
  385. */
  386. protected function buildOptionconfigurationData($data)
  387. {
  388. $option = new stdclass();
  389. $option->id = $data['id'];
  390. $option->fieldid = isset($data['fieldid']) ? $data['fieldid'] : '';
  391. $option->optionid = isset($data['optionid']) ? $data['optionid'] : '';
  392. $option->fieldconfig = isset($data['fieldconfig']) ? $data['fieldconfig'] : '';
  393. return $option;
  394. }
  395. /**
  396. * 构建日志数据。
  397. * Build audit log data.
  398. *
  399. * @param array $data
  400. * @access protected
  401. * @return object
  402. */
  403. protected function buildAuditLogData($data)
  404. {
  405. $auditLog = new stdclass();
  406. $auditLog->id = $data['id'];
  407. $auditLog->summary = isset($data['summary']) ? $data['summary'] : '';
  408. $auditLog->object_type = isset($data['objectType']) ? $data['objectType'] : '';
  409. $auditLog->object_id = isset($data['objectId']) ? $data['objectId'] : '';
  410. return $auditLog;
  411. }
  412. /**
  413. * 构建项目用户角色数据。
  414. * Build project role actor data.
  415. *
  416. * @param array $data
  417. * @access protected
  418. * @return object
  419. */
  420. protected function buildProjectRoleActorData($data)
  421. {
  422. $projectRole = new stdclass();
  423. $projectRole->id = $data['id'];
  424. $projectRole->pid = isset($data['pid']) ? $data['pid'] : '';
  425. $projectRole->roletype = $data['roletype'];
  426. $projectRole->roletypeparameter = $data['roletypeparameter'];
  427. return $projectRole;
  428. }
  429. /**
  430. * 构建角色成员数据。
  431. * Build member ship data.
  432. *
  433. * @param array $data
  434. * @access protected
  435. * @return object
  436. */
  437. protected function buildMemberShipData($data)
  438. {
  439. $memberShip = new stdclass();
  440. $memberShip->id = $data['id'];
  441. $memberShip->parent_id = $data['parentId'];
  442. $memberShip->child_id = $data['childId'];
  443. $memberShip->membership_type = $data['membershipType'];
  444. $memberShip->parent_name = $data['parentName'];
  445. $memberShip->child_name = $data['childName'];
  446. return $memberShip;
  447. }
  448. /**
  449. * 构建字段视图方案数据。
  450. * Build field screen layout data.
  451. *
  452. * @param array $data
  453. * @access protected
  454. * @return object
  455. */
  456. protected function buildFieldScreenLayoutItemData($data)
  457. {
  458. $layout = new stdclass();
  459. $layout->id = $data['id'];
  460. $layout->fieldidentifier = $data['fieldidentifier'];
  461. $layout->fieldscreentab = isset($data['fieldscreentab']) ? $data['fieldscreentab'] : '';
  462. return $layout;
  463. }
  464. /**
  465. * 构建字段配置数据。
  466. * Build field config scheme data.
  467. *
  468. * @param array $data
  469. * @access protected
  470. * @return object
  471. */
  472. protected function buildFieldConfigSchemeData($data)
  473. {
  474. $fieldConfig = new stdclass();
  475. $fieldConfig->id = $data['id'];
  476. $fieldConfig->configname = $data['name'];
  477. $fieldConfig->description = isset($data['description']) ? $data['description'] : '';
  478. $fieldConfig->fieldid = isset($data['fieldid']) ? $data['fieldid'] : '';
  479. return $fieldConfig;
  480. }
  481. /**
  482. * 构建字段方案类型数据。
  483. * Build field config scheme issue type data.
  484. *
  485. * @param array $data
  486. * @access protected
  487. * @return object
  488. */
  489. protected function buildFieldConfigSchemeIssueTypeData($data)
  490. {
  491. $fieldIssueType = new stdclass();
  492. $fieldIssueType->id = $data['id'];
  493. $fieldIssueType->issuetype = isset($data['issuetype']) ? $data['issuetype'] : '';
  494. $fieldIssueType->fieldconfigscheme = isset($data['fieldconfigscheme']) ? $data['fieldconfigscheme'] : '';
  495. $fieldIssueType->fieldconfiguration = isset($data['fieldconfiguration']) ? $data['fieldconfiguration'] : '';
  496. return $fieldIssueType;
  497. }
  498. /**
  499. * 构建工作流数据。
  500. * Build workflow data.
  501. *
  502. * @param array $data
  503. * @access protected
  504. * @return object
  505. */
  506. protected function buildWorkflowData($data)
  507. {
  508. $workflow = new stdclass();
  509. $workflow->id = $data['id'];
  510. $workflow->workflowname = $data['name'];
  511. $workflow->descriptor = $data['descriptor'];
  512. return $workflow;
  513. }
  514. /**
  515. * 构建工作流方案数据。
  516. * Build workflow scheme data.
  517. *
  518. * @param array $data
  519. * @access protected
  520. * @return object
  521. */
  522. protected function buildWorkflowSchemeData($data)
  523. {
  524. $workflow = new stdclass();
  525. $workflow->id = $data['id'];
  526. $workflow->name = $data['name'];
  527. $workflow->description = zget($data, 'description', '');
  528. return $workflow;
  529. }
  530. /**
  531. * 构建工作日志数据。
  532. * Build work log data.
  533. *
  534. * @param array $data
  535. * @access protected
  536. * @return object
  537. */
  538. protected function buildWorklogData($data)
  539. {
  540. $worklog = new stdclass();
  541. $worklog->id = $data['id'];
  542. $worklog->issueid = $data['issue'];
  543. $worklog->author = zget($data, 'author', '');
  544. $worklog->worklogbody = zget($data, 'body', '');
  545. $worklog->timeworked = zget($data, 'timeworked', 0);
  546. $worklog->created = zget($data, 'created', null);
  547. return $worklog;
  548. }
  549. /**
  550. * 构建变更记录数据。
  551. * Build change item data.
  552. *
  553. * @param array $data
  554. * @access protected
  555. * @return object
  556. */
  557. protected function buildChangeItemData($data)
  558. {
  559. $changeItem = new stdclass();
  560. $changeItem->id = $data['id'];
  561. $changeItem->groupid = $data['group'];
  562. $changeItem->fieldtype = $data['fieldtype'];
  563. $changeItem->field = $data['field'];
  564. $changeItem->oldvalue = zget($data, 'oldvalue', '');
  565. $changeItem->oldstring = zget($data, 'oldstring', '');
  566. $changeItem->newvalue = zget($data, 'newvalue', '');
  567. $changeItem->newstring = zget($data, 'newstring', '');
  568. return $changeItem;
  569. }
  570. /**
  571. * 构建变更记录分组数据。
  572. * Build change item group data.
  573. *
  574. * @param array $data
  575. * @access protected
  576. * @return object
  577. */
  578. protected function buildChangeGroupData($data)
  579. {
  580. $changeGroup = new stdclass();
  581. $changeGroup->id = $data['id'];
  582. $changeGroup->issueid = $data['issue'];
  583. $changeGroup->author = $data['author'];
  584. $changeGroup->created = $data['created'];
  585. return $changeGroup;
  586. }
  587. /**
  588. * 构建节点关联数据。
  589. * Build node association data.
  590. *
  591. * @param array $data
  592. * @access protected
  593. * @return object
  594. */
  595. protected function buildNodeAssociationData($data)
  596. {
  597. $association = new stdclass();
  598. $association->source_node_id = $data['sourceNodeId'];
  599. $association->source_node_entity = $data['sourceNodeEntity'];
  600. $association->sink_node_id = $data['sinkNodeId'];
  601. $association->sink_node_entity = $data['sinkNodeEntity'];
  602. $association->association_type = $data['associationType'];
  603. return $association;
  604. }
  605. /**
  606. * 构建版本发布数据。
  607. * Build fix version data.
  608. *
  609. * @param array $data
  610. * @access protected
  611. * @return object
  612. */
  613. protected function buildFixVersionData($data)
  614. {
  615. $fixVersion = new stdclass();
  616. $fixVersion->issue = $data['issue'];
  617. $fixVersion->version = $data['version'];
  618. return $fixVersion;
  619. }
  620. /**
  621. * 构建影响版本数据。
  622. * Build affects version data.
  623. *
  624. * @param array $data
  625. * @access protected
  626. * @return object
  627. */
  628. protected function buildAffectsVersionData($data)
  629. {
  630. $affectsVersion = new stdclass();
  631. $affectsVersion->issue = $data['issue'];
  632. $affectsVersion->version = $data['version'];
  633. return $affectsVersion;
  634. }
  635. /**
  636. * 获取Jira事务与禅道数据的关联关系。
  637. * Get Jira issue and ZenTao object links.
  638. *
  639. * @access protected
  640. * @return array
  641. */
  642. protected function getIssueData()
  643. {
  644. return $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('BID')->ne('')->andWhere('extra')->eq('issue')->fetchAll('AID');
  645. }
  646. /**
  647. * 获取Jira用户与禅道用户的关联关系。
  648. * Get Jira users.
  649. *
  650. * @access public
  651. * @return void
  652. */
  653. public function getJiraUser()
  654. {
  655. return $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('juser')->fetchPairs('AID', 'BID');
  656. }
  657. /**
  658. * 导入user数据。
  659. * Import jira user.
  660. *
  661. * @param array $dataList
  662. * @access protected
  663. * @return bool
  664. */
  665. protected function importJiraUser($dataList)
  666. {
  667. $this->loadModel('admin');
  668. $localUsers = $this->dao->dbh($this->dbh)->select('account')->from(TABLE_USER)->where('deleted')->eq('0')->fetchPairs();
  669. $userConfig = $this->session->jiraUser;
  670. $jiraUserRelation = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('juser')->fetchPairs();
  671. foreach($dataList as $data)
  672. {
  673. if(!empty($jiraUserRelation[$data->account])) continue;
  674. /* 如果是atlassian内部帐号,则不导入。 */
  675. if(isset($data->email) && strpos($data->email, '@connect.atlassian.com') !== false) continue;
  676. $user = new stdclass();
  677. $user->account = $this->processJiraUser($data->account, isset($data->email) ? $data->email : '');
  678. if(!isset($localUsers[$user->account]))
  679. {
  680. $user->realname = isset($data->realname) ? $data->realname : '';
  681. $user->password = $userConfig['password'];
  682. $user->email = isset($data->email) ? $data->email : '';
  683. $user->gender = 'm';
  684. $user->type = 'inside';
  685. $user->join = !empty($data->join) ? $data->join : helper::now();
  686. $user->jira = 1;
  687. $this->dao->dbh($this->dbh)->replace(TABLE_USER)->data($user, 'group')->exec();
  688. if(!dao::isError() && !empty($userConfig['group']))
  689. {
  690. $group = new stdclass();
  691. $group->account = $user->account;
  692. $group->group = $userConfig['group'];
  693. $group->project = '';
  694. $this->dao->dbh($this->dbh)->replace(TABLE_USERGROUP)->data($group)->exec();
  695. }
  696. }
  697. $jiraUserRelation[$data->account] = $user->account;
  698. $this->createTmpRelation('juser', $data->account, 'zuser', $user->account);
  699. }
  700. return true;
  701. }
  702. /**
  703. * 记录Jira数据和禅道数据的关联关系。
  704. * Create data tmp relation.
  705. *
  706. * @param string $AType
  707. * @param string|int $AID
  708. * @param string $BType
  709. * @param string|int $BID
  710. * @param string $extra
  711. * @access protected
  712. * @return object
  713. */
  714. protected function createTmpRelation($AType, $AID, $BType, $BID, $extra = '')
  715. {
  716. $relation = new stdclass();
  717. $relation->AType = $AType;
  718. $relation->BType = $BType;
  719. $relation->AID = $AID;
  720. $relation->BID = $BID;
  721. $relation->extra = $extra;
  722. $this->dao->dbh($this->dbh)->insert(JIRA_TMPRELATION)->data($relation)->exec();
  723. return $relation;
  724. }
  725. /**
  726. * 导入project数据。
  727. * Import jira project.
  728. *
  729. * @param array $dataList
  730. * @access protected
  731. * @return bool
  732. */
  733. protected function importJiraProject($dataList)
  734. {
  735. $this->app->loadConfig('execution');
  736. $this->app->loadLang('doc');
  737. $projectRoleActor = $this->getJiraProjectRoleActor($dataList);
  738. $archivedProject = $this->session->jiraMethod == 'api' ? array() : $this->getJiraArchivedProject($dataList);
  739. $sprintGroup = $this->getJiraSprint(array_keys($dataList));
  740. $jiraProjectRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jproject')->fetchAll('AID');
  741. foreach($dataList as $id => $data)
  742. {
  743. if(!empty($jiraProjectRelation[$id])) continue;
  744. if(isset($data->pstatus) && $data->pstatus == 'deleted')
  745. {
  746. $this->createTmpRelation('jproject', $id, 'zproject', 0);
  747. $this->createTmpRelation('jproject', $id, 'zproduct', 0);
  748. continue;
  749. }
  750. $data->status = in_array($data->id, $archivedProject) || !empty($data->archived) ? 'closed' : 'doing';
  751. $project = $this->createProject($data, $projectRoleActor);
  752. $executions = $this->createExecution($id, $project, $sprintGroup, $projectRoleActor);
  753. $productID = $this->createProduct($project, $executions);
  754. $systemID = $this->createSystem($productID);
  755. $this->createTmpRelation('jproject', $id, 'zproject', $project->id);
  756. $this->createTmpRelation('jproject', $id, 'zproduct', $productID);
  757. $this->createTmpRelation('joldkey', $data->originalkey, 'jnewkey', $data->pkey, $data->id);
  758. /* 如果是api方式,则在此刻插入版本信息。 */
  759. if($this->session->jiraMethod == 'api' && !empty($data->versions))
  760. {
  761. foreach($data->versions as $version)
  762. {
  763. $zentaoBuild = new stdclass();
  764. $zentaoBuild->id = $version['id'];
  765. $zentaoBuild->vname = $version['name'];
  766. $zentaoBuild->releasedate = isset($version['releaseDate']) ? $version['releaseDate'] : null;
  767. $zentaoBuild->archived = isset($version['archived']) ? $version['archived'] : 0;
  768. $zentaoBuild->released = isset($version['released']) ? $version['released'] : 0;
  769. $zentaoBuild->startdate = isset($version['startDate']) ? $version['startDate'] : null;
  770. $build = $this->createBuild((int)$productID, (int)$project->id, $systemID, $zentaoBuild, array(), array());
  771. $this->createRelease($build, $zentaoBuild, array(), array());
  772. $this->createTmpRelation('jversion', $version['id'], 'zversion', $build->id);
  773. }
  774. }
  775. }
  776. return true;
  777. }
  778. /**
  779. * 导入issue数据。
  780. * Import jira issue.
  781. *
  782. * @param array $dataList
  783. * @access protected
  784. * @return bool
  785. */
  786. protected function importJiraIssue($dataList)
  787. {
  788. $jiraRelation = $this->session->jiraRelation;
  789. $relations = $jiraRelation ? json_decode($jiraRelation, true) : array();
  790. $projectRelation = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jproject')->andWhere('BType')->eq('zproject')->fetchPairs();
  791. $productRelation = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jproject')->andWhere('BType')->eq('zproduct')->fetchPairs();
  792. $projectKeys = $this->dao->dbh($this->dbh)->select('extra,AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('joldkey')->andWhere('BType')->eq('jnewkey')->fetchAll('extra');
  793. $sprintRelation = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jsprint')->andWhere('BType')->eq('zexecution')->fetchPairs();
  794. $defaultExecution = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jproject')->andWhere('BType')->eq('zexecution')->fetchPairs();
  795. $issueList = $this->getIssueData();
  796. $jiraSprintList = $this->getJiraSprintIssue();
  797. $jiraActions = $this->getJiraWorkflowActions();
  798. $jiraStatusList = $this->getJiraStatusList();
  799. $customFields = $this->getJiraData($this->session->jiraMethod, 'customfield');
  800. $fieldValues = $this->getJiraData($this->session->jiraMethod, 'customfieldvalue');
  801. $fieldOptions = $this->getJiraData($this->session->jiraMethod, 'customfieldoption');
  802. $jiraResolutions = $this->getJiraData($this->session->jiraMethod, 'resolution');
  803. $jiraPriList = $this->getJiraData($this->session->jiraMethod, 'priority');
  804. $relations = $this->createWorkflow($relations, $jiraActions, $jiraResolutions, $jiraPriList, $jiraStatusList);
  805. $relations = $this->createWorkflowField($relations, $customFields, $fieldOptions, $jiraResolutions, $jiraPriList);
  806. $relations = $this->createWorkflowStatus($relations, $jiraStatusList);
  807. $relations = $this->createWorkflowAction($relations, $jiraActions);
  808. $relations = $this->createWorkflowGroup($relations, $projectRelation, $productRelation);
  809. $relations = $this->createResolution($relations);
  810. $workflows = $this->dao->dbh($this->dbh)->select('module,`table`')->from(TABLE_WORKFLOW)->where('buildin')->eq('0')->fetchPairs();
  811. $comments = $changeGroups = $changeItems = $worklogs = $files = $links = array();
  812. foreach($dataList as $id => $data)
  813. {
  814. if(!empty($issueList[$data->id])) continue;
  815. $issueProject = $data->project;
  816. if(!isset($projectRelation[$issueProject])) continue;
  817. $data->execution = !empty($jiraSprintList[$data->id]) ? $jiraSprintList[$data->id] : '';
  818. /* 将自定义字段数据赋值给issue对象。 */
  819. if($this->session->jiraMethod != 'api')
  820. {
  821. foreach($fieldValues as $fieldValue)
  822. {
  823. if($fieldValue->issue == $data->id)
  824. {
  825. if(!empty($fieldValue->datevalue))
  826. {
  827. $data->{$fieldValue->customfield} = $this->formatDatetime(date('Y-m-d H:i:s', strtotime($fieldValue->datevalue))); // 日期类型是datevalue
  828. }
  829. elseif(!empty($fieldValue->numbervalue))
  830. {
  831. $data->{$fieldValue->customfield} = $fieldValue->numbervalue; // 数字类型是numbervalue
  832. }
  833. elseif(!empty($fieldValue->textvalue))
  834. {
  835. $data->{$fieldValue->customfield} = $fieldValue->textvalue; // 文本类型是textvalue
  836. }
  837. elseif(isset($data->{$fieldValue->customfield}))
  838. {
  839. $data->{$fieldValue->customfield} .= ',' . $fieldValue->stringvalue; // 多选的情况
  840. }
  841. else
  842. {
  843. $data->{$fieldValue->customfield} = $fieldValue->stringvalue;
  844. }
  845. $fieldKey = !empty($customFields[$fieldValue->customfield]) ? $customFields[$fieldValue->customfield]->customfieldtypekey : '';
  846. if($fieldKey == 'com.pyxis.greenhopper.jira:gh-sprint' && !empty($sprintRelation[$fieldValue->stringvalue])) $data->execution = $sprintRelation[$fieldValue->stringvalue];
  847. }
  848. }
  849. }
  850. $projectID = $projectRelation[$issueProject];
  851. $productID = $productRelation[$issueProject];
  852. $executionID = !empty($data->execution) ? $data->execution : $defaultExecution[$issueProject];
  853. $zentaoObject = $relations['zentaoObject'][$data->issuetype];
  854. if($zentaoObject == 'requirement' || $zentaoObject == 'story' || $zentaoObject == 'epic')
  855. {
  856. $this->createStory((int)$productID, (int)$projectID, (int)$executionID, $zentaoObject, $data, $relations, $customFields);
  857. }
  858. elseif($zentaoObject == 'task')
  859. {
  860. $this->createTask((int)$projectID, (int)$executionID, $data, $relations, $customFields);
  861. }
  862. elseif($zentaoObject == 'bug')
  863. {
  864. $this->createBug((int)$productID, (int)$projectID, (int)$executionID, $data, $relations, $customFields);
  865. }
  866. elseif($zentaoObject == 'testcase')
  867. {
  868. $this->createCase((int)$productID, (int)$projectID, (int)$executionID, $data, $relations, $customFields);
  869. }
  870. elseif($zentaoObject == 'feedback')
  871. {
  872. $this->createFeedback((int)$productID, $data, $relations, $customFields);
  873. }
  874. elseif($zentaoObject == 'ticket')
  875. {
  876. $this->createTicket((int)$productID, $data, $relations, $customFields);
  877. }
  878. else
  879. {
  880. $this->createFlowData((int)$productID, (int)$projectID, $zentaoObject, $data, $relations, $customFields, $workflows);
  881. }
  882. $oldKey = zget($projectKeys[$issueProject], 'AID', '');
  883. $newKey = zget($projectKeys[$issueProject], 'BID', '');
  884. $issueKey = $oldKey ? $oldKey . '-' . $data->issuenum : $newKey . '-' . $data->issuenum;
  885. $this->createTmpRelation('jissueid', $data->id, 'jfilepath', '', "{$oldKey}/10000/{$issueKey}/");
  886. if($this->session->jiraMethod == 'api')
  887. {
  888. if(!empty($data->links)) $links = arrayUnion($links, $data->links);
  889. if(!empty($data->worklogs)) $worklogs = arrayUnion($worklogs, $data->worklogs);
  890. if(!empty($data->comments)) $comments = arrayUnion($comments, $data->comments);
  891. if(!empty($data->changeItems)) $changeItems = arrayUnion($changeItems, $data->changeItems);
  892. if(!empty($data->changeGroups)) $changeGroups = arrayUnion($changeGroups, $data->changeGroups);
  893. if(!empty($data->files)) $files = arrayUnion($files, $data->files);
  894. }
  895. }
  896. if($this->session->jiraMethod == 'api')
  897. {
  898. foreach($links as $index => $link) $links[$index] = $this->buildIssuelinkData($link);
  899. foreach($worklogs as $index => $worklog) $worklogs[$index] = $this->buildWorklogData($worklog);
  900. foreach($comments as $index => $comment) $comments[$index] = $this->buildActionData($comment);
  901. foreach($changeItems as $index => $item) $changeItems[$index] = $this->buildChangeItemData($item);
  902. foreach($changeGroups as $index => $group) $changeGroups[$index] = $this->buildChangeGroupData($group);
  903. foreach($files as $index => $file) $files[$index] = $this->buildFileData($file);
  904. $this->importJiraIssueLink($links);
  905. $this->importJiraWorkLog($worklogs);
  906. $this->importJiraAction($comments);
  907. $this->importJiraChangeItem($changeItems, $changeGroups);
  908. $this->importJiraFile($files);
  909. }
  910. return true;
  911. }
  912. /**
  913. * 导入版本数据。
  914. * Import jira build.
  915. *
  916. * @param array $dataList
  917. * @access protected
  918. * @return bool
  919. */
  920. protected function importJiraBuild($dataList)
  921. {
  922. $issueList = $this->getIssueData();
  923. $projectRelation = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)
  924. ->where('AType')->eq('jproject')
  925. ->andWhere('BType')->eq('zproject')
  926. ->fetchPairs();
  927. $projectProduct = $this->dao->dbh($this->dbh)->select('project,product')->from(TABLE_PROJECTPRODUCT)->where('project')->in(array_values($projectRelation))->fetchPairs();
  928. $productSystem = $this->dao->dbh($this->dbh)->select('id,product')->from(TABLE_SYSTEM)->fetchAll('product');
  929. $versionGroup = array();
  930. $nodeassociation = $this->getJiraData($this->session->jiraMethod, 'nodeassociation');
  931. $fixVersion = $this->getJiraData($this->session->jiraMethod, 'fixversion');
  932. $affectsVersion = $this->getJiraData($this->session->jiraMethod, 'affectsversion');
  933. foreach($nodeassociation as $node)
  934. {
  935. foreach($node as $key => $value)
  936. {
  937. $key = strtolower($key);
  938. $node->$key = $value;
  939. }
  940. if($node->sink_node_entity == 'Version' && $node->source_node_entity == 'Issue' && ($node->association_type == 'IssueFixVersion' || $node->association_type == 'IssueVersion'))
  941. {
  942. $data = new stdClass();
  943. $data->versionid = $node->sink_node_id;
  944. $data->issueid = $node->source_node_id;
  945. $data->relation = $node->association_type;
  946. $versionGroup[$node->sink_node_id][] = $data;
  947. }
  948. }
  949. foreach($fixVersion as $version)
  950. {
  951. $data = new stdClass();
  952. $data->versionid = $version->version;
  953. $data->issueid = $version->issue;
  954. $data->relation = 'IssueFixVersion';
  955. $versionGroup[$version->version][] = $data;
  956. }
  957. foreach($affectsVersion as $version)
  958. {
  959. $data = new stdClass();
  960. $data->versionid = $version->version;
  961. $data->issueid = $version->issue;
  962. $data->relation = 'IssueVersion';
  963. $versionGroup[$version->version][] = $data;
  964. }
  965. $versionRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jversion')->fetchAll('AID');
  966. foreach($dataList as $data)
  967. {
  968. if(!empty($versionRelation[$data->id])) continue;
  969. $buildProject = $data->project;
  970. if(empty($projectRelation[$buildProject])) continue;
  971. $projectID = $projectRelation[$buildProject];
  972. $productID = $projectProduct[$projectID];
  973. $system = !empty($productSystem[$productID]) ? $productSystem[$productID]->id : 0;
  974. $build = $this->createBuild((int)$productID, (int)$projectID, $system, $data, $versionGroup, $issueList);
  975. $this->createRelease($build, $data, !empty($versionGroup[$data->id]) ? $versionGroup[$data->id] : array(), $issueList);
  976. $this->createTmpRelation('jversion', $data->id, 'zbuild', $build->id);
  977. }
  978. return true;
  979. }
  980. /**
  981. * 导入issue link数据。
  982. * Import jira issue link.
  983. *
  984. * @param array $dataList
  985. * @access protected
  986. * @return bool
  987. */
  988. protected function importJiraIssueLink($dataList)
  989. {
  990. $issueLinkTypeList = array();
  991. $issueList = $this->getIssueData();
  992. $jiraRelation = $this->session->jiraRelation;
  993. $relations = $jiraRelation ? json_decode($jiraRelation, true) : array();
  994. if($this->config->edition != 'open') $relations = $this->createRelation($relations);
  995. $storyLink = $taskLink = $duplicateLink = $relatesLink = array();
  996. $issueLinkTypeList = zget($relations, 'zentaoLinkType', array());
  997. foreach($dataList as $issueLink)
  998. {
  999. $linkType = $issueLink->linktype;
  1000. if($linkType == 'jiraSubTask') $taskLink[$issueLink->source][] = $issueLink->destination;
  1001. if(empty($issueLinkTypeList[$linkType])) continue;
  1002. if($issueLinkTypeList[$linkType] == 'subStoryLink') $storyLink[$issueLink->source][] = $issueLink->destination;
  1003. if($issueLinkTypeList[$linkType] == 'subTaskLink') $taskLink[$issueLink->source][] = $issueLink->destination;
  1004. if($issueLinkTypeList[$linkType] == 'duplicate') $duplicateLink[$issueLink->source] = $issueLink->destination;
  1005. if($issueLinkTypeList[$linkType] == 'relates') $relatesLink[$issueLink->source] = $issueLink->destination;
  1006. }
  1007. $this->updateSubStory($storyLink, $issueList);
  1008. $this->updateSubTask($taskLink, $issueList);
  1009. $this->updateDuplicateStoryAndBug($duplicateLink, $issueList);
  1010. $this->updateRelatesObject($relatesLink, $issueList);
  1011. if($this->config->edition != 'open') $this->updateObjectRelation($issueLinkTypeList, $dataList, $issueList);
  1012. return true;
  1013. }
  1014. /**
  1015. * 导入工作日志数据。
  1016. * Import jira worklog.
  1017. *
  1018. * @param array $dataList
  1019. * @access protected
  1020. * @return bool
  1021. */
  1022. protected function importJiraWorkLog($dataList)
  1023. {
  1024. $issueList = $this->getIssueData();
  1025. $worklogRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jworklog')->fetchAll('AID');
  1026. foreach($dataList as $data)
  1027. {
  1028. if(!empty($worklogRelation[$data->id])) continue;
  1029. $issueID = $data->issueid;
  1030. if(!isset($issueList[$issueID])) continue;
  1031. $objectType = zget($issueList[$issueID], 'BType', '');
  1032. $objectID = zget($issueList[$issueID], 'BID', '');
  1033. if(empty($objectID)) continue;
  1034. $effort = new stdclass();
  1035. $effort->vision = $this->config->vision;
  1036. $effort->objectID = $objectID;
  1037. $effort->date = !empty($data->created) ? $this->formatDate(substr($data->created, 0, 10)) : null;
  1038. $effort->account = $this->getJiraAccount(isset($data->author) ? $data->author : '');
  1039. $effort->consumed = round($data->timeworked / 3600, 2);
  1040. $effort->work = $data->worklogbody;
  1041. $effort->objectType = substr($objectType, 1);
  1042. $this->dao->dbh($this->dbh)->insert(TABLE_EFFORT)->data($effort)->exec();
  1043. $effortID = $this->dao->dbh($this->dbh)->lastInsertID();
  1044. $this->createTmpRelation('jworklog', $data->id, 'zeffort', $effortID);
  1045. }
  1046. return true;
  1047. }
  1048. /**
  1049. * 导入issue评论数据。
  1050. * Import jira action.
  1051. *
  1052. * @param array $dataList
  1053. * @access protected
  1054. * @return bool
  1055. */
  1056. protected function importJiraAction($dataList)
  1057. {
  1058. $issueList = $this->getIssueData();
  1059. $actionRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jaction')->fetchAll('AID');
  1060. foreach($dataList as $data)
  1061. {
  1062. if(!empty($actionRelation[$data->id])) continue;
  1063. $issueID = $data->issueid;
  1064. $comment = $data->actionbody;
  1065. if(empty($comment)) continue;
  1066. if(!isset($issueList[$issueID])) continue;
  1067. $objectType = zget($issueList[$issueID], 'BType', '');
  1068. $objectID = zget($issueList[$issueID], 'BID', '');
  1069. if(empty($objectID)) continue;
  1070. $comment = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $comment);
  1071. $action = new stdclass();
  1072. $action->objectType = substr($objectType, 1);
  1073. $action->objectID = $objectID;
  1074. $action->actor = $this->getJiraAccount(isset($data->author) ? $data->author : '');
  1075. $action->action = 'commented';
  1076. $action->date = isset($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : '';
  1077. $action->comment = $comment;
  1078. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1079. $actionID = $this->dao->dbh($this->dbh)->lastInsertID();
  1080. $this->createTmpRelation('jaction', $data->id, 'zaction', $actionID);
  1081. }
  1082. return true;
  1083. }
  1084. /**
  1085. * 导入issue变更记录。
  1086. * Import jira issue change item.
  1087. *
  1088. * @param array $dataList
  1089. * @param array $changeGroups
  1090. * @access protected
  1091. * @return bool
  1092. */
  1093. protected function importJiraChangeItem($dataList, $changeGroups = array())
  1094. {
  1095. $issueList = $this->getIssueData();
  1096. $changeGroup = $changeGroups ?: $this->getJiraData($this->session->jiraMethod, 'changegroup');
  1097. $changeRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jchangeitem')->fetchAll('AID');
  1098. foreach($dataList as $data)
  1099. {
  1100. if(!empty($changeRelation[$data->id])) continue;
  1101. $group = $changeGroup[$data->groupid];
  1102. $issueID = $group->issueid;
  1103. if(!isset($issueList[$issueID])) continue;
  1104. $objectType = zget($issueList[$issueID], 'BType', '');
  1105. $objectID = zget($issueList[$issueID], 'BID', '');
  1106. if(empty($objectID)) continue;
  1107. $action = new stdclass();
  1108. $action->objectType = substr($objectType, 1);
  1109. $action->objectID = $objectID;
  1110. $action->actor = $this->getJiraAccount(isset($group->author) ? $group->author : '');
  1111. $action->action = 'commented';
  1112. $action->date = isset($group->created) ? $this->formatDatetime(substr($group->created, 0, 19)) : '';
  1113. $action->comment = sprintf($this->lang->convert->jira->changeItems, $data->field, $data->oldstring, $data->newstring);
  1114. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1115. $actionID = $this->dao->dbh($this->dbh)->lastInsertID();
  1116. $this->createTmpRelation('jchangeitem', $data->id, 'zaction', $actionID);
  1117. }
  1118. return true;
  1119. }
  1120. /**
  1121. * 导入file数据。
  1122. * Import jira file.
  1123. *
  1124. * @param array $dataList
  1125. * @access protected
  1126. * @return bool
  1127. */
  1128. protected function importJiraFile($dataList)
  1129. {
  1130. if($this->session->jiraMethod == 'api')
  1131. {
  1132. $this->app->loadClass('jira', true);
  1133. $jiraApi = json_decode($this->session->jiraApi, true);
  1134. $jiraApi = new jira($jiraApi['domain'], $jiraApi['admin'], $jiraApi['token']);
  1135. }
  1136. $this->loadModel('file');
  1137. $issueList = $this->getIssueData();
  1138. $filePaths = $this->dao->dbh($this->dbh)->select('AID,extra')->from(JIRA_TMPRELATION)
  1139. ->where('AType')->eq('jissueid')
  1140. ->andWhere('BType')->eq('jfilepath')
  1141. ->fetchPairs();
  1142. $fileRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jfile')->fetchAll('AID');
  1143. foreach($dataList as $fileAttachment)
  1144. {
  1145. if(!empty($fileRelation[$fileAttachment->id])) continue;
  1146. $issueID = $fileAttachment->issueid;
  1147. if(!isset($issueList[$issueID])) continue;
  1148. $objectType = zget($issueList[$issueID], 'BType', '');
  1149. $objectID = zget($issueList[$issueID], 'BID', '');
  1150. if(empty($objectID)) continue;
  1151. $fileID = $fileAttachment->id;
  1152. $fileName = $fileAttachment->filename;
  1153. $parts = explode('.', $fileName);
  1154. $extension = array_pop($parts);
  1155. $pathname = $this->file->setPathName((int)$fileID, $extension);
  1156. if($this->session->jiraMethod == 'api')
  1157. {
  1158. $fileContent = $jiraApi->downloadFile($fileAttachment->content);
  1159. file_put_contents($this->file->savePath . $pathname, $fileContent);
  1160. if(!is_file($this->file->savePath . $pathname) || filesize($this->file->savePath . $pathname) == 0) continue;
  1161. }
  1162. else
  1163. {
  1164. $jiraFile = $this->app->getTmpRoot() . 'attachments/' . $filePaths[$issueID] . $fileID;
  1165. if(!is_file($jiraFile)) continue;
  1166. }
  1167. $file = new stdclass();
  1168. $file->pathname = $pathname;
  1169. $file->title = $fileName;
  1170. $file->extension = substr($extension, 0, 30); // 防止超长报错
  1171. $file->size = $fileAttachment->filesize;
  1172. $file->objectType = substr($objectType, 1);
  1173. $file->objectID = $objectID;
  1174. $file->extra = in_array($file->objectType, array('epic', 'requirement', 'story')) ? '1' : '';
  1175. $file->addedBy = $this->getJiraAccount(isset($fileAttachment->author) ? $fileAttachment->author : '');
  1176. $file->addedDate = !empty($fileAttachment->created) ? $this->formatDatetime(substr($fileAttachment->created, 0, 19)) : null;
  1177. $this->dao->dbh($this->dbh)->insert(TABLE_FILE)->data($file)->exec();
  1178. $fileID = $this->dao->dbh($this->dbh)->lastInsertID();
  1179. if($this->session->jiraMethod != 'api') copy($jiraFile, $this->file->savePath . $file->pathname);
  1180. if(in_array($file->objectType, array('epic', 'requirement', 'story')))
  1181. {
  1182. $this->dao->dbh($this->dbh)->update(TABLE_STORYSPEC)->set("files=IF(files IS NOT NULL, CONCAT(files,',{$fileID},'), '{$fileID}')")->where('story')->eq($file->objectID)->andWhere('version')->eq($file->extra)->exec();
  1183. }
  1184. $this->createTmpRelation('jfile', $fileAttachment->id, 'zfile', $fileID);
  1185. }
  1186. $this->processJiraIssueContent($issueList);
  1187. return true;
  1188. }
  1189. /**
  1190. * 创建团队成员。
  1191. * Create team member.
  1192. *
  1193. * @param int $objectID
  1194. * @param string $createdBy
  1195. * @param string $type
  1196. * @access protected
  1197. * @return bool
  1198. */
  1199. protected function createTeamMember($objectID, $createdBy, $type)
  1200. {
  1201. if(!$createdBy) return false;
  1202. $member = new stdclass();
  1203. $member->root = $objectID;
  1204. $member->account = $createdBy;
  1205. $member->role = '';
  1206. $member->join = helper::now();
  1207. $member->type = $type;
  1208. $member->days = 0;
  1209. $member->hours = $this->config->execution->defaultWorkhours;
  1210. $this->dao->dbh($this->dbh)->replace(TABLE_TEAM)->data($member)->exec();
  1211. return true;
  1212. }
  1213. /**
  1214. * 为项目或者迭代创建默认的文档库。
  1215. * Create doc lib.
  1216. *
  1217. * @param int $productID
  1218. * @param int $projectID
  1219. * @param int $executionID
  1220. * @param string $name
  1221. * @param string $type
  1222. * @access protected
  1223. * @return boll
  1224. */
  1225. protected function createDocLib($productID, $projectID, $executionID, $name, $type)
  1226. {
  1227. $docLib = new stdclass();
  1228. $docLib->product = $productID;
  1229. $docLib->project = $projectID;
  1230. $docLib->execution = $executionID;
  1231. $docLib->name = $name;
  1232. $docLib->type = $type;
  1233. $docLib->main = '1';
  1234. $docLib->acl = 'default';
  1235. $this->dao->dbh($this->dbh)->insert(TABLE_DOCLIB)->data($docLib)->exec();
  1236. return true;
  1237. }
  1238. /**
  1239. * 创建项目。
  1240. * Create project.
  1241. *
  1242. * @param object $data
  1243. * @param array $projectRoleActor
  1244. * @access protected
  1245. * @return object
  1246. */
  1247. protected function createProject($data, $projectRoleActor)
  1248. {
  1249. /* Create project. */
  1250. $project = new stdclass();
  1251. $project->name = substr($data->pname, 0, 90);
  1252. $project->code = $data->pkey;
  1253. $project->desc = isset($data->description) ? $data->description : '';
  1254. $project->status = $data->status;
  1255. $project->type = 'project';
  1256. $project->model = 'scrum';
  1257. $project->grade = 1;
  1258. $project->acl = 'open';
  1259. $project->auth = 'extend';
  1260. $project->begin = !empty($data->created) ? $this->formatDate(substr($data->created, 0, 10)) : helper::now();
  1261. $project->end = date('Y-m-d', time() + 30 * 24 * 3600);
  1262. $project->days = helper::diffDate($project->end, $project->begin) + 1;
  1263. $project->PM = $this->getJiraAccount(isset($data->lead) ? $data->lead : '');
  1264. $project->openedBy = $this->getJiraAccount(isset($data->lead) ? $data->lead : '');
  1265. $project->openedDate = helper::now();
  1266. $project->openedVersion = $this->config->version;
  1267. $project->storyType = 'story,epic,requirement';
  1268. $this->dao->dbh($this->dbh)->insert(TABLE_PROJECT)->data($project)->exec();
  1269. $projectID = $this->dao->dbh($this->dbh)->lastInsertID();
  1270. $this->loadModel('action')->create('project', $projectID, 'opened');
  1271. $this->dao->dbh($this->dbh)->update(TABLE_PROJECT)->set('`order`')->eq($projectID * 5)->set('path')->eq(",$projectID,")->where('id')->eq($projectID)->exec();
  1272. $this->createTeamMember($projectID, $project->openedBy, 'project');
  1273. /* 如果Jira项目有团队成员。 */
  1274. if(!empty($projectRoleActor[$data->id]))
  1275. {
  1276. foreach($projectRoleActor[$data->id] as $userID)
  1277. {
  1278. $account = $this->getJiraAccount($userID);
  1279. if(!$account) continue;
  1280. if($account == $project->openedBy) continue;
  1281. $this->createTeamMember($projectID, $account, 'project');
  1282. }
  1283. }
  1284. $this->createDocLib(0, $projectID, 0, $this->lang->doclib->main['project'], 'project');
  1285. $project->id = $projectID;
  1286. return $project;
  1287. }
  1288. /**
  1289. * 为项目创建默认的同名执行。
  1290. * Create default execution.
  1291. *
  1292. * @param int $jiraProjectID
  1293. * @param object $project
  1294. * @param array $projectRoleActor
  1295. * @access protected
  1296. * @return int
  1297. */
  1298. protected function createDefaultExecution($jiraProjectID, $project, $projectRoleActor)
  1299. {
  1300. /* Create default execution. */
  1301. $execution = new stdclass();
  1302. $execution->name = $project->name;
  1303. $execution->code = $project->code;
  1304. $execution->desc = $project->desc;
  1305. $execution->status = 'wait';
  1306. $execution->project = $project->id;
  1307. $execution->parent = $project->id;
  1308. $execution->grade = 1;
  1309. $execution->type = 'sprint';
  1310. $execution->acl = 'open';
  1311. $execution->begin = $project->begin;
  1312. $execution->end = $project->end;
  1313. $execution->days = helper::diffDate($execution->end, $execution->begin) + 1;
  1314. $execution->PM = $project->PM;
  1315. $execution->openedBy = $project->openedBy;
  1316. $execution->openedDate = helper::now();
  1317. $execution->openedVersion = $this->config->version;
  1318. $this->dao->dbh($this->dbh)->insert(TABLE_PROJECT)->data($execution)->exec();
  1319. $executionID = $this->dao->dbh($this->dbh)->lastInsertID();
  1320. $this->loadModel('action')->create('execution', $executionID, 'opened');
  1321. $this->dao->dbh($this->dbh)->update(TABLE_PROJECT)->set('`order`')->eq($executionID * 5)->set('path')->eq(",{$project->id},$executionID,")->where('id')->eq($executionID)->exec();
  1322. $this->createTeamMember($executionID, $execution->openedBy, 'execution');
  1323. /* 如果Jira项目有团队成员。 */
  1324. if(!empty($projectRoleActor[$jiraProjectID]))
  1325. {
  1326. foreach($projectRoleActor[$jiraProjectID] as $userID)
  1327. {
  1328. $account = $this->getJiraAccount($userID);
  1329. if(!$account) continue;
  1330. if($account == $execution->openedBy) continue;
  1331. $this->createTeamMember($executionID, $account, 'execution');
  1332. }
  1333. }
  1334. $this->createDocLib(0, $project->id, $executionID, $this->lang->doclib->main['execution'], 'execution');
  1335. $this->createTmpRelation('jproject', $jiraProjectID, 'zexecution', $executionID);
  1336. return $executionID;
  1337. }
  1338. /**
  1339. * 创建执行。
  1340. * Create execution.
  1341. *
  1342. * @param int $jiraProjectID
  1343. * @param object $project
  1344. * @param array $sprintGroup
  1345. * @param array $projectRoleActor
  1346. * @access protected
  1347. * @return array
  1348. */
  1349. protected function createExecution($jiraProjectID, $project, $sprintGroup, $projectRoleActor)
  1350. {
  1351. $executions = array();
  1352. $executions[] = $this->createDefaultExecution($jiraProjectID, $project, $projectRoleActor);
  1353. if(!empty($sprintGroup[$jiraProjectID]))
  1354. {
  1355. $zentaoStatus = array('future' => 'wait', 'active' => 'doing', 'closed' => 'closed');
  1356. foreach($sprintGroup[$jiraProjectID] as $sprint)
  1357. {
  1358. /* Create execution. */
  1359. $execution = new stdclass();
  1360. $execution->name = $sprint->name;
  1361. $execution->code = 'jirasprint' . $sprint->id;
  1362. $execution->status = isset($sprint->state) ? zget($zentaoStatus, $sprint->state, 'wait') : 'wait';
  1363. $execution->project = $project->id;
  1364. $execution->parent = $project->id;
  1365. $execution->grade = 1;
  1366. $execution->desc = !empty($sprint->goal) ? $sprint->goal : '';
  1367. $execution->type = 'sprint';
  1368. $execution->acl = 'open';
  1369. $execution->begin = !empty($sprint->startDate) ? $this->formatDate(substr($sprint->startDate, 0, 10)) : helper::now();
  1370. $execution->end = !empty($sprint->endDate) ? $this->formatDate(substr($sprint->endDate, 0, 10)) : date('Y-m-d', time() + 24 * 3600);
  1371. $execution->days = helper::diffDate($execution->end, $execution->begin) + 1;
  1372. $execution->PM = $project->PM;
  1373. $execution->openedBy = $project->openedBy;
  1374. $execution->openedDate = helper::now();
  1375. $execution->openedVersion = $this->config->version;
  1376. $this->dao->dbh($this->dbh)->insert(TABLE_PROJECT)->data($execution)->exec();
  1377. $executionID = $this->dao->dbh($this->dbh)->lastInsertID();
  1378. $this->loadModel('action')->create('execution', $executionID, 'opened');
  1379. $this->dao->dbh($this->dbh)->update(TABLE_PROJECT)->set('`order`')->eq($executionID * 5)->set('path')->eq(",{$project->id},$executionID,")->where('id')->eq($executionID)->exec();
  1380. $this->createTeamMember($executionID, $execution->openedBy, 'execution');
  1381. if(!empty($projectRoleActor[$jiraProjectID]))
  1382. {
  1383. foreach($projectRoleActor[$jiraProjectID] as $userID)
  1384. {
  1385. $account = $this->getJiraAccount($userID);
  1386. if(!$account) continue;
  1387. if($account == $execution->openedBy) continue;
  1388. $this->createTeamMember($executionID, $account, 'execution');
  1389. }
  1390. }
  1391. $this->createDocLib(0, $project->id, $executionID, $this->lang->doclib->main['execution'], 'execution');
  1392. $this->createTmpRelation('jsprint', $sprint->id, 'zexecution', $executionID);
  1393. $executions[] = $executionID;
  1394. }
  1395. }
  1396. return $executions;
  1397. }
  1398. /**
  1399. * 创建产品。
  1400. * Create product.
  1401. *
  1402. * @param object $project
  1403. * @param array $executions
  1404. * @access protected
  1405. * @return int
  1406. */
  1407. protected function createProduct($project, $executions)
  1408. {
  1409. /* Create product. */
  1410. $product = new stdclass();
  1411. $product->name = $project->name;
  1412. $product->code = $project->code;
  1413. $product->type = 'normal';
  1414. $product->status = 'normal';
  1415. $product->acl = 'open';
  1416. $product->createdBy = $project->openedBy;
  1417. $product->createdDate = helper::now();
  1418. $product->createdVersion = $this->config->version;
  1419. $this->dao->dbh($this->dbh)->insert(TABLE_PRODUCT)->data($product)->exec();
  1420. $productID = $this->dao->dbh($this->dbh)->lastInsertID();
  1421. $this->loadModel('action')->create('product', $productID, 'opened');
  1422. $this->dao->dbh($this->dbh)->update(TABLE_PRODUCT)->set('`order`')->eq($productID * 5)->where('id')->eq($productID)->exec();
  1423. $this->dao->dbh($this->dbh)->replace(TABLE_PROJECTPRODUCT)->set('project')->eq($project->id)->set('product')->eq($productID)->set('branch')->eq('0')->exec();
  1424. $this->createDocLib($productID, 0, 0, $this->lang->doclib->main['product'], 'product');
  1425. /* 关联产品与迭代。 */
  1426. foreach($executions as $executionID) $this->dao->dbh($this->dbh)->replace(TABLE_PROJECTPRODUCT)->set('project')->eq($executionID)->set('product')->eq($productID)->set('branch')->eq('0')->exec();
  1427. return $productID;
  1428. }
  1429. /**
  1430. * 创建应用。
  1431. * Create system.
  1432. *
  1433. * @param int $productID
  1434. * @access protected
  1435. * @return int
  1436. */
  1437. protected function createSystem($productID)
  1438. {
  1439. $product = $this->fetchByID($productID, 'product');
  1440. /* 创建产品同名的应用。 */
  1441. $system = new stdclass();
  1442. $system->name = substr($product->name, 0, 80);
  1443. $system->product = $productID;
  1444. $system->status = 'active';
  1445. $system->desc = '';
  1446. $system->integrated = 0;
  1447. $system->createdBy = $this->app->user->account;
  1448. $system->createdDate = helper::now();
  1449. $this->dao->insert(TABLE_SYSTEM)->data($system)->autoCheck()->exec();
  1450. $systemID = $this->dao->lastInsertID();
  1451. $this->loadModel('action')->create('system', $systemID, 'created');
  1452. return $systemID;
  1453. }
  1454. /**
  1455. * 处理工作流内置字段数据。
  1456. * Process buildin field Data.
  1457. *
  1458. * @param string $module
  1459. * @param object $data
  1460. * @param object $object
  1461. * @param array $relations
  1462. * @param array $customFields
  1463. * @param bool $buildinFlow
  1464. * @access public
  1465. * @return object
  1466. */
  1467. public function processBuildinFieldData($module, $data, $object, $relations, $customFields, $buildinFlow = false)
  1468. {
  1469. $jiraFields = !empty($relations["zentaoField{$data->issuetype}"]) ? $relations["zentaoField{$data->issuetype}"] : array();
  1470. foreach($jiraFields as $jiraField => $zentaoField)
  1471. {
  1472. if(!empty($data->{$jiraField}))
  1473. {
  1474. $fields = $this->session->jiraMethod == 'api' ? zget($customFields, $data->issuetype, array()) : $customFields;
  1475. $controlCode = !empty($fields[$jiraField]->customfieldtypekey) ? $fields[$jiraField]->customfieldtypekey : 'com.atlassian.jira.plugin.system.customfieldtypes:textfield';
  1476. if(strpos($controlCode, 'multiuserpicker') !== false || strpos($controlCode, 'people') !== false)
  1477. {
  1478. $users = explode(',', $data->{$jiraField});
  1479. $object->{$zentaoField} = '';
  1480. foreach($users as $user) $object->{$zentaoField} .= $this->getJiraAccount($user) . ',';
  1481. }
  1482. elseif(strpos($controlCode, 'userpicker') !== false)
  1483. {
  1484. $object->{$zentaoField} = $this->getJiraAccount($data->{$jiraField});
  1485. }
  1486. else
  1487. {
  1488. $object->{$zentaoField} = $data->{$jiraField};
  1489. }
  1490. }
  1491. }
  1492. foreach($this->lang->convert->jira->buildinFields as $fieldCode => $buildinField)
  1493. {
  1494. $field = str_replace(range(0, 9), range('a', 'z'), $module . $fieldCode);
  1495. if(isset($buildinField['buildin']) && $buildinField['buildin'] === $buildinFlow) continue;
  1496. if($this->config->edition == 'open') continue;
  1497. if(!empty($data->{$buildinField['jiraField']}))
  1498. {
  1499. $object->{$field} = $data->{$buildinField['jiraField']};
  1500. if($fieldCode == 'reporter')
  1501. {
  1502. $object->{$field} = $this->getJiraAccount($object->{$field});
  1503. }
  1504. if($fieldCode == 'timeoriginalestimate' || $fieldCode == 'timespent')
  1505. {
  1506. $object->{$field} = round($object->{$field} / 3600, 2);
  1507. }
  1508. }
  1509. }
  1510. return $object;
  1511. }
  1512. /**
  1513. * 创建需求。
  1514. * Create story.
  1515. *
  1516. * @param int $productID
  1517. * @param int $projectID
  1518. * @param int $executionID
  1519. * @param string $type
  1520. * @param object $data
  1521. * @param array $reasonList
  1522. * @param array $customFields
  1523. * @access protected
  1524. * @return bool
  1525. * @param mixed[] $relations
  1526. */
  1527. protected function createStory($productID, $projectID, $executionID, $type, $data, $relations, $customFields)
  1528. {
  1529. $this->app->loadLang('story');
  1530. $this->loadModel('action');
  1531. $story = new stdclass();
  1532. $story->title = $data->summary;
  1533. $story->type = $type;
  1534. $story->product = $productID;
  1535. $story->pri = $data->priority ? $data->priority : 3;
  1536. $story->version = 1;
  1537. $story->grade = 1;
  1538. $story->stage = $this->convertStage($data->issuestatus, $data->issuetype, $relations);
  1539. $story->status = $this->convertStatus('story', $data->issuestatus, $data->issuetype, $relations);
  1540. $story->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1541. $story->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1542. $story->assignedTo = $this->getJiraAccount(isset($data->assignee) ? $data->assignee : '');
  1543. if($story->assignedTo) $story->assignedDate = helper::now();
  1544. if($data->resolution)
  1545. {
  1546. $story->closedReason = zget($relations["zentaoReason{$data->issuetype}"], $data->resolution, '');
  1547. if($story->closedReason && !isset($this->lang->story->reasonList[$story->closedReason])) $story->closedReason = 'done';
  1548. }
  1549. $story = $this->processBuildinFieldData('story', $data, $story, $relations, $customFields);
  1550. $this->dao->dbh($this->dbh)->insert(TABLE_STORY)->data($story)->exec();
  1551. if(!dao::isError())
  1552. {
  1553. $storyID = $this->dao->dbh($this->dbh)->lastInsertID();
  1554. $this->dao->dbh($this->dbh)->update(TABLE_STORY)->set('root')->eq($storyID)->set('path')->eq(",{$storyID},")->where('id')->eq($storyID)->exec();
  1555. $storyDesc = new stdclass();
  1556. $storyDesc->story = $storyID;
  1557. $storyDesc->version = 1;
  1558. $storyDesc->title = $story->title;
  1559. $storyDesc->spec = isset($data->description) ? $data->description : '';
  1560. $this->dao->dbh($this->dbh)->replace(TABLE_STORYSPEC)->data($storyDesc)->exec();
  1561. $this->dao->dbh($this->dbh)->replace(TABLE_PROJECTSTORY)->set('project')->eq($projectID)
  1562. ->set('product')->eq($productID)
  1563. ->set('story')->eq($storyID)
  1564. ->set('version')->eq('1')
  1565. ->exec();
  1566. if($type == 'story')
  1567. {
  1568. $this->dao->dbh($this->dbh)->replace(TABLE_PROJECTSTORY)->set('project')->eq($executionID)
  1569. ->set('product')->eq($productID)
  1570. ->set('story')->eq($storyID)
  1571. ->set('version')->eq('1')
  1572. ->exec();
  1573. }
  1574. /* 通过Api 导入时,Issue关联的版本在Issue中体现。 */
  1575. if(!empty($data->fixVersions))
  1576. {
  1577. foreach($data->fixVersions as $version)
  1578. {
  1579. $versionID = $version['id'];
  1580. $zentaoVersionID = $this->dao->dbh($this->dbh)->select('BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jversion')->andWhere('BType')->eq('zversion')->andWhere('AID')->eq($versionID)->fetch('BID');
  1581. if($zentaoVersionID) $this->dao->dbh($this->dbh)->update(TABLE_BUILD)->set('stories')->eq("CONCAT(stories,',',{$storyID})")->where('id')->eq($zentaoVersionID)->exec();
  1582. }
  1583. }
  1584. /* Create opened action from openedDate. */
  1585. $action = new stdclass();
  1586. $action->product = ",$productID,";
  1587. $action->objectType = 'story';
  1588. $action->objectID = $storyID;
  1589. $action->actor = $story->openedBy;
  1590. $action->action = 'opened';
  1591. $action->date = $story->openedDate;
  1592. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1593. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1594. $this->createTmpRelation("j$type", $data->id, "z$type", $storyID, 'issue');
  1595. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', $type);
  1596. }
  1597. return true;
  1598. }
  1599. /**
  1600. * 创建任务。
  1601. * Create task.
  1602. *
  1603. * @param int $projectID
  1604. * @param int $executionID
  1605. * @param object $data
  1606. * @param array $relations
  1607. * @param array $customFields
  1608. * @access protected
  1609. * @return bool
  1610. */
  1611. protected function createTask($projectID, $executionID, $data, $relations, $customFields)
  1612. {
  1613. $this->app->loadLang('task');
  1614. $this->loadModel('action');
  1615. $task = new stdclass();
  1616. $task->project = $projectID;
  1617. $task->execution = $executionID;
  1618. $task->name = $data->summary;
  1619. $task->type = 'devel';
  1620. $task->estimate = !empty($data->timeoriginalestimate) ? round($data->timeoriginalestimate / 3600, 2) : 0;
  1621. $task->left = !empty($data->timeestimate) ? round($data->timeestimate / 3600, 2) : 0;
  1622. $task->consumed = !empty($data->timespent) ? round($data->timespent / 3600, 2) : 0;
  1623. $task->pri = $data->priority ? $data->priority : 3;
  1624. $task->status = $this->convertStatus('task', $data->issuestatus, $data->issuetype, $relations);
  1625. $task->desc = isset($data->description) ? $data->description : '';
  1626. $task->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1627. $task->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1628. $task->assignedTo = $this->getJiraAccount(isset($data->assignee) ? $data->assignee : '');
  1629. $task->deadline = !empty($data->duedate) ? $this->formatDate(substr($data->duedate, 0, 10)) : null;
  1630. if($task->assignedTo) $task->assignedDate = helper::now();
  1631. if($data->resolution)
  1632. {
  1633. $reasonList = $relations["zentaoReason{$data->issuetype}"];
  1634. $task->closedReason = zget($reasonList, $data->resolution, '');
  1635. if($task->closedReason && !isset($this->lang->task->reasonList[$task->closedReason])) $task->closedReason = 'cancel';
  1636. }
  1637. $task = $this->processBuildinFieldData('task', $data, $task, $relations, $customFields);
  1638. $this->dao->dbh($this->dbh)->insert(TABLE_TASK)->data($task)->exec();
  1639. $taskID = $this->dao->dbh($this->dbh)->lastInsertID();
  1640. $this->dao->dbh($this->dbh)->update(TABLE_TASK)->set('path')->eq(",{$taskID},")->where('id')->eq($taskID)->exec();
  1641. /* Create opened action from openedDate. */
  1642. $action = new stdclass();
  1643. $action->objectType = 'task';
  1644. $action->objectID = $taskID;
  1645. $action->product = ",0,";
  1646. $action->project = $projectID;
  1647. $action->execution = $executionID;
  1648. $action->actor = $task->openedBy;
  1649. $action->action = 'opened';
  1650. $action->date = $task->openedDate;
  1651. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1652. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1653. $this->createTmpRelation('jtask', $data->id, 'ztask', $taskID, 'issue');
  1654. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', 'task');
  1655. return true;
  1656. }
  1657. /**
  1658. * 创建BUG。
  1659. * Create bug.
  1660. *
  1661. * @param int $productID
  1662. * @param int $projectID
  1663. * @param int $executionID
  1664. * @param object $data
  1665. * @param array $relations
  1666. * @param array $customFields
  1667. * @access protected
  1668. * @return bool
  1669. */
  1670. protected function createBug($productID, $projectID, $executionID, $data, $relations, $customFields)
  1671. {
  1672. $this->app->loadLang('bug');
  1673. $this->loadModel('action');
  1674. $bug = new stdclass();
  1675. $bug->product = $productID;
  1676. $bug->project = $projectID;
  1677. $bug->execution = $executionID;
  1678. $bug->title = $data->summary;
  1679. $bug->pri = $data->priority ? $data->priority : 3;
  1680. $bug->severity = 3;
  1681. $bug->status = $this->convertStatus('bug', $data->issuestatus, $data->issuetype, $relations);
  1682. $bug->steps = isset($data->description) ? $data->description : '';
  1683. $bug->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1684. $bug->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1685. $bug->openedBuild = 'trunk';
  1686. $bug->assignedTo = $bug->status == 'closed' ? 'closed' : $this->getJiraAccount(isset($data->assignee) ? $data->assignee : '');
  1687. $bug->deadline = !empty($data->duedate) ? $this->formatDate(substr($data->duedate, 0, 10)) : null;
  1688. if($bug->assignedTo) $bug->assignedDate = helper::now();
  1689. if($data->resolution)
  1690. {
  1691. $resolutionList = $relations["zentaoResolution{$data->issuetype}"];
  1692. $bug->resolution = zget($resolutionList, $data->resolution, '');
  1693. if($bug->resolution && !isset($this->lang->bug->resolutionList[$bug->resolution])) $bug->resolution = 'fixed';
  1694. }
  1695. $bug = $this->processBuildinFieldData('bug', $data, $bug, $relations, $customFields);
  1696. $this->dao->dbh($this->dbh)->insert(TABLE_BUG)->data($bug)->exec();
  1697. $bugID = $this->dao->dbh($this->dbh)->lastInsertID();
  1698. /* Create opened action from openedDate. */
  1699. $action = new stdclass();
  1700. $action->objectType = 'bug';
  1701. $action->objectID = $bugID;
  1702. $action->product = ",$productID,";
  1703. $action->project = $projectID ? $projectID : 0;
  1704. $action->execution = $executionID ? $executionID : 0;
  1705. $action->actor = $bug->openedBy;
  1706. $action->action = 'opened';
  1707. $action->date = $bug->openedDate;
  1708. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1709. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1710. /* 通过Api 导入时,Issue关联的版本在Issue中体现。 */
  1711. if(!empty($data->fixVersions))
  1712. {
  1713. foreach($data->fixVersions as $version)
  1714. {
  1715. $versionID = $version['id'];
  1716. $zentaoVersionID = $this->dao->dbh($this->dbh)->select('BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jversion')->andWhere('BType')->eq('zversion')->andWhere('AID')->eq($versionID)->fetch('BID');
  1717. if($zentaoVersionID) $this->dao->dbh($this->dbh)->update(TABLE_BUILD)->set('bugs')->eq("CONCAT(bugs,',',{$bugID})")->where('id')->eq($zentaoVersionID)->exec();
  1718. }
  1719. }
  1720. $this->createTmpRelation('jbug', $data->id, 'zbug', $bugID, 'issue');
  1721. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', 'bug');
  1722. return true;
  1723. }
  1724. /**
  1725. * 创建测试用例。
  1726. * Create case.
  1727. *
  1728. * @param int $productID
  1729. * @param int $projectID
  1730. * @param int $executionID
  1731. * @param object $data
  1732. * @param array $relations
  1733. * @param array $customFields
  1734. * @access protected
  1735. * @return bool
  1736. */
  1737. protected function createCase($productID, $projectID, $executionID, $data, $relations, $customFields)
  1738. {
  1739. $this->loadModel('action');
  1740. $case = new stdclass();
  1741. $case->product = $productID;
  1742. $case->project = $projectID;
  1743. $case->execution = $executionID;
  1744. $case->type = 'feature';
  1745. $case->version = '1';
  1746. $case->title = $data->summary;
  1747. $case->pri = $data->priority ? $data->priority : 3;
  1748. $case->status = $this->convertStatus('testcase', $data->issuestatus, $data->issuetype, $relations);
  1749. $case->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1750. $case->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1751. $case = $this->processBuildinFieldData('testcase', $data, $case, $relations, $customFields);
  1752. $this->dao->dbh($this->dbh)->insert(TABLE_CASE)->data($case)->exec();
  1753. $caseID = $this->dao->dbh($this->dbh)->lastInsertID();
  1754. /* Create opened action from openedDate. */
  1755. $action = new stdclass();
  1756. $action->objectType = 'testcase';
  1757. $action->objectID = $caseID;
  1758. $action->product = ",$productID,";
  1759. $action->execution = $executionID ? $executionID : 0;
  1760. $action->actor = $case->openedBy;
  1761. $action->action = 'opened';
  1762. $action->date = $case->openedDate;
  1763. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1764. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1765. $spec = new stdclass();
  1766. $spec->case = $caseID;
  1767. $spec->version = '1';
  1768. $spec->title = $case->title;
  1769. $spec->precondition = zget($case, 'precondition', '');
  1770. $spec->files = '';
  1771. $this->dao->insert(TABLE_CASESPEC)->data($spec)->exec();
  1772. $this->loadModel('testcase')->syncCase2Project($case, $caseID);
  1773. $this->createTmpRelation('jcase', $data->id, 'ztestcase', $caseID, 'issue');
  1774. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', 'case');
  1775. return true;
  1776. }
  1777. /**
  1778. * 创建反馈。
  1779. * Create feedback.
  1780. *
  1781. * @param int $productID
  1782. * @param object $data
  1783. * @param array $relations
  1784. * @param array $customFields
  1785. * @access protected
  1786. * @return bool
  1787. */
  1788. protected function createFeedback($productID, $data, $relations, $customFields)
  1789. {
  1790. $this->loadModel('action');
  1791. $feedback = new stdclass();
  1792. $feedback->product = $productID;
  1793. $feedback->title = $data->summary;
  1794. $feedback->public = '1';
  1795. $feedback->pri = $data->priority ? $data->priority : 3;
  1796. $feedback->status = $this->convertStatus('feedback', $data->issuestatus, $data->issuetype, $relations);
  1797. $feedback->desc = isset($data->description) ? $data->description : '';
  1798. $feedback->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1799. $feedback->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1800. $feedback->assignedTo = $feedback->status == 'closed' ? 'closed' : $this->getJiraAccount(isset($data->assignee) ? $data->assignee : '');
  1801. if($feedback->assignedTo) $feedback->assignedDate = helper::now();
  1802. if($data->resolution)
  1803. {
  1804. $reasonList = $relations["zentaoReason{$data->issuetype}"];
  1805. $feedback->closedReason = zget($reasonList, $data->resolution, '');
  1806. if($feedback->closedReason && !isset($this->lang->feedback->closedReasonList[$feedback->closedReason])) $feedback->closedReason = 'refuse';
  1807. }
  1808. $feedback = $this->processBuildinFieldData('feedback', $data, $feedback, $relations, $customFields);
  1809. $this->dao->dbh($this->dbh)->insert(TABLE_FEEDBACK)->data($feedback)->exec();
  1810. $feedbackID = $this->dao->dbh($this->dbh)->lastInsertID();
  1811. /* Create opened action from openedDate. */
  1812. $action = new stdclass();
  1813. $action->objectType = 'feedback';
  1814. $action->objectID = $feedbackID;
  1815. $action->product = ",$productID,";
  1816. $action->actor = $feedback->openedBy;
  1817. $action->action = 'opened';
  1818. $action->date = $feedback->openedDate;
  1819. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1820. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1821. $this->loadModel('feedback')->updateSubStatus($feedbackID, $feedback->status);
  1822. $this->createTmpRelation('jfeedback', $data->id, 'zfeedback', $feedbackID, 'issue');
  1823. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', 'feedback');
  1824. return true;
  1825. }
  1826. /**
  1827. * 创建工单。
  1828. * Create ticket.
  1829. *
  1830. * @param int $productID
  1831. * @param object $data
  1832. * @param array $relations
  1833. * @param array $customFields
  1834. * @access protected
  1835. * @return bool
  1836. */
  1837. protected function createTicket($productID, $data, $relations, $customFields)
  1838. {
  1839. $this->loadModel('action');
  1840. $ticket = new stdclass();
  1841. $ticket->product = $productID;
  1842. $ticket->title = $data->summary;
  1843. $ticket->type = 'code';
  1844. $ticket->pri = $data->priority ? $data->priority : 3;
  1845. $ticket->status = $this->convertStatus('ticket', $data->issuestatus, $data->issuetype, $relations, $relations);
  1846. $ticket->desc = isset($data->description) ? $data->description : '';
  1847. $ticket->openedBy = $this->getJiraAccount(isset($data->creator) ? $data->creator : '');
  1848. $ticket->openedDate = !empty($data->created) ? $this->formatDatetime(substr($data->created, 0, 19)) : null;
  1849. $ticket->assignedTo = $ticket->status == 'closed' ? 'closed' : $this->getJiraAccount(isset($data->assignee) ? $data->assignee : '');
  1850. $ticket->openedBuild = 'trunk';
  1851. if($ticket->assignedTo) $ticket->assignedDate = helper::now();
  1852. if($data->resolution)
  1853. {
  1854. $reasonList = $relations["zentaoReason{$data->issuetype}"];
  1855. $ticket->closedReason = zget($reasonList, $data->resolution, '');
  1856. if($ticket->closedReason && !isset($this->lang->ticket->closedReasonList[$ticket->closedReason])) $ticket->closedReason = 'refuse';
  1857. }
  1858. $ticket = $this->processBuildinFieldData('ticket', $data, $ticket, $relations, $customFields);
  1859. $this->dao->dbh($this->dbh)->insert(TABLE_TICKET)->data($ticket)->exec();
  1860. $ticketID = $this->dao->dbh($this->dbh)->lastInsertID();
  1861. /* Create opened action from openedDate. */
  1862. $action = new stdclass();
  1863. $action->objectType = 'ticket';
  1864. $action->objectID = $ticketID;
  1865. $action->product = ",$productID,";
  1866. $action->actor = $ticket->openedBy;
  1867. $action->action = 'opened';
  1868. $action->date = $ticket->openedDate;
  1869. $this->dao->dbh($this->dbh)->insert(TABLE_ACTION)->data($action)->exec();
  1870. $this->action->saveIndex($action->objectType, $action->objectID, $action->action);
  1871. $this->createTmpRelation('jticket', $data->id, 'zticket', $ticketID, 'issue');
  1872. $this->createTmpRelation('jissueid', $data->id, 'zissuetype', '', 'ticket');
  1873. return true;
  1874. }
  1875. /**
  1876. * 创建版本。
  1877. * Create build.
  1878. *
  1879. * @param int $productID
  1880. * @param int $projectID
  1881. * @param int $systemID
  1882. * @param object $data
  1883. * @param array $versionGroup
  1884. * @param array $issueList
  1885. * @access protected
  1886. * @return object
  1887. */
  1888. protected function createBuild($productID, $projectID, $systemID, $data, $versionGroup, $issueList)
  1889. {
  1890. /* Create build. */
  1891. $build = new stdclass();
  1892. $build->product = $productID;
  1893. $build->project = $projectID;
  1894. $build->system = $systemID;
  1895. $build->name = $data->vname;
  1896. $build->date = !empty($data->releasedate) ? $this->formatDate(substr($data->releasedate, 0, 10)) : null;
  1897. $build->builder = $this->app->user->account;
  1898. $build->createdBy = $this->app->user->account;
  1899. $build->createdDate = helper::now();
  1900. $this->dao->dbh($this->dbh)->insert(TABLE_BUILD)->data($build)->exec();
  1901. $buildID = $this->dao->dbh($this->dbh)->lastInsertID();
  1902. $versionid = $data->id;
  1903. /* Process build data. */
  1904. if(isset($versionGroup[$versionid]))
  1905. {
  1906. foreach($versionGroup[$versionid] as $issue)
  1907. {
  1908. $issueID = $issue->issueid;
  1909. if(empty($issueList[$issueID])) continue;
  1910. $objectID = zget($issueList[$issueID], 'BID', '');
  1911. $issueType = zget($issueList[$issueID], 'BType', '');
  1912. if(!$issueType || ($issueType != 'zstory' && $issueType != 'zbug')) continue;
  1913. if($issue->relation == 'IssueFixVersion')
  1914. {
  1915. if($issueType == 'zstory')
  1916. {
  1917. $this->dao->dbh($this->dbh)->update(TABLE_BUILD)->set("`stories` = IF(`stories` IS NOT NULL, CONCAT(`stories`, ',$objectID'), '{$objectID}')")->where('id')->eq($buildID)->exec();
  1918. }
  1919. if($issueType == 'zbug')
  1920. {
  1921. $this->dao->dbh($this->dbh)->update(TABLE_BUILD)->set("`bugs` = IF(`bugs` IS NOT NULL, CONCAT(`bugs`, ',$objectID'), {$objectID})")->where('id')->eq($buildID)->exec();
  1922. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('resolvedBuild')->eq($buildID)->where('id')->eq($objectID)->exec();
  1923. }
  1924. }
  1925. if($issue->relation == 'IssueVersion' && $issueType == 'zbug')
  1926. {
  1927. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set("`openedBuild` = IF(`openedBuild` = 'trunk', $buildID, CONCAT(`openedBuild`, ',$buildID'))")->where('id')->eq($objectID)->exec();
  1928. }
  1929. }
  1930. }
  1931. $build->id = $buildID;
  1932. return $build;
  1933. }
  1934. /**
  1935. * 创建发布。
  1936. * Create release.
  1937. *
  1938. * @param object $build
  1939. * @param object $data
  1940. * @param array $releaseIssue
  1941. * @param array $issueList
  1942. * @access protected
  1943. * @return bool
  1944. */
  1945. protected function createRelease($build, $data, $releaseIssue, $issueList)
  1946. {
  1947. $status = 'normal';
  1948. if(empty($data->released)) $status = 'wait';
  1949. if(!empty($data->archived)) $status = 'terminate';
  1950. /* Create release. */
  1951. $release = new stdclass();
  1952. $release->product = $build->product;
  1953. $release->project = $build->project;
  1954. $release->system = $build->system;
  1955. $release->build = $build->id;
  1956. $release->name = $build->name;
  1957. $release->date = helper::isZeroDate($data->startdate) ? null : $this->formatDate(substr($data->startdate, 0, 10));
  1958. $release->desc = isset($data->description) ? $data->description : '';
  1959. $release->status = $status;
  1960. $release->releasedDate = !empty($data->released) && !empty($data->releasedate) ? $this->formatDate(substr($data->releasedate, 0, 10)) : null;
  1961. $release->createdBy = $this->app->user->account;
  1962. $release->createdDate = helper::now();
  1963. $this->dao->dbh($this->dbh)->insert(TABLE_RELEASE)->data($release)->exec();
  1964. $releaseID = $this->dao->dbh($this->dbh)->lastInsertID();
  1965. /* Process release data. */
  1966. foreach($releaseIssue as $issue)
  1967. {
  1968. $issueID = $issue->issueid;
  1969. if(empty($issueList[$issueID])) continue;
  1970. $objectID = zget($issueList[$issueID], 'BID', '');
  1971. $issueType = zget($issueList[$issueID], 'BType', '');
  1972. if(!$issueType || ($issueType != 'zstory' && $issueType != 'zbug')) continue;
  1973. if($issue->relation == 'IssueFixVersion')
  1974. {
  1975. if($issueType == 'zstory')
  1976. {
  1977. $this->dao->dbh($this->dbh)->update(TABLE_RELEASE)->set("stories = IF(stories IS NOT NULL, CONCAT(stories, ',$objectID'), '{$objectID}')")->where('id')->eq($releaseID)->exec();
  1978. }
  1979. else
  1980. {
  1981. $this->dao->dbh($this->dbh)->update(TABLE_RELEASE)->set("bugs = IF(bugs IS NOT NULL, CONCAT(bugs, ',$objectID'), '{$objectID}')")->where('id')->eq($releaseID)->exec();
  1982. }
  1983. }
  1984. }
  1985. return true;
  1986. }
  1987. /**
  1988. * Jira内置字段新增成工作流字段。
  1989. * Create buildin field.
  1990. *
  1991. * @param string $module
  1992. * @param array $resolutions
  1993. * @param array $priList
  1994. * @param false $buildin
  1995. * @access protected
  1996. * @return bool
  1997. */
  1998. protected function createBuildinField($module, $resolutions, $priList, $buildin = false)
  1999. {
  2000. $this->loadModel('workflowfield');
  2001. foreach($this->lang->convert->jira->buildinFields as $fieldCode => $buildinField)
  2002. {
  2003. if(isset($buildinField['buildin']) && $buildinField['buildin'] !== $buildin) continue;
  2004. $options = array('code' => array(), 'name' => array());
  2005. if($fieldCode == 'pri')
  2006. {
  2007. foreach($priList as $pri)
  2008. {
  2009. $options['code'][] = $pri->id;
  2010. $options['name'][] = $pri->pname;
  2011. }
  2012. }
  2013. if($fieldCode == 'resolution')
  2014. {
  2015. foreach($resolutions as $resolution)
  2016. {
  2017. $options['code'][] = $resolution->id;
  2018. $options['name'][] = $resolution->pname;
  2019. }
  2020. }
  2021. $field = new stdclass();
  2022. $field->name = $buildinField['name'];
  2023. $field->field = str_replace(range(0, 9), range('a', 'z'), $module . $fieldCode);
  2024. $field->control = $buildinField['control'];
  2025. $field->type = $buildinField['type'];
  2026. $field->length = $buildinField['length'];
  2027. $field->integerDigits = in_array($field->type, $this->config->workflowfield->numberTypes) ? '10' : '';
  2028. $field->decimalDigits = in_array($field->type, $this->config->workflowfield->numberTypes) ? '2' : '';;
  2029. $field->expression = '';
  2030. $field->optionType = $buildinField['optionType'];
  2031. $field->sql = '';
  2032. $field->options = $options;
  2033. $field->placeholder = '';
  2034. $field->module = $module;
  2035. $field->group = '0';
  2036. $field->createdBy = $this->app->user->account;
  2037. $field->createdDate = helper::now();
  2038. if($field->type != 'text') $field->default = '';
  2039. $result = $this->workflowfield->create($module, $field, null, true);
  2040. }
  2041. return true;
  2042. }
  2043. /**
  2044. * 为工作流生成默认布局。
  2045. * Create default layout.
  2046. *
  2047. * @param array $fields
  2048. * @param object $flow
  2049. * @param int $group
  2050. * @access protected
  2051. * @return bool
  2052. */
  2053. protected function createDefaultLayout($fields, $flow, $group = 0)
  2054. {
  2055. foreach(array('browse', 'create', 'edit', 'view') as $action)
  2056. {
  2057. if($flow->module == 'feedback' && $action == 'view') $action = 'adminview';
  2058. foreach($fields as $field)
  2059. {
  2060. if($field->field == 'deleted') continue;
  2061. if(($action == 'create' || $action == 'edit') && in_array($field->field, array('id', 'parent', 'createdBy', 'createdDate', 'editedBy', 'editedDate', 'assignedBy', 'assignedDate', 'deleted'))) continue;
  2062. $layout = new stdclass();
  2063. $layout->module = $flow->module;
  2064. $layout->action = $action;
  2065. $layout->field = $field->field;
  2066. $layout->vision = $this->config->vision;
  2067. $layout->group = $group;
  2068. if(in_array($action, array('view', 'edit', 'adminview'))) $layout->position = 'info';
  2069. $this->dao->insert(TABLE_WORKFLOWLAYOUT)->data($layout)->autoCheck()->exec();
  2070. }
  2071. if($action == 'browse' && !empty($fields))
  2072. {
  2073. $layout->field = 'actions';
  2074. $hasExists = $this->dao->select('*')->from(TABLE_WORKFLOWLAYOUT)->where('module')->eq($layout->module)->andWhere('action')->eq($layout->action)->andWhere('group')->eq($layout->group)->andWhere('field')->eq('actions')->fetch();
  2075. if(!$hasExists) $this->dao->insert(TABLE_WORKFLOWLAYOUT)->data($layout)->autoCheck()->exec();
  2076. }
  2077. }
  2078. return true;
  2079. }
  2080. /**
  2081. * Jira对象新增成工作流。
  2082. * Create workflow.
  2083. *
  2084. * @param array $relations
  2085. * @param array $jiraActions
  2086. * @param array $jiraResolutions
  2087. * @param array $jiraPriList
  2088. * @param array $jiraStatusList
  2089. * @access protected
  2090. * @return array
  2091. */
  2092. protected function createWorkflow($relations, $jiraActions, $jiraResolutions, $jiraPriList, $jiraStatusList = array())
  2093. {
  2094. if($this->config->edition == 'open') return $relations;
  2095. $this->loadModel('workflow');
  2096. $this->loadModel('workflowfield');
  2097. $issueTypeList = $this->getJiraData($this->session->jiraMethod, 'issuetype');
  2098. $customFields = $this->getJiraCustomField();
  2099. $flowRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jissuetype')->andWhere('BType')->eq('zworkflow')->fetchAll('AID');
  2100. foreach($relations['zentaoObject'] as $jiraCode => $zentaoCode)
  2101. {
  2102. if($zentaoCode != 'add_custom') continue;
  2103. /* Jira事务类型新增成禅道工作流。 */
  2104. if(empty($flowRelation[$jiraCode]))
  2105. {
  2106. $flow = new stdclass();
  2107. $flow->name = substr(zget($issueTypeList[$jiraCode], 'pname', ''), 0, 30);
  2108. $flow->icon = 'flow';
  2109. $flow->module = 'jira' . $jiraCode;
  2110. $flow->approval = 'disabled';
  2111. $flow->type = 'flow';
  2112. $flow->table = 'zt_flow_' . $flow->module;
  2113. $flow->createdBy = $this->app->user->account;
  2114. $flow->createdDate = helper::now();
  2115. $flow->vision = $this->config->vision;
  2116. $flow->role = 'custom';
  2117. $flow->status = 'wait';
  2118. $flow->belong = 'project';
  2119. $this->dao->insert(TABLE_WORKFLOW)->data($flow)->autoCheck()->exec();
  2120. $flowID = $this->dao->lastInsertId();
  2121. $this->loadModel('action')->create('workflow', $flowID, 'Created');
  2122. $flow = $this->workflow->getByID($flowID);
  2123. $this->workflow->createFields($flow);
  2124. $this->workflow->createActions($flow);
  2125. $this->workflow->createLabels($flow);
  2126. $this->createBuildinField($flow->module, $jiraResolutions, $jiraPriList);
  2127. $fields = $this->workflowfield->getList($flow->module);
  2128. $this->createDefaultLayout($fields, $flow);
  2129. /* 将工作流发布到指定位置。 */
  2130. $_POST = array();
  2131. $_POST['navigator'] = 'secondary';
  2132. $_POST['app'] = 'scrum';
  2133. $_POST['positionModule'] = 'settings';
  2134. $_POST['position'] = 'before';
  2135. $_POST['syncRelease'] = 'all';
  2136. $_POST['module'] = $flow->module;
  2137. if($this->config->systemMode == 'light') $_POST['app'] = 'project';
  2138. if(isset($this->lang->scrum->menu->other['dropMenu']) && $this->config->systemMode != 'light')
  2139. {
  2140. $_POST['positionModule'] = 'other';
  2141. $_POST['dropMenu'] = 'issue';
  2142. }
  2143. $this->workflow->release($flowID);
  2144. $this->createTmpRelation('jissuetype', $jiraCode, 'zworkflow', $flow->module);
  2145. $relations['zentaoObject'][$jiraCode] = $flow->module;
  2146. }
  2147. else
  2148. {
  2149. $relations['zentaoObject'][$jiraCode] = $flowRelation[$jiraCode]->BID;
  2150. }
  2151. $customField = zget($customFields, $jiraCode, array());
  2152. foreach($customField as $id => $field)
  2153. {
  2154. $relations["jiraField{$jiraCode}"][] = $id;
  2155. $relations["zentaoField{$jiraCode}"][$id] = 'add_field';
  2156. }
  2157. $statusList = zget($jiraStatusList, $jiraCode, array());
  2158. foreach($statusList as $id => $status)
  2159. {
  2160. $relations["jiraStatus{$jiraCode}"][] = $id;
  2161. $relations["zentaoStatus{$jiraCode}"][$id] = 'add_flow_status';
  2162. }
  2163. if($this->session->jiraMethod == 'api')
  2164. {
  2165. $jiraAction = !empty($jiraActions[$jiraCode]['actions']) ? $jiraActions[$jiraCode]['actions'] : array();
  2166. }
  2167. else
  2168. {
  2169. $jiraAction = $jiraActions['actions'];
  2170. }
  2171. foreach($jiraAction as $id => $action)
  2172. {
  2173. if(!empty($action['name']) && $action['name'] == 'Create') continue;
  2174. $relations["jiraAction{$jiraCode}"][] = $id;
  2175. $relations["zentaoAction{$jiraCode}"][$id] = 'add_action';
  2176. }
  2177. }
  2178. return $relations;
  2179. }
  2180. /**
  2181. * Jira字段新增成工作流字段。
  2182. * Create workflow field.
  2183. *
  2184. * @param array $relations
  2185. * @param array $fields
  2186. * @param array $fieldOptions
  2187. * @param array $jiraResolutions
  2188. * @param array $jiraPriList
  2189. * @access protected
  2190. * @return array
  2191. */
  2192. protected function createWorkflowField($relations, $fields, $fieldOptions, $jiraResolutions, $jiraPriList)
  2193. {
  2194. if($this->config->edition == 'open') return $relations;
  2195. $this->loadModel('workflowfield');
  2196. $jiraFieldControl = $this->config->convert->jiraFieldControl;
  2197. $fieldRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jcustomfield')->fetchGroup('extra', 'AID');
  2198. /* 创建内置字段。 */
  2199. foreach($relations['zentaoObject'] as $jiraCode => $module) $this->createBuildinField($module, $jiraResolutions, $jiraPriList, true);
  2200. /* 创建自定义字段。 */
  2201. foreach($relations as $stepKey => $fieldList)
  2202. {
  2203. if(strpos($stepKey, 'zentaoField') === false) continue;
  2204. $jiraCode = str_replace('zentaoField', '', $stepKey);
  2205. $module = $relations['zentaoObject'][$jiraCode];
  2206. foreach($fieldList as $jiraField => $zentaoField)
  2207. {
  2208. if($zentaoField != 'add_field') continue;
  2209. if(empty($fieldRelation[$module][$jiraField]))
  2210. {
  2211. $jiraFields = $this->session->jiraMethod == 'api' ? zget($fields, $jiraCode, array()) : $fields;
  2212. $controlCode = !empty($jiraFields[$jiraField]->customfieldtypekey) ? $jiraFields[$jiraField]->customfieldtypekey : 'com.atlassian.jira.plugin.system.customfieldtypes:textfield';
  2213. $options = array('code' => array(), 'name' => array());
  2214. foreach($fieldOptions as $optionID => $fieldOption)
  2215. {
  2216. if($fieldOption->customfield != $jiraField) continue;
  2217. $options['code'][] = $optionID;
  2218. $options['name'][] = $fieldOption->customvalue;
  2219. }
  2220. if(empty($jiraFieldControl[$controlCode])) $controlCode = !empty($options['code']) ? 'com.atlassian.jira.plugin.system.customfieldtypes:select' : 'com.atlassian.jira.plugin.system.customfieldtypes:textfield';
  2221. $field = new stdclass();
  2222. $field->name = substr(zget($jiraFields[$jiraField], 'cfname', ''), 0, 60);
  2223. $field->field = 'jirafield' . str_replace(range(0, 9), range('a', 'z'), uniqid());
  2224. $field->control = $jiraFieldControl[$controlCode]['control'];
  2225. $field->type = $jiraFieldControl[$controlCode]['type'];
  2226. $field->length = $jiraFieldControl[$controlCode]['length'];
  2227. $field->integerDigits = in_array($field->type, $this->config->workflowfield->numberTypes) ? '10' : '';
  2228. $field->decimalDigits = in_array($field->type, $this->config->workflowfield->numberTypes) ? '2' : '';;
  2229. $field->expression = '';
  2230. $field->optionType = (strpos($controlCode, 'userpicker') !== false || strpos($controlCode, 'people') !== false) ? 'user' : 'custom';
  2231. $field->sql = '';
  2232. $field->options = $options;
  2233. $field->default = '';
  2234. $field->placeholder = '';
  2235. $field->module = $module;
  2236. $field->group = '0';
  2237. $field->createdBy = $this->app->user->account;
  2238. $field->createdDate = helper::now();
  2239. if(strpos(',select,multi-select,radio,checkbox,', ",$field->control,") !== false && $field->optionType == 'custom' && empty($options['code'])) $field->control = 'text';
  2240. $result = $this->workflowfield->create($module, $field, null, true);
  2241. $relation = $this->createTmpRelation('jcustomfield', $jiraField, 'zworkflowfield', $field->field, $field->module);
  2242. $fieldRelation[$module][$jiraField] = $relation;
  2243. $relations[$stepKey][$jiraField] = $field->field;
  2244. }
  2245. else
  2246. {
  2247. $relations[$stepKey][$jiraField] = $fieldRelation[$module][$jiraField]->BID;
  2248. }
  2249. }
  2250. }
  2251. return $relations;
  2252. }
  2253. /**
  2254. * Jira状态新增成工作流状态。
  2255. * Create workflow status.
  2256. *
  2257. * @param array $relations
  2258. * @param array $jiraStatusList
  2259. * @access protected
  2260. * @return array
  2261. */
  2262. protected function createWorkflowStatus($relations, $jiraStatusList)
  2263. {
  2264. if($this->config->edition == 'open') return $relations;
  2265. $this->loadModel('custom');
  2266. $currentLang = $this->app->getClientLang();
  2267. foreach($relations as $stepKey => $statusList)
  2268. {
  2269. if(strpos($stepKey, 'zentaoStatus') === false) continue;
  2270. $jiraCode = str_replace('zentaoStatus', '', $stepKey);
  2271. $module = $relations['zentaoObject'][$jiraCode];
  2272. foreach($statusList as $jiraStatus => $zentaoStatus)
  2273. {
  2274. if($zentaoStatus != 'add_case_status' && $zentaoStatus != 'add_flow_status') continue;
  2275. $zentaoCode = $jiraStatus;
  2276. if($zentaoStatus == 'add_case_status')
  2277. {
  2278. $items = $this->loadModel('setting')->getItem("module=testcase&section=statusList");
  2279. if(empty($items))
  2280. {
  2281. $this->loadModel('testcase');
  2282. $items = $this->lang->testcase->statusList;
  2283. foreach($items as $key => $value) $this->custom->setItem("{$currentLang}.testcase.statusList.$key.1", $value);
  2284. }
  2285. $this->custom->setItem("{$currentLang}.testcase.statusList.$zentaoCode.0", zget($jiraStatusList[$jiraCode][$jiraStatus], 'pname', ''));
  2286. }
  2287. elseif($zentaoStatus == 'add_flow_status')
  2288. {
  2289. $fieldOptions = $this->dao->select('options')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($module)->andWhere('field')->eq('status')->fetch('options');
  2290. $fieldOptions = json_decode($fieldOptions);
  2291. if(!$fieldOptions) $fieldOptions = new stdclass();
  2292. $fieldOptions->{$zentaoCode} = zget($jiraStatusList[$jiraCode][$jiraStatus], 'pname', '');
  2293. $this->dao->update(TABLE_WORKFLOWFIELD)->set('options')->eq(json_encode($fieldOptions))->where('module')->eq($module)->andWhere('field')->eq('status')->exec();
  2294. }
  2295. $relations[$stepKey][$jiraStatus] = $zentaoCode;
  2296. }
  2297. }
  2298. return $relations;
  2299. }
  2300. /**
  2301. * 导入jira状态变更为工作流扩展动作。
  2302. * Process workflow hooks.
  2303. *
  2304. * @param array $jiraAction
  2305. * @param array $jiraStepList
  2306. * @param string $module
  2307. * @access protected
  2308. * @return array
  2309. */
  2310. protected function processWorkflowHooks($jiraAction, $jiraStepList, $module)
  2311. {
  2312. if($this->session->jiraMethod != 'api' && empty($jiraAction['results']['unconditional-result']['@attributes']['step'])) return array();
  2313. $hooks = array();
  2314. $field = new stdclass();
  2315. $field->field = 'status';
  2316. $field->paramType = 'custom';
  2317. if($this->session->jiraMethod == 'api')
  2318. {
  2319. $field->param = $jiraAction['to'];
  2320. }
  2321. else
  2322. {
  2323. $jiraStep = $jiraAction['results']['unconditional-result']['@attributes']['step'];
  2324. $field->param = is_array($jiraStepList[$jiraStep]) ? $jiraStepList[$jiraStep][0] : $jiraStepList[$jiraStep];
  2325. }
  2326. $where = new stdclass();
  2327. $where->field = 'id';
  2328. $where->logicalOperator = 'and';
  2329. $where->operator = 'equal';
  2330. $where->paramType = 'record';
  2331. $where->param = 'id';
  2332. $hook = new stdclass();
  2333. $hook->action = 'update';
  2334. $hook->table = $module;
  2335. $hook->conditionType = 'data';
  2336. $hook->sqlResult = 'empty';
  2337. $hook->message = '';
  2338. $hook->comment = '';
  2339. $hook->fields = array($field);
  2340. $hook->conditions = array();
  2341. $hook->wheres = array($where);
  2342. $hook->sqlVars = array();
  2343. $hook->formVars = array();
  2344. $hook->recordVars = array($where->param);
  2345. $hook->formulaVars = array();
  2346. list($sql) = $this->loadModel('workflowhook')->check($hook);
  2347. $hook->sql = $sql;
  2348. $hooks[] = $hook;
  2349. return $hooks;
  2350. }
  2351. /**
  2352. * Jira动作新增成工作流动作。
  2353. * Create workflow action.
  2354. *
  2355. * @param array $relations
  2356. * @param array $jiraActions
  2357. * @access protected
  2358. * @return array
  2359. */
  2360. protected function createWorkflowAction($relations, $jiraActions)
  2361. {
  2362. if($this->config->edition == 'open') return $relations;
  2363. $this->loadModel('workflowaction');
  2364. $this->loadModel('workflowhook');
  2365. $currentLang = $this->app->getClientLang();
  2366. $flowActionRelation = $this->dao->dbh($this->dbh)->select('*')->from(JIRA_TMPRELATION)->where('AType')->eq('jflowaction')->fetchGroup('extra', 'AID');
  2367. foreach($relations as $stepKey => $actionList)
  2368. {
  2369. if(strpos($stepKey, 'zentaoAction') === false) continue;
  2370. $jiraCode = str_replace('zentaoAction', '', $stepKey);
  2371. $module = $relations['zentaoObject'][$jiraCode];
  2372. foreach($actionList as $jiraAction => $zentaoAction)
  2373. {
  2374. if($zentaoAction != 'add_action') continue;
  2375. if(empty($flowActionRelation[$module][$jiraAction]))
  2376. {
  2377. $actionGroup = $this->session->jiraMethod == 'api' ? zget($jiraActions, $jiraCode, array()) : $jiraActions;
  2378. if(!empty($actionGroup['actions'])) $hooks = $this->processWorkflowHooks($actionGroup['actions'][$jiraAction], !empty($actionGroup['steps']) ? $actionGroup['steps'] : array(), $module);
  2379. $action = new stdclass();
  2380. $action->name = $actionGroup['actions'][$jiraAction]['name'];
  2381. $action->action = $module . 'action' . $jiraAction;
  2382. $action->type = 'single';
  2383. $action->batchMode = 'same';
  2384. $action->open = 'none';
  2385. $action->position = 'browseandview';
  2386. $action->show = 'dropdownlist';
  2387. $action->desc = '';
  2388. $action->module = $module;
  2389. $action->method = 'operate';
  2390. $action->extensionType = 'override';
  2391. $action->conditions = '[]';
  2392. $action->hooks = helper::jsonEncode($hooks);
  2393. $action->status = 'disable';
  2394. $action->createdBy = $this->app->user->account;
  2395. $action->createdDate = helper::now();
  2396. $action->vision = $this->config->vision;
  2397. $action->group = '0';
  2398. $action->order = '999';
  2399. $this->dao->insert(TABLE_WORKFLOWACTION)->data($action)->exec();
  2400. $actionID = $this->dao->lastInsertId();
  2401. $action->id = $actionID;
  2402. $this->workflowaction->createFields($action);
  2403. $relation = $this->createTmpRelation('jflowaction', $jiraAction, 'zworkflowaction', $action->action, $module);
  2404. $flowActionRelation[$module][$jiraAction] = $relation;
  2405. $relations[$stepKey][$jiraAction] = $action->action;
  2406. }
  2407. else
  2408. {
  2409. $relations[$stepKey][$jiraAction] = $flowActionRelation[$module][$jiraAction]->BID;
  2410. }
  2411. }
  2412. }
  2413. return $relations;
  2414. }
  2415. /**
  2416. * 为项目创建工作流分组.
  2417. * Create group.
  2418. *
  2419. * @param string $type
  2420. * @param string $name
  2421. * @param array $objectList
  2422. * @param int $jiraProjectID
  2423. * @param int $zentaoProjectID
  2424. * @param array $productRelations
  2425. * @param array $projectFieldList
  2426. * @access protected
  2427. * @return bool
  2428. */
  2429. protected function createGroup($type, $name, $objectList, $jiraProjectID, $zentaoProjectID, $productRelations, $projectFieldList)
  2430. {
  2431. $group = new stdclass();
  2432. $group->name = substr($name, 0, 80) . $this->lang->workflowgroup->template;
  2433. $group->projectModel = $type == 'project' ? 'scrum' : '';
  2434. $group->projectType = $type == 'project' ? 'product' : '';
  2435. $group->type = $type;
  2436. $group->status = 'normal';
  2437. $group->createdBy = $this->app->user->account;
  2438. $group->createdDate = helper::now();
  2439. $group->disabledModules = '';
  2440. $group->exclusive = '1';
  2441. $this->dao->dbh($this->dbh);
  2442. $groupID = $this->workflowgroup->create($group);
  2443. $this->action->create('workflowgroup', $groupID, 'created');
  2444. $group->id = $groupID;
  2445. $flows = $this->workflowgroup->getFlows($group);
  2446. $actionList = array('browse', 'create', 'edit', 'view', 'adminview');
  2447. foreach($flows as $flow)
  2448. {
  2449. /* 只有该项目或者产品启用的事务类型才激活模板。 */
  2450. if(in_array($flow->module, $objectList))
  2451. {
  2452. $this->workflowgroup->setExclusive($flow->id, $groupID);
  2453. $this->dao->dbh($this->dbh)->update(TABLE_WORKFLOWACTION)->set('extensionType')->eq('extend')->where('module')->eq($flow->module)->andWhere('`group`')->eq($groupID)->andWhere('action')->in($actionList)->exec();
  2454. /* 内置字段添加到布局。 */
  2455. $fields = array();
  2456. foreach($this->lang->convert->jira->buildinFields as $fieldCode => $buildinField)
  2457. {
  2458. if(isset($buildinField['buildin']) && $buildinField['buildin'] == false) continue;
  2459. $field = new stdclass();
  2460. $field->field = str_replace(range(0, 9), range('a', 'z'), $flow->module . $fieldCode);
  2461. $fields[$fieldCode] = $field;
  2462. }
  2463. if($flow->buildin) $this->createDefaultLayout($fields, $flow, $groupID);
  2464. /* 扩展字段添加到布局。 */
  2465. if(empty($projectFieldList[$jiraProjectID][$flow->module])) continue;
  2466. $this->createDefaultLayout($projectFieldList[$jiraProjectID][$flow->module], $flow, $groupID);
  2467. }
  2468. elseif(!$flow->buildin)
  2469. {
  2470. $disabledModules[] = $flow->module;
  2471. }
  2472. }
  2473. if(!empty($disabledModules)) $this->dao->update(TABLE_WORKFLOWGROUP)->set('disabledModules')->eq(implode(',', array_unique($disabledModules)))->where('id')->eq($groupID)->exec();
  2474. if($type == 'product') $this->dao->dbh($this->dbh)->update(TABLE_PRODUCT)->set('workflowGroup')->eq($groupID)->where('id')->eq($productRelations[$jiraProjectID])->exec();
  2475. if($type == 'project') $this->dao->dbh($this->dbh)->update(TABLE_PROJECT)->set('workflowGroup')->eq($groupID)->where('id')->eq($zentaoProjectID)->exec();
  2476. $this->createTmpRelation('jproject', $jiraProjectID, 'zworkflowgroup', $groupID);
  2477. return true;
  2478. }
  2479. /**
  2480. * 创建工作部流程模版。
  2481. * Create workflow group.
  2482. *
  2483. * @param array $relations
  2484. * @param array $projectRelations
  2485. * @param array $productRelations
  2486. * @access protected
  2487. * @return array
  2488. */
  2489. protected function createWorkflowGroup($relations, $projectRelations, $productRelations)
  2490. {
  2491. if($this->config->edition == 'open') return $relations;
  2492. $this->loadModel('action');
  2493. $this->loadModel('workflowgroup');
  2494. $groupRelations = $this->dao->dbh($this->dbh)->select('AID,BID')->from(JIRA_TMPRELATION)->where('AType')->eq('jproject')->andWhere('BType')->eq('zworkflowgroup')->fetchPairs();
  2495. $projectList = $this->getJiraData($this->session->jiraMethod, 'project');
  2496. $projectIssueTypeList = $this->getIssueTypeList($relations);
  2497. $projectFieldList = $this->getJiraFieldGroupByProject($relations);
  2498. foreach($projectRelations as $jiraProjectID => $zentaoProjectID)
  2499. {
  2500. if(!empty($groupRelations[$jiraProjectID])) continue;
  2501. if(empty($projectList[$jiraProjectID])) continue;
  2502. $project = $projectList[$jiraProjectID];
  2503. $issueTypeList = !empty($projectIssueTypeList[$jiraProjectID]) ? $projectIssueTypeList[$jiraProjectID] : array();
  2504. $this->createGroup('project', $project->pname, $issueTypeList, (int)$jiraProjectID, (int)$zentaoProjectID, $productRelations, $projectFieldList);
  2505. $this->createGroup('product', $project->pname, $issueTypeList, (int)$jiraProjectID, (int)$zentaoProjectID, $productRelations, $projectFieldList);
  2506. }
  2507. return $relations;
  2508. }
  2509. /**
  2510. * Jira解决方案新增成禅道关闭原因或解决方案。
  2511. * Create zentao resolution.
  2512. *
  2513. * @param array $relations
  2514. * @access protected
  2515. * @return array
  2516. */
  2517. protected function createResolution($relations)
  2518. {
  2519. $this->loadModel('custom');
  2520. $currentLang = $this->app->getClientLang();
  2521. $jiraResolutions = $this->getJiraData($this->session->jiraMethod, 'resolution');
  2522. foreach($relations as $stepKey => $resolutionList)
  2523. {
  2524. if(strpos($stepKey, 'zentaoResolution') === false && strpos($stepKey, 'zentaoReason') === false) continue;
  2525. $jiraCode = strpos($stepKey, 'zentaoResolution') !== false ? str_replace('zentaoResolution', '', $stepKey) : str_replace('zentaoReason', '', $stepKey);
  2526. $module = $relations['zentaoObject'][$jiraCode];
  2527. foreach($resolutionList as $jiraResolution => $zentaoResolution)
  2528. {
  2529. if($zentaoResolution != 'add_resolution' && $zentaoResolution != 'add_reason' && $zentaoResolution != 'add_closedReason') continue;
  2530. $zentaoCode = 'jira' . $jiraResolution;
  2531. if($zentaoResolution == 'add_resolution') $section = 'resolutionList';
  2532. if($zentaoResolution == 'add_reason') $section = 'reasonList';
  2533. if($zentaoResolution == 'add_closedReason') $section = 'closedReasonList';
  2534. $items = $this->loadModel('setting')->getItem("module={$module}&section={$section}");
  2535. if(empty($items))
  2536. {
  2537. $this->app->loadLang($module);
  2538. $items = $this->lang->story->reasonList;
  2539. if($module == 'bug') $items = $this->lang->bug->resolutionList;
  2540. if($module == 'task') $items = $this->lang->task->reasonList;
  2541. if($module == 'feedback' || $module == 'ticket') $items = $this->lang->{$module}->closedReasonList;
  2542. foreach($items as $key => $value) $this->custom->setItem("{$currentLang}.{$module}.{$section}.{$key}.1", $value);
  2543. }
  2544. $this->custom->setItem("{$currentLang}.{$module}.{$section}.{$zentaoCode}.0", zget($jiraResolutions[$jiraResolution], 'pname', ''));
  2545. $relations[$stepKey][$jiraResolution] = $zentaoCode;
  2546. }
  2547. }
  2548. return $relations;
  2549. }
  2550. /**
  2551. * 更新子需求。
  2552. * Update sub story.
  2553. *
  2554. * @param array $storyLink
  2555. * @param array $issueList
  2556. * @access protected
  2557. * @return bool
  2558. */
  2559. protected function updateSubStory($storyLink, $issueList)
  2560. {
  2561. foreach($storyLink as $source => $dest)
  2562. {
  2563. if(!isset($issueList[$source])) continue;
  2564. $parentType = zget($issueList[$source], 'BType', '');
  2565. if(!in_array($parentType, array('zepic', 'zrequirement', 'zstory'))) continue;
  2566. $parentID = zget($issueList[$source], 'BID', '');
  2567. $this->dao->dbh($this->dbh)->update(TABLE_STORY)
  2568. ->set('parent')->eq('0')
  2569. ->set('isParent')->eq('1')
  2570. ->set('grade')->eq('1')
  2571. ->set('root')->eq($parentID)
  2572. ->set('path')->eq(",{$parentID},")
  2573. ->where('id')->eq($parentID)
  2574. ->exec();
  2575. foreach($dest as $childID)
  2576. {
  2577. if(!isset($issueList[$childID])) continue;
  2578. $childType = zget($issueList[$childID], 'BType', '');
  2579. if(!in_array($childType, array('zepic', 'zrequirement', 'zstory'))) continue;
  2580. $grade = $parentType == $childType ? 2 : 1;
  2581. $childrenID = zget($issueList[$childID], 'BID', '');
  2582. $this->dao->dbh($this->dbh)->update(TABLE_STORY)
  2583. ->set('parent')->eq($parentID)
  2584. ->set('isParent')->eq('0')
  2585. ->set('grade')->eq($grade)
  2586. ->set('root')->eq($parentID)
  2587. ->set('path')->eq(",{$parentID},{$childrenID},")
  2588. ->where('id')->eq($childrenID)
  2589. ->exec();
  2590. }
  2591. }
  2592. return true;
  2593. }
  2594. /**
  2595. * 更新子任务。
  2596. * Update sub task.
  2597. *
  2598. * @param array $taskLink
  2599. * @param array $issueList
  2600. * @access protected
  2601. * @return bool
  2602. */
  2603. protected function updateSubTask($taskLink, $issueList)
  2604. {
  2605. foreach($taskLink as $source => $dest)
  2606. {
  2607. if(!isset($issueList[$source])) continue;
  2608. if(zget($issueList[$source], 'BType', '') != 'ztask') continue;
  2609. $parentID = zget($issueList[$source], 'BID', '');
  2610. $this->dao->dbh($this->dbh)->update(TABLE_TASK)
  2611. ->set('parent')->eq('0')
  2612. ->set('isParent')->eq('1')
  2613. ->set('path')->eq(",{$parentID},")
  2614. ->where('id')->eq($parentID)
  2615. ->exec();
  2616. $parentExecution = $this->dao->select('execution')->from(TABLE_TASK)->where('id')->eq($parentID)->fetch('execution');
  2617. foreach($dest as $childID)
  2618. {
  2619. if(!isset($issueList[$childID])) continue;
  2620. if(zget($issueList[$childID], 'BType', '') != 'ztask') continue;
  2621. $childrenID = zget($issueList[$childID], 'BID', '');
  2622. $this->dao->dbh($this->dbh)->update(TABLE_TASK)
  2623. ->set('parent')->eq($parentID)
  2624. ->set('execution')->eq($parentExecution) // 子任务的执行随父任务
  2625. ->set('isParent')->eq('0')
  2626. ->set('path')->eq(",{$parentID},{$childrenID},")
  2627. ->where('id')->eq($childrenID)
  2628. ->exec();
  2629. }
  2630. }
  2631. return true;
  2632. }
  2633. /**
  2634. * 更新重复的需求和bug。
  2635. * Update duplicate story and bug.
  2636. *
  2637. * @param array $duplicateLink
  2638. * @param array $issueList
  2639. * @access protected
  2640. * @return bool
  2641. */
  2642. protected function updateDuplicateStoryAndBug($duplicateLink, $issueList)
  2643. {
  2644. foreach($duplicateLink as $source => $dest)
  2645. {
  2646. $sourceObjectType = zget($issueList[$source], 'BType', '');
  2647. $sourceObjectID = zget($issueList[$source], 'BID', '');
  2648. $destObjectType = zget($issueList[$dest], 'BType', '');
  2649. $destObjectID = zget($issueList[$dest], 'BID', '');
  2650. if(empty($sourceObjectType) || empty($destObjectType)) continue;
  2651. if($sourceObjectType != 'zstory' && $sourceObjectType != 'zbug') continue;
  2652. if($sourceObjectType != $destObjectType) continue;
  2653. if($sourceObjectType == 'zstory')
  2654. {
  2655. $this->dao->dbh($this->dbh)->update(TABLE_STORY)->set('duplicateStory')->eq($destObjectID)->where('id')->eq($sourceObjectID)->exec();
  2656. }
  2657. else
  2658. {
  2659. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('duplicateBug')->eq($destObjectID)->where('id')->eq($sourceObjectID)->exec();
  2660. }
  2661. }
  2662. return true;
  2663. }
  2664. /**
  2665. * 更新相关对象数据。
  2666. * Update relates object.
  2667. *
  2668. * @param array $relatesLink
  2669. * @param array $issueList
  2670. * @access protected
  2671. * @return bool
  2672. */
  2673. protected function updateRelatesObject($relatesLink, $issueList)
  2674. {
  2675. foreach($relatesLink as $source => $dest)
  2676. {
  2677. $sourceObjectType = zget($issueList[$source], 'BType', '');
  2678. $sourceObjectID = zget($issueList[$source], 'BID', '');
  2679. $destObjectType = zget($issueList[$dest], 'BType', '');
  2680. $destObjectID = zget($issueList[$dest], 'BID', '');
  2681. if(empty($sourceObjectType) || empty($destObjectType)) continue;
  2682. if($sourceObjectType == 'ztask' && $destObjectType == 'zstory')
  2683. {
  2684. $this->dao->dbh($this->dbh)->update(TABLE_TASK)->set('story')->eq($destObjectID)->where('id')->eq($sourceObjectID)->exec();
  2685. }
  2686. elseif($sourceObjectType == 'zstory' && $destObjectType == 'ztask')
  2687. {
  2688. $this->dao->dbh($this->dbh)->update(TABLE_TASK)->set('story')->eq($sourceObjectID)->where('id')->eq($destObjectID)->exec();
  2689. }
  2690. elseif($sourceObjectType == 'zstory' && $destObjectType == 'zbug')
  2691. {
  2692. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('story')->eq($sourceObjectID)->set('storyVersion')->eq(1)->where('id')->eq($destObjectID)->exec();
  2693. }
  2694. elseif($sourceObjectType == 'zbug' && $destObjectType == 'zstory')
  2695. {
  2696. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('story')->eq($destObjectID)->set('storyVersion')->eq(1)->where('id')->eq($sourceObjectID)->exec();
  2697. }
  2698. elseif($sourceObjectType == 'zstory' && $destObjectType == 'zstory')
  2699. {
  2700. $this->dao->dbh($this->dbh)->update(TABLE_STORY)->set("linkStories=concat(linkStories, ',{$destObjectID}')")->where('id')->eq($sourceObjectID)->exec();
  2701. }
  2702. elseif($sourceObjectType == 'zbug' && $destObjectType == 'zbug')
  2703. {
  2704. $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set("relatedBug=concat(relatedBug, ',{$destObjectID}')")->where('id')->eq($sourceObjectID)->exec();
  2705. }
  2706. }
  2707. return true;
  2708. }
  2709. /**
  2710. * 处理jira事务的正文内容。
  2711. * Process jira issue content.
  2712. *
  2713. * @param array $issueList
  2714. * @access protected
  2715. * @return bool
  2716. */
  2717. protected function processJiraIssueContent($issueList)
  2718. {
  2719. $issueTypeList = array();
  2720. foreach($issueList as $relation) $issueTypeList[$relation->BType] = substr($relation->BType, 1);
  2721. $fileGroup = array();
  2722. $fileList = $this->dao->dbh($this->dbh)->select('*')->from(TABLE_FILE)->where('objectType')->in($issueTypeList)->fetchAll();
  2723. foreach($fileList as $file) $fileGroup[$file->objectType][$file->objectID][$file->title] = $file;
  2724. foreach($issueList as $relation)
  2725. {
  2726. $objectType = substr($relation->BType, 1);
  2727. $objectID = $relation->BID;
  2728. if(empty($fileGroup[$objectType][$objectID])) continue;
  2729. $fileList = $fileGroup[$objectType][$objectID];
  2730. if($objectType == 'testcase') continue;
  2731. if($objectType == 'requirement' || $objectType == 'story' || $objectType == 'epic')
  2732. {
  2733. $content = $this->dao->dbh($this->dbh)->select('`spec`')->from(TABLE_STORYSPEC)->where('`story`')->eq($objectID)->fetch('spec');
  2734. $content = $this->processJiraContent($content, $fileList);
  2735. if($content) $this->dao->dbh($this->dbh)->update(TABLE_STORYSPEC)->set('`spec`')->eq($content)->where('`story`')->eq($objectID)->exec();
  2736. }
  2737. elseif($objectType == 'bug')
  2738. {
  2739. $content = $this->dao->dbh($this->dbh)->select('`steps`')->from(TABLE_BUG)->where('id')->eq($objectID)->fetch('steps');
  2740. $content = $this->processJiraContent($content, $fileList);
  2741. if($content) $this->dao->dbh($this->dbh)->update(TABLE_BUG)->set('`steps`')->eq($content)->where('id')->eq($objectID)->exec();
  2742. }
  2743. elseif($objectType == 'task')
  2744. {
  2745. $content = $this->dao->dbh($this->dbh)->select('`desc`')->from(TABLE_TASK)->where('id')->eq($objectID)->fetch('desc');
  2746. $content = $this->processJiraContent($content, $fileList);
  2747. if($content) $this->dao->dbh($this->dbh)->update(TABLE_TASK)->set('`desc`')->eq($content)->where('id')->eq($objectID)->exec();
  2748. }
  2749. elseif($objectType == 'ticket')
  2750. {
  2751. $content = $this->dao->dbh($this->dbh)->select('`desc`')->from(TABLE_TICKET)->where('id')->eq($objectID)->fetch('desc');
  2752. $content = $this->processJiraContent($content, $fileList);
  2753. if($content) $this->dao->dbh($this->dbh)->update(TABLE_TICKET)->set('`desc`')->eq($content)->where('id')->eq($objectID)->exec();
  2754. }
  2755. elseif($objectType == 'feedback')
  2756. {
  2757. $content = $this->dao->dbh($this->dbh)->select('`desc`')->from(TABLE_FEEDBACK)->where('id')->eq($objectID)->fetch('desc');
  2758. $content = $this->processJiraContent($content, $fileList);
  2759. if($content) $this->dao->dbh($this->dbh)->update(TABLE_FEEDBACK)->set('`desc`')->eq($content)->where('id')->eq($objectID)->exec();
  2760. }
  2761. else
  2762. {
  2763. $field = str_replace(range(0, 9), range('a', 'z'), $objectType . 'desc');
  2764. $content = $this->dao->dbh($this->dbh)->select("`{$field}`")->from("`zt_flow_{$objectType}`")->where('id')->eq($objectID)->fetch($field);
  2765. $content = $this->processJiraContent($content, $fileList);
  2766. if($content) $this->dao->dbh($this->dbh)->update("`zt_flow_{$objectType}`")->set("`{$field}`")->eq($content)->where('id')->eq($objectID)->exec();
  2767. }
  2768. $actions = $this->dao->dbh($this->dbh)->select('id,comment')->from(TABLE_ACTION)->where('objectType')->eq($objectType)->andWhere('objectID')->eq($objectID)->fetchAll();
  2769. foreach($actions as $action)
  2770. {
  2771. $content = $action->comment;
  2772. $content = $this->processJiraContent($content, $fileList);
  2773. if($content) $this->dao->dbh($this->dbh)->update(TABLE_ACTION)->set('`comment`')->eq($content)->where('`id`')->eq($action->id)->exec();
  2774. }
  2775. }
  2776. return true;
  2777. }
  2778. /**
  2779. * 处理jira编辑器中的内容。
  2780. * Process jira content.
  2781. *
  2782. * @param string $content
  2783. * @param array $fileList
  2784. * @access protected
  2785. * @return string
  2786. */
  2787. protected function processJiraContent($content, $fileList)
  2788. {
  2789. if(empty($content)) return '';
  2790. /* 旧版jira匹配规则。 */
  2791. preg_match_all('/!(.*?)!/', $content, $matches);
  2792. if(!empty($matches[0]))
  2793. {
  2794. foreach($matches[1] as $key => $fileName)
  2795. {
  2796. $fileName = substr($fileName, 0, strpos($fileName, '|'));
  2797. if(empty($fileList[$fileName])) continue;
  2798. $file = $fileList[$fileName];
  2799. $url = helper::createLink('file', 'read', "t={$file->extension}&fileID={$file->id}");
  2800. $content = str_replace($matches[0][$key], "<img src=\"{{$file->id}.{$file->extension}}\" alt=\"{$url}\"/>", $content);
  2801. }
  2802. }
  2803. /* 新版jira匹配规则。 */
  2804. preg_match_all('/<img[^>]*alt="([^"]*)"[^>]*>/i', $content, $matches);
  2805. if(!empty($matches[0]))
  2806. {
  2807. foreach($matches[1] as $key => $fileName)
  2808. {
  2809. if(empty($fileList[$fileName])) continue;
  2810. $file = $fileList[$fileName];
  2811. $url = helper::createLink('file', 'read', "t={$file->extension}&fileID={$file->id}");
  2812. $content = str_replace($matches[0][$key], "<img src=\"{{$file->id}.{$file->extension}}\" alt=\"{$url}\"/>", $content);
  2813. }
  2814. }
  2815. return $content;
  2816. }
  2817. }