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

class Acppro_Plugin_Core extends Zend_Controller_Plugin_Abstract {

	public function routeShutdown(Zend_Controller_Request_Abstract $request)
  {
		if (strpos($_SERVER['REQUEST_URI'], 'admin') === false) {
      return;
    }
		
    // Check your setting
    if (Engine_Api::_()->user()->getViewer()->isOnlyAdmin() && !Engine_Api::_()->getApi('settings', 'core')->getSetting('acppro.showlicense', 0)) {

      $view = Zend_Registry::isRegistered('Zend_View')
        ? Zend_Registry::get('Zend_View')
        : null;

      if (!$view) {
        return;
      }

      // Hide License Fields + Sidebar License
      $script = "
        en4.core.runonce.add(function() {

          // Hide all license wrappers
          scriptJquery('[id$=\"_licensekey-wrapper\"]').hide();
          scriptJquery('[id*=\"_licensekey-wrapper\"]').hide();
          scriptJquery('._licensekey-wrapper').hide();
					scriptJquery('#domain_name-wrapper').hide();

          // Hide sidebar license key block
          scriptJquery('.admin_header_version_inner li').each(function(){
            if(scriptJquery(this).text().toLowerCase().indexOf('license key') !== -1){
              scriptJquery(this).hide();
            }
          });

        });
      ";

      $view->headScript()->appendScript($script);
    }
  }

	public function onRenderLayoutDefault() {
	
    if( defined('_ENGINE_ADMIN_NEUTER') && _ENGINE_ADMIN_NEUTER ) return;

    $request = Zend_Controller_Front::getInstance()->getRequest();
    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();
    $settings = Engine_Api::_()->getApi('settings', 'core');
    
    $countActiveMembers = Engine_Api::_()->getDbTable('users', 'user')->countActiveMembers();
    $maxusers = Engine_Api::_()->getApi('settings', 'core')->getSetting('acppro.maxusers', 0);
    
    if($maxusers > 0 && $countActiveMembers >= $maxusers) {
      if($module == 'user' && $controller == 'signup' && $action == 'index') {
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
        $redirector->gotoRoute(array(), 'default', false);
      }
      $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
      $script = '
      en4.core.runonce.add(function() {
        scriptJquery ("#core_menu_mini_menu").find(".core_mini_signup").parent().remove();
      });';
      $view->headScript()->appendScript($script);
    }
	}
}
