Using new CB Activity with API

8 years 8 months ago #268822 by activha
Replied by activha on topic Using new CB Activity with API
Thanks for the explanation, I thought that I got it but cannot make it to work

I have corrected the modified php file as per your indications and left it in the k2 content plugin, then set the activity auto action like screenshots attached without any conditions.







Nothing is added and I have no error messages. Difficult to debug as a click to execute would only give [varx]

Can you help to find out what needs to be changed in all this to make it work ?

Thanks
Jean

PS cannot attach files seems your server refuses them
PHP code :
<?php

defined('_JEXEC') or die;
require_once(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');

//Load language file
$lang =  JFactory::getLanguage();
$lang->load('plg_content_K2_cb_activitystream', JPATH_ADMINISTRATOR);

class PlgContentK2_cb_activitystream extends JPlugin
{
    protected $autoloadLanguage = true;
	       
	//Add entry in activity stream
	public function onContentAfterSave($context, $article, $isNew)
	{       
		$user = JFactory::getUser();		   
	    
	    //In k2 component
	    if(JRequest::getVar('option') == JText::_('PLG_CONTENT_K2CB_COM_NAME'))
	    {
			//If edited
			if(!$isNew)
			{
				//Check for article category from edit category parameter
			
				if( in_array( $article->catid, $this->params->get('edit_article') ) )	 
			             $this->pushToCBActivity($user->id,'com_k2','edit',
			             						 JText::_('PLG_CONTENT_K2CB_EDIT_ART'),
			             						 K2HelperRoute::getItemRoute($article->id, $article->catid),$article->title,
			             						 $article->id,$article->introtext,$this->params->get('char'));   				     
			}
			else // If new
			{
				//Check for article category from add category parameter
				
				if( in_array( $article->catid, $this->params->get('add_article') ) )
			             $this->pushToCBActivity($user->id,'com_k2','add',JText::_('PLG_CONTENT_K2CB_ADD_ART'),
			             						 K2HelperRoute::getItemRoute($article->id, $article->catid),$article->title,
			             						 $article->id,$article->introtext,$this->params->get('char'));   				     
			   
			   }
		}//if
	}
	
	//Function to add activity in CB
	function pushToCBActivity($var1,$var2,$var3,$var4)
	{ 
		//load CB framework		
		global $_PLUGINS;
		if ( ( ! file_exists( JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php' ) ) || ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) ) {
			echo 'CB not installed'; return;
		}
 
		include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
 
		cbimport( 'cb.html' );
		cbimport( 'language.front' );
 
		$_PLUGINS->loadPluginGroup( 'user' );

		//push activity
		$linkHTML = '<a href="'.$act_link.'">'.$act_title.'</a>';
		//article image

		$message = '<div class="coverdiv">';
		
		//If article has image then add it to activity
		if($this->getArticleImage($articleid))
		{
		    $message .= '<div class="article-image">
		                         <a href="'.$act_link.'">
		                            <img src="'.$this->getArticleImage($articleid).'" width="'.$this->params->get('width').'px" /></a>
		                 </div>';
	    }
	    
	    //If article has introtext then add it to activity
	    if($introtext)
	    {
		   	//remove html tag from introtext
	    	$introtext  = strip_tags($introtext); 
			$introtext  = substr( $introtext,0,$no_of_char);	
	    	
	    	$message .= '<div class="article-text">'.$introtext.'</div>';
	    }
	    
	   $message .= '</div>';
	   
	   $title = $act_description.' '.$linkHTML;
		
		$var1 = $article;
		$var2 = $message;
		$var3 = $title;
		$var4 = $user->id;
		
		$_PLUGINS->trigger( 'k2_onContentAfterSave', array($var1,$var2,$var3,$var4) );

		return true;
	}
	
	//Get an image for given k2 article id
	function getArticleImage($articleid)
	{
		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_XS.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_XS.jpg';
		}

		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_S.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_S.jpg';
		}

		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_M.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_M.jpg';
		}

		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_L.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_L.jpg';
		}

		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_XL.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_XL.jpg';
		}

		if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$articleid).'_Generic.jpg'))
		{
			$article_image = JURI::root(true).'/media/k2/items/cache/'.md5("Image".$articleid).'_Generic.jpg';
		}

		return $article_image;		
	}

	
}
Attachments:

Please Log in to join the conversation.

8 years 8 months ago - 8 years 8 months ago #268885 by krileon
Replied by krileon on topic Using new CB Activity with API
You didn't configure the activity correctly based off your variables. You have the following.

[var1] = Article Object
[var2] = Message
[var3] = Title
[var4] = User ID

Only message and title are being set in pushToCBActivity. Your other variables have not been defined in that function. Please review your code as it's not functional. Please be sure to use an IDE with error checking (e.g. PHPStorm, NetBeans, etc..) so you can see undefined variable errors and other usage/syntax errors.

Your activity for example would be the following if your above 4 variables actually worked.

Type: k2
Title: [var3]
Message: [var2]
Owner: [var4]


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

8 years 8 months ago #268983 by activha
Replied by activha on topic Using new CB Activity with API
OK got it, corrected the code and all is working nicely. Thanks :)

I have just noticed that you also need to have a trigger name different for CB and the plugin, if no it fails.

To follow, I have other questions regarding chaining and language strings :

Can you tell me ?
- if it's possible to have several actions : adding a k2 plus adding a place plus adding a url in the same activity auto action ? (I tried to add a k2 and a place name with lat,lng but it failed)

- what format of substitution to use for language strings in auto actions : only _UE_XXX_STRING or [STRING]

- where to translate the action words laughing, smiling, watching.... etc. Several strings are not in the cb plugin language file

- if it is possible to have a module with all activities and comments concerning an item id ?

Thanks

Please Log in to join the conversation.

8 years 8 months ago #268994 by krileon
Replied by krileon on topic Using new CB Activity with API

- if it's possible to have several actions : adding a k2 plus adding a place plus adding a url in the same activity auto action ? (I tried to add a k2 and a place name with lat,lng but it failed)

Only status posts can have a location, action, or tags associated with them (this is to be improved in a later release). All activity types can have Links attached. A custom link type can be used to output custom HTML. See existing System Actions for several usage examples.

- what format of substitution to use for language strings in auto actions : only _UE_XXX_STRING or [STRING]

You can't substitute in language strings. You just supply the string and it'll translate as needed.

- where to translate the action words laughing, smiling, watching.... etc. Several strings are not in the cb plugin language file

In CB Activity as done with all other CB plugins.

- if it is possible to have a module with all activities and comments concerning an item id ?

No.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

8 years 8 months ago #269157 by activha
Replied by activha on topic Using new CB Activity with API
Thanks for all these details :)

I have noticed that my cb activity auto action did not work if debug was off. Do you have any idea why this behavior ?

I have also noticed that CB activity main page display did not follow the privacy rules. For instance mine are set to not display activities when people are not registered but main activity page does not respect this. Bug ?

Thanks

Please Log in to join the conversation.

8 years 8 months ago #269159 by krileon
Replied by krileon on topic Using new CB Activity with API

I have noticed that my cb activity auto action did not work if debug was off. Do you have any idea why this behavior ?

No idea, shouldn't matter as long as your trigger is working and firing correctly.

I have also noticed that CB activity main page display did not follow the privacy rules. For instance mine are set to not display activities when people are not registered but main activity page does not respect this. Bug ?

CB Activity doesn't integrate with CB Privacy at this time. You can only control access to the entire stream it self by adjusting the menu item view access level and/or the activity tab view access level.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.540 seconds

Facebook Twitter LinkedIn