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

class Elpis_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {
    $db = $this->getDb();

    if($this->_databaseOperationType != 'upgrade') {

      // landing page
      $select = new Zend_Db_Select($db);
      $select
          ->from('engine4_core_pages')
          ->where('name = ?', 'core_index_index')
          ->limit(1);
      $pageId = $select->query()->fetchObject()->page_id;
      
      $select = new Zend_Db_Select($db);
      $select
          ->from('engine4_core_content')
          ->where('page_id = ?', $pageId)
          ->where('type = ?', 'widget')
          ->where('name = ?', 'core.landing-page-banner');
      $info = $select->query()->fetch();
      if(!empty($info) ) {
        // Insert banner
        $db->insert('engine4_core_banners', array(
          'name' => 'core',
          'module' => 'core',
          'title' => 'Welcome to our Network',
					'caption' => 'Welcome to our Network',
          'body' => 'We offer everyone the opportunity to engage and discover the world with fun and positivity.',
          'photo_id' => 0,
          'params' => '{"label":"Join","uri":"signup"}',
          'custom' => 0
        ));
        $bannerId = $db->lastInsertId();
        if( $bannerId ) {
          $db->query('UPDATE `engine4_core_content` SET `params` = \'{"bannerId":"'.$bannerId.'","height":"550","title":"","nomobile":"0","name":"core.landing-page-banner"}\' WHERE `engine4_core_content`.`name` = "core.landing-page-banner" AND `engine4_core_content`.`page_id` = "3";');
        }
      }
    

      //Header work
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "inspira.header";');
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "harmony.header";');
			$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "pulse.header";');
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.header";');
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-main";');
  		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-mini";');
  		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-logo";');
  		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.search-mini";');
      $parent_content_id = $db->select()
  		        ->from('engine4_core_content', 'content_id')
  		        ->where('type = ?', 'container')
  		        ->where('page_id = ?', '1')
  		        ->where('name = ?', 'main')
  		        ->limit(1)
  		        ->query()
  		        ->fetchColumn();
  		if($parent_content_id) {
  		  $db->insert('engine4_core_content', array(
          'type' => 'widget',
          'name' => 'core.menu-mini',
          'page_id' => 1,
          'parent_content_id' => $parent_content_id,
          'order' => 9,
        ));
        $db->insert('engine4_core_content', array(
          'type' => 'widget',
          'name' => 'core.search-mini',
          'page_id' => 1,
          'parent_content_id' => $parent_content_id,
          'order' => 10,
        ));
  			$db->insert('engine4_core_content', array(
  		      'type' => 'widget',
  		      'name' => 'core.menu-logo',
  		      'page_id' => 1,
  		      'parent_content_id' => $parent_content_id,
  		      'order' => 11,
  		  ));
  		  $db->insert('engine4_core_content', array(
  		      'type' => 'widget',
  		      'name' => 'core.menu-main',
  		      'page_id' => 1,
  		      'parent_content_id' => $parent_content_id,
  		      'order' => 12,
  		  ));
  	  }
  	  
  	  //Footer
  	  $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "inspira.footer";');
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "harmony.footer";');
			$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "pulse.footer";');
      $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.footer";');
      $db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `engine4_core_content`.`name` = "core.menu-footer";');
  		$parent_content_id = $db->select()
  		        ->from('engine4_core_content', 'content_id')
  		        ->where('type = ?', 'container')
  		        ->where('page_id = ?', '2')
  		        ->where('name = ?', 'main')
  		        ->limit(1)
  		        ->query()
  		        ->fetchColumn();
  		if(!empty($parent_content_id)) {
  			$db->insert('engine4_core_content', array(
          'type' => 'widget',
          'name' => 'core.menu-footer',
          'page_id' => 2,
          'parent_content_id' => $parent_content_id,
          'order' => 9,
        ));
  	  }

      //Theme Enable
      $db->query('INSERT IGNORE INTO `engine4_core_themes` (`name`, `title`, `description`, `active`) VALUES
      ("elpis", "Elpis", "Elpis", 0);');
      $select = new Zend_Db_Select($db);
      $select->from('engine4_core_themes', 'name')
              ->where('active = ?', 1)
              ->limit(1);
      $themeActive = $select->query()->fetch();
      if($themeActive) {
        $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '0' WHERE  `engine4_core_themes`.`name` ='".$themeActive['name']."' LIMIT 1");
        $db->query("UPDATE  `engine4_core_modules` SET  `enabled` =  '0' WHERE  `engine4_core_modules`.`name` ='".$themeActive['name']."' LIMIT 1");
        $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '1' WHERE  `engine4_core_themes`.`name` ='elpis' LIMIT 1");
      }
    }

    //Theme Color Default Installation
    $theme = APPLICATION_PATH . '/application/themes/elpis';
    @chmod($theme, 0777);
    $filename = $theme . '/theme-variables.css';
    $fileExists = @file_exists($filename);
    if (empty($fileExists)) {
      @chmod($theme, 0777);
      $fh = @fopen($filename, 'w');
      $constant = ':root {
        --elpis-theme-color:#2491EB;
        --elpis-body-background-color:#EFF4FB;
        --elpis-font-color:#111111;
        --elpis-font-color-light:#808D97;
        --elpis-links-color:#2491EB;
        --elpis-links-hover-color:#2491EB;
        --elpis-headline-color:#1c2735;
        --elpis-border-color:#e2e4e6;
        --elpis-box-background-color:#FFFFFF;
        --elpis-box-background-color-alt:#f2f2f2;
        --elpis-comments-background-color:#FFFFFF;
        --elpis-input-background-color:#FFFFFF;
        --elpis-input-font-color:#111111;
        --elpis-input-border-color:#e4e4e4;
        --elpis-button-background-color:#2491EB;
        --elpis-button-background-color-hover:#59B4FF;
        --elpis-button-font-color:#FFFFFF;
        --elpis-button-border-color:#2491EB;
        --elpis-header-background-color:#FFFFFF;
        --elpis-mainmenu-background-color:#FFFFFF;
        --elpis-mainmenu-links-color:#002646;
        --elpis-mainmenu-links-hover-color:#FFFFFF;
        --elpis-mainmenu-links-hover-background-color:#2491EB;
        --elpis-minimenu-links-color:#FFFFFF;
        --elpis-minimenu-links-background-color:#2491EB;
        --elpis-minimenu-signup-background-color:#002646;
        --elpis-minimenu-signup-font-color:#FFFFFF;
        --elpis-footer-background-color:#FFFFFF;
        --elpis-footer-font-color:#676767;
        --elpis-footer-links-color:#676767;
        --elpis-footer-border-color:#E4E4E4;
        --elpis-button-font-color-hover:#FFFFFF;
        --elpis-button-border-color-hover:#59B4FF;
        --elpis-secondary-button-background-color:#f2f2f2;
        --elpis-secondary-button-background-color-hover:#e5e5e5;
        --elpis-secondary-button-font-color:#212529;
        --elpis-secondary-button-font-color-hover:#212529;
        --elpis-secondary-button-border-color:#E4E6EB;
        --elpis-secondary-button-border-color-hover:#d8dadf;
      }';
      @fwrite($fh, $constant);
      @chmod($filename, 0777);
      @fclose($fh);
      @chmod($filename, 0777);
      @chmod($filename, 0777);
      
      $db->query('INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES
      ("elpis.theme.color", 1);');
    }
    //Theme Color Default Installation

    parent::onInstall();
  }
  
  public function onDisable() {

    $db = $this->getDb();

    $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '0' WHERE  `engine4_core_themes`.`name` ='elpis' LIMIT 1");
    $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '1' WHERE  `engine4_core_themes`.`name` ='insignia' LIMIT 1");
    parent::onDisable();
  }

  function onEnable() {

    $db = $this->getDb();
    
    $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "inspira.header";');
    $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.header";');
    $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-main";');
		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-mini";');
		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-logo";');
		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.search-mini";');
    $parent_content_id = $db->select()
		        ->from('engine4_core_content', 'content_id')
		        ->where('type = ?', 'container')
		        ->where('page_id = ?', '1')
		        ->where('name = ?', 'main')
		        ->limit(1)
		        ->query()
		        ->fetchColumn();
		if($parent_content_id) {
		  $db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'core.menu-mini',
        'page_id' => 1,
        'parent_content_id' => $parent_content_id,
        'order' => 9,
      ));
      $db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'core.search-mini',
        'page_id' => 1,
        'parent_content_id' => $parent_content_id,
        'order' => 10,
      ));
			$db->insert('engine4_core_content', array(
		      'type' => 'widget',
		      'name' => 'core.menu-logo',
		      'page_id' => 1,
		      'parent_content_id' => $parent_content_id,
		      'order' => 11,
		  ));
		  $db->insert('engine4_core_content', array(
		      'type' => 'widget',
		      'name' => 'core.menu-main',
		      'page_id' => 1,
		      'parent_content_id' => $parent_content_id,
		      'order' => 12,
		  ));
	  }
	  
	  //Footer
	  $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "inspira.footer";');
    $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.footer";');
    $db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `engine4_core_content`.`name` = "core.menu-footer";');
		$parent_content_id = $db->select()
		        ->from('engine4_core_content', 'content_id')
		        ->where('type = ?', 'container')
		        ->where('page_id = ?', '2')
		        ->where('name = ?', 'main')
		        ->limit(1)
		        ->query()
		        ->fetchColumn();
		if(!empty($parent_content_id)) {
			$db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'core.menu-footer',
        'page_id' => 2,
        'parent_content_id' => $parent_content_id,
        'order' => 9,
      ));
	  }

    //Theme Enabled and disabled
    $select = new Zend_Db_Select($db);
    $select->from('engine4_core_themes', 'name')
            ->where('active = ?', 1)
            ->limit(1);
    $themeActive = $select->query()->fetch();
    if($themeActive) {
      $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '0' WHERE  `engine4_core_themes`.`name` ='".$themeActive['name']."' LIMIT 1");
      $db->query("UPDATE  `engine4_core_modules` SET  `enabled` =  '0' WHERE  `engine4_core_modules`.`name` ='".$themeActive['name']."' LIMIT 1");
      $db->query("UPDATE  `engine4_core_themes` SET  `active` =  '1' WHERE  `engine4_core_themes`.`name` ='elpis' LIMIT 1");
    }
    $db->query("UPDATE `engine4_core_settings` SET `value` = '0' WHERE `engine4_core_settings`.`name` = 'elpis.changelanding';");
    parent::onEnable();
  }
}