From a9b033e82fef9ec39fc5bbc022f02d39e3211439 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 21 Aug 2017 00:40:45 +0200 Subject: [PATCH] Can clone a website --- .../template/class/myobject.class.php | 8 +- htdocs/websites/class/website.class.php | 107 +++++++++++++++++- htdocs/websites/class/websitepage.class.php | 4 +- htdocs/websites/index.php | 71 +++++++----- 4 files changed, 154 insertions(+), 36 deletions(-) diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index fc8ecd1adf7..d8c35c64a81 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -149,9 +149,9 @@ class MyObject extends CommonObject /** * Clone and object into another one * - * @param User $user User that creates - * @param int $fromid Id of object to clone - * @return int New id of clone + * @param User $user User that creates + * @param int $fromid Id of object to clone + * @return mixed New object created, <0 if KO */ public function createFromClone(User $user, $fromid) { @@ -188,7 +188,7 @@ class MyObject extends CommonObject // End if (!$error) { $this->db->commit(); - return $object->id; + return $object; } else { $this->db->rollback(); return -1; diff --git a/htdocs/websites/class/website.class.php b/htdocs/websites/class/website.class.php index fd808139923..28e54fad1af 100644 --- a/htdocs/websites/class/website.class.php +++ b/htdocs/websites/class/website.class.php @@ -485,21 +485,39 @@ class Website extends CommonObject * @param User $user User making the clone * @param int $fromid Id of object to clone * @param string $newref New ref - * @return int New id of clone + * @return mixed New object created, <0 if KO */ - public function createFromClone($user, $fromid, $newref='') + public function createFromClone($user, $fromid, $newref) { global $hookmanager, $langs; - $error=0; + global $dolibarr_main_data_root; + + $error=0; dol_syslog(__METHOD__, LOG_DEBUG); $object = new self($this->db); + // Check no site with ref exists + if ($object->fetch(0, $newref) > 0) + { + $this->error='NewRefIsAlreadyUsed'; + return -1; + } + $this->db->begin(); // Load source object $object->fetch($fromid); + + $oldidforhome=$object->fk_default_home; + + $pathofwebsiteold=$dolibarr_main_data_root.'/websites/'.$object->ref; + $pathofwebsitenew=$dolibarr_main_data_root.'/websites/'.$newref; + dol_delete_dir_recursive($pathofwebsitenew); + + $fileindex=$pathofwebsitenew.'/index.php'; + // Reset some properties unset($object->id); unset($object->fk_user_creat); @@ -519,11 +537,92 @@ class Website extends CommonObject dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); } + if (! $error) + { + dolCopyDir($pathofwebsiteold, $pathofwebsitenew, $conf->global->MAIN_UMASK, 0); + + // Check symlink to medias and restore it if ko + $pathtomedias=DOL_DATA_ROOT.'/medias'; + $pathtomediasinwebsite=$pathofwebsitenew.'/medias'; + if (! is_link(dol_osencode($pathtomediasinwebsite))) + { + dol_syslog("Create symlink for ".$pathtomedias." into name ".$pathtomediasinwebsite); + dol_mkdir(dirname($pathtomediasinwebsite)); // To be sure dir for website exists + $result = symlink($pathtomedias, $pathtomediasinwebsite); + } + + $newidforhome=0; + + // Duplicate pages + $objectpages = new WebsitePage($this->db); + $listofpages = $objectpages->fetchAll($fromid); + foreach($listofpages as $pageid => $objectpageold) + { + // Delete old file + $filetplold=$pathofwebsitenew.'/page'.$pageid.'.tpl.php'; + dol_syslog("We regenerate alias page new name=".$filealias.", old name=".$fileoldalias); + dol_delete_file($filetplold); + + // Create new file + $objectpagenew = $objectpageold->createFromClone($user, $pageid, $objectpageold->pageurl, '', 0, $object->id); + //print $pageid.' = '.$objectpageold->pageurl.' -> '.$objectpagenew->id.' = '.$objectpagenew->pageurl.'
'; + if (is_object($objectpagenew) && $objectpagenew->pageurl) + { + $filealias=$pathofwebsitenew.'/'.$objectpagenew->pageurl.'.php'; + $filetplnew=$pathofwebsitenew.'/page'.$objectpagenew->id.'.tpl.php'; + + // Save page alias + $result=dolSavePageAlias($filealias, $object, $objectpagenew); + if (! $result) setEventMessages('Failed to write file '.$filealias, null, 'errors'); + + $result=dolSavePageContent($filetplnew, $object, $objectpagenew); + if (! $result) setEventMessages('Failed to write file '.$filetplnew, null, 'errors'); + + if ($pageid == $oldidforhome) + { + $newidforhome = $objectpagenew->id; + } + } + else + { + setEventMessages($objectpageold->error, $objectpageold->errors, 'errors'); + $error++; + } + } + } + + if (! $error) + { + // Restore id of home page + $object->fk_default_home = $newidforhome; + $res = $object->update($user); + if (! $res > 0) + { + $error++; + setEventMessages($objectpage->error, $objectpage->errors, 'errors'); + } + + if (! $error) + { + dol_delete_file($fileindex); + + $filetpl=$pathofwebsitenew.'/page'.$newidforhome.'.tpl.php'; + + $indexcontent = ''."\n"; + $result = file_put_contents($fileindex, $indexcontent); + if (! empty($conf->global->MAIN_UMASK)) + @chmod($fileindex, octdec($conf->global->MAIN_UMASK)); + } + } + // End if (!$error) { $this->db->commit(); - return $object->id; + return $object; } else { $this->db->rollback(); diff --git a/htdocs/websites/class/websitepage.class.php b/htdocs/websites/class/websitepage.class.php index a32f45e70fe..756d927aed9 100644 --- a/htdocs/websites/class/websitepage.class.php +++ b/htdocs/websites/class/websitepage.class.php @@ -309,7 +309,7 @@ class WebsitePage extends CommonObject * @param string $newlang New language * @param int $istranslation 1=New page is a translation of the cloned page. * @param int $newwebsite 0=Same web site, 1=New web site - * @return int New id of clone + * @return mixed New object created, <0 if KO */ public function createFromClone(User $user, $fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0) { @@ -350,7 +350,7 @@ class WebsitePage extends CommonObject if (!$error) { $this->db->commit(); - return $object->id; + return $object; } else { $this->db->rollback(); diff --git a/htdocs/websites/index.php b/htdocs/websites/index.php index 679d7f57d3a..00bc61d07c5 100644 --- a/htdocs/websites/index.php +++ b/htdocs/websites/index.php @@ -466,7 +466,7 @@ if ($action == 'setashome') if (! $res > 0) { $error++; - setEventMessages($objectpage->error, $objectpage->errors, 'errors'); + setEventMessages($object->error, $object->errors, 'errors'); } if (! $error) @@ -572,21 +572,12 @@ if ($action == 'updatemeta') dol_delete_file($fileoldalias); } - $aliascontent = 'id.'.tpl.php\'; '; - $aliascontent.= 'else require $dolibarr_main_data_root.\'/websites/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n"; - $aliascontent.= '?>'."\n"; - $result = file_put_contents($filealias, $aliascontent); - if (! empty($conf->global->MAIN_UMASK)) - @chmod($filealias, octdec($conf->global->MAIN_UMASK)); - + // Save page alias + $result=dolSavePageAlias($filealias, $object, $objectpage); if (! $result) setEventMessages('Failed to write file '.$filealias, null, 'errors'); // Save page of content $result=dolSavePageContent($filetpl, $object, $objectpage); - if ($result) { setEventMessages($langs->trans("Saved"), null, 'mesgs'); @@ -623,7 +614,13 @@ if (($action == 'updatesource' || $action == 'updatecontent' || $action == 'conf { $error++; setEventMessages($objectnew->error, $objectnew->errors, 'errors'); - $action='createfromclone'; + $action='preview'; + } + else + { + $object = $objectnew; + $id = $object->id; + $pageid = $object->fk_default_home; } } @@ -745,22 +742,12 @@ if (($action == 'updatesource' || $action == 'updatecontent' || $action == 'conf dol_delete_file($fileoldalias); } - $aliascontent = 'id.'.tpl.php\';'; - $aliascontent.= 'else require $dolibarr_main_data_root.\'/websites/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n"; - $aliascontent.= '?>'."\n"; - $result = file_put_contents($filealias, $aliascontent); - if (! empty($conf->global->MAIN_UMASK)) - @chmod($filealias, octdec($conf->global->MAIN_UMASK)); - + // Save page alias + $result=dolSavePageAlias($filealias, $object, $objectpage); if (! $result) setEventMessages('Failed to write file '.$filealias, null, 'errors'); - // Save page content $result=dolSavePageContent($filetpl, $object, $objectpage); - if ($result) { setEventMessages($langs->trans("Saved"), null, 'mesgs'); @@ -1012,7 +999,7 @@ if (count($object->records) > 0) if ($action == 'createfromclone') { // Create an array for form $formquestion = array( - array('type' => 'text', 'name' => 'siteref', 'label'=> $langs->trans("Website") ,'value'=> 'copy_of_'.$objectpage->pageurl), + array('type' => 'text', 'name' => 'siteref', 'label'=> $langs->trans("Website") ,'value'=> 'copy_of_'.$object->ref), //array('type' => 'checkbox', 'name' => 'is_a_translation', 'label' => $langs->trans("SiteIsANewTranslation"), 'value' => 0), //array('type' => 'other','name' => 'newlang','label' => $langs->trans("Language"), 'value' => $formadmin->select_language(GETPOST('newlang', 'az09')?GETPOST('newlang', 'az09'):$langs->defaultlang, 'newlang', 0, null, '', 0, 0, 'minwidth200')), //array('type' => 'other','name' => 'newwebsite','label' => $langs->trans("Website"), 'value' => $formwebsite->selectWebsite($object->id, 'newwebsite', 0)) @@ -1523,6 +1510,38 @@ function dolWebsiteReplacementOfLinks($website, $content) return $content; } + + + +/** + * Save content of a page on disk + * + * @param string $filealias Full path of filename to generate + * @param Website $object Object website + * @param WebsitePage $objectpage Object websitepage + * @return boolean True if OK + */ +function dolSavePageAlias($filealias, $object, $objectpage) +{ + global $conf; + + // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header) + dol_syslog("We regenerate the alias page filealias=".$filealias); + + $aliascontent = 'id.'.tpl.php\'; '; + $aliascontent.= 'else require $dolibarr_main_data_root.\'/websites/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n"; + $aliascontent.= '?>'."\n"; + $result = file_put_contents($filealias, $aliascontent); + if (! empty($conf->global->MAIN_UMASK)) + @chmod($filealias, octdec($conf->global->MAIN_UMASK)); + + return ($result?true:false); +} + + /** * Save content of a page on disk *