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

class Inspira_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {
    $db = $this->getDb();
    
    if($this->_databaseOperationType != 'upgrade') {
          
      //Quick links footer
      $menuitem_id = $db->select()
                      ->from('engine4_core_menuitems', 'id')
                      ->limit(1)
                      ->order('id DESC')
                      ->query()
                      ->fetchColumn();

      $db->query('INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `enabled`, `custom`, `order`) VALUES
      ("custom_'.$menuitem_id++.'", "core", "Albums", "", \'{"uri":"albums","icon":""}\', "inspira_quicklinks_footer", "", 1, 1, 1),
      ("custom_'.$menuitem_id++.'", "core", "Blogs", "", \'{"uri":"blogs","icon":""}\', "inspira_quicklinks_footer", "", 1, 1, 2),
      ("custom_'.$menuitem_id++.'", "core", "Events", "", \'{"uri":"events"}\', "inspira_quicklinks_footer", "", 1, 1, 3),
      ("custom_'.$menuitem_id++.'", "core", "Videos", "", \'{"uri":"videos","icon":"","target":"","enabled":"1"}\', "inspira_quicklinks_footer", "", 1, 1, 4),
      ("custom_'.$menuitem_id++.'", "core", "Music", "", \'{"uri":"music","icon":"","target":"","enabled":"1"}\', "inspira_quicklinks_footer", "", 1, 1, 5);');

			//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) {
				$db->insert('engine4_core_content', array(
						'type' => 'widget',
						'name' => 'inspira.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($parent_content_id) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'inspira.footer',
					'page_id' => 2,
					'parent_content_id' => $parent_content_id,
					'order' => 888,
				));
			}
      
      //Theme Enable
      $db->query('INSERT IGNORE INTO `engine4_core_themes` (`name`, `title`, `description`, `active`) VALUES
      ("inspira", "Inspira", "Inspira", 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` ='inspira' LIMIT 1");
      }
      
      //Start Make extra file for inspira theme custom css
      $themeDirName = APPLICATION_PATH . '/application/themes/inspira';
			@chmod($themeDirName, 0777);
			$fileName = $themeDirName . '/inspira-custom.css';
			$fileexists = @file_exists($fileName);
			if (empty($fileexists)) {
				@chmod($themeDirName, 0777);
				$fh = @fopen($fileName, 'w');
				@fwrite($fh, '/* ADD YOUR CUSTOM CSS HERE */');
				@chmod($fileName, 0777);
				@fclose($fh);
				@chmod($fileName, 0777);
				@chmod($fileName, 0777);
			}
    }

		//Theme Color Default Installation
		$theme = APPLICATION_PATH . '/application/themes/inspira';
		@chmod($theme, 0777);
		$filename = $theme . '/theme-variables.css';
		$fileExists = @file_exists($filename);
		if (empty($fileExists)) {
			@chmod($theme, 0777);
			$fh = @fopen($filename, 'w');
			$constant = ':root {
			--inspira-header-background-color:#1790BD;
			--inspira-header-logo-color:#FFFFFF;
			--inspira-mainmenu-background-color:#1790BD;
			--inspira-mainmenu-links-color:#FFFFFF;
			--inspira-mainmenu-links-hover-color:#1790BD;
			--inspira-mainmenu-links-hover-background-color:#FFFFFF;
			--inspira-minimenu-links-color:#1790BD;
			--inspira-minimenu-links-background-color:#FFFFFF;
			--inspira-footer-background-color:#000000;
			--inspira-footer-font-color:#FFFFFF;
			--inspira-footer-links-color:#FFFFFF;
			--inspira-footer-border-color:#FFFFFF;
			--inspira-theme-color:#1790BD;
			--inspira-body-background-color:#F5F5F5;
			--inspira-font-color:#5A5A5A;
			--inspira-font-color-light:#5A6064;
			--inspira-links-color:#1790BD;
			--inspira-links-hover-color:#168AB5;
			--inspira-headline-color:#323232;
			--inspira-border-color:#EAEAEA;
			--inspira-box-background-color:#FFFFFF;
			--inspira-box-background-alt-color:#F2F2F2;
			--inspira-form-label-color:#323232;
			--inspira-input-background-color:#FFFFFF;
			--inspira-input-font-color:#959595;
			--inspira-input-border-color:#D4D4D4;
			--inspira-button-background-color:#1790BD;
			--inspira-button-background-color-hover:#168AB5;
			--inspira-button-font-color:#FFFFFF;
			--inspira-button-font-color-hover:#FFFFFF;
			--inspira-button-border-color:#1790BD;
			--inspira-button-border-color-hover:#168AB5;
			--inspira-secondary-button-background-color:#F2F2F2;
			--inspira-secondary-button-background-color-hover:#E5E5E5;
			--inspira-secondary-button-font-color:#212529;
			--inspira-secondary-button-font-color-hover:#212529;
			--inspira-secondary-button-border-color:#E4E6EB;
			--inspira-secondary-button-border-color-hover:#D8DADF;
			--inspira-comments-background-color:#F7F7F7;
			--inspira-body-fontfamily:Roboto;
			--inspira-heading-fontfamily:Roboto;
			--inspira-mainmenu-fontfamily:Roboto;
			--inspira-tab-fontfamily:Roboto;
			}';
			@fwrite($fh, $constant);
			@chmod($filename, 0777);
			@fclose($fh);
			@chmod($filename, 0777);
			@chmod($filename, 0777);
			
			Engine_Api::_()->getApi('settings', 'core')->setSetting('inspira.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_content = $select
					->from('engine4_core_content')
					->where('page_id = ?', 1)
					->where('type = ?', 'widget')
					->where('name = ?', 'inspira.header')
					->limit(1);
			$content_id = $select_content->query()->fetchObject()->content_id;
			
			if(empty($content_id)) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'inspira.header',
					'page_id' => 1,
					'parent_content_id' => $parent_content_id,
					'order' => 20,
				));
			}
		}

	  //Footer
	  $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "core.menu-footer";');
    $db->query('DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = "charity.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)) {
			$select = new Zend_Db_Select($db);
			$select_content = $select
					->from('engine4_core_content')
					->where('page_id = ?', 2)
					->where('type = ?', 'widget')
					->where('name = ?', 'inspira.footer')
					->limit(1);
			$content_id = $select_content->query()->fetchObject()->content_id;
			
			if(empty($content_id)) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'inspira.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` ='inspira' LIMIT 1");
    }
    $db->query("UPDATE `engine4_core_settings` SET `value` = '0' WHERE `engine4_core_settings`.`name` = 'inspira.changelanding';");
    parent::onEnable();
  }

  public function onDisable() {
  
    $db = $this->getDb();
    
    //Header
    $db->query("DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = 'inspira.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` = "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();
  }
}
