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

class Gdpr_AdminSettingsController extends Core_Controller_Action_Admin {

  public function indexAction() {

    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_settings');

    $this->view->form = $form = new Gdpr_Form_Admin_Global();

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();

			include_once APPLICATION_PATH . "/application/modules/Gdpr/controllers/License.php";
      if (Engine_Api::_()->getApi('settings', 'core')->getSetting('gdpr.pluginactivated')) {
				foreach ($values as $key => $value) {
					if (Engine_Api::_()->getApi('settings', 'core')->hasSetting($key))
						Engine_Api::_()->getApi('settings', 'core')->removeSetting($key);
					Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
				}
				$form->addNotice('Your changes have been saved.');
				if($error)
					$this->_helper->redirector->gotoRoute(array());
			}
  	}
	}	

  function cookieAction() {
  
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_cookir');
    
    $this->view->form = $form = new Gdpr_Form_Admin_Cookie();
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      $values['gdpr_banneroption'] = json_encode($values['gdpr_banneroption']);
      foreach ($values as $key => $value) {
        Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
      }
      $form->addNotice('Your changes have been saved.');
    }
  }

  function dpoAction() {
    
    $settings = Engine_Api::_()->getApi('settings', 'core');
    
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_request');
    
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values['selectedItems'] as $value) {
        if( $_POST['delete'] == 'delete' ) {
          Engine_Api::_()->getItem('gdpr_content', $value)->delete();
        }
      }
    }

    $this->view->formFilter = $formFilter = new Gdpr_Form_Admin_Filter();
    $formFilter->populate($this->_getAllParams());
    
    $table = Engine_Api::_()->getDbTable('contents', 'gdpr');
    $select = $table->select()->where('type =?', 'gdpr')->order('creation_date DESC');

    if (isset($_POST['dpoemails']))
      $settings->setSetting('gdpr.dpoemails', $_POST['dpoemails']);

    $this->view->formEmail = $formEmail = new Gdpr_Form_Admin_Dpoemails();
    $formEmail->populate(array('dpoemails' => $settings->getSetting('gdpr.dpoemails')));

    if (!empty($_GET['name']))
      $select->where('name LIKE ("' . $_GET['name'] . '%")');

    if (!empty($_GET['email']))
      $select->where('email LIKE ("' . $_GET['email'] . '%")');

    $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator;
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }

  function viewAction() {
    $id = $this->_getParam('id');
    $this->view->item = $item = Engine_Api::_()->getItem('gdpr_content', $id);
  }
  
  function noteAction() {
  
    $id = $this->_getParam('id');
    $this->view->item = $item = Engine_Api::_()->getItem('gdpr_content', $id);
    
    $this->view->form = $form = new Gdpr_Form_Admin_Note();
    $form->populate(array('note' => $item->note));
    
    if (!$this->getRequest()->isPost())
      return;
      
    if (!$form->isValid($this->getRequest()->getPost()))
      return;
      
    $values = $form->getValues();
    $item->note = $values['note'];
    $item->save();

    $this->_forward('success', 'utility', 'core', array(
      'smoothboxClose' => 10,
      'parentRefresh' => 10,
      'messages' => array('Note saved successfully.')
    ));
  }
  
  function serviceApprovedAction()
  {
    $id = $this->_getParam('id');
    $service = Engine_Api::_()->getItem('gdpr_service', $id);
    $service->enabled = !$service->enabled;
    $service->save();
    header("Location:" . $_SERVER['HTTP_REFERER']);
    exit();
  }
  public function deleteServiceAction()
  {
    // In smoothbox
    $this->_helper->layout->setLayout('admin-simple');
    $this->view->form = $form = new Gdpr_Form_Admin_Delete();
    $form->setTitle('Delete This Service?');
    $form->setDescription('Are you sure that you want to delete this Service? It will not be recoverable after being deleted.');
    $form->submit->setLabel('Delete');
    $id = $this->_getParam('id');
    $this->view->item_id = $id;
    // Check post
    if ($this->getRequest()->isPost()) {
      $deleteform = Engine_Api::_()->getItem('gdpr_service', $id)->delete();
      $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array('Service Deleted Successfully.')
      ));
    }
  }
  
  public function deleteAction()
  {
    // In smoothbox
    $this->_helper->layout->setLayout('admin-simple');
    $this->view->form = $form = new Gdpr_Form_Admin_Delete();
    $form->setTitle('Delete This Entry?');
    $form->setDescription('Are you sure that you want to delete this Entry? It will not be recoverable after being deleted.');
    $form->submit->setLabel('Delete');
    $id = $this->_getParam('id');
    $this->view->item_id = $id;
    // Check post
    if ($this->getRequest()->isPost()) {
      $deleteform = Engine_Api::_()->getItem('gdpr_content', $id)->delete();
      $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array('Entry Deleted Successfully.')
      ));
    }
  }
  
  function replyAction()
  {
    $id = $this->_getParam('id');
    $this->view->item = $item = Engine_Api::_()->getItem('gdpr_content', $id);

    $this->_helper->layout->setLayout('admin-simple');
    $this->view->form = $form = new Gdpr_Form_Admin_Mail();
    if ($item->note)
      $form->removeElement('note');
    if (!$this->getRequest()->isPost())
      return;
    if (!$form->isValid($this->getRequest()->getPost()))
      return;
    $values = $form->getValues();
    unset($values['body_text']);

    $core_mail = Engine_Api::_()->getApi('mail', 'core');
    $core_mail->sendSystem($item->email, 'gdpr_admin_reply', array(
      'subject' => $values['subject'],
      'body' => $values['body'],
    )
    );
    if (!empty($values['note']))
      $item->note = $values['body'];
    $item->replied = 1;
    $item->save();

    $this->_forward('success', 'utility', 'core', array(
      'smoothboxClose' => 10,
      'parentRefresh' => 10,
      'messages' => array('You have successfully reply to member.')
    ));
  }

  function privacyAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_privacyse');
    
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values['selectedItems'] as $value) {
        if( $_POST['delete'] == 'delete' ) {
          Engine_Api::_()->getItem('gdpr_service', $value)->delete();
        }
      }
    }

    $select = Engine_Api::_()->getDbTable('services', 'gdpr')->select();
    $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator;
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }
  
  function addServiceAction()
  {
    $id = $this->_getParam('id');
    $this->view->item = $service = Engine_Api::_()->getItem('gdpr_service', $id);

    $this->_helper->layout->setLayout('admin-simple');
    $this->view->form = $form = new Gdpr_Form_Admin_Service();
    if ($id) {
      $form->setTitle('Edit This Service');
      $form->setDescription('Below you can edit this 3rd party service which is used on your website directly or in any plugin / module installed on your website.');
    }

    if (!$this->getRequest()->isPost()) {
      if ($id)
        $form->populate($service->toArray());
      return;
    }
    if (!$form->isValid($this->getRequest()->getPost()))
      return;
    $values = $form->getValues();

    $db = Engine_Api::_()->getItemTable('gdpr_service')->getAdapter();
    $db->beginTransaction();
    try {
      $table = Engine_Api::_()->getItemTable('gdpr_service');
      if (!$id)
        $service = $table->createRow();
      $service->setFromArray($form->getValues());
      $service->save();
      $db->commit();
    } catch (Exception $e) {
      throw $e;
    }

    $this->_forward('success', 'utility', 'core', array(
      'smoothboxClose' => 10,
      'parentRefresh' => 10,
      'messages' => array('Service successfully saved.')
    ));
  }
  
  function subjectAccessAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_accessrequest');
    
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values['selectedItems'] as $value) {
        if( $_POST['delete'] == 'delete' ) {
          Engine_Api::_()->getItem('gdpr_content', $value)->delete();
        }
      }
    }
    
    $this->view->formFilter = $formFilter = new Gdpr_Form_Admin_Filter();
    $formFilter->populate($this->_getAllParams());
    
    $table = Engine_Api::_()->getDbTable('contents', 'gdpr');
    $select = $table->select()->where('type =?', 'service-access')->order('creation_date DESC');

    if (!empty($_GET['name']))
      $select->where('name LIKE ("' . $_GET['name'] . '%")');
      
    if (!empty($_GET['email']))
      $select->where('email LIKE ("' . $_GET['email'] . '%")');
      
    $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator;
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }

  function unsubscribeAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_unsubscribe');
    
    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values['selectedItems'] as $value) {
        if( $_POST['delete'] == 'delete' ) {
          Engine_Api::_()->getItem('gdpr_content', $value)->delete();
        }
      }
    }

    $this->view->formFilter = $formFilter = new Gdpr_Form_Admin_Filter();
    $formFilter->populate($this->_getAllParams());
    
    $table = Engine_Api::_()->getDbTable('contents', 'gdpr');
    $select = $table->select()->where('type =?', 'unsubscribe')->order('creation_date DESC');

    if (!empty($_GET['name']))
      $select->where('name LIKE ("' . $_GET['name'] . '%")');
      
    if (!empty($_GET['email']))
      $select->where('email LIKE ("' . $_GET['email'] . '%")');
      
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator;
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }

  function forgotMeAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_forgotme');

    if ($this->getRequest()->isPost()) {
      $values = $this->getRequest()->getPost();
      foreach ($values['selectedItems'] as $value) {
        if( $_POST['delete'] == 'delete' ) {
          Engine_Api::_()->getItem('gdpr_content', $value)->delete();
        }
      }
    }

    $this->view->formFilter = $formFilter = new Gdpr_Form_Admin_Filter();
    $formFilter->populate($this->_getAllParams());
    
    $table = Engine_Api::_()->getDbTable('contents', 'gdpr');
    $select = $table->select()->where('type =?', 'forgot')->order('creation_date DESC');

    if (!empty($_GET['name']))
      $select->where('name LIKE ("' . $_GET['name'] . '%")');
      
    if (!empty($_GET['email']))
      $select->where('email LIKE ("' . $_GET['email'] . '%")');
      
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }
  
  function auditLogAction() {
  
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('gdpr_admin_main', array(), 'gdpr_admin_main_audit');

    $this->view->formFilter = $formFilter = new Gdpr_Form_Admin_Audit();
    $formFilter->populate($this->_getAllParams());

    $table = Engine_Api::_()->getDbTable('audits', 'gdpr');
    $select = $table->select()->where('email =?', !empty($_GET['email']) ? $_GET['email'] : "as");

    if (!empty($_GET['email']))
      $select->where('email LIKE ("' . $_GET['email'] . '%")');

    $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator;
    $paginator->setItemCountPerPage(20);
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));
  }
}
