/**
 * Check if a directory only contains the index.html
 * @TODO: better handling with JFile/JFolder
 *
 * @param    string    $directory: absolute path to directory
 * @param    integer   $catid: id of category
 */
function Joom_CheckEmptyDirectory($directory, $catid)
{
    //check if direcory exists, if no abort with error message
    if (!is_dir($directory)) {
        Joom_AlertErrorMessages(0, $catid, $directory, 0);
    }
    //open the directory
    $dir = opendir($directory);
    //if not succesful not enough write permissions, abort with error message
    if (!$dir) {
        Joom_AlertErrorMessages(0, $catid, $directory, 0);
    }
    $index = "index.html";
    while (false != ($entry = readdir($dir))) {
        //if entry is different from link to actual directory (.) or link to parent
        //directory (..) or index.html
        if ($entry != '.' && $entry != '..' && $entry != $index) {
            //close the directory
            closedir($dir);
            //error message
            Joom_AlertErrorMessages(0, $catid, $directory, 0);
        }
    }
}
 /**
  * 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');
 }
 /**
  * Delete picture(s) from directory and database
  *
  * @param    Array  $cid: id's of pictures to be deleted
  */
 function Joom_RemovePictures($ids)
 {
     $mainframe =& JFactory::getApplication('administrator');
     $database =& JFactory::getDBO();
     $config = Joom_getConfig();
     jimport('joomla.filesystem.file');
     //one or more pictures
     if (!is_array($ids) || count($ids) < 1) {
         //no picture(s) -> error message and abort
         $msg = JText::_('JGA_ALERT_SELECT_AN_ITEM_TO_DELETE');
         $mainframe->redirect('index.php?option=' . _JOOM_OPTION . '&act=pictures', $msg, 'error');
     }
     //two arrays, one for the succesful deleted, one for the pictures with
     //error in actions
     //TODO: error messages
     $deleted_items = array();
     $notdeleted_items = array();
     // loop through array
     foreach ($ids as $id) {
         //database query to get the category, name of picture and thumb
         $database->setQuery("SELECT id, catid, imgfilename, imgthumbname\n          FROM #__joomgallery\n          WHERE id = {$id}");
         if ($database->query()) {
             $row = $database->loadObject();
             //catpath for category
             $catpath = Joom_GetCatPath($row->catid);
             //database query to check if there are other pictures with this thumbnail
             //assigned and how many
             $database->setQuery("SELECT COUNT(id)\n            FROM #__joomgallery\n            WHERE imgthumbname = '" . $row->imgthumbname . "'\n            AND id != '" . $row->id . "'\n            AND catid = '" . $row->catid . "'");
             $count = $database->loadResult();
             //database query to check if there are other pictures with this detail
             //or original assigned ad how many
             $database->setQuery("SELECT COUNT(id)\n            FROM #__joomgallery\n            WHERE imgfilename = '" . $row->imgfilename . "'\n            AND id != '" . $row->id . "'\n            AND catid = '" . $row->catid . "'");
             $count2 = $database->loadResult();
             //delete the thumbnail if there are no other pictures
             //in same category assigned to it
             if ($count < 1) {
                 if (!JFile::delete(JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $row->imgthumbname)) {
                     //if thumbnail is not deleteable error message and abort
                     Joom_AlertErrorMessages(0, $row->catid, JPath::clean(JPATH_ROOT . DS . $config->jg_paththumbs . $catpath), $row->imgthumbname);
                 }
             }
             //delete the detail if there are no other detail and
             //originals from same category assigned to it
             if ($count2 < 1) {
                 if (!JFile::delete(JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $row->imgfilename)) {
                     //if detail is not deleteable error message and abort
                     Joom_AlertErrorMessages(0, $row->catid, JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpath), $row->imgfilename);
                 }
                 //original exists?
                 if (JFile::exists(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath . $row->imgfilename)) {
                     //delete it
                     if (!JFile::delete(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath . $row->imgfilename)) {
                         //if original is not deleteable error message and abort
                         Joom_AlertErrorMessages(0, $row->catid, JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath), $row->imgfilename);
                     }
                 }
             }
             //not succesful database query
         } else {
             echo "<script> alert('" . $database->getErrorMsg() . "');\n              window.history.go(-1); </script>\n";
         }
         //TODO aha: better wrap the following DB actions in 'begin...commit/rollback'
         //to get consistence in case of error
         //delete the database entry of picture
         $database->setQuery("DELETE\n          FROM #__joomgallery\n          WHERE id = {$id}");
         if (!$database->query()) {
             echo "<script> alert('" . $database->getErrorMsg() . "');\n              window.history.go(-1); </script>\n";
         }
         //delete the corresponding database entries in comments
         $database->setQuery("DELETE\n          FROM #__joomgallery_comments\n          WHERE cmtpic = {$id}");
         if (!$database->query()) {
             echo "<script> alert('" . $database->getErrorMsg() . "');\n              window.history.go(-1); </script>\n";
         }
         //delete the corresponding database entries in nameshields
         $database->setQuery("DELETE\n          FROM #__joomgallery_nameshields\n          WHERE npicid = {$id}");
         if (!$database->query()) {
             echo "<script> alert('" . $database->getErrorMsg() . "');\n              window.history.go(-1); </script>\n";
         }
         //add the id of succesful deleted picture to array
         array_push($deleted_items, $id);
     }
     $mainframe->redirect('index.php?option=' . _JOOM_OPTION . '&act=pictures');
 }