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

class Browserpush_AdminSettingsController extends Core_Controller_Action_Admin
{

  public function indexAction()
  {

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

    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('browserpush.defaultsettings')) {
      $this->setDefaultSettings();
    }
    
    $this->view->form = $form = new Browserpush_Form_Admin_Settings_General();

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

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

  public function reportsAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('browserpush_admin_main', array(), 'browserpush_admin_main_reports');

    $this->view->formFilter = $formFilter = new Browserpush_Form_Admin_Settings_Reports();

    // Process form
    $values = array();
    if ($formFilter->isValid($this->_getAllParams()))
      $values = $formFilter->getValues();

    foreach ($values as $key => $value) {
      if (null === $value) {
        unset($values[$key]);
      }
    }

    $values = array_merge(array(
      'order' => 'notification_id',
      'order_direction' => 'DESC',
    ), $values);
    $this->view->assign($values);

    $table = Engine_Api::_()->getDbTable('notifications', 'browserpush');
    $tableName = $table->info('name');

    $tablesch = Engine_Api::_()->getDbTable('scheduleds', 'browserpush');
    $selectSent = $tablesch->select()->from($tablesch->info('name'), '*')->where('sent =?', '1');
    if (!empty($values['from_date']) && !empty($values['to_date'])) {
      $startTime = date('Y-m-d', strtotime($values['from_date']));
      $endTime = date('Y-m-d', strtotime($values['to_date']));
      $selectSent->where("DATE(creation_date) between ('$startTime') and ('$endTime')");
    }
    $this->view->sent = $tablesch->fetchAll($selectSent);

    $selectReceivers = $table->select()->from($table->info('name'), '*')->where('param =?', '1');
    if (!empty($values['from_date']) && !empty($values['to_date'])) {
      $startTime = date('Y-m-d', strtotime($values['from_date']));
      $endTime = date('Y-m-d', strtotime($values['to_date']));
      $selectReceivers->where("DATE(creation_date) between ('$startTime') and ('$endTime')");
    }
    $this->view->receivers = $table->fetchAll($selectReceivers);


    $selectClickeds = $table->select()->from($table->info('name'), '*')->where('param =?', '2');
    if (!empty($values['from_date']) && !empty($values['to_date'])) {
      $startTime = date('Y-m-d', strtotime($values['from_date']));
      $endTime = date('Y-m-d', strtotime($values['to_date']));
      $selectClickeds->where("DATE(creation_date) between ('$startTime') and ('$endTime')");
    }
    $this->view->clicked = $table->fetchAll($selectClickeds);
  }

  public function subscriberAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('browserpush_admin_main', array(), 'browserpush_admin_main_managesubscriber');

    $this->view->formFilter = $formFilter = new Browserpush_Form_Admin_Settings_Filter();

    // Process form
    $values = array();
    if ($formFilter->isValid($this->_getAllParams()))
      $values = $formFilter->getValues();

    foreach ($values as $key => $value) {
      if (null === $value) {
        unset($values[$key]);
      }
    }

    $values = array_merge(array(
      'order' => 'user_id',
      'order_direction' => 'DESC',
    ), $values);
    $this->view->assign($values);

    $table = Engine_Api::_()->getDbTable('tokens', 'browserpush');
    $tableName = $table->info('name');
    $user = Engine_Api::_()->getDbTable('users', 'user');
    $userName = $user->info('name');
    $select = $user->select()->from($userName, array('user_id', 'email', 'username', 'displayname'))
      ->setIntegrityCheck(false)
      ->joinRight($tableName, $tableName . '.user_id = ' . $userName . '.user_id', array('ip_address', 'creation_date', 'access_token', 'test_user', 'user_agent', 'token_id', 'browser' => new Zend_Db_Expr('GROUP_CONCAT(browser)')))
      ->group("$tableName.user_id")
      ->group("access_token");
    if (!empty($values['ip_address']))
      $select->where('ip_address LIKE ?', '%' . $values['ip_address'] . '%');
    if (!empty($values['user_agent']))
      $select->where('user_agent LIKE ?', '%' . $values['user_agent'] . '%');
    if (!empty($values['displayname']))
      $select->where('displayname LIKE ?', '%' . $values['displayname'] . '%');
    if (!empty($values['browser']))
      $select->where('browser LIKE ?', '%' . $values['browser'] . '%');
    if (!empty($values['username']))
      $select->where('username LIKE ?', '%' . $values['username'] . '%');

    if (!empty($values['email']))
      $select->where('email LIKE ?', '%' . $values['email'] . '%');
    $select->order($tableName . '.token_id DESC');

    //Make paginator
    $page = $this->_getParam('page', 1);
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $this->view->paginator = $paginator->setCurrentPageNumber($page);
    $paginator->setItemCountPerPage(50);
  }
  public function testUserAction()
  {
    $token_id = $this->_getParam('token_id', false);
    $id = $this->_getParam('id', false);
    $action = $this->_getParam('act', false);
    $db = Engine_Db_Table::getDefaultAdapter();
    /*if($id)
      $where = 'user_id = '.$id;
    else*/
    $where = 'token_id = ' . $token_id;
    if ($action == 'remove')
      $condition = 0;
    else
      $condition = 1;
    $db->query("UPDATE engine4_browserpush_tokens SET test_user = " . $condition . " WHERE " . $where);
    header("Location:" . $_SERVER['HTTP_REFERER']);
    die;
  }
  public function fbSettingsAction()
  {

    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('browserpush_admin_main', array(), 'browserpush_admin_main_fbsettings');
    $this->view->form = $form = new Browserpush_Form_Admin_Settings_Fbsettings();
    if (_ENGINE_ADMIN_NEUTER) {
      $form->populate(array(
        'browserpush_serverkey' => '******',
        'browserpush_snippet' => '******',
      ));
    }
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      foreach ($values as $key => $value) {
        Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
      }

      $this->firebaseMessageFile();

      $form->addNotice('Your changes have been saved.');
    }
  }

  function firebaseMessageFile()
  {

    $string = "
// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/9.0.2/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.0.2/firebase-messaging-compat.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp(" . Engine_Api::_()->getApi('settings', 'core')->getSetting('browserpush.serverkey') . ");

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.onBackgroundMessage(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = payload.data.title;
  const notificationOptions = {
    icon: payload.data.icon,
    body: payload.data.body
  };

  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});
// [END background_handler]
";

    // Specify the file path
    $filePath = APPLICATION_PATH . '/firebase-messaging-sw.js';

    if (file_exists($filePath) && is_readable($filePath)) {
      // Open the file for writing (create if it doesn't exist)
      $file = fopen($filePath, 'w');

      // Check if file opening was successful
      if ($file) {
        // Write content to the file
        fwrite($file, $string);

        // Close the file
        fclose($file);
      }
    }
  }

  public function welcomeAction()
  {
    $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('browserpush_admin_main', array(), 'browserpush_admin_main_welcome');
    $this->view->form = $form = new Browserpush_Form_Admin_Settings_Welcome();

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
      $values = $form->getValues();
      $image = '';
      if (!empty($values['remove_icon_icon']))
        Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush_welcomeicon', 0);
      foreach ($values as $key => $value) {
        if ($key == 'icon' && $form->icon->getValue() != '') {
          $key = 'browserpush_welcomeicon';
          $file_ext = pathinfo($form->icon);
          $file_ext = $file_ext['extension'];
          $storage = Engine_Api::_()->getItemTable('storage_file');
          $storageObject = $storage->createFile($form->icon, array(
            'parent_id' => '1',
            'parent_type' => 'browserpush_token',
            'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
          ));
          // Remove temporary file
          //@unlink($file['tmp_name']);
          if ($storageObject->getIdentity()) {
            $file = Engine_Api::_()->getItemTable('storage_file')->getFile($storageObject->getIdentity());
            if ($file) {
              $image =  $file->map();
              $baseURL = (!empty($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"] == 'on')) ? "https://" : 'http://';
              $baseURL = $baseURL . $_SERVER['HTTP_HOST'];
              if (strpos($image, 'http') === false)
                $image = $baseURL . $image;
            }
          }
          $value = $image;
        }
        if (Engine_Api::_()->getApi('settings', 'core')->hasSetting($key))
          Engine_Api::_()->getApi('settings', 'core')->removeSetting($key);
        if (!$value && !strlen($value))
          continue;
        Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
      }
      $form->addNotice('Your changes have been saved.');
      $this->_helper->redirector->gotoRoute(array());
    }
  }

  public function removesubscriberAction()
  {
    // In smoothbox
    $this->_helper->layout->setLayout('admin-simple');
    $this->view->token_id = $token_id = $this->_getParam('token_id');

    // Check post
    if ($this->getRequest()->isPost()) {
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();

      try {
        $token = Engine_Api::_()->getItem('browserpush_token', $token_id);
        $token->delete();
        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }

      $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array('You have Successfully removed subscriber from list.')
      ));
    }

    // Output
    $this->renderScript('admin-settings/removesubscriber.tpl');
  }

  public function notificationAction()
  {

    $this->view->user_id = $user_id = $this->_getParam('token_id', false);
    if (!$user_id)
      $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('browserpush_admin_main', array(), 'browserpush_admin_main_sendnoti');
    $this->view->form = $form = new Browserpush_Form_Admin_Settings_Notification();
    if (!empty($user_id)) {
      $form->setDescription('Here you can configure the push notification message and send to this subscriber. You can also upload image and add URL to your message. You can also test the Push Notification by sending it to Test users first by using the "Send To Test Users" button.');
    }
    if ($user_id) {
      $this->view->isPopulated = true;
      $this->view->toObject = Engine_Api::_()->getItem('user', $user_id);
      $form->criteria->setAttrib('disabled', true);
      $form->token_id->setValue($user_id);
      $form->getElement('criteria')->addMultiOption($user_id, 'Token Id');
      $form->criteria->setValue($user_id);
      //$form->toValues->setValue($user_id);
      $_POST['criteria'] = $user_id;
    }

    if ($this->getRequest()->isPost() && $form->isValid($_POST)) {

      $values = $form->getValues();
      $title = $values['title'];
      $body = $values['description'];
      $href = $values['link'];
      $image = '';
      if (!empty($_FILES['icon']['name'])) {
        $file_ext = pathinfo($_FILES['icon']['name']);
        $file_ext = $file_ext['extension'];
        $storage = Engine_Api::_()->getItemTable('storage_file');
        $storageObject = $storage->createFile($form->icon, array(
          'parent_id' => '1',
          'parent_type' => 'browserpush_token',
          'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
        ));
        // Remove temporary file
        //@unlink($file['tmp_name']);
        if ($storageObject->getIdentity()) {
          $file = Engine_Api::_()->getItemTable('storage_file')->getFile($storageObject->getIdentity());
          if ($file) {
            $image =  $file->map();
            $baseURL = (!empty($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"] == 'on')) ? "https://" : 'http://';
            $baseURL = $baseURL . $_SERVER['HTTP_HOST'];
            if (strpos($image, 'http') === false)
              $image = $baseURL . $image;
          }
        }
      }
      $params = '';
      if (!empty($_POST['test_user'])) {
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('test_user' => true));
      } else if (!empty($_POST['token_id'])) {
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('token_id' => $_POST['token_id']));
      } else if ($values['criteria'] == 'all') {
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens();
      } else if ($values['criteria'] == 'memberlevel') {
        $params = $level = $values['memberlevel'];
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('level' => $level));
      } else if ($values['criteria'] == 'network') {
        $params = $network = $values['network'];
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('network' => $network));
      } else if ($values['criteria'] == 'user') {
        $params = $user_ids = $values['toValues'];
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('user_ids' => $user_ids));
      } else
        $tokens = Engine_Api::_()->getDbTable('tokens', 'browserpush')->getTokens(array('browser' => $values['criteria']));

      if (empty($_POST['test_user'])) {


        $scheduleds = Engine_Api::_()->getDbTable('scheduleds', 'browserpush');

        $row = $scheduleds->createRow();
        $row->title = $title;
        $row->description = $body;
        $row->file_id = (!empty($storageObject) ? $storageObject->getIdentity() : 0);
        $row->criteria = $values['criteria'];
        $row->param = $params;
        $row->link = $href;
        $row->scheduled_time = '';
        $row->sent = '1';
        $row->creation_date = date('Y-m-d H:i:s');
        $row->save();

        Engine_Api::_()->browserpush()->sendPush(array('title' => strip_tags($title), 'body' => $body, 'icon' => $image, 'click_action' => $href, 'scheduled_id' => $row->getIdentity()), $tokens);
        //$db = Engine_Db_Table::getDefaultAdapter();
        //$db->query("INSERT INTO `engine4_browserpush_scheduleds`(`title`, `description`, `file_id`, `criteria`, `param`, `link`, `scheduled_time`, `sent`, `creation_date`) VALUES ('".$title."','".$body."','".(!empty($storageObject) ? $storageObject->getIdentity() : 0)."','".$values['criteria']."','".$params."','".$href."','','1','".date('Y-m-d H:i:s')."')");
      } else {
        Engine_Api::_()->browserpush()->sendPush(array('title' => strip_tags($title), 'body' => $body, 'icon' => $image, 'click_action' => $href), $tokens);
        echo "1";
        die;
      }
      if ($user_id)
        $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => 10,
          'parentRefresh' => false,
          'messages' => array('Notification Sent Successfully.')
        ));
      $form->reset();
      $form->addNotice('Browser Notification Sent Successfully.');
    }
  }

  public function setDefaultSettings()
  {
    //domain name
    $domain = $_SERVER['HTTP_HOST'];
    $domain = explode('.', $domain);
    $domain = array_reverse($domain);
    $domain = isset($domain[1]) ? $domain[1] : Zend_Registry::get('StaticBaseUrl');
    if (!$domain)
      $domain = str_replace('/', '', Zend_Registry::get('StaticBaseUrl'));

    Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush_welcomeenable', 1);

    $site_title = Engine_Api::_()->getApi('settings', 'core')->getSetting('core_general_site_title', "My Community");
    $title = 'Welcome to ' . $site_title;
    Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush_welcometitle', $title);

    $description = 'Thanks for subscribing and welcome you to ' . $site_title . '!';
    Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush_welcomedescription', $description);

    $baseURL = (!empty($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"] == 'on')) ? "https://" : 'http://';
    $website = $baseURL . $_SERVER['HTTP_HOST'] . Zend_Registry::get('StaticBaseUrl');
    Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush_welcomelink', $website);

    $PathFile = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'Browserpush' . DIRECTORY_SEPARATOR . "externals" . DIRECTORY_SEPARATOR . "images"  . DIRECTORY_SEPARATOR;
    if (is_file($PathFile . "welcome.png")) {
      $image = $PathFile . "welcome.png";
      $key = 'browserpush_welcomeicon';
      $file_ext = pathinfo($image);
      $file_ext = $file_ext['extension'];
      $storage = Engine_Api::_()->getItemTable('storage_file');
      $storageObject = $storage->createFile($image, array(
        'parent_id' => '1',
        'parent_type' => 'browserpush_token',
        'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
      ));
      // Remove temporary file
      //@unlink($file['tmp_name']);
      if ($storageObject->getIdentity()) {
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($storageObject->getIdentity());
        if ($file) {
          $image =  $file->map();
          $baseURL = (!empty($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"] == 'on')) ? "https://" : 'http://';
          $baseURL = $baseURL . $_SERVER['HTTP_HOST'];
          if (strpos($image, 'http') === false)
            $image = $baseURL . $image;
        }
      }
      $value = $image;
      Engine_Api::_()->getApi('settings', 'core')->setSetting($key, $value);
    }
    Engine_Api::_()->getApi('settings', 'core')->setSetting('browserpush.defaultsettings', 1);
  }
}
