...
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
...
We do not insert the value directly in to database, almost case, developer using Feed service to handle insert process.
Code Block | ||||
---|---|---|---|---|
| ||||
Phpfox::getService('feed.process')->add('todo', $iTaskId, 0,0);
|
Let's update AddController.php, modify code as following
Code Block | ||||
---|---|---|---|---|
| ||||
<?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');
}
}
}
}
|
...
Define a callback service is simple, just add new class Callback under Service directory, past following code
Code Block | ||||
---|---|---|---|---|
| ||||
<?php
namespace Apps\TodoList\Service;
class Callback extends \Phpfox_Service
{
}
|
Then register callback service to start.php
Code Block | ||||
---|---|---|---|---|
| ||||
<?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,
Code Block | ||||
---|---|---|---|---|
| ||||
<?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
Later, you can be able to change the layout of your feed.
...
Integrating Like Service
Continue with Callback Service, update code
Code Block | ||||
---|---|---|---|---|
| ||||
<?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:
Add Comment Service
Continue edit Callback service, add 3 methods
Code Block | ||||
---|---|---|---|---|
| ||||
<?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:
Integrating Share Service
Continue edit Callback service, add following empty method
Code Block | ||||
---|---|---|---|---|
| ||||
// 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
if you want to modify you feed layout, visit below chapter:
...