-
Notifications
You must be signed in to change notification settings - Fork 78
/
fORMFile.php
1298 lines (1100 loc) · 43.6 KB
/
fORMFile.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
/**
* Provides file manipulation functionality for fActiveRecord classes
*
* @copyright Copyright (c) 2008-2011 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fORMFile
*
* @version 1.0.0b30
* @changes 1.0.0b30 Updated code for the new fUpload API [wb, 2011-08-24]
* @changes 1.0.0b29 Fixed a bug when uploading a new file to a column with an existing file that was not found on the filesystem [wb, 2011-05-10]
* @changes 1.0.0b28 Backwards Compatibility Break - ::configureImageUploadColumn() no longer accepts the optional `$image_type` as the fourth parameter, instead ::addFImageMethodCall() must be called with `saveChanges` as the `$method` and the image type as the first parameter [wb, 2010-11-30]
* @changes 1.0.0b27 Fixed column inheritance to properly handle non-images and inheriting into image upload columns [wb, 2010-09-18]
* @changes 1.0.0b26 Enhanced ::configureColumnInheritance() to ensure both columns specified have been set up as file upload columns [wb, 2010-08-18]
* @changes 1.0.0b25 Updated code to work with the new fORM API [wb, 2010-08-06]
* @changes 1.0.0b24 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b23 Fixed a bug with ::upload() that could cause a method called on a non-object error in relation to the upload directory not being defined [wb, 2010-05-10]
* @changes 1.0.0b22 Updated the TEMP_DIRECTORY constant to not include the trailing slash, code now uses DIRECTORY_SEPARATOR to fix issues on Windows [wb, 2010-04-28]
* @changes 1.0.0b21 Fixed ::set() to perform column inheritance, just like ::upload() does [wb, 2010-03-15]
* @changes 1.0.0b20 Fixed the `set` and `process` methods to return the record instance, changed `upload` methods to return the fFile object, updated ::reflect() with new return values [wb, 2010-03-15]
* @changes 1.0.0b19 Fixed a few missed instances of old fFile method names [wb, 2009-12-16]
* @changes 1.0.0b18 Updated code for the new fFile API [wb, 2009-12-16]
* @changes 1.0.0b17 Updated code for the new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
* @changes 1.0.0b16 fImage method calls for file upload columns will no longer cause notices due to a missing image type [wb, 2009-09-09]
* @changes 1.0.0b15 ::addFImageMethodCall() no longer requires column be an image upload column, inheritance to an image column now only happens for fImage objects [wb, 2009-07-29]
* @changes 1.0.0b14 Updated to use new fORM::registerInspectCallback() method [wb, 2009-07-13]
* @changes 1.0.0b13 Updated code for new fORM API [wb, 2009-06-15]
* @changes 1.0.0b12 Changed replacement values in preg_replace() calls to be properly escaped [wb, 2009-06-11]
* @changes 1.0.0b11 Updated code to use new fValidationException::formatField() method [wb, 2009-06-04]
* @changes 1.0.0b10 Fixed a bug where an inherited file upload column would not be properly re-set with an `existing-` input [wb, 2009-05-26]
* @changes 1.0.0b9 ::upload() and ::set() now set the `$values` entry to `NULL` for filenames that are empty [wb, 2009-03-02]
* @changes 1.0.0b8 Changed ::set() to accept objects and reject directories [wb, 2009-01-21]
* @changes 1.0.0b7 Changed the class to use the new fFilesystem::createObject() method [wb, 2009-01-21]
* @changes 1.0.0b6 Old files are now checked against the current file to prevent removal of an in-use file [wb, 2008-12-23]
* @changes 1.0.0b5 Fixed ::replicate() to ensure the temp directory exists and ::set() to use the temp directory [wb, 2008-12-23]
* @changes 1.0.0b4 ::objectify() no longer throws an exception when a file can't be found [wb, 2008-12-18]
* @changes 1.0.0b3 Added ::replicate() so that replicated files get pu in the temp directory [wb, 2008-12-12]
* @changes 1.0.0b2 Fixed a bug with objectifying file columns [wb, 2008-11-24]
* @changes 1.0.0b The initial implementation [wb, 2008-05-28]
*/
class fORMFile
{
// The following constants allow for nice looking callbacks to static methods
const addFImageMethodCall = 'fORMFile::addFImageMethodCall';
const addFUploadMethodCall = 'fORMFile::addFUploadMethodCall';
const begin = 'fORMFile::begin';
const commit = 'fORMFile::commit';
const configureColumnInheritance = 'fORMFile::configureColumnInheritance';
const configureFileUploadColumn = 'fORMFile::configureFileUploadColumn';
const configureImageUploadColumn = 'fORMFile::configureImageUploadColumn';
const delete = 'fORMFile::delete';
const deleteOld = 'fORMFile::deleteOld';
const encode = 'fORMFile::encode';
const inspect = 'fORMFile::inspect';
const moveFromTemp = 'fORMFile::moveFromTemp';
const objectify = 'fORMFile::objectify';
const populate = 'fORMFile::populate';
const prepare = 'fORMFile::prepare';
const process = 'fORMFile::process';
const processImage = 'fORMFile::processImage';
const reflect = 'fORMFile::reflect';
const replicate = 'fORMFile::replicate';
const reset = 'fORMFile::reset';
const rollback = 'fORMFile::rollback';
const set = 'fORMFile::set';
const upload = 'fORMFile::upload';
const validate = 'fORMFile::validate';
/**
* The temporary directory to use for various tasks
*
* @internal
*
* @var string
*/
const TEMP_DIRECTORY = '__flourish_temp';
/**
* Defines how columns can inherit uploaded files
*
* @var array
*/
static private $column_inheritence = array();
/**
* Methods to be called on fUpload before the file is uploaded
*
* @var array
*/
static private $fupload_method_calls = array();
/**
* Columns that can be filled by file uploads
*
* @var array
*/
static private $file_upload_columns = array();
/**
* Methods to be called on the fImage instance
*
* @var array
*/
static private $fimage_method_calls = array();
/**
* Columns that can be filled by image uploads
*
* @var array
*/
static private $image_upload_columns = array();
/**
* Keeps track of the nesting level of the filesystem transaction so we know when to start, commit, rollback, etc
*
* @var integer
*/
static private $transaction_level = 0;
/**
* Adds an fImage method call to the image manipulation for a column if an image file is uploaded
*
* Any call to fImage::saveChanges() will be called last. If no explicit
* method call to fImage::saveChanges() is made, it will be called
* implicitly with default parameters.
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to call the method for
* @param string $method The fImage method to call
* @param array $parameters The parameters to pass to the method
* @return void
*/
static public function addFImageMethodCall($class, $column, $method, $parameters=array())
{
$class = fORM::getClass($class);
if (empty(self::$file_upload_columns[$class][$column])) {
throw new fProgrammerException(
'The column specified, %s, has not been configured as a file or image upload column',
$column
);
}
if (empty(self::$fimage_method_calls[$class])) {
self::$fimage_method_calls[$class] = array();
}
if (empty(self::$fimage_method_calls[$class][$column])) {
self::$fimage_method_calls[$class][$column] = array();
}
self::$fimage_method_calls[$class][$column][] = array(
'method' => $method,
'parameters' => $parameters
);
}
/**
* Adds an fUpload method call to the fUpload initialization for a column
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to call the method for
* @param string $method The fUpload method to call
* @param array $parameters The parameters to pass to the method
* @return void
*/
static public function addFUploadMethodCall($class, $column, $method, $parameters=array())
{
if ($method == 'enableOverwrite') {
throw new fProgrammerException(
'The method specified, %1$s, is not compatible with how %2$s stores and associates files with records',
$method,
'fORMFile'
);
}
$class = fORM::getClass($class);
if (empty(self::$file_upload_columns[$class][$column])) {
throw new fProgrammerException(
'The column specified, %s, has not been configured as a file or image upload column',
$column
);
}
if (empty(self::$fupload_method_calls[$class])) {
self::$fupload_method_calls[$class] = array();
}
if (empty(self::$fupload_method_calls[$class][$column])) {
self::$fupload_method_calls[$class][$column] = array();
}
self::$fupload_method_calls[$class][$column][] = array(
'method' => $method,
'parameters' => $parameters
);
}
/**
* Begins a transaction, or increases the level
*
* @internal
*
* @return void
*/
static public function begin()
{
// If the transaction was started by something else, don't even track it
if (self::$transaction_level == 0 && fFilesystem::isInsideTransaction()) {
return;
}
self::$transaction_level++;
if (!fFilesystem::isInsideTransaction()) {
fFilesystem::begin();
}
}
/**
* Commits a transaction, or decreases the level
*
* @internal
*
* @return void
*/
static public function commit()
{
// If the transaction was started by something else, don't even track it
if (self::$transaction_level == 0) {
return;
}
self::$transaction_level--;
if (!self::$transaction_level) {
fFilesystem::commit();
}
}
/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static private function compose($message)
{
$args = array_slice(func_get_args(), 1);
if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}
/**
* Sets a column to be a file upload column
*
* Configuring a column to be a file upload column means that whenever
* fActiveRecord::populate() is called for an fActiveRecord object, any
* appropriately named file uploads (via `$_FILES`) will be moved into
* the directory for this column.
*
* Setting the column to a file path will cause the specified file to
* be copied into the directory for this column.
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to set as a file upload column
* @param fDirectory|string $directory The directory to upload/move to
* @return void
*/
static public function configureFileUploadColumn($class, $column, $directory)
{
$class = fORM::getClass($class);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve($class);
$data_type = $schema->getColumnInfo($table, $column, 'type');
$valid_data_types = array('varchar', 'char', 'text');
if (!in_array($data_type, $valid_data_types)) {
throw new fProgrammerException(
'The column specified, %1$s, is a %2$s column. Must be one of %3$s to be set as a file upload column.',
$column,
$data_type,
join(', ', $valid_data_types)
);
}
if (!is_object($directory)) {
$directory = new fDirectory($directory);
}
if (!$directory->isWritable()) {
throw new fEnvironmentException(
'The file upload directory, %s, is not writable',
$directory->getPath()
);
}
$camelized_column = fGrammar::camelize($column, TRUE);
fORM::registerActiveRecordMethod(
$class,
'upload' . $camelized_column,
self::upload
);
fORM::registerActiveRecordMethod(
$class,
'set' . $camelized_column,
self::set
);
fORM::registerActiveRecordMethod(
$class,
'encode' . $camelized_column,
self::encode
);
fORM::registerActiveRecordMethod(
$class,
'prepare' . $camelized_column,
self::prepare
);
fORM::registerReflectCallback($class, self::reflect);
fORM::registerInspectCallback($class, $column, self::inspect);
fORM::registerReplicateCallback($class, $column, self::replicate);
fORM::registerObjectifyCallback($class, $column, self::objectify);
$only_once_hooks = array(
'post-begin::delete()' => self::begin,
'pre-commit::delete()' => self::delete,
'post-commit::delete()' => self::commit,
'post-rollback::delete()' => self::rollback,
'post::populate()' => self::populate,
'post-begin::store()' => self::begin,
'post-validate::store()' => self::moveFromTemp,
'pre-commit::store()' => self::deleteOld,
'post-commit::store()' => self::commit,
'post-rollback::store()' => self::rollback,
'post::validate()' => self::validate
);
foreach ($only_once_hooks as $hook => $callback) {
if (!fORM::checkHookCallback($class, $hook, $callback)) {
fORM::registerHookCallback($class, $hook, $callback);
}
}
if (empty(self::$file_upload_columns[$class])) {
self::$file_upload_columns[$class] = array();
}
self::$file_upload_columns[$class][$column] = $directory;
}
/**
* Takes one file or image upload columns and sets it to inherit any uploaded/set files from another column
*
* @param mixed $class The class name or instance of the class
* @param string $column The column that will inherit the uploaded file
* @param string $inherit_from_column The column to inherit the uploaded file from
* @return void
*/
static public function configureColumnInheritance($class, $column, $inherit_from_column)
{
$class = fORM::getClass($class);
if (empty(self::$file_upload_columns[$class][$column])) {
throw new fProgrammerException(
'The column specified, %s, has not been configured as a file upload column',
$column
);
}
if (empty(self::$file_upload_columns[$class][$inherit_from_column])) {
throw new fProgrammerException(
'The column specified, %s, has not been configured as a file upload column',
$column
);
}
if (empty(self::$column_inheritence[$class])) {
self::$column_inheritence[$class] = array();
}
if (empty(self::$column_inheritence[$class][$inherit_from_column])) {
self::$column_inheritence[$class][$inherit_from_column] = array();
}
self::$column_inheritence[$class][$inherit_from_column][] = $column;
}
/**
* Sets a column to be an image upload column
*
* This method works exactly the same as ::configureFileUploadColumn()
* except that only image files are accepted.
*
* To alter an image, including the file type, use ::addFImageMethodCall().
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to set as a file upload column
* @param fDirectory|string $directory The directory to upload to
* @return void
*/
static public function configureImageUploadColumn($class, $column, $directory)
{
self::configureFileUploadColumn($class, $column, $directory);
$class = fORM::getClass($class);
$camelized_column = fGrammar::camelize($column, TRUE);
fORM::registerActiveRecordMethod(
$class,
'process' . $camelized_column,
self::process
);
if (empty(self::$image_upload_columns[$class])) {
self::$image_upload_columns[$class] = array();
}
self::$image_upload_columns[$class][$column] = TRUE;
self::addFUploadMethodCall(
$class,
$column,
'setMimeTypes',
array(
array(
'image/gif',
'image/jpeg',
'image/pjpeg',
'image/png'
),
self::compose('The file uploaded is not an image')
)
);
}
/**
* Deletes the files for this record
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function delete($object, &$values, &$old_values, &$related_records, &$cache)
{
$class = get_class($object);
foreach (self::$file_upload_columns[$class] as $column => $directory) {
// Remove the current file for the column
if ($values[$column] instanceof fFile) {
$values[$column]->delete();
}
// Remove the old files for the column
foreach (fActiveRecord::retrieveOld($old_values, $column, array(), TRUE) as $file) {
if ($file instanceof fFile) {
$file->delete();
}
}
}
}
/**
* Deletes old files for this record that have been replaced by new ones
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function deleteOld($object, &$values, &$old_values, &$related_records, &$cache)
{
$class = get_class($object);
// Remove the old files for the column
foreach (self::$file_upload_columns[$class] as $column => $directory) {
$current_file = $values[$column];
foreach (fActiveRecord::retrieveOld($old_values, $column, array(), TRUE) as $file) {
if ($file instanceof fFile && (!$current_file instanceof fFile || $current_file->getPath() != $file->getPath())) {
$file->delete();
}
}
}
}
/**
* Encodes a file for output into an HTML `input` tag
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return void
*/
static public function encode($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
list ($action, $subject) = fORM::parseMethod($method_name);
$column = fGrammar::underscorize($subject);
$filename = ($values[$column] instanceof fFile) ? $values[$column]->getName() : NULL;
if ($filename && strpos($values[$column]->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . $filename) !== FALSE) {
$filename = self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . $filename;
}
return fHTML::encode($filename);
}
/**
* Adds metadata about features added by this class
*
* @internal
*
* @param string $class The class being inspected
* @param string $column The column being inspected
* @param array &$metadata The array of metadata about a column
* @return void
*/
static public function inspect($class, $column, &$metadata)
{
if (!empty(self::$image_upload_columns[$class][$column])) {
$metadata['feature'] = 'image';
} elseif (!empty(self::$file_upload_columns[$class][$column])) {
$metadata['feature'] = 'file';
}
$metadata['directory'] = self::$file_upload_columns[$class][$column]->getPath();
}
/**
* Moves uploaded files from the temporary directory to the permanent directory
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function moveFromTemp($object, &$values, &$old_values, &$related_records, &$cache)
{
foreach ($values as $column => $value) {
if (!$value instanceof fFile) {
continue;
}
// If the file is in a temp dir, move it out
if (strpos($value->getParent()->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR) !== FALSE) {
$new_filename = str_replace(self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR, '', $value->getPath());
$new_filename = fFilesystem::makeUniqueName($new_filename);
$value->rename($new_filename, FALSE);
}
}
}
/**
* Turns a filename into an fFile or fImage object
*
* @internal
*
* @param string $class The class this value is for
* @param string $column The column the value is in
* @param mixed $value The value
* @return mixed The fFile, fImage or raw value
*/
static public function objectify($class, $column, $value)
{
if ((!is_string($value) && !is_numeric($value) && !is_object($value)) || !strlen(trim($value))) {
return $value;
}
$path = self::$file_upload_columns[$class][$column]->getPath() . $value;
try {
return fFilesystem::createObject($path);
// If there was some error creating the file, just return the raw value
} catch (fExpectedException $e) {
return $value;
}
}
/**
* Performs the upload action for file uploads during fActiveRecord::populate()
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function populate($object, &$values, &$old_values, &$related_records, &$cache)
{
$class = get_class($object);
foreach (self::$file_upload_columns[$class] as $column => $directory) {
if (fUpload::check($column, FALSE) || fRequest::check('existing-' . $column) || fRequest::check('delete-' . $column)) {
$method = 'upload' . fGrammar::camelize($column, TRUE);
$object->$method();
}
}
}
/**
* Prepares a file for output into HTML by returning filename or the web server path to the file
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return void
*/
static public function prepare($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
list ($action, $subject) = fORM::parseMethod($method_name);
$column = fGrammar::underscorize($subject);
if (sizeof($parameters) > 1) {
throw new fProgrammerException(
'The column specified, %s, does not accept more than one parameter',
$column
);
}
$translate_to_web_path = (empty($parameters[0])) ? FALSE : TRUE;
$value = $values[$column];
if ($value instanceof fFile) {
$path = ($translate_to_web_path) ? $value->getPath(TRUE) : $value->getName();
} else {
$path = NULL;
}
return fHTML::prepare($path);
}
/**
* Takes a directory and creates a temporary directory inside of it - if the temporary folder exists, all files older than 6 hours will be deleted
*
* @param string $folder The folder to create a temporary directory inside of
* @return fDirectory The temporary directory for the folder specified
*/
static private function prepareTempDir($folder)
{
// Let's clean out the upload temp dir
try {
$temp_dir = new fDirectory($folder->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR);
} catch (fValidationException $e) {
$temp_dir = fDirectory::create($folder->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR);
}
$temp_files = $temp_dir->scan();
foreach ($temp_files as $temp_file) {
if (filemtime($temp_file->getPath()) < strtotime('-6 hours')) {
unlink($temp_file->getPath());
}
}
return $temp_dir;
}
/**
* Handles re-processing an existing image file
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return fActiveRecord The record object, to allow for method chaining
*/
static public function process($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
list ($action, $subject) = fORM::parseMethod($method_name);
$column = fGrammar::underscorize($subject);
$class = get_class($object);
self::processImage($class, $column, $values[$column]);
return $object;
}
/**
* Performs image manipulation on an uploaded/set image
*
* @internal
*
* @param string $class The name of the class we are manipulating the image for
* @param string $column The column the image is assigned to
* @param fFile $image The image object to manipulate
* @return void
*/
static public function processImage($class, $column, $image)
{
// If we don't have an image or we haven't set it up to manipulate images, just exit
if (!$image instanceof fImage || empty(self::$fimage_method_calls[$class][$column])) {
return;
}
$save_changes_called = FALSE;
// Manipulate the image
if (!empty(self::$fimage_method_calls[$class][$column])) {
foreach (self::$fimage_method_calls[$class][$column] as $method_call) {
if ($method_call['method'] == 'saveChanges') {
$save_changes_called = TRUE;
}
$callback = array($image, $method_call['method']);
$parameters = $method_call['parameters'];
if (!is_callable($callback)) {
throw new fProgrammerException(
'The fImage method specified, %s, is not a valid method',
$method_call['method'] . '()'
);
}
call_user_func_array($callback, $parameters);
}
}
if (!$save_changes_called) {
call_user_func($image->saveChanges);
}
}
/**
* Adjusts the fActiveRecord::reflect() signatures of columns that have been configured in this class
*
* @internal
*
* @param string $class The class to reflect
* @param array &$signatures The associative array of `{method name} => {signature}`
* @param boolean $include_doc_comments If doc comments should be included with the signature
* @return void
*/
static public function reflect($class, &$signatures, $include_doc_comments)
{
$image_columns = (isset(self::$image_upload_columns[$class])) ? array_keys(self::$image_upload_columns[$class]) : array();
$file_columns = (isset(self::$file_upload_columns[$class])) ? array_keys(self::$file_upload_columns[$class]) : array();
foreach($file_columns as $column) {
$camelized_column = fGrammar::camelize($column, TRUE);
$noun = 'file';
if (in_array($column, $image_columns)) {
$noun = 'image';
}
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Encodes the filename of " . $column . " for output into an HTML form\n";
$signature .= " * \n";
$signature .= " * Only the filename will be returned, any directory will be stripped.\n";
$signature .= " * \n";
$signature .= " * @return string The HTML form-ready value\n";
$signature .= " */\n";
}
$encode_method = 'encode' . $camelized_column;
$signature .= 'public function ' . $encode_method . '()';
$signatures[$encode_method] = $signature;
if (in_array($column, $image_columns)) {
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Takes the existing image and runs it through the prescribed fImage method calls\n";
$signature .= " * \n";
$signature .= " * @return fActiveRecord The record object, to allow for method chaining\n";
$signature .= " */\n";
}
$process_method = 'process' . $camelized_column;
$signature .= 'public function ' . $process_method . '()';
$signatures[$process_method] = $signature;
}
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Prepares the filename of " . $column . " for output into HTML\n";
$signature .= " * \n";
$signature .= " * By default only the filename will be returned and any directory will be stripped.\n";
$signature .= " * The \$include_web_path parameter changes this behaviour.\n";
$signature .= " * \n";
$signature .= " * @param boolean \$include_web_path If the full web path to the " . $noun . " should be included\n";
$signature .= " * @return string The HTML-ready value\n";
$signature .= " */\n";
}
$prepare_method = 'prepare' . $camelized_column;
$signature .= 'public function ' . $prepare_method . '($include_web_path=FALSE)';
$signatures[$prepare_method] = $signature;
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Takes a file uploaded through an HTML form for " . $column . " and moves it into the specified directory\n";
$signature .= " * \n";
$signature .= " * Any columns that were designated as inheriting from this column will get a copy\n";
$signature .= " * of the uploaded file.\n";
$signature .= " * \n";
if ($noun == 'image') {
$signature .= " * Any fImage calls that were added to the column will be processed on the uploaded image.\n";
$signature .= " * \n";
}
$signature .= " * @return fFile The uploaded file\n";
$signature .= " */\n";
}
$upload_method = 'upload' . $camelized_column;
$signature .= 'public function ' . $upload_method . '()';
$signatures[$upload_method] = $signature;
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Takes a file that exists on the filesystem and copies it into the specified directory for " . $column . "\n";
$signature .= " * \n";
if ($noun == 'image') {
$signature .= " * Any fImage calls that were added to the column will be processed on the copied image.\n";
$signature .= " * \n";
}
$signature .= " * @return fActiveRecord The record object, to allow for method chaining\n";
$signature .= " */\n";
}
$set_method = 'set' . $camelized_column;
$signature .= 'public function ' . $set_method . '()';
$signatures[$set_method] = $signature;
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Returns metadata about " . $column . "\n";
$signature .= " * \n";
$signature .= " * @param string \$element The element to return. Must be one of: 'type', 'not_null', 'default', 'valid_values', 'max_length', 'feature', 'directory'.\n";
$signature .= " * @return mixed The metadata array or a single element\n";
$signature .= " */\n";
}
$inspect_method = 'inspect' . $camelized_column;
$signature .= 'public function ' . $inspect_method . '($element=NULL)';
$signatures[$inspect_method] = $signature;
}
}
/**
* Creates a copy of an uploaded file in the temp directory for the newly cloned record
*
* @internal
*
* @param string $class The class this value is for
* @param string $column The column the value is in
* @param mixed $value The value
* @return mixed The cloned fFile object
*/
static public function replicate($class, $column, $value)
{
if (!$value instanceof fFile) {
return $value;
}
// If the file we are replicating is in the temp dir, the copy can live there too
if (strpos($value->getParent()->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR) !== FALSE) {
$value = clone $value;
// Otherwise, the copy of the file must be placed in the temp dir so it is properly cleaned up
} else {
$upload_dir = self::$file_upload_columns[$class][$column];
try {
$temp_dir = new fDirectory($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR);
} catch (fValidationException $e) {
$temp_dir = fDirectory::create($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR);
}
$value = $value->duplicate($temp_dir);
}
return $value;
}
/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$column_inheritence = array();
self::$fupload_method_calls = array();
self::$file_upload_columns = array();
self::$fimage_method_calls = array();
self::$image_upload_columns = array();
self::$transaction_level = 0;
}
/**
* Rolls back a transaction, or decreases the level
*
* @internal
*
* @return void
*/
static public function rollback()
{
// If the transaction was started by something else, don't even track it
if (self::$transaction_level == 0) {
return;
}
self::$transaction_level--;
if (!self::$transaction_level) {
fFilesystem::rollback();
}
}
/**
* Copies a file from the filesystem to the file upload directory and sets it as the file for the specified column
*
* This method will perform the fImage calls defined for the column.
*
* @internal