load() public method

Initiliaze File Object
public load ( Model $property, Model $document, string $filename = null ) : void
$property Gc\Property\Model Property
$document Gc\Document\Model Document
$filename string Filename
return void
Beispiel #1
0
 /**
  * Initiliaze files
  *
  * @return void
  */
 protected function initializeFiles()
 {
     $_FILES = array('test' => array('name' => __DIR__ . '/_files/test.jpg', 'type' => 'plain/text', 'size' => 8, 'tmp_name' => __DIR__ . '/_files/test.jpg', 'error' => 0));
     $property = PropertyModel::fromArray(array('id' => 'test-upload'));
     $document = DocumentModel::fromArray(array('id' => 'test-upload'));
     $this->object->load($property, $document, 'test');
 }
Beispiel #2
0
 /**
  * Save upload editor
  *
  * @return void
  */
 public function save()
 {
     $parameters = $this->getConfig();
     $data = array();
     if (!empty($_FILES[$this->getName()]['name'])) {
         $oldFiles = $_FILES;
         $file = $_FILES[$this->getName()];
         //Ignore others data
         $_FILES = array();
         $_FILES[$this->getName()] = $file;
         $fileClass = new File();
         $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName());
         $fileClass->upload();
         $files = $fileClass->getFiles();
         if (!empty($files)) {
             foreach ($files as $file) {
                 $name = $file['filename'];
                 $file = $fileClass->getPath() . '/' . $name;
                 if (file_exists($file)) {
                     $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                     $finfo = finfo_open($const);
                     // return mimetype extension
                     if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) {
                         unlink($file);
                     } else {
                         $fileInfo = @getimagesize($file);
                         $data[] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']);
                     }
                     finfo_close($finfo);
                 }
             }
             if (empty($parameters['is_multiple']) and !empty($data[0])) {
                 $data = $data[0];
             }
             $data = serialize($data);
         }
         //Restore file data
         $_FILES = $oldFiles;
     } else {
         $data = $this->getRequest()->getPost()->get($this->getName() . '-hidden');
     }
     $this->setValue(empty($data) ? '' : $data);
 }
Beispiel #3
0
 /**
  * Save Image cropper editor
  *
  * @return void
  */
 public function save()
 {
     $post = $this->getRequest()->getPost();
     $parameters = $this->getConfig();
     $data = array();
     $imageModel = new Image();
     $fileClass = new File();
     $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName());
     $backgroundColor = empty($parameters['background']) ? '#000000' : $parameters['background'];
     if (!empty($_FILES[$this->getName()]['name'])) {
         $oldFiles = $_FILES;
         $file = $_FILES[$this->getName()];
         //Ignore others data
         $_FILES = array();
         $_FILES[$this->getName()] = $file;
         $fileClass->upload();
         $files = $fileClass->getFiles();
         if (!empty($files) and is_array($files)) {
             foreach ($files as $file) {
                 $name = $file['filename'];
                 $file = $fileClass->getPath() . $name;
                 if (file_exists($file)) {
                     $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                     $finfo = finfo_open($const);
                     // return mimetype extension
                     if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) {
                         unlink($file);
                     } else {
                         $fileInfo = @getimagesize($file);
                         $data['original'] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']);
                         $imageModel->open($file);
                         foreach ($parameters['size'] as $size) {
                             $imageModel->open($file);
                             $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor);
                             $sizeFilename = preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $name);
                             $imageModel->save($fileClass->getPath() . $sizeFilename);
                             $fileInfo = @getimagesize($fileClass->getPath() . $sizeFilename);
                             $data[$size['name']] = array('value' => $sizeFilename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0);
                         }
                     }
                     finfo_close($finfo);
                 }
             }
         }
         //Restore file data
         $_FILES = $oldFiles;
     } else {
         $data = $post->get($this->getName() . '-hidden');
         $data = unserialize($data);
         if (!empty($data)) {
             if (!empty($data['original']['value'])) {
                 foreach ($parameters['size'] as $size) {
                     $x = (int) $post->get($this->getName() . $size['name'] . '-x');
                     $y = (int) $post->get($this->getName() . $size['name'] . '-y');
                     $filename = !empty($data[$size['name']]['value']) ? $data[$size['name']]['value'] : preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $data['original']['value']);
                     $imageModel->open($fileClass->getPath() . $data['original']['value']);
                     $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor, $x, $y);
                     $imageModel->save($fileClass->getPath() . $filename);
                     if (!empty($data[$size['name']]['value'])) {
                         $data[$size['name']]['x'] = $x;
                         $data[$size['name']]['y'] = $y;
                     } else {
                         $fileInfo = @getimagesize($fileClass->getPath() . $filename);
                         $data[$size['name']] = array('value' => $filename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0);
                     }
                 }
                 foreach ($data as $name => $file) {
                     if ($name == 'original') {
                         continue;
                     }
                     $found = false;
                     foreach ($parameters['size'] as $size) {
                         if ($size['name'] == $name) {
                             $found = true;
                             $file['options'] = $size;
                             break;
                         }
                     }
                     if (empty($found)) {
                         unset($data[$name]);
                     }
                 }
             }
         }
     }
     $data = serialize($data);
     $this->setValue(empty($data) ? '' : $data);
 }
Beispiel #4
0
 /**
  * Load upload editor
  *
  * @return string
  */
 public function load()
 {
     $parameters = $this->getConfig();
     $options = empty($parameters['options']) ? array() : $parameters['options'];
     $this->initScript();
     $fileList = array();
     $files = unserialize($this->getValue());
     if (!empty($files)) {
         $fileClass = new File();
         $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument());
         foreach ($files as $fileData) {
             $fileObject = array();
             $fileObject['name'] = $fileData['value'];
             $fileObject['filename'] = $fileData['value'];
             $fileObject['thumbnail_url'] = $fileData['value'];
             $router = Registry::get('Application')->getMvcEvent()->getRouter();
             $fileObject['delete_url'] = $router->assemble(array('document_id' => $this->getDatatype()->getDocument()->getId(), 'property_id' => $this->getProperty()->getId(), 'file' => base64_encode($fileData['value'])), array('name' => 'content/media/remove'));
             $fileObject['delete_type'] = 'DELETE';
             $fileList[] = $fileObject;
         }
     }
     return $this->addPath(__DIR__)->render('upload-editor.phtml', array('property' => $this->getProperty(), 'uploadUrl' => $this->getUploadUrl(), 'name' => $this->getName(), 'files' => json_encode($fileList), 'options' => $options));
 }
Beispiel #5
0
 /**
  * Remove directories
  *
  * @return mixed
  */
 protected function removeDirectories()
 {
     $file = new File();
     $file->load($this->property, $this->document);
     $dir = $file->getPath() . $file->getDirectory();
     if (is_dir($dir)) {
         $data = glob($dir . '/*');
         foreach ($data as $file) {
             unlink($file);
         }
         $tmpDir = $dir;
         while ($tmpDir != GC_MEDIA_PATH . '/files') {
             rmdir($tmpDir);
             $tmpDir = realpath(dirname($tmpDir));
         }
     }
 }
Beispiel #6
0
 /**
  * Delete file
  *
  * @return \Zend\View\Model\JsonModel
  */
 public function removeAction()
 {
     $property = Property\Model::fromId($this->getRouteMatch()->getParam('property_id'));
     $document = Document\Model::fromId($this->getRouteMatch()->getParam('document_id'));
     if ($this->getRequest()->getMethod() != 'DELETE' or empty($document) or empty($property)) {
         return $this->returnJson(array('error' => true));
     }
     $file = base64_decode($this->getRouteMatch()->getParam('file'));
     $fileClass = new File();
     $fileClass->load($property, $document);
     return $this->returnJson(array($fileClass->remove($file)));
 }