<commit_before>// (C) Copyright 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

angular.module('mm.addons.mod_data')

/**
 * Helper to gather some common functions for database.
 *
 * @module mm.addons.mod_data
 * @ngdoc service
 * @name $mmaModDataHelper
 */
.factory('$mmaModDataHelper', function($mmaModData, $mmaModDataFieldsDelegate, $q, mmaModDataComponent, $mmFileUploader, $mmSite,
        $mmaModDataOffline, $mmFS, $mmFileUploaderHelper, $mmSitesManager) {

    var self = {
            searchOther: {
                'fn': 'firstname',
                'ln': 'lastname'
            }
        };

    /**
     * Displays fields for being shown.
     *
     * @module mm.addons.mod_data
     * @ngdoc method
     * @name $mmaModDataHelper#displayShowFields
     * @param {String} template   Template HMTL.
     * @param {Array}  fields     Fields that defines every content in the entry.
     * @param {Number} entryId    Entry ID.
     * @param {String} mode       Mode list or show.
     * @param {Object} actions    Actions that can be performed to the record.
     * @return {String}           Generated HTML.
     */
    self.displayShowFields = function(template, fields, entryId, mode, actions) {
        if (!template) {
            return "";
        }

        var replace, render;

        // Replace the fields found on template.
        angular.forEach(fields, function(field) {
            replace = "[[" + field.name + "]]";
            replace = replace.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
            replace = new RegExp(replace, 'gi');

            // Replace field by a generic directive.
            render = '<mma-mod-data-field mode="'+mode+'" field="fields['+ field.id + ']" value="entries['+ entryId +'].contents['+ field.id + ']" database="data" view-action="gotoEntry('+ entryId +')"></mma-mod-data-field>';
            template = template.replace(replace, render);
        });

        angular.forEach(actions, function(enabled, action) {
            replace = new RegExp("##" + action + "##", 'gi');
            if (enabled) {
                if (action == "moreurl") {
                    // Render more url directly because it can be part of an HTML attribute.
                    render = $mmSite.getURL() + '/mod/data/view.php?d={{data.id}}&rid=' + entryId;
                } else {
                    render = '<mma-mod-data-action action="' + action + '" entry="entries['+ entryId +']" database="data"></mma-mod-data-action>';
                }
                template = template.replace(replace, render);
            } else {
                template = template.replace(replace, "");
            }
        });

        return template;
    };

    /**
     * Returns an object with all the actions that the user can do over the record.
     *
     * @module mm.addons.mod_data
     * @ngdoc method
     * @name $mmaModDataHelper#getActions
     * @param {Object}  database     Database activity.
     * @param {Object}  accessInfo   Access info to the activity.
     * @param {Object}  record       Entry or record where the actions will be performed.
     * @return {Object}              Keyed with the action names and boolean to evalute if it can or cannot be done.
     */
    self.getActions = function(database, accessInfo, record) {
        var replacements = {};

        replacements.more = true;
        replacements.moreurl = true;
        replacements.user = true;
        replacements.userpicture = true;
        replacements.timeadded = true;
        replacements.timemodified = true;

        replacements.edit = record.canmanageentry; // This already checks capabilities and readonly period.
        replacements.delete = replacements.edit;
        replacements.approve = database.approval && accessInfo.canapprove && !record.approved;
        replacements.disapprove = database.approval && accessInfo.canapprove && record.approved;

        replacements.approvalstatus = database.approval;
        replacements.comments = database.comments;

        // Unsupported actions.
        replacements.delcheck = false;<commit_msg>MOBILE-2178 database: Do not allow to edit a deleted offline entry<commit_after>// (C) Copyright 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

angular.module('mm.addons.mod_data')

/**
 * Helper to gather some common functions for database.
 *
 * @module mm.addons.mod_data
 * @ngdoc service
 * @name $mmaModDataHelper
 */
.factory('$mmaModDataHelper', function($mmaModData, $mmaModDataFieldsDelegate, $q, mmaModDataComponent, $mmFileUploader, $mmSite,
        $mmaModDataOffline, $mmFS, $mmFileUploaderHelper, $mmSitesManager) {

    var self = {
            searchOther: {
                'fn': 'firstname',
                'ln': 'lastname'
            }
        };

    /**
     * Displays fields for being shown.
     *
     * @module mm.addons.mod_data
     * @ngdoc method
     * @name $mmaModDataHelper#displayShowFields
     * @param {String} template   Template HMTL.
     * @param {Array}  fields     Fields that defines every content in the entry.
     * @param {Number} entryId    Entry ID.
     * @param {String} mode       Mode list or show.
     * @param {Object} actions    Actions that can be performed to the record.
     * @return {String}           Generated HTML.
     */
    self.displayShowFields = function(template, fields, entryId, mode, actions) {
        if (!template) {
            return "";
        }

        var replace, render;

        // Replace the fields found on template.
        angular.forEach(fields, function(field) {
            replace = "[[" + field.name + "]]";
            replace = replace.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
            replace = new RegExp(replace, 'gi');

            // Replace field by a generic directive.
            render = '<mma-mod-data-field mode="'+mode+'" field="fields['+ field.id + ']" value="entries['+ entryId +'].contents['+ field.id + ']" database="data" view-action="gotoEntry('+ entryId +')"></mma-mod-data-field>';
            template = template.replace(replace, render);
        });

        angular.forEach(actions, function(enabled, action) {
            replace = new RegExp("##" + action + "##", 'gi');
            if (enabled) {
                if (action == "moreurl") {
                    // Render more url directly because it can be part of an HTML attribute.
                    render = $mmSite.getURL() + '/mod/data/view.php?d={{data.id}}&rid=' + entryId;
                } else {
                    render = '<mma-mod-data-action action="' + action + '" entry="entries['+ entryId +']" database="data"></mma-mod-data-action>';
                }
                template = template.replace(replace, render);
            } else {
                template = template.replace(replace, "");
            }
        });

        return template;
    };

    /**
     * Returns an object with all the actions that the user can do over the record.
     *
     * @module mm.addons.mod_data
     * @ngdoc method
     * @name $mmaModDataHelper#getActions
     * @param {Object}  database     Database activity.
     * @param {Object}  accessInfo   Access info to the activity.
     * @param {Object}  record       Entry or record where the actions will be performed.
     * @return {Object}              Keyed with the action names and boolean to evalute if it can or cannot be done.
     */
    self.getActions = function(database, accessInfo, record) {
        var replacements = {};

        replacements.more = true;
        replacements.moreurl = true;
        replacements.user = true;
        replacements.userpicture = true;
        replacements.timeadded = true;
        replacements.timemodified = true;

        replacements.edit = record.canmanageentry && !record.deleted; // This already checks capabilities and readonly period.
        replacements.delete = record.canmanageentry;
        replacements.approve = database.approval && accessInfo.canapprove && !record.approved && !record.deleted;
        replacements.disapprove = database.approval && accessInfo.canapprove && record.approved && !record.deleted;

        replacements.approvalstatus = database.approval;
        replacements.comments = database.comments;

        // Unsupported actions.
        replacements.delcheck = false;
{'fraction_non_alphanumeric': 0.12459995732878174, 'fraction_numerical': 0.002133560913164071, 'mean_word_length': 2.299894403379092, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3001830', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2523, 'n_words': 764}