149 lines
3.2 KiB
PHP
149 lines
3.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Trinity\Bundle\ContentManagerBundle\Block;
|
||
|
|
||
|
use Propel;
|
||
|
use PropelPDO;
|
||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||
|
use Trinity\Bundle\ContentManagerBundle\Model\BlockPeer;
|
||
|
use Trinity\Bundle\ContentManagerBundle\Model\Block;
|
||
|
|
||
|
class FileBlock extends Block
|
||
|
{
|
||
|
public $rollback_value = null;
|
||
|
public $delete_value = false;
|
||
|
/**
|
||
|
*
|
||
|
* Upload features to use block value as file type
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
protected $uploadDirs = array(
|
||
|
'value' => 'uploads/slots',
|
||
|
);
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
parent::__construct();
|
||
|
|
||
|
$this->setClassKey('Trinity\Bundle\ContentManagerBundle\Block\FileBlock');
|
||
|
}
|
||
|
|
||
|
public function setValue($v)
|
||
|
{
|
||
|
if (!isset($this->rollback_value)) {
|
||
|
$this->rollback_value = null;
|
||
|
}
|
||
|
|
||
|
$this->rollback_value = $this->getValue();
|
||
|
|
||
|
if ($v !== null && is_numeric($v)) {
|
||
|
$v = (string)$v;
|
||
|
}
|
||
|
|
||
|
if ($this->value !== $v) {
|
||
|
$this->value = $v;
|
||
|
$this->modifiedColumns[] = BlockPeer::VALUE;
|
||
|
}
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function getDeleteValue()
|
||
|
{
|
||
|
return $this->delete_value;
|
||
|
}
|
||
|
|
||
|
public function setDeleteValue($v)
|
||
|
{
|
||
|
$this->delete_value = $v;
|
||
|
}
|
||
|
|
||
|
public function getWebPathForValue()
|
||
|
{
|
||
|
return null === $this->value ? null : $this->getUploadDir('value') . DIRECTORY_SEPARATOR . $this->value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return the upload dir of $field field
|
||
|
*
|
||
|
* @param $field
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getUploadDir($field)
|
||
|
{
|
||
|
return $this->uploadDirs[$field];
|
||
|
}
|
||
|
|
||
|
public function preSave(PropelPDO $con = null)
|
||
|
{
|
||
|
$this->uploadValue();
|
||
|
|
||
|
return parent::preSave($con);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Upload the value field
|
||
|
*
|
||
|
* @return void
|
||
|
*
|
||
|
*/
|
||
|
public function uploadValue()
|
||
|
{
|
||
|
if (null === $this->value && !$this->delete_value && null !== $this->rollback_value) {
|
||
|
$this->value = $this->rollback_value; // keep the file
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (null === $this->value || !is_object($this->value)) {
|
||
|
return true; // no file to upload
|
||
|
}
|
||
|
|
||
|
$filename = $this->getNewFileName($this->value->guessExtension());
|
||
|
|
||
|
$this->value->move($this->getUploadDir('value'), $filename);
|
||
|
|
||
|
$this->value = $filename;
|
||
|
}
|
||
|
|
||
|
public function getNewFilename($extension)
|
||
|
{
|
||
|
return md5(time() . uniqid()) . ($extension ? '.' . $extension : '');
|
||
|
}
|
||
|
|
||
|
public function postSave(PropelPDO $con = null)
|
||
|
{
|
||
|
if ($this->delete_value) {
|
||
|
$this->removeValue();
|
||
|
}
|
||
|
|
||
|
return parent::postSave($con);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove the value file present in 'rollback_value'
|
||
|
*
|
||
|
* @return void
|
||
|
*
|
||
|
*/
|
||
|
public function removeValue()
|
||
|
{
|
||
|
if (null !== $this->rollback_value) {
|
||
|
|
||
|
$file = $this->getUploadDir('value') . DIRECTORY_SEPARATOR . $this->rollback_value;
|
||
|
|
||
|
if (file_exists($file)) {
|
||
|
unlink($file);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function postDelete(PropelPDO $con = null)
|
||
|
{
|
||
|
$this->removeValue();
|
||
|
|
||
|
return parent::postDelete($con);
|
||
|
}
|
||
|
}
|