root/trunk/scripts/categories.class.php

Revision 1775, 8.5 KB (checked in by robertb, 4 days ago)

#206

Line 
1<?php
2$categories = new Categories($l);
3
4class Categories {
5
6    var $categories = array();
7    var $l = array();
8
9    function Categories($l) {
10   
11        $this->l = $l;
12   
13        $this->get_categories();
14    }
15
16    function get($id, $data) {
17        return $this->categories[$id][$data];
18    }
19
20    function get_id($url) {
21        foreach($this->categories AS $cat) {
22            if($cat['url'] == $url) return $cat['id'];
23        }
24
25    }
26   
27    function get_categories() {
28        if(!defined("JLOG_UPDATE") AND !defined("JLOG_LOGIN")) {
29            $sql = "SELECT id, name, url, description FROM ".JLOG_DB_CATEGORIES;
30            $cat = new Query($sql);
31           if($cat->error()) {
32                echo "<pre>\n";
33                echo $cat->getError();
34                echo "</pre>\n";
35                die();
36           }
37           while($c = $cat->fetch()) {
38            $this->categories[$c['id']] =
39             array('id' => $c['id'], 'name' => $c['name'], 'url' => $c['url'], 'description' => $c['description'] );
40           }
41        }
42    }
43   
44    function get_assigned_categories($id) {
45        $sql = "SELECT cat_id FROM ".JLOG_DB_CATASSIGN." WHERE content_id = '".$id."'";
46        $assigned = new Query($sql);
47       if($assigned->error()) {
48            echo "<pre>\n";
49            echo $assigned->getError();
50            echo "</pre>\n";
51            die();
52       }
53       $ids = array();
54       while($a = $assigned->fetch()) {
55        $ids[] = $a['cat_id'];
56       }
57       return $ids;
58    }
59
60    function output_select($catassign) {
61    // $catassign is an array which contains all assigned ids
62   
63        if(count($this->categories) > 0) {
64            $output = "     <p><label for='categories'>".$this->l['admin']['categories']."</label><br />\n"
65                        ."      <select id='categories' name='categories[]' size='4' multiple='multiple'>\n"
66                        ."       <option value='no_categories'>".$this->l['admin']['no_categories']."</option>\n";
67   
68           foreach($this->categories AS $id => $data) {
69                if(is_array($catassign)) if(in_array($id, $catassign)) $selected = " selected='selected'";
70                else unset($selected);
71                $output .= "       <option".$selected." value='".$id."'>".$data['name']."</option>\n";
72            }
73   
74            $output .= "      </select>\n     </p>";
75   
76            return $output;
77        }
78    }
79
80    function output_rss($id) {
81        $ids = $this->get_assigned_categories($id);
82        if(is_array($ids)) {
83            foreach($ids AS $i) {
84                $output .= "        <category>".$this->get($i, 'name')."</category>\n";
85            }
86        }
87        return $output;
88    }
89
90    function output_assigned_links($ids) {
91        if(!is_array($ids)) $ids = $this->get_assigned_categories($ids);
92        if(is_array($ids)) {
93            foreach($ids as $id) {
94                $output .= $this->link($id)." ";
95            }
96        }
97        if(isset($output)) return " <span title='".$this->l['content_cat_linklist']."' class='catlinklist'>&raquo; ".$output."</span>";
98    }
99   
100    function output_whole_list($_before = " <ul id='categorieslist'>\n", $_after = " </ul>\n", $before = "  <li>", $after = "</li>\n") {
101        if(is_array($this->categories) AND count($this->categories)) {
102            $output = $_before;
103            foreach($this->categories AS $id => $tmp) {
104               $output .= $before.$this->link($id).$after;
105            }
106            $output .= $_after;
107            return $output;
108        }
109        else return false;
110    }
111   
112    function link($id) {
113        if(JLOG_CLEAN_URL) return "<a title='".$this->l['content_cat_link']."' href='".JLOG_PATH."/cat/".$this->categories[$id]['url']."/'>".$this->categories[$id]['name']."</a>";
114        else return "<a title='".$this->l['content_cat_link']."' href='".JLOG_PATH."/archive.php?cat=".$this->categories[$id]['url']."'>".$this->categories[$id]['name']."</a>";
115    }
116   
117    function output_whole_list_admin() {
118        $output = "
119        <table>
120         <tr>
121          <th>".$this->l['admin']['change']."</th>
122          <th>".$this->l['admin']['delete']."</th>
123          <th>".$this->l['admin']['cat_name']."</th>
124         </tr>";
125   
126         foreach($this->categories AS $id => $tmp) {
127            $output .= "
128         <tr>
129          <td><a href='".add_session_id_to_url("?action=change&amp;id=".$id)."'><img src='".JLOG_PATH."/img/JLOG_edit.png' alt='".$this->l['admin']['change']."' /></a></td>
130          <td><a href='".add_session_id_to_url("?action=trash&amp;id=".$id)."'><img src='".JLOG_PATH."/img/JLOG_trash.png' alt='".$this->l['admin']['delete']."' /></a></td>
131          <td>".$this->link($id)."</td>
132         </tr>\n";
133         }
134
135        $output .= "        </table>\n";
136   
137        return $output;
138    }
139   
140    function output_form($form_input = "", $action = 'new', $legend) {
141        $output = "
142        <form id='entryform' action='?action=".$action."' method='POST'>
143         <fieldset><legend>".$legend."</legend>
144         <p><label for='name'>".$this->l['admin']['cat_name']."</label><br />
145          <input id='name' name='name' class='long' maxlength='255' size='60' type='text' value='".$form_input['name']."' /></p>
146         <p><label for='url'>".$this->l['admin']['cat_url']."</label><br />
147          <input id='url' name='url' class='long' maxlength='100' size='60' type='text' value='".$form_input['url']."' />
148          <input name='id' type='hidden' value='".$form_input['id']."' /></p>
149         <p><label for='description'>".$this->l['admin']['cat_description']."</label><br />
150          <textarea id='description' name='description' class='short'>".$form_input['description']."</textarea></p>
151         <p><input type='submit' name='form_submit' value='".$this->l['admin']['submit']."' />
152          <a href='".add_session_id_to_url("categories.php")."'>".$this->l['admin']['cancel']."</a>
153          ".add_session_id_input_tag()."</p>
154        </fieldset>
155        </form>";
156
157        return $output;
158    }
159
160    function new_cat($form_input) {
161   
162        $form_input = escape_for_mysql($form_input);
163   
164        $sql = "INSERT INTO ".JLOG_DB_CATEGORIES." (name, url, description) VALUES
165                    ('".$form_input['name']."',
166                     '".$form_input['url']."',
167                     '".$form_input['description']."');";
168
169        $new = new Query($sql);
170   
171        if($new->error()) {
172         echo "<pre>\n";
173         echo $new->getError();
174         echo "</pre>\n";
175         die();
176        }
177    }
178   
179    function change_cat($form_input) {
180
181        $form_input = escape_for_mysql($form_input);
182   
183        $sql = "UPDATE ".JLOG_DB_CATEGORIES."
184                  SET
185                    name            = '".$form_input['name']."',
186                    url         = '".$form_input['url']."',
187                    description = '".$form_input['description']."'
188                  WHERE
189                   id = '".$form_input['id']."' LIMIT 1;";
190
191        $change = new Query($sql);
192   
193        if($change->error()) {
194         echo "<pre>\n";
195         echo $change->getError();
196         echo "</pre>\n";
197         die();
198        }
199    }
200   
201    function trash_cat($id) {
202   
203        $sql = "DELETE FROM ".JLOG_DB_CATEGORIES." WHERE id = '".escape_for_mysql($id)."' LIMIT 1";
204        $trash = new Query($sql);
205       if($trash->error()) {
206        echo "<pre>\n";
207        echo $trash->getError();
208        echo "</pre>\n";
209        die();
210       }
211
212        $sql = "DELETE FROM ".JLOG_DB_CATASSIGN." WHERE cat_id = '".escape_for_mysql($id)."' LIMIT 1";
213        $trash = new Query($sql);
214       if($trash->error()) {
215        echo "<pre>\n";
216        echo $trash->getError();
217        echo "</pre>\n";
218        die();
219       }
220
221   
222    }
223   
224    function validate($form_input) {
225        if(empty($form_input['name'])) $errors[] = $this->l['admin']['cat_noname'];
226
227        if(empty($form_input['url'])) $errors[] = $this->l['admin']['no_url'];
228        elseif(!preg_match("/^[a-z0-9\-_\.,]+$/", $form_input['url'])) $errors[] = $this->l['admin']['false_url_letters'];
229        else {
230            $sql = "SELECT id FROM ".JLOG_DB_CATEGORIES." WHERE url = '".escape_for_mysql($form_input['url'])."';";
231   
232            $check_url = new Query($sql);
233
234            if($check_url->error()) {
235             echo "<pre>\n";
236             echo $check_url->getError();
237             echo "</pre>\n";
238             die();
239            }
240
241            if($check_url->numRows() > 0) {
242                $c = $check_url->fetch();
243                if($c['id'] != $form_input['id']) $errors[] = $this->l['admin']['cat_duplicate'];
244            }
245        }
246   
247        return $errors;
248    }
249}
250?>
Note: See TracBrowser for help on using the browser.