Пример #1
0
 /**
  * Generate a layout thumbnail according to the configuration.
  *
  * @access public
  *
  * @param Layout        $layout The layout to treate
  * @param BBApplication $app    The current instance of BBApplication
  *
  * @return mixed FALSE if something wrong, the ressource path of the thumbnail elsewhere
  */
 public function generateThumbnail(Layout $layout, BBApplication $app)
 {
     // Is the layout valid ?
     if (!$layout->isValid()) {
         return false;
     }
     // Is some layout configuration existing ?
     if (null === $app->getConfig()->getSection('layout')) {
         return false;
     }
     $layoutconfig = $app->getConfig()->getSection('layout');
     // Is some thumbnail configuration existing ?
     if (!isset($layoutconfig['thumbnail'])) {
         return false;
     }
     $thumbnailconfig = $layoutconfig['thumbnail'];
     // Is gd available ?
     if (!function_exists('gd_info')) {
         return false;
     }
     $gd_info = gd_info();
     // Is the selected format supported by gd ?
     if (!isset($thumbnailconfig['format'])) {
         return false;
     }
     if (true !== $gd_info[strtoupper($thumbnailconfig['format']) . ' Support']) {
         return false;
     }
     // Is the template file existing ?
     if (!isset($thumbnailconfig['template'])) {
         return false;
     }
     $templatefile = $thumbnailconfig['template'];
     $thumbnaildir = dirname($templatefile);
     File::resolveFilepath($templatefile, null, array('include_path' => $app->getResourceDir()));
     if (false === file_exists($templatefile) || false === is_readable($templatefile)) {
         return false;
     }
     try {
         $gd_function = 'imagecreatefrom' . strtolower($thumbnailconfig['format']);
         $thumbnail = $gd_function($templatefile);
         $thumbnailfile = $thumbnaildir . '/' . $layout->getUid() . '.' . strtolower($thumbnailconfig['format']);
         // Is a background color existing ?
         if (!isset($thumbnailconfig['background']) || !is_array($thumbnailconfig['background']) || 3 != count($thumbnailconfig['background'])) {
             return false;
         }
         $background = imagecolorallocate($thumbnail, $thumbnailconfig['background'][0], $thumbnailconfig['background'][1], $thumbnailconfig['background'][2]);
         // Is a clipping zone existing ?
         if (!isset($thumbnailconfig['clip']) || !is_array($thumbnailconfig['clip']) || 4 != count($thumbnailconfig['clip'])) {
             return false;
         }
         $gridcolumn = 12;
         if (null !== ($lessconfig = $app->getConfig()->getSection('less'))) {
             if (isset($lessconfig['gridcolumn'])) {
                 $gridcolumn = $lessconfig['gridcolumn'];
             }
         }
         $domlayout = $layout->getDomDocument();
         if (!$domlayout->hasChildNodes() || !$domlayout->firstChild->hasChildNodes()) {
             $this->drawRect($thumbnail, $thumbnailconfig['clip'], $background);
         } else {
             $this->drawThumbnailZone($thumbnail, $domlayout->firstChild, $thumbnailconfig['clip'], $background, $gridcolumn);
         }
         imagesavealpha($thumbnail, true);
         $thumbnaildir = dirname(File::normalizePath($app->getCurrentResourceDir() . '/' . $thumbnailfile));
         if (false === is_dir($thumbnaildir)) {
             mkdir($thumbnaildir, 0755, true);
         }
         imagepng($thumbnail, File::normalizePath($app->getCurrentResourceDir() . '/' . $thumbnailfile));
     } catch (\Exception $e) {
         return false;
     }
     $layout->setPicPath($thumbnailfile);
     return $layout->getPicPath();
 }
Пример #2
0
 /**
  * @see BackBee\Renderer\RendererAdapterInterface::updateLayout()
  */
 public function updateLayout(Layout $layout, $layoutFile)
 {
     if (false === $layoutFile) {
         return false;
     }
     $mainLayoutRow = $layout->getDomDocument();
     if (false === $layout->isValid() || null === $mainLayoutRow) {
         throw new RendererException('Malformed data for the layout layout.');
     }
     // Add an php instruction to each final droppable zone found
     $xpath = new \DOMXPath($mainLayoutRow);
     $textNode = $mainLayoutRow->createTextNode('<?php echo $this->container()->first(); ?>');
     $nextNode = $mainLayoutRow->createTextNode('<?php echo $this->container()->next(); ?>');
     foreach ($xpath->query('//div[@class!="clear"]') as $node) {
         if (!$node->hasChildNodes()) {
             $node->appendChild(clone $textNode);
             $textNode = $nextNode;
         }
     }
     libxml_use_internal_errors(true);
     $domlayout = new \DOMDocument();
     $layoutcontent = str_replace(array('<?php', '?>'), array('&lt;?php', '?&gt;'), file_get_contents($layoutFile));
     @$domlayout->loadHTML($layoutcontent);
     $domlayout->formatOutput = true;
     $layoutNode = $domlayout->importNode($mainLayoutRow->firstChild, true);
     $layoutid = $layoutNode->getAttribute('id');
     $xPath = new \DOMXPath($domlayout);
     if (($targetNodes = $xPath->query('//div[@id="' . $layoutid . '"]')) && 0 < $targetNodes->length) {
         foreach ($targetNodes as $targetNode) {
             $targetNode->parentNode->replaceChild($layoutNode, $targetNode);
         }
     } elseif (($targetNodes = $domlayout->getElementsByTagName('body')) && 0 < $targetNodes->length) {
         foreach ($targetNodes as $targetNode) {
             $targetNode->appendChild($layoutNode);
         }
     } else {
         $domlayout->appendChild($layoutNode);
     }
     if (!file_put_contents($layoutFile, preg_replace_callback('/(&lt;|<)\\?php(.+)\\?(&gt;|>)/iu', create_function('$matches', 'return "<?php".html_entity_decode(urldecode($matches[2]))."?".">";'), $domlayout->saveHTML()))) {
         throw new RendererException(sprintf('Unable to save layout %s.', $layoutFile), RendererException::LAYOUT_ERROR);
     }
     libxml_clear_errors();
     return $layoutFile;
 }