@@ -50,6 +50,9 @@ class cms_types extends persistent {
50
50
*/
51
51
const TABLE = 'cms_types ' ;
52
52
53
+ /** File area name used for storing images. */
54
+ const ICON_FILE_AREA = 'cms_types_icon ' ;
55
+
53
56
/**
54
57
* Return the definition of the properties of this model.
55
58
*
@@ -143,6 +146,52 @@ public function get_custom_data(string $name) {
143
146
return $ cdata ->$ name ?? null ;
144
147
}
145
148
149
+ /**
150
+ * Get the metadata for the icon, or null if no icon is stored.
151
+ *
152
+ * @return \stored_file|null
153
+ */
154
+ public function get_icon_metadata (): ?\stored_file {
155
+ $ fs = get_file_storage ();
156
+ $ files = $ fs ->get_area_files (
157
+ \context_system::instance ()->id ,
158
+ 'mod_cms ' ,
159
+ self ::ICON_FILE_AREA ,
160
+ $ this ->get ('id ' )
161
+ );
162
+ // There should be 0 or 1 files. We ignore the directory (.) entry.
163
+ foreach ($ files as $ file ) {
164
+ if ($ file ->get_filename () !== '. ' ) {
165
+ return $ file ;
166
+ }
167
+ }
168
+ return null ;
169
+ }
170
+
171
+ /**
172
+ * Gets the URL for the type icon.
173
+ *
174
+ * @return \moodle_url|null
175
+ */
176
+ public function get_type_icon (): ?\moodle_url {
177
+ $ file = $ this ->get_icon_metadata ();
178
+
179
+ $ url = null ;
180
+ // There should only be at most one entry in the array.
181
+ if (!is_null ($ file )) {
182
+ $ filename = $ file ->get_filename ();
183
+ $ url = \moodle_url::make_pluginfile_url (
184
+ \context_system::instance ()->id ,
185
+ 'mod_cms ' ,
186
+ self ::ICON_FILE_AREA ,
187
+ $ this ->get ('id ' ),
188
+ '/ ' ,
189
+ $ filename
190
+ );
191
+ }
192
+ return $ url ;
193
+ }
194
+
146
195
/**
147
196
* Validates idnumber parameter.
148
197
*
@@ -261,6 +310,19 @@ public function get_for_export(): \stdClass {
261
310
}
262
311
unset($ export ->customdata );
263
312
313
+ $ file = $ this ->get_icon_metadata ();
314
+
315
+ // There should be at most one file.
316
+ if (!is_null ($ file )) {
317
+ $ filename = $ file ->get_filename ();
318
+ $ obj = new \stdClass ();
319
+ $ obj ->filename = $ filename ;
320
+ $ obj ->filesize = $ file ->get_filesize ();
321
+ $ obj ->mimetype = $ file ->get_mimetype ();
322
+ $ obj ->content = base64_encode ($ file ->get_content ());
323
+ $ export ->icon = $ obj ;
324
+ }
325
+
264
326
$ export ->datasourcedefs = new \stdClass ();
265
327
foreach (dsbase::get_datasources ($ this ) as $ ds ) {
266
328
$ name = $ ds ::get_shortname ();
@@ -287,6 +349,20 @@ public function set_from_import(\stdClass $data) {
287
349
288
350
$ this ->create ();
289
351
352
+ if (isset ($ data ->icon )) {
353
+ $ fs = get_file_storage ();
354
+ $ filerecord = [
355
+ 'component ' => 'mod_cms ' ,
356
+ 'filearea ' => self ::ICON_FILE_AREA ,
357
+ 'itemid ' => $ this ->get ('id ' ),
358
+ 'filename ' => $ data ->icon ->filename ,
359
+ 'filepath ' => '/ ' ,
360
+ 'contextid ' => \context_system::instance ()->id ,
361
+ 'mimetype ' => $ data ->icon ->mimetype ,
362
+ ];
363
+ $ fs ->create_file_from_string ($ filerecord , base64_decode ($ data ->icon ->content ));
364
+ }
365
+
290
366
foreach ($ data ->datasourcedefs as $ name => $ data ) {
291
367
$ ds = dsbase::get_datasource ($ name , $ this );
292
368
$ ds ->set_from_import ($ data );
@@ -301,7 +377,16 @@ public function set_from_import(\stdClass $data) {
301
377
* @return string|null
302
378
*/
303
379
public function get_cache_key (): ?string {
304
- return hash (lib::HASH_ALGO , serialize ($ this ->to_record ()));
380
+ $ data = $ this ->to_record ();
381
+
382
+ $ file = $ this ->get_icon_metadata ();
383
+ if (!is_null ($ file )) {
384
+ $ data ->filename = $ file ->get_filename ();
385
+ $ data ->filesize = $ file ->get_filesize ();
386
+ $ data ->mimetype = $ file ->get_mimetype ();
387
+ }
388
+
389
+ return hash (lib::HASH_ALGO , serialize ($ data ));
305
390
}
306
391
307
392
/**
0 commit comments