Topic: [TIP] Jot XML example  (Read 4840 times)

Pages: [1]   Go Down

#1: 29-Nov-2006, 04:03 AM

Moderator

bS
Posts: 398

WWW
Need comments in XML format?
Demo: source | xml

You can use technique to make comments available as a feed or to create some sort of AJAX implementation of Jot.

Here's how to do this:

1. Create a new chunk: JotTplXML
This is just one example of a format  but you can customize the format any way you want.. just be sure to use :esc in the user edited content placeholders to escape all html tags else the xml will choke.
Code:
[+comment.published:is=`1`:then=`
<comment id="[+comment.id+]"
postnr="[+comment.postnumber+]"
createdon="[+comment.createdon:date=`%B %d, %Y, %H:%M:%S`+]"
editedon="[+comment.editedon:date=`%B %d, %Y, %H:%M:%S`+]"
createdby="[+comment.createdby+]"
editedby="[+comment.editedby+]">
<user postcount="[+comment.userpostcount+]">
[+comment.createdby:userinfo=`username`:ifempty=`[+comment.custom.name:ifempty=`[+jot.guestname+]`+]`:esc+]
</user>
<subject>[+comment.title:esc+]</subject>
<message>[+comment.content:esc:nl2br:esc+]</message>
</comment>
`:strip+]

2. Create a new chunk: JotXML
You can enter the docid as a static value or you can use a snippet that takes a querystring id  to create a dynamic source document.  You can also add tag id if you want)

Code:
<?xml version="1.0"?>
[[Jot?docid=`45` &placeholders=`1` &output=`0` &tplComments=`JotTplXML` &pagination=`0`]]
<jot>
[+jot.html.comments+]
</jot>

3. Create a new document

- Set the Content Type to: text/xml
- In the document content field enter: {{JotXML}}

View the page Smiley
« Last Edit: 29-Nov-2006, 04:35 AM by bS »
Armand Pondman
MODx Coding Team
:: Jot :: PHx

#2: 29-Nov-2006, 04:13 AM

g5604
Posts: 395

WWW
great stuff  Grin

#3: 18-Dec-2006, 04:25 AM

Testers

andrazk
Posts: 144

How to get ALL comments and put them in an XML, not just the ones from a specific document?

#4: 19-Dec-2006, 01:21 AM

Moderator

bS
Posts: 398

WWW
How to get ALL comments and put them in an XML, not just the ones from a specific document?

that isn't possible yet, i was thinking of a next version that will support something like &docid=`*` and tagid=`*` that will give you all comments
Armand Pondman
MODx Coding Team
:: Jot :: PHx

#5: 3-Jan-2007, 04:38 PM

Shadock
Posts: 15

WWW
Here are the smalls hack for andrazk:
I've also changed the chunks to export in true rss 2.0 format. it's validated in rss validator and works for me in netvibes.

1) docid = '*' :
edit the file jot.db.class.inc.php in the includes folder of the snippet :
Function GetCommentCount, at the end :
replace :
Code:
$sql = 'SELECT count(id) FROM '.$this->tbl["content"].' WHERE uparent = '.$docid.' AND tagid = "' . $tagid .'"'.$where;

return intval($modx->db->getValue($sql));
by :
Code:
if ($docid == '*') {
      $sql = 'SELECT count(id) FROM '.$this->tbl["content"].' WHERE tagid = "' . $tagid .'"'.$where;
    } else
    {
  $sql = 'SELECT count(id) FROM '.$this->tbl["content"].' WHERE uparent = '.$docid.' AND tagid = "' . $tagid .'"'.$where;
}
return intval($modx->db->getValue($sql));


Function GetComments, at the end :
replace :
Code:
$sql = "select a.* from " . $tbl . " as a " . $tblcustom . " where uparent = '" . $docid . "' and tagid = '" . $tagid ."' and mode = '0' " . $where . $orderby . $limit;


#print $sql;
return $this->GetCommentsArray($sql);
by :
Code:
if ($docid == '*') {
      $sql = "select a.* from " . $tbl . " as a " . $tblcustom . " where tagid = '" . $tagid ."' and mode = '0' " . $where . $orderby . $limit;
    } else
    {
  $sql = "select a.* from " . $tbl . " as a " . $tblcustom . " where uparent = '" . $docid . "' and tagid = '" . $tagid ."' and mode = '0' " . $where . $orderby . $limit;
}

#print $sql;
return $this->GetCommentsArray($sql);

That's all for the '*' hack.

RSS 2.0 output :

JotTplXML :
Code:
[+comment.published:is=`1`:then=`
<item>
<title><![CDATA[ [+comment.title:esc+] ]]></title>
<link><![CDATA[ http://www.mysite.com/index.php?id=[+comment.uparent+] ]]></link>
<author><![CDATA[ [+comment.createdby:userinfo=`username`:ifempty=`[+comment.custom.name:ifempty=`[+jot.guestname+]`+]`:esc+] ]]></author>
<guid isPermaLink="false">[+comment.id+]</guid>
<pubDate>[+comment.createdon:date=`%a, %e %b %Y %H:%M:%S %z`+] </pubDate>
<description><![CDATA[ [+comment.content:esc:nl2br:esc+] ]]></description>
</item>
`:strip+]

JotXML :
Code:
<?xml version="1.0"?>
[[JotRSS?docid=`*` &placeholders=`1` &output=`0` &tplComments=`JotTplXML` &pagination=`0`]]
<rss version="2.0">
<channel>
<title>last comments</title>
<link><![CDATA[http://www.mysite.com]]></link>
<description>comments</description>
<language>fr</language>
<copyright>perso</copyright>
<ttl>1</ttl>
[+jot.html.comments+]
</channel></rss>

(you can of course change the datas inside <title>, <link>, <description>, <language>, <copyright> and <ttl>
« Last Edit: 3-Jan-2007, 06:51 PM by Shadock »

#6: 3-Jan-2007, 06:06 PM

Testers

andrazk
Posts: 144

Thanks for this mod.
But since I'm not an expert in PHx (I must still explore this extension and its use) I'm interested in how to get the title of the document containing the comment to include it in the comment title.

I'll try to call a snippet with the comment owner ID as a parameter and get the document data. Will report if my idea works.

Still, if I leave CDATA section in <description> tag, whole tpl is output like

Code:
<copyright />
  <ttl>120</ttl>
  [+comment.published:is=`1`:then=`
 <item>
  <title>title</title>
  <link>http://www.site.eu:80/#18a8bd4611</link>
 <description>
 <![CDATA[                  Comment value
  ]]>
  </description>
  <pubDate>Fri, 15 Dec 2006 14:09:36 +0100</pubDate>
  <guid isPermaLink="false">http://www.site.eu:80/#11</guid>
  <dc:creator>Irene</dc:creator>
  </item>
  `:strip+]
  </channel>

Note the PHx placeholders. If there is no CDATA tag everything is OK.
« Last Edit: 3-Jan-2007, 06:11 PM by andrazk »

#7: 3-Jan-2007, 06:50 PM

Shadock
Posts: 15

WWW
oups sorry i've forgotten : it's [+comment.uparent+]

I've updated the chunk above with the <link> tag :
<link><![CDATA[ http://www.mysite.com/index.php?id=[+comment.uparent+] ]]></link>

#8: 5-Jan-2007, 07:08 AM

Testers

andrazk
Posts: 144

I have a (looks like) PHx problem with Jot.

My document is
Code:
<?xml version="1.0" encoding="[(etomite_charset)]" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>[*pagetitle*]</title>
<link>[(site_url)][~39~]</link>
<description>[*description*]</description>
<language>english</language>
<copyright>[(site_name)]</copyright>
<ttl>120</ttl>

[[Jot? &docid=`*` &placeholders=`1` &output=`0` &tplComments=`tplJotRSSComments` &pagination=`0` ]]
[+jot.html.comments+]
</channel>
</rss>

and tplJotRSSComments
Code:
[+comment.published:is=`1`:then=`
<item>
<title>
                                     <![CDATA[ comment for "[[getField?docid=`[+comment.uparent+]`]]" ]]>
                                </title>
<link>[(site_url)][~[+comment.uparent+]~]#cid[+comment.id+]</link>
<description>
                                   <![CDATA[ [+comment.content:esc:nl2br:esc+] ]]>
                                 </description>
<pubDate>[+comment.createdon:date=`%a, %e %b %Y %H:%M:%S %z`+]</pubDate>
<guid isPermaLink="false">[(site_url)][~[+comment.uparent+]~]#cid[+comment.id+]</guid>
<dc:creator>[+comment.createdby:userinfo=`fullname`:ifempty=`[+comment.custom.name:ifempty=`[+jot.guestname+]`+]`:esc+]</dc:creator>

</item>
`:strip+]

The problem is that when I use CDATA in tplJotRSSComments the PHx tags
Code:
[+comment.published:is=`1`:then=`

`:strip+]
remains in the output XML.

As soon as there is no CDATA tags, everything is ok.
Why?
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.