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

class Sunshine_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {
    $db = $this->getDb();
    
    if($this->_databaseOperationType != 'upgrade') {
    
			$select = new Zend_Db_Select($db);
      $select->from('engine4_core_modules', 'name')
              ->where('name = ?', 'sunshine')
              ->limit(1);
      $moduleActive = $select->query()->fetch();
      if(empty($moduleActive)) {
				//Header work
				$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";');
				$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.header";');
				$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) {
					$select = new Zend_Db_Select($db);
					$select->from('engine4_core_content')
							->where('page_id = ?', 1)
							->where('type = ?', 'widget')
							->where('name = ?', 'sunshine.header');
					$info = $select->query()->fetch();
					if(empty($info)) {
						$db->insert('engine4_core_content', array(
							'type' => 'widget',
							'name' => 'sunshine.header',
							'page_id' => 1,
							'parent_content_id' => $parent_content_id,
							'order' => 20,
						));
					}
				}
				
				//Footer Work
				$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `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` = "inspira.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
				("sunshine", "Sunshine", "Sunshine", 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` ='sunshine' LIMIT 1");
				}
			}
    }

		//Theme Color Default Installation
		$theme = APPLICATION_PATH . '/application/themes/sunshine';
		@chmod($theme, 0777);
		$filename = $theme . '/theme-variables.css';
		$fileExists = @file_exists($filename);
		if (empty($fileExists)) {
			@chmod($theme, 0777);
			$fh = @fopen($filename, 'w');
			$constant = ':root {
			--sunshine-header-background-color:#FB8C0B;
			--sunshine-mainmenu-background-color:#FFFFFF;
			--sunshine-mainmenu-links-color:#060606;
			--sunshine-mainmenu-links-hover-color:#060606;
			--sunshine-mainmenu-links-hover-background-color:#F5DFC5;
			--sunshine-menu-tip-color:#000000;
			--sunshine-minimenu-links-color:#FFFFFF;
			--sunshine-footer-background-color:#E7E7E6;
			--sunshine-footer-font-color:#393838;
			--sunshine-footer-links-color:#393838;
			--sunshine-footer-border-color:#E4E4E4;
			--sunshine-theme-color:#FB8C0B;
			--sunshine-body-background-color:#F2F2F2;
			--sunshine-font-color:#161616;
			--sunshine-font-color-light:#808D97;
			--sunshine-links-color:#161616;
			--sunshine-links-hover-color:#FB8C0B;
			--sunshine-headline-color:#111111;
			--sunshine-border-color:#E2E4E6;
			--sunshine-box-background-color:#FFFFFF;
			--sunshine-box-background-alt-color:#F1F1F1;
			--sunshine-form-label-color:#455B6B;
			--sunshine-input-background-color:#FFFFFF;
			--sunshine-input-font-color:#868686;
			--sunshine-input-border-color:#DCDCDC;
			--sunshine-button-background-color:#FB8C0B;
			--sunshine-button-background-color-hover:#FB8C0B;
			--sunshine-button-font-color:#FFFFFF;
			--sunshine-button-border-color:#FB8C0B;
			--sunshine-button-border-color-hover:#FB8C0B;
			--sunshine-secondary-button-background-color:#f2f2f2;
			--sunshine-secondary-button-background-color-hover:#e5e5e5;
			--sunshine-secondary-button-font-color:#212529;
			--sunshine-secondary-button-font-color-hover:#212529;
			--sunshine-secondary-button-border-color:#E4E6EB;
			--sunshine-secondary-button-border-color-hover:#d8dadf;
			--sunshine-comments-background-color:#F7F7F7;
			--sunshine-body-fontfamily:Roboto;
			--sunshine-heading-fontfamily:Roboto;
			--sunshine-mainmenu-fontfamily:Roboto;
			--sunshine-tab-fontfamily:Roboto;
			}';
			@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
      ("sunshine.theme.color", 1);');
		}
		//Theme Color Default Installation
		
    parent::onInstall();
  }
  
  function onEnable() {
  
    $db = $this->getDb();
		
		//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";');
		$db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.header";');
    $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) {
			$select = new Zend_Db_Select($db);
      $select->from('engine4_core_content')
          ->where('page_id = ?', 1)
          ->where('type = ?', 'widget')
          ->where('name = ?', 'sunshine.header');
      $info = $select->query()->fetch();
      if(empty($info) ) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'sunshine.header',
					'page_id' => 1,
					'parent_content_id' => $parent_content_id,
					'order' => 20,
				));
			}
		}
		
		//Footer Work
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `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` = "inspira.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,
			));
		}

    $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` ='sunshine' LIMIT 1");
    }
    $db->query("UPDATE `engine4_core_settings` SET `value` = '0' WHERE `engine4_core_settings`.`name` = 'sunshine.changelanding';");
    parent::onEnable();
  }


  public function onDisable() {
  
    $db = $this->getDb();
    
    //Header
    $db->query("DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = 'sunshine.header';");
    $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 Work
    $db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `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` = "inspira.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` =  '1' WHERE  `engine4_core_modules`.`name` ='elpis' 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::onDisable();
  }
}
