Jlog PHP Cross Reference Blogging Systems

Source: /scripts/categories.class.php - 249 lines - 8613 bytes - Summary - Text - Print

   1  <?php
   2  $categories = new Categories($l);
   3  
   4  class 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") === false AND defined("JLOG_LOGIN") === false) {
  29              $sql = "SELECT * 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'> ".$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='?action=change&amp;id=".$id."'><img src='".JLOG_PATH."/img/JLOG_edit.png' alt='".$this->l['admin']['change']."' /></a></td>
 130            <td><a href='?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='categories.php'>".$this->l['admin']['cancel']."</a></p>
 153          </fieldset>
 154          </form>";
 155  
 156          return $output;
 157      }
 158  
 159      function new_cat($form_input) {
 160      
 161          $form_input = escape_for_mysql($form_input);
 162      
 163          $sql = "INSERT INTO ".JLOG_DB_CATEGORIES." (name, url, description) VALUES
 164                      ('".$form_input['name']."',
 165                       '".$form_input['url']."',
 166                       '".$form_input['description']."');";
 167  
 168          $new = new Query($sql);
 169      
 170          if($new->error()) {
 171           echo "<pre>\n";
 172           echo $new->getError();
 173           echo "</pre>\n";
 174           die();
 175          }
 176      }
 177      
 178      function change_cat($form_input) {
 179  
 180          $form_input = escape_for_mysql($form_input);
 181      
 182          $sql = "UPDATE ".JLOG_DB_CATEGORIES."
 183                    SET
 184                      name            = '".$form_input['name']."',
 185                      url         = '".$form_input['url']."',
 186                      description = '".$form_input['description']."'
 187                    WHERE
 188                     id = '".$form_input['id']."' LIMIT 1;";
 189  
 190          $change = new Query($sql);
 191      
 192          if($change->error()) {
 193           echo "<pre>\n";
 194           echo $change->getError();
 195           echo "</pre>\n";
 196           die();
 197          }
 198      }
 199      
 200      function trash_cat($id) {
 201      
 202          $sql = "DELETE FROM ".JLOG_DB_CATEGORIES." WHERE id = '".escape_for_mysql($id)."' LIMIT 1";
 203          $trash = new Query($sql);
 204         if($trash->error()) {
 205          echo "<pre>\n";
 206          echo $trash->getError();
 207          echo "</pre>\n";
 208          die();
 209         }
 210  
 211          $sql = "DELETE FROM ".JLOG_DB_CATASSIGN." WHERE cat_id = '".escape_for_mysql($id)."' LIMIT 1";
 212          $trash = new Query($sql);
 213         if($trash->error()) {
 214          echo "<pre>\n";
 215          echo $trash->getError();
 216          echo "</pre>\n";
 217          die();
 218         }
 219  
 220      
 221      }
 222      
 223      function validate($form_input) {
 224          if(empty($form_input['name'])) $errors[] = $this->l['admin']['cat_noname'];
 225  
 226          if(empty($form_input['url'])) $errors[] = $this->l['admin']['no_url'];
 227          elseif(!preg_match("/^[a-z0-9\-_\.\/,]+$/", $form_input['url'])) $errors[] = $this->l['admin']['false_url_letters'];
 228          else {
 229              $sql = "SELECT id FROM ".JLOG_DB_CATEGORIES." WHERE url = '".escape_for_mysql($form_input['url'])."';";
 230      
 231              $check_url = new Query($sql);
 232  
 233              if($check_url->error()) {
 234               echo "<pre>\n";
 235               echo $check_url->getError();
 236               echo "</pre>\n";
 237               die();
 238              }
 239  
 240              if($check_url->numRows() > 0) {
 241                  $c = $check_url->fetch();
 242                  if($c['id'] != $form_input['id']) $errors[] = $this->l['admin']['cat_duplicate'];
 243              }
 244          }
 245      
 246          return $errors;
 247      }
 248  }
 249  ?>

title

Description

title

Description

title

Description

title

title

Body