diary.module 12.5 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?

Dries's avatar
 
Dries committed
3
$module = array("find" => "diary_find",
Dries's avatar
 
Dries committed
4
                "help" => "diary_help",
Dries's avatar
 
Dries committed
5
                "menu" => "diary_menu",
Dries's avatar
 
Dries committed
6 7
                "page"  => "diary_page",
                "user" => "diary_user",
Dries's avatar
 
Dries committed
8
                "admin" => "diary_admin",
Dries's avatar
 
Dries committed
9
                "block" => "diary_block",
Dries's avatar
 
Dries committed
10
                "export" => "diary_export");
Dries's avatar
 
Dries committed
11

12
include_once "includes/common.inc";
Dries's avatar
 
Dries committed
13

Dries's avatar
 
Dries committed
14
function diary_find($keys) {
Dries's avatar
 
Dries committed
15
  global $user;
Dries's avatar
 
Dries committed
16 17 18
  $find = array();
  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id WHERE d.text LIKE '%". check_input($keys) ."%' ORDER BY d.timestamp DESC LIMIT 20");
  while ($diary = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
19
    array_push($find, array("subject" => "$diary->userid's diary", "link" => (user_permission($user) ? "admin.php?mod=diary&op=edit&id=$diary->id" : "module.php?mod=diary&op=view&name=$diary->userid"), "user" => $diary->userid, "date" => $diary->timestamp));
Dries's avatar
 
Dries committed
20 21 22 23 24
  }
  return $find;

}

Dries's avatar
 
Dries committed
25 26
function diary_search() {
  global $keys, $mod;
Dries's avatar
 
Dries committed
27 28
  print search_form($keys);
  print search_data($keys, $mod);
Dries's avatar
 
Dries committed
29 30
}

Dries's avatar
 
Dries committed
31 32 33 34 35 36 37
function diary_page_overview($num = 20) {
  global $theme, $user;

  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id ORDER BY d.timestamp DESC LIMIT $num");

  while ($diary = db_fetch_object($result)) {
    if ($time != date("F jS", $diary->timestamp)) {
Dries's avatar
 
Dries committed
38
      $output .= "<B>". $date = t(date("l", $timestamp)) .", ". t(date("F", $timestamp)) ." ". date("j", $diary->timestamp) ."</B>\n";
Dries's avatar
 
Dries committed
39 40 41
      $time = date("F jS", $diary->timestamp);
    }
    $output .= "<DL>\n";
Dries's avatar
 
Dries committed
42
    $output .= " <DD><P><B>$diary->userid ". t("wrote") .":</B></P></DD>\n";
Dries's avatar
 
Dries committed
43
    $output .= " <DL>\n";
Dries's avatar
 
Dries committed
44
    $output .= "  <DD><P>". check_output($diary->text, 1) ."</P><P>[ <A HREF=\"module.php?mod=diary&op=view&name=$diary->userid\">". t("more") ."</A> ]</P></DD>\n";
Dries's avatar
 
Dries committed
45 46 47 48 49
    $output .= " </DL>\n";
    $output .= "</DL>\n";
  }

  $theme->header();
Dries's avatar
 
Dries committed
50
  $theme->box(t("Online diary"), $output);
Dries's avatar
 
Dries committed
51 52 53 54 55 56 57
  $theme->footer();

}

function diary_page_entry($timestamp, $text, $id = 0) {
  if ($id) {
    $output .= "<DL>\n";
Dries's avatar
 
Dries committed
58
    $output .= " <DT><B>". format_date($timestamp, "large") .":</B></DT>\n";
Dries's avatar
 
Dries committed
59
    $output .= " <DD><P>[ <A HREF=\"module.php?mod=diary&op=edit&id=$id\">". t("edit") ."</A> ]</P><P>". check_output($text, 1) ."</P></DD>\n";
Dries's avatar
 
Dries committed
60 61 62 63
    $output .= "</DL>\n";
  }
  else {
    $output .= "<DL>\n";
Dries's avatar
 
Dries committed
64
    $output .= " <DT><B>". format_date($timestamp, "large") .":</B></DT>\n";
Dries's avatar
 
Dries committed
65 66 67 68 69 70 71 72 73
    $output .= " <DD><P>". check_output($text, 1) ."</P></DD>\n";
    $output .= "</DL>\n";
  }
  return $output;
}

function diary_page_display($username) {
  global $theme, $user;

Dries's avatar
 
Dries committed
74
  $username = empty($username) ? $user->userid : $username;
Dries's avatar
 
Dries committed
75

Dries's avatar
 
Dries committed
76 77 78 79 80 81 82
  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id WHERE u.userid = '$username' ORDER BY timestamp DESC");

  if ($username == $user->userid) {
    $output .= diary_page_entry(time(), "<BIG><A HREF=\"module.php?mod=diary&op=add\">Add new diary entry!</A></BIG><P>");
    while ($diary = db_fetch_object($result)) $output .= diary_page_entry($diary->timestamp, $diary->text, $diary->id);
  }
  else {
Dries's avatar
 
Dries committed
83
    $output .= "<B>". t("Username") .":</B> ". format_username($username);
Dries's avatar
 
Dries committed
84 85 86 87
    while ($diary = db_fetch_object($result)) $output .= diary_page_entry($diary->timestamp, $diary->text);
  }

  $theme->header();
Dries's avatar
 
Dries committed
88
  $theme->box(t("Online diary"), $output);
Dries's avatar
 
Dries committed
89 90 91 92 93
  $theme->footer();
}

function diary_page_add() {
  global $theme, $user, $allowed_html;
Dries's avatar
 
Dries committed
94

Dries's avatar
 
Dries committed
95 96
  $output .= "<FORM ACTION=\"module.php?mod=diary\" METHOD=\"post\">\n";

Dries's avatar
 
Dries committed
97
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
98 99
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\"></TEXTAREA><BR>\n";
  $output .= " <SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
100 101 102 103 104 105 106
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\">\n";
  $output .= "</P>\n";

  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
107

Dries's avatar
 
Dries committed
108
  $theme->header();
Dries's avatar
 
Dries committed
109
  $theme->box(t("Edit your diary"), $output);
Dries's avatar
 
Dries committed
110 111 112 113 114 115 116 117 118 119 120 121 122
  $theme->footer();
}

function diary_page_edit($id) {
  global $theme, $user, $allowed_html;

  $result = db_query("SELECT * FROM diaries WHERE id = $id");
  $diary = db_fetch_object($result);

  $output .= diary_page_entry($diary->timestamp, $diary->text);

  $output .= "<FORM ACTION=\"module.php?mod=diary\" METHOD=\"post\">\n";
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
123
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\">". check_textarea($diary->text) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
124
  $output .= " <SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
125 126 127 128 129 130 131
  $output .= "</P>\n";
  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$diary->id\">\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"timestamp\" VALUE=\"$diary->timestamp\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\"> <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Submit diary entry\">\n";
  $output .= "</P>\n";
  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
132

Dries's avatar
 
Dries committed
133
  $theme->header();
Dries's avatar
 
Dries committed
134
  $theme->box(t("Edit your diary"), $output);
Dries's avatar
 
Dries committed
135 136 137 138 139 140 141 142 143 144
  $theme->footer();
}

function diary_page_preview($text, $timestamp, $id = 0) {
  global $theme, $user, $allowed_html;

  $output .= diary_page_entry($timestamp, $text);

  $output .= "<FORM ACTION=\"module.php?mod=diary\" METHOD=\"post\">\n";
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
145
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\">". check_textarea($text) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
146
  $output .= " <SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
147 148 149 150 151 152 153 154 155 156
  $output .= "</P>\n";
  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Submit diary entry\">\n";
  $output .= "</P>\n";

  $output .= "</FORM>\n";

  $theme->header();
Dries's avatar
 
Dries committed
157
  $theme->box(t("Edit your diary"), $output);
Dries's avatar
 
Dries committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  $theme->footer();
}

function diary_page_submit($text, $id = 0) {
  global $user, $theme;

  if ($id) {
    watchdog("message", "old diary entry updated");
    db_query("UPDATE diaries SET text =  '". check_input($text) ."' WHERE id = $id");
  }
  else {
    watchdog("diary", "new diary entry added");
    db_query("INSERT INTO diaries (author, text, timestamp) VALUES ('$user->id', '". check_input($text) ."', '". time() ."')");
  }

  header("Location: module.php?mod=diary&op=view&name=$user->userid");
}

function diary_page() {
  global $op, $id, $name, $text, $timestamp;

  // Security check:
  if (strstr($id, " ") || strstr($name, " ")) {
    watchdog("error", "diary: attempt to provide malicious input through URI");
    exit();
  }

  switch($op) {
    case "add":
      diary_page_add();
      break;
   case "edit":
      diary_page_edit($id);
      break;
    case "view":
      diary_page_display($name);
      break;
    case "Preview diary entry":
      if ($id) diary_page_preview($text, $timestamp, $id);
      else diary_page_preview($text, time());
      break;
    case "Submit diary entry":
      if ($id) diary_page_submit($text, $id);
      else diary_page_submit($text);
      break;
    default:
      diary_page_overview();
  }
}

Dries's avatar
 
Dries committed
208 209
function diary_help() {
 ?>
Dries's avatar
 
Dries committed
210
  <P>Drupal's diary module allows registered users to maintain an online diary.  It provides easy-to-write and easy-to-read online diaries or journals that can be filled with daily thoughts, poetry, boneless blabber, spiritual theories, intimate details, valuable experiences, cynical rants, semi-coherent comments, writing experiments, artistic babblings, critics on current facts, fresh insights, diverse dreams, chronicles and mumbling madness available for public consumption.</P>
Dries's avatar
 
Dries committed
211 212 213
 <?
}

Dries's avatar
 
Dries committed
214 215 216 217
function diary_user($username, $section, $operation) {
  if ($section == "user" && $operation == "view") {
    $result = db_query("SELECT d.* FROM diaries d LEFT JOIN users u ON u.id = d.author WHERE u.userid = '$username' AND d.timestamp > ". (time() - 1209600) ." ORDER BY id DESC LIMIT 2");
    while ($diary = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
218
      $content .= "<DL><DT><B>". format_date($diary->timestamp, "large") .":</B></DT><DD><P>". check_output($diary->text) ."</P><P>[ <A HREF=\"module.php?mod=diary&op=view&name=$username\">more</A> ]</P></DD></DL>\n";
Dries's avatar
 
Dries committed
219 220 221
      $diaries++;
    }

Dries's avatar
 
Dries committed
222
    $block["subject"] = strtr(t("%a has posted %b recently"), array("%a" => $username, "%b" => format_plural($diaries, "diary entry", "diary entries")));
Dries's avatar
 
Dries committed
223 224 225 226 227 228
    $block["content"] = $content;

    return $block;
  }
}

Dries's avatar
 
Dries committed
229
function diary_menu() {
Dries's avatar
 
Dries committed
230
  return array("<A HREF=\"module.php?mod=diary&op=add\">". t("edit your diary") ."</A>", "<A HREF=\"module.php?mod=diary&op=view\">". t("view your diary") ."</A>");
Dries's avatar
 
Dries committed
231 232
}

Dries's avatar
 
Dries committed
233
function diary_block() {
Dries's avatar
 
Dries committed
234
  $result = db_query("SELECT u.userid, d.timestamp FROM diaries d LEFT JOIN users u ON d.author = u.id ORDER BY timestamp DESC LIMIT 10");
Dries's avatar
 
Dries committed
235 236 237

  while ($diary = db_fetch_object($result)) {
    if ($time != date("F jS", $diary->timestamp)) {
Dries's avatar
 
Dries committed
238
      $content .= "<P><B>". t(date("l", $diary->timestamp)) ."</B> (". date("m/d/Y", $diary->timestamp) .")</P>\n";
Dries's avatar
 
Dries committed
239 240 241 242 243 244 245
      $time = date("F jS", $diary->timestamp);
    }
    $content .= "<LI><A HREF=\"module.php?mod=diary&op=view&name=$diary->userid\">$diary->userid</A></LI>\n";
  }

  $block[0]["subject"] = "Recent diary entries";
  $block[0]["content"] = $content;
Dries's avatar
 
Dries committed
246
  $block[0]["info"] = "Recent diary entries";
Dries's avatar
 
Dries committed
247 248 249 250 251
  $block[0]["link"] = "module.php?mod=diary";

  return $block;
}

Dries's avatar
 
Dries committed
252
function diary_admin_edit($id) {
Dries's avatar
 
Dries committed
253 254 255 256 257 258 259 260
  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id WHERE d.id = $id");

  $diary = db_fetch_object($result);

  $output .= "<FORM ACTION=\"admin.php?mod=diary&op=save&id=$id\" METHOD=\"post\">\n";

  $output .= "<P>\n";
  $output .= " <B>Author:</B><BR>\n";
Dries's avatar
 
Dries committed
261
  $output .= " ". format_username($diary->userid) ."\n";
Dries's avatar
 
Dries committed
262 263 264 265
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= "<B>Diary entry:</B><BR>\n";
Dries's avatar
 
Dries committed
266
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"text\">". check_textarea($diary->text) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
267 268 269 270 271 272
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Save diary entry\">\n";
  $output .= "</P>\n";
  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
273

Dries's avatar
 
Dries committed
274 275 276
  print $output;
}

Dries's avatar
 
Dries committed
277
function diary_admin_save($id, $text) {
Dries's avatar
 
Dries committed
278
  db_query("UPDATE diaries SET text = '". check_input($text) ."' WHERE id = $id");
Dries's avatar
 
Dries committed
279
  watchdog("message", "diary: modified entry #$id.");
Dries's avatar
 
Dries committed
280 281
}

Dries's avatar
 
Dries committed
282
function diary_admin_display($order = "date") {
Dries's avatar
 
Dries committed
283 284 285 286 287
  // Initialize variables:
  $fields = array("author" => "author", "date" => "timestamp DESC");

  // Perform SQL query:
  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON u.id = d.author ORDER BY d.$fields[$order] LIMIT 50");
Dries's avatar
 
Dries committed
288

Dries's avatar
 
Dries committed
289
  // Display stories:
Dries's avatar
 
Dries committed
290
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
291
  $output .= " <TR>\n";
Dries's avatar
 
Dries committed
292
  $output .= "  <TH ALIGN=\"right\" COLSPAN=\"3\">\n";
Dries's avatar
 
Dries committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  $output .= "   <FORM ACTION=\"admin.php?mod=diary\" METHOD=\"post\">\n";
  $output .= "    <SELECT NAME=\"order\">\n";
  foreach ($fields as $key=>$value) {
    $output .= "     <OPTION VALUE=\"$key\"". ($key == $order ? " SELECTED" : "") .">Sort by $key</OPTION>\n";
  }
  $output .= "    </SELECT>\n";
  $output .= "    <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Update\">\n";
  $output .= "   </FORM>\n";
  $output .= "  </TH>\n";
  $output .= " </TR>\n";

  $output .= " <TR>\n";
  $output .= "  <TH>subject</TH>\n";
  $output .= "  <TH>author</TH>\n";
  $output .= "  <TH>operations</TH>\n";
  $output .= " </TR>\n";

  while ($diary = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
311
    $output .= " <TR><TD><A HREF=\"module.php?mod=diary&op=view&name=$diary->userid\">$diary->userid on ". format_date($diary->timestamp, "small") ."</A></TD><TD>". format_username($diary->userid) ."</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=diary&op=edit&id=$diary->id\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
312 313 314
  }

  $output .= "</TABLE>\n";
Dries's avatar
 
Dries committed
315

Dries's avatar
 
Dries committed
316 317 318 319 320 321
  print $output;
}


function diary_admin() {
  global $op, $id, $text, $order;
Dries's avatar
 
Dries committed
322

Dries's avatar
 
Dries committed
323 324
  print "<SMALL><A HREF=\"admin.php?mod=diary\">overview</A> | <A HREF=\"admin.php?mod=diary&op=search\">search diary</A> | <A HREF=\"admin.php?mod=diary&op=help\">help</A></SMALL><HR>\n";

Dries's avatar
 
Dries committed
325 326
  switch ($op) {
    case "edit":
Dries's avatar
 
Dries committed
327
      diary_admin_edit($id);
Dries's avatar
 
Dries committed
328
      break;
Dries's avatar
 
Dries committed
329 330 331 332 333 334
    case "help":
      diary_help();
      break;
    case "search":
      diary_search();
      break;
Dries's avatar
 
Dries committed
335
    case "Save diary entry":
Dries's avatar
 
Dries committed
336 337
      diary_admin_save($id, $text);
      diary_admin_edit($id);
Dries's avatar
 
Dries committed
338 339
      break;
    case "Update":
Dries's avatar
 
Dries committed
340
      diary_admin_display($order);
Dries's avatar
 
Dries committed
341 342
      break;
    default:
Dries's avatar
 
Dries committed
343
      diary_admin_display();
Dries's avatar
 
Dries committed
344 345 346
  }
}

Dries's avatar
 
Dries committed
347 348 349 350 351 352
function diary_export($uri) {
  if ($uri[2] == "diary") {
    print "TODO: export diary for user $uri[3]";
  }
}

Dries's avatar
 
Dries committed
353
?>