v1.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'idlabel' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'statuslabel' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'branchlabel' . DS . 'v1.php';
  6. class relatedList extends wg
  7. {
  8. /**
  9. * @var mixed[]
  10. */
  11. protected static $defineProps = array
  12. (
  13. 'data' => '?array', // 要显示的类型数据定义。
  14. 'showCount' => '?bool=true', // 是否显示数量。
  15. 'showIDLabel' => '?bool=true', // 是否显示ID标签。
  16. 'onRenderItem' => '?callback' // 渲染需求对象的回调函数。
  17. );
  18. /**
  19. * @param object $item
  20. * @param string $type
  21. * @param mixed[] $group
  22. */
  23. protected function getCommonItem($type, $group, $item)
  24. {
  25. $title = '';
  26. if(isset($item->title)) $title = $item->title;
  27. if(isset($item->name)) $title = $item->name;
  28. $showIDLabel = $this->prop('showIDLabel');
  29. $info = array
  30. (
  31. 'title' => $title,
  32. 'hint' => $title,
  33. 'leading' => $showIDLabel ? array('html' => wg(idLabel::create($item->id))->render()) : null
  34. );
  35. $urlTemplate = isset($group['url']) ? $group['url'] : null;
  36. if(is_null($urlTemplate)) $urlTemplate = common::hasPriv($type, 'view') ? createLink($type, 'view', "{$type}ID={id}") : null;
  37. $url = is_string($urlTemplate) ? str_replace('{id}', "$item->id", $urlTemplate) : null;
  38. if($url) $info['url'] = $url;
  39. if(isset($group['statusList']) && isset($item->status)) $info['content'] = array('html' => wg(statusLabel::create($item->status, $group['statusList'][$item->status]))->render(), 'className' => 'flex-none');
  40. if(isset($item->branchName)) $info['leading']['html'] = wg(branchLabel::create($item->branch, $item->branchName))->render() . $info['leading']['html'];
  41. $props = isset($group['props']) ? $group['props'] : array('data-toggle' => 'modal', 'data-size' => 'lg');
  42. if($type == 'build' && empty($item->execution)) $props['data-app'] = 'project';
  43. if($props) $info = array_merge($info, $props);
  44. return $info;
  45. }
  46. /**
  47. * @param string $type
  48. * @param mixed[] $group
  49. */
  50. protected function getGroupItems($type, $group)
  51. {
  52. $items = isset($group['items']) ? $group['items'] : array();
  53. if(is_object($items)) $items = array($items);
  54. if(!$items) return array();
  55. $list = array();
  56. $methodName = 'get'. ucfirst($type) . 'Item';
  57. $onRenderItem = $this->prop('onRenderItem');
  58. foreach($items as $index => $item)
  59. {
  60. if(is_string($item)) $item = (object)array('id' => $index, 'title' => $item);
  61. $listItem = method_exists($this, $methodName) ? $this->$methodName($group, $item) : $this->getCommonItem($type, $group, $item);
  62. if(is_callable($onRenderItem)) $listItem = $onRenderItem($listItem, $item, $type, $group);
  63. if(isset($group['onRender'])) $listItem = $group['onRender']($listItem, $item);
  64. $list[] = $listItem;
  65. }
  66. return $list;
  67. }
  68. protected function getBugItem($group, $item)
  69. {
  70. global $lang;
  71. $info = $this->getCommonItem('bug', $group, $item);
  72. if(isset($item->status)) $info['content']['html'] = '<span class="text nowrap status-' . $item->status . '">' . zget($lang->bug->statusList, $item->status) . '</span>';
  73. return $info;
  74. }
  75. protected function getLinkBugsItem($group, $item)
  76. {
  77. return $this->getBugItem($group, $item);
  78. }
  79. protected function getItems()
  80. {
  81. global $lang, $app;
  82. $data = $this->prop('data', array());
  83. $showCount = $this->prop('showCount');
  84. $items = array();
  85. $moduleName = $app->rawModule;
  86. foreach($data as $type => $group)
  87. {
  88. if(isset($group['title']))
  89. {
  90. $title = $group['title'];
  91. }
  92. else
  93. {
  94. $langName = 'legend' . ucfirst($type);
  95. $title = isset($lang->$moduleName->$langName) ? $lang->$moduleName->$langName : $type;
  96. }
  97. $groupItems = $this->getGroupItems((string)$type, $group);
  98. $content = zget($group, 'content', '');
  99. $items[] = array
  100. (
  101. 'title' => $title,
  102. 'titleAttrs' => array('title' => $title),
  103. 'items' => $groupItems,
  104. 'content' => $showCount ? array('html' => '<span class="label gray-pale rounded-full size-sm">' . count($groupItems) . '</span>' . $content) : null
  105. );
  106. }
  107. return $items;
  108. }
  109. protected function build()
  110. {
  111. return zui::nestedList
  112. (
  113. set::className('story-related-list'),
  114. set::itemProps(array('titleClass' => 'text-clip')),
  115. set::items($this->getItems())
  116. );
  117. }
  118. }