Re: Nieuws koppelen aan Twitter via UDT en Events [update]
Posted: Sat Dec 17, 2011 7:34 pm
Ah, dank je. Ga ik die leeg maken. Had dan overigens wel een tweet verwacht, maar dat staat daar los van schat ik in.
Gregor
Gregor
Content management as it is meant to be
https://forum.cmsmadesimple.org/
Wat verder opvalt, is dat cgblog niet terugkomt met een blog-overzicht, maar die, het overzicht dus, alleen maar weer via het menu zichtbaar kan worden gemaakt.cgblog_id = 273 title = Saildrive met leven erin aliased_title = Saildrive-met-leven-erin cgblog_url = cgblog_id = 273
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
echo ' aliased_title = ' . $aliased_title;
echo ' cgblog_url = ' . $cgblog_url;
echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
echo ' cgblog_url-1 = ' . $cgblog_url;
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
$prettyurl = 'logboek/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
}
echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
echo "debug";
$twitter->updateStatus($message, false);
}
}
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id) {
echo " in function ";
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
echo ' cgblog_url-function = ' . $cgblog_url;
return $cgblog_url;
}
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = 'logboek/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = 'logboek/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = false;
/* Prefix to use on all URLS from the blog module (string) */
$cgblog_page = 'logboek';
/* Extra parameter to add with tweet, f.i. a #xyz */
$extra_param = '#uisgebeatha';
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = $cgblog_page . '/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = $cgblog_page . '/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
// add extra parameter, if entered one
if ($extra_param != '') {
$message = $message . ' ' . $extra_param;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}
Code: Select all
/*******************************************************
* about
*******************************************************/
/*
This UDT can be used to automatically post a Twitter message when a news article is added.
The functionality is based on the Twitter and News module.
- First you need to install and setup the News and Twitter module.
- After you need to create a UDT with this code, for example you can call it: 'news_twitter_update_status'
- At last you need to connect this UDT to the 'NewsArticleAdded' event.
This can be done via Extensions -> Event Manager.
[s]! Currently this UDT only works when pretty urls are enabled.
It seems that the bit.ly url shortner ($twitter->shortenUrls($link)) has some problems with unfriendly urls.
Querystring parameters &var=1 are converted to &=1 which will break the link.[/s]
==
update: 16 Dec 2011
==
- This UDT is now compatible with cmsms 1.10
- Works with and without pretty urls bit.ly problem seems solved
- Added the ability to shortenurl's
- Added support for the news_url field, if you use these it's best to turnshortenurl's off
==
UPDATE: 4 Jan 2011
==
- Added the $extra_param configuration option
- Added the $hashtags configuration option
config:
- specify: $detailpage (page_id) of the news detail view.
- specify: $shortenurl (true|false) will use the bit.ly url shortner or not.
- specify: $extra_param (string) to add a default hashtag.
- specify: $hashtags (array) to modify the $params['title'] into a title with #hashtags, if $hashtags = array(); no replacement will take place
: for example: $hashtags = array('cup', 'foo', 'bar');
: "We have won the cup I repeat worldcup, well that is not my cupoftea" -> "We have won the #cup I repeat #worldcup that is not my #cupoftea"
*/
/*******************************************************
* globals/objects
*******************************************************/
$modops = cmsms()->GetModuleOperations();
// echo ' modops = ' . $modops;
$twitter = $modops->get_module_instance('Twitter');
// echo ' twitter = ' . $twitter;
$cgblog = $modops->get_module_instance('CGBlog');
// echo ' cgblog = ' . $cgblog;
/*******************************************************
* config
*******************************************************/
/* debug (bool) */
$debug = false;
/* custom (string) */
$detailpage = '';
/* shortenurl (bool) */
$shortenurl = true;
/* Prefix to use on all URLS from the blog module (string) */
$cgblog_page = $cgblog->GetPreference('urlprefix','cgblog');
/* Extra parameter to add with tweet, f.i. a #xyz */
$extra_param = '#uisgebeatha';
/* $hashtags (array) */
$hashtags = array('zeilen', 'wedstrijd', 'watersport');
/*******************************************************
* variables
*******************************************************/
/* CGBlog article related (string) */
$cgblog_id = $params['cgblog_id'];
// echo 'cgblog_id = ' . $cgblog_id;
$title = $params['title'];
// echo ' title = ' . $title;
/* added to link (array) */
$link['articleid'] = $params['cgblog_id'];
/*******************************************************
* subroutines
*******************************************************/
/**
* fetch the $cgblog_url by $cgblog_id
*
* @param string $cgblog_id
* @return string $cgblog_url
*
*/
function fetch_cgblog_url($cgblog_id){
$db = cmsms()->GetDb();
$query = "SELECT url
FROM ". cms_db_prefix(). "module_cgblog
WHERE cgblog_id = ?";
$cgblog_url = $db->GetOne($query, array($cgblog_id));
return $cgblog_url;
}
/*******************************************************
* program
*******************************************************/
$aliased_title = munge_string_to_url($title);
// echo ' aliased_title = ' . $aliased_title;
// echo ' cgblog_url = ' . $cgblog_url;
// echo ' cgblog_id = ' . $cgblog_id;
$cgblog_url = fetch_cgblog_url($cgblog_id);
// $cgblog_url = '';
if ($cgblog_url != '') {
$prettyurl = $cgblog_url;
} else {
// if $detailpage == '' we don't want an extra /
if ($detailpage != '') {
$prettyurl = $cgblog_page . '/' . $cgblog_id.'/'.$detailpage."/$aliased_title";
} else {
$prettyurl = $cgblog_page . '/' . $cgblog_id."/$aliased_title";
}
}
// echo ' prettyurl = ' . $prettyurl;
$link = $cgblog->CreateLink($cgblog_id, 'detail', $detailpage, '', $link,'', true, false, '', true, $prettyurl);
// title to hashed_title
$hashed_title = $title;
if (count($hashtags)) {
$hashtags_pattern = implode('|', $hashtags);
$words = explode(' ', $title);
$seen = array();
foreach ($words as $key => $word) {
if (preg_match("/$hashtags_pattern/i", $word) ) {
$stripped_word = preg_replace('/[^A-Za-z-]/','', $word);
if (strlen($word) == strlen($stripped_word)) {
if (! in_array($word, $seen) ) {
$seen[] = $word;
$hashed_title = preg_replace('~\b' . preg_quote($word, '~') . '\b~', '#$0', $hashed_title);
}
}
}
}
}
// strlen check of hashed_title - $extra_param
// math(140 - 22 - 11) = 107 ([max_twitter_length] - [: space $shortlink])
if (strlen($hashed_title) <= 107) {
$title = $hashed_title;
}
if ($shortenurl) {
$shortlink = $twitter->shortenUrls($link);
$message = $title. ' '. $shortlink;
} else {
$message = $title. ' '. $link;
}
// add extra parameter, if entered one
if ($extra_param != '') {
$message = $message . ' ' . $extra_param;
}
if ($debug) {
print "<pre>twitter: $message status: $params[status] link: $link title: $title prettyurl: $prettyurl \n</pre>";
} else {
if ($params[status] == 'published') {
$twitter->updateStatus($message, false);
}
}