2016-04-12 19:17:50 +02:00
< ? php
/* Copyright ( C ) 2007 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
* Copyright ( C ) 2015 Florian Henry < florian . henry @ open - concept . pro >
* Copyright ( C ) 2015 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
* Copyright ( C ) --- Put here your own copyright and developer email ---
*
* 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 />.
*/
/**
2017-10-20 21:39:19 +02:00
* \file website / website . class . php
* \ingroup website
2017-08-03 01:45:31 +02:00
* \brief File for the CRUD class of website ( Create / Read / Update / Delete )
2016-04-12 19:17:50 +02:00
*/
// Put here all includes required by your class file
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
/**
* Class Website
*/
class Website extends CommonObject
{
/**
* @ var string Id to identify managed objects
*/
public $element = 'website' ;
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'website' ;
2017-10-26 02:55:43 +02:00
/**
2018-07-05 02:31:49 +02:00
* @ var array Does website support multicompany module ? 0 = No test on entity , 1 = Test with field entity , 2 = Test with link by societe
2017-11-03 15:56:16 +01:00
*/
public $ismultientitymanaged = 1 ;
/**
* @ var string String with name of icon for website . Must be the part after the 'object_' into object_myobject . png
2017-10-26 02:55:43 +02:00
*/
public $picto = 'globe' ;
2016-04-12 19:17:50 +02:00
/**
2016-05-06 21:28:06 +02:00
* @ var int
2016-04-12 19:17:50 +02:00
*/
public $entity ;
2016-05-06 21:28:06 +02:00
/**
* @ var string
*/
2016-04-19 11:02:55 +02:00
public $ref ;
2016-05-06 21:28:06 +02:00
/**
* @ var string
*/
2016-04-12 19:17:50 +02:00
public $description ;
2016-05-06 21:28:06 +02:00
/**
* @ var int
*/
2016-04-12 19:17:50 +02:00
public $status ;
2016-05-06 21:28:06 +02:00
/**
* @ var mixed
*/
public $date_creation ;
/**
* @ var mixed
*/
2016-04-12 19:17:50 +02:00
public $tms = '' ;
2016-05-06 21:28:06 +02:00
/**
* @ var integer
*/
public $fk_default_home ;
2016-07-14 15:54:46 +02:00
/**
* @ var string
*/
public $virtualhost ;
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
/**
* Constructor
*
* @ param DoliDb $db Database handler
*/
public function __construct ( DoliDB $db )
{
$this -> db = $db ;
return 1 ;
}
/**
* Create object into database
*
* @ param User $user User that creates
* @ param bool $notrigger false = launch triggers after , true = disable triggers
*
* @ return int < 0 if KO , Id of created object if OK
*/
public function create ( User $user , $notrigger = false )
{
2017-12-19 11:40:29 +01:00
global $conf ;
2016-04-12 19:17:50 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
2017-07-19 16:38:00 +02:00
$now = dol_now ();
2016-04-12 19:17:50 +02:00
// Clean parameters
if ( isset ( $this -> entity )) {
$this -> entity = trim ( $this -> entity );
}
2016-04-19 11:02:55 +02:00
if ( isset ( $this -> ref )) {
$this -> ref = trim ( $this -> ref );
2016-04-12 19:17:50 +02:00
}
if ( isset ( $this -> description )) {
$this -> description = trim ( $this -> description );
}
if ( isset ( $this -> status )) {
$this -> status = trim ( $this -> status );
}
2017-07-19 16:38:00 +02:00
if ( empty ( $this -> date_creation )) $this -> date_creation = $now ;
if ( empty ( $this -> date_modification )) $this -> date_modification = $now ;
2016-04-12 19:17:50 +02:00
// Check parameters
2017-12-19 11:40:29 +01:00
if ( empty ( $this -> entity )) { $this -> entity = $conf -> entity ; }
2016-04-12 19:17:50 +02:00
// Insert request
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element . '(' ;
$sql .= 'entity,' ;
2016-04-19 11:02:55 +02:00
$sql .= 'ref,' ;
2016-04-12 19:17:50 +02:00
$sql .= 'description,' ;
$sql .= 'status,' ;
2016-07-14 15:54:46 +02:00
$sql .= 'fk_default_home,' ;
$sql .= 'virtualhost,' ;
2017-07-20 18:23:42 +02:00
$sql .= 'fk_user_create,' ;
2017-07-20 18:31:58 +02:00
$sql .= 'date_creation,' ;
2017-08-03 01:45:31 +02:00
$sql .= 'tms' ;
2016-04-12 19:17:50 +02:00
$sql .= ') VALUES (' ;
2017-10-26 13:09:11 +02:00
$sql .= ' ' . (( empty ( $this -> entity ) && $this -> entity != '0' ) ? 'NULL' : $this -> entity ) . ',' ;
2016-04-19 11:02:55 +02:00
$sql .= ' ' . ( ! isset ( $this -> ref ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> ref ) . " ' " ) . ',' ;
2016-04-12 19:17:50 +02:00
$sql .= ' ' . ( ! isset ( $this -> description ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> description ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> status ) ? 'NULL' : $this -> status ) . ',' ;
2016-05-02 15:51:18 +02:00
$sql .= ' ' . ( ! isset ( $this -> fk_default_home ) ? 'NULL' : $this -> fk_default_home ) . ',' ;
2017-09-15 15:41:07 +02:00
$sql .= ' ' . ( ! isset ( $this -> virtualhost ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> virtualhost ) . " ' " ) . " , " ;
2017-04-28 13:10:08 +02:00
$sql .= ' ' . ( ! isset ( $this -> fk_user_create ) ? $user -> id : $this -> fk_user_create ) . ',' ;
2017-08-03 01:45:31 +02:00
$sql .= ' ' . ( ! isset ( $this -> date_creation ) || dol_strlen ( $this -> date_creation ) == 0 ? 'NULL' : " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " ) . " , " ;
2017-07-19 16:38:00 +02:00
$sql .= ' ' . ( ! isset ( $this -> date_modification ) || dol_strlen ( $this -> date_modification ) == 0 ? 'NULL' : " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " );
2016-04-12 19:17:50 +02:00
$sql .= ')' ;
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error ) {
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
if ( ! $notrigger ) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action to call a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_CREATE',$user);
//if ($result < 0) $error++;
//// End call triggers
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return $this -> id ;
}
}
/**
* Load object in memory from the database
*
2017-10-07 02:29:27 +02:00
* @ param int $id Id object
* @ param string $ref Ref
* @ return int < 0 if KO , 0 if not found , > 0 if OK
2016-04-12 19:17:50 +02:00
*/
public function fetch ( $id , $ref = null )
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$sql = 'SELECT' ;
$sql .= ' t.rowid,' ;
$sql .= " t.entity, " ;
2016-04-19 11:02:55 +02:00
$sql .= " t.ref, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.description, " ;
$sql .= " t.status, " ;
2016-05-02 15:51:18 +02:00
$sql .= " t.fk_default_home, " ;
2016-07-14 15:54:46 +02:00
$sql .= " t.virtualhost, " ;
2017-04-28 13:10:08 +02:00
$sql .= " t.fk_user_create, " ;
$sql .= " t.fk_user_modif, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.date_creation, " ;
2017-07-20 18:36:18 +02:00
$sql .= " t.tms as date_modification " ;
2016-04-12 19:17:50 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as t' ;
2018-01-07 18:28:47 +01:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'website' ) . ')' ;
2016-04-12 19:17:50 +02:00
if ( null !== $ref ) {
2018-01-07 18:28:47 +01:00
$sql .= " AND t.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2016-04-12 19:17:50 +02:00
} else {
2018-01-07 18:28:47 +01:00
$sql .= ' AND t.rowid = ' . $id ;
2016-04-12 19:17:50 +02:00
}
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$numrows = $this -> db -> num_rows ( $resql );
if ( $numrows ) {
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
$this -> entity = $obj -> entity ;
2016-04-19 11:02:55 +02:00
$this -> ref = $obj -> ref ;
2016-04-12 19:17:50 +02:00
$this -> description = $obj -> description ;
$this -> status = $obj -> status ;
2016-05-02 15:51:18 +02:00
$this -> fk_default_home = $obj -> fk_default_home ;
2016-07-14 15:54:46 +02:00
$this -> virtualhost = $obj -> virtualhost ;
2017-04-28 13:10:08 +02:00
$this -> fk_user_create = $obj -> fk_user_create ;
$this -> fk_user_modif = $obj -> fk_user_modif ;
2016-04-12 19:17:50 +02:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
2017-07-20 18:36:18 +02:00
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2016-04-12 19:17:50 +02:00
}
$this -> db -> free ( $resql );
2017-08-03 01:45:31 +02:00
if ( $numrows > 0 ) {
// Lines
2017-08-03 03:58:42 +02:00
$this -> fetchLines ();
2017-08-03 01:45:31 +02:00
}
if ( $numrows > 0 ) {
2016-04-12 19:17:50 +02:00
return 1 ;
} else {
return 0 ;
}
} else {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
return - 1 ;
}
}
2017-08-03 01:45:31 +02:00
/**
* Load object lines in memory from the database
*
* @ return int < 0 if KO , 0 if not found , > 0 if OK
*/
2017-08-03 03:58:42 +02:00
public function fetchLines ()
2017-08-03 01:45:31 +02:00
{
$this -> lines = array ();
// Load lines with object MyObjectLine
return count ( $this -> lines ) ? 1 : 0 ;
}
2016-04-12 19:17:50 +02:00
/**
* Load object in memory from the database
*
* @ param string $sortorder Sort Order
* @ param string $sortfield Sort field
* @ param int $limit offset limit
* @ param int $offset offset limit
* @ param array $filter filter array
* @ param string $filtermode filter mode ( AND or OR )
*
* @ return int < 0 if KO , > 0 if OK
*/
public function fetchAll ( $sortorder = '' , $sortfield = '' , $limit = 0 , $offset = 0 , array $filter = array (), $filtermode = 'AND' )
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$sql = 'SELECT' ;
$sql .= ' t.rowid,' ;
$sql .= " t.entity, " ;
2016-04-19 11:02:55 +02:00
$sql .= " t.ref, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.description, " ;
$sql .= " t.status, " ;
2016-07-14 15:54:46 +02:00
$sql .= " t.fk_default_home, " ;
$sql .= " t.virtualhost, " ;
2017-04-28 13:10:08 +02:00
$sql .= " t.fk_user_create, " ;
$sql .= " t.fk_user_modif, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.date_creation, " ;
2017-07-20 18:36:18 +02:00
$sql .= " t.tms as date_modification " ;
2016-04-12 19:17:50 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as t' ;
2018-01-07 18:28:47 +01:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'website' ) . ')' ;
2016-04-12 19:17:50 +02:00
// Manage filter
$sqlwhere = array ();
if ( count ( $filter ) > 0 ) {
foreach ( $filter as $key => $value ) {
$sqlwhere [] = $key . ' LIKE \'%' . $this -> db -> escape ( $value ) . '%\'' ;
}
}
if ( count ( $sqlwhere ) > 0 ) {
2018-01-07 18:28:47 +01:00
$sql .= ' AND ' . implode ( ' ' . $filtermode . ' ' , $sqlwhere );
2016-04-12 19:17:50 +02:00
}
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
if ( ! empty ( $sortfield )) {
$sql .= $this -> db -> order ( $sortfield , $sortorder );
}
if ( ! empty ( $limit )) {
2017-01-03 11:22:03 +01:00
$sql .= ' ' . $this -> db -> plimit ( $limit , $offset );
2016-04-12 19:17:50 +02:00
}
2016-04-19 11:02:55 +02:00
$this -> records = array ();
2016-04-12 19:17:50 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2017-01-03 11:22:03 +01:00
$line = new self ( $this -> db );
2016-04-12 19:17:50 +02:00
$line -> id = $obj -> rowid ;
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
$line -> entity = $obj -> entity ;
2016-04-19 11:02:55 +02:00
$line -> ref = $obj -> ref ;
2016-04-12 19:17:50 +02:00
$line -> description = $obj -> description ;
$line -> status = $obj -> status ;
2016-05-02 15:51:18 +02:00
$line -> fk_default_home = $obj -> fk_default_home ;
2016-07-14 15:54:46 +02:00
$line -> virtualhost = $obj -> virtualhost ;
2017-04-28 13:10:08 +02:00
$this -> fk_user_create = $obj -> fk_user_create ;
$this -> fk_user_modif = $obj -> fk_user_modif ;
2016-04-12 19:17:50 +02:00
$line -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
2017-07-20 18:36:18 +02:00
$line -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2016-04-12 19:17:50 +02:00
2016-04-19 11:02:55 +02:00
$this -> records [ $line -> id ] = $line ;
2016-04-12 19:17:50 +02:00
}
$this -> db -> free ( $resql );
return $num ;
} else {
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
return - 1 ;
}
}
/**
* Update object into database
*
* @ param User $user User that modifies
* @ param bool $notrigger false = launch triggers after , true = disable triggers
*
* @ return int < 0 if KO , > 0 if OK
*/
public function update ( User $user , $notrigger = false )
{
$error = 0 ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
// Clean parameters
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
if ( isset ( $this -> entity )) {
$this -> entity = trim ( $this -> entity );
}
2016-04-19 11:02:55 +02:00
if ( isset ( $this -> ref )) {
$this -> ref = trim ( $this -> ref );
2016-04-12 19:17:50 +02:00
}
if ( isset ( $this -> description )) {
$this -> description = trim ( $this -> description );
}
if ( isset ( $this -> status )) {
$this -> status = trim ( $this -> status );
}
// Check parameters
// Put here code to add a control on parameters values
// Update request
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
$sql .= ' entity = ' . ( isset ( $this -> entity ) ? $this -> entity : " null " ) . ',' ;
2016-04-19 11:02:55 +02:00
$sql .= ' ref = ' . ( isset ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : " null " ) . ',' ;
2016-04-12 19:17:50 +02:00
$sql .= ' description = ' . ( isset ( $this -> description ) ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " ) . ',' ;
$sql .= ' status = ' . ( isset ( $this -> status ) ? $this -> status : " null " ) . ',' ;
2016-05-02 15:51:18 +02:00
$sql .= ' fk_default_home = ' . (( $this -> fk_default_home > 0 ) ? $this -> fk_default_home : " null " ) . ',' ;
2016-08-08 18:25:37 +02:00
$sql .= ' virtualhost = ' . (( $this -> virtualhost != '' ) ? " ' " . $this -> db -> escape ( $this -> virtualhost ) . " ' " : " null " ) . ',' ;
2017-04-28 13:10:08 +02:00
$sql .= ' fk_user_modif = ' . ( ! isset ( $this -> fk_user_modif ) ? $user -> id : $this -> fk_user_modif ) . ',' ;
2017-07-20 18:36:18 +02:00
$sql .= ' date_creation = ' . ( ! isset ( $this -> date_creation ) || dol_strlen ( $this -> date_creation ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " : 'null' );
$sql .= ', tms = ' . ( dol_strlen ( $this -> date_modification ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_modification ) . " ' " : " ' " . $this -> db -> idate ( dol_now ()) . " ' " );
2016-04-12 19:17:50 +02:00
$sql .= ' WHERE rowid=' . $this -> id ;
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
}
if ( ! $error && ! $notrigger ) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
* Delete object in database
*
* @ param User $user User that deletes
* @ param bool $notrigger false = launch triggers after , true = disable triggers
*
* @ return int < 0 if KO , > 0 if OK
*/
public function delete ( User $user , $notrigger = false )
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
if ( ! $error ) {
if ( ! $notrigger ) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
//// Call triggers
//$result=$this->call_trigger('MYOBJECT_DELETE',$user);
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
}
}
if ( ! $error ) {
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
$sql .= ' WHERE rowid=' . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
}
}
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
return - 1 * $error ;
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
2017-09-01 11:10:03 +02:00
* Load an object from its id and create a new one in database .
* This copy website directories , regenerate all the pages + alias pages and recreate the medias link .
2016-04-12 19:17:50 +02:00
*
2017-08-03 01:45:31 +02:00
* @ param User $user User making the clone
* @ param int $fromid Id of object to clone
* @ param string $newref New ref
2017-08-21 00:40:45 +02:00
* @ return mixed New object created , < 0 if KO
2016-04-12 19:17:50 +02:00
*/
2017-08-21 00:40:45 +02:00
public function createFromClone ( $user , $fromid , $newref )
2016-04-12 19:17:50 +02:00
{
2017-08-03 01:45:31 +02:00
global $hookmanager , $langs ;
2017-08-21 00:40:45 +02:00
global $dolibarr_main_data_root ;
$error = 0 ;
2016-04-12 19:17:50 +02:00
2017-08-03 01:45:31 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$object = new self ( $this -> db );
2016-04-12 19:17:50 +02:00
2017-08-21 00:40:45 +02:00
// Check no site with ref exists
if ( $object -> fetch ( 0 , $newref ) > 0 )
{
$this -> error = 'NewRefIsAlreadyUsed' ;
return - 1 ;
}
2016-04-12 19:17:50 +02:00
$this -> db -> begin ();
// Load source object
$object -> fetch ( $fromid );
2017-08-21 00:40:45 +02:00
$oldidforhome = $object -> fk_default_home ;
2017-10-20 21:39:19 +02:00
$pathofwebsiteold = $dolibarr_main_data_root . '/website/' . $object -> ref ;
$pathofwebsitenew = $dolibarr_main_data_root . '/website/' . $newref ;
2017-08-21 00:40:45 +02:00
dol_delete_dir_recursive ( $pathofwebsitenew );
$fileindex = $pathofwebsitenew . '/index.php' ;
2017-08-03 01:45:31 +02:00
// Reset some properties
unset ( $object -> id );
unset ( $object -> fk_user_creat );
unset ( $object -> import_key );
2016-04-12 19:17:50 +02:00
// Clear fields
2017-08-03 01:45:31 +02:00
$object -> ref = $newref ;
$object -> fk_default_home = 0 ;
$object -> virtualhost = '' ;
2016-04-12 19:17:50 +02:00
// Create clone
2017-08-03 01:45:31 +02:00
$object -> context [ 'createfromclone' ] = 'createfromclone' ;
2016-04-12 19:17:50 +02:00
$result = $object -> create ( $user );
if ( $result < 0 ) {
$error ++ ;
$this -> errors = $object -> errors ;
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
}
2017-08-21 00:40:45 +02:00
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 )
{
$filetpl = $pathofwebsitenew . '/page' . $newidforhome . '.tpl.php' ;
2018-02-28 14:36:22 +01:00
// Generate the index.php page to be the home page
//-------------------------------------------------
2018-03-04 11:09:53 +01:00
$result = dolSaveIndexPage ( $pathofwebsitenew , $fileindex , $filetpl );
2017-08-21 00:40:45 +02:00
}
}
2016-04-12 19:17:50 +02:00
// End
if ( ! $error ) {
$this -> db -> commit ();
2017-08-21 00:40:45 +02:00
return $object ;
2016-04-12 19:17:50 +02:00
} else {
$this -> db -> rollback ();
return - 1 ;
}
}
/**
* Return a link to the user card ( with optionaly the picto )
* Use this -> id , this -> lastname , this -> firstname
*
* @ param int $withpicto Include picto in link ( 0 = No picto , 1 = Include picto into link , 2 = Only picto )
* @ param string $option On what the link point to
* @ param integer $notooltip 1 = Disable tooltip
* @ param int $maxlen Max length of visible user name
* @ param string $morecss Add more css on link
* @ return string String with URL
*/
function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $maxlen = 24 , $morecss = '' )
{
global $langs , $conf , $db ;
global $dolibarr_main_authentication , $dolibarr_main_demo ;
global $menumanager ;
$result = '' ;
$companylink = '' ;
2017-10-26 02:55:43 +02:00
$label = '<u>' . $langs -> trans ( " WebSite " ) . '</u>' ;
2016-04-12 19:17:50 +02:00
$label .= '<div width="100%">' ;
2017-10-26 02:55:43 +02:00
$label .= '<b>' . $langs -> trans ( 'Nom' ) . ':</b> ' . $this -> ref ;
2016-04-12 19:17:50 +02:00
2017-10-26 02:55:43 +02:00
$linkstart = '<a href="' . DOL_URL_ROOT . '/website/card.php?id=' . $this -> id . '"' ;
$linkstart .= ( $notooltip ? '' : ' title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip' . ( $morecss ? ' ' . $morecss : '' ) . '"' );
$linkstart .= '>' ;
2016-04-12 19:17:50 +02:00
$linkend = '</a>' ;
2017-10-26 02:55:43 +02:00
$linkstart = $linkend = '' ;
2016-04-12 19:17:50 +02:00
if ( $withpicto )
{
2017-10-26 02:55:43 +02:00
$result .= ( $linkstart . img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? '' : 'class="classfortooltip"' )) . $linkend );
2016-04-12 19:17:50 +02:00
if ( $withpicto != 2 ) $result .= ' ' ;
}
2017-10-26 02:55:43 +02:00
$result .= $linkstart . $this -> ref . $linkend ;
2016-04-12 19:17:50 +02:00
return $result ;
}
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
/**
* Retourne le libelle du status d ' un user ( actif , inactif )
*
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Label of status
*/
function getLibStatut ( $mode = 0 )
{
return $this -> LibStatut ( $this -> status , $mode );
}
/**
* Renvoi le libelle d ' un status donne
*
* @ param int $status Id status
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Label of status
*/
function LibStatut ( $status , $mode = 0 )
{
global $langs ;
if ( $mode == 0 )
{
$prefix = '' ;
if ( $status == 1 ) return $langs -> trans ( 'Enabled' );
if ( $status == 0 ) return $langs -> trans ( 'Disabled' );
}
if ( $mode == 1 )
{
if ( $status == 1 ) return $langs -> trans ( 'Enabled' );
if ( $status == 0 ) return $langs -> trans ( 'Disabled' );
}
if ( $mode == 2 )
{
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $langs -> trans ( 'Enabled' );
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Disabled' );
}
if ( $mode == 3 )
{
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
}
if ( $mode == 4 )
{
if ( $status == 1 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $langs -> trans ( 'Enabled' );
if ( $status == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Disabled' );
}
if ( $mode == 5 )
{
if ( $status == 1 ) return $langs -> trans ( 'Enabled' ) . ' ' . img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
if ( $status == 0 ) return $langs -> trans ( 'Disabled' ) . ' ' . img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
}
}
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
/**
* Initialise object with example values
* Id must be 0 if object instance is a specimen
*
* @ return void
*/
public function initAsSpecimen ()
{
2017-04-28 13:10:08 +02:00
global $user ;
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
$this -> id = 0 ;
2017-07-19 16:38:00 +02:00
2016-05-02 15:51:18 +02:00
$this -> entity = 1 ;
$this -> ref = 'myspecimenwebsite' ;
$this -> description = 'A specimen website' ;
2016-04-12 19:17:50 +02:00
$this -> status = '' ;
2016-05-02 15:51:18 +02:00
$this -> fk_default_home = null ;
2016-07-14 15:54:46 +02:00
$this -> virtualhost = 'http://myvirtualhost' ;
2017-04-28 13:10:08 +02:00
$this -> fk_user_create = $user -> id ;
$this -> fk_user_modif = $user -> id ;
2016-05-02 15:51:18 +02:00
$this -> date_creation = dol_now ();
$this -> tms = dol_now ();
2016-04-12 19:17:50 +02:00
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
}
2017-11-05 03:39:27 +01:00
/**
* Generate a zip with all data of web site .
*
* @ return string Path to file with zip
*/
function exportWebSite ()
{
global $conf ;
$website = $this ;
2017-12-11 21:27:43 +01:00
if ( empty ( $website -> id ) || empty ( $website -> ref ))
{
setEventMessages ( " Website id or ref is not defined " , null , 'errors' );
return '' ;
}
dol_syslog ( " Create temp dir " . $conf -> website -> dir_temp );
2017-11-05 03:39:27 +01:00
dol_mkdir ( $conf -> website -> dir_temp );
2017-12-11 18:58:08 +01:00
if ( ! is_writable ( $conf -> website -> dir_temp ))
{
setEventMessages ( " Temporary dir " . $conf -> website -> dir_temp . " is not writable " , null , 'errors' );
return '' ;
}
2017-11-05 03:39:27 +01:00
$srcdir = $conf -> website -> dir_output . '/' . $website -> ref ;
2017-12-11 21:27:43 +01:00
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/containers' ;
2017-11-05 03:39:27 +01:00
$arrayreplacement = array ();
2017-12-11 21:27:43 +01:00
dol_syslog ( " Clear temp dir " . $destdir );
dol_delete_dir ( $destdir , 1 );
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
2017-11-05 03:39:27 +01:00
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacement );
2017-12-11 21:27:43 +01:00
$srcdir = DOL_DATA_ROOT . '/medias/image/' . $website -> ref ;
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/image/' . $website -> ref ;
2017-11-05 03:39:27 +01:00
2017-12-11 21:27:43 +01:00
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
2017-11-05 03:39:27 +01:00
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacement );
2017-12-27 20:21:19 +01:00
$srcdir = DOL_DATA_ROOT . '/medias/js/' . $website -> ref ;
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/js/' . $website -> ref ;
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacement );
2017-11-05 03:39:27 +01:00
// Build sql file
2017-12-11 21:27:43 +01:00
dol_syslog ( " Create containers dir " );
dol_mkdir ( $conf -> website -> dir_temp . '/' . $website -> ref . '/containers' );
2017-11-05 03:39:27 +01:00
2017-12-11 21:27:43 +01:00
$filesql = $conf -> website -> dir_temp . '/' . $website -> ref . '/website_pages.sql' ;
2017-11-05 03:39:27 +01:00
$fp = fopen ( $filesql , " w " );
2017-12-11 18:25:23 +01:00
if ( empty ( $fp ))
{
2017-12-11 18:58:08 +01:00
setEventMessages ( " Failed to create file " . $filesql , null , 'errors' );
2017-12-11 18:25:23 +01:00
return '' ;
}
2017-11-05 03:39:27 +01:00
$objectpages = new WebsitePage ( $this -> db );
$listofpages = $objectpages -> fetchAll ( $website -> id );
// Assign ->newid and ->newfk_page
$i = 1 ;
foreach ( $listofpages as $pageid => $objectpageold )
{
$objectpageold -> newid = $i ;
$i ++ ;
}
$i = 1 ;
foreach ( $listofpages as $pageid => $objectpageold )
{
// Search newid
$newfk_page = 0 ;
foreach ( $listofpages as $pageid2 => $objectpageold2 )
{
if ( $pageid2 == $objectpageold -> fk_page )
{
$newfk_page = $objectpageold2 -> newid ;
break ;
}
}
$objectpageold -> newfk_page = $newfk_page ;
$i ++ ;
}
foreach ( $listofpages as $pageid => $objectpageold )
{
$line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, title, description, keyword, status, date_creation, tms, lang, import_key, grabbed_from, content)' ;
$line .= " VALUES( " ;
$line .= $objectpageold -> newid . " +__MAXROWID__, " ;
$line .= ( $objectpageold -> newfk_page ? $this -> db -> escape ( $objectpageold -> newfk_page ) . " +__MAXROWID__ " : " null " ) . " , " ;
$line .= " __WEBSITE_ID__, " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> pageurl ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> title ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> description ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> keyword ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> status ) . " ', " ;
$line .= " ' " . $this -> db -> idate ( $objectpageold -> date_creation ) . " ', " ;
$line .= " ' " . $this -> db -> idate ( $objectpageold -> date_modification ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> lang ) . " ', " ;
$line .= ( $objectpageold -> import_key ? " ' " . $this -> db -> escape ( $objectpageold -> import_key ) . " ' " : " null " ) . " , " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> grabbed_from ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> content ) . " ' " ;
$line .= " ); " ;
$line .= " \n " ;
fputs ( $fp , $line );
}
fclose ( $fp );
if ( ! empty ( $conf -> global -> MAIN_UMASK ))
@ chmod ( $filesql , octdec ( $conf -> global -> MAIN_UMASK ));
// Build zip file
$filedir = $conf -> website -> dir_temp . '/' . $website -> ref ;
2017-12-11 21:27:43 +01:00
$fileglob = $conf -> website -> dir_temp . '/' . $website -> ref . '/website_' . $website -> ref . '-*.zip' ;
$filename = $conf -> website -> dir_temp . '/' . $website -> ref . '/website_' . $website -> ref . '-' . dol_print_date ( dol_now (), 'dayhourlog' ) . '.zip' ;
2017-11-05 03:39:27 +01:00
dol_delete_file ( $fileglob , 0 );
dol_compress_file ( $filedir , $filename , 'zip' );
return $filename ;
}
/**
* Open a zip with all data of web site and load it into database .
*
* @ param string $pathtofile Path of zip file
* @ return int < 0 if KO , Id of new website if OK
*/
function importWebSite ( $pathtofile )
{
global $conf ;
$result = 0 ;
$object = new Website ( $this -> db );
$filename = basename ( $pathtofile );
if ( ! preg_match ( '/^website_(.*)-(.*)$/' , $filename , $reg ))
{
$this -> errors [] = 'Bad format for filename ' . $filename . '. Must be website_XXX-VERSION.' ;
return - 1 ;
}
$websitecode = $reg [ 1 ];
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " website(ref, entity, description, status) values(' " . $websitecode . " ', " . $conf -> entity . " , 'Portal to sell your SaaS. Do not remove this entry.', 1) " ;
$resql = $this -> db -> query ( $sql );
return $result ;
}
2016-04-12 19:17:50 +02:00
}