All files / web/bundles/pimui/js/form/common save-form.js

78.79% Statements 26/33
92.86% Branches 13/14
57.14% Functions 4/7
78.79% Lines 26/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                  105x                                                     105x         122x   122x                                                                     65x 65x   65x   65x             65x 65x   65x 65x 65x 48x 19x   29x     65x 65x     65x         53x 53x 53x   53x 33x 33x   33x                  
'use strict';
 
/**
 * Save extension for simple entity types
 *
 * @author    Tamara Robichet <tamara.robichet@akeneo.com>
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'pim/form/common/save',
        'oro/messenger',
        'pim/saver/entity-saver',
        'pim/field-manager',
        'pim/i18n',
        'pim/user-context',
        'pim/router',
        'pim/common/property'
    ],
    function (
        $,
        _,
        __,
        BaseSave,
        messenger,
        EntitySaver,
        FieldManager,
        i18n,
        UserContext,
        router,
        propertyAccessor
    ) {
        return BaseSave.extend({
            /**
             * Sets message labels for updates
             */
            configure: function () {
                this.notReadyMessage = __(this.config.notReadyMessage);
 
                return BaseSave.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Given an array of fields, return the translation for each in a map
             *
             * @param  {Array} fields         An array of field objects
             * @param  {String} catalogLocale The locale
             * @return {Array}                An array of labels
             */
            getFieldLabels: function (fields, catalogLocale) {
                return _.map(fields, function (field) {
                    return i18n.getLabel(
                        field.attribute.label,
                        catalogLocale,
                        field.attribute.code
                    );
                });
            },
 
            /**
             * Shows an error message for the given message text and labels
             *
             * @param  {String} message The given error message
             * @param  {Array} labels   An array of field names
             */
            showFlashMessage: function (message, labels) {
                var flash = __(message, { 'fields': labels.join(', ') });
                messenger.notify('error', flash);
            },
 
            /**
             * {@inheritdoc}
             */
            save: function () {
                var excludedProperties = _.union(this.config.excludedProperties, ['meta']);
                var entity = _.omit(this.getFormData(), excludedProperties);
 
                var notReadyFields = FieldManager.getNotReadyFields();
 
                Iif (0 < notReadyFields.length) {
                    var catalogLocale = UserContext.get('catalogLocale');
                    var fieldLabels = this.getFieldLabels(notReadyFields, catalogLocale);
 
                    return this.showFlashMessage(this.notReadyMessage, fieldLabels);
                }
 
                this.showLoadingMask();
                this.getRoot().trigger('pim_enrich:form:entity:pre_save');
 
                const entityIdProperty = this.config.entityIdentifierParamName || 'code';
                let identifierProperty = 'identifier';
                if (this.config.identifierParamName !== undefined) {
                    if (this.config.identifierParamName === 'undefined') {
                        identifierProperty = undefined;
                    } else {
                        identifierProperty = this.configure.identifierParamName;
                    }
                }
                const entityId = propertyAccessor.accessProperty(this.getFormData(), entityIdProperty, '');
                const redirectAfterRouteIdentifierParamName = this.config.redirectAfterRouteIdentifierParamName
                    || 'identifier';
 
                return EntitySaver
                    .setUrl(this.config.url)
                    .setIdentifierProperty(identifierProperty)
                    .save(entityId, entity, this.config.method || 'POST')
                    .then(function (data) {
                        this.postSave(data);
                        this.setData(data);
                        this.getRoot().trigger('pim_enrich:form:entity:post_fetch', data);
 
                        if (this.config.redirectAfter) {
                            var params = {};
                            params[redirectAfterRouteIdentifierParamName] = entityId;
 
                            router.redirectToRoute(this.config.redirectAfter, params);
                        }
                    }.bind(this))
                    .fail(this.fail.bind(this))
                    .always(this.hideLoadingMask.bind(this));
            }
        });
    }
);