Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
php
php
Phpfox::getService('feed.process')->add('todo', $iTaskId, 0,0);

Let's update AddController.php, modify code as following

...

Visit '+ Add To Do', add new item, visit phpfox_feed, table you see a new entry with type_id is todolist'todo'.

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 just add new class Callback under Service directory, past following code

...

Then register callback service to start.php

Code Block
php
php
{code language=php}<?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
php
<?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

...

Integrating Like Service

Continue with Callback Service, update code

Code Block
php
php
<?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);
    }
}

...

Add Comment Service

Continue edit Callback service, add 3 methods

Code Block
php
php
<?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);
    }

...