/**
  * saves a changed or a new category
  *
  * @param integer $cid, if null -> new category
  */
 function Joom_User_SaveUserCat(&$cid)
 {
     $config = Joom_getConfig();
     $mainframe =& JFactory::getApplication('site');
     $database =& JFactory::getDBO();
     $user =& JFactory::getUser();
     jimport('joomla.filesystem.file');
     $row = new mosCatgs($database);
     if ($cid == 0) {
         $database->setQuery(" SELECT \n                              COUNT(cid)\n                            FROM \n                              #__joomgallery_catg\n                            WHERE \n                              owner=" . $user->get('id') . " \n                          ");
         $countcat = $database->loadResult();
         if ($countcat >= $config->jg_maxusercat) {
             $mainframe->redirect('index.php?option=com_joomgallery&func=showusercats' . _JOOM_ITEMID, false);
         }
         $newcat = true;
     } else {
         $newcat = false;
         //load an existing category
         $row->load($cid);
         //get old parent category
         $parentold = $row->parent;
         //get old category name
         $catnameold = $row->name;
     }
     //get new values
     if (!$row->bind($_POST)) {
         echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
         exit;
     }
     if (get_magic_quotes_gpc()) {
         $row->name = stripslashes($row->name);
         $row->description = stripslashes($row->description);
     }
     if (!$newcat) {
         if ($catnameold != $row->name) {
             // Macht den neuen Kategorienamen sicher, wenn geaendert
             JFilterOutput::objectHTMLSafe($row->name);
             // Joom_FixCatname; Umlaute werden umgewandelt und alle Sonderzeichen bis auf
             // den Unterstrich entfernt, gilt nur fuer den catpath
             $catname = Joom_FixCatname($row->name);
             $catnamemodif = true;
         } else {
             $catname = $catnameold;
             $catnamemodif = false;
         }
         //Kategorieordner verschieben, wenn die Parentzuordnung oder der Kategoriename
         //geaendert wurde
         if ($parentold != $row->parent || $catnamemodif == true) {
             //alten Pfad sichern
             $catpathold = $row->catpath;
             //Kategoriepfad der Parent-Kategorie lesen, nur noetig wenn parent != 0
             if ($row->parent != 0) {
                 $row_parent = new mosCatgs($database);
                 $row_parent->load($row->parent);
                 $catpathnew = $row_parent->catpath . '/' . $catname . '_' . $row->cid;
             } else {
                 $catpathnew = $catname . '_' . $row->cid;
             }
             //Kategoriepfad in DB aktualisieren
             $row->catpath = $catpathnew;
             $cat_originalpathold = JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpathold);
             $cat_picturepathold = JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpathold);
             $cat_thumbnailpathold = JPath::clean(JPATH_ROOT . DS . $config->jg_paththumbs . $catpathold);
             $cat_originalpathnew = JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpathnew);
             $cat_picturepathnew = JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpathnew);
             $cat_thumbnailpathnew = JPath::clean(JPATH_ROOT . DS . $config->jg_paththumbs . $catpathnew);
             //Ordner verschieben
             //TODO Fehlermeldungen
             JFolder::move($cat_originalpathold, $cat_originalpathnew);
             JFolder::move($cat_picturepathold, $cat_picturepathnew);
             JFolder::move($cat_thumbnailpathold, $cat_thumbnailpathnew);
             //wenn Parentkategorie geaendert wurde, den Catpath aller Unterkategorien
             //in DB anpassen
             $rowid = $row->cid;
             Joom_UpdateNewCatpath($rowid, $catpathold, $catpathnew);
         }
         if (!$row->store()) {
             echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
             exit;
         }
     } else {
         //Neue Kategorie
         //Kategorienamen absichern
         JFilterOutput::objectHTMLSafe($row->name);
         // Joom_FixCatname; Umlaute werden umgewandelt und alle Sonderzeichen bis auf
         // den Unterstrich entfernt, gilt nur fuer den catpath
         $catname = Joom_FixCatname($row->name);
         $row->img_position = 0;
         $row->catimage = null;
         //access von der Elternkategorie erben, wenn nicht Admin/SuperAdmin
         if (!$this->adminlogged) {
             $row->owner = $user->get('id');
             $rowparent = new mosCatgs($database);
             $rowparent->load($row->parent);
             $row->access = $rowparent->access;
         } else {
             $row->owner = null;
         }
         //Anlegen des DB Eintrages
         if (!$row->store()) {
             echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
             exit;
         }
         //catpath mit vergebener cid aufbauen
         $parentpathnew = Joom_GetCatPath($row->parent);
         $catpathnew = $parentpathnew . $catname . '_' . $row->cid;
         $row->catpath = $catpathnew;
         $row->store();
         //Kategorieverzeichnisse anlegen
         Joom_MakeDirectory(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpathnew);
         Joom_MakeDirectory(JPATH_ROOT . DS . $config->jg_pathimages . $catpathnew);
         Joom_MakeDirectory(JPATH_ROOT . DS . $config->jg_paththumbs . $catpathnew);
     }
     // Redirect zur Kategorieuebersicht
     $mainframe->redirect(JRoute::_('index.php?option=com_joomgallery&func=showusercats&uid=' . $user->get('id') . _JOOM_ITEMID, false));
 }
 /**
  * creates directories and the database entry for a category
  * @param object holds information about the new category
  * @return boolean true if success
  */
 function Joom_Migrate_CreateCategory($cat)
 {
     jimport('joomla.filesystem.file');
     //some checks
     if (!isset($cat->id)) {
         return false;
     }
     if (!isset($cat->name)) {
         $cat->name = 'no cat name';
     }
     if (!isset($cat->parent)) {
         $cat->parent = 0;
     }
     if (!isset($cat->description)) {
         $cat->description = '';
     }
     if (!isset($cat->ordering)) {
         $cat->ordering = 0;
     }
     if (!isset($cat->published)) {
         $cat->published = 0;
     }
     if (!isset($cat->owner)) {
         $cat->owner = 'NULL';
     }
     if (!isset($cat->catimage)) {
         $cat->catimage = '';
     }
     if (!isset($cat->img_position)) {
         $cat->img_position = 0;
     }
     //make the name safe
     JFilterOutput::objectHTMLSafe($cat->name);
     //if the new category should be assigned as subcategory...
     if ($cat->parent) {
         //save the category path of parent category in a variable
         $parentpath = Joom_GetCatPath($cat->parent);
         // otherwise let it empty
     } else {
         $parentpath = '';
     }
     //creation of category path
     //cleaning of category title with function Joom_FixFilename
     //so special chars are converted and underscore removed
     //affects only the category path
     $newcatname = Joom_FixCatname($cat->name);
     //add a undersore and the category id
     //affects only the category path
     $newcatname = $newcatname . '_' . $cat->id;
     //prepend - if exists - the parent category path
     $catpath = $parentpath . $newcatname;
     //create the paths of category for originals, pictures, thumbnails
     $cat_originalpath = JPath::clean(JPATH_ROOT . DS . $this->_config->jg_pathoriginalimages . $catpath);
     $cat_picturepath = JPath::clean(JPATH_ROOT . DS . $this->_config->jg_pathimages . $catpath);
     $cat_thumbnailpath = JPath::clean(JPATH_ROOT . DS . $this->_config->jg_paththumbs . $catpath);
     $result = array();
     $result[] = JFolder::create($cat_originalpath);
     $result[] = JFile::copy(JPATH_COMPONENT_SITE . DS . 'assets' . DS . 'index.html', $cat_originalpath . DS . 'index.html');
     $result[] = JFolder::create($cat_picturepath);
     $result[] = JFile::copy(JPATH_COMPONENT_SITE . DS . 'assets' . DS . 'index.html', $cat_picturepath . DS . 'index.html');
     $result[] = JFolder::create($cat_thumbnailpath);
     $result[] = JFile::copy(JPATH_COMPONENT_SITE . DS . 'assets' . DS . 'index.html', $cat_thumbnailpath . DS . 'index.html');
     //create database entry
     $query = "INSERT INTO #__joomgallery_catg\n                (cid, name, parent, description, ordering, access, published, owner, catimage, img_position, catpath)\n              VALUES\n                (" . $cat->id . ",\n                 '" . $cat->name . "',\n                 " . $cat->parent . ",\n                 '" . $cat->description . "',\n                 " . $cat->ordering . ",\n                 " . $cat->access . ",\n                 " . $cat->published . ",\n                 " . $cat->owner . ",\n                 '" . $cat->catimage . "',\n                 " . $cat->img_position . ",\n                 '" . $catpath . "')";
     $this->_db->setQuery($query);
     $result['db'] = $this->_db->query();
     if (!$result['db']) {
         $this->Joom_Migrate_SetError($this->_db->getErrorMsg(), true);
     }
     if (!in_array(false, $result)) {
         $this->Joom_Migrate_WriteLogfile("Category " . $cat->id . " created: " . $cat->name);
         return true;
     } else {
         $this->Joom_Migrate_WriteLogfile(" -> Error creating category " . $cat->id . ": " . $cat->name);
         return false;
     }
 }