protected function setUp()
 {
     $this->dispatcher = $this->getMock('\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $this->logger = $this->getMock('\\Psr\\Log\\LoggerInterface');
     DataLogger::init($this->logger);
     Dispatcher::setDispatcher($this->dispatcher);
 }
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Logger does not support the bar method.
  */
 public function testEventAborted()
 {
     $message = 'foo';
     $method = 'bar';
     $this->dataLogger->expects($this->never())->method($method);
     DataLogger::init($this->dataLogger);
     DataLogger::log($message, $method);
 }
 /**
  * Adds the block to the slot for the given language and page
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  *
  * @return string The saved content
  */
 public function add($sourceDir, array $options, $username)
 {
     $this->resolveAddOptions($options);
     $this->createContributorDir($sourceDir, $options, $username);
     $dir = $this->init($sourceDir, $options, $username)->getDirInUse();
     $blockName = $this->addBlockToSlot($dir, $options);
     $blockContent = $this->addBlock($dir, $options, $blockName);
     DataLogger::log(sprintf('Block "%s" has been added to the "%s" slot on page "%s" for the "%s_%s" language', $blockName, $options["slot"], $options["page"], $options["language"], $options["country"]));
     return $blockContent;
 }
 /**
  * Removes the block from the given slot
  *
  * @param $sourceDir
  * @param array $options
  * @param $username
  */
 public function remove($sourceDir, array $options, $username)
 {
     $dir = $this->init($sourceDir, $options, $username)->getDirInUse();
     $blockName = $options["blockname"];
     $blocksDir = $dir . '/blocks';
     $filename = sprintf('%s/%s.json', $blocksDir, $blockName);
     $options["block"] = JsonTools::jsonDecode(FilesystemTools::readFile($filename));
     Dispatcher::dispatch(BlockEvents::BLOCK_REMOVING, new BlockRemovingEvent($this->serializer, $filename));
     $this->filesystem->remove($filename);
     $this->removeBlockFromSlotFile($options, $dir);
     Dispatcher::dispatch(BlockEvents::BLOCK_REMOVED, new BlockRemovedEvent($this->serializer, $filename));
     DataLogger::log(sprintf('Block "%s" has been removed from the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 /**
  * Dispatches the event
  *
  * @param       $eventName
  * @param Event $event
  * @return Event
  */
 public static function dispatch($eventName, Event $event)
 {
     if (null === self::$dispatcher) {
         return $event;
     }
     self::$dispatcher->dispatch($eventName, $event);
     DataLogger::log(sprintf('The "%s" event was dispatched', $eventName));
     if ($event->getAbort()) {
         DataLogger::log(sprintf('The "%s" event was aborted', $eventName), DataLogger::ERROR);
         throw new EventAbortedException($event->getAbortMessage());
     }
     return $event;
 }
 /**
  * Edits the given block
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  * @param array $values
  */
 public function edit($sourceDir, array $options, $username, $values)
 {
     $this->resolveOptions($options);
     $this->init($sourceDir, $options, $username);
     $this->createContributorDir($sourceDir, $options, $username);
     $filename = sprintf('%s/blocks/%s.json', $this->getDirInUse(), $options["blockname"]);
     $currentBlock = $options["baseBlock"] = JsonTools::jsonDecode(FilesystemTools::readFile($filename));
     $values = $this->parseChildren($values);
     $block = JsonTools::join($currentBlock, $values);
     $encodedBlock = json_encode($block);
     $blockClass = BlockFactory::getBlockClass($block["type"]);
     $event = Dispatcher::dispatch(BlockEvents::BLOCK_EDITING, new BlockEditingEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     $blockContent = $event->getFileContent();
     FilesystemTools::writeFile($filename, $blockContent);
     Dispatcher::dispatch(BlockEvents::BLOCK_EDITED, new BlockEditedEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     DataLogger::log(sprintf('Block "%s" has been edited on the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 /**
  * Approves the removal of the given contribution
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  */
 public function approveRemoval($sourceDir, array $options, $username)
 {
     $this->init($sourceDir, $options, $username);
     $targetFilename = sprintf('%s/blocks/%s.json', $this->productionDir, $options['blockname']);
     if (!file_exists($targetFilename)) {
         // @codeCoverageIgnoreStart
         return;
         // @codeCoverageIgnoreEnd
     }
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVING_REMOVAL, new BlockApprovingRemovalEvent($this->serializer, $targetFilename));
     $this->filesystem->remove($targetFilename);
     $slotDefinition = $this->getSlotDefinition($this->productionDir);
     $blocks = $slotDefinition["blocks"];
     $key = array_search($options['blockname'], $blocks);
     unset($blocks[$key]);
     $slotDefinition["blocks"] = $blocks;
     $this->saveSlotDefinition($this->productionDir, $slotDefinition, $username);
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVED_REMOVAL, new BlockApprovedRemovalEvent($this->serializer, $targetFilename));
     DataLogger::log(sprintf('Block "%s" has been approved for removal on the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 public function restore($sourceDir, array $options, $username, $restoringBlockName)
 {
     $this->createContributorDir($sourceDir, $options, $username);
     $historyFileName = sprintf('%s/%s/history.json', $this->getArchiveDir(), $options["blockname"]);
     $history = json_decode(file_get_contents($historyFileName), true);
     // This happens when a user confirms a restoration then returns back and confirms again.
     // In this case there's nothing to restore.
     if (!array_key_exists($restoringBlockName, $history)) {
         return;
     }
     $restoringBlock = $history[$restoringBlockName];
     $filename = sprintf('%s/blocks/%s.json', $this->contributorDir, $options["blockname"]);
     $currentBlock = file_get_contents($filename);
     Dispatcher::dispatch(BlockEvents::BLOCK_RESTORING, new BlockRestoringEvent($this->serializer, $filename, $restoringBlock));
     file_put_contents($filename, json_encode($restoringBlock));
     unset($history[$restoringBlockName]);
     $now = date("Y-m-d-H.i.s");
     $history[$now] = json_decode($currentBlock, true);
     file_put_contents($historyFileName, json_encode($history));
     Dispatcher::dispatch(BlockEvents::BLOCK_RESTORED, new BlockRestoredEvent($this->serializer, $filename, $restoringBlock));
     DataLogger::log(sprintf('Block "%s" has been restored as "%s" on the slot "%s" on "%s" page for "%s_%s" language', $restoringBlockName, $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 private function boot()
 {
     BlockFactory::boot($this->app["red_kite_cms.configuration_handler"]);
     Dispatcher::setDispatcher($this->app["dispatcher"]);
     DataLogger::init($this->app["monolog"]);
     Translator::setTranslator($this->app["translator"]);
     $this->app["red_kite_cms.plugin_manager"]->boot();
     $theme = $this->app["red_kite_cms.plugin_manager"]->getActiveTheme();
     $this->app["red_kite_cms.theme"]->boot($theme);
     $this->app["red_kite_cms.theme_generator"]->boot($theme);
     $this->app["red_kite_cms.slots_generator"]->boot($theme);
     $this->app["red_kite_cms.theme_aligner"]->boot($theme);
     $siteIncompleteFile = $this->app["red_kite_cms.root_dir"] . '/app/data/' . $this->siteName . '/incomplete.json';
     if (file_exists($siteIncompleteFile)) {
         $this->createWebsitePages($theme);
         unlink($siteIncompleteFile);
     }
     $this->app["dispatcher"]->dispatch(CmsEvents::CMS_BOOTING, new CmsBootingEvent($this->app["red_kite_cms.configuration_handler"]));
     $this->app["red_kite_cms.template_assets"]->boot();
     $this->app["red_kite_cms.assetic"]->addFilter('cssrewrite', new CssRewriteFilter());
 }
 /**
  * Sets the json file contents
  * @param $fileContent
  *
  * @return $this
  */
 public function setFileContent($fileContent)
 {
     // Accapts only a json value
     if (null === json_decode($fileContent)) {
         DataLogger::log(sprintf('Event "%s" discharged the "%s" content because it is not a valid json', get_class($this), $fileContent), DataLogger::WARNING);
         return $this;
     }
     $this->fileContent = $fileContent;
     return $this;
 }
 private function render404page(GetResponseForExceptionEvent $event, $message)
 {
     $template = 'RedKiteCms/Resources/views/Frontend/404.html.twig';
     $content = $this->twig->render($template, array("message" => $message));
     $this->setUpResponse($event, $content);
     DataLogger::log($message, DataLogger::ERROR);
 }
 private function moveBlockToSameSlot($baseDir, array $options, $username)
 {
     $sourceDir = $this->init($baseDir, $options, $username)->getDirInUse();
     $slotsFilename = sprintf('%s/slot.json', $sourceDir);
     $slot = JsonTools::jsonDecode(FilesystemTools::readFile($slotsFilename), true);
     $blocks = $slot["blocks"];
     $key = array_search($options["blockname"], $blocks);
     $blockName = $blocks[$key];
     unset($blocks[$key]);
     array_splice($blocks, $options["position"], 0, $blockName);
     $slot["blocks"] = $blocks;
     $encodedSlot = json_encode($slot);
     $targetFile = sprintf('%s/blocks/%s.json', $sourceDir, $options["blockname"]);
     $event = Dispatcher::dispatch(BlockEvents::BLOCK_MOVING_SAME_SLOT, new BlockMovingSameSlotEvent($this->serializer, $blocks, $options["position"], $targetFile, $encodedSlot));
     $slotContent = $event->getFileContent();
     FilesystemTools::writeFile($slotsFilename, $slotContent);
     $block = FilesystemTools::readFile($targetFile);
     Dispatcher::dispatch(BlockEvents::BLOCK_MOVED_SAME_SLOT, new BlockMovedSameSlotEvent($this->serializer, $blocks, $options["position"], $targetFile, $encodedSlot));
     DataLogger::log(sprintf('Block "%s" has been moved to position "%s" on the slot "%s" on "%s" page for "%s_%s" language', $options["blockname"], $options["position"], $options["slot"], $options["page"], $options["language"], $options["country"]));
     return $block;
 }
 /**
  * Hides the current seo
  * @param $pageName
  * @param $languageName
  */
 public function hide($pageName, $languageName)
 {
     $this->contributorDefined();
     $baseDir = $this->pagesDir . '/' . $pageName . '/' . $languageName;
     $sourceFile = $baseDir . '/seo.json';
     Dispatcher::dispatch(PageEvents::PAGE_HIDING, new PageHidingEvent());
     unlink($sourceFile);
     Dispatcher::dispatch(PageEvents::PAGE_HID, new PageHidEvent());
     DataLogger::log(sprintf('Page "%s" for language "%s" was hidden from production', $pageName, $languageName));
 }
 /**
  * Removes the given page
  *
  * @param $pageName
  */
 public function remove($pageName)
 {
     if ($pageName == $this->configurationHandler->homepage()) {
         throw new RuntimeException("exception_homepage_cannot_be_removed");
     }
     $pageDir = $this->pagesDir . '/' . $pageName;
     Dispatcher::dispatch(PageCollectionEvents::PAGE_COLLECTION_REMOVING, new PageCollectionRemovingEvent($this->username, $pageDir));
     $filesystem = new Filesystem();
     if (file_exists($pageDir . '/page.json')) {
         $filesystem->mirror($pageDir, $this->pagesRemovedDir . '/' . $pageName . "-" . date("Y-m-d-H.i.s"));
     }
     $filesystem->remove($pageDir);
     Dispatcher::dispatch(PageCollectionEvents::PAGE_COLLECTION_REMOVED, new PageCollectionRemovedEvent($this->username, $pageDir));
     DataLogger::log(sprintf('Page "%s" was successfully removed from website', $pageName));
 }
 /**
  * Save the all website pages
  *
  * @param \RedKiteCms\Content\BlockManager\BlockManagerApprover $approver
  * @param array $languages
  * @param bool $saveCommonSlots Saves the common slots when true
  */
 public function saveAllPages(BlockManagerApprover $approver, array $languages, $saveCommonSlots = true)
 {
     $this->contributorDefined();
     $finder = new Finder();
     $pages = $finder->directories()->depth(0)->in($this->pagesDir);
     foreach ($pages as $page) {
         $page = (string) $page;
         $pageName = basename($page);
         foreach ($languages as $language) {
             $tokens = explode("_", $language);
             $options = array('page' => $pageName, 'language' => $tokens[0], 'country' => $tokens[1]);
             $this->save($approver, $options, $saveCommonSlots);
         }
         $saveCommonSlots = false;
     }
     Dispatcher::dispatch(PageCollectionEvents::SITE_SAVED, new SiteSavedEvent());
     DataLogger::log('The whole website\'s pages were successfully saved in production');
 }