| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
class pkWidgetFormInputFilePersistent extends sfWidgetForm |
|---|
| 15 |
{ |
|---|
| 16 |
|
|---|
| 17 |
* @param array $options An array of options |
|---|
| 18 |
* @param array $attributes An array of default HTML attributes |
|---|
| 19 |
* |
|---|
| 20 |
* @see sfWidgetFormInput |
|---|
| 21 |
* |
|---|
| 22 |
* In reality builds an array of two controls using the [] form field |
|---|
| 23 |
* name syntax |
|---|
| 24 |
*/ |
|---|
| 25 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 26 |
{ |
|---|
| 27 |
parent::configure($options, $attributes); |
|---|
| 28 |
|
|---|
| 29 |
$this->addOption('type', 'file'); |
|---|
| 30 |
$this->addOption('existing-html', false); |
|---|
| 31 |
$this->addOption('image-preview', null); |
|---|
| 32 |
$this->setOption('needs_multipart', true); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* @param string $name The element name |
|---|
| 37 |
* @param string $value The value displayed in this widget |
|---|
| 38 |
* (i.e. the browser-side filename submitted |
|---|
| 39 |
* on a previous partially successful |
|---|
| 40 |
* validation of this form) |
|---|
| 41 |
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes |
|---|
| 42 |
* @param array $errors An array of errors for the field |
|---|
| 43 |
* |
|---|
| 44 |
* @return string An HTML tag string |
|---|
| 45 |
* |
|---|
| 46 |
* @see sfWidgetForm |
|---|
| 47 |
*/ |
|---|
| 48 |
|
|---|
| 49 |
public function render($name, $value = null, $attributes = array(), $errors = array()) |
|---|
| 50 |
{ |
|---|
| 51 |
$exists = false; |
|---|
| 52 |
if (isset($value['persistid']) && strlen($value['persistid'])) |
|---|
| 53 |
{ |
|---|
| 54 |
$persistid = $value['persistid']; |
|---|
| 55 |
$info = pkValidatorFilePersistent::getFileInfo($persistid); |
|---|
| 56 |
if ($info) |
|---|
| 57 |
{ |
|---|
| 58 |
$exists = true; |
|---|
| 59 |
} |
|---|
| 60 |
} |
|---|
| 61 |
else |
|---|
| 62 |
{ |
|---|
| 63 |
|
|---|
| 64 |
$persistid = pkValidatorFilePersistent::createGuid(); |
|---|
| 65 |
} |
|---|
| 66 |
$result = ''; |
|---|
| 67 |
if ($exists) |
|---|
| 68 |
{ |
|---|
| 69 |
if ($this->hasOption('image-preview')) |
|---|
| 70 |
{ |
|---|
| 71 |
|
|---|
| 72 |
$subdir = sfConfig::get('sf_persistent_upload_preview_dir', "/uploaded-image-preview"); |
|---|
| 73 |
$parentdir = sfConfig::get('sf_web_dir'); |
|---|
| 74 |
$dir = "$parentdir$subdir"; |
|---|
| 75 |
pkValidatorFilePersistent::removeOldFiles($dir); |
|---|
| 76 |
$imagePreview = $this->getOption('image-preview'); |
|---|
| 77 |
$width = $imagePreview['width']; |
|---|
| 78 |
$height = $imagePreview['height']; |
|---|
| 79 |
if ($height === false) |
|---|
| 80 |
{ |
|---|
| 81 |
list($iwidth, $iheight) = getimagesize($info['tmp_name']); |
|---|
| 82 |
if ($iheight && $iwidth) |
|---|
| 83 |
{ |
|---|
| 84 |
$height = $width * ($iheight / $iwidth); |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
$resizeType = $imagePreview['resizeType']; |
|---|
| 88 |
if (!in_array($resizeType, array('c', 's'))) |
|---|
| 89 |
{ |
|---|
| 90 |
$resizeType = 'c'; |
|---|
| 91 |
} |
|---|
| 92 |
$imagename = "$persistid.$width.$height.$resizeType.jpg"; |
|---|
| 93 |
$url = "$subdir/$imagename"; |
|---|
| 94 |
$output = "$dir/$imagename"; |
|---|
| 95 |
if (!file_exists($output)) |
|---|
| 96 |
{ |
|---|
| 97 |
if ($imagePreview['resizeType'] === 'c') |
|---|
| 98 |
{ |
|---|
| 99 |
$method = 'cropOriginal'; |
|---|
| 100 |
} |
|---|
| 101 |
else |
|---|
| 102 |
{ |
|---|
| 103 |
$method = 'scaleToFit'; |
|---|
| 104 |
} |
|---|
| 105 |
if (!is_dir($dir)) |
|---|
| 106 |
{ |
|---|
| 107 |
|
|---|
| 108 |
// it permissions that allow your web server full access |
|---|
| 109 |
mkdir($dir); |
|---|
| 110 |
} |
|---|
| 111 |
pkImageConverter::$method( |
|---|
| 112 |
$info['tmp_name'], |
|---|
| 113 |
$output, |
|---|
| 114 |
$width, |
|---|
| 115 |
$height); |
|---|
| 116 |
} |
|---|
| 117 |
$result .= "<img src='$url' />"; |
|---|
| 118 |
} |
|---|
| 119 |
$result .= $this->getOption('existing-html'); |
|---|
| 120 |
} |
|---|
| 121 |
return $result . |
|---|
| 122 |
$this->renderTag('input', |
|---|
| 123 |
array_merge( |
|---|
| 124 |
array( |
|---|
| 125 |
'type' => $this->getOption('type'), |
|---|
| 126 |
'name' => $name . '[newfile]'), |
|---|
| 127 |
$attributes)) . |
|---|
| 128 |
$this->renderTag('input', |
|---|
| 129 |
array( |
|---|
| 130 |
'type' => 'hidden', |
|---|
| 131 |
'name' => $name . '[persistid]', |
|---|
| 132 |
'value' => $persistid)); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
} |
|---|
| 136 |
|
|---|