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

class Fbclone_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {

    $db = $this->getDb();

    if($this->_databaseOperationType != 'upgrade') {
			//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' => 'fbclone.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";');
			$db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 2 AND `engine4_core_content`.`name` = "core.menu-social-sites";');
			
			$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' => 'fbclone.footer',
					'page_id' => 2,
					'parent_content_id' => $parent_content_id,
					'order' => 888,
				));
			}
			
      //Member Home Page
      $page_id = $db->select()
              ->from('engine4_core_pages', 'page_id')
              ->where('name = ?', 'user_index_home')
              ->limit(1)
              ->query()
              ->fetchColumn();
      if($page_id) {
        $left_id = $db->select()
          ->from('engine4_core_content', 'content_id')
          ->where('page_id = ?', $page_id)
          ->where('type = ?', 'container')
          ->where('name = ?', 'left')
          ->limit(1)
          ->query()
          ->fetchColumn();
          
        $db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 4 AND `engine4_core_content`.`name` = "user.home-photo";');
        $db->query('DELETE FROM engine4_core_content WHERE `engine4_core_content`.`page_id` = 4 AND `engine4_core_content`.`name` = "user.home-links";');
        
        $db->insert('engine4_core_content', array(
          'type' => 'widget',
          'name' => 'fbclone.dashboard-links',
          'page_id' => $page_id,
          'parent_content_id' => $left_id,
          'order' => 1,
        ));
      }
      
      //Theme Enable
      $db->query('INSERT IGNORE INTO `engine4_core_themes` (`name`, `title`, `description`, `active`) VALUES
      ("fbclone", "Facebook Clone Theme", "Facebook Clone Theme", 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` ='fbclone' LIMIT 1");
      }
      
      //Start Make extra file for fbclone theme custom css
      $themeDirName = APPLICATION_PATH . '/application/themes/fbclone';
			@chmod($themeDirName, 0777);
			$fileName = $themeDirName . '/fbclone-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/fbclone';
    @chmod($theme, 0777);
    $filename = $theme . '/theme-variables.css';
    $fileExists = @file_exists($filename);
    if (empty($fileExists)) {
      @chmod($theme, 0777);
      $fh = @fopen($filename, 'w');
      $constant = ':root {
				--themewidget-radius:0.625rem;
        --fbclone-header-background-color:#FFFFFF;
				--fbclone-mainmenu-links-color:#636569;
				--fbclone-mainmenu-links-hover-color:#1D7CF7;
				--fbclone-minimenu-background-color:#ececec;
				--fbclone-minimenu-hover-background-color:#f2f7ff;
				--fbclone-mininmenu-links-color:#000000;
				--fbclone-mininmenu-links-hover-color:#1D7CF7;
				--fbclone-minimenu-search-background-color:#F6F6F6;
				--fbclone-minimenu-search-font-color:#5F727F;
				--fbclone-footer-background-color:#FDFDFD;
				--fbclone-footer-font-color:#737373;
				--fbclone-footer-links-color:#737373;
				--fbclone-theme-color:#1D7CF7;
				--fbclone-body-background-color:#F0F2F5;
				--fbclone-font-color:#000000;
				--fbclone-font-color-light:#65676B;
				--fbclone-links-color:#000000;
				--fbclone-links-hover-color:#1D7CF7;
				--fbclone-headline-color:#000000;
				--fbclone-border-color:#E1E2E3;
				--fbclone-box-background-color:#FFFFFF;
				--fbclone-box-background-color-alt:#F5F5F5;
				--fbclone-form-label-color:#455B6B;
				--fbclone-input-background-color:#FFFFFF;
				--fbclone-input-font-color:#5F727F;
				--fbclone-input-border-color:#D7D8DA;
				--fbclone-button-background-color:#1D7CF7;
				--fbclone-button-background-color-hover:#2783FB;
				--fbclone-button-font-color:#FFFFFF;
				--fbclone-button-font-color-hover:#FFFFFF;
			  --fbclone-button-border-color:#1D7CF7;
				--fbclone-button-border-color-hover:#2783FB;
			  --fbclone-secondary-button-background-color:#f2f2f2;
			  --fbclone-secondary-button-background-color-hover:#e5e5e5;
			  --fbclone-secondary-button-font-color:#212529;
			  --fbclone-secondary-button-font-color-hover:#212529;
			  --fbclone-secondary-button-border-color:#E4E6EB;
			  --fbclone-secondary-button-border-color-hover:#d8dadf;
				--fbclone-comments-background-color:#f9f9fb;
				--fbclone-dashboard-list-background-color-hover:#E5E5E5;
				--fbclone-dashboard-list-border-color:#dbdbdb;
				--fbclone-dashboard-font-color:#040609;
				--fbclone-dashboard-link-color:#040609;
				--fbclone-lp-signup-button-color:#46BD2E;
				--fbclone-lp-signup-button-border-color:#46BD2E;
				--fbclone-lp-signup-button-font-color:#FFFFFF;
				--fbclone-lp-signup-button-hover-color:#46BD2E;
				--fbclone-lp-signup-button-hover-font-color:#FFFFFF;
				--fbclone-body-fontfamily:Roboto;
				--fbclone-heading-fontfamily:Roboto;
				--fbclone-mainmenu-fontfamily:Roboto;
				--fbclone-tab-fontfamily:Roboto;
				--fbclone-body-fontsize:0.90rem;
				--fbclone-heading-fontsize:1rem;
				--fbclone-mainmenu-fontsize:0.90rem;
				--fbclone-tab-fontsize:0.90rem;
      }';
      @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
      ("fbclone.theme.color", 1);');
    }
		//Theme Color Default Installation

    parent::onInstall();
  }

  public function onDisable() {
  
    $db = $this->getDb();
    
    //Header
    $db->query("DELETE FROM `engine4_core_content` WHERE `engine4_core_content`.`name` = 'fbclone.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` = "fbclone.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`.`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 = ?', 'fbclone.header')
					->limit(1);
			$content_id = $select_content->query()->fetchObject()->content_id;
			if(empty($content_id)) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'fbclone.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` = "core.menu-social-sites";');
    $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 = ?', 'fbclone.footer')
				->limit(1);
			$content_id = $select_content->query()->fetchObject()->content_id;
			if(empty($content_id)) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'fbclone.footer',
					'page_id' => 2,
					'parent_content_id' => $parent_content_id,
					'order' => 9,
				));
			}
	  }
    
    //Theme active
    $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` ='fbclone' LIMIT 1");
    }
    $db->query("UPDATE `engine4_core_settings` SET `value` = '0' WHERE `engine4_core_settings`.`name` = 'fbclone.changelanding';");

    parent::onEnable();
  }
}