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

class Gdpr_Installer extends Engine_Package_Installer_Module {

  public function onInstall() {

    $db = $this->getDb();

    // Header Work
    $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 (empty($content_id)) {
      $db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'gdpr.cookie-banner',
        'page_id' => 1,
        'parent_content_id' => $parent_content_id,
        'order' => 999,
      ));
    }

    //Privacy Page
    $page_id = $db->select()
                  ->from('engine4_core_pages', 'page_id')
                  ->where('name = ?', 'core_help_privacy')
                  ->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 = ?', 'middle')
                    ->limit(1)
                    ->query()
                    ->fetchColumn();
      $db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'gdpr.cookies-privacy-consent',
        'page_id' => $page_id,
        'parent_content_id' => $left_id,
        'order' => 999,
      ));
    }

    $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'gdpr_index_index')
                ->limit(1)
                ->query()
                ->fetchColumn();
    if (!$page_id) {
      // Insert page
      $db->insert('engine4_core_pages', array(
        'name' => 'gdpr_index_index',
        'displayname' => 'Privacy Center Page',
        'title' => 'Privacy Center Page',
        'description' => 'This page is the view privacy center page.',
        'custom' => 0,
      ));
      
      $page_id = $db->lastInsertId();
      // Insert main
      $db->insert('engine4_core_content', array(
        'type' => 'container',
        'name' => 'main',
        'page_id' => $page_id,
      ));
      $main_id = $db->lastInsertId();

      // Insert middle
      $db->insert('engine4_core_content', array(
        'type' => 'container',
        'name' => 'middle',
        'page_id' => $page_id,
        'parent_content_id' => $main_id,
        'order' => 2,
      ));
      $middle_id = $db->lastInsertId();

      // Insert content
      $db->insert('engine4_core_content', array(
        'type' => 'widget',
        'name' => 'gdpr.privacy-center',
        'page_id' => $page_id,
        'parent_content_id' => $middle_id,
        'order' => 1,
      ));
    }
    parent::onInstall();
  }
}
