| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fORMFile |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
@changes |
|---|
| 24 |
@changes |
|---|
| 25 |
@changes |
|---|
| 26 |
@changes |
|---|
| 27 |
@changes |
|---|
| 28 |
@changes |
|---|
| 29 |
@changes |
|---|
| 30 |
@changes |
|---|
| 31 |
@changes |
|---|
| 32 |
@changes |
|---|
| 33 |
@changes |
|---|
| 34 |
@changes |
|---|
| 35 |
@changes |
|---|
| 36 |
@changes |
|---|
| 37 |
|
|---|
| 38 |
class fORMFile |
|---|
| 39 |
{ |
|---|
| 40 |
|
|---|
| 41 |
const addFImageMethodCall = 'fORMFile::addFImageMethodCall'; |
|---|
| 42 |
const addFUploadMethodCall = 'fORMFile::addFUploadMethodCall'; |
|---|
| 43 |
const begin = 'fORMFile::begin'; |
|---|
| 44 |
const commit = 'fORMFile::commit'; |
|---|
| 45 |
const configureColumnInheritance = 'fORMFile::configureColumnInheritance'; |
|---|
| 46 |
const configureFileUploadColumn = 'fORMFile::configureFileUploadColumn'; |
|---|
| 47 |
const configureImageUploadColumn = 'fORMFile::configureImageUploadColumn'; |
|---|
| 48 |
const delete = 'fORMFile::delete'; |
|---|
| 49 |
const deleteOld = 'fORMFile::deleteOld'; |
|---|
| 50 |
const encode = 'fORMFile::encode'; |
|---|
| 51 |
const inspect = 'fORMFile::inspect'; |
|---|
| 52 |
const moveFromTemp = 'fORMFile::moveFromTemp'; |
|---|
| 53 |
const objectify = 'fORMFile::objectify'; |
|---|
| 54 |
const populate = 'fORMFile::populate'; |
|---|
| 55 |
const prepare = 'fORMFile::prepare'; |
|---|
| 56 |
const process = 'fORMFile::process'; |
|---|
| 57 |
const processImage = 'fORMFile::processImage'; |
|---|
| 58 |
const reflect = 'fORMFile::reflect'; |
|---|
| 59 |
const replicate = 'fORMFile::replicate'; |
|---|
| 60 |
const reset = 'fORMFile::reset'; |
|---|
| 61 |
const rollback = 'fORMFile::rollback'; |
|---|
| 62 |
const set = 'fORMFile::set'; |
|---|
| 63 |
const upload = 'fORMFile::upload'; |
|---|
| 64 |
const validate = 'fORMFile::validate'; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
@internal |
|---|
| 71 |
|
|---|
| 72 |
@var |
|---|
| 73 |
|
|---|
| 74 |
const TEMP_DIRECTORY = '__flourish_temp'; |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
@var |
|---|
| 81 |
|
|---|
| 82 |
static private $column_inheritence = array(); |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
@var |
|---|
| 88 |
|
|---|
| 89 |
static private $fupload_method_calls = array(); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
@var |
|---|
| 95 |
|
|---|
| 96 |
static private $file_upload_columns = array(); |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
@var |
|---|
| 102 |
|
|---|
| 103 |
static private $fimage_method_calls = array(); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
@var |
|---|
| 109 |
|
|---|
| 110 |
static private $image_upload_columns = array(); |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
@var |
|---|
| 116 |
|
|---|
| 117 |
static private $transaction_level = 0; |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
@param |
|---|
| 124 |
@param |
|---|
| 125 |
@param |
|---|
| 126 |
@param |
|---|
| 127 |
@return |
|---|
| 128 |
|
|---|
| 129 |
static public function addFImageMethodCall($class, $column, $method, $parameters=array()) |
|---|
| 130 |
{ |
|---|
| 131 |
$class = fORM::getClass($class); |
|---|
| 132 |
|
|---|
| 133 |
if (empty(self::$file_upload_columns[$class][$column])) { |
|---|
| 134 |
throw new fProgrammerException( |
|---|
| 135 |
'The column specified, %s, has not been configured as a file or image upload column', |
|---|
| 136 |
$column |
|---|
| 137 |
); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
if (empty(self::$fimage_method_calls[$class])) { |
|---|
| 141 |
self::$fimage_method_calls[$class] = array(); |
|---|
| 142 |
} |
|---|
| 143 |
if (empty(self::$fimage_method_calls[$class][$column])) { |
|---|
| 144 |
self::$fimage_method_calls[$class][$column] = array(); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
self::$fimage_method_calls[$class][$column][] = array( |
|---|
| 148 |
'method' => $method, |
|---|
| 149 |
'parameters' => $parameters |
|---|
| 150 |
); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
@param |
|---|
| 158 |
@param |
|---|
| 159 |
@param |
|---|
| 160 |
@param |
|---|
| 161 |
@return |
|---|
| 162 |
|
|---|
| 163 |
static public function addFUploadMethodCall($class, $column, $method, $parameters=array()) |
|---|
| 164 |
{ |
|---|
| 165 |
if ($method == 'enableOverwrite') { |
|---|
| 166 |
throw new fProgrammerException( |
|---|
| 167 |
'The method specified, %1$s, is not compatible with how %2$s stores and associates files with records', |
|---|
| 168 |
$method, |
|---|
| 169 |
'fORMFile' |
|---|
| 170 |
); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
$class = fORM::getClass($class); |
|---|
| 174 |
|
|---|
| 175 |
if (empty(self::$file_upload_columns[$class][$column])) { |
|---|
| 176 |
throw new fProgrammerException( |
|---|
| 177 |
'The column specified, %s, has not been configured as a file or image upload column', |
|---|
| 178 |
$column |
|---|
| 179 |
); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
if (empty(self::$fupload_method_calls[$class])) { |
|---|
| 183 |
self::$fupload_method_calls[$class] = array(); |
|---|
| 184 |
} |
|---|
| 185 |
if (empty(self::$fupload_method_calls[$class][$column])) { |
|---|
| 186 |
self::$fupload_method_calls[$class][$column] = array(); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
self::$fupload_method_calls[$class][$column][] = array( |
|---|
| 190 |
'method' => $method, |
|---|
| 191 |
'parameters' => $parameters |
|---|
| 192 |
); |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
@internal |
|---|
| 200 |
|
|---|
| 201 |
@return |
|---|
| 202 |
|
|---|
| 203 |
static public function begin() |
|---|
| 204 |
{ |
|---|
| 205 |
|
|---|
| 206 |
if (self::$transaction_level == 0 && fFilesystem::isInsideTransaction()) { |
|---|
| 207 |
return; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
self::$transaction_level++; |
|---|
| 211 |
|
|---|
| 212 |
if (!fFilesystem::isInsideTransaction()) { |
|---|
| 213 |
fFilesystem::begin(); |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
@internal |
|---|
| 222 |
|
|---|
| 223 |
@return |
|---|
| 224 |
|
|---|
| 225 |
static public function commit() |
|---|
| 226 |
{ |
|---|
| 227 |
|
|---|
| 228 |
if (self::$transaction_level == 0) { |
|---|
| 229 |
return; |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
self::$transaction_level--; |
|---|
| 233 |
|
|---|
| 234 |
if (!self::$transaction_level) { |
|---|
| 235 |
fFilesystem::commit(); |
|---|
| 236 |
} |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
@param |
|---|
| 244 |
@param |
|---|
| 245 |
@param |
|---|
| 246 |
@return |
|---|
| 247 |
|
|---|
| 248 |
static private function compose($message) |
|---|
| 249 |
{ |
|---|
| 250 |
$args = array_slice(func_get_args(), 1); |
|---|
| 251 |
|
|---|
| 252 |
if (class_exists('fText', FALSE)) { |
|---|
| 253 |
return call_user_func_array( |
|---|
| 254 |
array('fText', 'compose'), |
|---|
| 255 |
array($message, $args) |
|---|
| 256 |
); |
|---|
| 257 |
} else { |
|---|
| 258 |
return vsprintf($message, $args); |
|---|
| 259 |
} |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
@param |
|---|
| 275 |
@param |
|---|
| 276 |
@param |
|---|
| 277 |
@return |
|---|
| 278 |
|
|---|
| 279 |
static public function configureFileUploadColumn($class, $column, $directory) |
|---|
| 280 |
{ |
|---|
| 281 |
$class = fORM::getClass($class); |
|---|
| 282 |
$table = fORM::tablize($class); |
|---|
| 283 |
$schema = fORMSchema::retrieve($class); |
|---|
| 284 |
$data_type = $schema->getColumnInfo($table, $column, 'type'); |
|---|
| 285 |
|
|---|
| 286 |
$valid_data_types = array('varchar', 'char', 'text'); |
|---|
| 287 |
if (!in_array($data_type, $valid_data_types)) { |
|---|
| 288 |
throw new fProgrammerException( |
|---|
| 289 |
'The column specified, %1$s, is a %2$s column. Must be one of %3$s to be set as a file upload column.', |
|---|
| 290 |
$column, |
|---|
| 291 |
$data_type, |
|---|
| 292 |
join(', ', $valid_data_types) |
|---|
| 293 |
); |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
if (!is_object($directory)) { |
|---|
| 297 |
$directory = new fDirectory($directory); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
if (!$directory->isWritable()) { |
|---|
| 301 |
throw new fEnvironmentException( |
|---|
| 302 |
'The file upload directory, %s, is not writable', |
|---|
| 303 |
$directory->getPath() |
|---|
| 304 |
); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
$camelized_column = fGrammar::camelize($column, TRUE); |
|---|
| 308 |
|
|---|
| 309 |
fORM::registerActiveRecordMethod( |
|---|
| 310 |
$class, |
|---|
| 311 |
'upload' . $camelized_column, |
|---|
| 312 |
self::upload |
|---|
| 313 |
); |
|---|
| 314 |
|
|---|
| 315 |
fORM::registerActiveRecordMethod( |
|---|
| 316 |
$class, |
|---|
| 317 |
'set' . $camelized_column, |
|---|
| 318 |
self::set |
|---|
| 319 |
); |
|---|
| 320 |
|
|---|
| 321 |
fORM::registerActiveRecordMethod( |
|---|
| 322 |
$class, |
|---|
| 323 |
'encode' . $camelized_column, |
|---|
| 324 |
self::encode |
|---|
| 325 |
); |
|---|
| 326 |
|
|---|
| 327 |
fORM::registerActiveRecordMethod( |
|---|
| 328 |
$class, |
|---|
| 329 |
'prepare' . $camelized_column, |
|---|
| 330 |
self::prepare |
|---|
| 331 |
); |
|---|
| 332 |
|
|---|
| 333 |
fORM::registerReflectCallback($class, self::reflect); |
|---|
| 334 |
fORM::registerInspectCallback($class, $column, self::inspect); |
|---|
| 335 |
fORM::registerReplicateCallback($class, $column, self::replicate); |
|---|
| 336 |
fORM::registerObjectifyCallback($class, $column, self::objectify); |
|---|
| 337 |
|
|---|
| 338 |
$only_once_hooks = array( |
|---|
| 339 |
'post-begin::delete()' => self::begin, |
|---|
| 340 |
'pre-commit::delete()' => self::delete, |
|---|
| 341 |
'post-commit::delete()' => self::commit, |
|---|
| 342 |
'post-rollback::delete()' => self::rollback, |
|---|
| 343 |
'post::populate()' => self::populate, |
|---|
| 344 |
'post-begin::store()' => self::begin, |
|---|
| 345 |
'post-validate::store()' => self::moveFromTemp, |
|---|
| 346 |
'pre-commit::store()' => self::deleteOld, |
|---|
| 347 |
'post-commit::store()' => self::commit, |
|---|
| 348 |
'post-rollback::store()' => self::rollback, |
|---|
| 349 |
'post::validate()' => self::validate |
|---|
| 350 |
); |
|---|
| 351 |
|
|---|
| 352 |
foreach ($only_once_hooks as $hook => $callback) { |
|---|
| 353 |
if (!fORM::checkHookCallback($class, $hook, $callback)) { |
|---|
| 354 |
fORM::registerHookCallback($class, $hook, $callback); |
|---|
| 355 |
} |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
if (empty(self::$file_upload_columns[$class])) { |
|---|
| 359 |
self::$file_upload_columns[$class] = array(); |
|---|
| 360 |
} |
|---|
| 361 |
|
|---|
| 362 |
self::$file_upload_columns[$class][$column] = $directory; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
@param |
|---|
| 370 |
@param |
|---|
| 371 |
@param |
|---|
| 372 |
@return |
|---|
| 373 |
|
|---|
| 374 |
static public function configureColumnInheritance($class, $column, $inherit_from_column) |
|---|
| 375 |
{ |
|---|
| 376 |
$class = fORM::getClass($class); |
|---|
| 377 |
|
|---|
| 378 |
if (empty(self::$column_inheritence[$class])) { |
|---|
| 379 |
self::$column_inheritence[$class] = array(); |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
if (empty(self::$column_inheritence[$class][$inherit_from_column])) { |
|---|
| 383 |
self::$column_inheritence[$class][$inherit_from_column] = array(); |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
self::$column_inheritence[$class][$inherit_from_column][] = $column; |
|---|
| 387 |
} |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
@param |
|---|
| 397 |
@param |
|---|
| 398 |
@param |
|---|
| 399 |
@param |
|---|
| 400 |
@return |
|---|
| 401 |
|
|---|
| 402 |
static public function configureImageUploadColumn($class, $column, $directory, $image_type=NULL) |
|---|
| 403 |
{ |
|---|
| 404 |
$valid_image_types = array(NULL, 'gif', 'jpg', 'png'); |
|---|
| 405 |
if (!in_array($image_type, $valid_image_types)) { |
|---|
| 406 |
$valid_image_types[0] = '{null}'; |
|---|
| 407 |
throw new fProgrammerException( |
|---|
| 408 |
'The image type specified, %1$s, is not valid. Must be one of: %2$s.', |
|---|
| 409 |
$image_type, |
|---|
| 410 |
join(', ', $valid_image_types) |
|---|
| 411 |
); |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
self::configureFileUploadColumn($class, $column, $directory); |
|---|
| 415 |
|
|---|
| 416 |
$class = fORM::getClass($class); |
|---|
| 417 |
|
|---|
| 418 |
$camelized_column = fGrammar::camelize($column, TRUE); |
|---|
| 419 |
|
|---|
| 420 |
fORM::registerActiveRecordMethod( |
|---|
| 421 |
$class, |
|---|
| 422 |
'process' . $camelized_column, |
|---|
| 423 |
self::process |
|---|
| 424 |
); |
|---|
| 425 |
|
|---|
| 426 |
if (empty(self::$image_upload_columns[$class])) { |
|---|
| 427 |
self::$image_upload_columns[$class] = array(); |
|---|
| 428 |
} |
|---|
| 429 |
|
|---|
| 430 |
self::$image_upload_columns[$class][$column] = $image_type; |
|---|
| 431 |
|
|---|
| 432 |
self::addFUploadMethodCall( |
|---|
| 433 |
$class, |
|---|
| 434 |
$column, |
|---|
| 435 |
'setMimeTypes', |
|---|
| 436 |
array( |
|---|
| 437 |
array( |
|---|
| 438 |
'image/gif', |
|---|
| 439 |
'image/jpeg', |
|---|
| 440 |
'image/pjpeg', |
|---|
| 441 |
'image/png' |
|---|
| 442 |
), |
|---|
| 443 |
self::compose('The file uploaded is not an image') |
|---|
| 444 |
) |
|---|
| 445 |
); |
|---|
| 446 |
} |
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
@internal |
|---|
| 453 |
|
|---|
| 454 |
@param |
|---|
| 455 |
@param |
|---|
| 456 |
@param |
|---|
| 457 |
@param |
|---|
| 458 |
@param |
|---|
| 459 |
@return |
|---|
| 460 |
|
|---|
| 461 |
static public function delete($object, &$values, &$old_values, &$related_records, &$cache) |
|---|
| 462 |
{ |
|---|
| 463 |
$class = get_class($object); |
|---|
| 464 |
|
|---|
| 465 |
foreach (self::$file_upload_columns[$class] as $column => $directory) { |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
if ($values[$column] instanceof fFile) { |
|---|
| 469 |
$values[$column]->delete(); |
|---|
| 470 |
} |
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
foreach (fActiveRecord::retrieveOld($old_values, $column, array(), TRUE) as $file) { |
|---|
| 474 |
if ($file instanceof fFile) { |
|---|
| 475 |
$file->delete(); |
|---|
| 476 |
} |
|---|
| 477 |
} |
|---|
| 478 |
} |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
@internal |
|---|
| 486 |
|
|---|
| 487 |
@param |
|---|
| 488 |
@param |
|---|
| 489 |
@param |
|---|
| 490 |
@param |
|---|
| 491 |
@param |
|---|
| 492 |
@return |
|---|
| 493 |
|
|---|
| 494 |
static public function deleteOld($object, &$values, &$old_values, &$related_records, &$cache) |
|---|
| 495 |
{ |
|---|
| 496 |
$class = get_class($object); |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
foreach (self::$file_upload_columns[$class] as $column => $directory) { |
|---|
| 500 |
$current_file = $values[$column]; |
|---|
| 501 |
foreach (fActiveRecord::retrieveOld($old_values, $column, array(), TRUE) as $file) { |
|---|
| 502 |
if ($file instanceof fFile && (!$current_file instanceof fFile || $current_file->getPath() != $file->getPath())) { |
|---|
| 503 |
$file->delete(); |
|---|
| 504 |
} |
|---|
| 505 |
} |
|---|
| 506 |
} |
|---|
| 507 |
} |
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
@internal |
|---|
| 514 |
|
|---|
| 515 |
@param |
|---|
| 516 |
@param |
|---|
| 517 |
@param |
|---|
| 518 |
@param |
|---|
| 519 |
@param |
|---|
| 520 |
@param |
|---|
| 521 |
@param |
|---|
| 522 |
@return |
|---|
| 523 |
|
|---|
| 524 |
static public function encode($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 525 |
{ |
|---|
| 526 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 527 |
|
|---|
| 528 |
$filename = ($values[$column] instanceof fFile) ? $values[$column]->getName() : NULL; |
|---|
| 529 |
if ($filename && strpos($values[$column]->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . $filename) !== FALSE) { |
|---|
| 530 |
$filename = self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR . $filename; |
|---|
| 531 |
} |
|---|
| 532 |
|
|---|
| 533 |
return fHTML::encode($filename); |
|---|
| 534 |
} |
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
@internal |
|---|
| 541 |
|
|---|
| 542 |
@param |
|---|
| 543 |
@param |
|---|
| 544 |
@param |
|---|
| 545 |
@return |
|---|
| 546 |
|
|---|
| 547 |
static public function inspect($class, $column, &$metadata) |
|---|
| 548 |
{ |
|---|
| 549 |
if (!empty(self::$image_upload_columns[$class][$column])) { |
|---|
| 550 |
$metadata['feature'] = 'image'; |
|---|
| 551 |
|
|---|
| 552 |
} elseif (!empty(self::$file_upload_columns[$class][$column])) { |
|---|
| 553 |
$metadata['feature'] = 'file'; |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
$metadata['directory'] = self::$file_upload_columns[$class][$column]->getPath(); |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
@internal |
|---|
| 564 |
|
|---|
| 565 |
@param |
|---|
| 566 |
@param |
|---|
| 567 |
@param |
|---|
| 568 |
@param |
|---|
| 569 |
@param |
|---|
| 570 |
@return |
|---|
| 571 |
|
|---|
| 572 |
static public function moveFromTemp($object, &$values, &$old_values, &$related_records, &$cache) |
|---|
| 573 |
{ |
|---|
| 574 |
foreach ($values as $column => $value) { |
|---|
| 575 |
if (!$value instanceof fFile) { |
|---|
| 576 |
continue; |
|---|
| 577 |
} |
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
if (strpos($value->getParent()->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR) !== FALSE) { |
|---|
| 581 |
$new_filename = str_replace(self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR, '', $value->getPath()); |
|---|
| 582 |
$new_filename = fFilesystem::makeUniqueName($new_filename); |
|---|
| 583 |
$value->rename($new_filename, FALSE); |
|---|
| 584 |
} |
|---|
| 585 |
} |
|---|
| 586 |
} |
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
@internal |
|---|
| 593 |
|
|---|
| 594 |
@param |
|---|
| 595 |
@param |
|---|
| 596 |
@param |
|---|
| 597 |
@return |
|---|
| 598 |
|
|---|
| 599 |
static public function objectify($class, $column, $value) |
|---|
| 600 |
{ |
|---|
| 601 |
if ((!is_string($value) && !is_numeric($value) && !is_object($value)) || !strlen(trim($value))) { |
|---|
| 602 |
return $value; |
|---|
| 603 |
} |
|---|
| 604 |
|
|---|
| 605 |
$path = self::$file_upload_columns[$class][$column]->getPath() . $value; |
|---|
| 606 |
|
|---|
| 607 |
try { |
|---|
| 608 |
|
|---|
| 609 |
return fFilesystem::createObject($path); |
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
} catch (fExpectedException $e) { |
|---|
| 613 |
return $value; |
|---|
| 614 |
} |
|---|
| 615 |
} |
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
@internal |
|---|
| 622 |
|
|---|
| 623 |
@param |
|---|
| 624 |
@param |
|---|
| 625 |
@param |
|---|
| 626 |
@param |
|---|
| 627 |
@param |
|---|
| 628 |
@return |
|---|
| 629 |
|
|---|
| 630 |
static public function populate($object, &$values, &$old_values, &$related_records, &$cache) |
|---|
| 631 |
{ |
|---|
| 632 |
$class = get_class($object); |
|---|
| 633 |
|
|---|
| 634 |
foreach (self::$file_upload_columns[$class] as $column => $directory) { |
|---|
| 635 |
if (fUpload::check($column) || fRequest::check('existing-' . $column) || fRequest::check('delete-' . $column)) { |
|---|
| 636 |
$method = 'upload' . fGrammar::camelize($column, TRUE); |
|---|
| 637 |
$object->$method(); |
|---|
| 638 |
} |
|---|
| 639 |
} |
|---|
| 640 |
} |
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
@internal |
|---|
| 647 |
|
|---|
| 648 |
@param |
|---|
| 649 |
@param |
|---|
| 650 |
@param |
|---|
| 651 |
@param |
|---|
| 652 |
@param |
|---|
| 653 |
@param |
|---|
| 654 |
@param |
|---|
| 655 |
@return |
|---|
| 656 |
|
|---|
| 657 |
static public function prepare($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 658 |
{ |
|---|
| 659 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 660 |
|
|---|
| 661 |
if (sizeof($parameters) > 1) { |
|---|
| 662 |
throw new fProgrammerException( |
|---|
| 663 |
'The column specified, %s, does not accept more than one parameter', |
|---|
| 664 |
$column |
|---|
| 665 |
); |
|---|
| 666 |
} |
|---|
| 667 |
|
|---|
| 668 |
$translate_to_web_path = (empty($parameters[0])) ? FALSE : TRUE; |
|---|
| 669 |
$value = $values[$column]; |
|---|
| 670 |
|
|---|
| 671 |
if ($value instanceof fFile) { |
|---|
| 672 |
$path = ($translate_to_web_path) ? $value->getPath(TRUE) : $value->getName(); |
|---|
| 673 |
} else { |
|---|
| 674 |
$path = NULL; |
|---|
| 675 |
} |
|---|
| 676 |
|
|---|
| 677 |
return fHTML::prepare($path); |
|---|
| 678 |
} |
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
@param |
|---|
| 685 |
@return |
|---|
| 686 |
|
|---|
| 687 |
static private function prepareTempDir($folder) |
|---|
| 688 |
{ |
|---|
| 689 |
|
|---|
| 690 |
try { |
|---|
| 691 |
$temp_dir = new fDirectory($folder->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 692 |
} catch (fValidationException $e) { |
|---|
| 693 |
$temp_dir = fDirectory::create($folder->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 694 |
} |
|---|
| 695 |
|
|---|
| 696 |
$temp_files = $temp_dir->scan(); |
|---|
| 697 |
foreach ($temp_files as $temp_file) { |
|---|
| 698 |
if (filemtime($temp_file->getPath()) < strtotime('-6 hours')) { |
|---|
| 699 |
unlink($temp_file->getPath()); |
|---|
| 700 |
} |
|---|
| 701 |
} |
|---|
| 702 |
|
|---|
| 703 |
return $temp_dir; |
|---|
| 704 |
} |
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
@internal |
|---|
| 711 |
|
|---|
| 712 |
@param |
|---|
| 713 |
@param |
|---|
| 714 |
@param |
|---|
| 715 |
@param |
|---|
| 716 |
@param |
|---|
| 717 |
@param |
|---|
| 718 |
@param |
|---|
| 719 |
@return |
|---|
| 720 |
|
|---|
| 721 |
static public function process($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 722 |
{ |
|---|
| 723 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 724 |
|
|---|
| 725 |
$class = get_class($object); |
|---|
| 726 |
|
|---|
| 727 |
self::processImage($class, $column, $values[$column]); |
|---|
| 728 |
|
|---|
| 729 |
return $object; |
|---|
| 730 |
} |
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
@internal |
|---|
| 737 |
|
|---|
| 738 |
@param |
|---|
| 739 |
@param |
|---|
| 740 |
@param |
|---|
| 741 |
@return |
|---|
| 742 |
|
|---|
| 743 |
static public function processImage($class, $column, $image) |
|---|
| 744 |
{ |
|---|
| 745 |
|
|---|
| 746 |
if (!$image instanceof fImage || empty(self::$fimage_method_calls[$class][$column])) { |
|---|
| 747 |
return; |
|---|
| 748 |
} |
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
if (!empty(self::$fimage_method_calls[$class][$column])) { |
|---|
| 752 |
foreach (self::$fimage_method_calls[$class][$column] as $method_call) { |
|---|
| 753 |
$callback = array($image, $method_call['method']); |
|---|
| 754 |
$parameters = $method_call['parameters']; |
|---|
| 755 |
if (!is_callable($callback)) { |
|---|
| 756 |
throw new fProgrammerException( |
|---|
| 757 |
'The fImage method specified, %s, is not a valid method', |
|---|
| 758 |
$method_call['method'] . '()' |
|---|
| 759 |
); |
|---|
| 760 |
} |
|---|
| 761 |
call_user_func_array($callback, $parameters); |
|---|
| 762 |
} |
|---|
| 763 |
} |
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
call_user_func( |
|---|
| 767 |
array($image, 'saveChanges'), |
|---|
| 768 |
isset(self::$image_upload_columns[$class][$column]) ? self::$image_upload_columns[$class][$column] : NULL |
|---|
| 769 |
); |
|---|
| 770 |
} |
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
@internal |
|---|
| 777 |
|
|---|
| 778 |
@param |
|---|
| 779 |
@param |
|---|
| 780 |
@param |
|---|
| 781 |
@return |
|---|
| 782 |
|
|---|
| 783 |
static public function reflect($class, &$signatures, $include_doc_comments) |
|---|
| 784 |
{ |
|---|
| 785 |
$image_columns = (isset(self::$image_upload_columns[$class])) ? array_keys(self::$image_upload_columns[$class]) : array(); |
|---|
| 786 |
$file_columns = (isset(self::$file_upload_columns[$class])) ? array_keys(self::$file_upload_columns[$class]) : array(); |
|---|
| 787 |
|
|---|
| 788 |
foreach($file_columns as $column) { |
|---|
| 789 |
$camelized_column = fGrammar::camelize($column, TRUE); |
|---|
| 790 |
|
|---|
| 791 |
$noun = 'file'; |
|---|
| 792 |
if (in_array($column, $image_columns)) { |
|---|
| 793 |
$noun = 'image'; |
|---|
| 794 |
} |
|---|
| 795 |
|
|---|
| 796 |
$signature = ''; |
|---|
| 797 |
if ($include_doc_comments) { |
|---|
| 798 |
$signature .= "/**\n"; |
|---|
| 799 |
$signature .= " * Encodes the filename of " . $column . " for output into an HTML form\n"; |
|---|
| 800 |
$signature .= " * \n"; |
|---|
| 801 |
$signature .= " * Only the filename will be returned, any directory will be stripped.\n"; |
|---|
| 802 |
$signature .= " * \n"; |
|---|
| 803 |
$signature .= " * @return string The HTML form-ready value\n"; |
|---|
| 804 |
$signature .= " */\n"; |
|---|
| 805 |
} |
|---|
| 806 |
$encode_method = 'encode' . $camelized_column; |
|---|
| 807 |
$signature .= 'public function ' . $encode_method . '()'; |
|---|
| 808 |
|
|---|
| 809 |
$signatures[$encode_method] = $signature; |
|---|
| 810 |
|
|---|
| 811 |
if (in_array($column, $image_columns)) { |
|---|
| 812 |
$signature = ''; |
|---|
| 813 |
if ($include_doc_comments) { |
|---|
| 814 |
$signature .= "/**\n"; |
|---|
| 815 |
$signature .= " * Takes the existing image and runs it through the prescribed fImage method calls\n"; |
|---|
| 816 |
$signature .= " * \n"; |
|---|
| 817 |
$signature .= " * @return fActiveRecord The record object, to allow for method chaining\n"; |
|---|
| 818 |
$signature .= " */\n"; |
|---|
| 819 |
} |
|---|
| 820 |
$process_method = 'process' . $camelized_column; |
|---|
| 821 |
$signature .= 'public function ' . $process_method . '()'; |
|---|
| 822 |
|
|---|
| 823 |
$signatures[$process_method] = $signature; |
|---|
| 824 |
} |
|---|
| 825 |
|
|---|
| 826 |
$signature = ''; |
|---|
| 827 |
if ($include_doc_comments) { |
|---|
| 828 |
$signature .= "/**\n"; |
|---|
| 829 |
$signature .= " * Prepares the filename of " . $column . " for output into HTML\n"; |
|---|
| 830 |
$signature .= " * \n"; |
|---|
| 831 |
$signature .= " * By default only the filename will be returned and any directory will be stripped.\n"; |
|---|
| 832 |
$signature .= " * The \$include_web_path parameter changes this behaviour.\n"; |
|---|
| 833 |
$signature .= " * \n"; |
|---|
| 834 |
$signature .= " * @param boolean \$include_web_path If the full web path to the " . $noun . " should be included\n"; |
|---|
| 835 |
$signature .= " * @return string The HTML-ready value\n"; |
|---|
| 836 |
$signature .= " */\n"; |
|---|
| 837 |
} |
|---|
| 838 |
$prepare_method = 'prepare' . $camelized_column; |
|---|
| 839 |
$signature .= 'public function ' . $prepare_method . '($include_web_path=FALSE)'; |
|---|
| 840 |
|
|---|
| 841 |
$signatures[$prepare_method] = $signature; |
|---|
| 842 |
|
|---|
| 843 |
$signature = ''; |
|---|
| 844 |
if ($include_doc_comments) { |
|---|
| 845 |
$signature .= "/**\n"; |
|---|
| 846 |
$signature .= " * Takes a file uploaded through an HTML form for " . $column . " and moves it into the specified directory\n"; |
|---|
| 847 |
$signature .= " * \n"; |
|---|
| 848 |
$signature .= " * Any columns that were designated as inheriting from this column will get a copy\n"; |
|---|
| 849 |
$signature .= " * of the uploaded file.\n"; |
|---|
| 850 |
$signature .= " * \n"; |
|---|
| 851 |
if ($noun == 'image') { |
|---|
| 852 |
$signature .= " * Any fImage calls that were added to the column will be processed on the uploaded image.\n"; |
|---|
| 853 |
$signature .= " * \n"; |
|---|
| 854 |
} |
|---|
| 855 |
$signature .= " * @return fFile The uploaded file\n"; |
|---|
| 856 |
$signature .= " */\n"; |
|---|
| 857 |
} |
|---|
| 858 |
$upload_method = 'upload' . $camelized_column; |
|---|
| 859 |
$signature .= 'public function ' . $upload_method . '()'; |
|---|
| 860 |
|
|---|
| 861 |
$signatures[$upload_method] = $signature; |
|---|
| 862 |
|
|---|
| 863 |
$signature = ''; |
|---|
| 864 |
if ($include_doc_comments) { |
|---|
| 865 |
$signature .= "/**\n"; |
|---|
| 866 |
$signature .= " * Takes a file that exists on the filesystem and copies it into the specified directory for " . $column . "\n"; |
|---|
| 867 |
$signature .= " * \n"; |
|---|
| 868 |
if ($noun == 'image') { |
|---|
| 869 |
$signature .= " * Any fImage calls that were added to the column will be processed on the copied image.\n"; |
|---|
| 870 |
$signature .= " * \n"; |
|---|
| 871 |
} |
|---|
| 872 |
$signature .= " * @return fActiveRecord The record object, to allow for method chaining\n"; |
|---|
| 873 |
$signature .= " */\n"; |
|---|
| 874 |
} |
|---|
| 875 |
$set_method = 'set' . $camelized_column; |
|---|
| 876 |
$signature .= 'public function ' . $set_method . '()'; |
|---|
| 877 |
|
|---|
| 878 |
$signatures[$set_method] = $signature; |
|---|
| 879 |
|
|---|
| 880 |
$signature = ''; |
|---|
| 881 |
if ($include_doc_comments) { |
|---|
| 882 |
$signature .= "/**\n"; |
|---|
| 883 |
$signature .= " * Returns metadata about " . $column . "\n"; |
|---|
| 884 |
$signature .= " * \n"; |
|---|
| 885 |
$signature .= " * @param string \$element The element to return. Must be one of: 'type', 'not_null', 'default', 'valid_values', 'max_length', 'feature', 'directory'.\n"; |
|---|
| 886 |
$signature .= " * @return mixed The metadata array or a single element\n"; |
|---|
| 887 |
$signature .= " */\n"; |
|---|
| 888 |
} |
|---|
| 889 |
$inspect_method = 'inspect' . $camelized_column; |
|---|
| 890 |
$signature .= 'public function ' . $inspect_method . '($element=NULL)'; |
|---|
| 891 |
|
|---|
| 892 |
$signatures[$inspect_method] = $signature; |
|---|
| 893 |
} |
|---|
| 894 |
} |
|---|
| 895 |
|
|---|
| 896 |
|
|---|
| 897 |
|
|---|
| 898 |
|
|---|
| 899 |
|
|---|
| 900 |
@internal |
|---|
| 901 |
|
|---|
| 902 |
@param |
|---|
| 903 |
@param |
|---|
| 904 |
@param |
|---|
| 905 |
@return |
|---|
| 906 |
|
|---|
| 907 |
static public function replicate($class, $column, $value) |
|---|
| 908 |
{ |
|---|
| 909 |
if (!$value instanceof fFile) { |
|---|
| 910 |
return $value; |
|---|
| 911 |
} |
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
if (strpos($value->getParent()->getPath(), self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR) !== FALSE) { |
|---|
| 915 |
$value = clone $value; |
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
} else { |
|---|
| 919 |
$upload_dir = self::$file_upload_columns[$class][$column]; |
|---|
| 920 |
|
|---|
| 921 |
try { |
|---|
| 922 |
$temp_dir = new fDirectory($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 923 |
} catch (fValidationException $e) { |
|---|
| 924 |
$temp_dir = fDirectory::create($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 925 |
} |
|---|
| 926 |
|
|---|
| 927 |
$value = $value->duplicate($temp_dir); |
|---|
| 928 |
} |
|---|
| 929 |
|
|---|
| 930 |
return $value; |
|---|
| 931 |
} |
|---|
| 932 |
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
|
|---|
| 937 |
@internal |
|---|
| 938 |
|
|---|
| 939 |
@return |
|---|
| 940 |
|
|---|
| 941 |
static public function reset() |
|---|
| 942 |
{ |
|---|
| 943 |
self::$column_inheritence = array(); |
|---|
| 944 |
self::$fupload_method_calls = array(); |
|---|
| 945 |
self::$file_upload_columns = array(); |
|---|
| 946 |
self::$fimage_method_calls = array(); |
|---|
| 947 |
self::$image_upload_columns = array(); |
|---|
| 948 |
self::$transaction_level = 0; |
|---|
| 949 |
} |
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 |
|
|---|
| 954 |
|
|---|
| 955 |
@internal |
|---|
| 956 |
|
|---|
| 957 |
@return |
|---|
| 958 |
|
|---|
| 959 |
static public function rollback() |
|---|
| 960 |
{ |
|---|
| 961 |
|
|---|
| 962 |
if (self::$transaction_level == 0) { |
|---|
| 963 |
return; |
|---|
| 964 |
} |
|---|
| 965 |
|
|---|
| 966 |
self::$transaction_level--; |
|---|
| 967 |
|
|---|
| 968 |
if (!self::$transaction_level) { |
|---|
| 969 |
fFilesystem::rollback(); |
|---|
| 970 |
} |
|---|
| 971 |
} |
|---|
| 972 |
|
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
|
|---|
| 979 |
@internal |
|---|
| 980 |
|
|---|
| 981 |
@param |
|---|
| 982 |
@param |
|---|
| 983 |
@param |
|---|
| 984 |
@param |
|---|
| 985 |
@param |
|---|
| 986 |
@param |
|---|
| 987 |
@param |
|---|
| 988 |
@return |
|---|
| 989 |
|
|---|
| 990 |
static public function set($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 991 |
{ |
|---|
| 992 |
$class = get_class($object); |
|---|
| 993 |
|
|---|
| 994 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 995 |
|
|---|
| 996 |
$doc_root = realpath($_SERVER['DOCUMENT_ROOT']); |
|---|
| 997 |
|
|---|
| 998 |
if (!array_key_exists(0, $parameters)) { |
|---|
| 999 |
throw new fProgrammerException( |
|---|
| 1000 |
'The method %s requires exactly one parameter', |
|---|
| 1001 |
$method_name . '()' |
|---|
| 1002 |
); |
|---|
| 1003 |
} |
|---|
| 1004 |
|
|---|
| 1005 |
$file_path = $parameters[0]; |
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
if ($file_path instanceof fFile) { |
|---|
| 1009 |
$file_path = $file_path->getPath(); |
|---|
| 1010 |
} elseif (is_object($file_path) && is_callable(array($file_path, '__toString'))) { |
|---|
| 1011 |
$file_path = $file_path->__toString(); |
|---|
| 1012 |
} elseif (is_object($file_path)) { |
|---|
| 1013 |
$file_path = (string) $file_path; |
|---|
| 1014 |
} |
|---|
| 1015 |
|
|---|
| 1016 |
if ($file_path !== NULL && $file_path !== '' && $file_path !== FALSE) { |
|---|
| 1017 |
if (!$file_path || (!file_exists($file_path) && !file_exists($doc_root . $file_path))) { |
|---|
| 1018 |
throw new fEnvironmentException( |
|---|
| 1019 |
'The file specified, %s, does not exist. This may indicate a missing enctype="multipart/form-data" attribute in form tag.', |
|---|
| 1020 |
$file_path |
|---|
| 1021 |
); |
|---|
| 1022 |
} |
|---|
| 1023 |
|
|---|
| 1024 |
if (!file_exists($file_path) && file_exists($doc_root . $file_path)) { |
|---|
| 1025 |
$file_path = $doc_root . $file_path; |
|---|
| 1026 |
} |
|---|
| 1027 |
|
|---|
| 1028 |
if (is_dir($file_path)) { |
|---|
| 1029 |
throw new fProgrammerException( |
|---|
| 1030 |
'The file specified, %s, is not a file but a directory', |
|---|
| 1031 |
$file_path |
|---|
| 1032 |
); |
|---|
| 1033 |
} |
|---|
| 1034 |
|
|---|
| 1035 |
$upload_dir = self::$file_upload_columns[$class][$column]; |
|---|
| 1036 |
|
|---|
| 1037 |
try { |
|---|
| 1038 |
$temp_dir = new fDirectory($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 1039 |
} catch (fValidationException $e) { |
|---|
| 1040 |
$temp_dir = fDirectory::create($upload_dir->getPath() . self::TEMP_DIRECTORY . DIRECTORY_SEPARATOR); |
|---|
| 1041 |
} |
|---|
| 1042 |
|
|---|
| 1043 |
$file = fFilesystem::createObject($file_path); |
|---|
| 1044 |
$new_file = $file->duplicate($temp_dir); |
|---|
| 1045 |
|
|---|
| 1046 |
} else { |
|---|
| 1047 |
$new_file = NULL; |
|---|
| 1048 |
} |
|---|
| 1049 |
|
|---|
| 1050 |
fActiveRecord::assign($values, $old_values, $column, $new_file); |
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
if (!empty(self::$column_inheritence[$class][$column])) { |
|---|
| 1054 |
foreach (self::$column_inheritence[$class][$column] as $other_column) { |
|---|
| 1055 |
self::set($object, $values, $old_values, $related_records, $cache, 'set' . fGrammar::camelize($other_column, TRUE), array($file)); |
|---|
| 1056 |
} |
|---|
| 1057 |
} |
|---|
| 1058 |
|
|---|
| 1059 |
if ($new_file) { |
|---|
| 1060 |
self::processImage($class, $column, $new_file); |
|---|
| 1061 |
} |
|---|
| 1062 |
|
|---|
| 1063 |
return $object; |
|---|
| 1064 |
} |
|---|
| 1065 |
|
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 |
|
|---|
| 1069 |
|
|---|
| 1070 |
@param |
|---|
| 1071 |
@param |
|---|
| 1072 |
@return |
|---|
| 1073 |
|
|---|
| 1074 |
static private function setUpFUpload($class, $column) |
|---|
| 1075 |
{ |
|---|
| 1076 |
$upload = new fUpload(); |
|---|
| 1077 |
|
|---|
| 1078 |
|
|---|
| 1079 |
if (!empty(self::$fupload_method_calls[$class][$column])) { |
|---|
| 1080 |
foreach (self::$fupload_method_calls[$class][$column] as $method_call) { |
|---|
| 1081 |
if (!is_callable($upload->{$method_call['method']})) { |
|---|
| 1082 |
throw new fProgrammerException( |
|---|
| 1083 |
'The fUpload method specified, %s, is not a valid method', |
|---|
| 1084 |
$method_call['method'] . '()' |
|---|
| 1085 |
); |
|---|
| 1086 |
} |
|---|
| 1087 |
call_user_func_array($upload->{$method_call['method']}, $method_call['parameters']); |
|---|
| 1088 |
} |
|---|
| 1089 |
} |
|---|
| 1090 |
|
|---|
| 1091 |
return $upload; |
|---|
| 1092 |
} |
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 |
|
|---|
| 1096 |
|
|---|
| 1097 |
|
|---|
| 1098 |
@internal |
|---|
| 1099 |
|
|---|
| 1100 |
@param |
|---|
| 1101 |
@param |
|---|
| 1102 |
@param |
|---|
| 1103 |
@param |
|---|
| 1104 |
@param |
|---|
| 1105 |
@param |
|---|
| 1106 |
@param |
|---|
| 1107 |
@return |
|---|
| 1108 |
|
|---|
| 1109 |
static public function upload($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 1110 |
{ |
|---|
| 1111 |
$class = get_class($object); |
|---|
| 1112 |
|
|---|
| 1113 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 1114 |
|
|---|
| 1115 |
$existing_temp_file = FALSE; |
|---|
| 1116 |
|
|---|
| 1117 |
|
|---|
| 1118 |
|
|---|
| 1119 |
try { |
|---|
| 1120 |
$upload_dir = self::$file_upload_columns[$class][$column]; |
|---|
| 1121 |
$temp_dir = self::prepareTempDir($upload_dir); |
|---|
| 1122 |
|
|---|
| 1123 |
if (!fUpload::check($column)) { |
|---|
| 1124 |
throw new fExpectedException('Please upload a file'); |
|---|
| 1125 |
} |
|---|
| 1126 |
|
|---|
| 1127 |
$uploader = self::setUpFUpload($class, $column); |
|---|
| 1128 |
$file = $uploader->move($temp_dir, $column); |
|---|
| 1129 |
|
|---|
| 1130 |
|
|---|
| 1131 |
} catch (fExpectedException $e) { |
|---|
| 1132 |
|
|---|
| 1133 |
|
|---|
| 1134 |
$existing_file = fRequest::get('existing-' . $column); |
|---|
| 1135 |
$delete_file = fRequest::get('delete-' . $column, 'boolean'); |
|---|
| 1136 |
$no_upload = $e->getMessage() == self::compose('Please upload a file'); |
|---|
| 1137 |
|
|---|
| 1138 |
if ($existing_file && $delete_file && $no_upload) { |
|---|
| 1139 |
$file = NULL; |
|---|
| 1140 |
|
|---|
| 1141 |
} elseif ($existing_file) { |
|---|
| 1142 |
|
|---|
| 1143 |
$file_path = $upload_dir->getPath() . $existing_file; |
|---|
| 1144 |
$file = fFilesystem::createObject($file_path); |
|---|
| 1145 |
|
|---|
| 1146 |
$current_file = $values[$column]; |
|---|
| 1147 |
|
|---|
| 1148 |
|
|---|
| 1149 |
if ($current_file && $file->getPath() == $current_file->getPath()) { |
|---|
| 1150 |
return; |
|---|
| 1151 |
} |
|---|
| 1152 |
|
|---|
| 1153 |
$existing_temp_file = TRUE; |
|---|
| 1154 |
|
|---|
| 1155 |
} else { |
|---|
| 1156 |
$file = NULL; |
|---|
| 1157 |
} |
|---|
| 1158 |
} |
|---|
| 1159 |
|
|---|
| 1160 |
|
|---|
| 1161 |
fActiveRecord::assign($values, $old_values, $column, $file); |
|---|
| 1162 |
|
|---|
| 1163 |
|
|---|
| 1164 |
if (!empty(self::$column_inheritence[$class][$column])) { |
|---|
| 1165 |
foreach (self::$column_inheritence[$class][$column] as $other_column) { |
|---|
| 1166 |
|
|---|
| 1167 |
if ($file) { |
|---|
| 1168 |
|
|---|
| 1169 |
|
|---|
| 1170 |
if (!$file instanceof fImage && isset(self::$image_upload_columns[$class][$other_column])) { |
|---|
| 1171 |
continue; |
|---|
| 1172 |
} |
|---|
| 1173 |
|
|---|
| 1174 |
$other_upload_dir = self::$file_upload_columns[$class][$other_column]; |
|---|
| 1175 |
$other_temp_dir = self::prepareTempDir($other_upload_dir); |
|---|
| 1176 |
|
|---|
| 1177 |
if ($existing_temp_file) { |
|---|
| 1178 |
$other_file = fFilesystem::createObject($other_temp_dir->getPath() . $file->getName()); |
|---|
| 1179 |
} else { |
|---|
| 1180 |
$other_file = $file->duplicate($other_temp_dir, FALSE); |
|---|
| 1181 |
} |
|---|
| 1182 |
|
|---|
| 1183 |
} else { |
|---|
| 1184 |
$other_file = $file; |
|---|
| 1185 |
} |
|---|
| 1186 |
|
|---|
| 1187 |
fActiveRecord::assign($values, $old_values, $other_column, $other_file); |
|---|
| 1188 |
|
|---|
| 1189 |
if (!$existing_temp_file && $other_file) { |
|---|
| 1190 |
self::processImage($class, $other_column, $other_file); |
|---|
| 1191 |
} |
|---|
| 1192 |
} |
|---|
| 1193 |
} |
|---|
| 1194 |
|
|---|
| 1195 |
|
|---|
| 1196 |
if (!$existing_temp_file && $file) { |
|---|
| 1197 |
self::processImage($class, $column, $file); |
|---|
| 1198 |
} |
|---|
| 1199 |
|
|---|
| 1200 |
return $file; |
|---|
| 1201 |
} |
|---|
| 1202 |
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
|
|---|
| 1207 |
@internal |
|---|
| 1208 |
|
|---|
| 1209 |
@param |
|---|
| 1210 |
@param |
|---|
| 1211 |
@param |
|---|
| 1212 |
@param |
|---|
| 1213 |
@param |
|---|
| 1214 |
@param |
|---|
| 1215 |
@return |
|---|
| 1216 |
|
|---|
| 1217 |
static public function validate($object, &$values, &$old_values, &$related_records, &$cache, &$validation_messages) |
|---|
| 1218 |
{ |
|---|
| 1219 |
$class = get_class($object); |
|---|
| 1220 |
|
|---|
| 1221 |
foreach (self::$file_upload_columns[$class] as $column => $directory) { |
|---|
| 1222 |
$column_name = fORM::getColumnName($class, $column); |
|---|
| 1223 |
|
|---|
| 1224 |
if (isset($validation_messages[$column])) { |
|---|
| 1225 |
$search_message = self::compose('%sPlease enter a value', fValidationException::formatField($column_name)); |
|---|
| 1226 |
$replace_message = self::compose('%sPlease upload a file', fValidationException::formatField($column_name)); |
|---|
| 1227 |
$validation_messages[$column] = str_replace($search_message, $replace_message, $validation_messages[$column]); |
|---|
| 1228 |
} |
|---|
| 1229 |
|
|---|
| 1230 |
|
|---|
| 1231 |
try { |
|---|
| 1232 |
if (fUpload::check($column)) { |
|---|
| 1233 |
$uploader = self::setUpFUpload($class, $column); |
|---|
| 1234 |
$uploader->validate($column); |
|---|
| 1235 |
} |
|---|
| 1236 |
} catch (fValidationException $e) { |
|---|
| 1237 |
if ($e->getMessage() != self::compose('Please upload a file')) { |
|---|
| 1238 |
$validation_messages[$column] = fValidationException::formatField($column_name) . $e->getMessage(); |
|---|
| 1239 |
} |
|---|
| 1240 |
} |
|---|
| 1241 |
} |
|---|
| 1242 |
} |
|---|
| 1243 |
|
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 |
|
|---|
| 1248 |
@return |
|---|
| 1249 |
|
|---|
| 1250 |
private function __construct() { } |
|---|
| 1251 |
} |
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
will@flourishlib.com |
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|
| 1259 |
|
|---|
| 1260 |
|
|---|
| 1261 |
|
|---|
| 1262 |
|
|---|
| 1263 |
|
|---|
| 1264 |
|
|---|
| 1265 |
|
|---|
| 1266 |
|
|---|
| 1267 |
|
|---|
| 1268 |
|
|---|
| 1269 |
|
|---|
| 1270 |
|
|---|
| 1271 |
|
|---|
| 1272 |
|
|---|
| 1273 |
|
|---|
| 1274 |
|
|---|
| 1275 |
|
|---|