Commit 2a7698ac authored by Penny Leach's avatar Penny Leach
Browse files

new template parser stuff

parent e385dbee
......@@ -148,6 +148,10 @@ class ArtefactTypeBlog extends ArtefactType {
public static function get_render_list() {
}
public static function is_0_or_1() {
return false;
}
public static function collapse_config() {
}
......@@ -224,6 +228,10 @@ class ArtefactTypeBlogPost extends ArtefactType {
public static function get_render_list() {
}
public static function is_0_or_1() {
return false;
}
public static function collapse_config() {
}
......
......@@ -81,6 +81,10 @@ class ArtefactTypeFileBase extends ArtefactType {
return array(FORMAT_ARTEFACT_LISTSELF);
}
public static function is_0_or_1() {
return false;
}
public function get_icon() {
}
......
......@@ -119,6 +119,10 @@ class ArtefactTypeProfile extends ArtefactType {
public static function get_render_list() {
return array(FORMAT_ARTEFACT_LISTSELF, FORMAT_ARTEFACT_RENDERFULL, FORMAT_ARTEFACT_RENDERMETADATA);
}
public static function is_0_or_1() {
return true;
}
public static function get_all_fields() {
return array(
......
......@@ -430,6 +430,12 @@ abstract class ArtefactType {
*/
public static abstract function get_render_list();
/**
* whether a user will have exactly 0 or 1 of this artefact type
* @abstract
*/
public static abstract function is_0_or_1();
// ******************** HELPER FUNCTIONS ******************** //
protected function get_artefact_type() {
......
......@@ -78,46 +78,98 @@ function template_parse_block($blockstr) {
$keyvalue = explode('=', $b);
$data[$keyvalue[0]] = substr($keyvalue[1], 1, -1);
}
if (!isset($data['id']) || empty($data['id']) || strpos($data['id'], 'tpl_') !== 0) {
throw new InvalidArgumentException("Invalid block section $blockstr - must have an id beginning with tpl_");
}
if (!isset($data['type']) || empty($data['type'])) {
throw new InvalidArgumentException("Invalid block section $blockstr - must have a type");
}
if (!isset($data['id']) || empty($data['id'])) {
throw new InvalidArgumentException("Invalid block $blockstr. Must have id");
$types = array('artefact', 'label', 'title', 'author', 'description');
if (!in_array($data['type'], $types)){
throw new InvalidArgumentException("Invalid block section $blockstr (type " . $data['type']
. " not one of " . implode(', ', $types));
}
if (!isset($data['tagtype'])) {
$data['tagtype'] = 'div';
}
// everything else can theoretically be optional....
template_validate_block($data, '');
template_validate_block($data, 'default');
if ($data['type'] != 'artefact') {
// no more validation to do.
return $data;
}
return $data;
}
if (isset($data['artefacttype'])) {
if (!$plugin = get_field('artefact_installed_type', 'plugin', 'name', $data['artefacttype'])) {
throw new InvalidArgumentException("artefacttype " . $data['artefacttype'] . " is not installed");
}
if (isset($data['format'])) { // check the artefacttype can render to this format.
safe_require('artefact', $plugin);
function template_validate_block(&$data, $name='') {
$type &= (isset($data[$name . 'type']) ? $data[$name . 'type'] : '');
$format &= (isset($data[$name . 'format']) ? $data[$name . 'format'] : '');
if ((empty($format) && empty($type)) || $format == 'label') { // labels are special cases
return true;
if (!artefact_can_render_to($data['artefacttype'], $data['format'])) {
throw new InvalidArgumentException("Artefacttype " . $data['artefacttype'] . " can't render to format "
. $format['format']);
}
}
}
// if we've got type but no format and we're looking at defaults, use main format.
if (!empty($type) && empty($format) && $name == 'default' && !empty($data['format'])) {
$format = $data['format'];
if (isset($data['plugintype'])) {
try {
safe_require('artefact', $data['plugintype']);
}
catch (Exception $e) {
throw new InvalidArgumentException("Couldn't find plugin type " . $data['plugintype']);
}
}
if (isset($data['defaultartefacttype'])) {
if (isset($data['artefacttype']) && $data['artefacttype'] != $data['defaultartefacttype']) {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
. " doesn't make sense given artefact type " . $data['artefacttype']);
}
else if (isset($data['plugintype'])
&& !in_array($data['defaultartefacttype'],
call_static_method(generate_class_name($data['plugintype']), 'get_artefact_types'))) {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
." doesn't make sense given plugin type " . $data['plugintype']);
}
if (!$plugin = get_field('artefact_installed_type', 'plugin', 'name', $data['defaultartefacttype'])) {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
. " is not installed");
}
// look for a default format...
if (!isset($data['defaultformat'])) {
if (isset($data['format'])) {
$data['defaultformat'] = $data['format'];
}
else {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
." specified but with no format method (couldn't find in either "
." default format, or fallback format field");
}
}
// check the default artefact type can render to the given default format
safe_require('artefact', $plugin);
if (!artefact_can_render_to($data['defaultartefacttype'], $data['defaultformat'])) {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
. " can't render to defaultformat " . $format['defaultformat']);
}
// figure out what plugin handles this type and validate the class exists.
if (!$plugin = get_field('artefact_installed_type', 'plugin', 'name', $type)) {
throw new InvalidArgumentException("{$name}type $type is not installed");
// check this default artefact is a 0 or 1 artefact
if (!call_static_method(generate_artefact_class_name($data['defaultartefacttype']), 'is_0_or_1')) {
throw new InvalidArgumentException("Default artefact type " . $data['defaultartefacttype']
." is not a 0 or 1 type artefact");
}
}
require_once('artefact.php');
safe_require('artefact', $plugin);
if (!artefact_can_render_to($type, $format)) {
throw new InvalidArgumentException("{$name}type $type can't render to {$name}format $format");
}
// @todo resizing stuff maybe
// @todo validate resizing stuff
return $data;
}
function template_locate($templatename, $fetchdb=true) {
......
<div class="header">{{block format="label" id="title"}}</div>
<div class="header">{{block type="title" id="tpl_title"}}<br />{{block type="author" id="tpl_author"}}</div>
<table>
<tr>
<td align="left" width="400">{{block id="block1" width="300"}}</td>
<td align="left" width="400">{{block id="block2" width="300"}}</td>
<td align="left" width="400">{{block type="artefact" id="tpl_block1" width="300"}}</td>
<td align="left" width="400">{{block type="artefact" id="tpl_block2" width="300"}}</td>
</tr>
<tr>
<td align="right" width="150">{{block type="image" width="150" height="150" format="renderfull" id="block3"}}</td>
<td align="left" width="400">{{block id="block4" width="300"}}</td>
<td align="right" width="150">{{block type="artefact" artefacttype="image" width="150" height="150" format="renderfull" id="tpl_block3"}}</td>
<td align="left" width="400">{{block type="artefact" id="tpl_block4" width="300"}}</td>
</tr>
<tr>
<td align="left" width="400">{{block id="block5" width="300" defaulttype="introduction" defaultformat="renderfull"}}</td>
<td align="left" width="400">{{block id="block6" width="300"}}</td>
<td align="left" width="400">{{block type="artefact" id="tpl_block5" width="300" defaultartefacttype="introduction" defaultformat="renderfull"}}</td>
<td align="left" width="400">{{block type="artefact" id="tpl_block6" width="300"}}</td>
</tr>
</table>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment