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

class Gdpr_Plugin_Core
{

  public function onUserLoginAfter($event)
  {
    if (!empty($_GET['restApi']))
      return;
    $user = $event->getPayload();
    if (!empty($_COOKIE['user_popup_consent'])) {
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'gdpr_popup_consent', 1);
    }
    if (!empty($_COOKIE['user_consent'])) {
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'user_consent', 1);
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'user_consent_time', $_COOKIE['user_consent_date']);
    }
  }
  public function onUserSignupAfter($event)
  {
    $user = $event->getPayload();
    if (!empty($_COOKIE['user_consent'])) {
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'user_consent', 1);
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'user_consent_time', $_COOKIE['user_consent_date']);
    }
    if (!empty($_COOKIE['user_popup_consent'])) {
      Engine_Api::_()->getDbTable('settings', 'user')->setSetting($user, 'gdpr_popup_consent', 1);
    }
  }

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

  public function onUserLogoutBefore($event)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    $userConsent = Engine_Api::_()->getDbTable('settings', 'user')->getSetting($viewer, 'user_consent');
    $userConsentTime = Engine_Api::_()->getDbTable('settings', 'user')->getSetting($viewer, 'user_consent_time');
    $gdprPopupConsent = Engine_Api::_()->getDbTable('settings', 'user')->getSetting($viewer, 'gdpr_popup_consent');
    if ($userConsentTime)
      setcookie('user_consent_date', $userConsentTime, time() + (86400 * 30), "/");
    if ($userConsent)
      setcookie('user_consent', $userConsent, time() + (86400 * 30), "/");
    if ($gdprPopupConsent)
      setcookie('user_popup_consent', $gdprPopupConsent, time() + (86400 * 30), "/");
  }

  public function onRenderLayoutDefault($event)
  {
    if (defined('_ENGINE_ADMIN_NEUTER') && _ENGINE_ADMIN_NEUTER)
      return;
    $viewer = Engine_Api::_()->user()->getViewer();
    if ($viewer->getIdentity()) {
      if ($viewer->level_id != 1) {
        $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
        $script = '';
        $script .= '
				en4.core.runonce.add(function() {
					scriptJquery("#gdpr-gdprconsentuser").parent().remove();
				});';
        $view->headScript()->appendScript($script);
      }
    }
  }
}

