Can clone a website

This commit is contained in:
Laurent Destailleur 2017-08-21 00:40:45 +02:00
parent b9511fd4c6
commit a9b033e82f
4 changed files with 154 additions and 36 deletions

View File

@ -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;

View File

@ -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.'<br>';
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 = '<?php'."\n";
$indexcontent.= '// File generated to provide a shortcut to the Home Page - DO NOT MODIFY - It is just an include.'."\n";
$indexcontent.= "include_once './".basename($filetpl)."'\n";
$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();

View File

@ -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();

View File

@ -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 = '<?php'."\n";
$aliascontent.= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
$aliascontent.= 'global $dolibarr_main_data_root;'."\n";
$aliascontent.= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->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 = '<?php'."\n";
$aliascontent.= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
$aliascontent.= 'global $dolibarr_main_data_root;'."\n";
$aliascontent.= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->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 = '<?php'."\n";
$aliascontent.= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
$aliascontent.= 'global $dolibarr_main_data_root;'."\n";
$aliascontent.= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->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
*