× Joomla Facebook Connect support forum

Topic-icon Publish to stream on dropdown select

Active Subscriptions:

None
16 years 6 months ago #2270 by playak
I already had a form where people can choose if they will attend an event. I wanted to build on this and automatically publish this sort of info to people's Facebook stream. It was tricky, but possible, so I thought I might as well share my results.
require_once (JPATH_ROOT.DS.'components'.DS.'com_jfbconnect'.DS.'helpers'.DS.'facebookhelper.php');
JFBConnectFacebookHelper::init();
$fbclient = JFBConnectFacebookHelper::getFbClient();
if (is_object($fbclient) && $fbclient->user && !$fbclient->users_hasAppPermission("publish_stream")) 
	$onchange = "FB.Connect.showPermissionDialog('publish_stream', function(x){if (x=='publish_stream') document.getElementById('involvement').submit();}); "; // involvement
else // else, just submit
	$onchange = "document.getElementById('involvement').submit();";

The $onchange then goes in the select part of the form:

$form = <<<EOT
<form method="post" action="calendar.php" id="involvement" promptpermission="publish_stream" >
	<input type="hidden" name="op" value="myevent">
	<input type="hidden" name="a&#91;cid&#93;" value="1417">
	<input type="hidden" name="a&#91;userid&#93;" value="2359">
	<span class="contribute"><strong>My Events</strong></span>&#58;
	<select name="a&#91;involvement&#93;" id="ainvolvement" onchange="$onchange" >
	<option value="none" >I will not go to this event</option>
	<option value="visit"  selected="selected">I plan to visit this event</option>
	<option value="compete" >I plan to compete in this event</option>
</form>
EOT;

(btw the form attribute promptpermission="publish_stream" doesn't work. i was hoping for that solution originally...)

and then when the form is submitted, the following code is executed&#58;

$fbmessage = "i will go to this event"; // or similar, depending on value of $a&#91;involvement&#93;
$attachment&#91;'name'&#93; = $event&#91;eventname&#93;;
$attachment&#91;'href'&#93; = "http&#58;//example.com/calendar.php?eid=$a&#91;cid&#93;";
$attachment&#91;'description'&#93; = strip_tags(substr($event&#91;eventdesc&#93;, 0, 200))."...";
if (file_exists("components/com_calendar/images/$a&#91;cid&#93;-s.jpg"))
{
	$attachment&#91;'media'&#93;&#91;0&#93;&#91;'type'&#93; = 'image';
	$attachment&#91;'media'&#93;&#91;0&#93;&#91;'src'&#93; = "http&#58;//example.com/components/com_calendar/images/$a&#91;cid&#93;-s.jpg";
	$attachment&#91;'media'&#93;&#91;0&#93;&#91;'href'&#93; = "http&#58;//example.com/calendar.php?eid=$a&#91;cid&#93;";
}
$attachment = json_encode($attachment);
$action_links&#91;0&#93;&#91;'text'&#93; = $event&#91;eventname&#93;;
$action_links&#91;0&#93;&#91;'href'&#93; = "http&#58;//example.com/calendar.php?eid=$a&#91;cid&#93;";
$action_links = json_encode($action_links);
$fbclient->stream_publish($fbmessage, $attachment, $action_links);
and voila.

if someone has a better solution or similar scripts, please share it with us :)
The topic has been locked.
Support Specialist
16 years 6 months ago #2273 by alzander
Thanks for the code snippet. This is doing it through the JavaScript method, which is client side. You could also add it to the server side to publish after the form is submitted using the following post:
<!-- m --><a class="postlink" href="www.cmsmarket.com/forum/index.php?f=38&t...rb_v=viewtopic#p2253">www.cmsmarket.com/forum/index.ph ... opic#p2253

After we work the few remaining kinks out of this release, we want to create some easy-to-use snippets in a JS file that others can embed in their site as well as more server side features for users to work with. That should help both 3rd party devs and others who want to implement the changes on their own site.

Thanks again!
The topic has been locked.