vendor/uvdesk/core-framework/Dashboard/HomepageTemplate.php line 25

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Dashboard;
  3. use Symfony\Component\Routing\RouterInterface;
  4. use Symfony\Contracts\Translation\TranslatorInterface;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Framework\ExtendableComponentInterface;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Dashboard\Segments\HomepageSectionInterface;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Dashboard\Segments\HomepageSectionItemInterface;
  9. class HomepageTemplate implements ExtendableComponentInterface
  10. {
  11.     const SECTION_TEMPLATE '<div class="uv-brick"><div class="uv-brick-head"><h6>[[ TITLE ]]</h6><p>[[ DESCRIPTION ]]</p></div><div class="uv-brick-section">[[ COLLECTION ]]</div></div>';
  12.     const SECTION_ITEM_TEMPLATE '<a href="[[ PATH ]]"><div class="uv-brick-container"><div class="uv-brick-icon">[[ SVG ]]</div><p>[[ TITLE ]]</p></div></a>';
  13.     private $sections = [];
  14.     private $sectionItems = [];
  15.     private $isOrganized false;
  16.     public function __construct(RouterInterface $routerUserService $userServiceTranslatorInterface $translator)
  17.     {
  18.         $this->router $router;
  19.         $this->userService $userService;
  20.         $this->translator $translator;
  21.     }
  22.     public function appendSection(HomepageSectionInterface $section$tags = [])
  23.     {
  24.         $this->sections[] = $section;
  25.     }
  26.     public function appendSectionItem(HomepageSectionItemInterface $sectionItem$tags = [])
  27.     {
  28.         $this->sectionItems[] = $sectionItem;
  29.     }
  30.     private function organizeCollection()
  31.     {
  32.         $references = [];
  33.         // Sort segments alphabetically
  34.         usort($this->sections, function ($section_1$section_2) {
  35.             return strcasecmp($section_1::getTitle(), $section_2::getTitle());
  36.         });
  37.         // @TODO: Refactor!!!
  38.         $findSectionByName = function (&$array$name) {
  39.             for ($i 0$i count($array); $i++) {
  40.                 if (strtolower($array[$i]::getTitle()) === $name) {
  41.                     return array($i$array[$i]);
  42.                 }
  43.             }
  44.         };
  45.         // re-inserting users section
  46.         $users_sec $findSectionByName($this->sections"users");
  47.         array_splice($this->sections$users_sec[0], 1);
  48.         array_splice($this->sections$findSectionByName($this->sections"knowledgebase")[0] + 10, [$users_sec[1]]);
  49.         usort($this->sectionItems, function ($item_1$item_2) {
  50.             return strcasecmp($item_1::getTitle(), $item_2::getTitle());
  51.         });
  52.         // Maintain array references
  53.         foreach ($this->sections as $reference => $section) {
  54.             $references[get_class($section)] = $reference;
  55.         }
  56.         // Iteratively add child segments to their respective parent segments
  57.         foreach ($this->sectionItems as $sectionItem) {
  58.             if (!array_key_exists($sectionItem::getSectionReferenceId(), $references)) {
  59.                 continue;
  60.                 // @TODO: Handle exception
  61.                 throw new \Exception("No dashboard section [" $sectionItem::getSectionReferenceId() . "] found for section item " $sectionItem::getTitle() . " [" get_class($sectionItem) . "].");
  62.             }
  63.             $this->sections[$references[$sectionItem::getSectionReferenceId()]]->appendItem($sectionItem);
  64.         }
  65.         $this->isOrganized true;
  66.     }
  67.     private function isSegmentAccessible($segment)
  68.     {
  69.         if ($segment::getRoles() != null) {
  70.             $is_accessible false;
  71.             foreach ($segment::getRoles() as $accessRole) {
  72.                 if ($this->userService->isAccessAuthorized($accessRole)) {
  73.                     $is_accessible true;
  74.                     break;
  75.                 }
  76.             }
  77.             return $is_accessible;
  78.         }
  79.         return true;
  80.     }
  81.     private function getAccessibleSegments()
  82.     {
  83.         $whitelist = [];
  84.         // Filter segments based on user credentials
  85.         foreach ($this->sections as $segment) {
  86.             if (false == $this->isSegmentAccessible($segment)) {
  87.                 continue;
  88.             }
  89.             foreach ($segment->getItemCollection() as $childSegment) {
  90.                 if (false == $this->isSegmentAccessible($childSegment)) {
  91.                     continue;
  92.                 }
  93.                 $whitelist[get_class($segment)][] = get_class($childSegment);
  94.             }
  95.         }
  96.         return $whitelist;
  97.     }
  98.     public function render()
  99.     {
  100.         if (false == $this->isOrganized) {
  101.             $this->organizeCollection();
  102.         }
  103.         $html '';
  104.         $whitelist $this->getAccessibleSegments();
  105.         // Render user accessible segments
  106.         foreach ($this->sections as $segment) {
  107.             if (empty($whitelist[get_class($segment)])) {
  108.                 continue;
  109.             }
  110.             $sectionHtml '';
  111.             $references $whitelist[get_class($segment)];
  112.             foreach ($segment->getItemCollection() as $childSegment) {
  113.                 if (!in_array(get_class($childSegment), $references)) {
  114.                     continue;
  115.                 }
  116.                 $sectionHtml .= strtr(self::SECTION_ITEM_TEMPLATE, [
  117.                     '[[ SVG ]]' => $childSegment::getIcon(),
  118.                     '[[ TITLE ]]' => $this->translator->trans($childSegment::getTitle()),
  119.                     '[[ PATH ]]' => $this->router->generate($childSegment::getRouteName()),
  120.                 ]);
  121.             }
  122.             $html .= strtr(self::SECTION_TEMPLATE, [
  123.                 '[[ TITLE ]]' => $this->translator->trans($segment::getTitle()),
  124.                 '[[ DESCRIPTION ]]' => $this->translator->trans($segment::getDescription()),
  125.                 '[[ COLLECTION ]]' => $sectionHtml,
  126.             ]);
  127.         }
  128.         return $html;
  129.     }
  130. }