<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Stories_Installer extends Engine_Package_Installer_Module
{
	public function onInstall()
	{

		$db = $this->getDb();

		//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) {

			$topContainerId = $db->select()
				->from('engine4_core_content', 'content_id')
				->where('page_id = ?', $page_id)
				->where('type = ?', 'container')
				->where('name = ?', 'main')
				->limit(1)
				->query()
				->fetchColumn();

			$middleContainerId = $db->select()
				->from('engine4_core_content', 'content_id')
				->where('page_id = ?', $page_id)
				->where('type = ?', 'container')
				->where('name = ?', 'middle')
				->where('parent_content_id = ?', $topContainerId)
				->limit(1)
				->query()
				->fetchColumn();

			$hasStories = $db->select()
				->from('engine4_core_content', 'content_id')
				->where('page_id = ?', $page_id)
				->where('name = ?', 'stories.all-stories')
				->limit(1)
				->query()
				->fetchColumn();
			if (empty($hasStories)) {
				$db->insert('engine4_core_content', array(
					'type' => 'widget',
					'name' => 'stories.all-stories',
					'page_id' => $page_id,
					'parent_content_id' => $middleContainerId,
					'order' => 0,
				));
			}
		}

		parent::onInstall();
	}
}
