angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope,$ionicPopup,$http, $database, $timeout,$rootScope, $cordovaFileTransfer,$cordovaFile,$ionicLoading,$state) {
$scope.preis="";
$scope.name="";
$scope.freitext="";
$scope.items=[];
$scope.item=[];
$scope.show = function() {
$ionicLoading.show({
template: '
Kauf wird abgeschlossen.
'
});
};
$scope.hide = function(){
$ionicLoading.hide();
};
$scope.validate = function() {
var link = 'http://api.raataar.de/rosen/vorschau.php';
$http.post(link, {name : $scope.name, freitext: $scope.freitext, rosen: $scope.preis}).then(function (res){
$scope.response = res.data;
window.open($scope.response, '_system', 'location=yes');
return false;
});
};
function successBuyHandler (result) {
$timeout(consumesuccessHandler, 500);
}
function successConsumeHandler (result) {
$scope.show($ionicLoading);
var link = 'http://api.raataar.de/rosen/kauf.php';
$http.post(link, {name : $scope.name, freitext: $scope.freitext, rosen: $scope.preis}).then(function (res){
$scope.filetodownload = res.data;
// File for download
var url = "http://api.raataar.de/rosen/" + $scope.filetodownload;
// File name only
var filename = url.split("/").pop();
// Save location
//var targetPath = cordova.file.externalRootDirectory + filename;
var targetPath = cordova.file.applicationStorageDirectory + filename;
var targetpublicPath = cordova.file.externalRootDirectory + "/WolleRosenKaufen/" + filename;
$scope.targetPath='';
$scope.targetPath=targetPath;
$scope.targetpublicPath=targetpublicPath;
$scope.filename=filename;
$cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
$scope.item={name: $scope.name, bild: $scope.targetpublicPath};
$scope.items.push($scope.item);
//itemName,itemBild,itemBildtext,itemKaufdatum,itemappimgpath,itemappimgpublicpath
$database.setBuys($scope.name,$scope.filename,$scope.freitext,'','', $scope.targetpublicPath);
//Ordner erstellen auf SD-Karte
$cordovaFile.createDir(cordova.file.externalRootDirectory, "WolleRosenKaufen", false)
.then(function (success) {
// success
}, function (error) {
// error
});
$cordovaFile.copyFile(cordova.file.applicationStorageDirectory, $scope.filename, cordova.file.externalRootDirectory + "/WolleRosenKaufen", $scope.filename)
.then(function (success) {
// success
}, function (error) {
// error
});
$scope.hide($ionicLoading);
$state.go('tab.buys');
}, function (error) {
console.log('Error');
}, function (progress) {
// PROGRESS HANDLING GOES HERE
});
return false;
});
}
function errorBuyHandler (error) {
var strResult = "";
var returnBool=false;
if(typeof error === 'object') {
strResult = JSON.stringify(error);
} else {
strResult = error;
}
returnBool=strResult.indexOf("Item Already Owned") != -1;
if (returnBool==true){
//alert("Schon gekauft");
$timeout(consumesuccessHandler, 500);
}
else{
var alertPopup = $ionicPopup.alert({
title: 'Oh Nein',
template: 'Der Kauf konnte nicht durchgeführt werden.'
});
alertPopup.then(function(res) {
console.log('Der Kauf konnte nicht durchgeführt werden');
});
}
}
function errorConsumeHandler (error) {
var alertPopup = $ionicPopup.alert({
title: 'Oh Nein',
template: 'Der Kauf wurde durchgeführt, aber deine Rosen konnten freigeschaltet werden.'
});
alertPopup.then(function(res) {
console.log('Der Kauf wurde durchgeführt, aber deine Rosen konnten freigeschaltet werden');
});
}
function consumesuccessHandler (result) {
inappbilling.consumePurchase(successConsumeHandler, errorConsumeHandler, $scope.preis);
}
$scope.buyRose = function() {
if((window.device && device.platform == "Android") && typeof inappbilling !== "undefined") {
if ($rootScope.storeinit==1){
//Wegenbug ausgehebelt
inappbilling.buy(successBuyHandler, errorBuyHandler, $scope.preis);
//successConsumeHandler();
}
else{
var alertPopup = $ionicPopup.alert({
title: 'Achtung',
template: 'Der Google-Store konnte nicht initialisiert werden.'
});
alertPopup.then(function(res) {
console.log('Store steht nicht zur Verfuegung');
});
}
}
}
})
.controller('BuysCtrl', function($scope, $database,$location,$ionicModal,$ionicLoading) {
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
if ($location.path() == "/tab/buys") {
$scope.show($ionicLoading);
fill_list();
$scope.hide($ionicLoading);
}
});
function fill_list(){
$scope.items=[];
$database.getAllBuys().then(function (result) {
if(result.length>0){
for(i=0;iLade...'
});
};
$scope.hide = function(){
$ionicLoading.hide();
};
$scope.showImages = function(imagesrc) {
$scope.imagesrc=imagesrc;
$scope.showModal('templates/image-popover.html');
}
$scope.showModal = function(templateUrl) {
$ionicModal.fromTemplateUrl(templateUrl, {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
});
}
// Close the modal
$scope.closeModal = function() {
$scope.modal.hide();
$scope.modal.remove()
};
})
.controller('Dummy', function($scope) {
});