<?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.
$activity=cbactivityData::getActivity(array('id','=',$id),null,null,false);
$activity = new ActivityTable( $_CB_database );
$activity->load( (int) $id );
Please Log in to join the conversation.
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...
Please Log in to join the conversation.
$_PLUGINS->trigger( 'k2_onContentAfterSave', array( TRIGGER_VARIABLES_HERE ) );
Please Log in to join the conversation.
//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();
//new code
$var1 = $actor_id;
$var2 = $articleid;
//to continue
$_PLUGINS->trigger( 'k2_onContentAfterSave', array( $var1 , $var2 ) );
Please Log in to join the conversation.
Please Log in to join the conversation.