<commit_before><?php
/**
 * theme_patch_0301
 * @package modules.theme
 */
class theme_patch_0301 extends patch_BasePatch
{ 
	private $themecodename = "projecttheme";
		
	/**
	 * Entry point of the patch execution.
	 */
	public function execute()
	{
		if (!PatchService::getInstance()->isInstalled('website', '0315'))
		{
			throw new Exception('Execute: change.php apply-patch website 0315 before this patch');
		}

		$this->createThemeTables();
		
		$installPath = f_util_FileUtils::buildWebeditPath('themes', $this->themecodename, 'install.xml');
		if (!file_exists($installPath))
		{
			$webfactoryPath = f_util_FileUtils::buildWebeditPath('themes', 'webfactory', 'install.xml');
			if (!file_exists($webfactoryPath))
			{
				$this->logError('Unable to install webfactory theme');
				return;
			}
			theme_ModuleService::getInstance()->installTheme('webfactory');
			$this->updatePageTemplateName('webfactory');			
			$this->applyPageTemplateToAllWebsite('webfactory');
		}		
		else
		{
			theme_ModuleService::getInstance()->installTheme($this->themecodename);
			$this->updatePageTemplateName($this->themecodename);
			$this->applyPageTemplateToAllWebsite($this->themecodename);
		}
	}

	private function applyPageTemplateToAllWebsite($themecodename)
	{
		$this->log('Allow page template to all website');
		$theme = theme_ThemeService::getInstance()->getByCodeName($themecodename);
		if (!$theme)
		{
			$this->logError('Unable to find ' . $themecodename . ' db theme');
			return;
		}
		
		$pageTemplates = $theme->getPublishedPagetemplateArray();
		$websites = website_WebsiteService::getInstance()->getAll();
		foreach ($websites as $website) 
		{
			foreach ($pageTemplates as $pageTemplate) 
			{
				$website->addAllowedpagetemplate($pageTemplate);
			}
			$website->save();
		}	
	}
		
	private function updatePageTemplateName($themecodename)
	{
		$this->log('Update template name for all pages');
		$sql = "UPDATE `m_website_doc_page` SET `template` =  CONCAT('". $themecodename ."/', `template`) WHERE `template` not like '". $themecodename ."/%'";
		$this->executeSQLQuery($sql);
		<commit_msg>[FIX] Correction du patch 0301 pour les references au template dynamique (cmpref::NNNNN)<commit_after><?php
/**
 * theme_patch_0301
 * @package modules.theme
 */
class theme_patch_0301 extends patch_BasePatch
{ 
	private $themecodename = "projecttheme";
		
	/**
	 * Entry point of the patch execution.
	 */
	public function execute()
	{
		if (!PatchService::getInstance()->isInstalled('website', '0315'))
		{
			throw new Exception('Execute: change.php apply-patch website 0315 before this patch');
		}

		$this->createThemeTables();
		
		$installPath = f_util_FileUtils::buildWebeditPath('themes', $this->themecodename, 'install.xml');
		if (!file_exists($installPath))
		{
			$webfactoryPath = f_util_FileUtils::buildWebeditPath('themes', 'webfactory', 'install.xml');
			if (!file_exists($webfactoryPath))
			{
				$this->logError('Unable to install webfactory theme');
				return;
			}
			theme_ModuleService::getInstance()->installTheme('webfactory');
			$this->updatePageTemplateName('webfactory');			
			$this->applyPageTemplateToAllWebsite('webfactory');
		}		
		else
		{
			theme_ModuleService::getInstance()->installTheme($this->themecodename);
			$this->updatePageTemplateName($this->themecodename);
			$this->applyPageTemplateToAllWebsite($this->themecodename);
		}
	}

	private function applyPageTemplateToAllWebsite($themecodename)
	{
		$this->log('Allow page template to all website');
		$theme = theme_ThemeService::getInstance()->getByCodeName($themecodename);
		if (!$theme)
		{
			$this->logError('Unable to find ' . $themecodename . ' db theme');
			return;
		}
		
		$pageTemplates = $theme->getPublishedPagetemplateArray();
		$websites = website_WebsiteService::getInstance()->getAll();
		foreach ($websites as $website) 
		{
			foreach ($pageTemplates as $pageTemplate) 
			{
				$website->addAllowedpagetemplate($pageTemplate);
			}
			$website->save();
		}	
	}
		
	private function updatePageTemplateName($themecodename)
	{
		$this->log("Update old dynamic template ref..");
		$sql = "UPDATE m_website_doc_page SET template = (SELECT template FROM m_website_doc_template WHERE document_id = REPLACE(m_website_doc_page.template, 'cmpref::', '')) WHERE m_website_doc_page.template like 'cmpref::%'";
		$this->executeSQLQuery($sql);
		
		$this->log('Update template name for all pages');
		$sql = "UPDATE `m_website_doc_page` SET `template` =  CONCAT('". $themecodename ."/', `template`) WHERE `template` not like '". $themecodename ."/%'";
		$this->executeSQLQuery($sql);
		
		$this->log('Update dynamic template name for all pages');
{'fraction_non_alphanumeric': 0.1501713796058269, 'fraction_numerical': 0.007712082262210797, 'mean_word_length': 5.079427083333333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5593537', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1460, 'n_words': 273}