view.html.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * The index view file of api module of ZenTaoPMS.
  4. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  5. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  6. * @author Sun Guangming<sunguangming@easycorp.ltd>
  7. * @package api
  8. * @link https://www.zentao.net
  9. */
  10. namespace zin;
  11. $apiHeader = $apiQuery = $apiParams = $apiResponse = array();
  12. $parseTree = function($data, $typeList, $level = 0) use(&$parseTree)
  13. {
  14. global $lang;
  15. $field = '';
  16. for($i = 0; $i < $level; $i++) $field .= '&nbsp;&nbsp;'. ($i == $level-1 ? '∟' : '&nbsp;') . '&nbsp;&nbsp;';
  17. $field .= $data['field'];
  18. $tbody[] = h::tr
  19. (
  20. h::td(html($field)),
  21. h::td(zget($typeList, (string)$data['paramsType'], '')),
  22. h::td(zget($lang->api->boolList, (string)$data['required'], '')),
  23. h::td($data['desc'])
  24. );
  25. if(isset($data['children']) && count($data['children']) > 0)
  26. {
  27. $level++;
  28. foreach($data['children'] as $item) $tbody[] = $parseTree($item, $typeList, $level);
  29. }
  30. return $tbody;
  31. };
  32. if(!empty($api->params['header']))
  33. {
  34. $tbody = array();
  35. foreach($api->params['header'] as $param)
  36. {
  37. $tbody[] = h::tr
  38. (
  39. h::td($param['field']),
  40. h::td('String'),
  41. h::td(zget($lang->api->boolList, (string)$param['required'])),
  42. h::td($param['desc'])
  43. );
  44. }
  45. $apiHeader[] = h3($lang->api->header);
  46. $apiHeader[] = h::table
  47. (
  48. setClass('table condensed bordered'),
  49. h::tr
  50. (
  51. h::th($lang->api->req->name),
  52. h::th($lang->api->req->type),
  53. h::th($lang->api->req->required),
  54. h::th($lang->api->req->desc)
  55. ),
  56. $tbody
  57. );
  58. }
  59. if(!empty($api->params['query']))
  60. {
  61. $tbody = array();
  62. foreach($api->params['query'] as $param)
  63. {
  64. $tbody[] = h::tr
  65. (
  66. h::td($param['field']),
  67. h::td('String'),
  68. h::td(zget($lang->api->boolList, (string)$param['required'])),
  69. h::td($param['desc'])
  70. );
  71. }
  72. $apiQuery[] = h3($lang->api->query);
  73. $apiQuery[] = h::table
  74. (
  75. setClass('table condensed bordered'),
  76. h::tr
  77. (
  78. h::th($lang->api->req->name),
  79. h::th($lang->api->req->type),
  80. h::th($lang->api->req->required),
  81. h::th($lang->api->req->desc)
  82. ),
  83. $tbody
  84. );
  85. }
  86. if(!empty($api->params['params']))
  87. {
  88. $tbody = array();
  89. foreach($api->params['params'] as $param) $tbody = array_merge($tbody, $parseTree($param, $typeList));
  90. $apiParams[] = h3($lang->api->params);
  91. $apiParams[] = h::table
  92. (
  93. setClass('table condensed bordered'),
  94. h::tr
  95. (
  96. h::th($lang->api->req->name),
  97. h::th($lang->api->req->type),
  98. h::th($lang->api->req->required),
  99. h::th($lang->api->req->desc)
  100. ),
  101. $tbody
  102. );
  103. }
  104. if($api->response)
  105. {
  106. $tbody = array();
  107. foreach($api->response as $response) $tbody = array_merge($tbody, $parseTree($response, $typeList));
  108. $apiResponse[] = h3($lang->api->response);
  109. $apiResponse[] = h::table
  110. (
  111. setClass('table condensed bordered'),
  112. h::tr
  113. (
  114. h::th($lang->api->req->name),
  115. h::th($lang->api->req->type),
  116. h::th($lang->api->req->required),
  117. h::th($lang->api->req->desc)
  118. ),
  119. $tbody
  120. );
  121. }
  122. $unsetProps = array('commonParams', 'params', 'paramsExample', 'response', 'responseExample');
  123. $apiData = (array)$api;
  124. foreach($unsetProps as $prop) unset($apiData[$prop]);
  125. $apiData['api'] = true;
  126. $apiData['title'] = "$api->method $api->path $api->title";
  127. $apiData['desc'] = htmlspecialchars_decode($apiData['desc']);
  128. div
  129. (
  130. setID('api-content'),
  131. setClass('article pb-6'),
  132. div
  133. (
  134. setClass("api-list-item row items-center mb-1 gap-2 flex-auto is-$api->method rounded"),
  135. div(setClass('font-mono w-14 text-center api-method py-1 rounded rounded-r-none'), $api->method),
  136. div(setClass('font-mono font-bold text-md api-path'), $api->path)
  137. ),
  138. div
  139. (
  140. setClass('flex gap-2 items-center'),
  141. h2(setClass('flex-none min-w-0 max-w-full'), $api->title),
  142. (isset($api->deleted) && $api->deleted) ? span(setClass('label danger flex-none'), $lang->deleted) : null
  143. ),
  144. div(setClass('desc'), html(htmlspecialchars_decode($api->desc))),
  145. $apiHeader,
  146. $apiQuery,
  147. $apiParams,
  148. $api->paramsExample ? h3($lang->api->paramsExample) : null,
  149. $api->paramsExample ? html("<pre><code>" . $api->paramsExample . "</code></pre>") : null,
  150. $apiResponse,
  151. $api->responseExample ? h3($lang->api->responseExample) : null,
  152. $api->responseExample ? html("<pre><code>" . $api->responseExample . "</code></pre>") : null,
  153. on::init()->do(
  154. 'const docApp = zui.DocApp.query("#api-content");',
  155. 'if(!docApp) return;',
  156. 'const apiData = ' . json_encode($apiData) . ';',
  157. 'docApp.$.update("doc", docApp.$.formatDataItem("doc", apiData));'
  158. )
  159. );