Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / permissions_by_term / src / Service / NodeEntityBundleInfo.php
1 <?php
2
3 namespace Drupal\permissions_by_term\Service;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Template\TwigEnvironment;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Entity\User;
9
10 /**
11  * Class Info
12  *
13  * @package Drupal\permissions_by_term\Service
14  */
15 class NodeEntityBundleInfo {
16
17   /**
18    * @var AccessStorage
19    */
20   private $accessStorage;
21
22   /**
23    * @var TermHandler
24    */
25   private $term;
26
27   /**
28    * @var TwigEnvironment
29    */
30   private $twig;
31
32   /**
33    * @var Connection
34    */
35   private $database;
36
37   /**
38    * Info constructor.
39    *
40    * @param AccessStorage   $accessStorage
41    * @param TermHandler            $term
42    * @param TwigEnvironment $twig
43    * @param Connection      $database
44    */
45   public function __construct(
46     AccessStorage $accessStorage,
47     TermHandler $term,
48     TwigEnvironment $twig,
49     Connection $database
50   ) {
51     $this->accessStorage = $accessStorage;
52     $this->term = $term;
53     $this->twig = $twig;
54     $this->database = $database;
55   }
56
57   /**
58    * @param $viewFilePath
59    * @param null $nid
60    * @param string $langcode
61    *
62    * @return string
63    * @throws \Twig_Error_Loader
64    * @throws \Twig_Error_Runtime
65    * @throws \Twig_Error_Syntax
66    */
67   public function renderNodeDetails($viewFilePath, $langcode, $nid = null) {
68     $roles = null;
69     $users = null;
70
71     if (!empty($nid)) {
72       $tids = $this->term->getTidsByNid($nid);
73       if (!empty($tids)) {
74         $uids = $this->accessStorage->getUserTermPermissionsByTids($tids, $langcode);
75         $rids = $this->accessStorage->getRoleTermPermissionsByTids($tids, $langcode);
76       }
77     }
78
79     if (!empty($rids)) {
80       $roles = Role::loadMultiple($rids);
81     }
82
83     if (!empty($uids)) {
84       $users = User::loadMultiple($uids);
85     }
86
87     $template = $this->twig->loadTemplate($viewFilePath);
88
89     return $template->render(['roles' => $roles, 'users' => $users]);
90
91   }
92
93   /**
94    * @return array
95    */
96   public function getPermissions()
97   {
98     $returnArray = null;
99
100     $permittedUsers = $this->database->select('permissions_by_term_user', 'pu')
101       ->fields('pu', ['uid', 'tid'])
102       ->execute()
103       ->fetchAll();
104
105     $permittedRoles = $this->database->select('permissions_by_term_role', 'pr')
106       ->fields('pr', ['rid', 'tid'])
107       ->execute()
108       ->fetchAll();
109
110     if (!empty($permittedRoles)) {
111       $returnArray['roleLabels'] = [];
112       foreach ($permittedRoles as $permittedRole) {
113         $role = Role::load($permittedRole->rid);
114         if (!empty($role)) {
115           $returnArray['roleLabels'][$permittedRole->tid][] = $role->label();
116         }
117       }
118     }
119
120     if (!empty($permittedUsers)) {
121       $returnArray['userDisplayNames'] = [];
122       foreach ($permittedUsers as $permittedUser) {
123         $user = User::load($permittedUser->uid);
124         if (!empty($user)) {
125           $returnArray['userDisplayNames'][$permittedUser->tid][] = $user->getDisplayName();
126         }
127       }
128     }
129
130     return $returnArray;
131   }
132
133 }