--- /dev/null
+'use strict';
+
+alloWebadminControllers
+ .controller('ProductListCtrl', ['$scope', '$routeParams', '$http', 'Product',
+ function ($scope, $routeParams, $http, Product) {
+ $scope.productsPerPage = 10;
+ $scope.itemsPerPage = $scope.productsPerPage;
+ $scope.orderProp = 'name';
+ $scope.showEditForm = 0;
+ $scope.products = [];
+ getResultsPage(1);
+
+ $scope.pagination = {
+ current: 1
+ };
+
+ $scope.pageChanged = function() {
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.productCreated = function() {
+ createOrUpdateProduct();
+ $scope.showAddForm = false;
+ };
+
+ $scope.productUpdated = function(id) {
+ createOrUpdateProduct(id);
+ $scope.showEditForm = 0;
+ };
+
+ $scope.productEditLaunched = function(id) {
+ $scope.showEditForm = id;
+ $scope.showAddForm = false;
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.productCreateLaunched = function() {
+ $scope.showEditForm = 0;
+ $scope.showAddForm = true;
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.productDeleted = function(id) {
+ $scope.showEditForm = 0;
+ $scope.showAddForm = false;
+ if(confirm('Supprimer le produit ?') == true) {
+ deleteProduct(id);
+ }
+ };
+
+ $scope.actionCanceled = function() {
+ $scope.showEditForm = 0;
+ $scope.showAddForm = false;
+ };
+
+ function getResultsPage(pageNumber) {
+ var offset = (pageNumber-1)*$scope.productsPerPage;
+ if(offset == null || offset == undefined || offset < 0) {
+ offset = 0;
+ }
+ Product.all({limit:$scope.productsPerPage, offset:offset}).$promise.then(function(data) {
+ $scope.products = data;
+ });
+ Product.nb().$promise.then(function(data) {
+ $scope.totalProducts = data;
+ $scope.totalItems = $scope.totalProducts.result;
+ });
+ $scope.newProduct = {
+ 'name':'',
+ 'diminutif':''
+ };
+ }
+
+ function createOrUpdateProduct(id) {
+ if(id != null && id != undefined && id > 0) {
+ Product.update({name: $scope.newProduct.name, diminutif: $scope.newProduct.diminutif, id: id}).$promise.then(function(data) {
+ $scope.response = data;
+ getResultsPage($scope.pagination.current);
+ });
+ } else {
+ Product.create({name: $scope.newProduct.name, diminutif: $scope.newProduct.diminutif}).$promise.then(function(data) {
+ $scope.response = data;
+ getResultsPage($scope.pagination.current);
+ });
+ }
+ }
+
+ function deleteProduct(id) {
+ if(id != null && id != undefined && id > 0) {
+ Product.delete({id: id}).$promise.then(function(data) {
+ $scope.response = data;
+ getResultsPage($scope.pagination.current);
+ });
+ }
+ }
+
+ }])
+ .controller('ProductDetailCtrl', ['$scope', '$routeParams', '$http', 'Product', 'Version', 'Channel',
+ function ($scope, $routeParams, $http, Product, Version, Channel) {
+ $scope.customersPerPage = 10;
+ $scope.versionsPerPage = 10;
+ $scope.orderProp = 'value';
+ $scope.itemsPerPage = $scope.versionsPerPage;
+ $scope.channels = [];
+ $scope.possibleChannels = [];
+ $scope.patches = [];
+ $scope.versionsFrom = [];
+ $scope.showEditForm = 0;
+ $scope.previousPage = {
+ message: "Liste des produits",
+ url: "/#/products"
+ };
+
+ getResultsPage(1);
+
+ $scope.pagination = {
+ current: 1
+ };
+
+ $scope.newVersion = {
+ 'nature':'patch',
+ 'value':''
+ };
+
+ $scope.pageChanged = function() {
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.versionCreated = function() {
+ createOrUpdateVersionForProduct();
+ $scope.showAddForm = false;
+ };
+
+ $scope.versionUpdated = function(id) {
+ createOrUpdateVersionForProduct(id);
+ $scope.showEditForm = 0;
+ };
+
+ $scope.versionEditLaunched = function(id) {
+ $scope.showEditForm = id;
+ $scope.showAddForm = false;
+ getPossibleChannels(id);
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.versionCreateLaunched = function() {
+ $scope.showEditForm = 0;
+ $scope.showAddForm = true;
+ getResultsPage($scope.pagination.current);
+ };
+
+ $scope.actionCanceled = function() {
+ $scope.showEditForm = 0;
+ $scope.showAddForm = false;
+ };
+
+ function getResultsPage(pageNumber) {
+ //noinspection JSUnresolvedVariable
+ var offset = (pageNumber-1)*$scope.versionsPerPage,
+ product_id = $routeParams.product_id;
+ if(product_id != null && product_id != undefined && product_id > 0) {
+ Product.info({id: product_id}).$promise.then(function(data) {
+ $scope.product = data.result;
+ });
+ Version.all({limit: $scope.versionsPerPage, offset: offset, product_id: product_id}).$promise.then(function(data) {
+ $scope.versions = data;
+ for(var item in data.result) {
+ //noinspection JSUnfilteredForInLoop
+ getChannelsForVersion(data.result[item].id);
+ //noinspection JSUnfilteredForInLoop
+ getPatchesForVersion(data.result[item].id);
+ }
+ });
+
+ Version.nb({product_id:product_id}).$promise.then(function(data) {
+ $scope.totalVersions = data;
+ $scope.totalItems = $scope.totalVersions.result;
+ });
+ }
+ }
+
+ function createOrUpdateVersionForProduct(id) {
+ var product_id = $routeParams.product_id;
+ if(id != null && id != undefined && id > 0) {
+ for(var possibleChannelIndex in $scope.possibleChannels.result) {
+ //noinspection JSUnfilteredForInLoop
+ var possibleChannel = $scope.possibleChannels.result[possibleChannelIndex],
+ key_found = false;
+ for(var channelIndex in $scope.channels[id].result) {
+ //noinspection JSUnfilteredForInLoop
+ var channel = $scope.channels[id].result[channelIndex];
+ if(possibleChannel.channel_key == channel.channel_key) {
+ key_found = true;
+ if(!possibleChannel.checked) {
+ Version.disable_channel({id: channel.id}).$promise.then(function(data) {
+ $scope.response = data;
+ });
+ }
+ }
+ }
+ if(possibleChannel.checked && !key_found) {
+ Version.enable_channel({version_id: id, channel_id: possibleChannel.id, status: 'maj_ok'}).$promise.then(function(data) {
+ $scope.response = data;
+ });
+ }
+ }
+ Version.update({product_id: product_id, nature: $scope.newVersion.nature, value: $scope.newVersion.value, id: id}).$promise.then(function(data) {
+ $scope.response = data;
+ getResultsPage($scope.pagination.current);
+ });
+ } else {
+ Version.create({product_id: product_id, nature: $scope.newVersion.nature, value: $scope.newVersion.value}).$promise.then(function(data) {
+ $scope.response = data;
+ getResultsPage($scope.pagination.current);
+ });
+ }
+ }
+
+ function getChannelsForVersion(id) {
+ Version.get_channels({version_id: id}).$promise.then(function(data) {
+ $scope.channels[id] = data;
+ });
+ }
+
+ function getPatchesForVersion(id) {
+ Version.get_patches({version_id_to: id}).$promise.then(function(data) {
+ $scope.patches[id] = data;
+ getVersionsFrom(id);
+ });
+ }
+
+ function getVersionsFrom(id) {
+ for(var item in $scope.patches[id].result) {
+ //noinspection JSUnfilteredForInLoop
+ var patch = $scope.patches[id].result[item];
+ Version.info({id: patch.version_id_from}).$promise.then(function(data) {
+ //noinspection JSReferencingMutableVariableFromClosure
+ $scope.versionsFrom[patch.id] = data;
+ });
+ }
+ }
+
+ function getPossibleChannels(id) {
+ $scope.possibleChannels = [];
+ Channel.all().$promise.then(function(data) {
+ $scope.possibleChannels = data;
+ for(var possibleChannelIndex in $scope.possibleChannels.result) {
+ //noinspection JSUnfilteredForInLoop
+ var possibleChannel = $scope.possibleChannels.result[possibleChannelIndex];
+ for(var channelIndex in $scope.channels[id].result) {
+ //noinspection JSUnfilteredForInLoop
+ var channel = $scope.channels[id].result[channelIndex];
+ if(possibleChannel.channel_key == channel.channel_key) {
+ possibleChannel.checked = true;
+ }
+ }
+ }
+ });
+ }
+ }]
+ );
\ No newline at end of file