<commit_before><?php
/*
 * This file is part of the AlphaLemon CMS Application and it is distributed
 * under the GPL LICENSE Version 2.0. To use this application you must leave
 * intact this copyright notice.
 *
 * Copyright (c) AlphaLemon <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * For extra documentation and help please visit http://www.alphalemon.com
 * 
 * @license    GPL LICENSE Version 2.0
 * 
 */

namespace AlphaLemon\AlphaLemonCmsBundle\Core\Listener\Page;

use AlphaLemon\AlphaLemonCmsBundle\Core\Event\Content\Page\BeforeAddPageCommitEvent;
use AlphaLemon\AlphaLemonCmsBundle\Core\Content\Template\AlTemplateManager;
use AlphaLemon\AlphaLemonCmsBundle\Core\Model\Orm\BlockModelInterface;
use AlphaLemon\AlphaLemonCmsBundle\Core\Model\Orm\LanguageModelInterface;

/**
 * Listen to the onBeforeAddPageCommit event to add the page's contents, when a new page is added
 *
 * @author AlphaLemon <<EMAIL>>
 */
class AddPageContentsListener
{
    private $languageModel;
    
    /**
     * Constructor
     * 
     * @param LanguageModelInterface $languageModel 
     */
    public function __construct(LanguageModelInterface $languageModel)
    {
        $this->languageModel = $languageModel;
    }
    
    /**
     * Adds the contents for the page when a new page is added, for each language of the site
     * 
     * @param BeforeAddPageCommitEvent $event
     * @throws Exception 
     */
    public function onBeforeAddPageCommit(BeforeAddPageCommitEvent $event)
    {
        if ($event->isAborted()) {
            return;
        }
        
        $pageManager = $event->getContentManager(); 
        $templateManager = $pageManager->getTemplateManager();
        $pageModel = $pageManager->getPageModel();
        try {
            $pageModel->startTransaction();
            $languages = $this->languageModel->activeLanguages();
            
            if (count($languages) > 0) {
                $result = true;
                // The min number of pages is setted to 1 because we are adding a page which has been saved but not 
                // committed so it counts as one
                $ignoreRepeatedSlots = $pageManager->getValidator()->hasPages(1); 
                $idPage = $pageManager->get()->getId();
                foreach ($languages as $alLanguage) {
                    $result = $templateManager->populate($alLanguage->getId(), $idPage, $ignoreRepeatedSlots);

                    if (!$result) break;    
                }

                if ($result) {
                    $pageModel->commit();
                }
                else {
                    $pageModel->rollBack();
                    
                    $event->abort();
                }
            }
        }
        catch(\Exception $e) {
            $event->abort();
            if (isset($pageModel) && $pageModel !== null) {
                $pageModel->rollBack();
            }
            
            throw $e;
        }
    }
}
<commit_msg>transaction is started only when there are blocks to add<commit_after><?php
/*
 * This file is part of the AlphaLemon CMS Application and it is distributed
 * under the GPL LICENSE Version 2.0. To use this application you must leave
 * intact this copyright notice.
 *
 * Copyright (c) AlphaLemon <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * For extra documentation and help please visit http://www.alphalemon.com
 *
 * @license    GPL LICENSE Version 2.0
 *
 */

namespace AlphaLemon\AlphaLemonCmsBundle\Core\Listener\Page;

use AlphaLemon\AlphaLemonCmsBundle\Core\Event\Content\Page\BeforeAddPageCommitEvent;
use AlphaLemon\AlphaLemonCmsBundle\Core\Content\Template\AlTemplateManager;
use AlphaLemon\AlphaLemonCmsBundle\Core\Model\Orm\BlockModelInterface;
use AlphaLemon\AlphaLemonCmsBundle\Core\Model\Orm\LanguageModelInterface;

/**
 * Listen to the onBeforeAddPageCommit event to add the page's contents, when a new page is added
 *
 * @author AlphaLemon <<EMAIL>>
 */
class AddPageContentsListener
{
    private $languageModel;

    /**
     * Constructor
     *
     * @param LanguageModelInterface $languageModel
     */
    public function __construct(LanguageModelInterface $languageModel)
    {
        $this->languageModel = $languageModel;
    }

    /**
     * Adds the contents for the page when a new page is added, for each language of the site
     *
     * @param BeforeAddPageCommitEvent $event
     * @throws Exception
     */
    public function onBeforeAddPageCommit(BeforeAddPageCommitEvent $event)
    {
        if ($event->isAborted()) {
            return;
        }

        $pageManager = $event->getContentManager();
        $templateManager = $pageManager->getTemplateManager();
        $pageModel = $pageManager->getPageModel();
        try {
            $languages = $this->languageModel->activeLanguages();
            if (count($languages) > 0) {
                $result = true;
                $pageModel->startTransaction();
                // The min number of pages is setted to 1 because we are adding a page which has been saved but not
                // committed so it counts as one
                $ignoreRepeatedSlots = $pageManager->getValidator()->hasPages(1);
                $idPage = $pageManager->get()->getId();
                foreach ($languages as $alLanguage) {
                    $result = $templateManager->populate($alLanguage->getId(), $idPage, $ignoreRepeatedSlots);

                    if (!$result) break;
                }

                if ($result) {
                    $pageModel->commit();
                }
                else {
                    $pageModel->rollBack();

                    $event->abort();
                }
            }
        }
        catch(\Exception $e) {
            $event->abort();
            if (isset($pageModel) && $pageModel !== null) {
                $pageModel->rollBack();
            }

            throw $e;
        }
    }
}

{'fraction_non_alphanumeric': 0.09972208598986432, 'fraction_numerical': 0.0022887036128821318, 'mean_word_length': 2.1150712830957232, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6545762', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1586, 'n_words': 435}