{"id":133,"date":"2006-02-03T08:03:59","date_gmt":"2006-02-03T13:03:59","guid":{"rendered":"http:\/\/crschmidt.net\/blog\/?p=133"},"modified":"2006-02-03T08:03:59","modified_gmt":"2006-02-03T13:03:59","slug":"ning-components-xnc_comment","status":"publish","type":"post","link":"https:\/\/crschmidt.net\/blog\/archives\/133\/ning-components-xnc_comment\/","title":{"rendered":"Ning Components: XNC_Comment"},"content":{"rendered":"<p>A long time ago, when I first started working in the Ning playground, the first thing that impressed me about it was the fact that it had ready built components for so much *stuff*. Comments. Ratings. Flickr. Amazon. Calendars. Questionairres (Which is a very difficult word to spell right.)<\/p>\n<p>Last night, after setting up <a href=\"http:\/\/xquery.ning.com\/\">X\\_Query<\/a>, I thought &#8220;hm, what if people want to comment?&#8221; And as I did, I remembered those components. I went into the <a href=\"http:\/\/developerdocumentation.ning.com\/\">developer documentation<\/a> and looked up <a href=\"http:\/\/developerdocumentation.ning.com\/post.php?Post:slug=XNC-XNC_Comment\">XNC\\_Comment<\/a>, which had a great, simple example of how to add comments to any content object.<\/p>\n<p>But I didn&#8217;t have any content objects. The app is built as an API, so there was no need for Content Objects (at least not until I build up the documentation).<\/p>\n<p>So I built a couple quick lines to add Content where I need it:<\/p>\n<p>&lt;?php<br \/>\n$d = XN\\_Query::create(&#8216;content&#8217;)->filter(&#8220;owner&#8221;)->filter(&#8220;type&#8221;, &#8220;=&#8221;, &#8220;Page&#8221;)->filter(&#8216;title&#8217;, &#8216;=&#8217;, $\\_SERVER[&#8216;SCRIPT\\_FILENAME&#8217;])->uniqueResult();<br \/>\nif (!$d) {<br \/>\n    $d = XN\\_Content::Create(&#8220;Page&#8221;, $\\_SERVER[&#8216;SCRIPT\\_FILENAME&#8217;]);<br \/>\n    $d->save();<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>Once this is done, the $d is an object for the current page. Once you&#8217;ve done that, you can just slightly modify the example from XNC_Comment:<\/p>\n<p>require\\_once <font color=\"#DD0000\">&#8216;XNC\/Comment.php&#8217;<\/font><font color=\"#007700\">;<br \/><\/font><font color=\"#0000BB\">$newComment <\/font><font color=\"#007700\">= new <\/font><font color=\"#0000BB\">XNC\\_Comment<\/font><font color=\"#007700\">(<\/font><font color=\"#0000BB\">$d<\/font><font color=\"#007700\">);<br \/><\/font><font color=\"#FF8000\">\/\/ Handle any form submission of adding a new comment<br \/><\/font><font color=\"#007700\">if (<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">willProcessForm<\/font><font color=\"#007700\">()) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">processForm<\/font><font color=\"#007700\">();<br \/>} elseif (<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">lastError<\/font><font color=\"#007700\">() != <\/font><font color=\"#0000BB\">XNC\\_Comment<\/font><font color=\"#007700\">::<\/font><font color=\"#0000BB\">ERROR\\_FORM\\_ABSENT<\/font><font color=\"#007700\">) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;print <\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">lastError<\/font><font color=\"#007700\">();<br \/>}<br \/><\/font><font color=\"#FF8000\">\/\/ Display the add a comment form (unless it was just submitted and saved)<br \/><\/font><font color=\"#007700\">if (<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">canProcessForm<\/font><font color=\"#007700\">()) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo <\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">buildForm<\/font><font color=\"#007700\">();<br \/>}<br \/><\/font><font color=\"#FF8000\">\/\/ Display a list of comments belonging to a parent object<br \/><\/font><font color=\"#007700\">if (<\/font><font color=\"#0000BB\">$d<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">my<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">content<\/font><font color=\"#007700\">(<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">referenceAttribute<\/font><font color=\"#007700\">,<\/font><font color=\"#0000BB\">true<\/font><font color=\"#007700\">)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach (<\/font><font color=\"#0000BB\">$d<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">my<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">content<\/font><font color=\"#007700\">(<\/font><font color=\"#0000BB\">$newComment<\/font><font color=\"#007700\">-&gt;<\/font><font color=\"#0000BB\">referenceAttribute<\/font><font color=\"#007700\">,<\/font><font color=\"#0000BB\">true<\/font><font color=\"#007700\">) as <\/font><font color=\"#0000BB\">$comment<\/font><font color=\"#007700\">) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo new <\/font><font color=\"#0000BB\">XNC\\_Comment<\/font><font color=\"#007700\">(<\/font><font color=\"#0000BB\">$comment<\/font><font color=\"#007700\">);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<\/font><\/p>\n<p>Now, you&#8217;ve got comments on any page you want! Just include these two chunks of code at the bottom of each page. This is one of the easiest things I&#8217;ve done. Now anyone can take these code snippets and use them in any Ning app. Hm&#8230; maybe the next step is to build a code snippets repository&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A long time ago, when I first started working in the Ning playground, the first thing that impressed me about it was the fact that it had ready built components for so much *stuff*. Comments. Ratings. Flickr. Amazon. Calendars. Questionairres (Which is a very difficult word to spell right.) Last night, after setting up X\\_Query, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,28],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-ning","category-php"],"_links":{"self":[{"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":0,"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"wp:attachment":[{"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crschmidt.net\/blog\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}