Advertisement
Guest User

Untitled

a guest
May 29th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.87 KB | None | 0 0
  1. var app = angular.module("AltranWebApp", ["ngRoute", "ngCookies"])
  2. .service('Global', function () {
  3. return {};
  4. });
  5.  
  6. app.config(function ($routeProvider) {
  7. $routeProvider
  8. .when("/", { templateUrl: 'partials/login.html', controller: 'loginController' })
  9. .when("/allProjects", { templateUrl: 'partials/allProjects.html', controller: 'allProjectsController' })
  10. .when("/inTimeProjects", { templateUrl: 'partials/inTimeProjects.html', controller: 'inTimeProjectsController' })
  11. .when("/form/:ID", { templateUrl: 'partials/form.html', controller: 'formController' })
  12. .when("/mail/:ID", { templateUrl: 'partials/mail.html', controller: 'mailController' })
  13. .when("/detailsProject/:ID", { templateUrl: 'partials/detailsProject.html', controller: 'detailsProjectController' })
  14. });
  15.  
  16. app.run(function ($rootScope, $cookies, $location) {
  17. $rootScope.logout = function () {
  18. $cookies.remove("userid");
  19. $cookies.remove("username");
  20. $cookies.remove("email");
  21.  
  22. $('.drawer').drawer('close');
  23.  
  24. $location.path("");
  25. };
  26. });
  27.  
  28.  
  29. app.controller('rootController', function ($scope, Global) {
  30. $scope.reloadPage = function () { alert("LEL"); }
  31. $scope.variavelteste = "G";
  32.  
  33. $scope.Global = Global;
  34. $scope.Global.buttonstyle = "";
  35.  
  36. });
  37.  
  38. app.controller('loginController', function ($scope, $rootScope, $location, $routeParams, $timeout, $cookies, Global) {
  39. $scope.Global = Global;
  40. $scope.Global.buttonstyle = "width: 0px; height: 0px; margin-top: -9999px;";
  41.  
  42. $('.drawer').drawer('close');
  43.  
  44. $scope.login = function () {
  45. var username = document.getElementById("login_username").value;
  46. var password = document.getElementById("login_password").value;
  47.  
  48. var apiurl = "http://altran.sytes.net/login";
  49. var passwordhash = CryptoJS.MD5(password).toString();
  50.  
  51. $.ajax({
  52. url:apiurl,
  53. type:"POST",
  54. data: JSON.stringify({name : username, pass : passwordhash}),
  55. contentType:"application/json; charset=utf-8",
  56. success: function(code, textStatus){
  57. if(code == 200)
  58. {
  59. var apiurl2 = 'http://altran.sytes.net/user/"' + username + '"' ;
  60.  
  61. $.get(apiurl2).then(function (response) {
  62. var data = response[0];
  63.  
  64. if (data != undefined || data != null) {
  65.  
  66. var now = new Date();
  67. var exp = new Date(now.getFullYear() + 1, now.getMonth(), now.getDate());
  68.  
  69. $cookies.put("userid", data.Id, { expires: exp });
  70. $cookies.put("username", username, { expires: exp });
  71. $cookies.put("email", data.Email, { expires: exp });
  72.  
  73.  
  74. $scope.Global.username = username;
  75. $scope.Global.email = data.Email;
  76.  
  77. if($scope.Global.imgLink != "")
  78. {
  79. $scope.Global.imgLink = "background-image: url('"+ data.Image_Link +"');";
  80. $cookies.put("urlLink", "background-image: url('"+ data.Image_Link +"');" , { expires: exp });
  81. }
  82. else
  83. {
  84. $scope.Global.imgLink = "";
  85. $cookies.put("urlLink", "" , { expires: exp });
  86. }
  87.  
  88. $location.path("allProjects");
  89.  
  90. $timeout(function () {
  91. $scope.currentPath = $location.path();
  92. }, 0);
  93. }
  94. else {
  95. alert("Falha ao efetuar Login.");
  96. }
  97.  
  98. });
  99. }
  100. },
  101. error: function(jqXHR, textStatus, errorThrown) {
  102. if(jqXHR.status == 500)
  103. {
  104. alert("Erro ao efetuar Login.");
  105. }
  106. else if(jqXHR.status == 666)
  107. {
  108. alert("Erro da base de dados.");
  109. }
  110. }
  111. });
  112. }
  113.  
  114. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  115. var uname = $cookies.get("username");
  116. var email = $cookies.get("email");
  117. var uLink = $cookies.get("urlLink");
  118.  
  119. $scope.Global.username = uname.toUpperCase();
  120. $scope.Global.email = email;
  121. $scope.Global.imgLink = uLink;
  122. $scope.Global.fLetter = uname.charAt(0);
  123.  
  124. $location.path("allProjects");
  125. }
  126.  
  127. });
  128.  
  129. app.controller('allProjectsController', function ($scope, $routeParams, $cookies, $location, Global) {
  130. $scope.Global = Global;
  131. $scope.Global.buttonstyle = "";
  132.  
  133. $('.drawer').drawer('close');
  134.  
  135. var apiurl = "http://altran.sytes.net/projects/" + $cookies.get("userid");
  136.  
  137. $.get(apiurl).then(function (response) {
  138.  
  139. for(var i=0; i < response.length; i++)
  140. {
  141. response[i].day = response[i].date.substring(0, 2);
  142. }
  143.  
  144. $scope.$apply(function () {
  145. $scope.projectos = response;
  146. });
  147.  
  148. });
  149.  
  150. $scope.openProject = function (id) {
  151. var goTo = "detailsProject/" + id + "/";
  152. $location.path(goTo);
  153. }
  154.  
  155. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  156. var uname = $cookies.get("username");
  157. var email = $cookies.get("email");
  158. var uLink = $cookies.get("urlLink");
  159.  
  160. $scope.Global.username = uname.toUpperCase();
  161. $scope.Global.email = email;
  162. $scope.Global.imgLink = uLink;
  163. $scope.Global.fLetter = uname.charAt(0);
  164.  
  165. $location.path("allProjects");
  166. }
  167. });
  168.  
  169. app.controller('inTimeProjectsController', function ($scope, $routeParams, $cookies, $location, Global) {
  170. $scope.Global = Global;
  171. $scope.Global.buttonstyle = "";
  172.  
  173. $('.drawer').drawer('close');
  174.  
  175. var apiurl = "http://altran.sytes.net/unProjects/" + $cookies.get("userid"); //TODO
  176.  
  177. $.get(apiurl).then(function (response) {
  178.  
  179. for(var i=0; i < response.length; i++)
  180. {
  181. response[i].day = response[i].date.substring(0, 2);
  182. }
  183.  
  184. $scope.$apply(function () {
  185. $scope.projectos = response;
  186. });
  187.  
  188. });
  189.  
  190. $scope.openProject = function (id) {
  191. var goTo = "detailsProject/" + id + "/";
  192. $location.path(goTo);
  193. }
  194.  
  195. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  196. var uname = $cookies.get("username");
  197. var email = $cookies.get("email");
  198. var uLink = $cookies.get("urlLink");
  199.  
  200. $scope.Global.username = uname.toUpperCase();
  201. $scope.Global.email = email;
  202. $scope.Global.imgLink = uLink;
  203. $scope.Global.fLetter = uname.charAt(0);
  204. }
  205. });
  206.  
  207. app.controller('mailController', function ($scope, $http, $routeParams, $cookies, Global) {
  208. $scope.Global = Global;
  209. $scope.Global.buttonstyle = "";
  210.  
  211. $('.drawer').drawer('close');
  212.  
  213.  
  214. $scope.result = 'hidden';
  215. $scope.resultMessage;
  216. $scope.formData;
  217. $scope.submitButtonDisabled = false;
  218. $scope.submitted = false;
  219. $scope.send_email = function(contactform) {
  220. $scope.submitted = true;
  221. $scope.submitButtonDisabled = true;
  222. if (contactform.$valid) {
  223. $http({
  224. method : 'POST',
  225. url : '/php/send_email.php',
  226. data : $.param($scope.formData), //param method from jQuery
  227. headers : { 'Content-Type': 'application/x-www-form-urlencoded' } //set the headers so angular passing info as form data (not request payload)
  228. }).success(function(data){
  229. alert('oi');
  230. console.log(data);
  231. if (data.success) { //success comes from the return json object
  232. $scope.submitButtonDisabled = true;
  233. $scope.resultMessage = data.message;
  234. $scope.result='bg-success';
  235. } else {
  236. $scope.submitButtonDisabled = false;
  237. $scope.resultMessage = data.message;
  238. $scope.result='bg-danger';
  239. }
  240. });
  241. } else {
  242. alert('oi');
  243. $scope.submitButtonDisabled = false;
  244. $scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley"> Please fill out all the fields.';
  245. $scope.result='bg-danger';
  246. }
  247. }
  248.  
  249. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  250. var uname = $cookies.get("username");
  251. var email = $cookies.get("email");
  252. var uLink = $cookies.get("urlLink");
  253.  
  254. $scope.Global.username = uname.toUpperCase();
  255. $scope.Global.email = email;
  256. $scope.Global.imgLink = uLink;
  257. $scope.Global.fLetter = uname.charAt(0);
  258. }
  259. });
  260.  
  261. app.controller('formController', function ($scope, $http, $routeParams, $cookies, Global) {
  262. $scope.done = 0;
  263.  
  264.  
  265. $('.drawer').drawer('close');
  266.  
  267. var apiurl = "http://altran.sytes.net/project/" + $routeParams.ID;
  268. $.get(apiurl).then(function (response) {
  269. var data = response[0];
  270. console.log(data);
  271.  
  272. $scope.$apply(function () {
  273. $scope.project = data;
  274. });
  275. });
  276.  
  277. var apiurl2 = "http://altran.sytes.net/questions/" + $routeParams.ID;
  278. $.get(apiurl2).then(function (response) {
  279. var data = response;
  280.  
  281. var json = [];
  282. for (var i = 0; i < data.length; i++) {
  283. var temp = {};
  284.  
  285. if(i>0 && i<data.length-1) //Serve para deixar os números das perguntas mais bonitas no CSS
  286. {
  287. if(i<10)
  288. {
  289. temp["number"] = "0" + i;
  290. }
  291. else
  292. {
  293. temp["number"] = i;
  294. }
  295. temp["description"] = data[i].Description.split(".")[1];
  296. }
  297. else
  298. {
  299. temp["number"] = "#";
  300. temp["description"] = data[i].Description;
  301. }
  302.  
  303. temp["question_id"] = data[i].Question_Id;
  304. temp["value"] = 0;
  305. temp["comments"] = "";
  306. json.push(temp);
  307. }
  308.  
  309. $scope.$apply(function () {
  310. $scope.done = 1;
  311. $scope.questions = json;
  312. });
  313. });
  314.  
  315. function allQuestionsAnswered() {
  316. for (var i = 0; i < $scope.questions.length; i++) {
  317. if ($scope.questions[i].value == 0) {
  318. return false;
  319. }
  320. if($scope.questions[i].value > 2 && $scope.questions[i].comments != '') {
  321. alert($scope.questions[i].value);
  322. return false;
  323. }
  324. }
  325. return true;
  326. }
  327.  
  328. $scope.submitForm = function (id) {
  329. if (allQuestionsAnswered()) {
  330. delete $scope.validationFailed;
  331. // code to go to the next step here
  332. } else {
  333.  
  334. $scope.validationFailed = true;
  335. return false;
  336. }
  337.  
  338.  
  339. console.log("posting data....");
  340. var formData = $scope.questions;
  341. var json = {
  342. assessement_id: id,
  343. answers: []
  344. };
  345.  
  346. for (var i = 0; i < formData.length; i++) {
  347. var temp = {};
  348. temp["id_pergunta"] = formData[i].question_id;
  349. temp["value"] = formData[i].value;
  350. temp["description"] = formData[i].comments;
  351. json.answers.push(temp);
  352. }
  353. console.log(json);
  354. var apiurl3 = "http://altran.sytes.net/answer/";
  355. $http({
  356. url: apiurl3,
  357. method: 'POST',
  358. data: json,
  359. dataType: "json"
  360. }).success(function(data, status, headers, config) {
  361. alert(data);
  362. // this callback will be called asynchronously
  363. // when the response is available
  364. }).error(function(data, status, headers, config) {
  365. // called asynchronously if an error occurs
  366. // or server returns response with an error status.
  367. });
  368. }
  369.  
  370. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  371. var uname = $cookies.get("username");
  372. var email = $cookies.get("email");
  373. var uLink = $cookies.get("urlLink");
  374.  
  375. $scope.Global.username = uname.toUpperCase();
  376. $scope.Global.email = email;
  377. $scope.Global.imgLink = uLink;
  378. $scope.Global.fLetter = uname.charAt(0);
  379. }
  380. });
  381.  
  382.  
  383. app.controller('detailsProjectController', function ($scope, $http, $location, $routeParams, $cookies, Global) {
  384.  
  385. $scope.done = 0;
  386. $('.drawer').drawer('close');
  387.  
  388. var apiurl = "http://altran.sytes.net/project/" + $routeParams.ID;
  389.  
  390. $.get(apiurl).then(function (response) {
  391. var data = response[0];
  392.  
  393. $scope.$apply(function () {
  394. $scope.project = data;
  395. $scope.done = 1;
  396. });
  397. });
  398.  
  399. $scope.formReply = function (id) {
  400. var goTo = "form/" + id + "/";
  401. $location.path(goTo);
  402. }
  403.  
  404. $scope.sendMail = function (id) {
  405. var goTo = "mail/" + id + "/";
  406. $location.path(goTo);
  407. }
  408.  
  409. if ($cookies.get("userid") != undefined || $cookies.get("userid") != null) {
  410. var uname = $cookies.get("username");
  411. var email = $cookies.get("email");
  412. var uLink = $cookies.get("urlLink");
  413.  
  414. $scope.Global.username = uname.toUpperCase();
  415. $scope.Global.email = email;
  416. $scope.Global.imgLink = uLink;
  417. $scope.Global.fLetter = uname.charAt(0);
  418. }
  419.  
  420. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement