summaryrefslogtreecommitdiff
path: root/modules/meta.module
blob: 092bf1d4c7f2326272e7548898045c37b066ff57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

function meta_help() {
  ?>
    To be written.
  <?php
}

function meta_conf() {
  return array("add and edit meta tags");
}

function meta_form($type, $edit = array()) {
  $c = db_query("SELECT * FROM collection WHERE types LIKE '%". check_input($type) ."%'");
  while ($collection = db_fetch_object($c)) {
    unset($array);
    $t = db_query("SELECT * FROM tag WHERE collections LIKE '%$collection->name%'");
    while ($tag = db_fetch_object($t)) {
      $array[$tag->attributes] = $tag->name;
    }
    $form .= form_select($collection->name, $collection->name, $edit[$collection->name], $array);
  }
  return $form;
}

function meta_save($type, $edit = array()) {
  $result = db_query("SELECT * FROM collection WHERE types LIKE '%". check_input($type) ."%'");
  while ($collection = db_fetch_object($result)) {
    $array[] = $edit[$collection->name];
  }
  return implode(", ", $array);
}

function meta_get_collection($cid) {
  return db_fetch_array(db_query("SELECT * FROM collection WHERE cid = '". check_input($cid) ."'"));
}

function meta_get_tag($tid) {
  return db_fetch_array(db_query("SELECT * FROM tag WHERE tid = '". check_input($tid) ."'"));
}

function meta_form_collection($edit = array()) {
  global $REQUEST_URI;

  $form .= form_textfield("Collection name", "name", $edit[name], 50, 64, "Required.  The name for this group or collection of meta-tags.  Example: 'Software'.");
  $form .= form_textfield("Types", "types", $edit[types], 50, 64, "Required.  A comma-seperated list of node types you want to associate this collection with.  Example: 'story, book'.");
  $form .= form_submit("Submit");

  if ($edit[cid]) {
    $form .= form_submit(t("Delete"));
    $form .= form_hidden("cid", $edit[cid]);
  }

  return form($REQUEST_URI, $form);
}

function meta_form_tag($edit = array()) {
  global $REQUEST_URI;

  $form .= form_textfield("Meta-tag name", "name", $edit[name], 50, 64, "Required.  The name for this meta-tag.  Example: 'Apache'.");
  $form .= form_textfield("Attributes", "attributes", $edit[attributes], 50, 64, "Required.  A comma-seperated list of keywords you want to associate this meta-tag with.  Example: 'Computers, Software, Webservers'.");
  $form .= form_textfield("Collections", "collections", $edit[collections], 50, 64, "Required.  A comma-seperated list of collections you want to associate this meta-tag with.  Example: 'Section'.");
  $form .= form_submit("Submit");

  if ($edit[tid]) {
    $form .= form_submit(t("Delete"));
    $form .= form_hidden("tid", $edit[tid]);
  }

  return form($REQUEST_URI, $form);
}

function meta_save_collection($edit) {
 if ($edit[cid] && $edit[name]) {
    db_query("UPDATE collection SET name = '". check_input($edit[name]) ."', types = '". check_input($edit[types]) ."' WHERE cid = '$edit[cid]'");
  }
  else if ($edit[cid]) {
    db_query("DELETE FROM collection WHERE cid = '". check_input($edit[cid]) ."'");
  }
  else {
    db_query("INSERT INTO collection (name, types) VALUES ('". check_input($edit[name]) ."', '". check_input($edit[types]) ."')");
  }
}

function meta_save_tag($edit) {
 if ($edit[tid] && $edit[name]) {
    db_query("UPDATE tag SET name = '". check_input($edit[name]) ."', attributes = '". check_input($edit[attributes]) ."', collections = '". check_input($edit[collections]) ."' WHERE tid = '$edit[tid]'");
  }
  else if ($edit[tid]) {
    db_query("DELETE FROM tag WHERE tid = '". check_input($edit[tid]) ."'");
  }
  else {
    db_query("INSERT INTO tag (name, attributes, collections) VALUES ('". check_input($edit[name]) ."', '". check_input($edit[attributes]) ."', '". check_input($edit[collections]) ."')");
  }
}

function meta_preview() {
  foreach (module_list() as $name) {
    if (module_hook($name, "status") && $name != "node") {
      $output .= "<H3>". ucfirst($name) ." type</H3>";
      $output .= meta_form($name) ."<HR>";
    }
  }
  return form("", $output);
}

function meta_overview() {
  $result = db_query("SELECT * FROM collection ORDER BY name");

  $output .= "<H3>Collection overview</H3>";
  $output .= "<TABLE BORDER=\"1\" CELLSPADDING=\"2\" CELLSPACING=\"2\">\n";
  $output .= " <TR><TH>name</TH><TH>node types</TH><TH>operations</TH></TR>\n";
  while ($collection = db_fetch_object($result)) {
    $output .= " <TR><TD>". check_output($collection->name) ."</TD><TD>". check_output($collection->types) ."</TD><TD><A HREF=\"admin.php?mod=meta&type=collection&op=edit&id=$collection->cid\">edit collection</A></TD></TR>\n";
  }
  $output .= "</TABLE>\n";

  $result = db_query("SELECT * FROM tag ORDER BY name");

  $output .= "<H3>Meta-tag overview</H3>";
  $output .= "<TABLE BORDER=\"1\" CELLSPADDING=\"2\" CELLSPACING=\"2\">\n";
  $output .= " <TR><TH>name</TH><TH>collections</TH><TH>meta attributes</TH><TH>operations</TH></TR>\n";
  while ($tag = db_fetch_object($result)) {
    $output .= " <TR><TD>". check_output($tag->name) ."</TD><TD>". check_output($tag->collections) ."</TD><TD>". check_output($tag->attributes) ."</TD><TD><A HREF=\"admin.php?mod=meta&type=tag&op=edit&id=$tag->tid\">edit tag</A></TD></TR>\n";
  }
  $output .= "</TABLE>\n";

  return $output;
}

function meta_admin() {
  global $user, $edit, $type, $op, $id;

  if (user_access($user, "add and edit meta tags")) {

    print "<SMALL><A HREF=\"admin.php?mod=meta&type=collection&op=add\">add new collection</A> | <A HREF=\"admin.php?mod=meta&type=tag&op=add\">add new meta-tag</A> | <A HREF=\"admin.php?mod=meta&op=preview\">preview node forms</A> | <A HREF=\"admin.php?mod=meta\">overview</A> | <A HREF=\"admin.php?mod=meta&op=help\">help</A></SMALL><HR>\n";

    switch ($op) {
      case "add":
        if ($type == "collection")
          print meta_form_collection();
        else
          print meta_form_tag();
        break;
      case "edit":
        if ($type == "collection")
          print meta_form_collection(meta_get_collection($id));
        else
          print meta_form_tag(meta_get_tag($id));
        break;
      case "help":
        print meta_help();
        break;
      case "preview":
        print meta_preview();
        break;
      case "Delete":
        $edit[name] = 0;
        // fall through:
      case "Submit":
        if ($type == "collection")
          print status(meta_save_collection($edit));
        else
          print status(meta_save_tag($edit));
        // fall through:
      default:
        print meta_overview();
    }
  }
  else {
    print message_access();
  }
}

?>