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

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