-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFieldtypeImageReference.module
812 lines (732 loc) · 30.6 KB
/
FieldtypeImageReference.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<?php
namespace ProcessWire;
/**
* Fieldtype 'Image Reference' stores a reference to an image from another page and/or folder to a selected image in the associated InputfieldImageReference.
* The image can be rendered in template files like any Pageimage (resizing/cropping)
* In case of SVG the file contents can be rendered inline
*
* ©2019 Gerhard Sitzmann
*
* ProcessWire 3.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class FieldtypeImageReference extends Fieldtype
{
/**
* Return an array of module information
*
* @return array
*/
public static function getModuleInfo()
{
return array(
'title' => __('Image Reference'),
'version' => 212,
'summary' => __('Fieldtype that stores a reference to a single image from various sources on the site'),
'author' => 'Gerhard Sitzmann',
'href' => '',
'installs' => [
'InputfieldImageReference',
'ImageReferenceHookHelper'
],
'requires' => [
'PHP>=7.1.0',
'ProcessWire>=3.0.120'
],
);
}
/**
* possible parent folders
*
*/
static public $parentFolders = array();
/**
* Include classes for virtual page
* virtual pageimages on that page serve as value for images from folders
*
*/
public function __construct()
{
// require_once(dirname(__FILE__) . '/ImageReferenceFile.php');
require_once(dirname(__FILE__) . '/ImageReferenceVirtualpage.php');
self::$parentFolders = array(
array('path' => $this->wire('config')->paths->templates, 'url' => $this->wire('config')->urls->templates),
array('path' => $this->wire('config')->paths->assets, 'url' => $this->wire('config')->urls->assets)
);
}
/**
* Return all compatible Fieldtypes
*
* @param Field $field
* @return null
*
*/
public function ___getCompatibleFieldtypes(Field $field)
{
// there are no other fieldtypes compatible with this one
return null;
}
/**
* Sanitize the value for runtime storage and return it.
* @param Page $page
* @param Field $field
* @param string $value json string
* @return string json string
*/
public function sanitizeValue(Page $page, Field $field, $value)
{
/**
* setting value via API as PageImage
* convert PageImage object to JSON string value
*/
$orgValue = $value;
if ($value instanceof PageImage) {
$value = $this->toJson($value->page->id, basename($value->filename));
if ((int) $orgValue->page->id === 0) {
/**
* image is coming from a folder
* copy image to folder of this field if it does not already exist
*/
$this->copyImageToFolder($orgValue->filename, $field->folderpath);
}
}
// if it's not a valid json string, then return a json string with empty values
$obj = json_decode($value);
if (!isset($obj->pageid) && !isset($obj->filename)) return $this->toJson();
$value = $this->wire('sanitizer')->text($value);
return $value;
}
/**
* Get a blank value used by this fieldtype
*
* @param Page $page
* @param Field $field
* @return string json string
*
*/
public function getBlankValue(Page $page, Field $field)
{
return $this->toJson();
}
/**
* Format value for output
* @param Page $page
* @param Field $field
* @param string $value json string
* @return mixed Pageimage|null
*
*/
public function ___formatValue(Page $page, Field $field, $value)
{
$value = json_decode($value);
if ($value->filename && (int) $value->pageid === 0) { // image from folder
$pageimages = self::getPageimages($field->folderpath);
return $pageimages->getFile($value->filename);
} elseif ($value->filename && $value->pageid !== 0) { // Pageimage
$pageimage = null;
$page = $this->wire('pages')->get($value->pageid);
if ($page->id) {
foreach ($field->imagesfields as $fieldName) {
if ($pageimage = $page->get($fieldName)->getFile($value->filename)) break;
}
}
if ($pageimage && $pageimage instanceof Pageimage) {
if (strtolower($pageimage->ext) == 'svg') $this->addHookProperty("Pageimage::svgcontent", $this, 'addPropertySvgcontent');
return $pageimage;
}
}
return null;
}
/**
* creates and returns a virtual Pageimages object with all folder images added as Pageimage
*
* @param string $folderpath can bei either full or partial disk path
* @return Pageimages
*/
static public function getPageimages($folderpath)
{
// make sure $folderpath is full disk path
if (!is_dir(DIRECTORY_SEPARATOR . trim($folderpath, '/'))) $folderpath = self::getBasePath($folderpath);
$p = new ImageReferenceVirtualpage($folderpath);
$field = new Field;
$field->type = new FieldtypeImage;
$field->name = 'imagereferencevirtualfield';
// $field->id = 0;
$pageimages = new Pageimages($p);
$pageimages->setField($field);
$pageimages->setTrackChanges(true);
foreach (wire('files')->find($folderpath) as $file) {
$pageimage = new Pageimage($pageimages, $file);
$variation = $pageimage->isVariation(pathinfo($file, PATHINFO_BASENAME), array('allowSelf' => true, 'verbose' => false));
if ($variation) continue; // add only originals
// Pagimage sanitizes filenames. So we have to rename all files in folder to match the sanitized name
if ($pageimage->filename != $file) {
wire('files')->rename($file, $pageimage->basename, array('throw' => true));
}
if (strtolower($pageimage->ext) == 'svg') $pageimage->set('svgcontent', FieldtypeImageReference::getFileContent($pageimage)); // if svg, add custom property
$pageimages->add($pageimage);
}
return $pageimages;
}
/**
* Return full disk path to file
* @param Field $field
* @param object $value
* @return string
* @throws WireException
*/
public function getFilepath(Page $page, Field $field, $value)
{
$filePath = self::getBasePath($field->folderpath) . $value->filename;
$fileUrl = self::getFileUrl($field->folderpath, $value->filename);
if (file_exists($filePath)) return $filePath;
$field->getInputfield($page)->error("File does not exist: {$fileUrl}");
return null;
}
/**
* Return full disk path to folder
*
* @param string $folderpath
* @return string disk path
*/
static public function getBasePath($folderpath)
{
foreach (self::$parentFolders as $parent) {
if (file_exists($parent['path'] . $folderpath)) return $parent['path'] . $folderpath;
}
}
/**
* Return url to folder
*
* @param string $folderpath
* @return string relative url
*/
static public function getBaseUrl($folderpath)
{
foreach (self::$parentFolders as $parent) {
if (file_exists($parent['path'] . $folderpath)) return $parent['url'] . $folderpath;
}
}
/**
* Return file URL
* @param string $folderpath relative path to folder
* @param string $basename file basename
* @return mixed string|null file url if file exists, null otherwise
*/
static public function getFileUrl($folderpath, $basename)
{
foreach (self::$parentFolders as $parent) {
if (file_exists($parent['path'] . $folderpath . $basename)) return $parent['url'] . $folderpath . $basename;
}
return null;
}
/**
* converts values into json string
*
* @param integer $pageid
* @param string $filename
* @return string json string
*/
public static function toJson($pageid = '0', $filename = '')
{
return json_encode(array('pageid' => "{$pageid}", 'filename' => $filename));
}
/**
* Given a raw value (value as stored in database), return the value as it would appear in a Page object.
*
* @param Page $page
* @param Field $field
* @param array $value
* @return string json string
*/
public function ___wakeupValue(Page $page, Field $field, $value)
{
return $this->toJson($value['pageid'], $value['data']);
}
/**
* Given an 'awake' value, as set by wakeupValue(), convert the value back to a basic type for storage in database.
*
* @param Page $page
* @param Field $field
* @param string $value json string
* @return array properties string filename, int pageid, data json string
*/
public function ___sleepValue(Page $page, Field $field, $value)
{
$obj = json_decode($value);
$filename = $this->wire('sanitizer')->string($obj->filename);
$pageid = $this->wire('sanitizer')->int($obj->pageid);
$sleepValue = array(
'data' => $filename,
'pageid' => $pageid
);
return $sleepValue;
}
/**
* Return stripped file contents (only for svg)
* returns only the <svg> node from the svg document sincs this is all we need to inline an svg
* @param Pageimage $image
* @return mixed string|null
* @throws WireException
*/
static public function getFileContent($image)
{
$mimeTypes = ['image/svg', 'image/svg+xml'];
if (strtolower($image->ext) !== 'svg' || !in_array(self::getFileMimetype($image), $mimeTypes)) {
return null;
} else {
$doc = new \DOMDocument;
$doc->loadXML(file_get_contents($image->filename));
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
$svg = $xpath->query('/svg:svg')->item(0);
if ($svg) {
return $doc->saveXML($svg);
} else {
throw new WireException("The SVG file seems corrupt. Could not find SVG tag in file");
}
}
}
/**
* Return file mime type
* @param Pageimage $image
* @return string
*/
static public function getFileMimetype($image)
{
return mime_content_type($image->filename);
}
/**
* Hook function for adding custom property 'svgcontent' to a PageImage object
*
* @param Hookevent $event
* @return string svg markup
*/
public function ___addPropertySvgcontent(Hookevent $event)
{
$event->return = self::getFileContent($event->object);
}
/**
* Return new instance of the Inputfield associated with this Fieldtype
*
* @param Page $page
* @param Field $field
* @return Inputfield
*
*/
public function getInputfield(Page $page, Field $field)
{
/** @var InputfieldImageReference $inputfield */
$inputfield = $this->modules->get('InputfieldImageReference');
foreach ($field->data as $key => $value) {
if ($key == 'folderpath') $value = trim(trim($value, '/')) . '/';
$inputfield->set($key, $value);
}
return $inputfield;
}
/**
* copies $sourceFile to $folderpath if it doesn't exist there
* required when image value set via API and image is coming from a different field with different destination folder setting
*
* @param string $sourceFile full path to source file
* @param string $folderpath
* @return void
*/
public function copyImageToFolder($sourceFile, $folderpath) {
if(self::getFileUrl($folderpath, basename($sourceFile))) return; // image already exists in folder
$destFile = self::getBasePath($folderpath) . basename($sourceFile);
if(!file_exists($destFile)) {
if(!copy($sourceFile, $destFile)) $this->wire('log')->error("Could not copy {$sourceFile} to {$destFile}");
}
}
/**
* Return the database schema in specified format
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field)
{
// do not get the default schema. We define it fully here
$schema = parent::getDatabaseSchema($field);
// return $schema;
$engine = $this->wire('config')->dbEngine;
$charset = $this->wire('config')->dbCharset;
$schema = array(
'pages_id' => 'int UNSIGNED NOT NULL',
'data' => 'VARCHAR(255) NOT NULL',
'pageid' => 'INT NOT NULL',
'keys' => array(
'primary' => 'PRIMARY KEY (`pages_id`)',
'data' => 'FULLTEXT(`data`)', // used for the filname
'pageid' => 'KEY (`pageid`)'
),
// additional data
'xtra' => array(
// any optional statements that should follow after the closing paren (i.e. engine, default charset, etc)
'append' => "ENGINE=$engine DEFAULT CHARSET=$charset",
// true (default) if this schema provides all storage for this fieldtype.
// false if other storage is involved with this fieldtype, beyond this schema (like repeaters, PageTable, etc.)
'all' => true,
)
);
/**
* added this to fix error: Field [fieldname] is not saveable because it is in a specific context
* error appears when field is inside a repeater
* Fix taken from https://github.com/processwire/processwire-issues/issues/512
* in commit https://github.com/processwire/processwire/commit/0aefc2baa3a31fbbbcc0274a5002b4703e52192a
*/
if ($field->flags & Field::flagFieldgroupContext) $field = $this->wire('fields')->get($field->name);
if ($field->id) $this->updateDatabaseSchema($field, $schema);
return $schema;
}
/**
* Update the DB schema, if necessary
*
* @param Field $field
* @param array $schema
*
*/
protected function updateDatabaseSchema(Field $field, array $schema)
{
$requiredVersion = 2;
$schemaVersion = (int) $field->get('schemaVersion');
if ($schemaVersion >= $requiredVersion) {
// already up-to-date
return;
}
if ($schemaVersion == 1) {
/**
* update schema to v2:
* remove data column
*/
//
$schemaVersion = 2;
$database = $this->wire('database');
$table = $database->escapeTable($field->getTable());
$query = $database->prepare("SHOW TABLES LIKE '$table'");
$query->execute();
$row = $query->fetch(\PDO::FETCH_NUM);
$query->closeCursor();
if (!empty($row)) {
// get information about data column as 0 indexed array
$query = $database->prepare("SHOW COLUMNS FROM `$table` WHERE field='filename'");
$query->execute();
if ($query->rowCount()) {
try {
$database->exec("ALTER TABLE `$table` MODIFY data VARCHAR(255) NOT NULL");
$database->exec("ALTER TABLE `$table` ADD FULLTEXT(`data`)");
$this->message("updated 'data' schema to text NOT NULL");
$database->exec("UPDATE `$table` SET data=filename");
$this->message("copied data from 'filename' to 'data' column");
$database->exec("ALTER TABLE `$table` DROP COLUMN filename");
$this->message("removed column 'filename'");
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}
$this->wire('log')->save('modules', "updated DB table for field {$field->name}");
}
$field->set('schemaVersion', $schemaVersion);
$field->save();
}
/**
* Match values for PageFinder
*
* @param DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param string $value
* @return DatabaseQuerySelect
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value)
{
if (!$subfield || $subfield == 'filename') $subfield = 'data';
if ($subfield != 'data' || $this->wire('database')->isOperator($operator)) {
// if dealing with something other than address, or operator is native to SQL,
// then let Fieldtype::getMatchQuery handle it instead
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
}
// if we get here, then we're performing either %= (LIKE and variations) or *= (FULLTEXT and variations)
$ft = new DatabaseQuerySelectFulltext($query);
$ft->match($table, $subfield, $operator, $value);
return $query;
}
/**
* Get information used for InputfieldSelector interactive selector builder
*
* This is for Lister purposes.
* We want nice labels for our lister selects (i.e. not raw db ones, i.e. 'some_column').
*
* @param Field $field The field we are working with.
* @param array $data Array of extra data, when/if needed.
* @return array
*
*/
public function ___getSelectorInfo(Field $field, array $data = array())
{
$info = parent::___getSelectorInfo($field, $data);
## filterable subfields for this field ##
if (isset($info['subfields']['data'])) unset($info['subfields']['data']);
$subfields = array(
'data' => array(
'name' => 'filename',
'input' => 'text',
'label' => $this->_('Filename'),
'operators' => array('%=', '!%=', '*='/* , '!*=' */, '~='/* , '!~=' */, '^='/* , '!^=' */, '$='/* , '!$=' */, '=', '!=', '=""', '!=""'),
'options' => array(),
),
'pageid' => array(
'name' => 'pageid',
'input' => 'number',
'label' => $this->_('Page ID'),
'operators' => array('=', '!=', /*'<', '>', '<=', '>=',*/ '=""', '!=""'),
'options' => array(),
),
);
$info['subfields'] = array_merge($info['subfields'], $subfields);
return $info;
}
public function ___upgrade($fromVersion, $toVersion)
{
if (version_compare($fromVersion, $toVersion == -1)) {
// install helper module
$modules = $this->wire('modules');
if (!$modules->isInstalled('ImageReferenceHookHelper')) $modules->install('ImageReferenceHookHelper');
$this->wire('log')->save('modules', 'installed module ImageReferenceHookHelper');
if ($toVersion == '114') {
// update field settings
foreach ($this->wire('fields') as $field) {
if (!$frompagefields = $field->get('imagesfieldspage')) continue;
// var_dump($field);
// @todo for some reason this code does not run
$fields = array_unique(array_merge($frompagefields, $field->get('imagesfields')));
$field->set('imagesfields', $fields);
$field->set('imagesfieldspage', array());
$field->save();
$this->wire('log')->save('modules', "updated field {$field->name} settings");
}
}
}
}
/**
* Get the inputfield used for configuration of this Fieldtype.
*
* @param Field $field
* @return InputfieldWrapper
*
*/
public function ___getConfigInputfields(Field $field)
{
$errorimgpage = false;
if ($field->imagespage) {
$hasImagefield = false;
$imagespage = $this->pages->get("id={$field->imagespage}, include=hidden");
foreach ($imagespage->fieldgroup as $f) {
if ($f->type instanceof FieldtypeImage) {
$hasImagefield = true;
break;
}
}
if (!$hasImagefield) {
$errorimgpage = $this->_("The selected page has no field for images. Choose a page with images field or add an images field to this page's template");
}
}
$errorfolder = false;
if ($field->fromfolder && $field->folderpath) {
$folderpath = trim($field->folderpath, '/') . '/';
if (file_exists($this->config->paths->templates . $folderpath)) {
$folder = $this->config->paths->templates . $folderpath;
$path = $this->config->urls->templates . $folderpath;
};
if (file_exists($this->config->paths->assets . $folderpath)) {
$folder = $this->config->paths->assets . $folderpath;
$path = $this->config->urls->assets . $folderpath;
};
if (!isset($folder)) {
$errorfolder = sprintf($this->_("Folder %s doesn't exist."), $folderpath);
} elseif (count(scandir($folder)) == 2) {
$errorfolder = sprintf($this->_("Folder %s is empty. You need to upload images."), $path);
}
}
$inputfields = parent::___getConfigInputfields($field);
$sourceswrapper = new InputfieldFieldset;
$sourceswrapper->label = $this->_("Choose Image sources");
/* images from pages*/
$wrapper = new InputfieldFieldset;
$wrapper->label = $this->_("Pick images from a page?");
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'frompages');
$f->label = $this->_("Pick images from a page?");
$f->attr('checked', $field->frompages ? 'checked' : '');
$f->attr('value', $field->frompages);
$wrapper->add($f);
/** @var Field $f */
$f = $this->modules->get('InputfieldPageListSelect');
$f->attr('name', 'imagespage');
$f->parent_id = 0; // 0 to include homepage
$f->label = $this->_("Choose a page to get images from");
$f->description = $this->_("This page will be used to supply images");
$f->attr('value', $field->imagespage);
$f->showIf('frompages=1');
$f->requiredIf('frompages=1');
$f->getErrors(true);
if ($errorimgpage) $f->error($errorimgpage);
$wrapper->add($f);
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'includechildren');
$f->label = $this->_("Include child pages of above page as source for images?");
$f->description = $this->_('If checked, all child pages of above page will also supply images to the inputfield');
$f->attr('checked', $field->includechildren ? 'checked' : '');
$f->attr('value', $field->includechildren);
$f->showIf('frompages=1');
$wrapper->add($f);
$sourceswrapper->add($wrapper);
/* images from folder*/
$wrapper = new InputfieldFieldset;
$wrapper->label = $this->_("Pick images from a defined folder?");
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'fromfolder');
$f->label = $this->_("Pick images from a defined folder?");
// $f->description = $this->_('If checked, all child pages of above page will serve images to the inputfield');
$f->attr('checked', $field->fromfolder ? 'checked' : '');
$f->attr('value', $field->fromfolder);
$wrapper->add($f);
/** @var Field $f */
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'folderpath');
$f->label = $this->_("The folder containing the images.");
$f->attr('value', trim($field->folderpath, '/') . '/');
$f->description = sprintf($this->_('A path relative to the **%1$s** or **%2$s**'), $this->config->urls->templates, $this->config->urls->assets);
$f->notes = $this->_("When the files are located in /site/templates/images/icons/, type: images/icons/");
$f->notes .= ' ' . $this->_("When the files are located in /site/assets/icons/, type: icons/");
$f->getErrors(true);
if ($errorfolder) {
$f->error($errorfolder);
$this->error($errorfolder);
}
$f->showIf('fromfolder=1');
if ($field->fromfolder) $f->required(1);
if ($field->fromfolder && !$field->folderpath) $f->error('You must enter a folder path');
$wrapper->add($f);
/** @var Field $f */
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'maxSize');
$f->label = $this->_('Max Megabytes for images in folder');
$f->description = $this->_('Specify in MB. Leave empty for no size restriction');
$f->notes = $this->_('Examples: 1MB, 2.5MB, 10MB');
$f->attr('value', $field->maxSize ? $field->maxSize : '');
$f->showIf = 'fromfolder=1';
$f->columnWidth = 50;
$wrapper->add($f);
/** @var InputfieldTextarea $f */
$f = $this->modules->get('InputfieldTextarea');
$f->attr('name', 'allowed_extensions');
$f->attr('value', $field->get('allowed_extensions') ? $field->get('allowed_extensions') : "gif jpg jpeg png svg");
$f->attr('rows', 3);
$f->label = $this->_('Valid File Extensions for Images in Folder');
$f->description = $this->_('Enter all file extensions allowed to upload into folder. Separate each extension by a space. No periods or commas. This field is not case sensitive.');
$f->showIf = 'fromfolder=1';
$f->columnWidth = 50;
$wrapper->append($f);
$sourceswrapper->add($wrapper);
/* images from page*/
$wrapper = new InputfieldFieldset;
$wrapper->label = $this->_("Pick images from the page being edited?");
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'fromeditpage');
$f->label2 = $this->_("Pick from the page being edited?");
$f->skipLabel(4);
$f->description = $this->_('Images from the page that is being edited, will be supplied. The page must have an images field for this option to work.');
$f->attr('checked', $field->fromeditpage ? 'checked' : '');
$f->attr('value', $field->fromeditpage);
$wrapper->add($f);
$sourceswrapper->add($wrapper);
/* images from any page*/
$wrapper = new InputfieldFieldset;
$wrapper->label = $this->_("Pick images from any page of the site?");
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'anypage');
$f->label2 = $this->_("Pick from any page?");
$f->skipLabel(4);
$f->description = $this->_('You can choose any page to pick images from.');
$f->attr('checked', $field->anypage ? 'checked' : '');
$f->attr('value', $field->anypage);
$wrapper->add($f);
$sourceswrapper->add($wrapper);
$inputfields->add($sourceswrapper);
/* image fields settings */
if ($field->imagespage || $field->fromeditpage || $field->anypage) {
$imagefieldswrapper = new InputfieldFieldset;
$imagefieldswrapper->label = $this->_("Choose Image fields");
/** @var Field $f */
$f = $this->modules->get('InputfieldCheckboxes');
$f->attr('name', 'imagesfields');
$f->optionColumns = 1;
// $f->showIf('imagespage|fromeditpage!=""');
// $f->requiredIf('imagespage|fromeditpage!=""');
$f->label = $this->_("Choose image fields that supply images");
$f->description = $this->_('Images from these fields will be supplied to the image picker.');
$f->required(1);
$fields = $this->wire('fields')->find('type=FieldtypeImage');
$options = array();
foreach ($fields as $fd) $options[$fd->name] = $fd->name;
$f->options = $options;
$f->attr('value', $field->imagesfields);
$imagefieldswrapper->add($f);
$inputfields->add($imagefieldswrapper);
}
/* general settings */
$wrapper = new InputfieldFieldset;
$wrapper->label = $this->_("General settings");
/** @var Field $f */
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'previewWidth');
$f->label = $this->_("Width of the selected image in px");
$f->description = $this->_('Sets the width of the selected image inside the inputfield, defaults to 200px');
$f->attr('value', $field->previewWidth);
// $inputfields->add($f);
$wrapper->add($f);
/** @var Field $f */
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'thumbWidth');
$f->label = $this->_("Width of thumbnail images in px");
$f->description = $this->_('Sets the width of the thumbnail images inside the input field, defaults to 100px');
$f->attr('value', $field->thumbWidth);
// $inputfields->add($f);
$wrapper->add($f);
$inputfields->add($wrapper);
/** @var Field $f */
$f = $this->modules->get('InputfieldMarkup');
$f->attr('name', 'FieldtypeImageReference_instructions');
$f->label = $this->_("How to use this field in a template");
$f->attr('value', '<code><img src="<?= $page->' . $field->name . '->url ?>"> // renders the image<br>
$page->' . $field->name . '->svgcontent // returns the text content for inlining SVG (works only with SVG images)<br><br>
All properties and methods for Pageimage can be used (resizing, cropping etc.)<br>
$thumb = $page->' . $field->name . '->width(100)<br><br>
Field is fully searchable for image filename by API<br>
$pages->find("' . $field->name . '.filename=xyz.png")<br><br>
// set an image via API<br>
$p = $pages->get(1001);<br>
$image = $p->image; // returns a Pageimage object<br>
$p->of(false);<br>
$p->set('. $field->name . ', $image); // sets the Pageimage object to the ImageReference field. This gets converted to a JSON string internally for storage<br>
$p->save();</code>');
$inputfields->add($f);
return $inputfields;
}
}