<commit_before><?php
/*---------------------------------------------------------------------------
 * @Project: Alto CMS
 * @Project URI: http://altocms.com
 * @Description: Advanced Community Engine
 * @Copyright: Alto CMS Team
 * @License: GNU GPL v2 & MIT
 *----------------------------------------------------------------------------
 */

/**
 * @package modules.mresource
 * @since   1.0
 */
class ModuleMresource_MapperMresource extends Mapper {

    /**
     * @param ModuleMresource_EntityMresource $oMresource
     *
     * @return int|bool
     */
    public function Add($oMresource) {

        $aParams = array(
            ':date_add' => F::Now(),
            ':user_id' => $oMresource->GetUserId(),
            ':link' => $oMresource->IsLink() ? 1 : 0,
            ':type' => $oMresource->GetType(),
            ':path_url' => $oMresource->GetPathUrl(),
            ':path_file' => $oMresource->GetPathFile(),
            ':hash_url' => $oMresource->GetHashUrl(),
            ':hash_file' => $oMresource->GetHashFile(),
            ':storage' => $oMresource->GetStorage(),
            ':uuid' => $oMresource->GetUuid(),
        );
        $sql = "
            SELECT mresource_id
            FROM ?_mresource
            WHERE
                storage = ?:storage AND uuid = ?:uuid
            LIMIT 1
            ";
        $nId = $this->oDb->sqlSelectCell($sql, $aParams);
        if (!$nId) {
            $sql = "
            INSERT INTO ?_mresource
            (
                date_add,
                user_id,
                link,
                type,
                path_url,
                path_file,
                hash_url,
                hash_file,
                storage,
                uuid
            )
            VALUES (
                ?:date_add,
                ?d:user_id,
                ?d:link,
                ?d:type,
                ?:path_url,
                ?:path_file,
                ?:hash_url,
                ?:hash_file,
                ?:storage,
                ?:uuid
            )
            ";
            $nId = $this->oDb->sqlQuery($sql, $aParams);
        }
        return $nId ? $nId : false;
    }

    /**
     * @param string $sTargetTmp
     * @param int    $iTargetId
     *
     * @return bool
     */
    public function ResetTmpRelById($sTargetTmp, $iTargetId) {

        return $this->oDb->query(
          'UPDATE ?_mresource_target SET target_tmp = null, target_id = ?d  where target_tmp = ?',
          $iTargetId,
          $sTargetTmp
        ) !== false;
    }

    /**
     * @param ModuleMresource_EntityMresource $oMresource
     *
     * @return bool
     */
    public function AddTargetRel($oMresource) {

        $aParams = array(
            ':id' => $oMresource->GetMresourceId(),
            ':target_type' => $oMresource->GetTargetType(),
            ':target_id' => $oMresource->GetTargetId(),
            ':date_add' => F::Now(),
            ':description' => $oMresource->GetDescription(),
            ':target_tmp' => $oMresource->GetTargetTmp(),
            ':incount' => $oMresource->GetIncount() ? $oMresource->GetIncount() : 1,
        );
        $sql = "
            SELECT mresource_id
            FROM ?_mresource_target
            WHERE
                target_type = ?:target_type
                AND target_id = ?d:target_id
                AND mresource_id = ?d:id
            LIMIT 1
        ";
        if ($iId = $this->oDb->sqlSelectCell($sql, $aParams)) {
            $sql = "
                UPDATE ?_mresource_target
                SET incount=incount+?d:incount
                WHERE mresource_id = ?d:id
            ";
            if ($this->oDb->sqlQuery($sql, $aParams)) {
                return $iId;
            }
        } else {
            $sql = "
                INSERT INTO ?_mresource_target
                (
                    mresource_id,
                    target_type,
                    target_id,
                    date_add,
                    description,
                    target_tmp,
                    incount
                )
                VALUES (
                    ?d:id,
                    ?:target_type,
                    ?d:target_id,
                    ?:date_add,
                    ?:description,
                    ?:target_tmp,
                    ?d:incount
                )
            ";
            if ($iId = $this->oDb->sqlQuery($sql, $aParams)) {
                return $iId ? $iId : false;
            }
        }
        return false;
    }

    /**
     * @param array $aCriteria
     *
     * @return array
     */
    protected function _getMresourcesRelByCriteria($aCriteria) {

        $aFilter = (isset($aCriteria['filter']) ? $aCriteria['filter'] : array());
        if (isset($aCriteria['fields'])) {
            if (is_array($aCriteria['filter'])) {
                $sFields = implode(',', $aCriteria['filter']);
            } else {
                $sFields = (string)$aCriteria['filter'];
            }
        } else {
            $sFields = 'mrt.*, mr.*';
        }
        $oSql = $this->oDb->sql("
            SELECT
                id AS ARRAY_KEY,
                $sFields
            FROM ?_mresource_target AS mrt
                INNER JOIN ?_mresource AS mr ON mr.mresource_id=mrt.mresource_id
            WHERE
                1=1
                {AND mrt.id=?d:id}
                {AND mrt.id IN (?a:ids)}
                {AND mrt.mresource_id=(?d:mresource_id)}
                {AND mrt.mresource_id IN (?a:mresource_ids)}
                {AND mrt.target_type=?:target_type}
                {AND mrt.target_type IN (?a:target_types)}<commit_msg>Fix: в функции выбора связи медиаресурса, использовалcя ключ filter вместо fields для выбора запрашиваемых полей<commit_after><?php
/*---------------------------------------------------------------------------
 * @Project: Alto CMS
 * @Project URI: http://altocms.com
 * @Description: Advanced Community Engine
 * @Copyright: Alto CMS Team
 * @License: GNU GPL v2 & MIT
 *----------------------------------------------------------------------------
 */

/**
 * @package modules.mresource
 * @since   1.0
 */
class ModuleMresource_MapperMresource extends Mapper {

    /**
     * @param ModuleMresource_EntityMresource $oMresource
     *
     * @return int|bool
     */
    public function Add($oMresource) {

        $aParams = array(
            ':date_add' => F::Now(),
            ':user_id' => $oMresource->GetUserId(),
            ':link' => $oMresource->IsLink() ? 1 : 0,
            ':type' => $oMresource->GetType(),
            ':path_url' => $oMresource->GetPathUrl(),
            ':path_file' => $oMresource->GetPathFile(),
            ':hash_url' => $oMresource->GetHashUrl(),
            ':hash_file' => $oMresource->GetHashFile(),
            ':storage' => $oMresource->GetStorage(),
            ':uuid' => $oMresource->GetUuid(),
        );
        $sql = "
            SELECT mresource_id
            FROM ?_mresource
            WHERE
                storage = ?:storage AND uuid = ?:uuid
            LIMIT 1
            ";
        $nId = $this->oDb->sqlSelectCell($sql, $aParams);
        if (!$nId) {
            $sql = "
            INSERT INTO ?_mresource
            (
                date_add,
                user_id,
                link,
                type,
                path_url,
                path_file,
                hash_url,
                hash_file,
                storage,
                uuid
            )
            VALUES (
                ?:date_add,
                ?d:user_id,
                ?d:link,
                ?d:type,
                ?:path_url,
                ?:path_file,
                ?:hash_url,
                ?:hash_file,
                ?:storage,
                ?:uuid
            )
            ";
            $nId = $this->oDb->sqlQuery($sql, $aParams);
        }
        return $nId ? $nId : false;
    }

    /**
     * @param string $sTargetTmp
     * @param int    $iTargetId
     *
     * @return bool
     */
    public function ResetTmpRelById($sTargetTmp, $iTargetId) {

        return $this->oDb->query(
          'UPDATE ?_mresource_target SET target_tmp = null, target_id = ?d  where target_tmp = ?',
          $iTargetId,
          $sTargetTmp
        ) !== false;
    }

    /**
     * @param ModuleMresource_EntityMresource $oMresource
     *
     * @return bool
     */
    public function AddTargetRel($oMresource) {

        $aParams = array(
            ':id' => $oMresource->GetMresourceId(),
            ':target_type' => $oMresource->GetTargetType(),
            ':target_id' => $oMresource->GetTargetId(),
            ':date_add' => F::Now(),
            ':description' => $oMresource->GetDescription(),
            ':target_tmp' => $oMresource->GetTargetTmp(),
            ':incount' => $oMresource->GetIncount() ? $oMresource->GetIncount() : 1,
        );
        $sql = "
            SELECT mresource_id
            FROM ?_mresource_target
            WHERE
                target_type = ?:target_type
                AND target_id = ?d:target_id
                AND mresource_id = ?d:id
            LIMIT 1
        ";
        if ($iId = $this->oDb->sqlSelectCell($sql, $aParams)) {
            $sql = "
                UPDATE ?_mresource_target
                SET incount=incount+?d:incount
                WHERE mresource_id = ?d:id
            ";
            if ($this->oDb->sqlQuery($sql, $aParams)) {
                return $iId;
            }
        } else {
            $sql = "
                INSERT INTO ?_mresource_target
                (
                    mresource_id,
                    target_type,
                    target_id,
                    date_add,
                    description,
                    target_tmp,
                    incount
                )
                VALUES (
                    ?d:id,
                    ?:target_type,
                    ?d:target_id,
                    ?:date_add,
                    ?:description,
                    ?:target_tmp,
                    ?d:incount
                )
            ";
            if ($iId = $this->oDb->sqlQuery($sql, $aParams)) {
                return $iId ? $iId : false;
            }
        }
        return false;
    }

    /**
     * @param array $aCriteria
     *
     * @return array
     */
    protected function _getMresourcesRelByCriteria($aCriteria) {

        $aFilter = (isset($aCriteria['filter']) ? $aCriteria['filter'] : array());
        if (isset($aCriteria['fields'])) {
            if (is_array($aCriteria['fields'])) {
                $sFields = implode(',', $aCriteria['fields']);
            } else {
                $sFields = (string)$aCriteria['fields'];
            }
        } else {
            $sFields = 'mrt.*, mr.*';
        }
        $oSql = $this->oDb->sql("
            SELECT
                id AS ARRAY_KEY,
                $sFields
            FROM ?_mresource_target AS mrt
                INNER JOIN ?_mresource AS mr ON mr.mresource_id=mrt.mresource_id
            WHERE
                1=1
                {AND mrt.id=?d:id}
                {AND mrt.id IN (?a:ids)}
                {AND mrt.mresource_id=(?d:mresource_id)}
                {AND mrt.mresource_id IN (?a:mresource_ids)}
                {AND mrt.target_type=?:target_type}
                {AND mrt.target_type IN (?a:target_types)}
{'fraction_non_alphanumeric': 0.14853134590092065, 'fraction_numerical': 0.0017536168347216134, 'mean_word_length': 1.1913544668587897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '3750664', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3163, 'n_words': 591}