/** * 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; }
/** * 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"])); }
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; }