/**
  * public static getCloneFromID
  *
  * Clones a Template, changes some attributes
  * and writes it to persistence (MySQL for now)
  *
  * @param anyTemplateID as the ID of Template to be cloned
  * @param String label receive a new label for this Template
  * @param boolean $setPrivate Should the template be set as a private one ?  ALSO determines if the new template should point to the same file
  * @param boolean $dontCopyClientSpaces Should the clientspaces be copied ?
  * @param integer $tplFrom the original template ID to get good rows
  * @return a valid new CMS_pageTemplate
  */
 static function getCloneFromID($templateID = 0, $label = false, $setPrivate = false, $dontCopyClientSpaces = false, $tplFrom = false)
 {
     $ret = false;
     $model = new CMS_pageTemplate($templateID);
     if ($model->getID() > 0) {
         //New blank one
         $tpl = new CMS_pageTemplate();
         //First write a new object to get it's ID
         $tpl->writeToPersistence();
         //Setting label
         $label = $label ? $label : $model->getLabel();
         $tpl->setLabel($label);
         //Copying template definition file (if not private template)
         if ($setPrivate) {
             $filename = $model->getDefinitionFile();
         } else {
             $filename = "pt" . $tpl->getID() . "_" . SensitiveIO::sanitizeAsciiString($tpl->getLabel()) . ".xml";
         }
         if ($setPrivate || CMS_file::copyTo(PATH_TEMPLATES_FS . "/" . $model->getDefinitionFile(), PATH_TEMPLATES_FS . "/" . $filename)) {
             $tpl->setDefinitionFile($filename);
             //Copying groupsStack from database
             foreach ($model->getGroups() as $grp) {
                 $tpl->addGroup($grp);
             }
             //Copying image file from model to a new one
             if ($setPrivate) {
                 $tpl->setImage($model->getImage());
             } else {
                 if ($model->getImage()) {
                     $ext = io::substr($model->getImage(), strrpos($model->getImage(), "."));
                     $imagefilename = "pt" . $tpl->getID() . "_" . SensitiveIO::sanitizeAsciiString($tpl->getLabel()) . $ext;
                     if (CMS_file::copyTo(PATH_TEMPLATES_IMAGES_FS . "/" . $model->getImage(), PATH_TEMPLATES_IMAGES_FS . "/" . $imagefilename)) {
                         $tpl->setImage($imagefilename);
                     }
                 } else {
                     $tpl->setImage();
                 }
             }
             //set private if asked to.
             if ($setPrivate) {
                 $tpl->setPrivate(true);
             }
             //copy description
             $tpl->setDescription($model->getDescription());
             //websites denied
             $websitesDenied = $model->getWebsitesDenied();
             foreach ($websitesDenied as $websiteId) {
                 if (CMS_websitesCatalog::exists($websiteId)) {
                     //to check if website still exists
                     $tpl->denyWebsite($websiteId);
                 }
             }
             //Copy printing definition
             $tpl->setPrintingClientSpaces($model->getPrintingClientSpaces());
             //Partial update for groups and image
             $tpl->writeToPersistence();
             //Copying template clientspaces rows definitions
             if (!$dontCopyClientSpaces) {
                 $suffixes = array('archived', 'deleted', 'edited', 'edition', 'public');
                 foreach ($suffixes as $suffix) {
                     if ($tplFrom) {
                         $sql = "\n\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t*\n\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $tplFrom . "'\n\t\t\t\t\t\t\t";
                     } else {
                         $sql = "\n\t\t\t\t\t\t\t\tselect\n\t\t\t\t\t\t\t\t\t*\n\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $model->getID() . "'\n\t\t\t\t\t\t\t";
                     }
                     $q = new CMS_query($sql);
                     while ($arr = $q->getArray()) {
                         $sql1 = "\n\t\t\t\t\t\t\t\tinsert into\n\t\t\t\t\t\t\t\t\t`mod_standard_clientSpaces_" . $suffix . "`\n\t\t\t\t\t\t\t\tset\n\t\t\t\t\t\t\t\t\t`template_cs`='" . $tpl->getID() . "',\n\t\t\t\t\t\t\t\t\t`tagID_cs`='" . SensitiveIO::sanitizeSQLString($arr["tagID_cs"]) . "',\n\t\t\t\t\t\t\t\t\t`rowsDefinition_cs`='" . SensitiveIO::sanitizeSQLString($arr["rowsDefinition_cs"]) . "',\n\t\t\t\t\t\t\t\t\t`type_cs`='" . $arr["type_cs"] . "',\n\t\t\t\t\t\t\t\t\t`order_cs`='" . $arr["order_cs"] . "'\n\t\t\t\t\t\t\t";
                         $q1 = new CMS_query($sql1);
                         unset($q1);
                     }
                     unset($q);
                 }
             }
             //CMS_Template to return
             $ret = $tpl;
         }
         unset($model);
     }
     if ($tpl) {
         //Clean if any error when out
         if (!$ret) {
             $tpl->destroy();
         }
         unset($tpl);
     }
     return $ret;
 }