<commit_before><?php

namespace Craft;

class VisitsCountField_VisitsCountService extends BaseApplicationComponent
{
    /**
     * Increment the visits count
     *
     * @param EntryModel $entry
     */
    public function increment(EntryModel $entry)
    {
        // Refresh the entry
        /** @var EntryModel $entry */
        $entry = craft()->entries->getEntryById($entry->id);

        $sessionId = session_id();
        if (!$this->isSessionRegisteredForEntry($sessionId, $entry)) {
            /** @var FieldLayoutFieldModel[] $fields */
            $fields = $entry->getFieldLayout()->getFields();

            foreach ($fields as $fieldLayoutModel) {
                $value = null;

                $field = $fieldLayoutModel->getField();
                $handle = $field->getAttribute('handle');

                // If the field is for storing visits, let's store it there.
                if ($field->getFieldType() instanceof VisitsCountField_VisitsCountFieldType) {
                    $value = $entry->getContent()->getAttribute($handle);
                    if ($value === null) {
                        $value = 0;
                    }
                    $value++;
                } elseif (
                    $field->getFieldType() instanceof CategoriesFieldType
                    || $field->getFieldType() instanceof AssetsFieldType
                ) {
                    // Hack for validation errors on relations with categories and assets
                    // See http://craftcms.stackexchange.com/a/11146
                    $value = $entry->{$handle}->ids();
                }

                if ($handle && $value) {
                    $entry->setContentFromPost([
                        $handle => $value
                    ]);
                }
            }

            if (craft()->entries->saveEntry($entry)) {
                $this->registerSessionForEntry($sessionId, $entry);
            } else {
                VisitsCountFieldPlugin::log(
                    sprintf('Cannot save entry for visits count: %s', print_r($entry->getAllErrors(), true))
                );
            }
        }
    }

    /**
     * @param string $sessionId
     * @param EntryModel $entry
     * @return bool
     */
    private function isSessionRegisteredForEntry($sessionId, EntryModel $entry)
    {
        $record = new VisitsCountField_VisitsCountHistoryRecord();

        return $record->find(
            sprintf(
                'sessionId = "%s" AND entry = %s',
                addslashes($sessionId),
                addslashes($entry->id)
            )
        ) !== null;
    }

    /**
     * @param string $sessionId
     * @param EntryModel $entry
     */
    private function registerSessionForEntry($sessionId, EntryModel $entry)
    {
        $model = new VisitsCountField_VisitsCountHistoryModel();
        $model->setAttributes([
            'entry' => $entry,
            'sessionId' => $sessionId
        ]);
        if ($model->validate()) {
            $record = new VisitsCountField_VisitsCountHistoryRecord();
            $record->entry = $model->entry->id;
            $record->sessionId = $model->sessionId;
            if (!$record->save()) {
                VisitsCountFieldPlugin::log(
                    sprintf('Cannot save visists count history record; %s', print_r($record->getErrors(), true))
                );
            }
        }
    }
}<commit_msg>Don't increment non-saved entry (crashes live preview mode)<commit_after><?php

namespace Craft;

class VisitsCountField_VisitsCountService extends BaseApplicationComponent
{
    /**
     * Increment the visits count
     *
     * @param EntryModel $entry
     */
    public function increment(EntryModel $entry)
    {
        // Don't do anything in preview mode
        if (!$entry->id) return;

        // Refresh the entry
        /** @var EntryModel $entry */
        $entry = craft()->entries->getEntryById($entry->id);

        $sessionId = session_id();
        if (!$this->isSessionRegisteredForEntry($sessionId, $entry)) {
            /** @var FieldLayoutFieldModel[] $fields */
            $fields = $entry->getFieldLayout()->getFields();

            foreach ($fields as $fieldLayoutModel) {
                $value = null;

                $field = $fieldLayoutModel->getField();
                $handle = $field->getAttribute('handle');

                // If the field is for storing visits, let's store it there.
                if ($field->getFieldType() instanceof VisitsCountField_VisitsCountFieldType) {
                    $value = $entry->getContent()->getAttribute($handle);
                    if ($value === null) {
                        $value = 0;
                    }
                    $value++;
                } elseif (
                    $field->getFieldType() instanceof CategoriesFieldType
                    || $field->getFieldType() instanceof AssetsFieldType
                ) {
                    // Hack for validation errors on relations with categories and assets
                    // See http://craftcms.stackexchange.com/a/11146
                    $value = $entry->{$handle}->ids();
                }

                if ($handle && $value) {
                    $entry->setContentFromPost([
                        $handle => $value
                    ]);
                }
            }

            if (craft()->entries->saveEntry($entry)) {
                $this->registerSessionForEntry($sessionId, $entry);
            } else {
                VisitsCountFieldPlugin::log(
                    sprintf('Cannot save entry for visits count: %s', print_r($entry->getAllErrors(), true))
                );
            }
        }
    }

    /**
     * @param string $sessionId
     * @param EntryModel $entry
     * @return bool
     */
    private function isSessionRegisteredForEntry($sessionId, EntryModel $entry)
    {
        $record = new VisitsCountField_VisitsCountHistoryRecord();

        return $record->find(
            sprintf(
                'sessionId = "%s" AND entry = %s',
                addslashes($sessionId),
                addslashes($entry->id)
            )
        ) !== null;
    }

    /**
     * @param string $sessionId
     * @param EntryModel $entry
     */
    private function registerSessionForEntry($sessionId, EntryModel $entry)
    {
        $model = new VisitsCountField_VisitsCountHistoryModel();
        $model->setAttributes([
            'entry' => $entry,
            'sessionId' => $sessionId
        ]);
        if ($model->validate()) {
            $record = new VisitsCountField_VisitsCountHistoryRecord();
            $record->entry = $model->entry->id;
            $record->sessionId = $model->sessionId;
            if (!$record->save()) {
                VisitsCountFieldPlugin::log(
                    sprintf('Cannot save visists count history record; %s', print_r($record->getErrors(), true))
                );
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1210473601373587, 'fraction_numerical': 0.0017169838317355845, 'mean_word_length': 1.5936920222634507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, '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': '7052161', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1707, 'n_words': 372}