Prepare code to clone site an fetch external page

This commit is contained in:
Laurent Destailleur 2017-08-03 01:45:31 +02:00
parent 26730faa4c
commit 81b11100b0
7 changed files with 201 additions and 151 deletions

View File

@ -1000,7 +1000,7 @@ class Commande extends CommonOrder
$this->db->begin();
// get extrafields so they will be clone
// get lines so they will be clone
foreach($this->lines as $line)
$line->fetch_optionals($line->rowid);

View File

@ -5030,57 +5030,6 @@ abstract class CommonObject
}
}
/**
* Load an object from its id and create a new one in database
*
* @param User $user User that creates
* @param int $fromid Id of object to clone
* @return int New id of clone
*/
public function createFromCloneCommon(User $user, $fromid)
{
global $user, $langs;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetchCommon($fromid);
// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
unset($object->import_key);
// Clear fields
$object->ref = "copy_of_".$object->ref;
$object->title = $langs->trans("CopyOf")." ".$object->title;
// ...
// Create clone
$result = $object->createCommon($user);
// Other options
if ($result < 0) {
$error++;
$this->error = $object->error;
$this->errors = $object->errors;
dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
}
// End
if (!$error) {
$this->db->commit();
return $object->id;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Load object in memory from the database

View File

@ -142,6 +142,55 @@ class MyObject extends CommonObject
return $this->createCommon($user, $notrigger);
}
/**
* 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
*/
public function createFromClone(User $user, $fromid)
{
global $hookmanager, $langs;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetchCommon($fromid);
// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
unset($object->import_key);
// Clear fields
$object->ref = "copy_of_".$object->ref;
$object->title = $langs->trans("CopyOf")." ".$object->title;
// ...
// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->createCommon($user);
if ($result < 0) {
$error++;
$this->error = $object->error;
$this->errors = $object->errors;
}
// End
if (!$error) {
$this->db->commit();
return $object->id;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Load object in memory from the database
*
@ -151,7 +200,25 @@ class MyObject extends CommonObject
*/
public function fetch($id, $ref = null)
{
return $this->fetchCommon($id, $ref);
$result = $this->fetchCommon($id, $ref);
if ($result > 0 && ! empty($this->table_element_line)) $this->fetch_lines();
return $result;
}
/**
* Load object lines in memory from the database
*
* @param int $id Id object
* @param string $ref Ref
* @return int <0 if KO, 0 if not found, >0 if OK
*/
public function fetch_lines($id, $ref = null)
{
$this->lines=array();
// Load lines with object MyObjectLine
return count($this->lines)?1:0;
}
/**
@ -368,20 +435,16 @@ class MyObject extends CommonObject
}
/**
* Class MyModuleObjectLine
* Class MyObjectLine. You can also remove this and generate a CRUD class for lines objects.
*/
class MyModuleObjectLine
/*
class MyObjectLine
{
/**
* @var int ID
*/
// @var int ID
public $id;
/**
* @var mixed Sample line property 1
*/
// @var mixed Sample line property 1
public $prop1;
/**
* @var mixed Sample line property 2
*/
// @var mixed Sample line property 2
public $prop2;
}
*/

View File

@ -22,8 +22,7 @@
/**
* \file websites/website.class.php
* \ingroup websites
* \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
* Put some comments here
* \brief File for the CRUD class of website (Create/Read/Update/Delete)
*/
// Put here all includes required by your class file
@ -33,9 +32,6 @@ require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
/**
* Class Website
*
* Put here description of your class
* @see CommonObject
*/
class Website extends CommonObject
{
@ -137,16 +133,16 @@ class Website extends CommonObject
$sql.= 'virtualhost,';
$sql.= 'fk_user_create,';
$sql.= 'date_creation,';
$sql.= 'tmps';
$sql.= 'tms';
$sql .= ') VALUES (';
$sql .= ' '.(! isset($this->entity)?'NULL':$this->entity).',';
$sql .= ' '.(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").',';
$sql .= ' '.(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").',';
$sql .= ' '.(! isset($this->status)?'NULL':$this->status).',';
$sql .= ' '.(! isset($this->fk_default_home)?'NULL':$this->fk_default_home).',';
$sql .= ' '.(! isset($this->virtualhost)?'NULL':$this->virtualhost).',';
$sql .= ' '.(! isset($this->virtualhost)?'NULL':"'".$this->virtualhost)."',";
$sql .= ' '.(! isset($this->fk_user_create)?$user->id:$this->fk_user_create).',';
$sql .= ' '.(! isset($this->date_creation) || dol_strlen($this->date_creation)==0?'NULL':"'".$this->db->idate($this->date_creation)."'");
$sql .= ' '.(! isset($this->date_creation) || dol_strlen($this->date_creation)==0?'NULL':"'".$this->db->idate($this->date_creation)."'").",";
$sql .= ' '.(! isset($this->date_modification) || dol_strlen($this->date_modification)==0?'NULL':"'".$this->db->idate($this->date_creation)."'");
$sql .= ')';
@ -237,7 +233,15 @@ class Website extends CommonObject
}
$this->db->free($resql);
if ($numrows) {
if ($numrows > 0) {
// Lines
$this->fetch_lines();
{
return -3;
}
}
if ($numrows > 0) {
return 1;
} else {
return 0;
@ -250,6 +254,21 @@ class Website extends CommonObject
}
}
/**
* Load object lines in memory from the database
*
* @return int <0 if KO, 0 if not found, >0 if OK
*/
public function fetch_lines()
{
$this->lines=array();
// Load lines with object MyObjectLine
return count($this->lines)?1:0;
}
/**
* Load object in memory from the database
*
@ -463,32 +482,37 @@ class Website extends CommonObject
/**
* Load an object from its id and create a new one in database
*
* @param int $fromid Id of object to clone
*
* @return int New id of clone
* @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
*/
public function createFromClone($fromid)
public function createFromClone($user, $fromid, $newref='')
{
dol_syslog(__METHOD__, LOG_DEBUG);
global $hookmanager, $langs;
$error=0;
global $user;
$error = 0;
$object = new Website($this->db);
dol_syslog(__METHOD__, LOG_DEBUG);
$object = new self($this->db);
$this->db->begin();
// Load source object
$object->fetch($fromid);
// Reset object
$object->id = 0;
// Reset some properties
unset($object->id);
unset($object->fk_user_creat);
unset($object->import_key);
// Clear fields
// ...
$object->ref=$newref;
$object->fk_default_home=0;
$object->virtualhost='';
// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->create($user);
// Other options
if ($result < 0) {
$error ++;
$this->errors = $object->errors;

View File

@ -22,8 +22,7 @@
/**
* \file websites/websitepage.class.php
* \ingroup websites
* \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
* Put some comments here
* \brief File for the CRUD class of websitepage (Create/Read/Update/Delete)
*/
// Put here all includes required by your class file
@ -518,6 +517,7 @@ class WebsitePage extends CommonObject
/**
* Load an object from its id and create a new one in database
*
* @param User $user User making the clone
* @param int $fromid Id of object to clone
* @param string $newref New ref/alias of page
* @param string $newlang New language
@ -525,13 +525,13 @@ class WebsitePage extends CommonObject
* @param int $newwebsite 0=Same web site, 1=New web site
* @return int New id of clone
*/
public function createFromClone($fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0)
public function createFromClone(User $user, $fromid, $newref, $newlang='', $istranslation=0, $newwebsite=0)
{
global $user, $langs;
global $hookmanager, $langs;
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);
$error = 0;
$object = new self($this->db);
$this->db->begin();
@ -551,9 +551,8 @@ class WebsitePage extends CommonObject
if (! empty($newwebsite)) $object->fk_website=$newwebsite;
// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->create($user);
// Other options
if ($result < 0) {
$error++;
$this->error = $object->error;

View File

@ -1,41 +0,0 @@
<?php
/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/externalsite/frametop.php
* \ingroup externalsite
* \brief Top frame to show external web application
*/
require ("../main.inc.php");
//require_once (DOL_DOCUMENT_ROOT."/websites/class/website.class.php");
$langs->load("externalsite");
top_htmlhead("","");
print '<body id="mainbody">' . "\n";
top_menu("","","_top");
print '</body>';
/*
$website = new Website($db);
$listofwebsites = $website->fetchAll();
*/

View File

@ -555,11 +555,23 @@ if ($action == 'updatemeta')
}
// Update page
if (($action == 'updatesource' || $action == 'updatecontent' || $action == 'confirm_createpagefromclone')
if (($action == 'updatesource' || $action == 'updatecontent' || $action == 'confirm_createfromclone' || $action == 'confirm_createpagefromclone')
|| ($action == 'preview' && (GETPOST('refreshsite') || GETPOST('refreshpage') || GETPOST('preview'))))
{
$object->fetch(0, $website);
if ($action == 'confirm_createfromclone')
{
$objectnew = new Website($db);
$result = $objectnew->createFromClone($user, $object->id, GETPOST('siteref','aZ09'), (GETPOST('newlang','aZ09')?GETPOST('newlang','aZ09'):''));
if ($result < 0)
{
$error++;
setEventMessages($objectnew->error, $objectnew->errors, 'errors');
$action='createfromclone';
}
}
if ($action == 'confirm_createpagefromclone')
{
$istranslation=(GETPOST('is_a_translation','aZ09')=='on'?1:0);
@ -576,7 +588,7 @@ if (($action == 'updatesource' || $action == 'updatecontent' || $action == 'conf
if (! $error)
{
$objectpage = new WebsitePage($db);
$result = $objectpage->createFromClone($pageid, GETPOST('pageurl','aZ09'), (GETPOST('newlang','aZ09')?GETPOST('newlang','aZ09'):''), $istranslation, GETPOST('newwebsite','int'));
$result = $objectpage->createFromClone($user, $pageid, GETPOST('pageurl','aZ09'), (GETPOST('newlang','aZ09')?GETPOST('newlang','aZ09'):''), $istranslation, GETPOST('newwebsite','int'));
if ($result < 0)
{
$error++;
@ -825,7 +837,7 @@ if (count($object->records) > 0)
if (! empty($object->virtualhost)) $virtualurl=$object->virtualhost;
}
if ($website && $action == 'preview')
if ($website && ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone'))
{
$disabled='';
if (empty($user->rights->websites->write)) $disabled=' disabled="disabled"';
@ -835,7 +847,7 @@ if (count($object->records) > 0)
//print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("MediaFiles")).'" name="editmedia">';
print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditCss")).'" name="editcss">';
print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditMenu")).'" name="editmenu">';
//print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("CloneSite")).'" name="createfromclone">';
print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("CloneSite")).'" name="createfromclone">';
}
print '</div>';
@ -843,7 +855,7 @@ if (count($object->records) > 0)
// Button for websites
print '<div class="websitetools">';
if ($action == 'preview')
if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone')
{
print '<div class="websiteinputurl" id="websiteinputurl">';
print '<input type="text" id="previewsiteurl" class="minwidth200imp" name="previewsite" placeholder="'.$langs->trans("http://myvirtualhost").'" value="'.$virtualurl.'">';
@ -933,14 +945,29 @@ if (count($object->records) > 0)
print '<input type="submit" class="button" name="refreshpage" value="'.$langs->trans("Load").'"'.($atleastonepage?'':' disabled="disabled"').'>';
if ($action == 'preview' || $action == 'createpagefromclone')
if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone')
{
$disabled='';
if (empty($user->rights->websites->write)) $disabled=' disabled="disabled"';
// Confirmation to clone
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' => '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))
);
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?pageid=' . $pageid, $langs->trans('CloneSite'), '', 'confirm_createfromclone', $formquestion, 0, 1, 200);
print $formconfirm;
}
if ($pageid > 0)
{
// Confirmation to delete
// Confirmation to clone
if ($action == 'createpagefromclone') {
// Create an array for form
$formquestion = array(
@ -971,7 +998,7 @@ if (count($object->records) > 0)
print '<div class="websitetools">';
if ($website && $pageid > 0 && ($action == 'preview' || $action == 'createpagefromclone'))
if ($website && $pageid > 0 && ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone'))
{
$websitepage = new WebSitePage($db);
$websitepage->fetch($pageid);
@ -1017,7 +1044,7 @@ if (count($object->records) > 0)
if ($action == 'preview' || $action == 'createpagefromclone')
if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone')
{
// Adding jquery code to change on the fly url of preview ext
if (! empty($conf->use_javascript_ajax))
@ -1181,14 +1208,42 @@ if ($action == 'editmeta' || $action == 'create')
print '<br>';
dol_fiche_head();
$h = 0;
$head = array();
$head[$h][0] = dol_buildpath('/websites/index.php',1).'?id='.$object->id;
$head[$h][1] = $langs->trans("AddPage");
$head[$h][2] = 'card';
$h++;
//dol_fiche_head($head, 'card', $langs->trans("AddPage"), -1, 'globe');
if ($action == 'create') print_fiche_titre($langs->trans("AddPage"));
print '<!-- Edit Meta -->'."\n";
//print '<div class="fichecenter">';
if ($action == 'create')
{
print ' * '.$langs->trans("CreateByFetchingExternalPage").'<br>';
print '<table class="border" width="100%">';
print '<tr><td class="titlefieldcreate">';
print $langs->trans("URL");
print '</td><td>';
print '<input class="flat minwidth300" type="text" name="externalurl" value="" placeholder="http://externalsite/pagetofetch"> ';
print '<input class="button" type="submit" name="fetchexternalurl" value="'.$langs->trans("FetchAndCreate").'">';
print '</td></tr>';
print '</table>';
print '<br>';
print ' * '.$langs->trans("OrEnterPageInfoManually").'<br>';
}
print '<table class="border" width="100%">';
if ($action != 'create')
{
print '<tr><td>';
print '<tr><td class="titlefield">';
print $langs->trans('WEBSITE_PAGEURL');
print '</td><td>';
print '/public/websites/index.php?website='.urlencode($website).'&pageid='.urlencode($pageid);
@ -1208,25 +1263,25 @@ if ($action == 'editmeta' || $action == 'create')
print '<tr><td class="titlefieldcreate fieldrequired">';
print $langs->trans('WEBSITE_PAGENAME');
print '</td><td>';
print '<input type="text" class="flat" size="96" name="WEBSITE_PAGENAME" value="'.dol_escape_htmltag($pageurl).'">';
print '<input type="text" class="flat maxwidth300" name="WEBSITE_PAGENAME" value="'.dol_escape_htmltag($pageurl).'">';
print '</td></tr>';
print '<tr><td class="fieldrequired">';
print $langs->trans('WEBSITE_TITLE');
print '</td><td>';
print '<input type="text" class="flat" size="96" name="WEBSITE_TITLE" value="'.dol_escape_htmltag($pagetitle).'">';
print '<input type="text" class="flat quatrevingtpercent" name="WEBSITE_TITLE" value="'.dol_escape_htmltag($pagetitle).'">';
print '</td></tr>';
print '<tr><td>';
print $langs->trans('WEBSITE_DESCRIPTION');
print '</td><td>';
print '<input type="text" class="flat" size="96" name="WEBSITE_DESCRIPTION" value="'.dol_escape_htmltag($pagedescription).'">';
print '<input type="text" class="flat quatrevingtpercent" name="WEBSITE_DESCRIPTION" value="'.dol_escape_htmltag($pagedescription).'">';
print '</td></tr>';
print '<tr><td>';
print $langs->trans('WEBSITE_KEYWORDS');
print '</td><td>';
print '<input type="text" class="flat" size="128" name="WEBSITE_KEYWORDS" value="'.dol_escape_htmltag($pagekeywords).'">';
print '<input type="text" class="flat quatrevingtpercent" name="WEBSITE_KEYWORDS" value="'.dol_escape_htmltag($pagekeywords).'">';
print '</td></tr>';
print '<tr><td>';
@ -1236,8 +1291,9 @@ if ($action == 'editmeta' || $action == 'create')
print '</td></tr>';
print '</table>';
//print '</div>';
dol_fiche_end();
//dol_fiche_end();
print '</div>';
@ -1298,7 +1354,7 @@ print "</div>\n</form>\n";
if ($action == 'preview' || $action == 'createpagefromclone')
if ($action == 'preview' || $action == 'createfromclone' || $action == 'createpagefromclone')
{
if ($pageid > 0)
{