2626 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2727 *
2828 */
29+
2930namespace OCA \Settings \Controller ;
3031
32+ use OCA \Settings \AppInfo \Application ;
3133use OCP \AppFramework \Http \TemplateResponse ;
34+ use OCP \AppFramework \Services \IInitialState ;
3235use OCP \Group \ISubAdmin ;
3336use OCP \IGroupManager ;
3437use OCP \INavigationManager ;
3538use OCP \IUserSession ;
39+ use OCP \Settings \IDeclarativeManager ;
40+ use OCP \Settings \IDeclarativeSettingsForm ;
3641use OCP \Settings \IIconSection ;
3742use OCP \Settings \IManager as ISettingsManager ;
3843use OCP \Settings \ISettings ;
44+ use OCP \Util ;
3945
46+ /**
47+ * @psalm-import-type DeclarativeSettingsFormField from IDeclarativeSettingsForm
48+ */
4049trait CommonSettingsTrait {
4150
4251 /** @var ISettingsManager */
@@ -54,28 +63,26 @@ trait CommonSettingsTrait {
5463 /** @var ISubAdmin */
5564 private $ subAdmin ;
5665
66+ private IDeclarativeManager $ declarativeSettingsManager ;
67+
68+ /** @var IInitialState */
69+ private $ initialState ;
70+
5771 /**
5872 * @return array{forms: array{personal: array, admin: array}}
5973 */
6074 private function getNavigationParameters (string $ currentType , string $ currentSection ): array {
61- $ templateParameters = [
62- 'personal ' => $ this ->formatPersonalSections ($ currentType , $ currentSection ),
63- 'admin ' => []
64- ];
65-
66- $ templateParameters ['admin ' ] = $ this ->formatAdminSections (
67- $ currentType ,
68- $ currentSection
69- );
70-
7175 return [
72- 'forms ' => $ templateParameters
76+ 'forms ' => [
77+ 'personal ' => $ this ->formatPersonalSections ($ currentType , $ currentSection ),
78+ 'admin ' => $ this ->formatAdminSections ($ currentType , $ currentSection ),
79+ ],
7380 ];
7481 }
7582
7683 /**
7784 * @param IIconSection[][] $sections
78- * @psam -param 'admin'|'personal' $type
85+ * @psalm -param 'admin'|'personal' $type
7986 * @return list<array{anchor: string, section-name: string, active: bool, icon: string}>
8087 */
8188 protected function formatSections (array $ sections , string $ currentSection , string $ type , string $ currentType ): array {
@@ -87,7 +94,11 @@ protected function formatSections(array $sections, string $currentSection, strin
8794 } elseif ($ type === 'personal ' ) {
8895 $ settings = $ this ->settingsManager ->getPersonalSettings ($ section ->getID ());
8996 }
90- if (empty ($ settings ) && !($ section ->getID () === 'additional ' && count (\OC_App::getForms ('admin ' )) > 0 )) {
97+
98+ /** @psalm-suppress PossiblyNullArgument */
99+ $ declarativeFormIDs = $ this ->declarativeSettingsManager ->getFormIDs ($ this ->userSession ->getUser (), $ type , $ section ->getID ());
100+
101+ if (empty ($ settings ) && empty ($ declarativeFormIDs ) && !($ section ->getID () === 'additional ' && count (\OC_App::getForms ('admin ' )) > 0 )) {
91102 continue ;
92103 }
93104
@@ -107,14 +118,14 @@ protected function formatSections(array $sections, string $currentSection, strin
107118 return $ templateParameters ;
108119 }
109120
110- protected function formatPersonalSections (string $ currentType , string $ currentSections ): array {
121+ protected function formatPersonalSections (string $ currentType , string $ currentSection ): array {
111122 $ sections = $ this ->settingsManager ->getPersonalSections ();
112- return $ this ->formatSections ($ sections , $ currentSections , 'personal ' , $ currentType );
123+ return $ this ->formatSections ($ sections , $ currentSection , 'personal ' , $ currentType );
113124 }
114125
115- protected function formatAdminSections (string $ currentType , string $ currentSections ): array {
126+ protected function formatAdminSections (string $ currentType , string $ currentSection ): array {
116127 $ sections = $ this ->settingsManager ->getAdminSections ();
117- return $ this ->formatSections ($ sections , $ currentSections , 'admin ' , $ currentType );
128+ return $ this ->formatSections ($ sections , $ currentSection , 'admin ' , $ currentType );
118129 }
119130
120131 /**
@@ -133,6 +144,9 @@ private function formatSettings(array $settings): array {
133144 return ['content ' => $ html ];
134145 }
135146
147+ /**
148+ * @psalm-param 'admin'|'personal' $type
149+ */
136150 private function getIndexResponse (string $ type , string $ section ): TemplateResponse {
137151 if ($ type === 'personal ' ) {
138152 if ($ section === 'theming ' ) {
@@ -144,9 +158,24 @@ private function getIndexResponse(string $type, string $section): TemplateRespon
144158 $ this ->navigationManager ->setActiveEntry ('admin_settings ' );
145159 }
146160
161+ $ this ->declarativeSettingsManager ->loadSchemas ();
162+
147163 $ templateParams = [];
148164 $ templateParams = array_merge ($ templateParams , $ this ->getNavigationParameters ($ type , $ section ));
149165 $ templateParams = array_merge ($ templateParams , $ this ->getSettings ($ section ));
166+
167+ /** @psalm-suppress PossiblyNullArgument */
168+ $ declarativeFormIDs = $ this ->declarativeSettingsManager ->getFormIDs ($ this ->userSession ->getUser (), $ type , $ section );
169+ if (!empty ($ declarativeFormIDs )) {
170+ foreach ($ declarativeFormIDs as $ app => $ ids ) {
171+ /** @psalm-suppress PossiblyUndefinedArrayOffset */
172+ $ templateParams ['content ' ] .= join (array_map (fn (string $ id ) => '<div id=" ' . $ app . '_ ' . $ id . '"></div> ' , $ ids ));
173+ }
174+ Util::addScript (Application::APP_ID , 'declarative-settings-forms ' );
175+ /** @psalm-suppress PossiblyNullArgument */
176+ $ this ->initialState ->provideInitialState ('declarative-settings-forms ' , $ this ->declarativeSettingsManager ->getFormsWithValues ($ this ->userSession ->getUser (), $ type , $ section ));
177+ }
178+
150179 $ activeSection = $ this ->settingsManager ->getSection ($ type , $ section );
151180 if ($ activeSection ) {
152181 $ templateParams ['pageTitle ' ] = $ activeSection ->getName ();
0 commit comments