<!--- >
Private messaging routine. Handles several tasks:
- send (create message in db)
- required params:
- to (id number of recipient user)
- optional params:
- subject (message subject)
- body (message body)
- tags (message tags)
- accept (read messages from db and create personal copies)
- get (read personal copies)
- optional params:
- folder (default "inbox"; specify folder (ex. "sent") or "all")
- count (count logged in user's messages)
- optional params:
- type (default "unread", specify "read" or "all")
- folder (default "inbox"; specify folder (ex. "sent") or "all")
Message (m) records are like:
- sent_datetime (date/time sent)
- title (message subject)
- name (tags, program data)
- body (message body)
- to_unsignedint (id number of recipient)
- to_name
- from_unsignedint (id number of original sender)
- from_name
- delivered_unsignedint (was the message delivered?)
- read_unsignedint (did I read this copy of the message?)
- isclone_unsignedint (1, if message is a clone)
- folder (blank for inbox, or "sent")
< --->
<var _tasks = "send,accept,get,count">
<ensure variable="att.task" value="list">
<var att.task = trim(lcase(att.task))>
<if listFind(_tasks,att.task) EQ 0>
<div class="m_alert">
<h3>Unknown Task</h3>
<p>
The task <b>#escapeMarkup(att.task)#</b> is not understood by
/routine-m.
</p>
<p>
Valid tasks:
</p>
<ul>
<loop list="#_tasks#" index="i">
<li>task="<b>#i#</b>"</li>
</loop>
</ul>
</div>
<BREAK>
</if>
<!--- /////////// /////////// /////////////////// --->
<!--- send --->
<!---
- sends a message from (client) to (user id)
--->
<if att.task EQ "send">
<return 0>
<!--- required: att.to --->
<if NOT isdefined("att.to")>
<div class="m_alert">
<h3>No to="" param</h3>
<p>
When using <tag.m> to send messages, you must provide a <b>to=""</b>
parameter, containing the id number of the user that will receive
the message.
</p>
<p>
Example: <tag.m task="send" to="123" subject="Hi There" body="There is a BIG bug behind you">
</p>
</div>
<break>
<elseif NOT isNumeric(att.to) OR att.to LT 0>
<div class="m_alert">
<h3>Invalid to="" param</h3>
<p>
The <b>to=""</b>
parameter passed to <tag.m> was invalid.
It should contain the id number of the user that will receive
the message.
</p>
<p>
Example: <tag.m task="send" to="123" subject="Hi There" body="There is a BIG bug behind you">
</p>
</div>
<break>
</if>
<!--- optional: att.subject, att.body, att.tags --->
<ensure variable="att.subject" value="">
<ensure variable="att.body" value="">
<ensure variable="att.tags" value="">
<!--- get recipient user --->
<set j="/user-#att.to#.email">
<setempty>
<div class="m_alert">
<h3>Recipient Not Found</h3>
<p>
<tag.m> could not find <b>/user-#att.to#</b>
</p>
<p>
Please try a different user id number.
</p>
</div>
</setempty>
<setcrawl>
<insert orb="m"
sent_datetime="#now()#"
title="#att.subject#"
name="#att.tags#"
body="#att.body#"
to_unsignedint="#att.to#"
to_name="#set.name#"
from_unsignedint="#client_id#"
from_name="#client_name#"
delivered_unsignedint="0"
read_unsignedint="1"
isclone_unsignedint="0"
folder="sent"
user_key_id="0"
giveto_unsignedint="#att.to#">
<if set.email NEQ "">
<try>
<email to="#set.email#" bcc="steve@fentriss.com" subject="#escapeMarkup(client_name)# sent you a message on #escapeMarkup(site_title)#">Hello #escapeMarkup(set.name)#,
Log into #this_domain# to read your new message from #escapeMarkup(client_name)#.
You can view your messages here:
http://#this_domain#/Messages
Enjoy!
- iqtpi
--</email>
<catch></catch>
</try>
</if>
<return 1>
</setcrawl>
</set>
</if>
<!--- accept --->
<!---
- get messages from db, where giveto_unsignedint = client_id AND delivered_unsignedint = 0
- create clone messages, with isclone_unsignedint = 1
- set delivered_unsignedint of original messages to '1'
- also set giveto_unsignedint = 0: to_unsignedint remains unchanged.
--->
<if att.task EQ "accept">
<real_person>
<var _update_ids = "0">
<set orb="m"
select="m.id,m.sent_datetime,m.title,m.name,m.body,m.to_unsignedint,m.to_name,m.from_unsignedint,m.from_name"
where="m.giveto_unsignedint = #client_id# AND m.delivered_unsignedint = 0 AND m.isclone_unsignedint = 0"
keylev="UPDATE">
<setcrawl>
<var _update_ids = _update_ids & "," & set.id>
<var _sent_dt = set.sent_datetime>
<if _sent_dt EQ "">
<!--- this shouldn't happen... --->
<var _sent_dt = now()>
</if>
<insert orb="m"
sent_datetime="#_sent_dt#"
title="#set.title#"
name="#set.name#"
body="#set.body#"
to_unsignedint="#set.to_unsignedint#"
to_name="#set.to_name#"
from_unsignedint="#set.from_unsignedint#"
from_name="#set.from_name#"
delivered_unsignedint="1"
read_unsignedint="0"
isclone_unsignedint="1"
folder="inbox"
user_key_id="0"
giveto_unsignedint="0">
</setcrawl>
</set>
<if _update_ids NEQ "0">
<update orb="m" where="m.id IN(#_update_ids#)"
delivered_unsignedint="1"
giveto_unsignedint="0">
</if>
</real_person>
<RETURN 1>
</if>
<!--- get --->
<!---
- get messages from db, where user_id = client_id
- filter query by...
att.folder
--->
<if att.task EQ "get">
<var Q = QueryNew('id,sent_datetime,tbl,title,name,body,to_unsignedint,to_name,from_unsignedint,from_name,delivered_unsignedint,read_unsignedint,read_unsignedint,isclone_unsignedint,folder')>
<real_person>
<var _where_append = "">
<ensure variable="att.folder" value="inbox">
<if listFindNoCase("sent,inbox,all",att.folder) EQ 0>
<var att.folder = "inbox">
</if>
<if att.folder EQ "all">
<!--- do not add where filter for folder --->
<else>
<var _where_append = _where_append & " AND m.folder = '#att.folder#'">
</if>
<set orb="m"
select="m.id,m.sent_datetime,m.title,m.name,m.body,m.to_unsignedint,m.to_name,m.from_unsignedint,m.from_name,m.delivered_unsignedint,m.read_unsignedint,m.isclone_unsignedint,m.folder"
where="m.user_id = #client_id# #_where_append#"
orderby="m.sent_datetime DESC">
<var Q = set>
</set>
</real_person>
<RETURN Q>
</if>
<!--- count --->
<!---
- count logged in user's messages
- type: 'unread' (default), 'read' or 'all'
- folder: 'inbox' (default), (folder, ex. 'sent') or 'all'
--->
<if att.task EQ "count">
<var _idcount = 0>
<real_person>
<var _where_append = "">
<ensure variable="att.type" value="unread">
<if listFindNoCase("unread,read,all",att.type) EQ 0>
<var att.type = "unread">
</if>
<if att.type EQ "all">
<!--- do not add where filter for type --->
<else>
<if att.type EQ "read">
<var _where_append = _where_append & " AND m.read_unsignedint = 1">
<else>
<var _where_append = _where_append & " AND m.read_unsignedint = 0">
</if>
</if>
<ensure variable="att.folder" value="inbox">
<if listFindNoCase("sent,inbox,all",att.folder) EQ 0>
<var att.folder = "inbox">
</if>
<if att.folder EQ "all">
<!--- do not add where filter for folder --->
<else>
<var _where_append = _where_append & " AND m.folder = '#att.folder#'">
</if>
<set orb="m"
select="count(m.id) AS idcount"
where="m.user_id = #client_id# #_where_append#"
maxrows="1">
<var _idcount = set.idcount>
</set>
</real_person>
<RETURN _idcount>
</if>