<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class User_ProfileController extends Core_Controller_Action_Standard
{
  public function init()
  {
    // @todo this may not work with some of the content stuff in here, double-check
    $subject = null;
    if( !Engine_Api::_()->core()->hasSubject() )
    {
      $id = $this->_getParam('id');

      // use viewer ID if not specified
      //if( is_null($id) )
      //  $id = Engine_Api::_()->user()->getViewer()->getIdentity();

      if( null !== $id )
      {
        $subject = Engine_Api::_()->user()->getUser($id);
        if( $subject->getIdentity() )
        {
          Engine_Api::_()->core()->setSubject($subject);
        }
      }
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $this->_helper->requireSubject('user');
    $this->_helper->requireAuth()
    //  ->setNoForward()                         // for showing image and name irrespective of privacy
      ->setAuthParams($subject, $viewer, 'view')
      ->isValid();
  }

  public function indexAction()
  {
    $subject = Engine_Api::_()->core()->getSubject();
    $viewer = Engine_Api::_()->user()->getViewer();

    // check public settings
    $require_check = Engine_Api::_()->getApi('settings', 'core')->core_general_portal;
    if( !$require_check && !$this->_helper->requireUser()->isValid() ) {
      return;
    }

    // Check enabled
    if( !$subject->enabled && !$viewer->isAdmin() ) {
      return $this->_forward('requireauth', 'error', 'core');
    }

    // Check block
    if( $viewer->isBlockedBy($subject) && !$viewer->isAdmin() ) {
      return $this->_forward('requireauth', 'error', 'core');
    }

    // Increment view count
    if( !$subject->isSelf($viewer) ) {
      $subject->view_count++;
      $subject->save();
    }


    // Check to see if profile styles is allowed
    $style_perm = Engine_Api::_()->getDbtable('permissions', 'authorization')->getAllowed('user', $subject->level_id, 'style');
		$user_front = Zend_Registry::isRegistered('user_front') ? Zend_Registry::get('user_front') : null;
    if($style_perm){
      // Get styles
      $table = Engine_Api::_()->getDbtable('styles', 'core');
      $select = $table->select()
        ->where('type = ?', $subject->getType())
        ->where('id = ?', $subject->getIdentity())
        ->limit();

      $row = $table->fetchRow($select);
      if( null !== $row && !empty($row->style) )
      {
        $this->view->headStyle()->appendStyle($row->style);
      }
    }

    // Render
    $row = $subject->membership()->getRow($viewer);
    if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('userprofilelock') && !empty(Engine_Api::_()->getApi('settings', 'core')->getSetting('userprofilelock.enable', 1)) && !$viewer->isSuperAdmin() && $viewer->getIdentity() != $subject->getIdentity() && $subject->profilelock && !$row && !$row->active) {

    } else {
			if(!empty($user_front)) {
				$this->_helper->content->setNoRender()->setEnabled();
			}
    }
  }

  public function statusAction() {
    $status = $this->_getParam('status');
    $subject = Engine_Api::_()->core()->getSubject();
    $subject->status = $status;
    $subject->status_date = date('Y-m-d H:i:s');
    $subject->save();
    exit();
  }
}
