public function generateLayout($filename, Site $site = null, $extention = self::EXTENSION) { try { $data = Yaml::parse($filename); $uid = $this->getUidGenerator()->generateUid($filename, $data, $site); $layout = new Layout($uid); $layout->setPicPath($layout->getUid() . '.png'); if ($site !== null) { $layout->setSite($site); } if (array_key_exists('label', $data) && $data['label'] !== null) { $layout->setLabel($data['label']); } else { $layout->setLabel(basename($filename, '.' . self::EXTENSION)); } if (array_key_exists('template', $data)) { $this->computeTemplate($layout, $data['template']); } if (array_key_exists('columns', $data)) { $layout->setData($this->computeColumns($data['columns'])); } else { throw new LayoutYamlException('Layout ' . $layout->getLabel() . ' definition need columns', LayoutYamlException::NO_COLUMN_ERROR); } } catch (ParseException $e) { throw new LayoutYamlException($e->getMessage(), LayoutYamlException::LAYOUT_BUILD_ERROR, $e, $e->getParsedFile(), $e->getParsedLine()); } return $layout; }
/** * @covers ::getLayoutsAction */ public function testGetLayoutsAction() { // authenticate user , set up permissions $user = $this->createAuthUser('super_admin', array('ROLE_API_USER')); $aclManager = $this->getBBApp()->getContainer()->get('security.acl_manager'); $aclManager->insertOrUpdateObjectAce(new ObjectIdentity($this->site->getObjectIdentifier(), get_class($this->site)), new UserSecurityIdentity($user->getGroups()[0]->getId(), 'BackBee\\Security\\Group'), MaskBuilder::MASK_VIEW); $aclManager->insertOrUpdateObjectAce(new ObjectIdentity('all', 'BackBee\\Site\\Layout'), new UserSecurityIdentity($user->getGroups()[0]->getId(), 'BackBee\\Security\\Group'), MaskBuilder::MASK_VIEW); $response = $this->getController()->getLayoutsAction($this->site->getUid()); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('application/json', $response->headers->get('Content-Type')); $content = json_decode($response->getContent(), true); $this->assertInternalType('array', $content); $this->assertCount(1, $content); $this->assertEquals($this->layout->getUid(), $content[0]['uid']); }
/** * 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(); }
public function flush(Layout $layout) { if (null === $this->app->getEntityManager()->find('BackBee\\Site\\Layout', $layout->getUid())) { $this->app->getEntityManager()->persist($layout); } $this->app->getEntityManager()->flush($layout); }