<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Engine_View_Helper_Hooks extends Zend_View_Helper_Abstract
{
  protected $_separator = "\n\n";
  
  public function hooks($name, $payload = null)
  {
    $dispatcher = Engine_Hooks_Dispatcher::getInstance();
    $event = $dispatcher->callEvent($name, $payload);
    $responses = $event->getResponses();

    if( !is_array($responses) || empty($responses) ) {
      return '';
    }

    $content = '';
    foreach( $responses as $response ) {
      if( is_string($response) ) {
        $content .= $response;
      } else if( is_array($response) && !empty($response['type']) ) {
        if( $response['type'] == 'partial' ) {
          $content .= $this->view->partial(@$response['args'][0], @$response['args'][1], @$response['args'][2], @$response['args'][3]);
        } else if( $response['type'] == 'action' ) {
          $content .= $this->view->action(@$response['args'][0], @$response['args'][1], @$response['args'][2], @$response['args'][3]);
        }
      } else {
        throw new Zend_View-Exception('Unknown data type returned in '.get_class($this));
        continue;
      }

      $content .= $this->getSeparator();
    }

    return $content;
  }

  public function getSeparator()
  {
    return $this->_separator;
  }

  public function setSeparator($separator)
  {
    $this->_separator = $separator;
    return $this;
  }
}
