Esempio n. 1
0
/**
 * Primary method for a block of user-customizable content inside a template.
 *
 * Insertables are the core method of injecting blocks of user-customizable content into a template.
 *
 * An insertable must be on a template that has a registered page URL, as the baseurl is what is tracked as one of the main primary keys.
 * The other PK is the insertable's name, which must be unique on that one template.
 *
 * #### Smarty Parameters
 *
 *  * name
 *    * The key name of this input value, must be present and unique on this template.
 *  * assign
 *    * Assign the value instead of outputting to the screen.
 *  * title
 *    * When editing the insertable, the title displayed along side the input field.
 *  * type
 *
 * #### Example Usage
 *
 * <pre>
 * {insertable name="body" title="Body Content"}
 * <p>
 * This is some example content!
 * </p>
 * {/insertable}
 * </pre>
 *
 * <pre>
 * {insertable name="img1" title="Large Image" assign="img1"}
 * {img src="`$img1`" placeholder="generic" dimensions="800x400"}
 * {/insertable}
 * </pre>
 *
 * @param array       $params  Associative (and/or indexed) array of smarty parameters passed in from the template
 * @param string|null $content Null on opening pass, rendered source of the contents inside the block on closing pass
 * @param Smarty      $smarty  Parent Smarty template object
 * @param boolean     $repeat  True at the first call of the block-function (the opening tag) and
 * false on all subsequent calls to the block function (the block's closing tag).
 * Each time the function implementation returns with $repeat being TRUE,
 * the contents between {func}...{/func} are evaluated and the function implementation
 * is called again with the new block contents in the parameter $content.
 *
 * @return string
 */
function smarty_block_insertable($params, $content, $smarty, &$repeat){

	$assign = (isset($params['assign']))? $params['assign'] : false;

	// This only needs to be called once.
	// If a value is being assigned, then it's on the first pass so the value will be assigned by the time the content is hit.
	if($assign){
		if($repeat){
			// Running the first time with an assign variable, OK!
		}
		else{
			return $content;
		}
	}
	else{
		// No assign requested, run on the second only.
		if($repeat){
			return '';
		}
		else{
			// Continue!
		}
	}

	$page = PageRequest::GetSystemRequest()->getPageModel();

	// I need to use the parent to lookup the current base url.
	$baseurl = PageRequest::GetSystemRequest()->getBaseURL();

	if(!isset($params['name'])) return '';

	$i = InsertableModel::Construct($page->get('site'), $baseurl, $params['name']);

	if($i->exists()){
		$value = $i->get('value');
	}
	else{
		$value = $content;
	}

	if(isset($params['type']) && $params['type'] == 'markdown'){
		// Convert this markdown code to HTML via the built-in Michielf library.
		$value = Core\MarkdownProcessor::defaultTransform($value);
		//$value = Michelf\MarkdownExtra::defaultTransform($value);
	}
	else{
		// Coreify the string
		$value = \Core\parse_html($value);
	}

	if($assign){
		$smarty->assign($assign, $value);
	}
	else{
		return $value;
	}
}
 /**
  * Hook to check for any new files in the system and register pages (or deregister them as necessary).
  * 
  * Only runs if the config option is enabled to do so.
  */
 public static function _AutoRegisterFiles()
 {
     if (!\ConfigHandler::Get('/markdownbrowser/autoregister')) {
         echo 'Skipping autoregistration of markdown files, configuration option for this feature is disabled.' . "\n";
         return true;
     }
     $dir = \ConfigHandler::Get('/markdownbrowser/basedir');
     $markdownFiles = [];
     if (!$dir) {
         echo 'Skipping autoregistration of markdown files, no markdown directory configured.' . "\n";
         return true;
     }
     // Make sure it's readable!
     $dir = \Core\Filestore\Factory::Directory($dir);
     $dirbase = $dir->getPath();
     $dirlen = strlen($dirbase);
     if (!$dir->exists()) {
         echo 'Skipping autoregistration of markdown files, ' . $dir->getPath() . ' does not exist.' . "\n";
         return true;
     }
     $all = [];
     $files = $dir->ls('md', true);
     foreach ($files as $file) {
         /** @var \Core\Filestore\File $file */
         $fileBase = substr($file->getFilename(), $dirlen);
         if (strpos($fileBase, 'index.md') !== false) {
             $fileRel = substr($fileBase, 0, -8);
         } else {
             $fileRel = substr($fileBase, 0, -3);
         }
         if (preg_match('/[A-Z]/', $fileRel) !== 0) {
             $warning = t('STRING_MARKDOWNBROWSER_WARNING_FILE_HAS_CAPITALS');
         } elseif (strpos($fileRel, ' ')) {
             $warning = t('STRING_MARKDOWNBROWSER_WARNING_FILE_HAS_SPACES');
         } else {
             $warning = '';
         }
         if ($warning == '') {
             $url = '/markdownbrowser/view/' . $fileRel;
             $all[$url] = ['file' => $file, 'page' => null];
         } else {
             echo $warning . ' - ' . $fileRel . "\n";
         }
     }
     // Now that the files are loaded into memory, load any page that may already exist.
     // This will be used to ignore entries that already have a page, to create ones without,
     // and to remove pages that no longer have a corresponding file.
     $pages = PageModel::Find(['baseurl LIKE /markdownbrowser/view%']);
     foreach ($pages as $p) {
         $url = $p->get('baseurl');
         if (!isset($all[$url])) {
             $all[$url] = ['file' => null, 'page' => null];
         }
         $all[$url]['page'] = $p;
     }
     // Now $all contains everything I need to process on! :)
     foreach ($all as $url => &$dat) {
         /** @var PageModel|null $page */
         $page = $dat['page'];
         /** @var \Core\Filestore\File|null $file */
         $file = $dat['file'];
         if ($page && !$file) {
             // There is a page but no file, DELETE!
             $page->delete();
             echo 'Deleted page for non-existent file: ' . $url . "\n";
         } elseif (!$page && $file) {
             // There is a file but no page, create.
             $contents = $file->getContents();
             // Convert these contents from markdown to HTML.
             $processor = new \Core\MarkdownProcessor();
             $html = $processor->transform($contents);
             // Pre-populate this page with information from the rendered markdown document.
             // If this page exists, then it'll be updated and kept in sync.
             // Else, it'll still be set with what's in the document and kept in sync.
             $page = PageModel::Construct($url);
             $page->set('title', $processor->getMeta('title'));
             $page->set('body', $html);
             $page->set('baseurl', $url);
             $page->set('rewriteurl', $url);
             $page->set('editurl', str_replace('/markdownbrowser/view', '/markdownbrowser/update', $url));
             $page->set('component', 'markdown-browser');
             $page->set('selectable', 1);
             $page->set('published', $file->getMTime());
             $page->set('updated', $file->getMTime());
             $page->set('created', $file->getMTime());
             $page->save();
             echo 'Created page for new file: ' . $url . "\n";
         }
     }
     return true;
 }