/**
  * Deletes the folders and the database entry of category
  *
  * @param    integer   $catid: id of category, e.g. 10
  */
 function Joom_DeleteCategory($catid)
 {
     $database =& JFactory::getDBO();
     $config = Joom_getConfig();
     $mainframe =& JFactory::getApplication('administrator');
     //path of category
     $catpath = Joom_GetCatPath($catid);
     //compose the paths for originals, pictures, thumbs
     $catorigdir = JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath);
     $catpicdir = JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpath);
     $catthumbdir = JPath::clean(JPATH_ROOT . DS . $config->jg_paththumbs . $catpath);
     //check with function Joom_CheckEmptyDirectory()
     //if folders are empty and writeable (permissions set for deletion)
     Joom_CheckEmptyDirectory($catorigdir, $catid);
     Joom_CheckEmptyDirectory($catpicdir, $catid);
     Joom_CheckEmptyDirectory($catthumbdir, $catid);
     //delete the folder in originals
     $resorig = JFolder::delete($catorigdir);
     //if not succesful, output an error message and abort
     if (!$resorig) {
         Joom_AlertErrorMessages(0, $catid, $catorigdir, 0);
     }
     //delete the folder in pictures
     $respic = JFolder::delete($catpicdir);
     //if not succesful....
     if (!$respic) {
         //try to recreate the folder in originals
         $resdiro = Joom_MakeDirectory($catorigdir);
         //if not succesful, output an error message and abort
         if ($resdiro != 0) {
             if ($resdiro == -1) {
                 Joom_AlertErrorMessages(0, $catid, $catorigdir, 0);
             }
             if ($resdiro == -2) {
                 Joom_AlertErrorMessages(0, $catid, $catorigdir, 0);
             }
         } else {
             //if not succesful, output an error message and abort
             Joom_AlertErrorMessages(0, $catid, $catpicdir, 0);
         }
     }
     //delete the thumbnail folder
     $resthumb = JFolder::delete($catthumbdir);
     //if not succesful....
     if (!$resthumb) {
         //try to recreate the folder in originals
         $resdiro = Joom_MakeDirectory($catorigdir);
         //if not succesful, output an error message and abort
         if ($resdiro != 0) {
             if ($resdiro == -1) {
                 Joom_AlertErrorMessages(0, $catid, $catorigdir, 0);
             }
             if ($resdiro == -2) {
                 Joom_AlertErrorMessages(0, $catid, $catorigdir, 0);
             }
         } else {
             //if not succesful, output an error message about thumbnail folder and abort
             Joom_AlertErrorMessages(0, $catid, $catthumbdir, 0);
         }
         //and try to recreate the folder in pictures
         $resdirp = Joom_MakeDirectory($catpicdir);
         //if not succesful in recreation,  output an error message and abort
         if ($resdirp != 0) {
             if ($resdirp == -1) {
                 Joom_AlertErrorMessages(0, $catid, $catpicdir, 0);
             }
             if ($resdirp == -2) {
                 Joom_AlertErrorMessages(0, $catid, $catpicdir, 0);
             }
         } else {
             //if not succesful, output an error message about picture folder and abort
             Joom_AlertErrorMessages(0, $catid, $catpicdir, 0);
         }
     }
     //delete database entry if all folders succesfully deleted
     $database->setQuery("DELETE\n        FROM #__joomgallery_catg\n        WHERE cid = {$catid}");
     $database->query();
     echo $database->getErrorMsg();
     //update of ordering
     $fp = new mosCatgs($database);
     $fp->reorder();
     //delete the userstate variable 'catid' if exists
     $mainframe->setUserState('joom.pictures.catid', '0');
 }
 /**
  * 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));
 }