33namespace Drupal \quanthub_core ;
44
55use Drupal \Component \Datetime \Time ;
6- use Drupal \Component \DependencyInjection \ContainerInterface ;
76use Drupal \Core \Cache \CacheBackendInterface ;
7+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
88use Drupal \Core \Language \LanguageManager ;
99use Drupal \Core \Session \AccountProxy ;
1010
1616class AllowedContentManager implements QuanthubCoreInterface {
1717
1818 /**
19- * Name of user data argument for datasets .
19+ * Dataset URN field name .
2020 */
21- const USER_DATA_DATASETS = 'allowed_datasets ' ;
21+ const URN_FIELD = 'field_quanthub_urn ' ;
2222
2323 /**
2424 * The 15 minutes cache time.
2525 */
2626 const CACHE_TIME = 900 ;
2727
28- /**
29- * The (lazy loaded) dependency injection (DI) container.
30- *
31- * @var ?\Drupal\Component\DependencyInjection\ContainerInterface
32- */
33- protected ?ContainerInterface $ container ;
34-
35- /**
36- * The (lazy loaded) SDMX client.
37- *
38- * @var ?\Drupal\quanthub_core\QuanthubSdmxClient
39- */
40- protected ?QuanthubSdmxClient $ quanthubSdmxClient ;
41-
4228 /**
4329 * The time service.
4430 *
@@ -68,11 +54,18 @@ class AllowedContentManager implements QuanthubCoreInterface {
6854 protected $ languageManager ;
6955
7056 /**
71- * Dataset List.
57+ * The node storage.
58+ *
59+ * @var \Drupal\node\NodeStorageInterface
60+ */
61+ protected $ nodeStorage ;
62+
63+ /**
64+ * The prohibited datasets cache.
7265 *
7366 * @var array
7467 */
75- protected $ datasets = [];
68+ private $ datasets = [];
7669
7770 /**
7871 * {@inheritDoc}
@@ -82,11 +75,13 @@ public function __construct(
8275 CacheBackendInterface $ cache ,
8376 LanguageManager $ language_manager ,
8477 Time $ time ,
78+ EntityTypeManagerInterface $ entity_type_manager ,
8579 ) {
8680 $ this ->currentUser = $ current_user ;
8781 $ this ->cache = $ cache ;
8882 $ this ->languageManager = $ language_manager ;
8983 $ this ->time = $ time ;
84+ $ this ->nodeStorage = $ entity_type_manager ->getStorage ('node ' );
9085 }
9186
9287 /**
@@ -97,32 +92,59 @@ public function getAllowedDatasetList() {
9792 // Check that dataset list is not already saved to cache.
9893 if ($ cache = $ this ->cache ->get ($ this ->getCacheCid ())) {
9994 if (!empty ($ cache ->data )) {
100- $ this -> datasets = $ cache ->data ;
95+ $ datasets = $ cache ->data ;
10196 }
10297 else {
103- $ this -> datasets = [];
98+ $ datasets = [];
10499 }
105100 }
106101 else {
107- $ this -> datasets = $ this ->getUserDatasetList ();
102+ $ datasets = $ this ->getUserDatasetList ();
108103
109104 // Support latest version.
110- foreach ($ this -> datasets as $ dataset ) {
105+ foreach ($ datasets as $ dataset ) {
111106 $ latest_dataset = preg_replace ('/\(.+\)$/ ' , '(~) ' , $ dataset );
112107 if ($ latest_dataset !== $ dataset ) {
113- $ this -> datasets [] = $ latest_dataset ;
108+ $ datasets [] = $ latest_dataset ;
114109 }
115110 }
116111
117112 // Update datasets in cache.
118113 $ this ->cache ->set (
119114 $ this ->getCacheCid (),
120- $ this -> datasets ,
115+ $ datasets ,
121116 $ this ->time ->getCurrentTime () + $ this ::CACHE_TIME
122117 );
123118 }
124119
125- return $ this ->datasets ;
120+ return $ datasets ;
121+ }
122+
123+ /**
124+ * Gets prohibited datasets list.
125+ *
126+ * The list is calculated from DB data based on URN default field
127+ * or user specified one.
128+ */
129+ public function getProhibitedDatasetList ($ field_name = self ::URN_FIELD ) {
130+ if (!isset ($ this ->datasets [$ field_name ])) {
131+ $ datasets = [];
132+ $ result = $ this ->nodeStorage ->getAggregateQuery ()
133+ ->accessCheck (FALSE )
134+ ->condition ('status ' , 1 )
135+ ->exists ($ field_name )
136+ ->groupby ($ field_name )
137+ ->execute ();
138+ foreach ($ result as $ item ) {
139+ if (!empty ($ item [$ field_name ])) {
140+ $ datasets [] = $ item [$ field_name ];
141+ }
142+ }
143+
144+ $ this ->datasets [$ field_name ] = array_diff ($ datasets , $ this ->getAllowedDatasetList ());
145+ }
146+
147+ return $ this ->datasets [$ field_name ];
126148 }
127149
128150 /**
@@ -139,20 +161,7 @@ public function getCacheCid() {
139161 * Get User's Dataset List.
140162 */
141163 public function getUserDatasetList () {
142- return $ this ->quanthubSdmxClient ()->getDatasetList ();
143- }
144-
145- /**
146- * Get Dependency Injection container.
147- *
148- * @return \Drupal\Component\DependencyInjection\ContainerInterface
149- * Current Dependency Injection container.
150- */
151- protected function getContainer (): ContainerInterface {
152- if (!isset ($ this ->container )) {
153- $ this ->container = quanthub_core_container ();
154- }
155- return $ this ->container ;
164+ return self ::quanthubSdmxClient ()->getDatasetList ();
156165 }
157166
158167 /**
@@ -161,11 +170,8 @@ protected function getContainer(): ContainerInterface {
161170 * @return \Drupal\quanthub_core\QuanthubSdmxClient
162171 * QuanthubSdmxClient service.
163172 */
164- protected function quanthubSdmxClient (): QuanthubSdmxClient {
165- if (!isset ($ this ->quanthubSdmxClient )) {
166- $ this ->quanthubSdmxClient = $ this ->getContainer ()->get ('sdmx_client ' );
167- }
168- return $ this ->quanthubSdmxClient ;
173+ protected static function quanthubSdmxClient (): QuanthubSdmxClient {
174+ return \Drupal::service ('sdmx_client ' );
169175 }
170176
171177}
0 commit comments