Using new CB Activity with API

8 years 8 months ago #268744 by activha
Using new CB Activity with API was created by activha
Hello

I did not found a tutorial to upgrade our plugins with your new CB Activity API.

Could you help us ?

We use the following code to push k2 content to CB activity old version and need to use this plugin for editorial reasons (CB Blog has not enough editing options for K2)

Coud you tell me what needs to be changed or only correct it quickly ?

Thanks in advance
<?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($actor_id,$act_type,$act_subtype,$act_description,$act_link,$act_title, $articleid,$introtext,$no_of_char)
	{ 
		//load CB framework
		global $_CB_framework, $mainframe;
		if(defined( 'JPATH_ADMINISTRATOR'))
		{
			if(!file_exists(JPATH_ADMINISTRATOR.'/components/com_comprofiler/plugin.foundation.php'))
			{
				echo JText::_('PLG_CONTENT_K2CB_CB_NOT_INSTALLED_MESSAGE');
				return false;
			}
			include_once( JPATH_ADMINISTRATOR.'/components/com_comprofiler/plugin.foundation.php' );
		}
		else
		{
			if(!file_exists($mainframe->getCfg('absolute_path').'/administrator/components/com_comprofiler/plugin.foundation.php'))
			{
				echo JText::_('PLG_CONTENT_K2CB_CB_NOT_INSTALLED_MESSAGE');
				return false;
			}
			include_once( $mainframe->getCfg('absolute_path').'/administrator/components/com_comprofiler/plugin.foundation.php' );
		}

		cbimport('cb.plugins');
		cbimport('cb.html');
		cbimport('cb.database');
		cbimport('language.front');
		cbimport('cb.snoopy');
		cbimport('cb.imgtoolbox');

		global $_CB_framework, $_CB_database, $ueConfig;

		//load cb activity plugin class
		if(!file_exists(JPATH_SITE.DS."components".DS."com_comprofiler".DS."plugin".DS."user".DS."plug_cbactivity".DS."cbactivity.class.php"))
		{
			echo JText::_('PLG_CONTENT_K2CB_CB_ACTIVITY_NOT_INSTALLED_MESSAGE');
			return false;
		}
		require_once(JPATH_SITE.DS."components".DS."com_comprofiler".DS."plugin".DS."user".DS."plug_cbactivity".DS."cbactivity.class.php");

		//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>';
		
       //add activity in activity stream
		$activity=cbactivityData::getActivity(array('id','=',$id),null,null,false);
		$activity->set('user_id',$actor_id);
		$activity->set('type',$act_type);
		$activity->set('subtype',$act_subtype);
		$activity->set('item',$articleid);
		$activity->set('title', $act_description.' '.$linkHTML);
		$activity->set('icon','nameplate');
		$activity->set('message',$message );
		$activity->set('date',$_CB_framework->getUTCDate());
		$activity->store();

		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;		
	}

	
}

Please Log in to join the conversation.

8 years 8 months ago - 8 years 8 months ago #268745 by krileon
Replied by krileon on topic Using new CB Activity with API
Don't use the API directly at all. Fire a new CB trigger then act on it using CB Auto Actions and you will no longer have to maintain all that custom code. To see how activity is logged directly see the below activity model for CB Auto Actions.

components/com_comprofiler/plugin/user/plug_cbautoactions/models/activity.php

That data API class is gone and no longer used. You use a direct database object. Example as follows.

FROM:
$activity=cbactivityData::getActivity(array('id','=',$id),null,null,false);
TO:
$activity    = new ActivityTable( $_CB_database );

$activity->load( (int) $id );

What values you set also needs to change as some of those are gone with other new ones being added. See the _comprofiler_plugin_activity database table for the structure. CB Activity however is being incorrectly included.

See the below in regards to just firing a trigger.

www.joomlapolis.com/forum/153-professional-member-support/230677-new-trigger-for-cb-auto-action-to-integrate-with-dpcalendar#268286

There won't be any API documentation or tutorials until later. I just don't have the time right now to write any.


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 #268748 by activha
Replied by activha on topic Using new CB Activity with API

Don't use the API directly at all. Fire a new CB trigger then act on it using CB Auto Actions and you will no longer have to maintain all that custom code...


Can you tell me how to use CB Auto Actions with outside components such as K2 or Hotspots ?

For now I used custom code with content plugins and onContentAfterSave for K2 and onAfterHotspotSave for Hotspots to add new activity item.

I don't really see what to do with auto actions ? what CB trigger would trigger an activity when a K2 item or a Hotspot is added ?

Please Log in to join the conversation.

8 years 8 months ago #268749 by krileon
Replied by krileon on topic Using new CB Activity with API
You need to first load CBs API then loads its plugins as shown in the below topic.

www.joomlapolis.com/forum/153-professional-member-support/230677-new-trigger-for-cb-auto-action-to-integrate-with-dpcalendar#268286

Next you need to fire your custom trigger. Multiple examples of triggers being fired shown in the below tutorial.

www.joomlapolis.com/support/tutorials/120-api-usage/18358-using-cb-triggers

So for example you could have the below trigger.

$_PLUGINS->trigger( 'k2_onContentAfterSave', array( TRIGGER_VARIABLES_HERE ) );

CB Auto Actions can parse up to 10 trigger variables. So where TRIGGER_VARIABLES_HERE is you can send as much or as little information as you want (accessed with [var1-10] substitution usage). You can also send objects or arrays as variables and they'll parse into substitutions as well (e.g. [var1_title]).

You'd just add your trigger to your Activity action and it'll act on it. Ensure CB Auto Actions is up to date with 6.3.1 as it's able to log all forms of activity (see System Actions for examples).

This basically means you do not have to maintain anything. The trigger API isn't changing in the foreseeable future and if it does we'll implement B/C. Letting CB Auto Actions bridge the two together causes the maintenance to simply mean keeping CB Auto Actions up to date. If you use the API directly it may or may not change, which will cause your usage to break.


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 #268751 by activha
Replied by activha on topic Using new CB Activity with API
I think I understand.
So my code would be something from
 //add activity in activity stream
		$activity=cbactivityData::getActivity(array('id','=',$id),null,null,false);
		$activity->set('user_id',$actor_id);
		$activity->set('type',$act_type);
		$activity->set('subtype',$act_subtype);
		$activity->set('item',$articleid);
		$activity->set('title', $act_description.' '.$linkHTML);
		$activity->set('icon','nameplate');
		$activity->set('message',$message );
		$activity->set('date',$_CB_framework->getUTCDate());
		$activity->store();
to
		//new code	
		$var1 = $actor_id;
		$var2 = $articleid;
		//to continue 	
		
		$_PLUGINS->trigger( 'k2_onContentAfterSave', array( $var1 , $var2  ) );
Is this correct ?

Please Log in to join the conversation.

8 years 8 months ago - 8 years 8 months ago #268752 by krileon
Replied by krileon on topic Using new CB Activity with API
You need to send whatever information you want to use in the activity logging. It looks like you need to be sending $message, $act_description, and $linkHTML as well. One of the most important variables you're sending though is the user id (actor_id) as you'll need to set Owner to [var1] for your current usage so the activity logs for that specific user.

You can send up to 10 variables as that's the limit CB Auto Action will parse, but a variable can be an object or array as well. So for example you could send the entire K2 article object if you wanted, which gives substitution access to all of that objects variables.

For example if the first variable is the article object you can substitute its values with [var1_title], [var1_created_by], etc..

Your code also needs to ensure CB Activity gets loaded, which is done by loadPluginGroup. See the first URL provided in my previous reply. You should be able to replace all of your pushToCBActivity code with the API loading and trigger firing.


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.331 seconds

Facebook Twitter LinkedIn