/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { // merge settings $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); $media = $settings['mediaId']; return $this->renderResponse('SonataMediaBundle:Block:block_feature_media.html.twig', array('media' => $media, 'block' => $block, 'settings' => $settings), $response); }
/** * {@inheritdoc} */ public function getSetting($name) { if (!array_key_exists($name, $this->settings)) { throw new \RuntimeException(sprintf('Unable to find the option `%s` (%s) - define the option in the related BlockServiceInterface', $name, $this->block->getType())); } return $this->settings[$name]; }
/** * {@inheritdoc} */ public function buildEditForm(FormMapper $formMapper, BlockInterface $block) { $contextChoices = $this->getContextChoices(); $formatChoices = $this->getFormatChoices($block->getSetting('mediaId')); $translator = $this->container->get('translator'); $formMapper->add('settings', 'sonata_type_immutable_array', array('keys' => array(array('title', 'text', array('required' => false)), array('content', 'textarea', array('required' => false)), array('orientation', 'choice', array('choices' => array('left' => $translator->trans('feature_left_choice', array(), 'SonataMediaBundle'), 'right' => $translator->trans('feature_right_choice', array(), 'SonataMediaBundle')))), array('context', 'choice', array('required' => true, 'choices' => $contextChoices)), array('format', 'choice', array('required' => count($formatChoices) > 0, 'choices' => $formatChoices)), array($this->getMediaBuilder($formMapper), null, array())))); }
/** * {@inheritdoc} */ public function render(BlockInterface $block, Response $response = null) { if ($this->logger) { $this->logger->info(sprintf('[cms::renderBlock] block.id=%d, block.type=%s ', $block->getId(), $block->getType())); } try { $service = $this->blockServiceManager->get($block); $service->load($block); // load the block $response = $service->execute($block, $response); if (!$response instanceof Response) { throw new \RuntimeException('A block service must return a Response object'); } } catch (\Exception $e) { if ($this->logger) { $this->logger->crit(sprintf('[cms::renderBlock] block.id=%d - error while rendering block - %s', $block->getId(), $e->getMessage())); } if ($this->debug) { throw $e; } $response = new Response(); $response->setPrivate(); } return $response; }
/** * {@inheritdoc} */ public function load(BlockInterface $block) { $userManager = $this->container->get('fos_user.user_manager'); $settings = $block->getSettings(); $userRegistrations = $userManager->fetchRegistrationCount(isset($settings['fetchCountRecord']) ? $settings['fetchCountRecord'] : 5); $block->setSetting('userRegistrations', $userRegistrations); }
/** * {@inheritdoc} */ public function validateBlock(ErrorElement $errorElement, BlockInterface $block) { if (($name = $block->getSetting('menu_name')) && $name !== '' && !$this->menuProvider->has($name)) { // If we specified a menu_name, check that it exists $errorElement->with('menu_name')->addViolation('sonata.block.menu.not_existing', array('name' => $name))->end(); } }
/** * {@inheritdoc} */ public function addChildren(BlockInterface $child) { $this->children[] = $child; $child->setParent($this); if ($child instanceof DashboardBlockInterface) { $child->setDashboard($this->getDashboard()); } }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); $revisions = array(); foreach ($this->auditReader->findRevisionHistory($settings['limit'], 0) as $revision) { $revisions[] = array('revision' => $revision, 'entities' => $this->auditReader->findEntitesChangedAtRevision($revision->getRev())); } return $this->renderResponse('SonataDoctrineORMAdminBundle:Block:block_audit.html.twig', array('block' => $block, 'settings' => $settings, 'revisions' => $revisions), $response); }
/** * {@inheritdoc} */ public function addChild(BlockInterface $child, $key = null) { $child->setParent($this); if ($key != null) { $this->children->set($key, $child); return true; } return $this->children->add($child); }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); $response = $this->renderResponse('IphpCoreBundle:Block:block_container.html.twig', array('container' => $block, 'settings' => $settings), $response); //$response->setContent('Контент контейнера' /*Mustache::replace($settings['layout'], array( 'CONTENT' => $response->getContent() ))*/ //); return $response; }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); $dashboardGroups = $this->pool->getDashboardGroups(); $visibleGroups = array(); foreach ($dashboardGroups as $name => $dashboardGroup) { if (!$settings['groups'] || in_array($name, $settings['groups'])) { $visibleGroups[] = $dashboardGroup; } } return $this->renderResponse($this->pool->getTemplate('list_block'), array('block' => $block, 'settings' => $settings, 'admin_pool' => $this->pool, 'groups' => $visibleGroups), $response); }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $parameters = (array) json_decode($block->getSetting('parameters'), true); $parameters = array_merge($parameters, array('_block' => $block)); $settings = array_merge($this->getDefaultSettings(), (array) $block->getSettings()); try { $actionContent = $this->kernel->render($settings['action'], $parameters); } catch (\Exception $e) { throw $e; } $content = self::mustache($block->getSetting('layout'), array('CONTENT' => $actionContent)); return $this->renderResponse('SonataBlockBundle:Block:block_core_action.html.twig', array('content' => $content, 'block' => $block), $response); }
/** * {@inheritdoc} */ public function buildEditForm(FormMapper $formMapper, BlockInterface $block) { if (!$block->getSetting('mediaId') instanceof MediaInterface) { $this->load($block); } $formatChoices = $this->getFormatChoices($block->getSetting('mediaId')); $keys[] = array('title', 'text', array('required' => false)); $keys[] = array($this->getMediaBuilder($formMapper), null, array()); $keys[] = array('format', 'choice', array('required' => count($formatChoices) > 0, 'choices' => $formatChoices)); if ($this->getTemplates()) { $keys[] = array('template', 'choice', array('choices' => $this->getTemplates())); } $formMapper->add('settings', 'sonata_type_immutable_array', array('keys' => $keys, 'attr' => array('class' => 'bruery-immutable-container'))); }
/** * {@inheritdoc} */ public function load(BlockInterface $block) { $gender = $this->manager->fetchGenderCount(); $genderChart = []; $genderList = []; foreach ($gender as $data) { switch ($data['gender']) { case 'u': $genderList[] = ['gender' => $this->translator->trans('gender_unknown', [], 'SonataUserBundle'), 'total' => $data['total']]; $genderChart[$this->translator->trans('gender_unknown', [], 'SonataUserBundle')] = array($data['total']); break; case 'm': $genderList[] = ['gender' => $this->translator->trans('gender_male', [], 'SonataUserBundle'), 'total' => $data['total']]; $genderChart[$this->translator->trans('gender_male', [], 'SonataUserBundle')] = array($data['total']); break; case 'f': $genderList[] = ['gender' => $this->translator->trans('gender_female', [], 'SonataUserBundle'), 'total' => $data['total']]; $genderChart[$this->translator->trans('gender_female', [], 'SonataUserBundle')] = array($data['total']); break; } } $totalGender = $this->manager->fetchGenderCountTotal(); $block->setSetting('genderList', $genderList); $block->setSetting('genderChart', json_encode($genderChart)); $block->setSetting('genderTotal', $totalGender); }
/** * {@inheritdoc} */ public function load(BlockInterface $block) { $ageBrackets = $this->collectionManager->findByContext($this->context); $ageBracket = $this->manager->fetchAgeBracketCount(); $dataAgeBracketList = []; $dataAgeBracketChart = []; $dataAgeBracketBar = []; //PieChart Data & List Data foreach ($ageBrackets as $idx => $bracket) { $dataAgeBracketChart[$bracket->getName()] = array(0); $dataAgeBracketList[$idx] = ['age' => $bracket->getName(), 'total' => 0]; foreach ($ageBracket as $age) { if ($bracket->getName() == $age['name']) { $dataAgeBracketChart[$bracket->getName()] = array($age['ageBracketCount']); $dataAgeBracketList[$idx] = ['age' => $bracket->getName(), 'total' => $age['ageBracketCount']]; } } } $ageByGender = $this->manager->fetchAgeBracketCountByGender(); //LineGraph Data $ageX = []; $ageU = []; $ageF = []; $ageM = []; foreach ($ageBrackets as $idx => $bracket) { $ageX[$idx] = $bracket->getName(); $ageU[$idx] = "0"; $ageM[$idx] = "0"; $ageF[$idx] = "0"; foreach ($ageByGender as $age) { if ($bracket->getName() == $age['name']) { switch ($age['gender']) { case 'u': $ageU[$idx] = $age['ageBracketCount']; break; case 'm': $ageM[$idx] = $age['ageBracketCount']; break; case 'f': $ageF[$idx] = $age['ageBracketCount']; break; } } } } $dataAgeBracketBar['x'] = $ageX; $dataAgeBracketBar['dataU'] = $ageU; $dataAgeBracketBar['dataM'] = $ageM; $dataAgeBracketBar['dataF'] = $ageF; $ageBracketTotal = $this->manager->fetchAgeBracketCountTotal(); $block->setSetting('ageBracketList', $dataAgeBracketList); $block->setSetting('ageBracketBar', json_encode($dataAgeBracketBar)); $block->setSetting('ageBracketChart', json_encode($dataAgeBracketChart)); $block->setSetting('ageBracketTotal', $ageBracketTotal); }
/** * @param BlockInterface $block * @param OutputInterface $output * @param bool $extended * @param int $space */ public function renderBlock(BlockInterface $block, OutputInterface $output, $extended, $space = 0) { $output->writeln(sprintf('%s <comment>> Id: %d - type: %s - name: %s</comment>', str_repeat(' ', $space), $block->getId(), $block->getType(), $block->getName())); if ($extended) { $output->writeln(sprintf('%s page class: <comment>%s</comment>', str_repeat(' ', $space + 1), get_class($block->getPage()))); foreach ($block->getSettings() as $name => $value) { $output->writeln(sprintf('%s %s: %s', str_repeat(' ', $space + 1), $name, var_export($value, 1))); } } foreach ($block->getChildren() as $block) { $this->renderBlock($block, $output, $extended, $space + 1); } }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { // merge settings $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); $feeds = false; if ($settings['url']) { $options = array('http' => array('user_agent' => 'Sonata/RSS Reader', 'timeout' => 2)); // retrieve contents with a specific stream context to avoid php errors $content = @file_get_contents($settings['url'], false, stream_context_create($options)); if ($content) { // generate a simple xml element try { $feeds = new \SimpleXMLElement($content); $feeds = $feeds->channel->item; } catch (\Exception $e) { // silently fail error } } } return $this->renderResponse('SonataBlockBundle:Block:block_core_rss.html.twig', array('feeds' => $feeds, 'block' => $block, 'settings' => $settings), $response); }
/** * @param BlockInterface $block * @param array $attributes * @param string $prefix * * @return string */ public function renderAttributes(BlockInterface $block = null, $attributes = array(), $prefix = 'attr') { if ($attributes && is_string($attributes)) { $prefix = $attributes; $attributes = array(); } if ($block) { foreach ($block->getSettings() as $key => $value) { $key = trim(mb_strtolower($key, 'utf8')); $match = null; if (preg_match('@^' . preg_quote($prefix) . '-([a-z][-a-z0-9]*)$@', $key, $match)) { $attributes[$match[1]] = $value; } } } $attributesString = ''; foreach ($attributes as $key => $value) { $key = trim(mb_strtolower($key, 'utf8')); if (preg_match('@^[a-z][-a-z0-9]*$@', $key)) { $attributesString .= $key . '="' . addslashes($value) . '" '; } } return trim($attributesString); }
/** * @param BlockInterface $block * @param array $stats */ protected function stopTracing(BlockInterface $block, array $stats) { $e = $this->traces[$block->getId()]->stop(); $this->traces[$block->getId()] = array_merge($stats, array('duration' => $e->getDuration(), 'memory_end' => memory_get_usage(true), 'memory_peak' => memory_get_peak_usage(true))); $this->traces[$block->getId()]['cache']['lifetime'] = $this->traces[$block->getId()]['cache']['age'] + $this->traces[$block->getId()]['cache']['ttl']; }
/** * @param \Sonata\BlockBundle\Model\BlockInterface $block * @return \Sonata\CacheBundle\Cache\CacheInterface; */ protected function getCacheService(BlockInterface $block) { $type = isset($this->cacheBlocks[$block->getType()]) ? $this->cacheBlocks[$block->getType()] : false; if (!$type) { return false; } return $this->cacheManager->getCacheService($type); }
/** * {@inheritdoc} */ public function preUpdate(BlockInterface $block) { $block->setSetting('galleryId', is_object($block->getSetting('galleryId')) ? $block->getSetting('galleryId')->getId() : null); }
/** * @param OptionsResolverInterface $optionsResolver * @param BlockInterface $block */ protected function setDefaultSettings(OptionsResolverInterface $optionsResolver, BlockInterface $block) { // defaults for all blocks $optionsResolver->setDefaults(array('use_cache' => true, 'extra_cache_keys' => array(), 'attr' => array(), 'template' => false, 'ttl' => (int) $block->getTtl())); $optionsResolver->addAllowedTypes(array('use_cache' => array('bool'), 'extra_cache_keys' => array('array'), 'attr' => array('array'), 'ttl' => array('int'), 'template' => array('string', 'bool'))); // add type and class settings for block $class = ClassUtils::getClass($block); $settingsByType = isset($this->settingsByType[$block->getType()]) ? $this->settingsByType[$block->getType()] : array(); $settingsByClass = isset($this->settingsByClass[$class]) ? $this->settingsByClass[$class] : array(); $optionsResolver->setDefaults(array_merge($settingsByType, $settingsByClass)); }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); return $this->renderResponse('SonataAdminBundle:Block:block_admin_list.html.twig', array('block' => $block, 'settings' => $settings, 'admin_pool' => $this->pool), $response); }
/** * {@inheritdoc} */ public function load(BlockInterface $block) { if (is_numeric($block->getSetting('pageId', null))) { $cmsManager = $this->cmsManagerSelector->retrieve(); $site = $block->getPage()->getSite(); $block->setSetting('pageId', $cmsManager->getPage($site, $block->getSetting('pageId'))); } }
/** * {@inheritdoc} */ public function buildEditForm(FormMapper $formMapper, BlockInterface $block) { $formatChoices = $this->getFormatChoices($block->getSetting('mediaId')); $formMapper->add('settings', 'sonata_type_immutable_array', array('keys' => array(array('title', 'text', array('required' => false, 'label' => 'form.label_title')), array('content', 'textarea', array('required' => false, 'label' => 'form.label_content')), array('orientation', 'choice', array('required' => false, 'choices' => array('left' => 'form.label_orientation_left', 'right' => 'form.label_orientation_right'), 'label' => 'form.label_orientation')), array($this->getMediaBuilder($formMapper), null, array()), array('format', 'choice', array('required' => count($formatChoices) > 0, 'choices' => $formatChoices, 'label' => 'form.label_format'))), 'translation_domain' => 'SonataMediaBundle')); }
/** * {@inheritdoc} */ public function execute(BlockInterface $block, Response $response = null) { $settings = array_merge($this->getDefaultSettings(), $block->getSettings()); return $this->renderResponse('RadioSolutionMenuBundle:Block:block_core_menu.html.twig', array('block' => $block, 'settings' => $settings), $response); }
/** * @param BlockInterface $block */ private function resolveIds(BlockInterface $block) { $block->setSetting('collectionId', is_object($block->getSetting('collectionId')) ? $block->getSetting('collectionId')->getId() : null); }
/** * @todo: this function should be remove into a proper statefull object * * {@inheritdoc} */ public function validate(ErrorElement $errorElement, BlockInterface $block) { if (!$block->getId() && !$block->getType()) { return; } if ($this->inValidate) { return; } // As block can be nested, we only need to validate the main block, no the children try { $this->inValidate = true; $this->get($block)->validateBlock($errorElement, $block); $this->inValidate = false; } catch (\Exception $e) { $this->inValidate = false; } }
/** * Returns the exception filter for given block. * * @param BlockInterface $block * * @return FilterInterface * * @throws \RuntimeException */ public function getBlockFilter(BlockInterface $block) { $type = $block->getType(); $name = isset($this->blockFilters[$type]) ? $this->blockFilters[$type] : $this->defaultFilter; $service = $this->getFilterService($name); if (!$service instanceof FilterInterface) { throw new \RuntimeException(sprintf('The service "%s" is not an exception filter', $name)); } return $service; }
/** * {@inheritdoc} */ public function getCacheKeys(BlockInterface $block) { return array('block_id' => $block->getId(), 'updated_at' => $block->getUpdatedAt() ? $block->getUpdatedAt()->format('U') : strtotime('now')); }