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

class Serenity_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {
    $db = $this->getDb();
    if($this->_databaseOperationType != 'upgrade') {
      $default_constants = array(
        'theme_color'  => '1',
        'custom_theme_color'  => '1',
        'themewidget_radius' => '1px',
        'serenity_header_background_color'  => '#03598f',
        'serenity_mainmenu_background_color'  => '#fff',
        'serenity_mainmenu_links_color'  => '#111111',
        'serenity_mainmenu_links_hover_color'  => '#fff',
        'serenity_mainmenu_links_hover_background_color'  => '#fe4497',
				'serenity_menu_tip_color' => '#FFFFFF',
        'serenity_minimenu_links_color'  => '#FFFFFF',
        'serenity_minimenu_link_active_color'  => '#fe4497',
        'serenity_footer_background_color'  => '#FFFFFF',
        'serenity_footer_font_color'  => '#676767',
        'serenity_footer_links_color'  => '#676767',
				'serenity_footer_copyright_color'  => '#676767',
        'serenity_footer_border_color'  => '#e4e4e4',
        'serenity_theme_color'  => '#03598f',
        'serenity_body_background_color'  => '#e6ecf0',
        'serenity_font_color'  => '#111111',
        'serenity_font_color_light'  => '#444444',
        'serenity_links_color'  => '#111111',
        'serenity_links_hover_color'  => '#03598f',
        'serenity_headline_color'  => '#111111',
        'serenity_border_color'  => '#e2e4e6',
        'serenity_box_background_color'  => '#FFFFFF',
        'serenity_form_label_color'  => '#455B6B',
        'serenity_input_background_color'  => '#fff',
        'serenity_input_font_color'  => '#5f727f',
        'serenity_input_border_color'  => '#d7d8da',
        'serenity_button_background_color'  => '#03598f',
        'serenity_button_background_color_hover'  => '#fe4497',
        'serenity_button_font_color'  => '#FFFFFF',
        'serenity_button_border_color'  => '#03598f',
        'serenity_comments_background_color'  => '#f5f5f5',
  			'serenity_body_fontfamily' => '"Source Sans Pro"',
        'serenity_heading_fontfamily' => '"Source Sans Pro"',
        'serenity_mainmenu_fontfamily' => '"Source Sans Pro"',
        'serenity_tab_fontfamily' => '"Source Sans Pro"',
      );
      $this->readWriteXML('', '', $default_constants);
      
      // 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 Social Network!!',
					'caption' => 'Welcome to our Social Network!!',
          'body' => 'Love what you see?',
          'photo_id' => 0,
          'params' => '{"label":"Get Started","uri":"signup"}',
          'custom' => 0
        ));
        $bannerId = $db->lastInsertId();
        if( $bannerId ) {
          $db->query('UPDATE `engine4_core_content` SET `params` = \'{"bannerId":"'.$bannerId.'","height":"600","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
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "inspira.header";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "charity.header";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-main";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-mini";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-logo";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `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`.`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` = "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
			$db->query('INSERT IGNORE INTO `engine4_core_themes` (`name`, `title`, `description`, `active`) VALUES
      ("serenity", "Serenity", "Serenity", 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` ='serenity' LIMIT 1");
			}
    }
    
    parent::onInstall();
  }
  
  public function onDisable() {

    $db = $this->getDb();
    
		//Header
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "inspira.header";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "charity.header";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-main";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-mini";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-logo";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `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`.`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` = "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` =  '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();
  }

  function onEnable() {

    $db = $this->getDb();
    
		//Header
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "inspira.header";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "charity.header";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-main";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-mini";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `engine4_core_content`.`name` = "core.menu-logo";');
		$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 1 AND `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`.`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` = "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` ='serenity' LIMIT 1");
		}
		$db->query("UPDATE `engine4_core_settings` SET `value` = '0' WHERE `engine4_core_settings`.`name` = 'serenity.changelanding';");
    parent::onEnable();
  }
  
  function readWriteXML($keys, $value, $default_constants = null) {
    $filePath = APPLICATION_PATH . "/application/settings/constants.xml";
    $results = simplexml_load_file($filePath);

    if (!empty($keys) && !empty($value)) {
        $contactsThemeArray = array($keys => $value);
    } elseif (!empty($keys)) {
        $contactsThemeArray = array($keys => '');
    } elseif ($default_constants) {
        $contactsThemeArray = $default_constants;
    }

    foreach ($contactsThemeArray as $key => $value) {
      $xmlNodes = $results->xpath('/root/constant[name="' . $key . '"]');
      $nodeName = @$xmlNodes[0];
      $params = json_decode(json_encode($nodeName));
      $paramsVal = @$params->value;
      if ($paramsVal && $paramsVal != '' && $paramsVal != null) {
          $nodeName->value = $value;
      } else {
          $entry = $results->addChild('constant');
          $entry->addChild('name', $key);
          $entry->addChild('value', $value);
      }
    }
    return $results->asXML($filePath);
  }
}
