+/**
+ * @brief Subpage where the admin can delete an item from their node given the GUID
+ *
+ * This subpage of the admin panel offers the nodes admin to delete an item from
+ * the node, given the GUID or the display URL such as http://example.com/display/123456.
+ * The item will then be marked as deleted in the database and processed accordingly.
+ *
+ * @param App $a
+ * @return string
+ */
+function admin_page_deleteitem(App $a) {
+ $t = get_markup_template("admin_deleteitem.tpl");
+
+ return replace_macros($t, array(
+ '$title' => t('Administration'),
+ '$page' => t('Delete Item'),
+ '$submit' => t('Delete this Item'),
+ '$intro1' => t('On this page you can delete an item from your node. If the item is a top level posting, the entire thread will be deleted.'),
+ '$intro2' => t('You need to know the GUID of the item. You can find it e.g. by looking at the display URL. The last part of http://example.com/display/123456 is the GUID, here 123456.'),
+ '$deleteitemguid' => array('deleteitemguid', t("GUID"), '', t("The GUID of the item you want to delete."), 'required', 'autofocus'),
+ '$baseurl' => System::baseUrl(),
+ '$form_security_token' => get_form_security_token("admin_deleteitem")
+ ));
+}
+/**
+ * @brief Process send data from Admin Delete Item Page
+ *
+ * The GUID passed through the form should be only the GUID. But we also parse
+ * URLs like the full /display URL to make the process more easy for the admin.
+ *
+ * @param App $a
+ */
+function admin_page_deleteitem_post(App $a) {
+ if (!x($_POST['page_deleteitem_submit'])) {
+ return;
+ }
+
+ check_form_security_token_redirectOnErr('/admin/deleteitem/', 'admin_deleteitem');
+
+ if (x($_POST['page_deleteitem_submit'])) {
+ $guid = trim(notags($_POST['deleteitemguid']));
+ // The GUID should not include a "/", so if there is one, we got an URL
+ // and the last part of it is most likely the GUID.
+ if (strpos($guid, '/')) {
+ $guid = substr($guid, strrpos($guid, '/')+1);
+ }
+ // Now that we have the GUID get all IDs of the associated entries in the
+ // item table of the DB and drop those items, which will also delete the
+ // associated threads.
+ $r = dba::select('item', array('id'), array('guid'=>$guid));
+ while ($row = dba::fetch($r)) {
+ drop_item($row['id'], false);
+ }
+ dba::close($r);
+ }
+
+ info(t('Item marked for deletion.').EOL);
+ goaway('admin/deleteitem');
+ return; // NOTREACHED
+}
+