Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created September 6, 2011 08:27
Show Gist options
  • Save larscwallin/1196951 to your computer and use it in GitHub Desktop.
Save larscwallin/1196951 to your computer and use it in GitHub Desktop.
XPDO to ExtJs Model JSON
{"model":{
"modx":{}
,"modChunk":{
"class": "modChunk",
"extends":"modElement",
"fields": [
{
"name": "id",
"type": "int"
}
,{
"name": "name",
"type": "string",
"defaultValue":""
}
,{
"name": "description",
"type": "string",
"defaultValue":"Chunk"
}
,{
"name": "editor_type",
"type": "int",
"defaultValue":"0"
}
,{
"name": "category",
"type": "int",
"defaultValue":"0"
}
,{
"name": "cache_type",
"type": "int",
"defaultValue":"0"
}
,{
"name": "snippet",
"type": "string",
"defaultValue":""
}
,{
"name": "locked",
"type": "boolean",
"defaultValue":"0"
}
],
"associations": [
{
"type": "belongsTo",
"model": "modCategory",
"primaryKey": "category",
"foreignKey": "id",
"associationKey": "category"
}
]
}
,"modCategory":{
"class": "modCategory",
"extends":"",
"fields": [
{
"name": "id",
"type": "int"
}
,{
"name": "parent",
"type": "int",
"defaultValue":"0"
}
,{
"name": "category",
"type": "string",
"defaultValue":""
}
],
"associations": []
}
,"modSnippet":{
"class": "modSnippet",
"extends":"modScript",
"fields": [
{
"name": "id",
"type": "int"
}
,{
"name": "cache_type",
"type": "int",
"defaultValue":"0"
}
,{
"name": "snippet",
"type": "string",
"defaultValue":""
}
,{
"name": "locked",
"type": "boolean",
"defaultValue":"0"
}
,{
"name": "moduleguid",
"type": "string",
"defaultValue":""
}
],
"associations": []
}
}
}
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" />
<xsl:output method="text" />
<xsl:output indent="yes" />
<xsl:output media-type="text/plain" />
<xsl:param name="directory"/>
<xsl:param name="label-composite">Composites</xsl:param>
<xsl:param name="label-aggregate">Aggregates</xsl:param>
<xsl:param name="xpdo-model"></xsl:param>
<xsl:param name="include-objects">modSnippet modChunk</xsl:param>
<xsl:param name="include-dependencies">1</xsl:param>
<xsl:param name="include-local-only">0</xsl:param>
<xsl:param name="ident">
<xsl:text> </xsl:text>
</xsl:param>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="model">
{"model":{
"modx":{}
<xsl:for-each select="./object">
<xsl:if test="contains($include-objects,@class) or $include-objects='*'">
<xsl:call-template name="xpdo-object" >
<xsl:with-param name="skip-delimiter">0</xsl:with-param>
</xsl:call-template>
<!-- Get all associated Classes -->
<xsl:for-each select="./aggregate">
<xsl:call-template name="xpdo-object" >
<xsl:with-param name="object" select="@class"></xsl:with-param>
<xsl:with-param name="include-dependencies" select="1"></xsl:with-param>
<xsl:with-param name="skip-delimiter">0</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
<xsl:for-each select="./composite">
<xsl:call-template name="xpdo-object" >
<xsl:with-param name="object" select="@class"></xsl:with-param>
<xsl:with-param name="include-dependencies" select="1"></xsl:with-param>
<xsl:with-param name="skip-delimiter">0</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
<!-- /Get all associated Classes -->
</xsl:if>
</xsl:for-each>}
}
</xsl:template>
<xsl:template name="xpdo-object" match="object">
<xsl:param name="object" />
<xsl:param name="skip-delimiter" />
<xsl:for-each select=".">
,"<xsl:value-of select="@class" />":{
"class": "<xsl:value-of select="@class" />",
"extends":"<xsl:value-of select="@extends" />",
"fields": [
{
"name": "id",
"type": "int"
}
<xsl:for-each select="//object[@class=current()/@class]/field">
<xsl:apply-templates select="current()" />
</xsl:for-each>
],
"associations": [<xsl:choose>
<xsl:when test="$include-dependencies=1">
<xsl:choose>
<xsl:when test="$include-local-only=1">
<xsl:apply-templates select="./aggregate[@owner!='foreign']" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="./aggregate" />
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="./composite" />
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>]
}
</xsl:for-each>
</xsl:template>
<xsl:template match="field[@phptype='string']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "string",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='json']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "string",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='integer']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "int",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='date']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "date",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='boolean']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "boolean",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='datetime']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "date",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<xsl:template match="field[@phptype='timestamp']">
<xsl:text> ,{</xsl:text>
"name": "<xsl:value-of select="@key" />",
"type": "string",
"defaultValue":"<xsl:value-of select="@default" />"
}
</xsl:template>
<!--
<xsl:template match="field[@index="fk"]">
,{
//itemCls:"xpdo-object-field, <xsl:value-of select="@phptype" />, xpdo-foreign-object-<xsl:value-of select="../aggregate[@local=current()/@key]/@class" />",
}
</xsl:template>
-->
<xsl:template match="aggregate">
{
<xsl:choose>
<xsl:when test="@owner='local'"> "type": "hasMany",</xsl:when>
<xsl:otherwise> "type": "belongsTo",</xsl:otherwise>
</xsl:choose>
"model": "<xsl:value-of select="./@class" />",
"primaryKey": "<xsl:value-of select="@local" />",
"foreignKey": "<xsl:value-of select="@foreign" />",
"associationKey": "<xsl:value-of select="@local" />"
}
</xsl:template>
<xsl:template match="composite">
{
<xsl:choose>
<xsl:when test="@owner='local'"> "type": "hasMany",</xsl:when>
<xsl:otherwise> "type": "belongsTo",</xsl:otherwise>
</xsl:choose>
"model": "<xsl:value-of select="./@class" />",
"primaryKey": "<xsl:value-of select="@local" />",
"foreignKey": "<xsl:value-of select="@foreign" />",
"associationKey": "<xsl:value-of select="@local" />"
}
</xsl:template>
</xsl:stylesheet>
<!--
<validation>
<rule field="category" name="preventBlank" type="xPDOValidationRule" rule="xPDOMinLengthValidationRule" value="1" message="category_err_ns_name" />
</validation>
<validation>
<rule field="name" name="invalid" type="preg_match" rule="/^(?!\s)[a-zA-Z0-9\x2d-\x2f\x7f-\xff_-\s]+(?!\s)$/" message="tv_err_invalid_name" />
<rule field="name" name="reserved" type="preg_match" rule="/(?!^(id|type|contentType|pagetitle|longtitle|description|alias|link_attributes|published|pub_date|unpub_date|parent|isfolder|introtext|content|richtext|template|menuindex|searchable|cacheable|createdby|createdon|editedby|editedon|deleted|deletedby|deletedon|publishedon|publishedby|menutitle|donthit|haskeywords|hasmetatags|privateweb|privatemgr|content_dispo|hidemenu|class_key|context_key|content_type)$)/" message="tv_err_reserved_name" />
</validation>
<validation>
<rule field="name" name="name" type="xPDOValidationRule" rule="xPDOMinLengthValidationRule" value="1" message="content_type_err_ns_name" />
</validation>
<validation>
<rule field="name" name="invalid" type="preg_match" rule="/^(?!\s)[a-zA-Z0-9\x2d-\x2f\x7f-\xff_-\s]+(?!\s)$/" message="chunk_err_invalid_name" />
</validation
<validation>
<rule field="category" name="preventBlank" type="xPDOValidationRule" rule="xPDOMinLengthValidationRule" value="1" message="category_err_ns_name" />
</validation>
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment