<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Poke_IndexController extends Core_Controller_Action_Standard {

  public function pokeAction() {

    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    $this->view->viewer_id = $viewer_id = $viewer->getIdentity();

    $id = $this->_getParam('id', null);
    $manageaction_id = $this->_getParam('manageaction_id', null);
    $this->view->manageaction = $manageActions = Engine_Api::_()->getItem('poke_manageaction', $manageaction_id);
    $name = strtolower(str_replace(' ', '_', @$manageActions->name));

    $this->view->item = $item = Engine_Api::_()->getItem('user', $id);

    $this->view->isPoke = $isPoke = Engine_Api::_()->getDbtable('pokes', 'poke')->isPoke(array('poster_id' => $viewer_id, 'receiver_id' => $id, 'manageaction_id' => $manageaction_id));
    
    $pokeTable = Engine_Api::_()->getDbtable('pokes', 'poke');

    if ($this->getRequest()->isPost()) {
      $values = array();
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {

        $values['poster_id'] = $viewer_id;
        $values['receiver_id'] = $id;
        if ($manageaction_id)
          $values['manageaction_id'] = $manageaction_id;
        $row = $pokeTable->createRow();
        $row->setFromArray($values);
        $row->save();

        $notification = Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification($item, $viewer, $viewer, 'poke', array("pokeActionText" => $manageActions->name, 'manageaction_id' => $manageaction_id, 'poke_id' => $row->getIdentity()));
        
        if($notification) {
          $row->notification_id = $notification->getIdentity();
          $row->save();
        }
        
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }
      $pokeBackActionText = $this->view->translate("%s is successfully sent.", $manageActions->name);
      return $this->_forward('success', 'utility', 'core', array(
        'parentRefresh' => 10,
        'messages' => array($pokeBackActionText)
      ));
    }
  }

  public function backRequestAction() {
    
    $id = $this->_getParam('id', null);
    $notification_id = $this->_getParam('notification_id', null);
    
    $pokes = Engine_Api::_()->getItem('poke_poke', $id);
    if(!empty($pokes->notification_id) && isset($pokes->notification_id)) {
      $notification_id = $pokes->notification_id;
    }
    $manageActions = Engine_Api::_()->getItem('poke_manageaction', $pokes->manageaction_id);
    $viewer = Engine_Api::_()->user()->getViewer();
    $item = Engine_Api::_()->getItem('user', $pokes->poster_id);
    
    $pokeBackActionText = $this->view->translate("has sent back %s", $manageActions->name);
    $db = Engine_Db_Table::getDefaultAdapter();
    try {
      // Set the requests as handled
      $notification = Engine_Api::_()->getItem('activity_notification', $notification_id);
      if( $notification ) {
        $notification->mitigated = true;
        $notification->read = 1;
        $notification->save();
      }
      
      Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification($item, $viewer, $viewer, 'poke_back', array("pokeBackActionText" => $pokeBackActionText));
      
      $db->query("DELETE FROM `engine4_poke_pokes` WHERE `engine4_poke_pokes`.`poke_id` = $id LIMIT 1");
      
      echo json_encode(array('status' => 'true', 'message' => Zend_Registry::get('Zend_Translate')->_('Your response is sent successfully.')));die;
    } catch( Exception $e ) {
      $db->rollBack();
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('An error has occurred.');
      $this->view->exception = $e->__toString();
    }
  }

  public function cancelRequestAction() {
  
    $id = $this->_getParam('id', null);
    $notification_id = $this->_getParam('notification_id', null);
    
    $pokes = Engine_Api::_()->getItem('poke_poke', $id);
    if(!empty($pokes->notification_id) && isset($pokes->notification_id)) {
      $notification_id = $pokes->notification_id;
    }

    $db = Engine_Db_Table::getDefaultAdapter();
    try {
      $db->query("DELETE FROM `engine4_poke_pokes` WHERE `engine4_poke_pokes`.`poke_id` = $id LIMIT 1");
      
      // Set the requests as handled
      $notification = Engine_Api::_()->getItem('activity_notification', $notification_id);
      if( $notification ) {
        $notification->delete();
      }
      
      echo json_encode(array('status' => 'true', 'message' => Zend_Registry::get('Zend_Translate')->_("This notification is ignored.")));die;
    } catch( Exception $e ) {
      $db->rollBack();

      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('An error has occurred.');
      $this->view->exception = $e->__toString();
    }
  }
  
  public function requestPokeAction()
  {
    $this->view->notification = $notification = $this->_getParam('notification');
    $this->setTokenData();
  }
  
  private function setTokenData()
  {
    $this->view->tokenName = $tokenName = 'token_' . $this->view->notification->getSubject()->getGuid();
    $salt = Engine_Api::_()->getApi('settings', 'core')->getSetting('core.secret');
    $this->view->tokenValue = $this->view->token(null, $tokenName, $salt);
  }
}
