Skip to content

Instantly share code, notes, and snippets.

@cirpo
Created November 8, 2010 21:57
Show Gist options
  • Select an option

  • Save cirpo/668344 to your computer and use it in GitHub Desktop.

Select an option

Save cirpo/668344 to your computer and use it in GitHub Desktop.
<?php
public function likeAction()
{
// Make sure user exists
if( !$this->_helper->requireUser()->isValid() ) return;
// Collect params
$action_id = $this->_getParam('action_id');
$viewer = $this->_helper->api()->user()->getViewer();
// Start transaction
$db = $this->_helper->api()->getDbtable('likes', 'activity')->getAdapter();
$db->beginTransaction();
try
{
$action = $this->_helper->api()->getDbtable('actions', 'activity')->getActionById($action_id);
// Check authorization
if (!Engine_Api::_()->authorization()->isAllowed($action->getObject(), null, 'comment'))
throw new Engine_Exception('This user is not allowed to like this item');
$action->likes()->addLike($viewer);
// Add notification for owner of activity (if user and not viewer)
if( $action->subject_type == 'user' && $action->subject_id != $viewer->getIdentity() )
{
$actionOwner = Engine_Api::_()->getItemByGuid($action->subject_type."_".$action->subject_id);
Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification($actionOwner, $viewer, $action, 'liked', array(
'label' => 'post'
));
}
$db->commit();
}
catch( Exception $e )
{
$db->rollBack();
throw $e;
}
// Success
$this->view->status = true;
$this->view->message = Zend_Registry::get('Zend_Translate')->_('You now like this action.');
// Redirect if not json context
if( null === $this->_helper->contextSwitch->getCurrentContext() )
{
$this->_helper->redirector->gotoRoute(array(), 'core_home');
}
else if ('json'===$this->_helper->contextSwitch->getCurrentContext())
{
$this->view->body = $this->view->activity($action, array('noList' => true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment