Source for file functions.mod_rewrite_setup.php

Documentation is available at functions.mod_rewrite_setup.php

  1. <?php
  2. /**
  3.  * Functions to upgrade to modrewrite, used by plugin setup
  4.  *
  5.  * @author      Stefan Seifarth / stese
  6.  * @copyright   © www.polycoder.de
  7.  * @package     Contenido
  8.  * @subpackage  ModRewrite
  9.  */
  10.  
  11.  
  12. /******************************************
  13. * File      :   functions.mod_rewrite_setup.php
  14. * Project   :   Contenido
  15. * Descr     :   functions to upgrade to modrewrite
  16. *
  17. * Author    :   Stefan Seifarth
  18. * Created   :   28.12.2005
  19. * Modified  :   28.12.2005
  20. * Modified  :   06.03.2008, Murat Purc, extracted from setup/lib directory
  21. * 
  22. * © www.polycoder.de
  23. */
  24. error_reporting (E_ALL ^ E_NOTICE);
  25.  
  26.  
  27. /**
  28.  * upgradeModRewrite
  29.  * 
  30.  * handle upgrading of mod rewrite needed
  31.  * database table columns
  32.  */
  33. function upgradeModRewrite () {
  34.     global $cfg;
  35.  
  36.     $db = new DB_Contenido();
  37.  
  38.     $sql = "ALTER TABLE " . $cfg["tab"]["art_lang"] . " ADD urlname VARCHAR( 128 ) AFTER title";
  39.     $db->query( $sql );
  40.  
  41.     // check for empty article fields
  42.     $sql = "SELECT idlang, title, idart FROM " . $cfg["tab"]["art_lang"] . " WHERE urlname IS NULL OR urlname = ''";
  43.     $db->query($sql);
  44.     while ( $db->next_record() ) {
  45.         set_article($db->f('title'), $db->f('idart'), $db->f('idlang'));
  46.     }
  47.  
  48.     // check for empty category fields
  49.     $sql = "SELECT name, idcat, idlang FROM " . $cfg["tab"]["cat_lang"] . " WHERE urlname IS NULL OR urlname = ''";
  50.     $db->query($sql);
  51.     while ( $db->next_record() ) {
  52.         set_category($db->f('name'), $db->f('idcat'), $db->f('idlang'));
  53.     }
  54.  
  55. }
  56.  
  57.  
  58. function uninstallModRewrite() {
  59.     global $cfg;
  60.     $db = new DB_Contenido();
  61.     $sql = "ALTER TABLE " . $cfg["tab"]["art_lang"] . " DROP urlname";
  62.     $db->query( $sql );
  63. }
  64.  
  65.  
  66. /**
  67.  * set_article()
  68.  *
  69.  * set websafe name in article list
  70.  *
  71.  * insert new websafe name in article list
  72.  *
  73.  * @param   string  original name (will be converted)
  74.  * @param   integer current article id
  75.  * @param   integer current language id
  76.  * @return  boolean true if insert was successfully
  77.  */
  78. function set_article ( $str_name = "", $int_id = 0, $int_lang_id = 0, $int_idcat = 0) {
  79.     global $cfg;
  80.     static $db;
  81.     
  82.     if (!isset($db)) {
  83.         $db = new DB_Contenido();
  84.     }
  85.     
  86.     // create websafe name
  87.     $str_new_name = capiStrCleanURLCharacters($str_name);
  88.  
  89.     // check if websafe name already exists
  90.     if ( in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat) ) {
  91.         // create new websafe name if exists
  92.         $str_new_name = capiStrCleanURLCharacters($str_name) . "_" . $int_id;
  93.     } 
  94.  
  95.     // check again - and set name
  96.     if (!in_articles($str_new_name, $int_id, $int_lang_id, $int_idcat)) {
  97.         // insert websafe name in article list
  98.         $sql = "UPDATE " . $cfg["tab"]["art_lang"] . " SET urlname = '" . $str_new_name . "' WHERE idart = '" . $int_id . "' AND idlang = '" . $int_lang_id . "'";
  99.         return $db->query($sql);
  100.     } else {
  101.         return false;
  102.     }
  103. } // end function
  104.  
  105.  
  106.  
  107.  
  108. /**
  109.  * set_category()
  110.  *
  111.  * set websafe name in category list
  112.  *
  113.  * insert new websafe name in category list
  114.  *
  115.  * @param   string  original name (will be converted)
  116.  * @param   integer current article id
  117.  * @param   integer current language id
  118.  * @return  boolean true if insert was successfully
  119.  */
  120. function set_category ( $str_name = "", $int_id = 0, $int_lang_id = 0) {
  121.     global $cfg;
  122.     static $db;
  123.     
  124.     if (!isset($db)) {
  125.         $db = new DB_Contenido();
  126.     }
  127.     
  128.     // create websafe name
  129.     $str_new_name = capiStrCleanURLCharacters($str_name);
  130.  
  131.     // check if websafe name already exists
  132.     if (in_category($str_new_name, $int_id, $int_lang_id)) {
  133.     // create new websafe name if exists
  134.     $str_new_name = capiStrCleanURLCharacters($str_name) . "_" . $int_id;
  135.     }
  136.  
  137.  
  138.     // check again - and set name
  139.     if (!in_category($str_new_name, $int_id, $int_lang_id)) {
  140.         // insert websafe name in article list
  141.         $sql = "UPDATE " . $cfg["tab"]["cat_lang"] . " SET urlname = '$str_new_name' WHERE idcat = '$int_id' AND idlang = '$int_lang_id'";
  142.         return $db->query($sql);
  143.     } else {
  144.         return false;
  145.     }
  146. } // end function
  147.  
  148.  
  149.  
  150.     
  151. /**
  152.  * in_articles()
  153.  *
  154.  * Check articles on websafe name
  155.  *
  156.  * Check all articles in the current category
  157.  * on existing same websafe name
  158.  *
  159.  * @param   string  Websafe name to check
  160.  * @param   integer current article id
  161.  * @param   integer current language id
  162.  * @param   integer current category id
  163.  * @return  boolean true if websafename already exists, false if not
  164.  */
  165. function in_articles ( $str_name = "", $int_id = 0, $int_lang_id = 0, $int_idcat = 0) {
  166.     global $cfg;
  167.     static $db;
  168.     
  169.     if (!isset($db)) {
  170.         $db = new DB_Contenido();
  171.     }
  172.     
  173.     // handle multipages
  174.     if ($int_idcat > 0) {
  175.         $int_category_id = $int_idcat;
  176.     } else {
  177.         // get category id if not set
  178.         $sql = "SELECT idcat FROM " . $cfg["tab"]["cat_art"] . " WHERE idart = '$int_id'";
  179.         $db->query($sql);
  180.         $db->next_record();
  181.         $int_category_id = ($db->f("idcat") > 0 ) ? $db->f("idcat") : "0";
  182.     }
  183.  
  184.     $str_where = " ca.idcat = '$int_category_id' AND"
  185.                . " al.idlang = '" . $int_lang_id . "' AND"
  186.                . " al.urlname = '" . $str_name . "' AND"
  187.                . " al.idart <> '$int_id'";
  188.  
  189.     // check if websafe name is in this category
  190.     $sql = "SELECT count(al.idart) as numcats FROM " . $cfg["tab"]["art_lang"] . " al LEFT JOIN " . $cfg["tab"]["cat_art"] . " ca ON al.idart = ca.idart WHERE " . $str_where;
  191.     $db->query($sql);
  192.     $db->next_record();
  193.  
  194.     $int_count = $db->f("numcats");
  195.  
  196.     return ($int_count > 0) ? true : false;
  197. } // end function
  198.  
  199.  
  200.  
  201. /**
  202.  * in_categories()
  203.  *
  204.  * Check categories on websafe name
  205.  *
  206.  * Check all categories in the main parent
  207.  * category on existing same websafe name
  208.  *
  209.  * @param   string  Websafe name to check
  210.  * @param   integer current category id
  211.  * @param   integer current language id
  212.  * @return  boolean true if websafename already exists, false if not
  213.  */
  214. function in_category ( $str_name = "", $int_id = 0, $int_lang_id = 0) {
  215.     global $cfg;
  216.     static $db;
  217.     
  218.     if (!isset($db)) {
  219.         $db = new DB_Contenido();
  220.     }
  221.  
  222.     // get parentid
  223.     $sql = "SELECT parentid FROM " . $cfg["tab"]["cat"] . " WHERE idcat = '$int_id'";
  224.     $db->query($sql);
  225.     $db->next_record();
  226.     $int_parent_id = ($db->f("parentid") > 0 ) ? $db->f("parentid") : "0";
  227.  
  228.     $str_where = " c.parentid = '$int_parent_id' AND"
  229.                . " cl.idlang = '" . $int_lang_id . "' AND"
  230.                . " cl.urlname = '" . $str_name . "' AND"
  231.                . " cl.idcat <> '$int_id'";
  232.  
  233.     // check if websafe name is in this category
  234.     $sql = "SELECT count(cl.idcat) as numcats FROM " . $cfg["tab"]["cat_lang"] . " cl LEFT JOIN " . $cfg["tab"]["cat"] . " c ON cl.idcat = c.idcat WHERE " . $str_where;
  235.     $db->query($sql);
  236.     $db->next_record();
  237.  
  238.     $int_count = $db->f("numcats");
  239.  
  240.     return ($int_count > 0) ? true : false;
  241. } // end function
  242.  
  243.  
  244. ?>

Documentation generated on Mon, 26 May 2008 19:42:18 +0200 by phpDocumentor 1.4.0