<commit_before><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright      Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license        GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link           git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Attachments;

use Nooku\Library;

/**
 * Attachment Database Row
 *
 * @author  <NAME> <https://nooku.assembla.com/profile/stevenrombauts>
 * @package Nooku\Component\Attachments
 */
class ModelEntityAttachment extends Library\ModelEntityRow
{
    public function save()
    {
        $return = parent::save();

        if ($return && $this->row && $this->table)
        {
            $relation                            = $this->getObject('com:attachments.model.entity.relation');
            $relation->attachments_attachment_id = $this->id;
            $relation->table                     = $this->table;
            $relation->row                       = $this->row;

            if (!$relation->load()) {
                $relation->save();
            }
        }

        // Save the thumbnail if the attachment is an image
        if ($this->file->isImage())
        {
            $thumbnail         = $this->getObject('com:files.model.entity.thumbnail');
            $thumbnail->source = $this->file;

            if (!file_exists($this->thumbnail_fullpath))
            {
                $thumbnail->setThumbnailSize(4 / 3)
                    ->generateThumbnail()
                    ->save($this->thumbnail_fullpath);
            }

            if (isset($this->x1) && isset($this->x2))
            {
                // Cropping existing thumbnail
                $thumbnail->setProperties(array(
                    'source' => $this->file,
                    'x1'     => $this->x1,
                    'x2'     => $this->x2,
                    'y1'     => $this->y1,
                    'y2'     => $this->y2
                ))
                    ->cropThumbnail()
                    ->save($this->thumbnail_fullpath);
            }
        }

        return $return;
    }

    public function delete()
    {
        $return = parent::delete();

        if ($return)
        {
            try
            {
                $query = array(
                    'container' => $this->container,
                    'name'      => array(
                        $this->path,
                        $this->thumbnail
                    )
                );

                $controller = $this->getObject('com:files.controller.file');
                $controller->getRequest()->setQuery($query);

                $controller->delete();
            }
            catch (Library\ControllerExceptionResourceNotFound $e) {}

            $this->getObject('com:attachments.database.table.relations')
                ->select(array('attachments_attachment_id' => $this->id))
                ->delete();
        }

        return $return;
    }

    public function getPropertRelation()
    {
        $relation = $this->getObject('com:attachments.database.table.relations')
            ->select(array('attachments_attachment_id' => $this->id), Library\Database::FETCH_ROW);

        return $relation;
    }

    public function getPropertyFile()
    {
        $file = $this->getObject('com:files.model.files')
            ->container($this->container)
            ->name($this->path)
            ->fetch();

        return $file;
    }

    public function getPropertyThumbnail()
    {
        $path            = pathinfo($this->path);
        $path['dirname'] = $path['dirname'] === '.' ? '' : $path['dirname'] . '/';

        $thumbnail = $path['dirname'] . $path['filename'] . '_thumb.' . $path['extension'];

        return $thumbnail;
    }

    public function getPropertyThumbnailFullpath()
    {
        return dirname($this->file->fullpath) . '/' . $this->thumbnail;
    }

    public function toArray()
    {
        $data              = parent::toArray();
        $data['thumbnail'] = $this->thumbnail;

        return $data;
    }
}<commit_msg>ModelEntityAttachment not creating relations since API changes and load() removed from Entity<commit_after><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright      Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license        GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link           git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Attachments;

use Nooku\Library;

/**
 * Attachment Database Row
 *
 * @author  <NAME> <https://nooku.assembla.com/profile/stevenrombauts>
 * @package Nooku\Component\Attachments
 */
class ModelEntityAttachment extends Library\ModelEntityRow
{
    public function save()
    {
        $return = parent::save();

        if ($return && $this->row && $this->table)
        {
            $properties = array(
                'attachments_attachment_id' => $this->id,
                'table' => $this->table,
                'row' => $this->row,
            );

            $relation = $this->getObject('com:attachments.model.relations')
                ->setState($properties)
                ->fetch();

            if($relation->isNew())
            {
                $relation = $this->getObject('com:attachments.model.relations')->create();
                $relation->setProperties($properties);
                $relation->save();
            }
        }

        // Save the thumbnail if the attachment is an image
        if ($this->file->isImage())
        {
            $thumbnail         = $this->getObject('com:files.model.entity.thumbnail');
            $thumbnail->source = $this->file;

            if (!file_exists($this->thumbnail_fullpath))
            {
                $thumbnail->setThumbnailSize(4 / 3)
                    ->generateThumbnail()
                    ->save($this->thumbnail_fullpath);
            }

            if (isset($this->x1) && isset($this->x2))
            {
                // Cropping existing thumbnail
                $thumbnail->setProperties(array(
                    'source' => $this->file,
                    'x1'     => $this->x1,
                    'x2'     => $this->x2,
                    'y1'     => $this->y1,
                    'y2'     => $this->y2
                ))
                    ->cropThumbnail()
                    ->save($this->thumbnail_fullpath);
            }
        }

        return $return;
    }

    public function delete()
    {
        $return = parent::delete();

        if ($return)
        {
            try
            {
                $query = array(
                    'container' => $this->container,
                    'name'      => array(
                        $this->path,
                        $this->thumbnail
                    )
                );

                $controller = $this->getObject('com:files.controller.file');
                $controller->getRequest()->setQuery($query);

                $controller->delete();
            }
            catch (Library\ControllerExceptionResourceNotFound $e) {}

            $this->getObject('com:attachments.database.table.relations')
                ->select(array('attachments_attachment_id' => $this->id))
                ->delete();
        }

        return $return;
    }

    public function getPropertRelation()
    {
        $relation = $this->getObject('com:attachments.database.table.relations')
            ->select(array('attachments_attachment_id' => $this->id), Library\Database::FETCH_ROW);

        return $relation;
    }

    public function getPropertyFile()
    {
        $file = $this->getObject('com:files.model.files')
            ->container($this->container)
            ->name($this->path)
            ->fetch();

        return $file;
    }

    public function getPropertyThumbnail()
    {
        $path            = pathinfo($this->path);
        $path['dirname'] = $path['dirname'] === '.' ? '' : $path['dirname'] . '/';

        $thumbnail = $path['dirname'] . $path['filename'] . '_thumb.' . $path['extension'];

        return $thumbnail;
    }

    public function getPropertyThumbnailFullpath()
    {
        return dirname($this->file->fullpath) . '/' . $this->thumbnail;
    }

    public function toArray()
    {
        $data              = parent::toArray();
        $data['thumbnail'] = $this->thumbnail;

        return $data;
    }
}
{'fraction_non_alphanumeric': 0.1465648854961832, 'fraction_numerical': 0.004932472108044627, 'mean_word_length': 1.5512282804074295, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 148, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, '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': '4146433', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2280, 'n_words': 381}