<commit_before><?php
/**
 * Phanbook : Delightfully simple forum software
 *
 * Licensed under The GNU License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @link    http://phanbook.com Phanbook Project
 * @since   1.0.0
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
namespace Phanbook\Models\Behavior;

use Phalcon\Mvc\Model\Behavior;
use Phalcon\Mvc\Model\BehaviorInterface;
use Phalcon\Mvc\ModelInterface;
use Phalcon\Security\Random;
use Phalcon\Logger\Adapter\File as Logger;

use Phanbook\Models\Audit;
use Phanbook\Models\AuditDetail;

/**
 * Phanbook\Model\Behavior\Blameable
 *
 */
class Blameable extends Behavior implements BehaviorInterface
{
    /**
     * {@inheritdoc}
     *
     * @param string                      $eventType
     * @param \Phalcon\Mvc\ModelInterface $model
     */
    public function notify($eventType, ModelInterface $model)
    {
        //Fires 'logAfterUpdate' if the event is 'afterCreate'
        if ($eventType == 'afterCreate') {
            return $this->auditAfterCreate($model);
        }

        //Fires 'logAfterUpdate' if the event is 'afterUpdate'
        if ($eventType == 'afterUpdate') {
            return $this->auditAfterUpdate($model);
        }
    }

    /**
     * Creates an Audit isntance based on the current enviroment
     *
     * @param  string                      $type
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return Audit
     */
    public function createAudit($type, ModelInterface $model)
    {
        //Get the session service
        $auth    = $model->getDI()->getAuth();
        if (!empty($auth->getUserId())) {
            //Get the request service
            $request = $model->getDI()->getRequest();
            $random  = new Random();
            $audit   = new Audit();

            $audit->setId($random->uuid());
            $audit->setUserId($auth->getUserId());
            //The model who performed the action
            $audit->setModelname(get_class($model));

            //The client IP address
            $audit->setIpaddress(ip2long($request->getClientAddress()));

            //Action is an update
            $audit->setType($type);

            //Current time
            $audit->setCreatedAt(date('Y-m-d H:i:s'));
            return $audit;
        }

    }

    /**
     * Audits an DELETE operation
     *
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return boolean
     */
    public function auditAfterCreate(ModelInterface $model)
    {
        //Create a new audit
        $audit = $this->createAudit('C', $model);
        if (is_object($audit)) {
            $metaData = $model->getModelsMetaData();
            $fields   = $metaData->getAttributes($model);
            $details  = array();

            foreach ($fields as $field) {
                $auditDetail = new AuditDetail();
                $auditDetail->setFieldName($field);
                $auditDetail->setOldValue(null);
                $auditDetail->setNewValue($model->readAttribute($field));

                $details[] = $auditDetail;
            }
            $audit->details = $details;
            if (!$audit->save()) {
                $this->saveLoger($audit->getMessages());
            }
        }
    }

    /**
     * Audits an UPDATE operation
     *
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return boolean
     */
    public function auditAfterUpdate(ModelInterface $model)
    {
        $changedFields = $model->getChangedFields();

        if (count($changedFields) == 0) {
            return null;<commit_msg>Fix columns id not null<commit_after><?php
/**
 * Phanbook : Delightfully simple forum software
 *
 * Licensed under The GNU License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @link    http://phanbook.com Phanbook Project
 * @since   1.0.0
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
namespace Phanbook\Models\Behavior;

use Phalcon\Mvc\Model\Behavior;
use Phalcon\Mvc\Model\BehaviorInterface;
use Phalcon\Mvc\ModelInterface;
use Phalcon\Security\Random;
use Phalcon\Logger\Adapter\File as Logger;

use Phanbook\Models\Audit;
use Phanbook\Models\AuditDetail;

/**
 * Phanbook\Model\Behavior\Blameable
 *
 */
class Blameable extends Behavior implements BehaviorInterface
{
    /**
     * {@inheritdoc}
     *
     * @param string                      $eventType
     * @param \Phalcon\Mvc\ModelInterface $model
     */
    public function notify($eventType, ModelInterface $model)
    {
        //Fires 'logAfterUpdate' if the event is 'afterCreate'
        if ($eventType == 'afterCreate') {
            return $this->auditAfterCreate($model);
        }

        //Fires 'logAfterUpdate' if the event is 'afterUpdate'
        if ($eventType == 'afterUpdate') {
            return $this->auditAfterUpdate($model);
        }
    }

    /**
     * Creates an Audit isntance based on the current enviroment
     *
     * @param  string                      $type
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return Audit
     */
    public function createAudit($type, ModelInterface $model)
    {
        //Get the session service
        $auth    = $model->getDI()->getAuth();
        if (!empty($auth->getUserId())) {
            //Get the request service
            $request = $model->getDI()->getRequest();
            $random  = new Random();
            $audit   = new Audit();

            $audit->setId($random->uuid());
            $audit->setUserId($auth->getUserId());
            //The model who performed the action
            $audit->setModelname(get_class($model));

            //The client IP address
            $audit->setIpaddress(ip2long($request->getClientAddress()));

            //Action is an update
            $audit->setType($type);

            //Current time
            $audit->setCreatedAt(date('Y-m-d H:i:s'));
            return $audit;
        }

    }

    /**
     * Audits an DELETE operation
     *
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return boolean
     */
    public function auditAfterCreate(ModelInterface $model)
    {
        //Create a new audit
        $audit = $this->createAudit('C', $model);
        if (is_object($audit)) {
            $metaData = $model->getModelsMetaData();
            $fields   = $metaData->getAttributes($model);
            $details  = [];
            $random = new Random();

            foreach ($fields as $field) {
                $auditDetail = new AuditDetail();
                $auditDetail->setId($random->uuid());
                $auditDetail->setFieldName($field);
                $auditDetail->setOldValue(null);
                $auditDetail->setNewValue($model->readAttribute($field));

                $details[] = $auditDetail;
            }
            $audit->details = $details;
            if (!$audit->save()) {
                $this->saveLoger($audit->getMessages());
            }
        }
    }

    /**
     * Audits an UPDATE operation
     *
     * @param  \Phalcon\Mvc\ModelInterface $model
     * @return boolean
     */
    public function auditAfterUpdate(ModelInterface $model)
    {
        $changedFields = $model->getChangedFields();

        if (count($changedFields) == 0) {
            return null;
{'fraction_non_alphanumeric': 0.1283049255133539, 'fraction_numerical': 0.0018789424238357267, 'mean_word_length': 2.034201954397394, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '239268', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2108, 'n_words': 468}