v1.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'dropmenu' . DS . 'v1.php';
  4. class programMenu extends wg
  5. {
  6. private $programs = array();
  7. /**
  8. * @var mixed[]
  9. */
  10. protected static $defineProps = array(
  11. 'programs?: array',
  12. 'activeClass?: string="active"',
  13. 'activeIcon?: string="check"',
  14. 'activeKey?: string',
  15. 'link?: string',
  16. 'leadingAngle?: bool'
  17. );
  18. public static function getPageCSS()
  19. {
  20. return file_get_contents(__DIR__ . DS . 'css' . DS . 'v1.css');
  21. }
  22. private function buildMenuTree($parent, $parentID)
  23. {
  24. global $lang;
  25. $children = $this->getChildProgram($parentID);
  26. if(count($children) === 0) return array();
  27. $parent[] = array('id' => 0, 'icon' => 'icon-cards-view', 'text' => $lang->program->all);
  28. foreach($children as $child)
  29. {
  30. $item = array('id' => $child->id, 'icon' => 'icon-cards-view', 'text' => $child->name, 'items' => array());
  31. if(isset($child->icon)) $item['icon'] = $child->icon;
  32. $items = $this->buildMenuTree($item['items'], $child->id);
  33. if(count($items) !== 0)
  34. {
  35. $item['items'] = $items;
  36. }
  37. else
  38. {
  39. unset($item['items']);
  40. }
  41. $parent[] = $item;
  42. }
  43. return $parent;
  44. }
  45. private function getTitle($activeKey)
  46. {
  47. global $lang;
  48. if(empty($activeKey)) return $lang->program->all;
  49. foreach($this->programs as $program)
  50. {
  51. if($program->id == $activeKey) return $program->name;
  52. }
  53. return $lang->program->all;
  54. }
  55. private function getChildProgram($id)
  56. {
  57. return array_filter($this->programs, function($program) use($id) {return $program->parent == $id;});
  58. }
  59. protected function build()
  60. {
  61. $this->programs = (array)$this->prop('programs');
  62. $activeKey = $this->prop('activeKey');
  63. $link = $this->prop('link');
  64. $leadingAngle = $this->prop('leadingAngle');
  65. $closeLink = str_replace('{id}', '0', $link);
  66. return zui::dropmenu
  67. (
  68. set('_id', 'programMenu'),
  69. set::className('program-menu btn'),
  70. set::defaultValue($activeKey),
  71. set::leadingAngle($leadingAngle),
  72. set::text($this->getTitle($activeKey)),
  73. set::caret(true),
  74. set::popWidth(200),
  75. set::popClass('popup text-md'),
  76. set::data(array('search' => false, 'checkIcon' => false, 'title' => data('lang.product.selectProgram'), 'link' => $link, 'data' => $this->buildMenuTree(array(), 0)))
  77. );
  78. }
  79. }