root/trunk/plugins/acronyms.php

Revision 463, 4.9 KB (checked in by michiel, 19 months ago)

all data manipulating queries now get run with ->exec() instead of ->query()
Fix some more errors in php dev mode

Re #119 and #125

  • Property svn:keywords set to Id
Line 
1<?php
2Class acronyms extends MvBlog_plugin implements MvBlog_pluginiface {
3    /* variables */
4    public $name    = "Acronyms";
5    public $author  = "Michiel van Baak";
6    public $license = "GPL";
7    public $website = "http://www.mvblog.org";
8    public $description = "Acronym replacement module. Searches for know Acronyms and put <acronym> tag around them.";
9
10    private $_mvblog;
11    private $_acronyms;
12
13    /* methods */
14    /* __construct {{{ */
15    public function __construct(&$mvblog) {
16        $this->addHook("text_output", "acroreplace");
17        $this->_mvblog =& $mvblog;
18    }
19    /* }}} */
20    /* activate {{{ */
21    public function activate() {
22        /* get all acronyms from database */
23        $res =& $this->_mvblog->db->query("SELECT id,acronym,description FROM acronyms ORDER BY UPPER(acronym)");
24        if (PEAR::isError($res)) {
25            die($res->getMessage());
26        }
27        while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
28            $this->_acronyms[$row["id"]] = array($row["acronym"], $row["description"]);
29        }
30    }
31    /* }}} */
32    /* deactivate {{{ */
33    public function deactivate() {
34        unset($this->_acronyms);
35    }
36    /* }}} */
37    /* acroreplace {{{ */
38    public function acroreplace($text) {
39        foreach ($this->_acronyms as $acro) {
40            $acronym     = $acro[0];
41            $description = $acro[1];
42            //replace all the supplied acronyms
43            $text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b$acronym\b(?!:)(?![^<>]*?>)|imsU","<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>" , $text);
44            //allow user to overwrite acronym replacing by putting the term between $ signs
45            $text = preg_replace("|[$]<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>[$]|imsU" , "$acronym" , $text);
46        }
47        return trim($text);
48    }
49    /* }}} */
50    /* show_settings {{{ */
51    public function show_settings() {
52
53        ?><a href="index.php?action=edit_plugin_setting&amp;plugin=acronyms&id=0"><?php echo gettext("create new"); ?></a><?php
54        foreach ($this->_acronyms as $id => $acro) {
55            ?>
56            <div class="log_post">
57                <div class="log_post_head">
58                    <h1 class="log_post_h1"><a href="?action=edit_plugin_setting&plugin=acronyms&amp;id=<?php echo $id; ?>"><?php echo stripslashes($acro[0]); ?></a></h1>
59                </div>
60                <div class="log_post_body">
61                    <div class="log_post_normal">
62                        <?php echo stripslashes($acro[1]); ?>
63                    </div>
64                </div>
65                <div class="log_post_foot">
66                </div>
67            </div>
68            <?php
69        }
70    }
71    /* }}} */
72    /* edit_setting {{{ */
73    public function edit_setting($requestdata) {
74        $id = $requestdata["id"];
75        if ($id == 0) {
76            $acro["id"]          = 0;
77            $acro["acronym"]     = "acronym name";
78            $acro["description"] = "acronym description";
79        } else {
80            $acro = array(
81                "id"          => $id,
82                "acronym"     => $this->_acronyms[$id][0],
83                "description" => $this->_acronyms[$id][1]
84            );
85        }
86        ?>
87        <form name="acronym" method="post" action="index.php">
88        <input type="hidden" name="action" value="save_plugin_setting" />
89        <input type="hidden" name="plugin" value="acronyms" />
90        <input type="hidden" name="acro[action]" id="act" value="" />
91        <input type="hidden" name="acro[id]" value="<?php echo $acro["id"]; ?>" />
92        <div class="log_post">
93            <div class="log_post_head">
94                <h1 class="log_post_h1"><input type="text" name="acro[acronym]" value="<?php echo stripslashes($acro["acronym"]); ?>" /></h1>
95            </div>
96            <div class="log_post_body">
97                <input type="text" name="acro[description]" value="<?php echo stripslashes($acro["description"]); ?>" style="width: 500px;" /><br />
98                <input type="submit" value="<?php echo gettext("save"); ?>" />
99                <?php if ($id) { ?>
100                    <input type="button" value="<?php echo gettext("delete"); ?>" onClick="document.getElementById('act').value='delete';document.forms.acronym.submit();" />
101                <?php } ?>
102            </div>
103            <div class="log_postfoot">
104            </div>
105        </div>
106        </form>
107        <?php
108    }
109    /* }}} */
110    /* save_setting {{{ */
111    public function save_setting($requestdata) {
112        $acro = $requestdata["acro"];
113        if ($acro["action"] == "delete") {
114            /* remove acro */
115            unset($this->_acronyms[$acro["id"]]);
116            $sql = sprintf("DELETE FROM acronyms WHERE id=%d", $acro["id"]);
117            $this->_mvblog->db->exec($sql);
118        } else {
119            if ($acro["id"]) {
120                $this->_acronyms[$acro["id"]] = array($acro["acronym"], $acro["description"]);
121                /* store it in db */
122                $sql = sprintf("UPDATE acronyms SET acronym='%s', description='%s' WHERE id=%d", $acro["acronym"], $acro["description"], $acro["id"]);
123                $this->_mvblog->db->exec($sql);
124            } else {
125                $sql = sprintf("INSERT INTO acronyms (acronym, description) values ('%s', '%s')", $acro["acronym"], $acro["description"]);
126                $res = $this->_mvblog->db->exec($sql);
127                /* fetch the same record so we can get the id */
128                $sql = sprintf("SELECT id FROM acronyms WHERE acronym='%s' AND description='%s'", $acro["acronym"], $acro["description"]);
129                $res = $this->_mvblog->db->query($sql);
130                $row = $res->fetchRow();
131                $this->_acronyms[$row[0]] = array($acro["acronym"], $acronym["description"]);
132            }
133        }
134        $this->show_settings();
135    }
136    /* }}} */
137}
138?>
Note: See TracBrowser for help on using the browser.