Source for file functions.mod_rewrite_setup.php

Documentation is available at functions.mod_rewrite_setup.php

  1. <?php
  2. /**
  3.  * Installer for Advanced Mod Rewrite Plugin, used by plugin setup.
  4.  *
  5.  * Some features are taken over from initial functions.mod_rewrite_setup.php file beeing created by
  6.  * Stefan Seifarth.
  7.  *
  8.  * @todo: Remove code into an class file....
  9.  *
  10.  * @author      Murat Purc <murat@purc.de>
  11.  * @copyright   © ww.purc.de
  12.  * @package     Contenido
  13.  * @subpackage  ModRewrite
  14.  */
  15.  
  16.  
  17. defined('CON_FRAMEWORK'or die('Illegal call');
  18.  
  19. error_reporting (E_ALL E_NOTICE);
  20.  
  21.  
  22. /**
  23.  * Plugin setup class
  24.  *
  25.  * @todo should extend PluginSetupAbstract and implement IPluginSetup
  26.  *
  27.  * @author      Murat Purc (murat@purc.de)
  28.  * @package     Contenido
  29.  * @subpackage  PluginInstaller
  30.  */
  31. class PluginSetup {
  32.  
  33.     private static $_bInitialized false;
  34.  
  35.     private static $_cfg;
  36.  
  37.     private static $_db;
  38.  
  39.  
  40.     public static function initialize(){
  41.         if (self::$_bInitialized == true{
  42.             return;
  43.         }
  44.         self::$_cfg $GLOBALS['cfg'];
  45.         self::$_db  new DB_Contenido();
  46.     }
  47.  
  48.  
  49.     /**
  50.      * Install plugin
  51.      *
  52.      * Handle upgrading of mod rewrite needed database table columns
  53.      */
  54.     public static function install(){
  55.  
  56.         self::initialize();
  57.  
  58.         // check the existance of art_lang.urlname
  59.         $sql "SELECT urlname FROM " self::$_cfg['tab']['art_lang'" LIMIT 0,1";
  60.         self::$_db->query($sql);
  61.         if (!self::$_db->next_record()) {
  62.             // add field 'urlname' to table
  63.             $sql "ALTER TABLE " self::$_cfg['tab']['art_lang'" ADD urlname VARCHAR( 128 ) AFTER title";
  64.             self::$_db->query($sql);
  65.         }
  66.  
  67.         // check the existance of cat_lang.urlpath
  68.         $sql "SELECT urlpath FROM " self::$_cfg['tab']['cat_lang'" LIMIT 0,1";
  69.         self::$_db->query($sql);
  70.         if (!self::$_db->next_record()) {
  71.             // add field 'urlpath' to table
  72.             $sql "ALTER TABLE " self::$_cfg['tab']['cat_lang'" ADD urlpath VARCHAR( 255 ) AFTER urlname";
  73.             self::$_db->query($sql);
  74.         }
  75.  
  76.         // check for empty article fields
  77.         $sql "SELECT idlang, title, idart FROM " self::$_cfg['tab']['art_lang'" WHERE urlname IS NULL OR urlname = ''";
  78.         self::$_db->query($sql);
  79.         while (self::$_db->next_record()) {
  80.             self::_setArticle(self::$_db->f('title')self::$_db->f('idart')self::$_db->f('idlang'));
  81.         }
  82.  
  83.         // check for empty category urlname
  84.         $sql "SELECT name, idcat, idlang FROM " self::$_cfg['tab']['cat_lang'" WHERE urlname IS NULL OR urlname = ''";
  85.         self::$_db->query($sql);
  86.         while (self::$_db->next_record()) {
  87.             self::_setCategory(self::$_db->f('name')self::$_db->f('idcat')self::$_db->f('idlang'));
  88.         }
  89.  
  90.         // check for empty category urlpath
  91.         $sql "SELECT name, idcat, idlang FROM " self::$_cfg['tab']['cat_lang'" WHERE urlpath IS NULL OR urlpath = ''";
  92.         self::$_db->query($sql);
  93.         while (self::$_db->next_record()) {
  94.             self::_setCategoryPath(self::$_db->f('idcat')self::$_db->f('idlang'));
  95.         }
  96.     }
  97.  
  98.  
  99.     /**
  100.      * Upgrade plugin
  101.      *
  102.      * Handle upgrading of mod rewrite needed database table columns
  103.      */
  104.     public static function upgrade(){
  105.         self::install();
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Delete plugin
  111.      *
  112.      * Removed done changes to database during installation/upgrade process
  113.      */
  114.     public static function uninstall({
  115.         self::initialize();
  116.         // remove field 'urlpath' from 'cat_lang' table
  117.         $sql "ALTER TABLE " self::$_cfg['tab']['cat_lang'" DROP urlpath";
  118.         self::$_db->query($sql);
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Set websafe name in article list
  124.      *
  125.      * insert new websafe name in article list
  126.      *
  127.      * @param   string  original name (will be converted)
  128.      * @param   integer current article id
  129.      * @param   integer current language id
  130.      * @return  boolean true if insert was successfully
  131.      */
  132.     private static function _setArticle($sName=""$iArtId=0$iLangId=0$iCatId=0{
  133.         static $db;
  134.  
  135.         if (!isset($db)) {
  136.             $db new DB_Contenido();
  137.         }
  138.  
  139.         // create websafe name
  140.         $sNewName capiStrCleanURLCharacters($sName);
  141.  
  142.         // check if websafe name already exists
  143.         if (self::_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  144.             // create new websafe name if exists
  145.             $sNewName capiStrCleanURLCharacters($sName'_' $iArtId;
  146.         }
  147.  
  148.         // check again - and set name
  149.         if (!self::_inArticles($sNewName$iArtId$iLangId$iCatId)) {
  150.             // insert websafe name in article list
  151.             $sql "UPDATE " self::$_cfg['tab']['art_lang'" SET urlname = '" $sNewName "' WHERE idart = '" $iArtId "' AND idlang = '" $iLangId "'";
  152.             return $db->query($sql);
  153.         else {
  154.             return false;
  155.         }
  156.     }
  157.  
  158.  
  159.     /**
  160.      * Set websafe name in category list
  161.      *
  162.      * insert new websafe name in category list
  163.      *
  164.      * @param   string  original name (will be converted)
  165.      * @param   integer current article id
  166.      * @param   integer current language id
  167.      * @return  boolean true if insert was successfully
  168.      */
  169.     private static function _setCategory($sName=''$iCatId=0$iLangId=0{
  170.         static $db;
  171.  
  172.         if (!isset($db)) {
  173.             $db new DB_Contenido();
  174.         }
  175.  
  176.         // create websafe name
  177.         $sNewName capiStrCleanURLCharacters($sName);
  178.  
  179.         // check if websafe name already exists
  180.         if (self::_inCategory($sNewName$iCatId$iLangId)) {
  181.             // create new websafe name if exists
  182.             $sNewName capiStrCleanURLCharacters($sName'_' $iCatId;
  183.         }
  184.  
  185.         // check again - and set name
  186.         if (!self::_inCategory($sNewName$iCatId$iLangId)) {
  187.             // insert websafe name in article list
  188.             $sql "UPDATE " self::$_cfg['tab']['cat_lang'" SET urlname = '$sNewName' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  189.             return $db->query($sql);
  190.         else {
  191.             return false;
  192.         }
  193.     }
  194.  
  195.  
  196.     /**
  197.      * Build and set recursiv path for mod_rewrite rule like server directories
  198.      * (dir1/dir2/dir3)
  199.      *
  200.      * @param   int     $iCatId   Latest category id
  201.      * @param   int     $iLangId  Language id
  202.      * @param   int     $iLastId  Last category id
  203.      * @return     string    linkpath with correct uri
  204.      */
  205.     private static function _setCategoryPath($iCatId=0$iLangId=0$iLastId=0{
  206.         static $db;
  207.  
  208.         if (!isset($db)) {
  209.             $db new DB_Contenido();
  210.         }
  211.  
  212.         $aDirs     array();
  213.         $bFinish   false;
  214.         $iTmpCatId $iCatId;
  215.  
  216.         while ($bFinish == false{
  217.             $sql "SELECT cl.urlname, c.parentid FROM " self::$_cfg['tab']['cat_lang'" cl "
  218.                  . "LEFT JOIN " self::$_cfg['tab']['cat'" c ON cl.idcat = c.idcat "
  219.                  . "WHERE cl.idcat = '$iTmpCatId' AND cl.idlang = '$iLangId'";
  220.             $db->query($sql);
  221.             if ($db->next_record()) {
  222.                 $aDirs[]   $db->f('urlname');
  223.                 $iTmpCatId $db->f('parentid');
  224.  
  225.                 if ($db->f('parentid'== || $db->f('parentid'== $iLastId{
  226.                     $bFinish true;
  227.                 }
  228.             else {
  229.                 $bFinish true;
  230.             }
  231.         }
  232.  
  233.         // reverse array entries and create directory string
  234.         $sPath join('/'array_reverse($aDirs));
  235.  
  236.         // insert urlpath for category
  237.         $sql "UPDATE " self::$_cfg['tab']['cat_lang'" SET urlpath = '$sPath' WHERE idcat = '$iCatId' AND idlang = '$iLangId'";
  238.         return $db->query($sql);
  239.     }
  240.  
  241.  
  242.     /**
  243.      * Check articles on websafe name
  244.      *
  245.      * Check all articles in the current category on existing same websafe name
  246.      *
  247.      * @param   string  Websafe name to check
  248.      * @param   integer current article id
  249.      * @param   integer current language id
  250.      * @param   integer current category id
  251.      * @return  boolean true if websafename already exists, false if not
  252.      */
  253.     private static function _inArticles($sName=''$iArtId=0$iLangId=0$iCatId=0{
  254.         static $db;
  255.  
  256.         if (!isset($db)) {
  257.             $db new DB_Contenido();
  258.         }
  259.  
  260.         $iCatId = (int) $iCatId;
  261.  
  262.         // handle multipages
  263.         if ($iCatId == 0{
  264.             // get category id if not set
  265.             $sql "SELECT idcat FROM " self::$_cfg['tab']['cat_art'" WHERE idart = '$iArtId'";
  266.             $db->query($sql);
  267.             $db->next_record();
  268.             $iCatId ($db->f('idcat'0$db->f('idcat''0';
  269.         }
  270.  
  271.         $sWhere " ca.idcat = '$iCatId' AND al.idlang = '$iLangId "' AND"
  272.                 . " al.urlname = '" $sName "' AND al.idart <> '$iArtId'";
  273.  
  274.         // check if websafe name is in this category
  275.         $sql "SELECT count(al.idart) as numcats FROM " self::$_cfg['tab']['art_lang'" al LEFT JOIN " self::$_cfg['tab']['cat_art'" ca ON al.idart = ca.idart WHERE " $sWhere;
  276.         $db->query($sql);
  277.         $db->next_record();
  278.  
  279.         return ($db->f('numcats'0true false;
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Check categories on websafe name
  285.      *
  286.      * Check all categories in the main parent category on existing same websafe name
  287.      *
  288.      * @param   string  Websafe name to check
  289.      * @param   integer current category id
  290.      * @param   integer current language id
  291.      * @return  boolean true if websafename already exists, false if not
  292.      */
  293.     private static function _inCategory($sName=''$iCatId=0$iLangId=0{
  294.         static $db;
  295.  
  296.         if (!isset($db)) {
  297.             $db new DB_Contenido();
  298.         }
  299.  
  300.         // get parentid
  301.         $sql "SELECT parentid FROM " self::$_cfg['tab']['cat'" WHERE idcat = '$iCatId'";
  302.         $db->query($sql);
  303.         $db->next_record();
  304.         $iParentId ($db->f('parentid'0$db->f('parentid''0';
  305.  
  306.         $sWhere " c.parentid = '$iParentId' AND cl.idlang = '$iLangId "' AND"
  307.                 . " cl.urlname = '" $sName "' AND cl.idcat <> '$iCatId'";
  308.  
  309.         // check if websafe name is in this category
  310.         $sql "SELECT count(cl.idcat) as numcats FROM " self::$_cfg['tab']['cat_lang'" cl LEFT JOIN " self::$_cfg['tab']['cat'" c ON cl.idcat = c.idcat WHERE " $sWhere;
  311.         $db->query($sql);
  312.         $db->next_record();
  313.  
  314.         return ($db->f('numcats'0true false;
  315.     }
  316.  
  317. }

Documentation generated on Sun, 08 Feb 2009 22:00:59 +0100 by phpDocumentor 1.4.1