/**
  * 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"]));
 }
 /**
  * Approves a contribution
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  *
  * @return array The approved block
  */
 public function approve($sourceDir, array $options, $username)
 {
     $this->init($sourceDir, $options, $username);
     $sourceFilename = sprintf('%s/blocks/%s.json', $this->contributorDir, $options['blockname']);
     $targetFilename = sprintf('%s/blocks/%s.json', $this->productionDir, $options['blockname']);
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVING, new BlockApprovingEvent($this->serializer, $sourceFilename, $targetFilename));
     $blockValues = JsonTools::jsonDecode(FilesystemTools::readFile($sourceFilename));
     $blockValues["history"] = array();
     FilesystemTools::writeFile($targetFilename, json_encode($blockValues));
     $slotDefinitionContribution = $this->getSlotDefinition($this->getContributorDir());
     $this->saveSlotDefinition($this->productionDir, $slotDefinitionContribution);
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVED, new BlockApprovedEvent($this->serializer, $sourceFilename, $targetFilename));
     DataLogger::log(sprintf('Block "%s" has been approved on the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
     return $blockValues;
 }
 private function doParseChildren(array $children)
 {
     $parsedChildren = array();
     foreach ($children as $child) {
         if (!array_key_exists("type", $child)) {
             // @codeCoverageIgnoreStart
             continue;
             // @codeCoverageIgnoreEnd
         }
         $block = BlockFactory::createBlock($child["type"]);
         $encodedBlock = $this->serializer->serialize($block, 'json');
         $updatedBlock = JsonTools::join($encodedBlock, $child);
         $block = $this->serializer->deserialize(json_encode($updatedBlock), get_class($block), 'json');
         $block->updateSource();
         $children = json_decode($this->serializer->serialize($block, 'json'), true);
         if (array_key_exists("children", $children)) {
             $children["children"] = $this->doParseChildren($children["children"]);
         }
         $parsedChildren[] = $children;
     }
     return $parsedChildren;
 }
 public function fetchArchivedBlocks($archiveDir, $slotName)
 {
     if (!is_dir($archiveDir)) {
         return array();
     }
     $found = array();
     $blockName = basename($archiveDir);
     // Backward compatibility
     $this->updateHistory($archiveDir);
     $historyFile = $archiveDir . '/history.json';
     $history = json_decode(file_get_contents($historyFile), true);
     foreach ($history as $name => $block) {
         $block = JsonTools::toBlock($this->serializer, json_encode($block));
         if (null === $block) {
             continue;
         }
         $block->setName($blockName);
         $block->setSlotName($slotName);
         $block->setHistoryName($name);
         $found[$name] = $block;
     }
     krsort($found);
     $result = array_values($found);
     return $result;
 }
 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;
 }
 /**
  * Renders the slots
  * @param \RedKiteCms\FilesystemEntity\Page $page
  *
  * @return array
  */
 protected function renderSlots(Page $page)
 {
     $templateRenderer = $this->options['page_renderer'];
     // We need to render all blocks to avoid problems when a kind ok block is
     // not present on a page
     $availableBlocks = array();
     $blocks = BlockFactory::createAllBlocks();
     foreach ($blocks as $block) {
         if ($block->isInternal()) {
             continue;
         }
         $availableBlocks[$block->getType()] = JsonTools::toJson($this->options["serializer"], $block);
     }
     $templateRenderer->render($page, array('available_blocks' => $availableBlocks));
     $slots = $templateRenderer->getSlots();
     $cmsBlocks = $templateRenderer->renderCmsBlocks($blocks, $this->options["username"], array('available_blocks' => $availableBlocks));
     return array('slots' => $slots, 'cms_blocks' => $cmsBlocks, 'available_blocks' => $availableBlocks);
 }