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.         if (!\Pimcore\Document::isAvailable()) {
  48.             Logger::error("Couldn't create image-thumbnail of document " $this->getRealFullPath() . ' no document adapter is available');
  49.             return;
  50.         }
  51.         try {
  52.             $converter \Pimcore\Document::getInstance();
  53.             $converter->load($this);
  54.             // read from blob here, because in $this->update() (see above) $this->getFileSystemPath() contains the old data
  55.             $pageCount $converter->getPageCount();
  56.             $this->setCustomSetting('document_page_count'$pageCount);
  57.         } catch (\Exception $e) {
  58.             Logger::error((string) $e);
  59.             $this->setCustomSetting('document_page_count''failed');
  60.         }
  61.     }
  62.     /**
  63.      * returns null when page count wasn't processed yet (done asynchronously)
  64.      *
  65.      * @return int|null
  66.      */
  67.     public function getPageCount()
  68.     {
  69.         return $this->getCustomSetting('document_page_count');
  70.     }
  71.     /**
  72.      * @param string|array|Image\Thumbnail\Config $thumbnailName
  73.      * @param int $page
  74.      * @param bool $deferred $deferred deferred means that the image will be generated on-the-fly (details see below)
  75.      *
  76.      * @return Document\ImageThumbnail
  77.      */
  78.     public function getImageThumbnail($thumbnailName$page 1$deferred false)
  79.     {
  80.         if (!\Pimcore\Document::isAvailable()) {
  81.             Logger::error("Couldn't create image-thumbnail of document " $this->getRealFullPath() . ' no document adapter is available');
  82.             return new Document\ImageThumbnail(null);
  83.         }
  84.         if (!$this->getCustomSetting('document_page_count')) {
  85.             Logger::info('Image thumbnail not yet available, processing is done asynchronously.');
  86.             $this->addToUpdateTaskQueue();
  87.         }
  88.         return new Document\ImageThumbnail($this$thumbnailName$page$deferred);
  89.     }
  90.     /**
  91.      * @param int|null $page
  92.      *
  93.      * @return string|null
  94.      */
  95.     public function getText($page null)
  96.     {
  97.         if (\Pimcore\Document::isAvailable() && \Pimcore\Document::isFileTypeSupported($this->getFilename())) {
  98.             if ($this->getCustomSetting('document_page_count')) {
  99.                 $cacheKey 'asset_document_text_' $this->getId() . '_' . ($page $page 'all');
  100.                 if (!$text Cache::load($cacheKey)) {
  101.                     $document \Pimcore\Document::getInstance();
  102.                     $text $document->getText($page$this);
  103.                     Cache::save($text$cacheKey$this->getCacheTags(), null99true); // force cache write
  104.                 }
  105.                 return $text;
  106.             } else {
  107.                 Logger::info('Unable to fetch text of ' $this->getRealFullPath() . ' as it was not processed yet by the maintenance script');
  108.             }
  109.         } else {
  110.             Logger::warning("Couldn't get text out of document " $this->getRealFullPath() . ' no document adapter is available');
  111.         }
  112.         return null;
  113.     }
  114. }