<commit_before><?php
/**
 * forums_ForumgroupService
 * @package modules.forums
 */
class forums_ForumgroupService extends f_persistentdocument_DocumentService
{
	/**
	 * @var forums_ForumgroupService
	 */
	private static $instance;

	/**
	 * @return forums_ForumgroupService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = self::getServiceClassInstance(get_class());
		}
		return self::$instance;
	}

	/**
	 * @return forums_persistentdocument_forumgroup
	 */
	public function getNewDocumentInstance()
	{
		return $this->getNewDocumentInstanceByModelName('modules_forums/forumgroup');
	}

	/**
	 * Create a query based on 'modules_forums/forumgroup' model.
	 * Return document that are instance of modules_forums/forumgroup,
	 * including potential children.
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createQuery()
	{
		return $this->pp->createQuery('modules_forums/forumgroup');
	}
	
	/**
	 * Create a query based on 'modules_forums/forumgroup' model.
	 * Only documents that are strictly instance of modules_forums/forumgroup
	 * (not children) will be retrieved
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createStrictQuery()
	{
		return $this->pp->createQuery('modules_forums/forumgroup', false);
	}
	
	/**
	 * @param Integer $parentId
	 * @return forums_persistentdocument_forumgroup[]
	 */
	public function getByTopicParentId($parentId)
	{
		$query = $this->createQuery()->add(Restrictions::published());
		$query->createCriteria('topic')->add(Restrictions::childOf($parentId));
		return $query->find();
	}

	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return void
	 */
	protected function preSave($document, $parentNodeId)
	{
		parent::preSave($document, $parentNodeId);
		
		$parent = $this->getMountParent($document, $parentNodeId);
		
		// Generate or move the topic.
		$topic = $document->getTopic();
		if ($topic === null)
		{
			$topic = website_SystemtopicService::getInstance()->getNewDocumentInstance();
			$topic->setReferenceId($document->getId());
			$topic->setLabel($document->getLabel());
			$topic->setDescription($document->getDescription());
			$topic->setPublicationstatus('DRAFT');
			$topic->save($parent->getId());
			$document->setTopic($topic);
			$document->setWebsite(DocumentHelper::getDocumentInstance($topic->getDocumentService()->getWebsiteId($topic)));
		}
		else if ($parent !== $this->getParentOf($topic))
		{
			if ($document->getWebsite()->getId() != $parent->getDocumentService()->getWebsiteId($parent))
			{
				throw new BaseException('You can only move a forum inside the same website!', 'modules.forums.document.forum.exception.Cant-move-to-another-website');
			}
			$topic->getDocumentService()->moveTo($topic, $parent->getId());
		}
		
		// Recompile locked and excludeFromRss if needed.
		if ($document->isPropertyModified('lockedMode'))
		{
			$this->refreshProperty($document, 'locked', $parentNodeId);
		}
		if ($document->isPropertyModified('excludeFromRssMode'))
		{
			$this->refreshProperty($document, 'excludeFromRss', $parentNodeId);
		}
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param string $propertyName
	 */
	private function refreshProperty($document, $propertyName, $parentNodeId)
	{
		$setter = 'set'.ucfirst($propertyName);
		switch ($document->{'get'.ucfirst($propertyName).'Mode'}())
		{
			case forums_ModuleService::EXTBOOL_INHERIT:
				$document->{$setter}($this->getForumgroupParent($document, $parentNodeId)->{'get'.ucfirst($propertyName)}());
				break;
				
			case forums_ModuleService::EXTBOOL_TRUE:
				$document->{$setter}(true);
				break;
				
			case forums_ModuleService::EXTBOOL_FALSE:
				$document->{$setter}(false);
				break;
		}
	}
		
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return forums_persistentdocument_forumgroup
	 */
	protected function getForumgroupParent($document, $parentNodeId)
	{
		$parent = $document->getDocumentService()->getParentOf($document);
		if ($parent === null)
		{
			$parent = DocumentHelper::getDocumentInstance($parentNodeId);
		}
		if ($parent instanceof forums_persistentdocument_forumgroup)
		{
			return $parent;
		}
		throw new Exception('Bad parent type: ' . get_class($parent));
	}

	/**
	 * @param forums_persistentdocument_forum $forum
	 * @return forums_persistentdocument_post
	 */
	public function getLastPostRecursive($forum)
	{
		$query = forums_PostService::getInstance()->createQuery()
			->add(Restrictions::isNull('deleteddate'))
			->addOrder(Order::desc('document_creationdate'))
			->setFirstResult(0)->setMaxResults(1);
		$criteria = $query->createCriteria('thread')->createCriteria('forum');
		$criteria->add(Restrictions::orExp(Restrictions::eq('id', $forum->getId()), Restrictions::descendentOf($forum->getId())));
		return f_util_ArrayUtils::firstElement($query->find());
	}
	
	/**
	 * @param forums_persistentdocument_forum $forum
	 * @return forums_persistentdocument_post
	 */
	public function getInfosRecursive($forum)
	{
		$query = forums_ForumService::getInstance()->createQuery();
		$query->add(Restrictions::orExp(Restrictions::eq('id', $forum->getId()), Restrictions::descendentOf($forum->getId())));
		$query->setProjection(Projections::sum('nbpost', 'nbpostrecursive'), Projections::sum('nbthread', 'nbthreadrecursive'));
		return f_util_ArrayUtils::firstElement($query->find());
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId
	 */
	protected function postSave($document, $parentNodeId)
	{
		parent::postSave($document, $parentNodeId);
		
		// Recompile locked and excludeFromRss if needed.
		if ($document->isPropertyModified('locked'))
		{
			$this->refreshPropertyOnChildren($document, 'locked');
		}
		if ($document->isPropertyModified('excludeFromRss'))
		{
			$this->refreshPropertyOnChildren($document, 'excludeFromRss');
		}
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param string $propertyName
	 */
	private function refreshPropertyOnChildren($document, $propertyName)
	{
		$setter = 'set'.ucfirst($propertyName);
		$getter = 'get'.ucfirst($propertyName);
		$query = $this->createQuery()->add(Restrictions::eq($propertyName.'Mode', forums_ModuleService::EXTBOOL_INHERIT))
				->add(Restrictions::childOf($document->getId()));
		foreach ($query->find() as $forum)
		{<commit_msg>[FIX #34614] Synchro du libellé des systemtopic et groupes de forums<commit_after><?php
/**
 * forums_ForumgroupService
 * @package modules.forums
 */
class forums_ForumgroupService extends f_persistentdocument_DocumentService
{
	/**
	 * @var forums_ForumgroupService
	 */
	private static $instance;

	/**
	 * @return forums_ForumgroupService
	 */
	public static function getInstance()
	{
		if (self::$instance === null)
		{
			self::$instance = self::getServiceClassInstance(get_class());
		}
		return self::$instance;
	}

	/**
	 * @return forums_persistentdocument_forumgroup
	 */
	public function getNewDocumentInstance()
	{
		return $this->getNewDocumentInstanceByModelName('modules_forums/forumgroup');
	}

	/**
	 * Create a query based on 'modules_forums/forumgroup' model.
	 * Return document that are instance of modules_forums/forumgroup,
	 * including potential children.
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createQuery()
	{
		return $this->pp->createQuery('modules_forums/forumgroup');
	}
	
	/**
	 * Create a query based on 'modules_forums/forumgroup' model.
	 * Only documents that are strictly instance of modules_forums/forumgroup
	 * (not children) will be retrieved
	 * @return f_persistentdocument_criteria_Query
	 */
	public function createStrictQuery()
	{
		return $this->pp->createQuery('modules_forums/forumgroup', false);
	}
	
	/**
	 * @param Integer $parentId
	 * @return forums_persistentdocument_forumgroup[]
	 */
	public function getByTopicParentId($parentId)
	{
		$query = $this->createQuery()->add(Restrictions::published());
		$query->createCriteria('topic')->add(Restrictions::childOf($parentId));
		return $query->find();
	}

	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return void
	 */
	protected function preSave($document, $parentNodeId)
	{
		parent::preSave($document, $parentNodeId);
		
		$parent = $this->getMountParent($document, $parentNodeId);
		
		// Generate or move the topic.
		$topic = $document->getTopic();
		if ($topic === null)
		{
			$topic = website_SystemtopicService::getInstance()->getNewDocumentInstance();
			$topic->setReferenceId($document->getId());
			$topic->setLabel($document->getLabel());
			$topic->setDescription($document->getDescription());
			$topic->setPublicationstatus('DRAFT');
			$topic->save($parent->getId());
			$document->setTopic($topic);
			$document->setWebsite(DocumentHelper::getDocumentInstance($topic->getDocumentService()->getWebsiteId($topic)));
		}
		else if ($parent !== $this->getParentOf($topic))
		{
			if ($document->getWebsite()->getId() != $parent->getDocumentService()->getWebsiteId($parent))
			{
				throw new BaseException('You can only move a forum inside the same website!', 'modules.forums.document.forum.exception.Cant-move-to-another-website');
			}
			$topic->getDocumentService()->moveTo($topic, $parent->getId());
		}
		
		// Recompile locked and excludeFromRss if needed.
		if ($document->isPropertyModified('lockedMode'))
		{
			$this->refreshProperty($document, 'locked', $parentNodeId);
		}
		if ($document->isPropertyModified('excludeFromRssMode'))
		{
			$this->refreshProperty($document, 'excludeFromRss', $parentNodeId);
		}
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param string $propertyName
	 */
	private function refreshProperty($document, $propertyName, $parentNodeId)
	{
		$setter = 'set'.ucfirst($propertyName);
		switch ($document->{'get'.ucfirst($propertyName).'Mode'}())
		{
			case forums_ModuleService::EXTBOOL_INHERIT:
				$document->{$setter}($this->getForumgroupParent($document, $parentNodeId)->{'get'.ucfirst($propertyName)}());
				break;
				
			case forums_ModuleService::EXTBOOL_TRUE:
				$document->{$setter}(true);
				break;
				
			case forums_ModuleService::EXTBOOL_FALSE:
				$document->{$setter}(false);
				break;
		}
	}
		
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return forums_persistentdocument_forumgroup
	 */
	protected function getForumgroupParent($document, $parentNodeId)
	{
		$parent = $document->getDocumentService()->getParentOf($document);
		if ($parent === null)
		{
			$parent = DocumentHelper::getDocumentInstance($parentNodeId);
		}
		if ($parent instanceof forums_persistentdocument_forumgroup)
		{
			return $parent;
		}
		throw new Exception('Bad parent type: ' . get_class($parent));
	}

	/**
	 * @param forums_persistentdocument_forum $forum
	 * @return forums_persistentdocument_post
	 */
	public function getLastPostRecursive($forum)
	{
		$query = forums_PostService::getInstance()->createQuery()
			->add(Restrictions::isNull('deleteddate'))
			->addOrder(Order::desc('document_creationdate'))
			->setFirstResult(0)->setMaxResults(1);
		$criteria = $query->createCriteria('thread')->createCriteria('forum');
		$criteria->add(Restrictions::orExp(Restrictions::eq('id', $forum->getId()), Restrictions::descendentOf($forum->getId())));
		return f_util_ArrayUtils::firstElement($query->find());
	}
	
	/**
	 * @param forums_persistentdocument_forum $forum
	 * @return forums_persistentdocument_post
	 */
	public function getInfosRecursive($forum)
	{
		$query = forums_ForumService::getInstance()->createQuery();
		$query->add(Restrictions::orExp(Restrictions::eq('id', $forum->getId()), Restrictions::descendentOf($forum->getId())));
		$query->setProjection(Projections::sum('nbpost', 'nbpostrecursive'), Projections::sum('nbthread', 'nbthreadrecursive'));
		return f_util_ArrayUtils::firstElement($query->find());
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId
	 */
	protected function postSave($document, $parentNodeId)
	{
		parent::postSave($document, $parentNodeId);
		
		// Recompile locked and excludeFromRss if needed.
		if ($document->isPropertyModified('locked'))
		{
			$this->refreshPropertyOnChildren($document, 'locked');
		}
		if ($document->isPropertyModified('excludeFromRss'))
		{
			$this->refreshPropertyOnChildren($document, 'excludeFromRss');
		}
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
	 * @return void
	 */
	protected function postUpdate($document, $parentNodeId)
	{
		// Update related topics.
		if ($document->isPropertyModified('description') || $document->isPropertyModified('label'))
		{
			$topic = $document->getTopic();
			$topic->setLabel($document->getLabel());
			$topic->setDescription($document->getDescription());
			$topic->save();
		}
	}
	
	/**
	 * @param forums_persistentdocument_forumgroup $document
	 * @param string $propertyName
	 */
	private function refreshPropertyOnChildren($document, $propertyName)
	{
		$setter = 'set'.ucfirst($propertyName);
		$getter = 'get'.ucfirst($propertyName);
		$query = $this->createQuery()->add(Restrictions::eq($propertyName.'Mode', forums_ModuleService::EXTBOOL_INHERIT))
				->add(Restrictions::childOf($document->getId()));
		foreach ($query->find() as $forum)
		{
{'fraction_non_alphanumeric': 0.15275772451631534, 'fraction_numerical': 0.0006497256713831938, 'mean_word_length': 5.455265610438024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 160, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5923713', 'n_tokens_mistral': 4626, 'n_tokens_neox': 4095, 'n_words': 729}