-
Notifications
You must be signed in to change notification settings - Fork 191
/
Pagefile.php
1030 lines (943 loc) · 30 KB
/
Pagefile.php
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace ProcessWire;
/**
* ProcessWire Pagefile
*
* #pw-summary Represents a single file item attached to a page, typically via a File Fieldtype.
* #pw-summary-traversal For the most part you’ll want to traverse from the parent `Pagefiles` object than these methods.
* #pw-summary-manipulation Remember to follow up any manipulations with a `$pages->save()` call.
* #pw-summary-tags Be sure to see the `Pagefiles::getTag()` and `Pagesfiles::findTag()` methods, which enable you retrieve files by tag.
* #pw-use-constructor
* #pw-body =
* Pagefile objects are contained by a `Pagefiles` object.
* #pw-body
*
* ProcessWire 3.x, Copyright 2018 by Ryan Cramer
* https://processwire.com
*
* @property-read string $url URL to the file on the server.
* @property-read string $httpUrl URL to the file on the server including scheme and hostname.
* @property-read string $URL Same as $url property but with browser cache busting query string appended. #pw-group-other
* @property-read string $HTTPURL Same as the cache-busting uppercase “URL” property, but includes scheme and hostname. #pw-group-other
* @property-read string $filename full disk path to the file on the server.
* @property-read string $name Returns the filename without the path, same as the "basename" property.
* @property-read string $hash Get a unique hash (for the page) representing this Pagefile.
* @property-read string $tagsArray Get file tags as an array. #pw-group-tags @since 3.0.17
* @property int $sort Sort order in database. #pw-group-other
* @property string $basename Returns the filename without the path.
* @property string $description Value of the file’s description field (string), if enabled. Note you can also set this property directly.
* @property string $tags Value of the file’s tags field (string), if enabled. #pw-group-tags
* @property string $ext File’s extension (i.e. last 3 or so characters)
* @property int $filesize File size (number of bytes).
* @property int $modified Unix timestamp of when Pagefile (file, description or tags) was last modified. #pw-group-date-time
* @property int $mtime Unix timestamp of when file (only) was last modified. #pw-group-date-time
* @property int $created Unix timestamp of when file was created. #pw-group-date-time
* @property string $filesizeStr File size as a formatted string, i.e. “123 Kb”.
* @property Pagefiles $pagefiles The Pagefiles WireArray that contains this file. #pw-group-other
* @property Page $page The Page object that this file is part of. #pw-group-other
* @property Field $field The Field object that this file is part of. #pw-group-other
*
* @method void install($filename)
* @method string httpUrl()
* @method string noCacheURL($http = false)
*
*/
class Pagefile extends WireData {
/**
* Timestamp 'created' used by pagefiles that are temporary, not yet published
*
*/
const createdTemp = 10;
/**
* Reference to the owning collection of Pagefiles
*
* @var Pagefiles
*
*/
protected $pagefiles;
/**
* Extra file data
*
* @var array
*
*/
protected $filedata = array();
/**
* Construct a new Pagefile
*
* ~~~~~
* // Construct a new Pagefile, assumes that $page->files is a FieldtypeFile Field
* $pagefile = new Pagefile($page->files, '/path/to/file.pdf');
* ~~~~~
*
* @param Pagefiles $pagefiles The Pagefiles WireArray that will contain this file.
* @param string $filename Full path and filename to this Pagefile.
*
*/
public function __construct(Pagefiles $pagefiles, $filename) {
$this->pagefiles = $pagefiles;
if(strlen($filename)) $this->setFilename($filename);
$this->set('description', '');
$this->set('tags', '');
$this->set('formatted', false); // has an output formatter been run on this Pagefile?
$this->set('modified', 0);
$this->set('created', 0);
}
/**
* Set the filename associated with this Pagefile.
*
* No need to call this as it's already called from the constructor.
* This exists so that Pagefile/Pageimage descendents can create cloned variations, if applicable.
*
* #pw-internal
*
* @param string $filename
*
*/
public function setFilename($filename) {
$basename = basename($filename);
if(DIRECTORY_SEPARATOR != '/') $filename = str_replace('\\' . $basename, '/' . $basename, $filename); // To correct issue with XAMPP in Windows
if($basename != $filename && strpos($filename, $this->pagefiles->path()) !== 0) {
$this->install($filename);
} else {
$this->set('basename', $basename);
}
}
/**
* Install this Pagefile
*
* Implies copying the file to the correct location (if not already there), and populating its name.
* The given $filename may be local (path) or external (URL).
*
* #pw-hooker
*
* @param string $filename Full path and filename of file to install, or http/https URL to pull file from.
* @throws WireException
*
*/
protected function ___install($filename) {
$basename = $filename;
if(strpos($basename, '?') !== false) {
list($basename, $queryString) = explode('?', $basename);
if($queryString) {} // do not use in basename
}
if(empty($basename)) throw new WireException("Empty filename");
$basename = $this->pagefiles->cleanBasename($basename, true, false, true);
$pathInfo = pathinfo($basename);
$basename = basename($basename, ".$pathInfo[extension]");
$basenameNoExt = $basename;
$basename .= ".$pathInfo[extension]";
// ensure filename is unique
$cnt = 0;
while(file_exists($this->pagefiles->path() . $basename)) {
$cnt++;
$basename = "$basenameNoExt-$cnt.$pathInfo[extension]";
}
$destination = $this->pagefiles->path() . $basename;
if(strpos($filename, '://') === false) {
if(!is_readable($filename)) throw new WireException("Unable to read: $filename");
if(!copy($filename, $destination)) throw new WireException("Unable to copy: $filename => $destination");
} else {
$http = $this->wire(new WireHttp());
// note: download() method throws excepton on failure
$http->download($filename, $destination);
// download was successful
}
$this->wire('files')->chmod($destination);
$this->changed('file');
parent::set('basename', $basename);
}
/**
* Sets a value in this Pagefile
*
* Externally, this would be used to set the file’s basename or description
*
* #pw-internal
*
* @param string $key
* @param mixed $value
* @return Pagefile|WireData
*
*/
public function set($key, $value) {
if($key == 'basename') {
$value = $this->pagefiles->cleanBasename($value, false);
} else if($key == 'description') {
return $this->setDescription($value);
} else if($key == 'modified') {
$value = ctype_digit("$value") ? (int) $value : strtotime($value);
} else if($key == 'created') {
$value = ctype_digit("$value") ? (int) $value : strtotime($value);
} else if($key == 'tags') {
$this->tags($value);
return $this;
} else if($key == 'filedata') {
if(is_array($value)) $this->filedata($value);
return $this;
}
if(strpos($key, 'description') === 0 && preg_match('/^description(\d+)$/', $value, $matches)) {
// check if a language description is being set manually by description123 where 123 is language ID
$languages = $this->wire('languages');
if($languages) {
$language = $languages->get((int) $matches[1]);
if($language && $language->id) return $this->setDescription($value, $language);
}
}
return parent::set($key, $value);
}
/**
* Get or set filedata
*
* Filedata is any additional data that you want to store with the file’s database record.
*
*
* - To get a value, specify just the $key argument. Null is returned if request value is not present.
* - To get all values, omit all arguments. An associative array will be returned.
* - To set a value, specify the $key and the $value to set.
* - To set all values at once, specify an associative array for the $key argument.
* - To unset, specify boolean false (or null) for $key, and the name of the property to unset as $value.
* - To unset, you can also get all values, unset it from the retuned array, and set the array back.
*
* #pw-internal
*
* @param string|array|false|null $key Specify array to set all file data, or key (string) to set or get a property,
* Or specify boolean false to remove key specified by $value argument.
* @param null|string|array|int|float $value Specify a value to set for given property
* @return Pagefile|Pageimage|array|string|int|float|bool|null
*
*/
public function filedata($key = '', $value = null) {
$filedata = $this->filedata;
$changed = false;
if($key === false || $key === null) {
// unset property named in $value
if(!empty($value) && isset($filedata[$value])) {
unset($this->filedata[$value]);
$changed = true;
}
} else if(empty($key)) {
// return all
return $filedata;
} else if(is_array($key)) {
// set all
if($key != $filedata) {
$this->filedata = $key;
$changed = true;
}
} else if($value === null) {
// return value for key
return isset($this->filedata[$key]) ? $this->filedata[$key] : null;
} else {
// set value for key
if(!isset($filedata[$key]) || $filedata[$key] != $value) {
$this->filedata[$key] = $value;
$changed = true;
}
}
if($changed) {
$this->trackChange('filedata', $filedata, $this->filedata);
if($this->page && $this->field) $this->page->trackChange($this->field->name);
}
return $this;
}
/**
* Set a description, optionally parsing JSON language-specific descriptions to separate properties
*
* @param string|array $value
* @param Page|Language Langage to set it for. Omit to determine automatically.
* @return $this
*
*/
protected function setDescription($value, Page $language = null) {
/** @var Languages $languages */
$languages = $this->wire('languages');
/** @var Language|null $language */
$field = $this->field;
$noLang = $field && $field->get('noLang'); // noLang setting to disable multi-language from InputfieldFile
if(!is_null($language) && $language->id) {
$name = "description";
if(!$language->isDefault() && !$noLang) {
$name .= $language->id;
}
parent::set($name, $value);
if($name != 'description' && $this->isChanged($name)) $this->trackChange('description');
return $this;
}
if(is_array($value)) {
$values = $value;
} else {
// check if it contains JSON?
$first = substr($value, 0, 1);
$last = substr($value, -1);
if(($first == '{' && $last == '}') || ($first == '[' && $last == ']')) {
$values = json_decode($value, true);
} else {
$values = array();
}
}
$numChanges = 0;
if($values && count($values)) {
$n = 0;
foreach($values as $id => $v) {
// first item is always default language. this ensures description will still
// work even if language support is later uninstalled.
$name = 'description';
if($noLang && $n > 0) break;
$n++;
if(ctype_digit("$id")) {
$id = (int) $id;
if(!$id) $id = '';
$name = $n > 0 ? "description$id" : "description";
} else if($id === 'default') {
$name = 'description';
} else if($languages) {
$language = $languages->get($id); // i.e. "default" or "es"
if(!$language->id) continue;
$name = $language->isDefault() ? "description" : "description$language->id";
}
parent::set($name, $v);
if($this->isChanged($name)) $numChanges++;
}
} else {
// no JSON values so assume regular language description
$languages = $this->wire('languages');
$language = $languages ? $this->wire('user')->language : null;
if($languages && $language && !$noLang && !$language->isDefault()) {
$name = "description$language->id";
} else {
$name = "description";
}
parent::set($name, $value);
if($this->isChanged($name)) $numChanges++;
}
if($numChanges && !$this->isChanged('description')) $this->trackChange('description');
return $this;
}
/**
* Get or set the file’s description (with multi-language support).
*
* When not in a multi-language environment, you can still use this method but we recommend using the simpler method of just
* getting/seting the `Pagefile::$description` property directly instead.
*
* ~~~~~
* // Get a Pagefile to work with
* $pagefile = $page->files->first();
*
* // Setting description
* $pagefile->description('en', 'Setting English description');
* $pagefile->description('de', 'Setting German description');
*
* // Getting description for current language (whatever it happens to be)
* echo $pagefile->description();
*
* // Getting description for language "de"
* echo $pagefile->description('de');
* ~~~~~
*
* #pw-group-common
* #pw-group-manipulation
*
* @param null|bool|Language|array
* - To GET in current user language: Omit arguments or specify null.
* - To GET in another language: Specify a Language name, id or object.
* - To GET in all languages as a JSON string: Specify boolean true (if LanguageSupport not installed, regular string returned).
* - To GET in all languages as an array indexed by language name: Specify boolean true for both arguments.
* - To SET for a language: Specify a language name, id or object, plus the $value as the 2nd argument.
* - To SET in all languages as a JSON string: Specify boolean true, plus the JSON string $value as the 2nd argument (internal use only).
* - To SET in all languages as an array: Specify the array here, indexed by language ID or name, and omit 2nd argument.
* @param null|string $value Specify only when you are setting (single language) rather than getting a value.
* @return string
*
*/
public function description($language = null, $value = null) {
if($language === true && $value === true) {
// return all in array indexed by language name
/** @var Languages $languages */
$languages = $this->wire('languages');
if(!$languages) return array('default' => parent::get('description'));
$value = array();
foreach($languages as $language) {
$value[$language->name] = (string) parent::get("description" . ($language->isDefault() ? '' : $language->id));
}
return $value;
}
if(!is_null($value)) {
// set description mode
if($language === true) {
// set all language descriptions
$this->setDescription($value);
} else {
// set specific language description
$this->setDescription($value, $language);
}
return $value;
}
if(is_array($language)) {
// set all from array, then return description in current language
$this->setDescription($language);
$language = null;
$value = null;
}
if((is_string($language) || is_int($language)) && $this->wire('languages')) {
// convert named or ID'd languages to Language object
$language = $this->wire('languages')->get($language);
}
if(is_null($language)) {
// return description for current user language, or inherit from default if not available
$user = $this->wire('user');
$value = null;
if($user->language && $user->language->id) $value = parent::get("description{$user->language}");
if(empty($value)) {
// inherit default language value
$value = parent::get("description");
}
} else if($language === true) {
// return JSON string of all languages if applicable
$languages = $this->wire('languages');
if($languages && $languages->count() > 1) {
$values = array(0 => parent::get("description"));
foreach($languages as $lang) {
if($lang->isDefault()) continue;
$v = parent::get("description$lang");
if(empty($v)) continue;
$values[$lang->id] = $v;
}
$flags = defined("JSON_UNESCAPED_UNICODE") ? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES : 0; // more fulltext friendly
$value = json_encode($values, $flags);
} else {
// no languages present so just return string with description
$value = parent::get("description");
}
} else if(is_object($language) && $language->id) {
// return description for specific language or blank if not available
if($language->isDefault()) $value = parent::get("description");
else $value = parent::get("description$language");
}
// we only return strings, so return blank rather than null
if(is_null($value)) $value = '';
return $value;
}
/**
* Get a value from this Pagefile
*
* #pw-internal
*
* @param string $key
* @return mixed Returns null if value does not exist
*
*/
public function get($key) {
$value = null;
if($key == 'name') $key = 'basename';
if($key == 'pathname') $key = 'filename';
switch($key) {
case 'url':
case 'httpUrl':
case 'filename':
case 'description':
case 'tags':
case 'ext':
case 'hash':
case 'filesize':
case 'filesizeStr':
// 'basename' property intentionally excluded
$value = $this->$key();
break;
case 'tagsArray':
$value = $this->tags(true);
break;
case 'URL':
// nocache url
$value = $this->noCacheURL();
break;
case 'HTTPURL':
$value = $this->noCacheURL(true);
break;
case 'pagefiles':
$value = $this->pagefiles;
break;
case 'page':
$value = $this->pagefiles->getPage();
break;
case 'field':
$value = $this->pagefiles->getField();
break;
case 'modified':
case 'created':
$value = parent::get($key);
if(empty($value)) {
$value = filemtime($this->filename());
parent::set($key, $value);
}
break;
case 'fileData':
case 'filedata':
$value = $this->filedata();
break;
case 'mtime':
$value = filemtime($this->filename());
break;
}
if(is_null($value)) return parent::get($key);
return $value;
}
/**
* Hookable no-cache URL
*
* #pw-internal
*
* @param bool $http Include scheme and hostname?
* @return string
*
*/
public function ___noCacheURL($http = false) {
return ($http ? $this->httpUrl() : $this->url()) . '?nc=' . @filemtime($this->filename());
}
/**
* Return the next sibling Pagefile in the parent Pagefiles, or NULL if at the end.
*
* #pw-group-traversal
*
* @return Pagefile|null
*
*/
public function getNext() {
return $this->pagefiles->getNext($this);
}
/**
* Return the previous sibling Pagefile in the parent Pagefiles, or NULL if at the beginning.
*
* #pw-group-traversal
*
* @return Pagefile|null
*
*/
public function getPrev() {
return $this->pagefiles->getPrev($this);
}
/**
* Return the web accessible URL to this Pagefile.
*
* ~~~~~
* // Example of using the url method/property
* foreach($page->files as $file) {
* echo "<li><a href='$file->url'>$file->description</a></li>";
* }
* ~~~~~
*
* #pw-hooks
* #pw-common
*
* @return string
* @see Pagefile:httpUrl()
*
*/
public function url() {
return $this->wire('hooks')->isHooked('Pagefile::url()') ? $this->__call('url', array()) : $this->___url();
}
/**
* Hookable version of url() method
*
* @return string
*
*/
protected function ___url() {
return $this->pagefiles->url . $this->basename;
}
/**
* Return the web accessible URL (with scheme and hostname) to this Pagefile.
*
* @return string
* @see Pagefile::url()
*
*/
public function ___httpUrl() {
$page = $this->pagefiles->getPage();
$url = substr($page->httpUrl(), 0, -1 * strlen($page->url()));
return $url . $this->url();
}
/**
* Returns the full disk path name filename to the Pagefile.
*
* #pw-hooks
* #pw-common
*
* @return string
*
*/
public function filename() {
return $this->wire('hooks')->isHooked('Pagefile::filename()') ? $this->__call('filename', array()) : $this->___filename();
}
/**
* Hookable version of filename() method
*
*/
protected function ___filename() {
return $this->pagefiles->path . $this->basename;
}
/**
* Returns the basename of this Pagefile (name and extension, without disk path).
*
* @param bool $ext Specify false to exclude the extension (default=true)
* @return string
*
*/
public function basename($ext = true) {
$basename = parent::get('basename');
if(!$ext) $basename = basename($basename, "." . $this->ext());
return $basename;
}
/**
* Get or set the "tags" property, when in use.
*
* ~~~~~
* $file = $page->files->first();
* $tags = $file->tags(); // Get tags string
* $tags = $file->tags(true); // Get tags array
* $file->tags("foo bar baz"); // Set tags to be these 3 tags
* $tags->tags(["foo", "bar", "baz"]); // Same as above, using array
* ~~~~~
*
* #pw-group-tags
* #pw-group-manipulation
*
* @param bool|string|array $value Specify one of the following:
* - Omit to simply return the tags as a string.
* - Boolean true if you want to return tags as an array (rather than string).
* - Boolean false to return tags as an array, with lowercase enforced.
* - String or array if you are setting the tags.
* @return string|array Returns the current tags as a string or an array.
* When an array is returned, it is an associative array where the key and value are both the tag (keys are always lowercase).
* @see Pagefile::addTag(), Pagefile::hasTag(), Pagefile::removeTag()
*
*/
public function tags($value = null) {
if(is_bool($value)) {
// return array of tags
$tags = parent::get('tags');
$tags = str_replace(array(',', '|'), ' ', $tags);
$_tags = explode(' ', $tags);
$tags = array();
foreach($_tags as $key => $tag) {
$tag = trim($tag);
if($value === false) $tag = strtolower($tag); // force lowercase
if(!strlen($tag)) continue;
$tags[strtolower($tag)] = $tag;
}
} else if($value !== null) {
// set tags
if(is_array($value)) $value = implode(' ', $value); // convert to string
$value = $this->wire('sanitizer')->text($value);
if(strpos($value, "\t") !== false) $value = str_replace("\t", " ", $value);
// collapse extra whitespace
while(strpos($value, " ") !== false) $value = str_replace(" ", " ", $value);
parent::set('tags', $value);
$tags = $value;
} else {
// just get tags string
$tags = parent::get('tags');
}
return $tags;
}
/**
* Does this file have the given tag(s)?
*
* ~~~~~
* $file = $page->files->first();
*
* if($file->hasTag('foobar')) {
* // file has the "foobar" tag
* }
*
* if($file->hasTag("foo|baz")) {
* // file has either the foo OR baz tag
* }
*
* if($file->hasTag("foo,baz")) {
* // file has both the foo AND baz tags (since 3.0.17)
* }
* ~~~~~
*
* #pw-changelog 3.0.17 Added support for AND mode, where multiple tags can be specified and all must be present to return true.
* #pw-changelog 3.0.17 OR mode now returns found tag rather than boolean true.
* #pw-group-tags
*
* @param string $tag Specify one of the following:
* - Single tag without whitespace.
* - Multiple tags separated by a "|" to determine if Pagefile has at least one of the tags.
* - Multiple tags separated by a comma to determine if Pagefile has all of the tags. (since 3.0.17)
* @return bool|string True if it has the given tag(s), false if not.
* - If multiple tags were specified separated by a "|", then if at least one was present, this method returns the found tag.
* - If multiple tags were specified separated by a space or comma, and all tags are present, it returns true. (since 3.0.17)
* @see Pagefile::tags(), Pagefile::addTag(), Pagefile::removeTag()
*
*/
public function hasTag($tag) {
$tags = $this->tags(false); // all tags in array, lowercase
if(empty($tags)) return false;
$modeAND = null;
$tag = trim(strtolower($tag));
if(strpos($tag, '|') !== false) {
$findTags = explode('|', $tag);
$modeAND = false;
} else if(strpos($tag, ',') !== false) {
$findTags = explode(',', $tag);
$modeAND = true;
} else {
$findTags = array($tag);
}
$numTags = 0;
$numFound = 0;
$tagFound = '';
foreach($findTags as $tag) {
$tag = trim($tag);
if(!strlen($tag)) continue;
$tag = str_replace(' ', '_', $tag);
$numTags++;
if(isset($tags[$tag])) {
$numFound++;
if($modeAND === false) {
$tagFound = $tag;
break;
}
}
}
if($modeAND === false) {
// OR mode: must have at least one of given tags, and we return the found tag
return $numFound > 0 ? $tagFound : false;
} else if($modeAND === true) {
// AND mode: must have all of the given tags
return $numFound == $numTags;
}
// single tag
return $numFound > 0;
}
/**
* Add the given tag to this file’s tags (if not already present)
*
* ~~~~~
* $file = $page->files->first();
* $file->addTag('foo'); // add single tag
* $file->addTag('foo,bar,baz'); // add multiple tags
* $file->addTag(['foo', 'bar', 'baz']); // same as above, using array
* ~~~~~
*
* #pw-group-tags
* #pw-group-manipulation
*
* @param string|array $tag Tag to add, or array of tags to add, or CSV string of tags to add.
* @return $this
* @since 3.0.17
* @see Pagefile::tags(), Pagefile::hasTag(), Pagefile::removeTag()
*
*/
public function addTag($tag) {
if(is_array($tag)) {
$addTags = $tag;
} else if(strpos($tag, ',') !== false) {
$addTags = explode(',', $tag);
} else {
$addTags = array($tag);
}
$tags = $this->tags(true);
$numAdded = 0;
foreach($addTags as $tag) {
if($this->hasTag($tag)) continue;
$tag = $this->wire('sanitizer')->text(trim($tag));
$tag = str_replace(' ', '_', $tag);
$tags[strtolower($tag)] = $tag;
$numAdded++;
}
if($numAdded) $this->tags($tags);
return $this;
}
/**
* Remove the given tag from this file’s tags (if present)
*
* ~~~~~
* $file = $page->files->first();
* $file->removeTag('foo'); // remove single tag
* $file->removeTag('foo,bar,baz'); // remove multiple tags
* $file->removeTag(['foo', 'bar', 'baz']); // same as above, using array
* ~~~~~
*
* #pw-group-tags
* #pw-group-manipulation
*
* @param string $tag Tag to remove, or array of tags to remove, or CSV string of tags to remove.
* @return $this
* @since 3.0.17
* @see Pagefile::tags(), Pagefile::hasTag(), Pagefile::addTag()
*
*/
public function removeTag($tag) {
$tags = $this->tags(true);
if(!count($tags)) return $this; // no tags to remove
if(is_array($tag)) {
$removeTags = $tag;
} else if(strpos($tag, ',') !== false) {
$removeTags = explode(',', $tag);
} else {
$removeTags = array($tag);
}
$numRemoved = 0;
foreach($removeTags as $tag) {
$tag = strtolower(trim($tag));
$tag = str_replace(' ', '_', $tag);
if(!isset($tags[$tag])) continue;
unset($tags[strtolower($tag)]);
$numRemoved++;
}
if($numRemoved) $this->tags($tags);
return $this;
}
/**
* Has the output already been formatted?
*
* #pw-internal
*
*/
public function formatted() {
return parent::get('formatted') ? true : false;
}
/**
* Returns the filesize in number of bytes.
*
* @return int
*
*/
public function filesize() {
return @filesize($this->filename());
}
/**
* Returns the filesize in a formatted, output-ready string (i.e. "123 kB")
*
* @return string
*
*/
public function filesizeStr() {
return wireBytesStr($this->filesize());
}
/**
* Returns the file’s extension - "pdf", "jpg", etc.
*
* @return string
*
*/
public function ext() {
return substr($this->basename(), strrpos($this->basename(), '.')+1);
}
/**
* When dereferenced as a string, a Pagefile returns its basename
*
* @return string
*
*/
public function __toString() {
return (string) $this->basename;
}
/**
* Return a unique MD5 hash representing this Pagefile.
*
* This hash can be counted on to be unique among all files on a given page, regardless of field.
*
* @return string
*
*/
public function hash() {
$hash = parent::get('hash');
if($hash) return $hash;
$this->set('hash', md5($this->basename()));
return parent::get('hash');
}
/**
* Delete the physical file on disk, associated with this Pagefile
*
* #pw-internal Public API should use removal methods from the parent Pagefiles.
*
* @return bool True on success, false on fail
*
*/
public function unlink() {
if(!strlen($this->basename) || !is_file($this->filename)) return true;
return unlink($this->filename);
}
/**
* Rename this file
*
* Remember to follow this up with a `$page->save()` for the page that the file lives on.
*
* #pw-group-manipulation
*
* @param string $basename New name to use. Must be just the file basename (no path).
* @return string|bool Returns new name (basename) on success, or boolean false if rename failed.
*
*/
public function rename($basename) {
$basename = $this->pagefiles->cleanBasename($basename, true);
if(rename($this->filename, $this->pagefiles->path . $basename)) {
$this->set('basename', $basename);
return $this->basename();
}
return false;
}
/**
* Copy this file to the new specified path
*
* #pw-internal
*
* @param string $path Path (not including basename)
* @return bool result of copy() function
*
*/
public function copyToPath($path) {
$result = copy($this->filename, $path . $this->basename());
if($this->config->chmodFile) chmod($path . $this->basename(), octdec($this->config->chmodFile));
return $result;
}
/**
* Hook called a property changes (from Wire)
*
* Alert the $pagefiles of the change
*
* #pw-internal
*
* @param string $what
* @param mixed $old
* @param mixed $new
*
*/
public function ___changed($what, $old = null, $new = null) {
if(in_array($what, array('description', 'tags', 'file'))) {
$this->set('modified', time());
$this->pagefiles->trackChange('item');
}
parent::___changed($what, $old, $new);
}