v1.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'btn' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'sidebar' . DS . 'v1.php';
  5. class thinkStepMenu extends wg
  6. {
  7. /**
  8. * @var mixed[]
  9. */
  10. private $modules = array();
  11. /**
  12. * @var mixed[]
  13. */
  14. protected static $defineProps = array(
  15. 'modules: array',
  16. 'wizard: object',
  17. 'marketID?: int',
  18. 'from?: string',
  19. 'activeKey?: int',
  20. 'hover?: bool=true',
  21. 'showAction?: bool=true',
  22. 'toggleNonNodeShow?: bool=false',
  23. 'checkbox?: bool',
  24. 'preserve?: string|bool',
  25. 'checkOnClick?: bool|string',
  26. 'defaultNestedShow?: bool=true',
  27. 'hidden?: bool=false',
  28. 'onCheck?: function',
  29. 'sortable?: array',
  30. 'onSort?: function'
  31. );
  32. public static function getPageCSS()
  33. {
  34. return file_get_contents(__DIR__ . DS . 'css' . DS . 'v1.css');
  35. }
  36. public static function getPageJS()
  37. {
  38. return file_get_contents(__DIR__ . DS . 'js' . DS . 'v1.js');
  39. }
  40. /**
  41. * @param mixed[] $modules
  42. * @param mixed[] $quotedTitle
  43. * @param string $quoteIndex
  44. */
  45. private function getQuotedText($modules, $quotedTitle, &$quoteIndex = '')
  46. {
  47. foreach($modules as $item)
  48. {
  49. if(in_array($item->id, $quotedTitle)) $quoteIndex = $quoteIndex . (!empty($quoteIndex) ? '、' : '') . $item->index;
  50. if(!empty($item->children))
  51. {
  52. $childrenResult = $this->getQuotedText($item->children, $quotedTitle, $quoteIndex);
  53. if($childrenResult) return $childrenResult;
  54. }
  55. }
  56. return $quoteIndex;
  57. }
  58. /**
  59. * @param mixed[] $items
  60. * @param int $parentID
  61. */
  62. private function buildMenuTree($items, $parentID = 0)
  63. {
  64. global $config;
  65. if(empty($items)) $items = $this->modules;
  66. if(empty($items)) return array();
  67. $activeKey = $this->prop('activeKey');
  68. $toggleNonNodeShow = $this->prop('toggleNonNodeShow');
  69. $hidden = $this->prop('hidden');
  70. $sortTree = $this->prop('sortable') || $this->prop('onSort');
  71. $wizard = $this->prop('wizard');
  72. $hiddenModelType = in_array($wizard->model, $config->thinkwizard->hiddenMenuModel);
  73. $parentItems = array();
  74. $questionItems = array();
  75. if($hiddenModelType) $questionItems = array_values($items);
  76. foreach($items as $setting)
  77. {
  78. if(!is_object($setting)) continue;
  79. $options = !empty($setting->options) && is_string($setting->options) ? json_decode($setting->options) : array();
  80. $quotedTitle = !empty($options->quoteTitle) ? explode(',', $options->quoteTitle) : null;
  81. $quotedText = '';
  82. /* 给引用其他问题的多选题添加标签。Add tags to multiple-choice questions that reference other questions. */
  83. if($quotedTitle)
  84. {
  85. $quotedIndex = $this->getQuotedText($this->modules, $quotedTitle);
  86. $quotedText = sprintf($this->lang->thinkstep->treeLabel, $quotedIndex);
  87. }
  88. $itemQuestionIndex = 0;
  89. foreach ($questionItems as $index => $questionItem)
  90. {
  91. if($questionItem->id == $setting->id) $itemQuestionIndex = $index;
  92. }
  93. if(!empty($itemQuestionIndex) && $hiddenModelType) $preStep = $questionItems[$itemQuestionIndex - 1];
  94. $canView = common::hasPriv('thinkstep', 'view');
  95. $unClickable = $toggleNonNodeShow && $setting->id != $activeKey && $setting->type != 'node' && json_decode($setting->answer) == null;
  96. $preStepAnswer = !empty($preStep) && !empty($preStep->answer) ? json_decode($preStep->answer, true) : array();
  97. $preStepResult = !empty($preStepAnswer) ? $preStepAnswer['result'] : array();
  98. $preHasAnswer = (!empty($preStep) && $preStep->type == 'transition') ? $preStepAnswer : $preStepResult;
  99. $hiddenStep = $unClickable || (!empty($preStep) && empty($preHasAnswer));
  100. $item = array(
  101. 'key' => $setting->id,
  102. 'text' => (isset($setting->index) ? ($setting->index . '. ') : '') . $setting->title,
  103. 'subtitle' => (!empty($quotedText) && !in_array($wizard->model, $config->thinkwizard->hiddenMenuModel) && $this->prop('showAction')) ? array('html' => "<span class='label size-sm rounded-full warning-pale'>$quotedText</span>") : null,
  104. 'hint' => $unClickable ? $this->lang->thinkrun->error->unanswered :$setting->title,
  105. 'url' => $unClickable || !$canView ? '' : $setting->url,
  106. 'data-id' => $setting->id,
  107. 'data-type' => $setting->type,
  108. 'data-parent' => $setting->parent,
  109. 'data-order' => $setting->order,
  110. 'data-level' => $setting->grade,
  111. 'selected' => $setting->id == $activeKey,
  112. 'disabled' => $unClickable,
  113. 'actions' => $this->prop('showAction') ? $this->getActions($setting) : null,
  114. 'data-wizard' => $setting->wizard,
  115. 'class' => $hiddenStep && $hidden ? 'hidden' : ''
  116. );
  117. if($sortTree) $item['trailingIcon'] = 'move muted cursor-move';
  118. $children = zget($setting, 'children', array());
  119. if(!empty($children))
  120. {
  121. $children = $this->buildMenuTree($children, $setting->id);
  122. $item['items'] = $children;
  123. }
  124. $parentItems[] = $item;
  125. }
  126. return $parentItems;
  127. }
  128. private function setMenuTreeProps()
  129. {
  130. global $app, $lang;
  131. $this->lang = $lang;
  132. $this->modules = $this->prop('modules');
  133. $app->loadLang('thinkstep');
  134. $untitledLangs = array('transition' => $lang->thinkstep->untitled . $lang->thinkstep->transition, 'question' => $lang->thinkstep->untitled . $lang->thinkstep->question);
  135. jsVar('untitledLangs', $untitledLangs);
  136. $this->setProp('items', $this->buildMenuTree(array(), 0));
  137. }
  138. private function getActions($item)
  139. {
  140. $canCreate = common::hasPriv('thinkstep', 'create');
  141. $canEdit = common::hasPriv('thinkstep', 'edit');
  142. $canDelete = common::hasPriv('thinkstep', 'delete');
  143. if(!$canCreate && !$canEdit && !$canDelete) return array();
  144. $actions = array();
  145. $moreBtn = $this->getOperateItems($item);
  146. $actions[] = array(
  147. 'key' => 'more',
  148. 'icon' => 'ellipsis-v',
  149. 'type' => 'dropdown',
  150. 'caret' => false,
  151. 'dropdown' => array(
  152. 'placement' => 'bottom-start',
  153. 'items' => $moreBtn
  154. )
  155. );
  156. return $actions;
  157. }
  158. private function getOperateItems($item)
  159. {
  160. global $config;
  161. $wizard = $this->prop('wizard');
  162. $canAddChild = true;
  163. $showQuestionOfNode = true;
  164. $hiddenModelType = in_array($wizard->model, $config->thinkwizard->hiddenMenuModel);
  165. $previewCanActions = !$hiddenModelType || ($hiddenModelType && $item->type == 'transition');
  166. $from = '';
  167. if($hiddenModelType) $from = strtolower($wizard->type);
  168. if(!empty($item->children))
  169. {
  170. foreach($item->children as $child)
  171. {
  172. if($canAddChild && $child->type == 'question') $canAddChild = false;
  173. if($showQuestionOfNode && $child->type == 'node') $showQuestionOfNode = false;
  174. }
  175. }
  176. if($hiddenModelType) $canAddChild = false;
  177. $canCreate = common::hasPriv('thinkstep', 'create');
  178. $canEdit = common::hasPriv('thinkstep', 'edit');
  179. $canDelete = common::hasPriv('thinkstep', 'delete');
  180. $canLink = common::hasPriv('thinkstep', 'link');
  181. $parentID = $item->type != 'node' ? $item->parent : $item->id;
  182. $confirmTips = empty($item->link) ? $this->lang->thinkstep->deleteTips[$item->type] : array('message' => $this->lang->thinkstep->tips->deleteLinkStep, 'icon' => 'icon-exclamation-sign', 'iconClass' => 'warning-pale rounded-full icon-2x', 'size' => 'sm');
  183. $menus = array();
  184. $transitionAction = array();
  185. if($canCreate)
  186. {
  187. if($item->type == 'node' && !$hiddenModelType) $menus[] = array(
  188. 'key' => 'addNode',
  189. 'icon' => 'add-chapter',
  190. 'text' => $this->lang->thinkstep->actions['sameNode'],
  191. 'onClick' => jsRaw("() => addNode({$item->id}, 'same')")
  192. );
  193. if($item->grade != 3 && $item->type == 'node' && $canAddChild) $menus[] = array(
  194. 'key' => 'addNode',
  195. 'icon' => 'add-sub-chapter',
  196. 'text' => $this->lang->thinkstep->actions['childNode'],
  197. 'onClick' => jsRaw("() => addNode({$item->id}, 'child')")
  198. );
  199. $transitionAction[] = $previewCanActions ? array('type' => 'divider') : null;
  200. $transitionAction[] = array(
  201. 'key' => 'transition',
  202. 'icon' => 'transition',
  203. 'text' => $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['transition'],
  204. 'onClick' => jsRaw("() => addQuestion({$item->id}, {$parentID}, 'transition')"),
  205. );
  206. }
  207. $marketID = $this->prop('marketID');
  208. $itemHasQuoted = empty($item->hasQuoted) || $item->hasQuoted == 0;
  209. $deleteItem = (!$item->existNotNode && $itemHasQuoted) ? array(
  210. 'key' => 'deleteNode',
  211. 'icon' => 'trash',
  212. 'text' => $this->lang->thinkstep->actions['delete'],
  213. 'innerClass' => 'ajax-submit',
  214. 'data-url' => createLink('thinkstep', 'delete', "marketID={$marketID}&stepID={$item->id}&from={$from}"),
  215. 'data-confirm' => $confirmTips,
  216. ) : array(
  217. 'key' => 'deleteNode',
  218. 'icon' => 'trash',
  219. 'text' => $this->lang->thinkstep->actions['delete'],
  220. 'innerClass' => 'text-gray opacity-50',
  221. 'hint' => $item->existNotNode ? $this->lang->thinkstep->cannotDeleteNode : $this->lang->thinkstep->cannotDeleteQuestion,
  222. );
  223. $linkItem = ($canLink && $item->type === 'question') ? array(
  224. 'key' => 'linkNode',
  225. 'icon' => 'link',
  226. 'text' => $this->lang->thinkstep->actions['link'],
  227. 'data-url' => createLink('thinkstep', 'link', "marketID={$marketID}&stepID={$item->id}"),
  228. 'data-toggle' => 'modal',
  229. 'data-dismiss' => 'modal',
  230. 'data-size' => 'sm'
  231. ) : array(
  232. 'key' => 'linkNode',
  233. 'icon' => 'link',
  234. 'text' => $this->lang->thinkstep->actions['link'],
  235. 'innerClass' => 'text-gray opacity-50',
  236. 'hint' => $this->lang->thinkstep->tips->linkBlocks
  237. );
  238. $menus = array_merge($menus, array(
  239. ($canEdit && $previewCanActions) ? array(
  240. 'key' => 'editNode',
  241. 'icon' => 'edit',
  242. 'text' => $this->lang->thinkstep->actions['edit'],
  243. 'url' => createLink('thinkstep', 'edit', "marketID={$marketID}&stepID={$item->id}&from={$from}")
  244. ) : null,
  245. ($canDelete && $previewCanActions) ? $deleteItem : null,
  246. in_array($wizard->model, $config->thinkwizard->venn) && $item->type == 'question' && $canLink ? $linkItem : null
  247. ), $transitionAction);
  248. $canAddQuestions = ($showQuestionOfNode && $item->type == 'node') || $item->hasSameQuestion || $item->type == 'question';
  249. if($canCreate && !$hiddenModelType && $canAddQuestions) $menus = array_merge($menus, array(
  250. array('type' => 'divider'),
  251. $this->buildMenuItem('radio', 'radio', $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['radio'], $item, $parentID, 'radio'),
  252. $this->buildMenuItem('checkbox', 'checkbox', $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['checkbox'], $item, $parentID, 'checkbox'),
  253. $this->buildMenuItem('input', 'input', $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['input'], $item, $parentID, 'input'),
  254. $this->buildMenuItem('tableInput', 'multi-input', $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['tableInput'], $item, $parentID, 'tableInput'),
  255. $this->buildMenuItem('multicolumn', 'multi-input', $this->lang->thinkstep->createStep . $this->lang->thinkstep->actions['multicolumn'], $item, $parentID, 'multicolumn'),
  256. ));
  257. return $menus;
  258. }
  259. /**
  260. * @param object $item
  261. * @param string $key
  262. * @param string $icon
  263. * @param string $text
  264. * @param int $parentID
  265. */
  266. private function buildMenuItem($key, $icon, $text, $item, $parentID)
  267. {
  268. return array(
  269. 'key' => $key,
  270. 'icon' => $icon,
  271. 'text' => $text,
  272. 'onClick' => jsRaw("() => addQuestion({$item->id}, {$parentID}, 'question', '{$key}')"),
  273. );
  274. }
  275. private function buildActions()
  276. {
  277. return btn(set::type('ghost'), setClass('text-gray absolute top-2 right-3 z-10 toggle-btn'), set::icon('fold-all'), on::click('toggleQuestionShow'));
  278. }
  279. protected function build()
  280. {
  281. $this->setMenuTreeProps();
  282. $treeProps = $this->props->pick(array('items', 'activeClass', 'activeIcon', 'activeKey', 'onClickItem', 'defaultNestedShow', 'changeActiveKey', 'isDropdownMenu', 'checkbox', 'checkOnClick', 'onCheck', 'sortable', 'onSort'));
  283. $isInSidebar = $this->parent instanceof sidebar;
  284. $treeType = (!empty($treeProps['onSort']) || !empty($treeProps['sortable'])) ? 'sortableTree' : 'tree';
  285. list($marketID, $from) = $this->prop(array('marketID', 'from'));
  286. return array
  287. (
  288. div
  289. (
  290. setClass('think-node-menu rounded bg-white col bg-canvas pb-3 h-full no-morph'),
  291. setData(array('marketID' => $marketID, 'from' => $from ?? '')),
  292. zui::$treeType
  293. (
  294. set::_id('thinkNodeMenu'),
  295. setClass('pl-4'),
  296. set::_tag('menu'),
  297. set::defaultNestedShow(true),
  298. set::hover(true),
  299. set::toggleIcons(array('expanded' => 'caret-down cursor-pointer', 'collapsed' => 'caret-right cursor-pointer')),
  300. set::className('tree-lines bg-canvas col flex-auto scrollbar-hover scrollbar-thin overflow-y-auto overflow-x-hidden'),
  301. set($treeProps)
  302. ),
  303. $isInSidebar ? array
  304. (
  305. $this->buildActions(),
  306. row
  307. (
  308. setClass('w-full h-10 justify-end p-1 absolute bottom-0 right-0 pr-4 z-10 bg-canvas'),
  309. btn
  310. (
  311. set::type('ghost'),
  312. set::size('sm'),
  313. set::icon('menu-arrow-left text-gray'),
  314. set::hint($this->lang->collapse),
  315. on::click('hideSidebar')
  316. )
  317. ),
  318. h::js("$('#mainContainer').addClass('has-sidebar');$('#mainContainer .sidebar').addClass('relative');")
  319. ) : null
  320. ),
  321. );
  322. }
  323. }