/**
  * Recursively copy a folder and its contents
  * http://aidan.dotgeek.org/lib/?file=function.copyr.php
  *
  * @author      Aidan Lister <*****@*****.**>
  * @version     1.0.1
  * @param       string   $source    Source path
  * @param       string   $dest      Destination path
  * @return      bool     Returns TRUE on success, FALSE on failure
  */
 function copyDir($source, $dest)
 {
     clearstatcache();
     // Simple copy for a file
     if (is_file($source)) {
         return File::copy($source, $dest);
     }
     // Make destination directory
     if (!File::isDir($dest)) {
         File::createDir($dest);
     }
     // Loop through the folder
     $dir = dir($source);
     while (false !== ($entry = $dir->read())) {
         // Skip pointers
         if ($entry == '.' || $entry == '..') {
             continue;
         }
         // Deep copy directories
         if ($dest !== "{$source}/{$entry}") {
             MyFile::copyDir("{$source}/{$entry}", "{$dest}/{$entry}");
         }
     }
     // Clean up
     $dir->close();
     return true;
 }
 function perform()
 {
     // get a list with all the global template sets
     $ts = new TemplateSets();
     $globalTemplates = $ts->getGlobalTemplateSets();
     foreach ($globalTemplates as $template) {
         if ($template->getName() == $this->_newTemplateId) {
             $this->_view = new PluginSiteTemplateSetsListView($this->_blogInfo);
             $this->_view->setErrorMessage($this->_locale->tr("error_duplicate_templateset_name"));
             $this->setCommonData();
             return false;
         }
     }
     $ts = new TemplateSetStorage();
     $blogId = $this->_blogInfo->getId();
     $sourceTemplateFolder = $ts->getTemplateFolder($this->_templateId);
     $newTemplateFolder = $ts->getBaseTemplateFolder() . "/" . $this->_newTemplateId;
     if (MyFile::copyDir($sourceTemplateFolder, $newTemplateFolder)) {
         $ts->addTemplate($this->_newTemplateId);
     } else {
         File::deleteDir($newTemplateFolder);
         $this->_view = new PluginSiteTemplateSetsListView($this->_blogInfo);
         $this->_view->setErrorMessage($this->_locale->tr("error_copying_templateset"));
         $this->setCommonData();
         return false;
     }
     // if everything went ok...
     $this->_session->setValue("blogInfo", $this->_blogInfo);
     $this->saveSession();
     $this->_view = new PluginSiteTemplateSetsListView($this->_blogInfo);
     $this->_view->setSuccessMessage($this->_locale->tr("templateeditor_templateset_copyed_ok"));
     $this->setCommonData();
     // clear the cache
     CacheControl::resetBlogCache($this->_blogInfo->getId());
     return true;
 }