<?php
 
class NP_FancierURL extends NucleusPlugin{
  var $title = null;
 
  function getName()  { return 'FancierURL'; }
  function getAuthor() { return 'Dave St.Germain, modified by ketsugi'; }
  function getURL()   { return 'http://pobblelabs.org/'; }
  function getVersion() { return '0.4'; }
  function getDescription() {
    return 'Improves FancyURLs by allowing more descriptive URLs.';
  }
  function supportsFeature($what) {
    switch($what) {
      case 'SqlTablePrefix': return 1;
      default: return 0;
    }
  }
 
  function getEventList() {
    return array('PreAddItem','PostAddItem', 'PrePluginOptionsEdit', 'PostPluginOptionsUpdate');
  }
 
  function event_PreAddItem(&$data) {
      $this->title = $data['title'];
  }
 
  function event_PostAddItem(&$data) {
    $url = $this->getItemOption($data['itemid'],'urltitle');
    if ($url == '') $url = $this->_makefancyTitle($this->title);    
    $q = 'UPDATE ' . sql_table('item') . ' SET iurltitle=\'' . $url . '\' WHERE inumber=' . $data['itemid'];
    sql_query($q);
  }
 
  function event_PrePluginOptionsEdit(&$data) {
    if ($data['plugid'] == $this->GetID()) {
      foreach($data['options'] as $option) {
        if ($option['name'] == 'spaceReplace') $this->spaceReplace = $option['value'];
      }
    }
  }
 
  function event_PostPluginOptionsUpdate(&$data) {
    if ($data['context'] == 'item') {
      $url = $this->getItemOption($data['itemid'],'urltitle');
      if ($url == '') $url = $this->_makefancyTitle($data['item']['title']);
      $q = 'UPDATE ' . sql_table('item') . ' SET iurltitle=\'' . $url . '\' WHERE inumber=' . $data['itemid'];
      sql_query($q);
    }
    elseif ($data['context'] == 'global') {
      if ($data['plugid'] == $this->GetID() && $this->getOption(spaceReplace) !== $this->spaceReplace) {
        $this->_updateDB();
      }
    }
  }
 
  function doTemplateVar(&$item, $linktype='') {
    switch ($linktype) {
      case 'member': break;
      default: echo $this->_makeLink($item->itemid, $item->title, null, $item->timestamp);
    }
  }
 
  function doTemplateCommentsVar(&$item, &$comment, $linktype='') {
    $this->doTemplateVar(&$item);
  }
 
  function doSkinVar($skinType, $linktype, $var1='') {
    if ($linktype == 'archivelist') {
      global $blog;
      if ($this->getOption(itemArchivePrefix) == 'yes') echo '/archives/' . $blog['id'];
      else echo '/archives/';
    }
    else if ($linktype == 'member') {
      echo '/member/' . $var1;
    }
    switch($skinType) {
      case 'archive':
        global $archivenext, $archiveprev, $archivetype;
        $next = implode('/', explode('-', $archivenext));
        $prev = implode('/', explode('-', $archiveprev));
 
        if ($linktype == 'prevlink') {
          echo $this->_makeLink(0, 0, $prev,null);
        }
        else if ($linktype == 'nextlink') {
          echo $this->_makeLink(0, 0, $next,null);
        }
        break;
      case 'item':
        global $itemtitleprev, $itemtitlenext, $itemidprev, $itemidnext;
        if ($linktype == 'prevlink') {
          echo $this->_makeLink($itemidprev, $itemtitleprev);
        }
        else if ($linktype == 'nextlink') {
          if (!isset($itemidnext) || $itemidnext == '') echo '';
          else echo $this->_makeLink($itemidnext, $itemtitlenext);
        }
        break;
    }
  }
 
  function install() {
    $q = 'ALTER TABLE '. sql_table('item') . ' ADD `iurltitle` VARCHAR( 160 ) AFTER `ititle`';
    sql_query($q);
    $q = 'ALTER TABLE '. sql_table('item') .' ADD INDEX(`iurltitle`)';
    sql_query($q);
 
    $this->createOption('itemArchivePrefix','Use WordPress-style URLs?','yesno','no');
    $this->createOption('itemName','The name of the "item" file (not necessary if WP URLs are enabled)','text','item');
    $this->createOption('archiveName','The name of the "archive" file (not necessary if WP URLs are enabled)','text','archive');
    $this->createOption('spaceReplace','The character which replaces spaces in URL titles (Dashes are recommended for SEO)','select','-', 'underscore|_|dash|-');
    $this->createItemOption('urltitle','Custom URL plug (leave blank for default)','text','');
    $this->_updateDB();
  }
 
  function uninstall() {
    $q = 'ALTER TABLE '. sql_table('item') .' DROP INDEX `iurltitle`';
    sql_query($q);
    $q = 'ALTER TABLE '. sql_table('item') .' DROP `iurltitle`';
    sql_query($q);
  }
 
  function _updateDB() {
    $q = 'SELECT inumber, ititle FROM '. sql_table('item');
    $res = sql_query($q);
 
    while($row = mysql_fetch_object($res)) {
      $url = $this->getItemOption($row->inumber,'urltitle');
      if ($url == '') $url = $this->_makefancyTitle($row->ititle);
      $q = 'UPDATE ' . sql_table('item') . ' SET iurltitle=\'' . $url . '\' WHERE inumber=' . $row->inumber;
      sql_query($q);
    }
  }
 
  function _makeLink($id, $title, $archive = null, $timestamp = null) {
    global $CONF;
    //$link = $CONF['BlogURL'];
    if ($archive) {
      global $blogid;
      if ($this->getOption(itemArchivePrefix) == 'yes') $link .= '/' . $this->getOption(archiveName) . '/' . $blogid;
      $link .= '/'. $archive;
      return $link;
    }
    else {
      if ($timestamp) {
        $date = getdate($timestamp);
        $q = 'SELECT iurltitle FROM ' . sql_table('item') . ' WHERE inumber=' . $id;
        $res = sql_query($q);
        $obj = mysql_fetch_object($res);
      }
      else {
        $q = 'SELECT UNIX_TIMESTAMP(itime) as itime, iurltitle FROM ' . sql_table('item') . ' WHERE inumber=' . $id;
        $res = sql_query($q);
        $obj = mysql_fetch_object($res);
        $date = getdate($obj->itime);
      }
      if ($this->getOption(itemArchivePrefix) == 'no') $link .= '/' . $this->getOption(itemName);
      $link .= '/' . $date['year'] . '/' . $date['mon'] . '/' . $date['mday'] . '/' . $obj->iurltitle;
      return $link;
    }
  }
 
  function _makefancyTitle($title) {
    $title = trim($title);
    $title = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $title);
    preg_match_all('/[a-zA-Z0-9]+/', $title, $nt);
 
    $temp = implode($this->getOption(spaceReplace), $nt[0]);
    return strtolower($temp);
  }
 
}
?>
fancierurlcode.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki