In phpFox, a feed item is a story about an actor perform an action in which members can like, comment, share, ...
Example feed about admin post a status
An activity stream is a list of activities performed by a relevant actors, on phpFox system there are 2 kind of activity stream
phpFox store feed data in table phpfox_feed, look in to table information, there are following important columns:
When a member post a new to do item, we must add a new feed with associate data
We do not insert the value directly in to database, almost case, developer using Feed to handle insert process.
Phpfox::getService('feed.process')->add('todo', $iTaskId, 0,0); |
Let's update AddController.php, modify code as following
<?php namespace Apps\TodoList\Controller; class AddController extends \Phpfox_Component { public function process() { // Get phpFox core template service $template = $this->template(); // set view title $template->setTitle('Add To Do'); // set view breadcrumb $template->setBreadCrumb('Add To Do', $this->url()->makeUrl('to-do-list/add')); // add your section menus $template->buildSectionMenu('to-do-list', [ 'Browse' => $this->url()->makeUrl('/to-do-list'), 'Create' => $this->url()->makeUrl('/to-do-list/add'), ]); // get current requests $request = $this->request(); // get request data $vals = $request->get('val'); if (!empty($vals)) { // validate if (empty($vals['name'])) { \Phpfox_Error::set(_p('To do name is required')); } if (empty($vals['description'])) { \Phpfox_Error::set(_p('Do do description is required')); } if (\Phpfox_Error::isPassed()) { // insert to do item database $iItemId = \Phpfox::getLib('database')->insert(\Phpfox::getT('todolist_task'),[ 'user_id'=> \Phpfox::getUserId(), // get current user id 'name'=> $vals['name'], 'description'=>$vals['description'], 'time_stamp'=>time(), // creatation time 'time_update'=>time(), // last modification time 'privacy'=>0, // public 'task_status'=>0, // mark task is in-complete ]); // insert new feed \Phpfox::getService('feed.process')->add('todo', $iItemId, 0,0); $this->url()->send('to-do-list'); } } } } |
Visit '+ Add To Do', add new item, visit phpfox_feed, table you see a new entry with type_id is todolist.
phpFox Feed service will handle how to fetch feed items from database, but it's does not know detail information about feed,
so it will interact to others using a Callback service, in phpFox almost app have at least one callback service.
Define a callback service is simple, Just add new class Callback under Service directory, past following code
<?php namespace Apps\TodoList\Service; class Callback extends \Phpfox_Service { } |
Then register callback service to start.php
{code language=php} // register service $module->addServiceNames([ 'todo.browse' => Service\Browse::class, 'todo' => Service\TodoList::class, 'todo.callback' => Service\Callback::class, ]); |
Now we will add getActivityFeed() to the Callback,
<?php namespace Apps\TodoList\Service; use Phpfox; class Callback extends \Phpfox_Service { public function getActivityFeed($aRow, $aCallback = null, $isChild = false){ $aItem = \Phpfox::getService('todo')->getForBrowse($aRow['item_id']); $aReturn = array_merge(array( 'feed_title' => $aItem['name'], 'privacy' => $aItem['privacy'], 'feed_info' => _p('post_new_to_do'), 'feed_link' => Phpfox::permalink('to-do-list.view', $aItem['task_id'], $aItem['name']), 'feed_content' => $aItem['name'], 'total_comment' => $aItem['total_comment'], 'feed_total_like' => $aItem['total_like'], 'feed_is_liked' => isset($aRow['is_liked']) ? $aRow['is_liked'] : false, 'feed_icon' => '', 'time_stamp' => $aRow['time_stamp'], 'enable_like' => true, 'comment_type_id' => 'todo', 'like_type_id' => 'todo', 'custom_data_cache' => $aRow ), $aRow); return $aReturn; } } |
Click on phpFox home page, you see the first feed result
!../images/https://docs.phpfox.com/download/attachments/851993/todolist-feed.png?api=v2!
As you see, the feed headline is concat from the actor and and a phrase "post_new_to_do",
add this phrase to phrase.json then re-validate app. The final result will be.
Continue with Callback Service, update code
<?php namespace Apps\TodoList\Service; use Phpfox; class Callback extends \Phpfox_Service { // this method return required information to show a feed item on stream public function getActivityFeed($aRow, $aCallback = null, $isChild = false) { $aItem = \Phpfox::getService('todo') ->getForBrowse($aRow['item_id']); $isLiked = false; if(Phpfox::isUser()){ $isLiked = $this->database() ->select('l.like_id AS is_liked') ->from(Phpfox::getT('todolist_task'), 'b') ->leftJoin(Phpfox::getT('like'), 'l', 'l.type_id = \'todo\' AND l.item_id = b.task_id AND l.user_id = ' . Phpfox::getUserId()) ->execute('getSlaveField'); } $aReturn = array_merge([ 'feed_title' => $aItem['name'], 'privacy' => $aItem['privacy'], 'feed_info' => _p('post_new_to_do'), 'feed_link' => Phpfox::permalink('to-do-list.view', $aItem['task_id'], $aItem['name']), 'feed_content' => $aItem['name'], 'total_comment' => $aItem['total_comment'], 'feed_total_like' => $aItem['total_like'], 'feed_is_liked' => $isLiked, 'feed_icon' => '', 'time_stamp' => $aRow['time_stamp'], 'enable_like' => true, 'comment_type_id' => 'todo', 'like_type_id' => 'todo', 'custom_data_cache' => $aRow, ], $aRow); return $aReturn; } public function addLike($iItemId) { $this->database()->updateCount('like', 'type_id = \'todo\' AND item_id = ' . (int)$iItemId . '', 'total_like', 'todolist_task', 'task_id = ' . (int)$iItemId); } } |
Visit Homepage again, you can like the posted to do as the attachment:
Continue edit Callback service, add 3 methods
<?php // update total comment count public function addComment($aVals) { $this->database() ->updateCounter('todolist_task', 'total_comment', 'task_id', $aVals['item_id']); } // Comment callback required public function getAjaxCommentVar() { } // Comment callback required public function getCommentItem($iId) { $aRow = Phpfox::getService('todo') ->getForBrowse($iId); $aRow['comment_item_id'] = $iId; $aRow['comment_user_id'] = Phpfox::getUserId(); $aRow['comment_view_id'] = 0; if (empty($aRow)) { return false; } return $aRow; } // update total comment count public function deleteComment($iId) { $data = ['total_comment' => ['= total_comment -', 1]]; $table = Phpfox::getT('todolist_task'); $this->database()->update($table, $data, 'task_id = ' . (int)$iId); } |
Visit Home page again, you can post some comments on to do item as the attachment:
Continue edit Callback service, add method
// share service required callback method public function canShareItemOnFeed(){ } |
Visit Home page again, click on share button on to do list item, put some line then submit, the result