diff --git a/apps/views/default/layout.ls b/apps/views/default/layout.ls index a545f83..144f4c5 100644 --- a/apps/views/default/layout.ls +++ b/apps/views/default/layout.ls @@ -1,5 +1,4 @@ diff --git a/blog/assets/main.js b/blog/assets/main.js new file mode 100644 index 0000000..f499551 --- /dev/null +++ b/blog/assets/main.js @@ -0,0 +1,126 @@ +var scheme = undefined; +String.prototype.__ = function() +{ + return this +} +function subscribe(prefix) +{ + if(scheme) return; + // get scheme + $.get(prefix+"/rst/subscribe.html") + .done(function(d) { + scheme = $.parseHTML(d) + var obs = riot.observable() + $(scheme).css("visibility","hidden") + $("#desktop" ).append(scheme) + obs.on("exit", function(){ + $(scheme).remove() + scheme = undefined + }) + obs.on("rendered", function(d){ + $(".afx-window-title", scheme).html("Subscribe") + $("[data-id='send']", scheme).click(function(){ + var status = $("[data-id='status']", scheme) + status.html(""); + var els = $("[data-class='data']", scheme) + var data = {} + + for(var i = 0; i < els.length; i++) + data[els[i].name] = $(els[i]).val() + if(data.email == "" || data.subject == "" || data.content == "" || data.name == "") + return status.html("Please enter all the fields"); + var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if(!re.test(String(data.email).toLowerCase())) + return status.html("Email is not correct"); + + $.ajax({ + type: 'POST', + url: prefix + "/service/subscribe", + contentType: 'application/json', + data: JSON.stringify(data), + dataType: 'json', + success: null + }).done(function(r){ + if(r.error) + { + console.log(r.error) + alert(r.error) + } + else + { + obs.trigger("exit") + alert("You have been subscribed. Thanks") + } + }).fail(function(e, s){ + console.log(e) + alert("Error: " + e) + }) + }) + $(scheme).css("visibility","visible") + }) + riot.mount(scheme, {observable:obs}) + }) + .fail(function() { + alert( "Cannot get the form" ); + }) +} +function mailtoMe(prefix) +{ + if(scheme) return; + // get scheme + $.get(prefix+"/rst/sendto.html") + .done(function(d) { + scheme = $.parseHTML(d) + var obs = riot.observable() + $(scheme).css("visibility","hidden") + $("#desktop" ).append(scheme) + obs.on("exit", function(){ + $(scheme).remove() + scheme = undefined + }) + obs.on("rendered", function(d){ + $(".afx-window-title", scheme).html("Send me an email") + $("[data-id='send']", scheme).click(function(){ + var status = $("[data-id='status']", scheme) + status.html(""); + var els = $("[data-class='data']", scheme) + var data = {} + + for(var i = 0; i < els.length; i++) + data[els[i].name] = $(els[i]).val() + if(data.email == "" || data.subject == "" || data.content == "" || data.name == "") + return status.html("Please enter all the fields"); + var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + if(!re.test(String(data.email).toLowerCase())) + return status.html("Email is not correct"); + + $.ajax({ + type: 'POST', + url: prefix + "/service/sendmail", + contentType: 'application/json', + data: JSON.stringify(data), + dataType: 'json', + success: null + }).done(function(r){ + if(r.error) + { + console.log(r.error) + alert(r.error) + } + else + { + obs.trigger("exit") + alert("Email sent. Thank") + } + }).fail(function(){ + alert("Service unavailable at the moment") + }) + }) + $(scheme).css("visibility","visible") + }) + riot.mount(scheme, {observable:obs}) + }) + .fail(function() { + alert( "Cannot get the form" ); + }) +} \ No newline at end of file diff --git a/blog/controllers/PostController.lua b/blog/controllers/PostController.lua index c3f6e74..6559642 100644 --- a/blog/controllers/PostController.lua +++ b/blog/controllers/PostController.lua @@ -161,7 +161,7 @@ function PostController:analyse(n) end self.template:set(message, "Analyse complete") else - self.template:set(message, "Cannotto analyse") + self.template:set(message, "Cannot analyse") end return true end diff --git a/blog/controllers/ServiceController.lua b/blog/controllers/ServiceController.lua new file mode 100644 index 0000000..aa97ea4 --- /dev/null +++ b/blog/controllers/ServiceController.lua @@ -0,0 +1,57 @@ +BaseController:subclass( + "ServiceController", + { + registry = {}, + models = {"subscribers"} + } +) + +function fail(msg) + std.json() + std.t(JSON.encode({error = msg})) +end + +function result(obj) + std.json() + std.t(JSON.encode({result = obj, error = false})) +end + +function ServiceController:sendmail() + if not REQUEST.query.json then + fail("unknown request") + end + local rq = (JSON.decodeString(REQUEST.query.json)) + local to = "xsang.le@gmail.com" + local from = "From: " .. rq.email .. "\n" + local suject = "Subject: " .. rq.subject .. "\n" + local content = "Contact request from:" .. rq.name .. "\n Email: " .. rq.email .. "\n" .. rq.content .. "\n" + + local cmd = 'echo "' .. utils.escape(from .. suject .. content) .. '"| sendmail ' .. to + + --print(cmd) + local r = os.execute(cmd) + + if r then + result(r) + else + fail("Cannot send email at the moment, the service may be down") + end + return false +end + +function ServiceController:subscribe() + if not REQUEST.query.json then + fail("unknown request") + end + local rq = (JSON.decodeString(REQUEST.query.json)) + -- check if email is exist + local data = self.subscribers:find({exp = {["="] = {email = rq.email}}}) + if data and #data > 0 then + fail("You are already/previously subscribed") + else + -- save to database + self.subscribers:create(rq) + result("Ok") + end + return false +end diff --git a/blog/models/SubscribersModel.lua b/blog/models/SubscribersModel.lua new file mode 100644 index 0000000..83c68e2 --- /dev/null +++ b/blog/models/SubscribersModel.lua @@ -0,0 +1,8 @@ +BaseModel:subclass("SubscribersModel",{ + registry = {}, + name = "subscribers", + fields = { + name = "TEXT", + email = "TEXT" + } +}) \ No newline at end of file diff --git a/blog/views/default/layout.ls b/blog/views/default/layout.ls index b010741..2fb8a31 100644 --- a/blog/views/default/layout.ls +++ b/blog/views/default/layout.ls @@ -23,7 +23,7 @@ - + @@ -98,11 +98,11 @@ diff --git a/blog/views/default/post/detail.ls b/blog/views/default/post/detail.ls index 680e51c..f0b65b6 100644 --- a/blog/views/default/post/detail.ls +++ b/blog/views/default/post/detail.ls @@ -22,9 +22,10 @@ end end echo(table.concat(atags, ", ")) + local old_url = HTTP_ROOT.."/r:id:"..data.id ?> -
+
diff --git a/blog/views/default/post/posts.ls b/blog/views/default/post/posts.ls index 8227c02..781c7f7 100644 --- a/blog/views/default/post/posts.ls +++ b/blog/views/default/post/posts.ls @@ -29,9 +29,10 @@ end echo(table.concat(atags, ", ")) local url = HTTP_ROOT.."/post/id/"..data.id + local old_url = HTTP_ROOT.."/r:id:"..data.id ?> -
+