vendor/pimcore/pimcore/models/Asset/Document.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Asset;
  15. use Pimcore\Cache;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. /**
  19.  * @method \Pimcore\Model\Asset\Dao getDao()
  20.  */
  21. class Document extends Model\Asset
  22. {
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     protected $type 'document';
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     protected function update($params = [])
  31.     {
  32.         if ($this->getDataChanged()) {
  33.             $this->removeCustomSetting('document_page_count');
  34.         }
  35.         parent::update($params);
  36.         if ($params['isUpdate']) {
  37.             $this->clearThumbnails();
  38.         }
  39.     }
  40.     /**
  41.      * @internal
  42.      *
  43.      * @param string|null $path
  44.      */
  45.     public function processPageCount($path null)
  46.     {
  47.         $pageCount null;
  48.         if (!\Pimcore\Document::isAvailable()) {
  49.             Logger::error("Couldn't create image-thumbnail of document " $this->getRealFullPath() . ' no document adapter is available');
  50.             return;
  51.         }
  52.         try {
  53.             $converter \Pimcore\Document::getInstance();
  54.             $converter->load($this);
  55.             // read from blob here, because in $this->update() (see above) $this->getFileSystemPath() contains the old data
  56.             $pageCount $converter->getPageCount();
  57.             $this->setCustomSetting('document_page_count'$pageCount);
  58.         } catch (\Exception $e) {
  59.             Logger::error((string) $e);
  60.             $this->setCustomSetting('document_page_count''failed');
  61.         }
  62.     }
  63.     /**
  64.      * returns null when page count wasn't processed yet (done asynchronously)
  65.      *
  66.      * @return int|null
  67.      */
  68.     public function getPageCount()
  69.     {
  70.         return $this->getCustomSetting('document_page_count');
  71.     }
  72.     /**
  73.      * @param string|array|Image\Thumbnail\Config $thumbnailName
  74.      * @param int $page
  75.      * @param bool $deferred $deferred deferred means that the image will be generated on-the-fly (details see below)
  76.      *
  77.      * @return Document\ImageThumbnail
  78.      */
  79.     public function getImageThumbnail($thumbnailName$page 1$deferred false)
  80.     {
  81.         if (!\Pimcore\Document::isAvailable()) {
  82.             Logger::error("Couldn't create image-thumbnail of document " $this->getRealFullPath() . ' no document adapter is available');
  83.             return new Document\ImageThumbnail(null);
  84.         }
  85.         if (!$this->getCustomSetting('document_page_count')) {
  86.             Logger::info('Image thumbnail not yet available, processing is done asynchronously.');
  87.             $this->addToUpdateTaskQueue();
  88.             return new Document\ImageThumbnail(null);
  89.         }
  90.         return new Document\ImageThumbnail($this$thumbnailName$page$deferred);
  91.     }
  92.     /**
  93.      * @param int|null $page
  94.      *
  95.      * @return string|null
  96.      */
  97.     public function getText($page null)
  98.     {
  99.         if (\Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($this->getFilename())) {
  100.             if ($this->getCustomSetting('document_page_count')) {
  101.                 $cacheKey 'asset_document_text_' $this->getId() . '_' . ($page $page 'all');
  102.                 if (!$text Cache::load($cacheKey)) {
  103.                     $document \Pimcore\Document::getInstance();
  104.                     $text $document->getText($page$this);
  105.                     Cache::save($text$cacheKey$this->getCacheTags(), null99true); // force cache write
  106.                 }
  107.                 return $text;
  108.             } else {
  109.                 Logger::info('Unable to fetch text of ' $this->getRealFullPath() . ' as it was not processed yet by the maintenance script');
  110.             }
  111.         } else {
  112.             Logger::warning("Couldn't get text out of document " $this->getRealFullPath() . ' no document adapter is available');
  113.         }
  114.         return null;
  115.     }
  116. }