+}
+
+function get_messages($user, $lstart, $lend) {
+
+ return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
+ `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
+ count( * ) as count
+ FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
+ WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
+ intval($user), intval($lstart), intval($lend)
+ );
+}
+
+function render_messages($msg, $t) {
+
+ $a = get_app();
+
+ $tpl = get_markup_template($t);
+ $rslt = '';
+
+ foreach($msg as $rr) {
+
+ if($rr['unknown']) {
+ $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
+ }
+ elseif (link_compare($rr['from-url'], $myprofile)){
+ $participants = sprintf( t("You and %s"), $rr['name']);
+ }
+ else {
+ $participants = sprintf( t("%s and You"), $rr['from-name']);
+ }
+
+ if($a->theme['template_engine'] === 'internal') {
+ $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
+ $body_e = template_escape($rr['body']);
+ $to_name_e = template_escape($rr['name']);
+ }
+ else {
+ $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
+ $body_e = $rr['body'];
+ $to_name_e = $rr['name'];
+ }
+
+ $rslt .= replace_macros($tpl, array(
+ '$id' => $rr['id'],
+ '$from_name' => $participants,
+ '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
+ '$sparkle' => ' sparkle',
+ '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
+ '$subject' => $subject_e,
+ '$delete' => t('Delete conversation'),
+ '$body' => $body_e,
+ '$to_name' => $to_name_e,
+ '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
+ '$ago' => relative_date($rr['mailcreated']),
+ '$seen' => $rr['mailseen'],
+ '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
+ ));
+ }