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

class Pulse_Plugin_Core extends Zend_Controller_Plugin_Abstract {

	public function onRenderLayoutDefault(){

    $request = Zend_Controller_Front::getInstance()->getRequest();
    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();
    $settings = Engine_Api::_()->getApi('settings', 'core');
    $changelanding = $settings->getSetting('pulse.changelanding', 0);
    $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
    if(!empty($changelanding) && $module == 'core' && $controller == 'index' && $action == 'index') {
      $script = '
        en4.core.runonce.add(function() {
          scriptJquery ("body").addClass("pulse_landingpage_" + '.$changelanding.');
        });';
      $view->headScript()->appendScript($script);
		}

		$script = '
		en4.core.runonce.add(function(e) {
      var height = scriptJquery(".layout_page_header").height();
			if(document.getElementById("global_wrapper")) {
				scriptJquery("#global_wrapper").css("margin-top", height+"px");
			}
		});';
    $view->headScript()->appendScript($script);

		$viewer = Engine_Api::_()->user()->getViewer();
		if($viewer->getIdentity()) {
      $options = unserialize($settings->getSetting("pulse.headerloggedinoptions",'a:4:{i:0;s:6:"search";i:1;s:8:"miniMenu";i:2;s:8:"mainMenu";i:3;s:4:"logo";}'));
    } else {
      $options = unserialize($settings->getSetting("pulse.headernonloggedinoptions",'a:4:{i:0;s:6:"search";i:1;s:8:"miniMenu";i:2;s:8:"mainMenu";i:3;s:4:"logo";}'));
    }
		// if(engine_in_array('mainMenu',$options)){
		// 	$script = '
    //     en4.core.runonce.add(function() {
    //       scriptJquery ("body").addClass("sidebarmenu_enabled");
    //     });';
    //   $view->headScript()->appendScript($script);
		// } else {
		// 	$script = '
		// 	en4.core.runonce.add(function() {
		// 		scriptJquery ("body").addClass("sidebarmenu_disabled");
		// 	});';
		// 	$view->headScript()->appendScript($script);
		// }

		$script = '
			en4.core.runonce.add(function() {
				scriptJquery ("body").addClass("color_scheme_" + '.Engine_Api::_()->getApi('settings', 'core')->getSetting('pulse.theme.color', 1).');
			});';
		$view->headScript()->appendScript($script);
		
    //Theme mode active

    $script = "var isThemeModeActive = true;";
    $view->headScript()->appendScript($script);

		$usegoogleFont = $settings->getSetting('pulse.googlefonts', 0);
		if (!empty($usegoogleFont)) {
			$fonts = [];
			// Helper function
			$addFont = function($family, $variants) use (&$fonts) {
				$family = trim(str_replace('"', '', $family));
				$variants = trim($variants);
				if (!$family) return;
				if (!isset($fonts[$family])) {
					$fonts[$family] = [];
				}
				if ($variants) {
					$variantsArr = explode(',', $variants);
					$fonts[$family] = array_unique(array_merge($fonts[$family], $variantsArr));
				}
			};
			
			// Collect fonts
			$addFont($settings->getSetting('pulse.googlebody.fontfamily'), $settings->getSetting('pulse.googlebody.fontfamilyvariants'));

			$addFont($settings->getSetting('pulse.googleheading.fontfamily'),$settings->getSetting('pulse.googleheading.fontfamilyvariants'));

			$addFont($settings->getSetting('pulse.googletab.fontfamily'), $settings->getSetting('pulse.googletab.fontfamilyvariants'));

			// Build final string
			if (!empty($fonts)) {
				$fontStrings = [];
				foreach ($fonts as $family => $variants) {
					if (!empty($variants)) {
						sort($variants);
						$fontStrings[] = $family . ':' . implode(',', $variants);
					} else {
						$fontStrings[] = $family;
					}
				}
				$url = 'https://fonts.googleapis.com/css?family=' . implode('|', $fontStrings);
				$view->headLink()->appendStylesheet($url);
			}
		}
	}

  public function onRenderLayoutDefaultSimple($event) {
    return $this->onRenderLayoutDefault($event, 'simple');
  }
}
