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

class Userprofilelock_Installer extends Engine_Package_Installer_Module
{

	public function onInstall()
	{

		$db = $this->getDb();

		// profile page
		$pageId = $db->select()
			->from('engine4_core_pages', 'page_id')
			->where('name = ?', 'user_profile_index')
			->limit(1)
			->query()
			->fetchColumn();

		if ($pageId) {
			$hasTop = $db->select()
				->from('engine4_core_content', 'content_id')
				->where('name = ?', 'top')
				->where('page_id = ?', $pageId)
				->where('type = ?', 'container')
				->limit(1)
				->query()
				->fetchColumn();

			if (!empty($hasTop)) {

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

				if ($topMiddleId) {

					$hasProfileLock = $db->select()
						->from('engine4_core_content', 'content_id')
						->where('page_id = ?', $pageId)
						->where('name = ?', 'userprofilelock.profile-lock')
						->limit(1)
						->query()
						->fetchColumn();

					if (!$hasProfileLock) {
						$db->insert('engine4_core_content', array(
							'type' => 'widget',
							'name' => 'userprofilelock.profile-lock',
							'page_id' => $pageId,
							'parent_content_id' => $topMiddleId,
							'order' => 3,
						));
					}
				}
			}

			//Left side widget place
			$hasLeft = $db->select()
				->from('engine4_core_content', 'content_id')
				->where('name = ?', 'left')
				->where('page_id = ?', $pageId)
				->where('type = ?', 'container')
				->limit(1)
				->query()
				->fetchColumn();

			if (!empty($hasLeft)) {

				$hasProfileLockOwner = $db->select()
					->from('engine4_core_content', 'content_id')
					->where('page_id = ?', $pageId)
					->where('name = ?', 'userprofilelock.profile-lock-owner')
					->limit(1)
					->query()
					->fetchColumn();

				if (empty($hasProfileLockOwner)) {
					$db->insert('engine4_core_content', array(
						'type' => 'widget',
						'name' => 'userprofilelock.profile-lock-owner',
						'page_id' => $pageId,
						'parent_content_id' => $hasLeft,
						'order' => 1,
					));
				}
			}
		}
		parent::onInstall();
	}
}
