Source for file class.modrewriteinstaller.php

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

Documentation generated on Tue, 25 Nov 2008 22:07:27 +0100 by phpDocumentor 1.4.1