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

  public function onMenuInitialize_UserSettingsSupport($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity()) 
      return false;

    if(!Engine_Api::_()->getApi('settings', 'core')->getSetting('core.enablesupport', 1))
      return false;
    
    return true;
  }

  public function onMenuInitialize_CoreHelpsupportMobileSupportinbox($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity()) 
      return false;

    if(!Engine_Api::_()->getApi('settings', 'core')->getSetting('core.enablesupport', 1))
      return false;
    
    return true;
  }

  public function onMenuInitialize_CoreMinimenuSupportinbox($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity()) 
      return false;

    if(!Engine_Api::_()->getApi('settings', 'core')->getSetting('core.enablesupport', 1))
      return false;
    
    return true;
  }
  
  public function onMenuInitialize_UserSettingsNetwork($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity()) 
      return false;
    
    if(!Engine_Api::_()->getApi('settings', 'core')->getSetting('network.enable', 1))
      return false;
    
    $networks = Engine_Api::_()->getDbTable('networks', 'network')->fetchAll();
    if(engine_count($networks) == 0)
      return false;
    
    return true;
  }
  
  public function onMenuInitialize_CoreMinimenuEdit($row) {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity()) {
      return array(
        'label' => "Edit Profile",
        'icon' => 'fi fi-rr-user-pen',
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'edit',
          'action' => 'profile',
          'id' => $viewer->getIdentity(),
        )
      );
    }

    return false;
  }
  
  public function canDelete()
  {
    // Check subject
    if( !Engine_Api::_()->core()->hasSubject('user') ) {
      return false;
    }
    $subject = Engine_Api::_()->core()->getSubject('user');

    // Check viewer
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    // Check auth
    return (bool) Engine_Api::_()->authorization()->getPermission($subject, 'user', 'delete');
  }

  // core_main
  public function onMenuInitialize_CoreMainHome($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    $request = Zend_Controller_Front::getInstance()->getRequest();
    $route = array(
      'route' => 'default',
    );

    if( $viewer->getIdentity() ) {
      $route['route'] = 'user_general';
      $route['params'] = array(
        'action' => 'home',
      );
      if( 'user' == $request->getModuleName() &&
        'index' == $request->getControllerName() &&
        'home' == $request->getActionName() ) {
        $route['active'] = true;
      }
    }
    if( !empty($row->params['icon']) ) {
      return array_merge($row->params, $route);
    }
    return $route;
  }
  
  public function onMenuInitialize_UserSettingsNotifications($row) {
  
    $notificationTypes = Engine_Api::_()->getDbtable('notificationTypes', 'activity')->getDefaultNotifications();
		if(count($notificationTypes) == 0)
			return false;
		
		return true;
  }

  // core_mini
  public function onMenuInitialize_CoreMiniAdmin($row)
  {
    if( Engine_Api::_()->user()->getViewer()->isAllowed('admin') ) {
      return array(
        'label' => $row->label,
        'route' => 'admin_default',
        'class' => 'no-dloader',
      );
    }

    return false;
  }

  public function onMenuInitialize_CoreMiniProfile($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      $photo = '';
      if( Zend_Registry::isRegistered('Zend_View') ) {
        $view = Zend_Registry::get('Zend_View');
        $photo = $view->itemPhoto($viewer, 'thumb.icon');
      }
      return array(
        'label' => $photo . '<span>'. $row->label . '</span>',
        'uri' => $viewer->getHref(),
      );
    }
    return false;
  }

  public function onMenuInitialize_CoreMiniSettings($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      return array(
        'label' => $row->label,
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'settings',
          'action' => 'general',
        )
      );
    }

    return false;
  }

  public function onMenuInitialize_CoreHelpsupportMobileSettings($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      return array(
        'label' => $row->label,
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'settings',
          'action' => 'general',
        )
      );
    }

    return false;
  }

  public function onMenuInitialize_CoreMiniAuth($row)
  {
		
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      return false;
//       return array(
//         'label' => 'Sign Out',
//         'route' => 'user_logout',
//         'class' => 'no-dloader',
//       );
    } else {
      $request = Zend_Controller_Front::getInstance()->getRequest();
      $moduleName = $request->getModuleName();
      $actionName = $request->getActionName();
      $controllerName = $request->getControllerName();
      
      if($controllerName == 'signup') {
        return array(
          'class' => 'user_auth_link',
          'label' => 'Sign In',
          'route' => 'user_login',
          'params' => array(
            // Nasty hack
            //'return_url' => $uri,
          ),
        );
      } else if($moduleName == 'core' && $controllerName == 'index' && $actionName == 'index') {
        return array(
          'class' => 'user_auth_link',
          'label' => 'Sign In',
          'route' => 'user_login',
          'params' => array(
            // Nasty hack
            //'return_url' => $uri,
          ),
        );
      } else {
        //$uri = !empty($_SESSION['return_url']) ? $_SESSION['return_url'] : '';
				$uri = $_SERVER['REQUEST_URI'];
        if($controllerName != 'auth' && $controllerName != 'signup' && strpos($uri,".js") === false && strpos($uri,".css") === false) {
           //'64-' . base64_encode($_SERVER['REQUEST_URI']);
          $_SESSION['return_url'] = $uri;
        }
        return array(
          'class' => 'user_auth_link',
          'label' => 'Sign In',
          'route' => 'user_login',
          'params' => array(
            // Nasty hack
            //'return_url' => $uri,
          ),
        );
      }
    }
  }

  public function onMenuInitialize_CoreMiniSignup($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity() ) {
      return array(
        'label' => 'Sign Up',
        'route' => 'user_signup',
        'class' => 'user_signup_link',
      );
    }

    return false;
  }

  // user_home
  public function onMenuInitialize_UserHomeView($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    if( $viewer->getIdentity() ) {
      return array(
        'label' => $row->label,
        'icon' => isset($row->params['icon']) ? $row->params['icon'] : '',
        'route' => 'user_profile',
        'params' => array(
          'id' => $viewer->getIdentity()
        )
      );
    }
    return false;
  }

  public function onMenuInitialize_UserHomeEdit($row)
  {
    return array(
      'label' => 'Edit My Profile',
      'class' => 'icon_user_edit',
      'route' => 'user_extended',
      'params' => array(
        'controller' => 'edit',
        'action' => 'profile'
      )
    );
  }
  public function onMenuInitialize_UserDeletePhotos($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    $subject = Engine_Api::_()->core()->getSubject();
    if( !$viewer->isSelf($subject) ) {
      return false;
    }

    if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('album')) {
      return false;
    }

    if( $subject->authorization()->isAllowed($viewer, 'edit') ) {
      return array(
        'label' => 'Delete Profile Photos',
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'edit',
          'action' => 'profile-photos',
        )
      );
    }

    return false;
  }

  // user_profile
  public function onMenuInitialize_UserProfileEdit($row)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    $subject = Engine_Api::_()->core()->getSubject();

    $label = "Edit My Profile";
    if( !$viewer->isSelf($subject) ) {
      $label = "Edit User Profile";
    }
    $auth = $subject->isSuperAdmin() ? $viewer->isSuperAdmin($subject) : 1;
    if( $subject->authorization()->isAllowed($viewer, 'edit') && $auth) {
      return array(
        'label' => $label,
        'class' => 'icon_user_edit',
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'edit',
          'action' => 'profile',
          'id' => ( $viewer->getGuid(false) == $subject->getGuid(false) ? null : $subject->getIdentity() ),
        )
      );
    }

    return false;
  }

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

    // Not logged in
    if( !$viewer->getIdentity() || $viewer->getGuid(false) === $subject->getGuid(false) ) {
      return false;
    }

    // No blocked
    if( $viewer->isBlockedBy($subject) ) {
      return false;
    }

    // Check if friendship is allowed in the network
    $eligible = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('user.friends.eligible', 2);
    if( !$eligible ) {
      return '';
    }

    // check admin level setting if you can befriend people in your network
    else if( $eligible == 1 ) {

      $networkMembershipTable = Engine_Api::_()->getDbtable('membership', 'network');
      $networkMembershipName = $networkMembershipTable->info('name');

      $select = new Zend_Db_Select($networkMembershipTable->getAdapter());
      $select
        ->from($networkMembershipName, 'user_id')
        ->join($networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null)
        ->where("`{$networkMembershipName}`.user_id = ?", $viewer->getIdentity())
        ->where("`{$networkMembershipName}_2`.user_id = ?", $subject->getIdentity())
      ;

      $data = $select->query()->fetch();

      if( empty($data) ) {
        return '';
      }
    }

    // One-way mode
    $direction = (int) Engine_Api::_()->getApi('settings', 'core')->getSetting('user.friends.direction', 1);
    if( !$direction ) {
      $viewerRow = $viewer->membership()->getRow($subject);
      $subjectRow = $subject->membership()->getRow($viewer);
      $params = array();

      // Viewer?
      if( null === $subjectRow ) {
        // Follow
        $params[] = array(
          'label' => 'Follow',
          'class' => 'smoothbox icon_friend_add',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'add',
            'user_id' => $subject->getIdentity()
          ),
        );
      } else if( $subjectRow->resource_approved == 0 ) {
        // Cancel follow request
        $params[] = array(
          'label' => 'Cancel Follow Request',
          'class' => 'smoothbox icon_friend_remove',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'cancel',
            'user_id' => $subject->getIdentity()
          ),
        );
      } else {
        // Unfollow
        $params[] = array(
          'label' => 'Following',
          'class' => 'smoothbox icon_friend_remove',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'remove',
            'user_id' => $subject->getIdentity()
          ),
        );
      }
      // Subject?
      if( null === $viewerRow ) {
        // Do nothing
      } else if( $viewerRow->resource_approved == 0 ) {
        // Approve follow request
        $params[] = array(
          'label' => 'Approve Follow Request',
          'class' => 'smoothbox icon_friend_add',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'confirm',
            'user_id' => $subject->getIdentity()
          ),
        );
      } else {
        // Remove as follower?
        $params[] = array(
          'label' => 'Remove as Follower',
          'class' => 'smoothbox icon_friend_remove',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'remove',
            'user_id' => $subject->getIdentity(),
            'rev' => true,
          ),
        );
      }
      if( engine_count($params) == 1 ) {
        return $params[0];
      } else if( engine_count($params) == 0 ) {
        return false;
      } else {
        return $params;
      }
    }

    // Two-way mode
    else {
      $row = $viewer->membership()->getRow($subject);
      if( null === $row ) {
        // Add
        return array(
          'label' => 'Add Friend',
          'class' => 'smoothbox icon_friend_add',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'add',
            'user_id' => $subject->getIdentity()
          ),
        );
      } else if( $row->user_approved == 0 ) {
        // Cancel request
        return array(
          'label' => 'Cancel Request',
          'class' => 'smoothbox icon_friend_remove',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'cancel',
            'user_id' => $subject->getIdentity()
          ),
        );
      } else if( $row->resource_approved == 0 ) {
        // Approve request
        $respond[] = array(
          'label' => 'Approve Friend Request',
          'class' => 'smoothbox icon_friend_accept',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'confirm',
            'user_id' => $subject->getIdentity()
          ),
        );
				$respond[] = array(
          'label' => 'Ignore Friend Request',
          'class' => 'smoothbox icon_friend_reject',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'reject',
            'user_id' => $subject->getIdentity()
          ),
        );
				return $respond;
      } else {
        // Remove friend
        return array(
          'label' => 'Remove Friend',
          'class' => 'smoothbox icon_friend_remove',
          'route' => 'user_extended',
          'params' => array(
            'controller' => 'friends',
            'action' => 'remove',
            'user_id' => $subject->getIdentity()
          ),
        );
      }
    }
  }

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

    // Can't block self or if not logged in
    if( !$viewer->getIdentity() || $viewer->getGuid() == $subject->getGuid() ) {
      return false;
    }

    if( !Engine_Api::_()->authorization()->getPermission($viewer, 'user', 'block') ) {
      return false;
    }

    if( !Engine_Api::_()->authorization()->getPermission($subject, 'user', 'canblock') ) {
      return false;
    }

    if( !$subject->isBlockedBy($viewer) ) {
      return array(
        'label' => 'Block Member',
        'class' => 'smoothbox icon_block',
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'block',
          'action' => 'add',
          'user_id' => $subject->getIdentity()
        ),
      );
    } else {
      return array(
        'label' => 'Unblock Member',
        'class' => 'smoothbox icon_block',
        'route' => 'user_extended',
        'params' => array(
          'controller' => 'block',
          'action' => 'remove',
          'user_id' => $subject->getIdentity()
        ),
      );
    }
  }

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

    if( !$viewer->getIdentity() ||
      !$subject->getIdentity() ||
      $viewer->isSelf($subject) ) {
      return false;
    } else {
      return array(
        'label' => 'Report',
        'class' => 'smoothbox icon_report',
        'route' => 'default',
        'params' => array(
          'module' => 'core',
          'controller' => 'report',
          'action' => 'create',
          'subject' => $subject->getGuid(),
          'format' => 'smoothbox',
        ),
      );
    }
  }

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

    if( !$subject->authorization()->isAllowed($viewer, 'delete') ) {
      return false;
    }

    $auth = $subject->isSuperAdmin() ? $viewer->isSuperAdmin($subject) : 1;
    if( !$viewer->isAllowed('admin') || !$viewer->isAdmin() || !$auth) {
      return false;
    }
    return array(
      'label' => 'Admin Settings',
      'class' => 'smoothbox icon_edit',
      'route' => 'admin_default',
      'params' => array(
        'module' => 'user',
        'controller' => 'manage',
        'action' => 'edit',
        'id' => $subject->getIdentity(),
        'format' => 'smoothbox',
      ),
    );
  }

	public function onMenuInitialize_UserLocationEdit($row) {

    if(Engine_Api::_()->getApi('settings', 'core')->getSetting('enableglocation', 0) != 1)
      return false;
		
    $viewer = Engine_Api::_()->user()->getViewer();
    $subject = Engine_Api::_()->core()->getSubject();
    $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
    $label = $view->translate('Edit Location');
    $action = 'edit-location';
    $class = '';
    $getLoggedInuserLocation = Engine_Api::_()->getDbTable('locations', 'core')->getLocationData(array('resource_type' => $subject->getType(), 'resource_id' => $subject->getIdentity()));
    if (empty($getLoggedInuserLocation)) {
      $label = $view->translate('Add Location');
      $action = 'add-location';
      $class = 'ajaxsmoothbox';
    }
    if ($subject->authorization()->isAllowed($viewer, 'edit')) {
      return array(
				'label' => $label,
				'route' => 'user_general',
				'icon' => 'fi fi-rr-marker',
				'params' => array(
					'module' => 'user',
					'controller' => 'index',
					'action' => $action,
					'id' => ( $subject->getIdentity() ),
				),
				'class' => $class,
      );
    }
    return false;
  }

	public function onMenuInitialize_CoreMinimenuWallet($row)
  {
		$view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;

		$viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer->getIdentity()) 
      return false;

		if(Engine_Api::_()->getDbtable('gateways', 'payment')->getEnabledWalletGateway() && Engine_Api::_()->getDbtable('gateways', 'payment')->getEnabledGatewayCount() > 0) {

			return array(
				'label' => $view->translate("Wallet") . ': '. Engine_Api::_()->payment()->getCurrencyPrice($viewer->wallet_amount,'','',''),
				'route' => 'default',
				'icon' => 'fi fi-rr-marker',
				'params' => array(
					'module' => 'payment',
					'controller' => 'settings',
					'action' => "wallet",
				),
      );
		}
		return false;
  }
}
