2016-04-12 19:17:50 +02:00
< ? php
2018-09-25 15:00:37 +02:00
/* Copyright ( C ) 2007 - 2018 Laurent Destailleur < eldy @ users . sourceforge . net >
2016-04-12 19:17:50 +02:00
* 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 >
2018-09-09 13:29:58 +02:00
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2016-04-12 19:17:50 +02:00
*
* 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-04-12 19:17:50 +02:00
*/
/**
2019-12-15 10:02:20 +01:00
* \file htdocs / website / class / website . class . php
2017-10-20 21:39:19 +02:00
* \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
2019-11-08 15:51:54 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2016-04-12 19:17:50 +02:00
//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' ;
2018-08-31 18:27:16 +02:00
2016-04-12 19:17:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
public $table_element = 'website' ;
2018-08-31 18:27:16 +02:00
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 ;
2018-08-31 18:27:16 +02:00
2017-11-03 15:56:16 +01:00
/**
* @ 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
/**
2018-08-31 18:27:16 +02:00
* @ var int Entity
2016-04-12 19:17:50 +02:00
*/
public $entity ;
2018-08-31 18:27:16 +02:00
2016-05-06 21:28:06 +02:00
/**
2018-08-31 18:27:16 +02:00
* @ var string Ref
2016-05-06 21:28:06 +02:00
*/
2016-04-19 11:02:55 +02:00
public $ref ;
2018-08-31 18:27:16 +02:00
2016-05-06 21:28:06 +02:00
/**
2018-08-31 18:27:16 +02:00
* @ var string description
2016-05-06 21:28:06 +02:00
*/
2016-04-12 19:17:50 +02:00
public $description ;
2018-08-31 18:27:16 +02:00
2020-02-19 18:32:04 +01:00
/**
* @ var string Main language of web site
*/
public $lang ;
/**
* @ var string List of languages of web site ( 'fr' , 'es_MX' , ... )
*/
public $otherlang ;
2016-05-06 21:28:06 +02:00
/**
2018-08-31 18:27:16 +02:00
* @ var int Status
2016-05-06 21:28:06 +02:00
*/
2016-04-12 19:17:50 +02:00
public $status ;
2018-08-31 18:27:16 +02:00
2016-05-06 21:28:06 +02:00
/**
2019-11-10 12:22:24 +01:00
* @ var integer | string date_creation
2016-05-06 21:28:06 +02:00
*/
public $date_creation ;
2019-11-10 12:23:37 +01:00
2019-11-10 12:22:24 +01:00
/**
* @ var integer | string date_modification
*/
2019-01-15 10:53:28 +01:00
public $date_modification ;
2018-08-31 18:27:16 +02:00
2016-05-06 21:28:06 +02:00
/**
* @ var integer
*/
public $fk_default_home ;
2020-11-24 22:31:03 +01:00
/**
* @ var int User Create Id
*/
2019-01-15 10:53:28 +01:00
public $fk_user_creat ;
2018-08-31 18:27:16 +02:00
2016-07-14 15:54:46 +02:00
/**
* @ var string
*/
public $virtualhost ;
2017-07-19 16:38:00 +02:00
2019-12-07 13:09:55 +01:00
/**
* @ var int
*/
public $use_manifest ;
2020-06-08 23:42:48 +02:00
/**
* @ var int
*/
public $position ;
2019-10-31 21:09:33 +01:00
/**
* List of containers
*
* @ var array
*/
public $lines ;
const STATUS_DRAFT = 0 ;
const STATUS_VALIDATED = 1 ;
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 )
{
2020-02-21 16:37:07 +01:00
global $conf , $langs ;
2017-12-19 11:40:29 +01:00
2016-04-12 19:17:50 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
2019-11-08 15:51:54 +01:00
$now = dol_now ();
2016-04-12 19:17:50 +02:00
// Clean parameters
if ( isset ( $this -> entity )) {
2019-02-13 23:53:33 +01:00
$this -> entity = ( int ) $this -> entity ;
2016-04-12 19:17:50 +02:00
}
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 )) {
2019-02-13 23:53:33 +01:00
$this -> status = ( int ) $this -> status ;
2016-04-12 19:17:50 +02:00
}
2020-10-31 14:32:18 +01:00
if ( empty ( $this -> date_creation )) {
$this -> date_creation = $now ;
}
if ( empty ( $this -> date_modification )) {
$this -> date_modification = $now ;
}
// Remove spaces and be sure we have main language only
$this -> lang = preg_replace ( '/[_-].*$/' , '' , trim ( $this -> lang )); // en_US or en-US -> en
$tmparray = explode ( ',' , $this -> otherlang );
2020-02-21 11:36:16 +01:00
if ( is_array ( $tmparray )) {
2020-04-10 10:59:32 +02:00
foreach ( $tmparray as $key => $val ) {
2021-04-07 09:29:40 +02:00
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
2021-04-07 10:01:05 +02:00
if ( empty ( trim ( $val ))) {
unset ( $tmparray [ $key ]);
continue ;
}
2020-02-21 11:36:16 +01:00
$tmparray [ $key ] = preg_replace ( '/[_-].*$/' , '' , trim ( $val )); // en_US or en-US -> en
}
$this -> otherlang = join ( ',' , $tmparray );
}
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01:00
// Check parameters
if ( empty ( $this -> entity )) {
$this -> entity = $conf -> entity ;
}
if ( empty ( $this -> lang )) {
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " MainLanguage " ));
return - 1 ;
}
2016-04-12 19:17:50 +02:00
// Insert request
2019-11-08 15:51:54 +01:00
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this -> table_element . '(' ;
$sql .= 'entity,' ;
$sql .= 'ref,' ;
$sql .= 'description,' ;
2020-02-19 18:32:04 +01:00
$sql .= 'lang,' ;
$sql .= 'otherlang,' ;
2019-11-08 15:51:54 +01:00
$sql .= 'status,' ;
$sql .= 'fk_default_home,' ;
$sql .= 'virtualhost,' ;
$sql .= 'fk_user_creat,' ;
$sql .= 'date_creation,' ;
2020-06-08 23:42:48 +02:00
$sql .= 'position,' ;
2019-11-08 15:51:54 +01:00
$sql .= 'tms' ;
2016-04-12 19:17:50 +02:00
$sql .= ') VALUES (' ;
2019-11-08 15:51:54 +01:00
$sql .= ' ' . (( empty ( $this -> entity ) && $this -> entity != '0' ) ? 'NULL' : $this -> entity ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> ref ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> ref ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> description ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> description ) . " ' " ) . ',' ;
2020-02-19 18:32:04 +01:00
$sql .= ' ' . ( ! isset ( $this -> lang ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> lang ) . " ' " ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> otherlang ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> otherlang ) . " ' " ) . ',' ;
2019-11-08 15:51:54 +01:00
$sql .= ' ' . ( ! isset ( $this -> status ) ? '1' : $this -> status ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> fk_default_home ) ? 'NULL' : $this -> fk_default_home ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> virtualhost ) ? 'NULL' : " ' " . $this -> db -> escape ( $this -> virtualhost ) . " ' " ) . " , " ;
$sql .= ' ' . ( ! isset ( $this -> fk_user_creat ) ? $user -> id : $this -> fk_user_creat ) . ',' ;
$sql .= ' ' . ( ! isset ( $this -> date_creation ) || dol_strlen ( $this -> date_creation ) == 0 ? 'NULL' : " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " ) . " , " ;
2020-06-08 23:42:48 +02:00
$sql .= ' ' . (( int ) $this -> position ) . " , " ;
2019-11-08 15:51:54 +01:00
$sql .= ' ' . ( ! isset ( $this -> date_modification ) || dol_strlen ( $this -> date_modification ) == 0 ? 'NULL' : " ' " . $this -> db -> idate ( $this -> date_modification ) . " ' " );
2016-04-12 19:17:50 +02:00
$sql .= ')' ;
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2019-11-08 15:51:54 +01:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
}
if ( ! $error ) {
2019-11-08 15:51:54 +01:00
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . $this -> table_element );
2016-04-12 19:17:50 +02:00
2020-02-19 18:32:04 +01:00
// Create subdirectory per language
$tmplangarray = explode ( ',' , $this -> otherlang );
if ( is_array ( $tmplangarray )) {
dol_mkdir ( $conf -> website -> dir_output . '/' . $this -> ref );
2020-02-21 17:53:37 +01:00
foreach ( $tmplangarray as $val ) {
2021-02-26 12:03:07 +01:00
if ( trim ( $val ) == $this -> lang ) {
continue ;
}
2020-02-19 18:32:04 +01:00
dol_mkdir ( $conf -> website -> dir_output . '/' . $this -> ref . '/' . trim ( $val ));
}
}
2022-04-02 16:08:04 +02:00
// Uncomment this and change WEBSITE to your own tag if you
2020-10-31 18:51:30 +01:00
// want this action to call a trigger.
// if (!$notrigger) {
// // Call triggers
2022-04-02 16:08:04 +02:00
// $result = $this->call_trigger('WEBSITE_CREATE',$user);
2020-10-31 18:51:30 +01:00
// if ($result < 0) $error++;
// // End call triggers
// }
}
if ( ! $error ) {
$stringtodolibarrfile = " # Some properties for Dolibarr web site CMS \n " ;
$stringtodolibarrfile .= " param=value \n " ;
//print $conf->website->dir_output.'/'.$this->ref.'/.dolibarr';exit;
file_put_contents ( $conf -> website -> dir_output . '/' . $this -> ref . '/.dolibarr' , $stringtodolibarrfile );
}
2020-05-03 14:28:23 +02:00
2016-04-12 19:17:50 +02:00
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2019-11-08 15:51:54 +01:00
return - 1 * $error ;
2016-04-12 19:17:50 +02:00
} 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 );
2021-09-30 15:59:47 +02:00
$sql = " SELECT " ;
$sql .= " t.rowid, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.entity, " ;
2016-04-19 11:02:55 +02:00
$sql .= " t.ref, " ;
2020-08-04 02:50:56 +02:00
$sql .= " t.position, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.description, " ;
2020-02-19 18:32:04 +01:00
$sql .= " t.lang, " ;
$sql .= " t.otherlang, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.status, " ;
2016-05-02 15:51:18 +02:00
$sql .= " t.fk_default_home, " ;
2019-12-07 13:09:55 +01:00
$sql .= " t.use_manifest, " ;
2016-07-14 15:54:46 +02:00
$sql .= " t.virtualhost, " ;
2018-09-25 15:00:37 +02:00
$sql .= " t.fk_user_creat, " ;
2017-04-28 13:10:08 +02:00
$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 " ;
2021-09-30 15:59:47 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as t " ;
$sql .= " WHERE t.entity IN ( " . getEntity ( 'website' ) . " ) " ;
2019-11-08 15:51:54 +01:00
if ( ! empty ( $ref )) {
$sql .= " AND t.ref = ' " . $this -> db -> escape ( $ref ) . " ' " ;
2016-04-12 19:17:50 +02:00
} else {
2021-09-30 15:59:47 +02:00
$sql .= " AND t.rowid = " . ( int ) $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 ;
2020-08-04 02:50:56 +02:00
$this -> position = $obj -> position ;
2016-04-12 19:17:50 +02:00
$this -> description = $obj -> description ;
2020-02-19 18:32:04 +01:00
$this -> lang = $obj -> lang ;
$this -> otherlang = $obj -> otherlang ;
2016-04-12 19:17:50 +02:00
$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 ;
2019-12-07 13:09:55 +01:00
$this -> use_manifest = $obj -> use_manifest ;
2018-09-25 15:00:37 +02:00
$this -> fk_user_creat = $obj -> fk_user_creat ;
2017-04-28 13:10:08 +02:00
$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 {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
2019-11-08 15:51:54 +01:00
return - 1 ;
2016-04-12 19:17:50 +02:00
}
}
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
{
2019-11-08 15:51:54 +01:00
$this -> lines = array ();
2017-08-03 01:45:31 +02:00
// Load lines with object MyObjectLine
2019-11-08 15:51:54 +01:00
return count ( $this -> lines ) ? 1 : 0 ;
2017-08-03 01:45:31 +02:00
}
2016-04-12 19:17:50 +02:00
/**
2020-03-21 11:04:33 +01:00
* Load all object in memory ( $this -> records ) from the database
2016-04-12 19:17:50 +02:00
*
* @ 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
*/
2019-01-27 15:20:16 +01:00
public function fetchAll ( $sortorder = '' , $sortfield = '' , $limit = 0 , $offset = 0 , array $filter = array (), $filtermode = 'AND' )
2016-04-12 19:17:50 +02:00
{
dol_syslog ( __METHOD__ , LOG_DEBUG );
2021-09-30 15:59:47 +02:00
$sql = " SELECT " ;
$sql .= " t.rowid, " ;
2016-04-12 19:17:50 +02:00
$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, " ;
2020-02-19 18:32:04 +01:00
$sql .= " t.lang, " ;
$sql .= " t.otherlang, " ;
2016-04-12 19:17:50 +02:00
$sql .= " t.status, " ;
2016-07-14 15:54:46 +02:00
$sql .= " t.fk_default_home, " ;
$sql .= " t.virtualhost, " ;
2018-09-25 15:00:37 +02:00
$sql .= " t.fk_user_creat, " ;
2017-04-28 13:10:08 +02:00
$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 " ;
2021-09-30 15:59:47 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . $this -> table_element . " as t " ;
$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 ) {
2021-08-28 01:45:53 +02:00
$sqlwhere [] = $key . " LIKE '% " . $this -> db -> escape ( $value ) . " %' " ;
2016-04-12 19:17:50 +02:00
}
}
if ( count ( $sqlwhere ) > 0 ) {
2021-09-02 13:58:42 +02:00
$sql .= ' AND ' . implode ( ' ' . $this -> db -> escape ( $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 )) {
2019-01-27 11:55:16 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2016-04-12 19:17:50 +02:00
}
if ( ! empty ( $limit )) {
2021-08-27 22:42:04 +02: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 ;
2020-02-19 18:32:04 +01:00
$line -> lang = $obj -> lang ;
$line -> otherlang = $obj -> otherlang ;
2016-04-12 19:17:50 +02:00
$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 ;
2018-09-25 15:00:37 +02:00
$this -> fk_user_creat = $obj -> fk_user_creat ;
2017-04-28 13:10:08 +02:00
$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 {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
2019-11-08 15:51:54 +01:00
return - 1 ;
2016-04-12 19:17:50 +02:00
}
}
/**
* 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 )
{
2020-02-21 16:37:07 +01:00
global $conf , $langs ;
2020-02-19 18:32:04 +01:00
2016-04-12 19:17:50 +02:00
$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 )) {
2019-02-13 23:53:33 +01:00
$this -> entity = ( int ) $this -> entity ;
2016-04-12 19:17:50 +02:00
}
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 )) {
2019-02-13 23:53:33 +01:00
$this -> status = ( int ) $this -> status ;
2016-04-12 19:17:50 +02:00
}
2020-02-21 11:36:16 +01:00
// Remove spaces and be sure we have main language only
$this -> lang = preg_replace ( '/[_-].*$/' , '' , trim ( $this -> lang )); // en_US or en-US -> en
$tmparray = explode ( ',' , $this -> otherlang );
if ( is_array ( $tmparray )) {
2020-04-10 10:59:32 +02:00
foreach ( $tmparray as $key => $val ) {
2021-04-07 09:29:40 +02:00
// It possible we have empty val here if postparam WEBSITE_OTHERLANG is empty or set like this : 'en,,sv' or 'en,sv,'
2021-04-07 10:01:05 +02:00
if ( empty ( trim ( $val ))) {
unset ( $tmparray [ $key ]);
continue ;
}
2020-02-21 11:36:16 +01:00
$tmparray [ $key ] = preg_replace ( '/[_-].*$/' , '' , trim ( $val )); // en_US or en-US -> en
}
$this -> otherlang = join ( ',' , $tmparray );
}
2020-02-21 16:37:07 +01:00
if ( empty ( $this -> lang )) {
$this -> error = $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " MainLanguage " ));
return - 1 ;
}
2020-02-21 11:36:16 +01:00
2016-04-12 19:17:50 +02:00
// Check parameters
// Put here code to add a control on parameters values
// Update request
2019-11-08 15:51:54 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this -> table_element . ' SET' ;
$sql .= ' entity = ' . ( isset ( $this -> entity ) ? $this -> entity : " null " ) . ',' ;
$sql .= ' ref = ' . ( isset ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : " null " ) . ',' ;
$sql .= ' description = ' . ( isset ( $this -> description ) ? " ' " . $this -> db -> escape ( $this -> description ) . " ' " : " null " ) . ',' ;
2020-02-19 18:32:04 +01:00
$sql .= ' lang = ' . ( isset ( $this -> lang ) ? " ' " . $this -> db -> escape ( $this -> lang ) . " ' " : " null " ) . ',' ;
$sql .= ' otherlang = ' . ( isset ( $this -> otherlang ) ? " ' " . $this -> db -> escape ( $this -> otherlang ) . " ' " : " null " ) . ',' ;
2019-11-08 15:51:54 +01:00
$sql .= ' status = ' . ( isset ( $this -> status ) ? $this -> status : " null " ) . ',' ;
$sql .= ' fk_default_home = ' . (( $this -> fk_default_home > 0 ) ? $this -> fk_default_home : " null " ) . ',' ;
2019-12-07 13:09:55 +01:00
$sql .= ' use_manifest = ' . (( int ) $this -> use_manifest ) . ',' ;
2019-11-08 15:51:54 +01:00
$sql .= ' virtualhost = ' . (( $this -> virtualhost != '' ) ? " ' " . $this -> db -> escape ( $this -> virtualhost ) . " ' " : " null " ) . ',' ;
$sql .= ' fk_user_modif = ' . ( ! isset ( $this -> fk_user_modif ) ? $user -> id : $this -> fk_user_modif ) . ',' ;
$sql .= ' date_creation = ' . ( ! isset ( $this -> date_creation ) || dol_strlen ( $this -> date_creation ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " : 'null' ) . ',' ;
2019-08-12 17:34:18 +02:00
$sql .= ' tms = ' . ( dol_strlen ( $this -> date_modification ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_modification ) . " ' " : " ' " . $this -> db -> idate ( dol_now ()) . " ' " );
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2016-04-12 19:17:50 +02:00
$this -> db -> begin ();
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2019-11-08 15:51:54 +01:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
}
if ( ! $error && ! $notrigger ) {
// Uncomment this and change MYOBJECT to your own tag if you
// want this action calls a trigger.
2020-02-19 18:32:04 +01:00
// Create subdirectory per language
$tmplangarray = explode ( ',' , $this -> otherlang );
if ( is_array ( $tmplangarray )) {
dol_mkdir ( $conf -> website -> dir_output . '/' . $this -> ref );
2020-02-21 17:53:37 +01:00
foreach ( $tmplangarray as $val ) {
2021-02-26 12:03:07 +01:00
if ( trim ( $val ) == $this -> lang ) {
continue ;
}
2020-02-19 18:32:04 +01:00
dol_mkdir ( $conf -> website -> dir_output . '/' . $this -> ref . '/' . trim ( $val ));
}
}
2016-04-12 19:17:50 +02:00
//// Call triggers
2022-04-02 16:08:04 +02:00
//$result=$this->call_trigger('WEBSITE_MODIFY',$user);
2016-04-12 19:17:50 +02:00
//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 ();
2019-11-08 15:51:54 +01:00
return - 1 * $error ;
2016-04-12 19:17:50 +02:00
} 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 )
{
2022-04-25 10:14:30 +02:00
global $conf ;
2016-04-12 19:17:50 +02:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
$error = 0 ;
$this -> db -> begin ();
if ( ! $error ) {
if ( ! $notrigger ) {
2022-04-02 16:08:04 +02:00
// Uncomment this and change WEBSITE to your own tag if you
2016-04-12 19:17:50 +02:00
// want this action calls a trigger.
//// Call triggers
2022-04-02 16:08:04 +02:00
//$result=$this->call_trigger('WEBSITE_DELETE',$user);
2016-04-12 19:17:50 +02:00
//if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
//// End call triggers
}
}
if ( ! $error ) {
2019-11-08 15:51:54 +01:00
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this -> table_element ;
2021-03-14 12:20:23 +01:00
$sql .= ' WHERE rowid=' . (( int ) $this -> id );
2016-04-12 19:17:50 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2019-11-08 15:51:54 +01:00
$error ++ ;
$this -> errors [] = 'Error ' . $this -> db -> lasterror ();
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
}
}
2021-02-26 12:03:07 +01:00
if ( ! $error && ! empty ( $this -> ref )) {
2022-04-25 10:14:30 +02:00
$pathofwebsite = DOL_DATA_ROOT . ( $conf -> entity > 1 ? '/' . $conf -> entity : '' ) . '/website/' . $this -> ref ;
2018-09-08 14:10:07 +02:00
dol_delete_dir_recursive ( $pathofwebsite );
}
2016-04-12 19:17:50 +02:00
// Commit or rollback
if ( $error ) {
$this -> db -> rollback ();
2019-11-08 15:51:54 +01:00
return - 1 * $error ;
2016-04-12 19:17:50 +02:00
} else {
$this -> db -> commit ();
return 1 ;
}
}
/**
2021-05-20 10:34:49 +02:00
* Load a website its id and create a new one in database .
2017-09-01 11:10:03 +02:00
* 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
2018-08-13 23:22:20 +02:00
* @ param string $newlang New language
2017-08-21 00:40:45 +02:00
* @ return mixed New object created , < 0 if KO
2016-04-12 19:17:50 +02:00
*/
2019-01-27 15:20:16 +01:00
public function createFromClone ( $user , $fromid , $newref , $newlang = '' )
2016-04-12 19:17:50 +02:00
{
2020-10-31 14:32:18 +01:00
global $conf , $langs ;
2017-08-21 00:40:45 +02:00
global $dolibarr_main_data_root ;
2019-01-15 10:53:28 +01:00
$now = dol_now ();
2019-11-08 15:51:54 +01:00
$error = 0 ;
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01:00
dol_syslog ( __METHOD__ , LOG_DEBUG );
2017-08-03 01:45:31 +02:00
2022-02-22 18:46:46 +01:00
$newref = dol_sanitizeFileName ( $newref );
if ( empty ( $newref )) {
$this -> error = 'ErrorBadParameter' ;
return - 1 ;
}
2017-08-03 01:45:31 +02:00
$object = new self ( $this -> db );
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01:00
// Check no site with ref exists
2021-02-26 12:03:07 +01:00
if ( $object -> fetch ( 0 , $newref ) > 0 ) {
2019-11-08 15:51:54 +01:00
$this -> error = 'ErrorNewRefIsAlreadyUsed' ;
2017-08-21 00:40:45 +02:00
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
2019-11-08 15:51:54 +01:00
$oldidforhome = $object -> fk_default_home ;
$oldref = $object -> ref ;
2017-08-21 00:40:45 +02:00
2022-04-25 10:14:30 +02:00
$pathofwebsiteold = $dolibarr_main_data_root . ( $conf -> entity > 1 ? '/' . $conf -> entity : '' ) . '/website/' . dol_sanitizeFileName ( $oldref );
$pathofwebsitenew = $dolibarr_main_data_root . ( $conf -> entity > 1 ? '/' . $conf -> entity : '' ) . '/website/' . dol_sanitizeFileName ( $newref );
2017-08-21 00:40:45 +02:00
dol_delete_dir_recursive ( $pathofwebsitenew );
2019-11-08 15:51:54 +01:00
$fileindex = $pathofwebsitenew . '/index.php' ;
2017-08-21 00:40:45 +02:00
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
2019-11-08 15:51:54 +01:00
$object -> ref = $newref ;
$object -> fk_default_home = 0 ;
$object -> virtualhost = '' ;
2019-01-15 10:53:28 +01:00
$object -> date_creation = $now ;
$object -> fk_user_creat = $user -> id ;
2020-08-04 02:50:56 +02:00
$object -> position = (( int ) $object -> position ) + 1 ;
$object -> status = self :: STATUS_DRAFT ;
2021-02-26 12:03:07 +01:00
if ( empty ( $object -> lang )) {
$object -> lang = substr ( $langs -> defaultlang , 0 , 2 ); // Should not happen. Protection for corrupted site with no languages
}
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 ) {
2019-11-08 15:51:54 +01:00
$error ++ ;
2020-06-08 23:37:49 +02:00
$this -> error = $object -> error ;
2016-04-12 19:17:50 +02:00
$this -> errors = $object -> errors ;
2019-11-08 15:51:54 +01:00
dol_syslog ( __METHOD__ . ' ' . join ( ',' , $this -> errors ), LOG_ERR );
2016-04-12 19:17:50 +02:00
}
2021-02-26 12:03:07 +01:00
if ( ! $error ) {
2020-05-03 14:49:06 +02:00
dolCopyDir ( $pathofwebsiteold , $pathofwebsitenew , $conf -> global -> MAIN_UMASK , 0 , null , 2 );
2017-08-21 00:40:45 +02:00
// Check symlink to medias and restore it if ko
2019-11-08 15:51:54 +01:00
$pathtomedias = DOL_DATA_ROOT . '/medias' ; // Target
$pathtomediasinwebsite = $pathofwebsitenew . '/medias' ; // Source / Link name
2021-02-26 12:03:07 +01:00
if ( ! is_link ( dol_osencode ( $pathtomediasinwebsite ))) {
2017-08-21 00:40:45 +02:00
dol_syslog ( " Create symlink for " . $pathtomedias . " into name " . $pathtomediasinwebsite );
2019-11-08 15:51:54 +01:00
dol_mkdir ( dirname ( $pathtomediasinwebsite )); // To be sure dir for website exists
2017-08-21 00:40:45 +02:00
$result = symlink ( $pathtomedias , $pathtomediasinwebsite );
}
2019-06-02 17:27:00 +02:00
// Copy images and js dir
2019-11-08 15:51:54 +01:00
$pathofmediasjsold = DOL_DATA_ROOT . '/medias/js/' . $oldref ;
$pathofmediasjsnew = DOL_DATA_ROOT . '/medias/js/' . $newref ;
2019-06-02 17:27:00 +02:00
dolCopyDir ( $pathofmediasjsold , $pathofmediasjsnew , $conf -> global -> MAIN_UMASK , 0 );
2019-11-08 15:51:54 +01:00
$pathofmediasimageold = DOL_DATA_ROOT . '/medias/image/' . $oldref ;
$pathofmediasimagenew = DOL_DATA_ROOT . '/medias/image/' . $newref ;
2019-06-02 17:27:00 +02:00
dolCopyDir ( $pathofmediasimageold , $pathofmediasimagenew , $conf -> global -> MAIN_UMASK , 0 );
2019-11-08 15:51:54 +01:00
$newidforhome = 0 ;
2017-08-21 00:40:45 +02:00
// Duplicate pages
$objectpages = new WebsitePage ( $this -> db );
$listofpages = $objectpages -> fetchAll ( $fromid );
2021-02-26 12:03:07 +01:00
foreach ( $listofpages as $pageid => $objectpageold ) {
2017-08-21 00:40:45 +02:00
// Delete old file
2019-11-08 15:51:54 +01:00
$filetplold = $pathofwebsitenew . '/page' . $pageid . '.tpl.php' ;
2017-08-21 00:40:45 +02:00
dol_delete_file ( $filetplold );
// Create new file
2018-08-13 23:22:20 +02:00
$objectpagenew = $objectpageold -> createFromClone ( $user , $pageid , $objectpageold -> pageurl , '' , 0 , $object -> id , 1 );
2019-01-19 12:46:38 +01:00
2017-08-21 00:40:45 +02:00
//print $pageid.' = '.$objectpageold->pageurl.' -> '.$objectpagenew->id.' = '.$objectpagenew->pageurl.'<br>';
2021-02-26 12:03:07 +01:00
if ( is_object ( $objectpagenew ) && $objectpagenew -> pageurl ) {
2020-10-31 14:32:18 +01:00
$filealias = $pathofwebsitenew . '/' . $objectpagenew -> pageurl . '.php' ;
2019-11-08 15:51:54 +01:00
$filetplnew = $pathofwebsitenew . '/page' . $objectpagenew -> id . '.tpl.php' ;
2017-08-21 00:40:45 +02:00
// Save page alias
2019-11-08 15:51:54 +01:00
$result = dolSavePageAlias ( $filealias , $object , $objectpagenew );
2021-02-26 12:03:07 +01:00
if ( ! $result ) {
setEventMessages ( 'Failed to write file ' . $filealias , null , 'errors' );
}
2017-08-21 00:40:45 +02:00
2019-11-08 15:51:54 +01:00
$result = dolSavePageContent ( $filetplnew , $object , $objectpagenew );
2021-02-26 12:03:07 +01:00
if ( ! $result ) {
setEventMessages ( 'Failed to write file ' . $filetplnew , null , 'errors' );
}
2017-08-21 00:40:45 +02:00
2021-02-26 12:03:07 +01:00
if ( $pageid == $oldidforhome ) {
2017-08-21 00:40:45 +02:00
$newidforhome = $objectpagenew -> id ;
}
2021-02-26 12:03:07 +01:00
} else {
2017-08-21 00:40:45 +02:00
setEventMessages ( $objectpageold -> error , $objectpageold -> errors , 'errors' );
$error ++ ;
}
}
}
2021-02-26 12:03:07 +01:00
if ( ! $error ) {
2017-08-21 00:40:45 +02:00
// Restore id of home page
$object -> fk_default_home = $newidforhome ;
2020-10-31 14:32:18 +01:00
$res = $object -> update ( $user );
2021-02-26 12:03:07 +01:00
if ( ! ( $res > 0 )) {
2020-10-31 14:32:18 +01:00
$error ++ ;
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
2017-08-21 00:40:45 +02:00
2021-02-26 12:03:07 +01:00
if ( ! $error ) {
2020-10-31 14:32:18 +01:00
$filetpl = $pathofwebsitenew . '/page' . $newidforhome . '.tpl.php' ;
$filewrapper = $pathofwebsitenew . '/wrapper.php' ;
2017-08-21 00:40:45 +02:00
2021-05-20 10:34:49 +02:00
// Re-generates the index.php page to be the home page, and re-generates the wrapper.php
//--------------------------------------------------------------------------------------
2020-10-31 14:32:18 +01:00
$result = dolSaveIndexPage ( $pathofwebsitenew , $fileindex , $filetpl , $filewrapper );
}
2017-08-21 00:40:45 +02:00
}
2019-01-19 12:46:38 +01:00
unset ( $object -> context [ 'createfromclone' ]);
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 ();
2019-11-08 15:51:54 +01:00
return - 1 ;
2016-04-12 19:17:50 +02:00
}
}
/**
2019-02-10 12:59:09 +01:00
* Return a link to the user card ( with optionally the picto )
2016-04-12 19:17:50 +02:00
* 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
2020-10-31 14:32:18 +01:00
* @ param integer $notooltip 1 = Disable tooltip
* @ param int $maxlen Max length of visible user name
* @ param string $morecss Add more css on link
2016-04-12 19:17:50 +02:00
* @ return string String with URL
*/
2019-02-25 20:35:59 +01:00
public function getNomUrl ( $withpicto = 0 , $option = '' , $notooltip = 0 , $maxlen = 24 , $morecss = '' )
2016-04-12 19:17:50 +02:00
{
global $langs , $conf , $db ;
2020-10-31 14:32:18 +01:00
global $dolibarr_main_authentication , $dolibarr_main_demo ;
global $menumanager ;
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01:00
$result = '' ;
$companylink = '' ;
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01:00
$label = '<u>' . $langs -> trans ( " WebSite " ) . '</u>' ;
$label .= '<br>' ;
$label .= '<b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref . '<br>' ;
$label .= '<b>' . $langs -> trans ( 'MainLanguage' ) . ':</b> ' . $this -> lang ;
2016-04-12 19:17:50 +02:00
2020-10-31 14:32:18 +01: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 .= '>' ;
2019-11-08 15:51:54 +01:00
$linkend = '</a>' ;
2016-04-12 19:17:50 +02:00
2017-10-26 02:55:43 +02:00
$linkstart = $linkend = '' ;
2021-02-26 12:03:07 +01:00
if ( $withpicto ) {
2020-10-31 14:32:18 +01:00
$result .= ( $linkstart . img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? '' : 'class="classfortooltip"' )) . $linkend );
2021-02-26 12:03:07 +01:00
if ( $withpicto != 2 ) {
$result .= ' ' ;
}
2016-04-12 19:17:50 +02:00
}
2019-11-08 15:51:54 +01: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
*/
2019-02-25 20:35:59 +01:00
public function getLibStatut ( $mode = 0 )
2016-04-12 19:17:50 +02:00
{
2019-01-27 11:55:16 +01:00
return $this -> LibStatut ( $this -> status , $mode );
2016-04-12 19:17:50 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-12 19:17:50 +02:00
/**
* 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
*/
2019-02-25 20:35:59 +01:00
public function LibStatut ( $status , $mode = 0 )
2016-04-12 19:17:50 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-04-12 19:17:50 +02:00
global $langs ;
2021-02-26 12:03:07 +01:00
if ( empty ( $this -> labelStatus ) || empty ( $this -> labelStatusShort )) {
2019-10-31 21:09:33 +01:00
global $langs ;
//$langs->load("mymodule");
2021-10-16 19:37:57 +02:00
$this -> labelStatus [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Disabled' );
$this -> labelStatus [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
$this -> labelStatusShort [ self :: STATUS_DRAFT ] = $langs -> transnoentitiesnoconv ( 'Disabled' );
$this -> labelStatusShort [ self :: STATUS_VALIDATED ] = $langs -> transnoentitiesnoconv ( 'Enabled' );
2016-04-12 19:17:50 +02:00
}
2019-10-31 21:09:33 +01:00
$statusType = 'status5' ;
2021-02-26 12:03:07 +01:00
if ( $status == self :: STATUS_VALIDATED ) {
$statusType = 'status4' ;
}
2019-10-31 21:09:33 +01:00
2019-11-01 12:52:03 +01:00
return dolGetStatus ( $this -> labelStatus [ $status ], $this -> labelStatusShort [ $status ], '' , $statusType , $mode );
2016-04-12 19:17:50 +02:00
}
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 ()
{
2020-10-31 14:32:18 +01:00
global $user ;
2017-07-19 16:38:00 +02:00
2016-04-12 19:17:50 +02:00
$this -> id = 0 ;
2020-12-01 02:41:19 +01:00
$this -> specimen = 1 ;
2016-05-02 15:51:18 +02:00
$this -> entity = 1 ;
$this -> ref = 'myspecimenwebsite' ;
$this -> description = 'A specimen website' ;
2020-02-19 18:32:04 +01:00
$this -> lang = 'en' ;
2020-02-21 11:36:16 +01:00
$this -> otherlang = 'fr,es' ;
2020-11-24 22:31:03 +01:00
$this -> status = 1 ;
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' ;
2018-09-25 15:00:37 +02:00
$this -> fk_user_creat = $user -> id ;
2017-04-28 13:10:08 +02:00
$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-11-05 03:39:27 +01:00
/**
* Generate a zip with all data of web site .
*
2019-08-14 01:43:06 +02:00
* @ return string Path to file with zip or '' if error
2017-11-05 03:39:27 +01:00
*/
2019-02-25 20:35:59 +01:00
public function exportWebSite ()
2017-11-05 03:39:27 +01:00
{
2018-09-26 11:15:11 +02:00
global $conf , $mysoc ;
2017-11-05 03:39:27 +01:00
$website = $this ;
2021-02-26 12:03:07 +01:00
if ( empty ( $website -> id ) || empty ( $website -> ref )) {
2017-12-11 21:27:43 +01:00
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 );
2021-02-26 12:03:07 +01:00
if ( ! is_writable ( $conf -> website -> dir_temp )) {
2017-12-11 18:58:08 +01:00
setEventMessages ( " Temporary dir " . $conf -> website -> dir_temp . " is not writable " , null , 'errors' );
return '' ;
}
2018-07-23 14:08:20 +02:00
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref ;
dol_syslog ( " Clear temp dir " . $destdir );
2019-11-08 15:51:54 +01:00
$count = 0 ; $countreallydeleted = 0 ;
2018-07-23 14:08:20 +02:00
$counttodelete = dol_delete_dir_recursive ( $destdir , $count , 1 , 0 , $countreallydeleted );
2021-02-26 12:03:07 +01:00
if ( $counttodelete != $countreallydeleted ) {
2018-07-23 14:08:20 +02:00
setEventMessages ( " Failed to clean temp directory " . $destdir , null , 'errors' );
return '' ;
}
2017-11-05 03:39:27 +01:00
2019-11-08 15:51:54 +01:00
$arrayreplacementinfilename = array ();
$arrayreplacementincss = array ();
2019-08-19 18:25:27 +02:00
$arrayreplacementincss [ 'file=image/' . $website -> ref . '/' ] = " file=image/__WEBSITE_KEY__/ " ;
$arrayreplacementincss [ 'file=js/' . $website -> ref . '/' ] = " file=js/__WEBSITE_KEY__/ " ;
2019-08-19 18:06:27 +02:00
$arrayreplacementincss [ 'medias/image/' . $website -> ref . '/' ] = " medias/image/__WEBSITE_KEY__/ " ;
$arrayreplacementincss [ 'medias/js/' . $website -> ref . '/' ] = " medias/js/__WEBSITE_KEY__/ " ;
2019-11-29 23:19:23 +01:00
if ( $mysoc -> logo_small ) {
2020-10-31 14:32:18 +01:00
$arrayreplacementincss [ 'file=logos%2Fthumbs%2F' . $mysoc -> logo_small ] = " file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__ " ;
2019-11-29 23:19:23 +01:00
}
if ( $mysoc -> logo_mini ) {
2020-10-31 14:32:18 +01:00
$arrayreplacementincss [ 'file=logos%2Fthumbs%2F' . $mysoc -> logo_mini ] = " file=logos%2Fthumbs%2F__LOGO_MINI_KEY__ " ;
2019-11-29 23:19:23 +01:00
}
if ( $mysoc -> logo ) {
2020-10-31 14:32:18 +01:00
$arrayreplacementincss [ 'file=logos%2Fthumbs%2F' . $mysoc -> logo ] = " file=logos%2Fthumbs%2F__LOGO_KEY__ " ;
2019-11-29 23:19:23 +01:00
}
2017-11-05 03:39:27 +01:00
2020-05-03 14:58:53 +02:00
// Create output directories
2019-08-19 18:06:27 +02:00
dol_syslog ( " Create containers dir " );
dol_mkdir ( $conf -> website -> dir_temp . '/' . $website -> ref . '/containers' );
2020-05-03 14:58:53 +02:00
dol_mkdir ( $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/image/websitekey' );
dol_mkdir ( $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/js/websitekey' );
// Copy files into 'containers'
$srcdir = $conf -> website -> dir_output . '/' . $website -> ref ;
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/containers' ;
2019-08-19 18:06:27 +02:00
2017-12-11 21:27:43 +01:00
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
2020-05-03 14:58:53 +02:00
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacementinfilename , 2 );
2017-11-05 03:39:27 +01:00
2020-05-03 14:58:53 +02:00
// Copy files into medias/image
2017-12-11 21:27:43 +01:00
$srcdir = DOL_DATA_ROOT . '/medias/image/' . $website -> ref ;
2018-08-16 23:57:19 +02:00
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/image/websitekey' ;
2017-11-05 03:39:27 +01:00
2017-12-11 21:27:43 +01:00
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
2019-08-19 18:06:27 +02:00
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacementinfilename );
2017-11-05 03:39:27 +01:00
2020-05-03 14:58:53 +02:00
// Copy files into medias/js
2017-12-27 20:21:19 +01:00
$srcdir = DOL_DATA_ROOT . '/medias/js/' . $website -> ref ;
2018-08-16 23:57:19 +02:00
$destdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/medias/js/websitekey' ;
2017-12-27 20:21:19 +01:00
dol_syslog ( " Copy content from " . $srcdir . " into " . $destdir );
2019-08-19 18:06:27 +02:00
dolCopyDir ( $srcdir , $destdir , 0 , 1 , $arrayreplacementinfilename );
2017-12-27 20:21:19 +01:00
2020-05-03 14:58:53 +02:00
// Make some replacement into some files
2019-08-19 18:06:27 +02:00
$cssindestdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/containers/styles.css.php' ;
2019-08-19 18:25:27 +02:00
dolReplaceInFile ( $cssindestdir , $arrayreplacementincss );
2017-11-05 03:39:27 +01:00
2019-08-19 18:06:27 +02:00
$htmldeaderindestdir = $conf -> website -> dir_temp . '/' . $website -> ref . '/containers/htmlheader.html' ;
2019-08-19 18:25:27 +02:00
dolReplaceInFile ( $htmldeaderindestdir , $arrayreplacementincss );
2019-08-19 18:06:27 +02:00
// Build sql file
2017-12-11 21:27:43 +01:00
$filesql = $conf -> website -> dir_temp . '/' . $website -> ref . '/website_pages.sql' ;
2019-01-27 11:55:16 +01:00
$fp = fopen ( $filesql , " w " );
2021-02-26 12:03:07 +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
2019-11-08 15:51:54 +01:00
$i = 1 ;
2021-02-26 12:03:07 +01:00
foreach ( $listofpages as $pageid => $objectpageold ) {
2019-11-08 15:51:54 +01:00
$objectpageold -> newid = $i ;
2017-11-05 03:39:27 +01:00
$i ++ ;
}
2019-11-08 15:51:54 +01:00
$i = 1 ;
2021-02-26 12:03:07 +01:00
foreach ( $listofpages as $pageid => $objectpageold ) {
2017-11-05 03:39:27 +01:00
// Search newid
2019-11-08 15:51:54 +01:00
$newfk_page = 0 ;
2021-02-26 12:03:07 +01:00
foreach ( $listofpages as $pageid2 => $objectpageold2 ) {
if ( $pageid2 == $objectpageold -> fk_page ) {
2017-11-05 03:39:27 +01:00
$newfk_page = $objectpageold2 -> newid ;
break ;
}
}
2019-11-08 15:51:54 +01:00
$objectpageold -> newfk_page = $newfk_page ;
2017-11-05 03:39:27 +01:00
$i ++ ;
}
2021-02-26 12:03:07 +01:00
foreach ( $listofpages as $pageid => $objectpageold ) {
2018-08-16 23:57:19 +02:00
$allaliases = $objectpageold -> pageurl ;
2019-11-08 15:51:54 +01:00
$allaliases .= ( $objectpageold -> aliasalt ? ',' . $objectpageold -> aliasalt : '' );
2018-08-16 23:57:19 +02:00
2019-11-08 15:51:54 +01:00
$line = '-- Page ID ' . $objectpageold -> id . ' -> ' . $objectpageold -> newid . '__+MAX_llx_website_page__ - Aliases ' . $allaliases . ' --;' ; // newid start at 1, 2...
$line .= " \n " ;
2018-08-16 23:57:19 +02:00
fputs ( $fp , $line );
2018-08-16 21:48:16 +02:00
// Warning: We must keep llx_ here. It is a generic SQL.
2022-02-06 12:17:42 +01:00
$line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, lang, image, keywords, status, date_creation, tms, import_key, grabbed_from, type_container, htmlheader, content, author_alias, allowed_in_frames)' ;
2019-06-02 12:36:07 +02:00
2019-11-08 15:51:54 +01:00
$line .= " VALUES( " ;
$line .= $objectpageold -> newid . " __+MAX_llx_website_page__, " ;
$line .= ( $objectpageold -> newfk_page ? $this -> db -> escape ( $objectpageold -> newfk_page ) . " __+MAX_llx_website_page__ " : " null " ) . " , " ;
$line .= " __WEBSITE_ID__, " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> pageurl ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> aliasalt ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> title ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> description ) . " ', " ;
2020-02-19 18:32:04 +01:00
$line .= " ' " . $this -> db -> escape ( $objectpageold -> lang ) . " ', " ;
2019-11-08 15:51:54 +01:00
$line .= " ' " . $this -> db -> escape ( $objectpageold -> image ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> keywords ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> status ) . " ', " ;
$line .= " ' " . $this -> db -> idate ( $objectpageold -> date_creation ) . " ', " ;
$line .= " ' " . $this -> db -> idate ( $objectpageold -> date_modification ) . " ', " ;
$line .= ( $objectpageold -> import_key ? " ' " . $this -> db -> escape ( $objectpageold -> import_key ) . " ' " : " null " ) . " , " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> grabbed_from ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> type_container ) . " ', " ;
2018-08-20 21:00:45 +02:00
$stringtoexport = $objectpageold -> htmlheader ;
2019-11-08 15:51:54 +01:00
$stringtoexport = str_replace ( array ( " \r \n " , " \r " , " \n " ), " __N__ " , $stringtoexport );
2018-08-20 21:00:45 +02:00
$stringtoexport = str_replace ( 'file=image/' . $website -> ref . '/' , " file=image/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=js/' . $website -> ref . '/' , " file=js/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'medias/image/' . $website -> ref . '/' , " medias/image/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'medias/js/' . $website -> ref . '/' , " medias/js/__WEBSITE_KEY__/ " , $stringtoexport );
2018-09-26 11:15:11 +02:00
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo_small , " file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo_mini , " file=logos%2Fthumbs%2F__LOGO_MINI_KEY__ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo , " file=logos%2Fthumbs%2F__LOGO_KEY__ " , $stringtoexport );
2019-11-08 15:51:54 +01:00
$line .= " ' " . $this -> db -> escape ( str_replace ( array ( " \r \n " , " \r " , " \n " ), " __N__ " , $stringtoexport )) . " ', " ; // Replace \r \n to have record on 1 line
2018-08-20 21:00:45 +02:00
$stringtoexport = $objectpageold -> content ;
2019-11-08 15:51:54 +01:00
$stringtoexport = str_replace ( array ( " \r \n " , " \r " , " \n " ), " __N__ " , $stringtoexport );
2018-08-20 21:00:45 +02:00
$stringtoexport = str_replace ( 'file=image/' . $website -> ref . '/' , " file=image/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=js/' . $website -> ref . '/' , " file=js/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'medias/image/' . $website -> ref . '/' , " medias/image/__WEBSITE_KEY__/ " , $stringtoexport );
$stringtoexport = str_replace ( 'medias/js/' . $website -> ref . '/' , " medias/js/__WEBSITE_KEY__/ " , $stringtoexport );
2018-09-26 11:15:11 +02:00
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo_small , " file=logos%2Fthumbs%2F__LOGO_SMALL_KEY__ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo_mini , " file=logos%2Fthumbs%2F__LOGO_MINI_KEY__ " , $stringtoexport );
$stringtoexport = str_replace ( 'file=logos%2Fthumbs%2F' . $mysoc -> logo , " file=logos%2Fthumbs%2F__LOGO_KEY__ " , $stringtoexport );
2019-08-15 02:57:00 +02:00
// When we have a link src="image/websiteref/file.png" into html content
$stringtoexport = str_replace ( '="image/' . $website -> ref . '/' , '="image/__WEBSITE_KEY__/' , $stringtoexport );
2020-06-12 15:27:28 +02:00
$line .= " ' " . $this -> db -> escape ( $stringtoexport ) . " ', " ; // Replace \r \n to have record on 1 line
2022-02-06 12:17:42 +01:00
$line .= " ' " . $this -> db -> escape ( $objectpageold -> author_alias ) . " ', " ;
$line .= " ' " . $this -> db -> escape ( $objectpageold -> allowed_in_frames ) . " ' " ;
2019-11-08 15:51:54 +01:00
$line .= " ); " ;
$line .= " \n " ;
2022-02-06 12:17:42 +01:00
2017-11-05 03:39:27 +01:00
fputs ( $fp , $line );
2018-09-11 00:11:14 +02:00
// Add line to update home page id during import
//var_dump($this->fk_default_home.' - '.$objectpageold->id.' - '.$objectpageold->newid);exit;
2021-02-26 12:03:07 +01:00
if ( $this -> fk_default_home > 0 && ( $objectpageold -> id == $this -> fk_default_home ) && ( $objectpageold -> newid > 0 )) { // This is the record with home page
2020-10-31 14:32:18 +01:00
// Warning: We must keep llx_ here. It is a generic SQL.
$line = " UPDATE llx_website SET fk_default_home = " . ( $objectpageold -> newid > 0 ? $this -> db -> escape ( $objectpageold -> newid ) . " __+MAX_llx_website_page__ " : " null " ) . " WHERE rowid = __WEBSITE_ID__; " ;
2019-11-08 15:51:54 +01:00
$line .= " \n " ;
2018-09-11 00:11:14 +02:00
fputs ( $fp , $line );
}
2017-11-05 03:39:27 +01:00
}
2021-08-31 13:09:52 +02:00
$line = " \n -- For Dolibarr v14+ --; \n " ;
$line .= " UPDATE llx_website SET lang = ' " . $this -> db -> escape ( $this -> fk_default_lang ) . " ' WHERE rowid = __WEBSITE_ID__; \n " ;
2021-04-19 15:28:49 +02:00
$line .= " UPDATE llx_website SET otherlang = ' " . $this -> db -> escape ( $this -> otherlang ) . " ' WHERE rowid = __WEBSITE_ID__; \n " ;
2021-04-18 23:09:23 +02:00
$line .= " \n " ;
fputs ( $fp , $line );
2017-11-05 03:39:27 +01:00
fclose ( $fp );
2021-02-26 12:03:07 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2017-11-05 03:39:27 +01:00
@ chmod ( $filesql , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-26 12:03:07 +01:00
}
2017-11-05 03:39:27 +01:00
// Build zip file
2018-08-16 21:48:16 +02:00
$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' ;
2020-06-12 15:31:12 +02:00
$filename = $conf -> website -> dir_temp . '/' . $website -> ref . '/website_' . $website -> ref . '-' . dol_print_date ( dol_now (), 'dayhourlog' ) . '-V' . (( float ) DOL_VERSION ) . '.zip' ;
2017-11-05 03:39:27 +01:00
dol_delete_file ( $fileglob , 0 );
2019-08-14 01:43:06 +02:00
$result = dol_compress_file ( $filedir , $filename , 'zip' );
2017-11-05 03:39:27 +01:00
2021-02-26 12:03:07 +01:00
if ( $result > 0 ) {
2019-08-14 01:43:06 +02:00
return $filename ;
2021-02-26 12:03:07 +01:00
} else {
2019-08-14 01:43:06 +02:00
global $errormsg ;
$this -> error = $errormsg ;
return '' ;
}
2017-11-05 03:39:27 +01:00
}
/**
* 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
*/
2019-02-25 20:35:59 +01:00
public function importWebSite ( $pathtofile )
2017-11-05 03:39:27 +01:00
{
2018-09-26 11:15:11 +02:00
global $conf , $mysoc ;
2017-11-05 03:39:27 +01:00
2018-08-16 21:48:16 +02:00
$error = 0 ;
2017-11-05 03:39:27 +01:00
2018-08-16 21:48:16 +02:00
$object = $this ;
2021-02-26 12:03:07 +01:00
if ( empty ( $object -> ref )) {
2018-08-16 21:48:16 +02:00
$this -> error = 'Function importWebSite called on object not loaded (object->ref is empty)' ;
return - 1 ;
}
2021-08-28 03:09:18 +02:00
dol_delete_dir_recursive ( $conf -> website -> dir_temp . " / " . $object -> ref );
2019-10-09 15:01:19 +02:00
dol_mkdir ( $conf -> website -> dir_temp . '/' . $object -> ref );
2017-11-05 03:39:27 +01:00
$filename = basename ( $pathtofile );
2021-02-26 12:03:07 +01:00
if ( ! preg_match ( '/^website_(.*)-(.*)$/' , $filename , $reg )) {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Bad format for filename ' . $filename . '. Must be website_XXX-VERSION.' ;
2017-11-05 03:39:27 +01:00
return - 1 ;
}
2018-08-16 21:48:16 +02:00
$result = dol_uncompress ( $pathtofile , $conf -> website -> dir_temp . '/' . $object -> ref );
2019-10-09 15:01:19 +02:00
2021-02-26 12:03:07 +01:00
if ( ! empty ( $result [ 'error' ])) {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Failed to unzip file ' . $pathtofile . '.' ;
2018-08-16 21:48:16 +02:00
return - 1 ;
}
2017-11-05 03:39:27 +01:00
2019-08-19 18:25:27 +02:00
$arrayreplacement = array ();
$arrayreplacement [ '__WEBSITE_ID__' ] = $object -> id ;
$arrayreplacement [ '__WEBSITE_KEY__' ] = $object -> ref ;
2019-11-08 15:51:54 +01:00
$arrayreplacement [ '__N__' ] = $this -> db -> escape ( " \n " ); // Restore \n
2019-08-19 18:25:27 +02:00
$arrayreplacement [ '__LOGO_SMALL_KEY__' ] = $this -> db -> escape ( $mysoc -> logo_small );
$arrayreplacement [ '__LOGO_MINI_KEY__' ] = $this -> db -> escape ( $mysoc -> logo_mini );
$arrayreplacement [ '__LOGO_KEY__' ] = $this -> db -> escape ( $mysoc -> logo );
2017-11-05 03:39:27 +01:00
2019-08-19 18:25:27 +02:00
// Copy containers
2019-11-08 15:51:54 +01:00
dolCopyDir ( $conf -> website -> dir_temp . '/' . $object -> ref . '/containers' , $conf -> website -> dir_output . '/' . $object -> ref , 0 , 1 ); // Overwrite if exists
2017-11-05 03:39:27 +01:00
2019-08-19 18:25:27 +02:00
// Make replacement into css and htmlheader file
$cssindestdir = $conf -> website -> dir_output . '/' . $object -> ref . '/styles.css.php' ;
2019-11-08 15:51:54 +01:00
$result = dolReplaceInFile ( $cssindestdir , $arrayreplacement );
2019-08-19 18:25:27 +02:00
$htmldeaderindestdir = $conf -> website -> dir_output . '/' . $object -> ref . '/htmlheader.html' ;
$result = dolReplaceInFile ( $htmldeaderindestdir , $arrayreplacement );
2018-09-10 22:58:20 +02:00
// Now generate the master.inc.php page
2019-11-08 15:51:54 +01:00
$filemaster = $conf -> website -> dir_output . '/' . $object -> ref . '/master.inc.php' ;
2018-09-10 23:23:44 +02:00
$result = dolSaveMasterFile ( $filemaster );
2021-02-26 12:03:07 +01:00
if ( ! $result ) {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Failed to write file ' . $filemaster ;
2018-09-10 23:20:31 +02:00
$error ++ ;
}
2018-09-10 22:58:20 +02:00
2019-11-08 15:51:54 +01:00
dolCopyDir ( $conf -> website -> dir_temp . '/' . $object -> ref . '/medias/image/websitekey' , $conf -> website -> dir_output . '/' . $object -> ref . '/medias/image/' . $object -> ref , 0 , 1 ); // Medias can be shared, do not overwrite if exists
dolCopyDir ( $conf -> website -> dir_temp . '/' . $object -> ref . '/medias/js/websitekey' , $conf -> website -> dir_output . '/' . $object -> ref . '/medias/js/' . $object -> ref , 0 , 1 ); // Medias can be shared, do not overwrite if exists
2018-08-16 21:48:16 +02:00
2021-08-28 03:09:18 +02:00
$sqlfile = $conf -> website -> dir_temp . " / " . $object -> ref . '/website_pages.sql' ;
2018-08-16 21:48:16 +02:00
$result = dolReplaceInFile ( $sqlfile , $arrayreplacement );
$this -> db -> begin ();
2018-09-26 11:28:41 +02:00
// Search the $maxrowid because we need it later
2019-11-08 15:51:54 +01:00
$sqlgetrowid = 'SELECT MAX(rowid) as max from ' . MAIN_DB_PREFIX . 'website_page' ;
$resql = $this -> db -> query ( $sqlgetrowid );
2021-02-26 12:03:07 +01:00
if ( $resql ) {
2019-11-08 15:51:54 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$maxrowid = $obj -> max ;
2018-08-16 23:57:19 +02:00
}
2018-09-26 11:28:41 +02:00
// Load sql record
2019-11-08 15:51:54 +01:00
$runsql = run_sql ( $sqlfile , 1 , '' , 0 , '' , 'none' , 0 , 1 ); // The maxrowid of table is searched into this function two
2021-02-26 12:03:07 +01:00
if ( $runsql <= 0 ) {
2019-11-08 15:51:54 +01:00
$this -> errors [] = 'Failed to load sql file ' . $sqlfile ;
2018-08-16 21:48:16 +02:00
$error ++ ;
}
2018-08-16 23:57:19 +02:00
$objectpagestatic = new WebsitePage ( $this -> db );
// Make replacement of IDs
2019-01-27 11:55:16 +01:00
$fp = fopen ( $sqlfile , " r " );
2021-02-26 12:03:07 +01:00
if ( $fp ) {
while ( ! feof ( $fp )) {
2019-08-16 02:17:33 +02:00
$reg = array ();
2018-08-16 23:57:19 +02:00
// Warning fgets with second parameter that is null or 0 hang.
$buf = fgets ( $fp , 65000 );
2021-02-26 12:03:07 +01:00
if ( preg_match ( '/^-- Page ID (\d+)\s[^\s]+\s(\d+).*Aliases\s(.*)\s--;/i' , $buf , $reg )) {
2018-08-16 23:57:19 +02:00
$oldid = $reg [ 1 ];
$newid = ( $reg [ 2 ] + $maxrowid );
$aliasesarray = explode ( ',' , $reg [ 3 ]);
dol_syslog ( " Found ID " . $oldid . " to replace with ID " . $newid . " and shortcut aliases to create: " . $reg [ 3 ]);
dol_move ( $conf -> website -> dir_output . '/' . $object -> ref . '/page' . $oldid . '.tpl.php' , $conf -> website -> dir_output . '/' . $object -> ref . '/page' . $newid . '.tpl.php' , 0 , 1 , 0 , 0 );
2020-05-20 13:19:32 +02:00
$objectpagestatic -> fetch ( $newid );
2018-09-26 11:28:41 +02:00
// The move is not enough, so we regenerate page
2019-11-08 15:51:54 +01:00
$filetpl = $conf -> website -> dir_output . '/' . $object -> ref . '/page' . $newid . '.tpl.php' ;
2020-02-21 11:36:16 +01:00
$result = dolSavePageContent ( $filetpl , $object , $objectpagestatic );
if ( ! $result ) {
$this -> errors [] = 'Failed to write file ' . basename ( $filetpl );
$error ++ ;
}
2018-09-26 11:28:41 +02:00
// Regenerate alternative aliases pages
2021-02-26 12:03:07 +01:00
if ( is_array ( $aliasesarray )) {
foreach ( $aliasesarray as $aliasshortcuttocreate ) {
if ( trim ( $aliasshortcuttocreate )) {
2019-11-08 15:51:54 +01:00
$filealias = $conf -> website -> dir_output . '/' . $object -> ref . '/' . trim ( $aliasshortcuttocreate ) . '.php' ;
2020-02-21 11:36:16 +01:00
$result = dolSavePageAlias ( $filealias , $object , $objectpagestatic );
if ( ! $result ) {
$this -> errors [] = 'Failed to write file ' . basename ( $filealias );
$error ++ ;
}
2019-08-16 02:31:06 +02:00
}
2019-08-16 02:17:33 +02:00
}
2018-08-16 23:57:19 +02:00
}
}
}
}
2018-08-16 21:48:16 +02:00
2020-02-06 23:35:04 +01:00
// Read record of website that has been updated by the run_sql function previously called so we can get the
// value of fk_default_home that is ID of home page
2021-09-30 15:59:47 +02:00
$sql = " SELECT fk_default_home FROM " . MAIN_DB_PREFIX . " website WHERE rowid = " . (( int ) $object -> id );
2020-02-06 23:35:04 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
$object -> fk_default_home = $obj -> fk_default_home ;
} else {
//$this->errors[] = 'Failed to get the Home page';
//$error++;
}
}
// Regenerate index page to point to the new index page
2018-12-04 12:40:36 +01:00
$pathofwebsite = $conf -> website -> dir_output . '/' . $object -> ref ;
dolSaveIndexPage ( $pathofwebsite , $pathofwebsite . '/index.php' , $pathofwebsite . '/page' . $object -> fk_default_home . '.tpl.php' , $pathofwebsite . '/wrapper.php' );
2021-02-26 12:03:07 +01:00
if ( $error ) {
2018-08-16 21:48:16 +02:00
$this -> db -> rollback ();
return - 1 ;
2021-02-26 12:03:07 +01:00
} else {
2018-08-16 21:48:16 +02:00
$this -> db -> commit ();
return $object -> id ;
}
2017-11-05 03:39:27 +01:00
}
2018-10-03 18:01:27 +02:00
2020-05-20 13:19:32 +02:00
/**
2021-05-20 10:34:49 +02:00
* Rebuild all files of a containers of a website . Rebuild also the wrapper . php file . TODO Add other files too .
2020-05-20 13:19:32 +02:00
* Note : Files are already regenerated during importWebSite so this function is useless when importing a website .
*
2020-06-09 21:43:42 +02:00
* @ return int < 0 if KO , >= 0 if OK
2020-05-20 13:19:32 +02:00
*/
public function rebuildWebSiteFiles ()
{
global $conf ;
$error = 0 ;
$object = $this ;
2021-02-26 12:03:07 +01:00
if ( empty ( $object -> ref )) {
2021-01-24 13:04:02 +01:00
$this -> error = 'Function rebuildWebSiteFiles called on object not loaded (object->ref is empty)' ;
2020-05-20 13:19:32 +02:00
return - 1 ;
}
$objectpagestatic = new WebsitePage ( $this -> db );
2021-09-30 15:59:47 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " website_page WHERE fk_website = " . (( int ) $this -> id );
2020-05-20 13:19:32 +02:00
$resql = $this -> db -> query ( $sql );
2020-10-31 14:32:18 +01:00
if ( ! $resql ) {
2020-05-20 13:19:32 +02:00
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
$num = $this -> db -> num_rows ( $resql );
2021-01-24 13:27:52 +01:00
// Loop on each container/page
2020-10-31 14:32:18 +01:00
$i = 0 ;
2020-05-20 13:19:32 +02:00
while ( $i < $num ) {
$obj = $this -> db -> fetch_object ( $resql );
$newid = $obj -> rowid ;
$objectpagestatic -> fetch ( $newid );
$aliasesarray = explode ( ',' , $objectpagestatic -> aliasalt );
$filetpl = $conf -> website -> dir_output . '/' . $object -> ref . '/page' . $newid . '.tpl.php' ;
$result = dolSavePageContent ( $filetpl , $object , $objectpagestatic );
if ( ! $result ) {
$this -> errors [] = 'Failed to write file ' . basename ( $filetpl );
$error ++ ;
}
2021-01-24 14:31:29 +01:00
// Add main alias to list of alternative aliases
if ( ! empty ( $objectpagestatic -> pageurl ) && ! in_array ( $objectpagestatic -> pageurl , $aliasesarray )) {
$aliasesarray [] = $objectpagestatic -> pageurl ;
}
// Regenerate all aliases pages (pages with a natural name)
if ( is_array ( $aliasesarray )) {
foreach ( $aliasesarray as $aliasshortcuttocreate ) {
if ( trim ( $aliasshortcuttocreate )) {
2020-05-20 13:19:32 +02:00
$filealias = $conf -> website -> dir_output . '/' . $object -> ref . '/' . trim ( $aliasshortcuttocreate ) . '.php' ;
$result = dolSavePageAlias ( $filealias , $object , $objectpagestatic );
if ( ! $result ) {
$this -> errors [] = 'Failed to write file ' . basename ( $filealias );
$error ++ ;
}
}
}
}
$i ++ ;
}
2021-05-20 10:34:49 +02:00
if ( ! $error ) {
// Save wrapper.php
$pathofwebsite = $conf -> website -> dir_output . '/' . $object -> ref ;
$filewrapper = $pathofwebsite . '/wrapper.php' ;
dolSaveIndexPage ( $pathofwebsite , '' , '' , $filewrapper );
}
2020-11-24 22:31:03 +01:00
if ( $error ) {
2020-05-20 13:19:32 +02:00
return - 1 ;
2020-06-09 22:09:57 +02:00
} else {
2020-06-09 21:43:42 +02:00
return $num ;
2020-05-20 13:19:32 +02:00
}
}
2019-12-07 15:46:14 +01:00
/**
* Return if web site is a multilanguage web site . Return false if there is only 0 or 1 language .
*
* @ return boolean True if web site is a multilanguage web site
*/
public function isMultiLang ()
{
2020-03-06 13:44:15 +01:00
return ( empty ( $this -> otherlang ) ? false : true );
2019-12-07 15:46:14 +01:00
}
2018-10-03 18:01:27 +02:00
/**
2018-10-14 20:54:28 +02:00
* Component to select language inside a container ( Full CSS Only )
2018-10-03 18:01:27 +02:00
*
2020-02-27 21:43:11 +01:00
* @ param array | string $languagecodes 'auto' to show all languages available for page , or language codes array like array ( 'en' , 'fr' , 'de' , 'es' )
2018-10-14 20:54:28 +02:00
* @ param Translate $weblangs Language Object
* @ param string $morecss More CSS class on component
* @ param string $htmlname Suffix for HTML name
* @ return string HTML select component
2018-10-03 18:01:27 +02:00
*/
2019-01-27 15:20:16 +01:00
public function componentSelectLang ( $languagecodes , $weblangs , $morecss = '' , $htmlname = '' )
2018-10-03 18:01:27 +02:00
{
2018-10-14 20:54:28 +02:00
global $websitepagefile , $website ;
2018-10-14 20:04:25 +02:00
2021-02-26 12:03:07 +01:00
if ( ! is_object ( $weblangs )) {
return 'ERROR componentSelectLang called with parameter $weblangs not defined' ;
}
2018-10-03 18:01:27 +02:00
2020-02-27 21:43:11 +01:00
$arrayofspecialmainlanguages = array (
'en' => 'en_US' ,
'sq' => 'sq_AL' ,
'ar' => 'ar_SA' ,
'eu' => 'eu_ES' ,
'bn' => 'bn_DB' ,
'bs' => 'bs_BA' ,
'ca' => 'ca_ES' ,
2021-02-10 03:12:19 +01:00
'zh' => 'zh_CN' ,
2020-02-27 21:43:11 +01:00
'cs' => 'cs_CZ' ,
'da' => 'da_DK' ,
'et' => 'et_EE' ,
'ka' => 'ka_GE' ,
'el' => 'el_GR' ,
'he' => 'he_IL' ,
'kn' => 'kn_IN' ,
'km' => 'km_KH' ,
'ko' => 'ko_KR' ,
'lo' => 'lo_LA' ,
'nb' => 'nb_NO' ,
'fa' => 'fa_IR' ,
'sr' => 'sr_RS' ,
'sl' => 'sl_SI' ,
'uk' => 'uk_UA' ,
'vi' => 'vi_VN'
);
2018-10-14 20:54:28 +02:00
// Load tmppage if we have $websitepagefile defined
2019-11-08 15:51:54 +01:00
$tmppage = new WebsitePage ( $this -> db );
2018-10-14 20:54:28 +02:00
$pageid = 0 ;
2021-02-26 12:03:07 +01:00
if ( ! empty ( $websitepagefile )) {
2020-10-31 14:32:18 +01:00
$websitepagefileshort = basename ( $websitepagefile );
2021-02-26 12:03:07 +01:00
if ( $websitepagefileshort == 'index.php' ) {
$pageid = $website -> fk_default_home ;
} else {
$pageid = str_replace ( array ( '.tpl.php' , 'page' ), array ( '' , '' ), $websitepagefileshort );
}
if ( $pageid > 0 ) {
2018-10-14 20:04:25 +02:00
$tmppage -> fetch ( $pageid );
2018-10-14 20:54:28 +02:00
}
}
2020-02-27 21:43:11 +01:00
// Fill $languagecodes array with existing translation, nothing if none
2021-02-26 12:03:07 +01:00
if ( ! is_array ( $languagecodes ) && $pageid > 0 ) {
2018-10-14 20:54:28 +02:00
$languagecodes = array ();
2019-11-08 15:51:54 +01:00
$sql = " SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " website_page as wp " ;
2021-03-30 19:12:07 +02:00
$sql .= " WHERE wp.fk_website = " . (( int ) $website -> id );
2021-03-22 13:31:06 +01:00
$sql .= " AND (wp.fk_page = " . (( int ) $pageid ) . " OR wp.rowid = " . (( int ) $pageid );
2021-02-26 12:03:07 +01:00
if ( $tmppage -> fk_page > 0 ) {
2021-03-30 19:12:07 +02:00
$sql .= " OR wp.fk_page = " . (( int ) $tmppage -> fk_page ) . " OR wp.rowid = " . (( int ) $tmppage -> fk_page );
2021-02-26 12:03:07 +01:00
}
2019-11-08 15:51:54 +01:00
$sql .= " ) " ;
2018-10-14 20:54:28 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 12:03:07 +01:00
if ( $resql ) {
while ( $obj = $this -> db -> fetch_object ( $resql )) {
2018-10-14 20:54:28 +02:00
$newlang = $obj -> lang ;
2021-02-26 12:03:07 +01:00
if ( $obj -> rowid == $pageid ) {
$newlang = $obj -> lang ;
}
if ( ! in_array ( $newlang , $languagecodes )) {
$languagecodes [] = $newlang ;
}
2018-10-14 20:54:28 +02:00
}
}
}
2020-02-27 21:43:11 +01:00
// Now $languagecodes is always an array. Example array('en', 'fr', 'es');
2018-10-14 20:54:28 +02:00
2020-02-27 21:43:11 +01:00
$languagecodeselected = substr ( $weblangs -> defaultlang , 0 , 2 ); // Because we must init with a value, but real value is the lang of main parent container
2021-02-26 12:03:07 +01:00
if ( ! empty ( $websitepagefile )) {
2018-10-14 20:54:28 +02:00
$pageid = str_replace ( array ( '.tpl.php' , 'page' ), array ( '' , '' ), basename ( $websitepagefile ));
2021-02-26 12:03:07 +01:00
if ( $pageid > 0 ) {
2020-02-27 21:43:11 +01:00
$pagelang = substr ( $tmppage -> lang , 0 , 2 );
$languagecodeselected = substr ( $pagelang , 0 , 2 );
2021-02-26 12:03:07 +01:00
if ( ! in_array ( $pagelang , $languagecodes )) {
$languagecodes [] = $pagelang ; // We add language code of page into combo list
}
2018-10-14 20:04:25 +02:00
}
}
2018-10-03 20:00:52 +02:00
$weblangs -> load ( 'languages' );
2018-10-14 20:04:25 +02:00
//var_dump($weblangs->defaultlang);
2018-10-03 20:00:52 +02:00
2018-10-03 18:01:27 +02:00
$url = $_SERVER [ " REQUEST_URI " ];
2019-11-08 15:51:54 +01:00
$url = preg_replace ( '/(\?|&)l=([a-zA-Z_]*)/' , '' , $url ); // We remove param l from url
2018-10-03 18:01:27 +02:00
//$url = preg_replace('/(\?|&)lang=([a-zA-Z_]*)/', '', $url); // We remove param lang from url
2019-11-08 15:51:54 +01:00
$url .= ( preg_match ( '/\?/' , $url ) ? '&' : '?' ) . 'l=' ;
2021-02-26 12:03:07 +01:00
if ( ! preg_match ( '/^\//' , $url )) {
$url = '/' . $url ;
}
2018-10-03 18:01:27 +02:00
2019-11-08 15:51:54 +01:00
$HEIGHTOPTION = 40 ;
2018-10-03 18:01:27 +02:00
$MAXHEIGHT = 4 * $HEIGHTOPTION ;
$nboflanguage = count ( $languagecodes );
2019-11-08 15:51:54 +01:00
$out = '<!-- componentSelectLang' . $htmlname . ' -->' . " \n " ;
2018-10-15 13:40:22 +02:00
2019-11-08 15:51:54 +01:00
$out .= '<style>' ;
$out .= '.componentSelectLang' . $htmlname . ':hover { height: ' . min ( $MAXHEIGHT , ( $HEIGHTOPTION * $nboflanguage )) . 'px; overflow-x: hidden; overflow-y: ' . ((( $HEIGHTOPTION * $nboflanguage ) > $MAXHEIGHT ) ? ' scroll' : 'hidden' ) . '; }' . " \n " ;
$out .= '.componentSelectLang' . $htmlname . ' li { line-height: ' . $HEIGHTOPTION . 'px; }' . " \n " ;
$out .= '.componentSelectLang' . $htmlname . ' {
2018-10-03 18:01:27 +02:00
display : inline - block ;
padding : 0 ;
height : '.$HEIGHTOPTION.' px ;
overflow : hidden ;
transition : all . 3 s ease ;
2019-03-29 22:31:52 +01:00
margin : 0 0 0 0 ;
2018-10-03 18:01:27 +02:00
vertical - align : top ;
}
. componentSelectLang '.$htmlname.' : hover , . componentSelectLang '.$htmlname.' : hover a { background - color : #fff; color: #000 !important; }
ul . componentSelectLang '.$htmlname.' { width : 150 px ; }
ul . componentSelectLang '.$htmlname.' : hover . fa { visibility : hidden ; }
. componentSelectLang '.$htmlname.' a { text - decoration : none ; width : 100 % ; }
2020-02-27 23:11:39 +01:00
. componentSelectLang '.$htmlname.' li { display : block ; padding : 0 px 15 px ; margin - left : 0 ; margin - right : 0 ; }
2018-10-03 18:01:27 +02:00
. componentSelectLang '.$htmlname.' li : hover { background - color : #EEE; }
' ;
2019-11-08 15:51:54 +01:00
$out .= '</style>' ;
$out .= '<ul class="componentSelectLang' . $htmlname . ( $morecss ? ' ' . $morecss : '' ) . '">' ;
2020-02-27 21:43:11 +01:00
2021-02-26 12:03:07 +01:00
if ( $languagecodeselected ) {
2020-02-27 21:43:11 +01:00
// Convert $languagecodeselected into a long language code
if ( strlen ( $languagecodeselected ) == 2 ) {
$languagecodeselected = ( empty ( $arrayofspecialmainlanguages [ $languagecodeselected ]) ? $languagecodeselected . '_' . strtoupper ( $languagecodeselected ) : $arrayofspecialmainlanguages [ $languagecodeselected ]);
}
$countrycode = strtolower ( substr ( $languagecodeselected , - 2 ));
2018-10-03 20:58:45 +02:00
$label = $weblangs -> trans ( " Language_ " . $languagecodeselected );
2021-02-26 12:03:07 +01:00
if ( $countrycode == 'us' ) {
$label = preg_replace ( '/\s*\(.*\)/' , '' , $label );
}
2020-06-06 20:11:01 +02:00
$out .= '<a href="' . $url . substr ( $languagecodeselected , 0 , 2 ) . '"><li><img height="12px" src="/medias/image/common/flags/' . $countrycode . '.png" style="margin-right: 5px;"/><span class="websitecomponentlilang">' . $label . '</span>' ;
2019-11-08 15:51:54 +01:00
$out .= '<span class="fa fa-caret-down" style="padding-left: 5px;" />' ;
$out .= '</li></a>' ;
2018-10-03 18:01:27 +02:00
}
2019-11-08 15:51:54 +01:00
$i = 0 ;
2021-02-26 12:03:07 +01:00
if ( is_array ( $languagecodes )) {
foreach ( $languagecodes as $languagecode ) {
2020-10-31 14:32:18 +01:00
// Convert $languagecode into a long language code
if ( strlen ( $languagecode ) == 2 ) {
$languagecode = ( empty ( $arrayofspecialmainlanguages [ $languagecode ]) ? $languagecode . '_' . strtoupper ( $languagecode ) : $arrayofspecialmainlanguages [ $languagecode ]);
}
2021-02-26 12:03:07 +01:00
if ( $languagecode == $languagecodeselected ) {
continue ; // Already output
}
2018-10-03 18:01:27 +02:00
2020-10-31 14:32:18 +01:00
$countrycode = strtolower ( substr ( $languagecode , - 2 ));
$label = $weblangs -> trans ( " Language_ " . $languagecode );
2021-02-26 12:03:07 +01:00
if ( $countrycode == 'us' ) {
$label = preg_replace ( '/\s*\(.*\)/' , '' , $label );
}
2020-10-31 14:32:18 +01:00
$out .= '<a href="' . $url . substr ( $languagecode , 0 , 2 ) . '"><li><img height="12px" src="/medias/image/common/flags/' . $countrycode . '.png" style="margin-right: 5px;"/><span class="websitecomponentlilang">' . $label . '</span>' ;
2021-02-26 12:03:07 +01:00
if ( empty ( $i ) && empty ( $languagecodeselected )) {
$out .= '<span class="fa fa-caret-down" style="padding-left: 5px;" />' ;
}
2020-10-31 14:32:18 +01:00
$out .= '</li></a>' ;
$i ++ ;
}
}
$out .= '</ul>' ;
return $out ;
}
2016-04-12 19:17:50 +02:00
}