Wolle-Rosen-Kaufen/www/js/controllers.js

206 lines
5.6 KiB
JavaScript

angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope,$ionicPopup,$http, $database, $timeout,$rootScope, $cordovaFileTransfer,$cordovaFile) {
$scope.preis="";
$scope.name="";
$scope.freitext="";
$scope.items=[];
$scope.item=[];
$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;
});
};
$scope.loaddata = function() {
$scope.items=[];
$database.getAllBuys().then(function (result) {
if(result.length>0){
alert("daten da");
for(i=0;i<result.length;i++){
$scope.items.push(result[i]);
alert(i);
}
} else {
$scope.items=[];
}
});
};
$scope.savedata = function() {
$scope.item={name: $scope.name, bild: "test"};
$scope.items.push($scope.item);
$database.setBuys($scope.name,"test");
};
function successBuyHandler (result) {
$timeout(consumesuccessHandler, 1000);
}
function successConsumeHandler (result) {
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
});
}, function (error) {
console.log('Error');
}, function (progress) {
// PROGRESS HANDLING GOES HERE
});
return false;
});
}
function errorBuyHandler (error) {
/*
var strResult = "";
if(typeof error === 'object') {
strResult = JSON.stringify(error);
} else {
strResult = error;
}
alert("Feher: \r\n"+strResult );
*/
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) {
$scope.items=[];
//$scope.items=$localstorage.getObject('items');
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
if ($location.path() == "/tab/buys") {
$database.getAllBuys().then(function (result) {
if(result.length>0){
for(i=0;i<result.length;i++){
$scope.items.push(result[i]);
}
} else {
$scope.items=[];
}
});
}
});
})
.controller('Dummy', function($scope) {
});