private function loadBlock($pageDir, $block, $salicName) { $blockType = BlockSettings::data2($block['type']); if (!$blockType->subblocks) { // default content: '<blockkey>' $file = $pageDir . $salicName . "_" . $this->current_lang . self::dataFileExtension; $content = is_file($file) ? file_get_contents($file) : '<p><i>< ' . $block['key'] . ' ></i></p>'; //TODO: ?block id as default content } else { // subblock = each file, eg. main_blockname_en/left.html.twig, gets loaded into $content['left'] $dir = $pageDir . $salicName . '/'; $content = array(); if (is_dir($dir)) { if ($blockType->subblocks === true) { // -> variable subblocks $subblocks = range(1, $block['subblock-count']); //TODO: backend solution or better solution } else { // predefined subblocks $subblocks = $blockType->subblocks; } foreach ($subblocks as $subblock) { $filename = $dir . $subblock . "_" . $this->current_lang . self::dataFileExtension; $subcontent = is_file($filename) ? file_get_contents($filename) : '<p><i>< ' . $block['key'] . '.' . $subblock . ' ></i></p>'; $content[$subblock] = $subcontent; } } else { if ($blockType->subblocks === true) { // -> variable subblocks, initialize empty elements foreach (range(1, $block['subblock-count']) as $subblock) { $subcontent = '<p><i>< ' . $block['key'] . '.' . $subblock . ' ></i></p>'; $content[$subblock] = $subcontent; } } } } $vars = $blockType->vars; foreach ($block['vars'] as $var => $val) { $vars[$var] = $val; } $data = array('baseurl' => $this->baseUrl, 'baseurl_international' => $this->baseUrlInternational, 'debug_mode' => GeneralSettings::get()->debugMode, 'content' => $content, 'vars' => $vars); if ($blockType->editable) { $data['salic_name'] = $salicName; } // only add salic name if editable return $this->twig->render('blocks/' . $block['type'] . self::templateExtension, $data); }
public function validate() { // check if template exists $templateSettings = TemplateSettings::get(); if (!$templateSettings->exists($this->template)) { throw new SalicSettingsException("Template '" . $this->template . "' not found", $this->file, $templateSettings->templates); } // create empty areas that are not present $myTemplate = $templateSettings->data($this->template); $templateAreas = $myTemplate->areas; foreach ($templateAreas as $area) { if (!array_key_exists($area, $this->areas)) { $this->areas[$area] = []; } } // check blocks list foreach ($this->areas as $areaKey => &$blocks) { // check if area exists in template if (!in_array($areaKey, $templateAreas)) { throw new SalicSettingsException("Area '{$areaKey}' doesn't exist in the template", $this->file . self::fis . 'areas'); } $blockKeys = array(); // for duplicate checking foreach ($blocks as $i => &$block) { // check all blocks $block['key'] = self::getString('key', $block, null, "areas>{$areaKey}>{$i}"); // use index, blockKey is not known yet // check for duplicate keys $blockKey = $block['key']; if (in_array($blockKey, $blockKeys)) { throw new SalicSettingsException("Duplicate blockKey '{$blockKey}'", $this->file . self::fis . "areas>{$areaKey}"); } $blockKeys[] = $blockKey; $block['type'] = self::getString('type', $block, null, "areas>{$areaKey}>{$blockKey}"); // eg. 'templates.json:areas>main>intro' if (!BlockSettings::exists2($block['type'])) { throw new SalicSettingsException("Block '{$block['type']}' is not defined in blocks.json", $this->file . self::fis . "areas>{$areaKey}>{$blockKey}", BlockSettings::get()->blocktypes); } $blockSettings = BlockSettings::data2($block['type']); if ($blockSettings->subblocks === true) { // if the blocktype has variable subblocks, check if the count is given $block['subblock-count'] = self::getInt('subblock-count', $block, 1, "areas>{$areaKey}>{$blockKey}"); } // set to parsed value, TODO: move stuff like this to parseFromJson() $block['vars'] = self::getDict('vars', $block, [], "areas>{$areaKey}>{$blockKey}"); } } // check variables list $templateVars = $myTemplate->variables; foreach ($this->variables as $var => $value) { // check if variable exists in template if (!array_key_exists($var, $templateVars)) { throw new SalicSettingsException("Variable '{$var}' doesn't exist in the template", $this->file . self::fis . 'variables'); } //TODO: variable value type checking } }
public function doSavePage($pagekey, array $regions) { foreach ($regions as $key => $val) { $page_dir = "site/data/{$pagekey}/"; if (!is_dir($page_dir)) { if (!mkdir($page_dir, 0750, true)) { // rwxr-x---, TODO: configurable directory permissions throw new \Exception("Failed to create directory '{$page_dir}'"); } } $pageSettings = PageSettings::get($pagekey); $blockSettings = BlockSettings::get(); $templateSettings = TemplateSettings::data2($pageSettings->template); // possible key formats: // - '_field' // - 'area_block' // - 'area_block.subblock' if ($key[0] === '_') { // we have a field if (preg_match('/^_([a-z0-9-]+)$/', $key, $matches) !== 1) { // matches 'area-name_block-name' throw new SalicApiException("Invalid field-key format: '{$key}'"); } $fieldKey = $matches[1]; if ($fieldKey && !in_array($fieldKey, $templateSettings->fields)) { throw new SalicApiException("Invalid field: '{$fieldKey}'", $templateSettings->fields); } // save as '_field-key_lang.ext' $filename = '_' . $fieldKey . '_' . $this->current_lang . self::dataFileExtension; } else { // we have a block if (preg_match('/^([a-z0-9-]+)_([a-z0-9-]+)(?:\\.([a-z0-9-]+))?$/', $key, $matches) !== 1) { // matches 'area-name_block-name' or 'area_block.sub-block' throw new SalicApiException("Invalid block-key format: '{$key}'"); } $areaKey = $matches[1]; $blockKey = $matches[2]; $subblock = @$matches[3]; // subblock is optional if (!array_key_exists($areaKey, $pageSettings->areas)) { throw new SalicApiException("Invalid area: '{$areaKey}'", $pageSettings->areas); } if (($myBlock = PageSettings::getBlock($pageSettings->areas[$areaKey], $blockKey)) == null) { throw new SalicApiException("Invalid block: '{$blockKey}'", $pageSettings->areas[$areaKey]); } $blockType = $blockSettings->data($myBlock['type']); if ($subblock) { if ($blockType->subblocks === true ? !is_numeric($subblock) : !in_array($subblock, $blockType->subblocks)) { throw new SalicApiException("Invalid subblock: '{$subblock}'[type={$myBlock['type']}]", $blockType->subblocks); } } if (!$blockType->editable) { throw new SalicApiException("Block not editable: '{$blockKey}'[type={$myBlock['type']}]"); } // save as 'area_block/subblock_lang.ext' if (!$subblock) { $filename = $areaKey . '_' . $blockKey . '_' . $this->current_lang . self::dataFileExtension; } else { Utils::mkdirs($page_dir . $areaKey . '_' . $blockKey); $filename = $areaKey . '_' . $blockKey . '/' . $subblock . '_' . $this->current_lang . self::dataFileExtension; } } $flag = file_put_contents($page_dir . $filename, $val, LOCK_EX); // lock the file exclusively while writing if ($flag === false) { throw new \Exception("Failed to write file '{$page_dir}{$filename}" . self::dataFileExtension . "'"); } //TODO: set file permissions } }